Files
lighthouse/slasher/src/lib.rs
Eitan Seri-Levi 70bcba1e6b Redb slasher backend impl (#4529)
* initial redb impl

* redb impl

* remove phantom data

* fixed table definition

* fighting the borrow checker

* a rough draft that doesnt cause lifetime issues

* refactoring

* refactor

* refactor

* passing unit tests

* refactor

* refactor

* refactor

* commit

* move everything to one database

* remove panics, ready for a review

* merge

* a working redb impl

* passing a ref of txn to cursor

* this tries to create a second write transaction when initializing cursor. breaks everything

* Use 2 lifetimes and subtyping

Also fixes a bug in last_key caused by rev and next_back cancelling out

* Move table into cursor

* Merge remote-tracking branch 'origin/unstable' into redb-slasher-backend-impl

* changes based on feedback

* update lmdb

* fix lifetime issues

* moving everything from Cursor to Transaction

* update

* upgrade to redb 2.0

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into redb-slasher-backend-impl

* bring back cursor

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into redb-slasher-backend-impl

* fix delete while

* linting

* linting

* switch to lmdb

* update redb to v2.1

* build fixes, remove unwrap or default

* another build error

* hopefully this is the last build error

* fmt

* cargo.toml

* fix mdbx

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into redb-slasher-backend-impl

* Remove a collect

* Merge remote-tracking branch 'origin/unstable' into redb-slasher-backend-impl

* Merge branch 'redb-slasher-backend-impl' of https://github.com/eserilev/lighthouse into redb-slasher-backend-impl

* re-enable test

* fix failing slasher test

* Merge remote-tracking branch 'origin/unstable' into redb-slasher-backend-impl

* Rename DB file to `slasher.redb`
2024-07-01 01:36:40 +00:00

94 lines
3.5 KiB
Rust

#![deny(missing_debug_implementations)]
#![cfg_attr(
not(any(feature = "mdbx", feature = "lmdb", feature = "redb")),
allow(unused, clippy::drop_non_drop)
)]
mod array;
mod attestation_queue;
mod attester_record;
mod batch_stats;
mod block_queue;
pub mod config;
mod database;
mod error;
pub mod metrics;
mod migrate;
mod slasher;
pub mod test_utils;
pub use crate::slasher::Slasher;
pub use attestation_queue::{AttestationBatch, AttestationQueue, SimpleBatch};
pub use attester_record::{AttesterRecord, CompactAttesterRecord, IndexedAttesterRecord};
pub use block_queue::BlockQueue;
pub use config::{Config, DatabaseBackend, DatabaseBackendOverride};
pub use database::{
interface::{Database, Environment, RwTransaction},
IndexedAttestationId, SlasherDB,
};
pub use error::Error;
use types::{AttesterSlashing, AttesterSlashingBase, AttesterSlashingElectra};
use types::{EthSpec, IndexedAttestation, ProposerSlashing};
#[derive(Debug, PartialEq)]
pub enum AttesterSlashingStatus<E: EthSpec> {
NotSlashable,
/// A weird outcome that can occur when we go to lookup an attestation by its target
/// epoch for a surround slashing, but find a different attestation -- indicating that
/// the validator has already been caught double voting.
AlreadyDoubleVoted,
DoubleVote(Box<IndexedAttestation<E>>),
SurroundsExisting(Box<IndexedAttestation<E>>),
SurroundedByExisting(Box<IndexedAttestation<E>>),
}
#[derive(Debug, PartialEq)]
pub enum ProposerSlashingStatus {
NotSlashable,
DoubleVote(Box<ProposerSlashing>),
}
impl<E: EthSpec> AttesterSlashingStatus<E> {
pub fn into_slashing(
self,
new_attestation: &IndexedAttestation<E>,
) -> Option<AttesterSlashing<E>> {
use AttesterSlashingStatus::*;
// The surrounding attestation must be in `attestation_1` to be valid.
match self {
NotSlashable => None,
AlreadyDoubleVoted => None,
DoubleVote(existing) | SurroundedByExisting(existing) => {
match (&*existing, new_attestation) {
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: existing_att.clone(),
attestation_2: new.clone(),
}))
}
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: existing.clone().to_electra(),
attestation_2: new_attestation.clone().to_electra(),
})),
}
}
SurroundsExisting(existing) => match (&*existing, new_attestation) {
(IndexedAttestation::Base(existing_att), IndexedAttestation::Base(new)) => {
Some(AttesterSlashing::Base(AttesterSlashingBase {
attestation_1: new.clone(),
attestation_2: existing_att.clone(),
}))
}
// A slashing involving an electra attestation type must return an `AttesterSlashingElectra` type
(_, _) => Some(AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: new_attestation.clone().to_electra(),
attestation_2: existing.clone().to_electra(),
})),
},
}
}
}