From eb78e1d596982b83e9fd42a1a35cc8508d702081 Mon Sep 17 00:00:00 2001 From: realbigsean Date: Mon, 17 Jun 2024 16:12:20 -0400 Subject: [PATCH 1/3] indexed att on disk --- consensus/types/src/indexed_attestation.rs | 32 ++++++++++++++++++++++ consensus/types/src/lib.rs | 3 +- slasher/src/database.rs | 15 +++++----- slasher/src/migrate.rs | 7 ++++- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/consensus/types/src/indexed_attestation.rs b/consensus/types/src/indexed_attestation.rs index 00cac09abf3..429d65898a9 100644 --- a/consensus/types/src/indexed_attestation.rs +++ b/consensus/types/src/indexed_attestation.rs @@ -239,6 +239,38 @@ mod quoted_variable_list_u64 { } } +#[derive(Debug, Clone, Encode, Decode, PartialEq)] +#[ssz(enum_behaviour = "union")] +pub enum IndexedAttestationOnDisk { + Base(IndexedAttestationBase), + Electra(IndexedAttestationElectra), +} + +#[derive(Debug, Clone, Encode, PartialEq)] +#[ssz(enum_behaviour = "union")] +pub enum IndexedAttestationRefOnDisk<'a, E: EthSpec> { + Base(&'a IndexedAttestationBase), + Electra(&'a IndexedAttestationElectra), +} + +impl<'a, E: EthSpec> From<&'a IndexedAttestation> for IndexedAttestationRefOnDisk<'a, E> { + fn from(attestation: &'a IndexedAttestation) -> Self { + match attestation { + IndexedAttestation::Base(attestation) => Self::Base(attestation), + IndexedAttestation::Electra(attestation) => Self::Electra(attestation), + } + } +} + +impl From> for IndexedAttestation { + fn from(attestation: IndexedAttestationOnDisk) -> Self { + match attestation { + IndexedAttestationOnDisk::Base(attestation) => Self::Base(attestation), + IndexedAttestationOnDisk::Electra(attestation) => Self::Electra(attestation), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 716c8314788..213e09699d5 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -178,7 +178,8 @@ pub use crate::fork_versioned_response::{ForkVersionDeserialize, ForkVersionedRe pub use crate::graffiti::{Graffiti, GRAFFITI_BYTES_LEN}; pub use crate::historical_batch::HistoricalBatch; pub use crate::indexed_attestation::{ - IndexedAttestation, IndexedAttestationBase, IndexedAttestationElectra, IndexedAttestationRef, + IndexedAttestation, IndexedAttestationBase, IndexedAttestationElectra, + IndexedAttestationOnDisk, IndexedAttestationRef, IndexedAttestationRefOnDisk, }; pub use crate::light_client_bootstrap::{ LightClientBootstrap, LightClientBootstrapAltair, LightClientBootstrapCapella, diff --git a/slasher/src/database.rs b/slasher/src/database.rs index 16a67cd684f..56a9dd09244 100644 --- a/slasher/src/database.rs +++ b/slasher/src/database.rs @@ -18,12 +18,12 @@ use std::marker::PhantomData; use std::sync::Arc; use tree_hash::TreeHash; use types::{ - Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationBase, ProposerSlashing, + Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRefOnDisk, ProposerSlashing, SignedBeaconBlockHeader, Slot, }; /// Current database schema version, to check compatibility of on-disk DB with software. -pub const CURRENT_SCHEMA_VERSION: u64 = 3; +pub const CURRENT_SCHEMA_VERSION: u64 = 4; /// Metadata about the slashing database itself. const METADATA_DB: &str = "metadata"; @@ -458,7 +458,9 @@ impl SlasherDB { }; let attestation_key = IndexedAttestationId::new(indexed_att_id); - let data = indexed_attestation.as_ssz_bytes(); + let indexed_attestation_on_disk: IndexedAttestationRefOnDisk = + indexed_attestation.into(); + let data = indexed_attestation_on_disk.as_ssz_bytes(); cursor.put(attestation_key.as_ref(), &data)?; drop(cursor); @@ -482,11 +484,8 @@ impl SlasherDB { .ok_or(Error::MissingIndexedAttestation { id: indexed_attestation_id.as_u64(), })?; - // TODO(electra): make slasher fork-aware and return correct variant of attestation. Both - // Base and Electra variant can the same SSZ encoding, however the smaller list maximum of - // Base can error with an Electra attestation with heavy participation. - let indexed_attestation: IndexedAttestationBase = ssz_decode(bytes)?; - Ok(IndexedAttestation::Base(indexed_attestation)) + let indexed_attestation: IndexedAttestationOnDisk = ssz_decode(bytes)?; + Ok(indexed_attestation.into()) } fn get_attestation_data_root( diff --git a/slasher/src/migrate.rs b/slasher/src/migrate.rs index 674ab9c132d..61122907d5a 100644 --- a/slasher/src/migrate.rs +++ b/slasher/src/migrate.rs @@ -1,5 +1,6 @@ use crate::{database::CURRENT_SCHEMA_VERSION, Error, SlasherDB}; -use types::EthSpec; +use ssz::{Decode, Encode}; +use types::{EthSpec, IndexedAttestationBase, IndexedAttestationOnDisk}; impl SlasherDB { /// If the database exists, and has a schema, attempt to migrate it to the current version. @@ -17,6 +18,10 @@ impl SlasherDB { software_schema_version: CURRENT_SCHEMA_VERSION, }), (x, y) if x == y => Ok(self), + (3, 4) => { + // TODO migration + Ok(self) + } (_, _) => Err(Error::IncompatibleSchemaVersion { database_schema_version: schema_version, software_schema_version: CURRENT_SCHEMA_VERSION, From 0044d60835e84627252c048f5bbf6926493afb5c Mon Sep 17 00:00:00 2001 From: realbigsean Date: Mon, 17 Jun 2024 16:16:25 -0400 Subject: [PATCH 2/3] fix lints --- slasher/src/database.rs | 4 ++-- slasher/src/migrate.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/slasher/src/database.rs b/slasher/src/database.rs index 56a9dd09244..16089706a00 100644 --- a/slasher/src/database.rs +++ b/slasher/src/database.rs @@ -18,8 +18,8 @@ use std::marker::PhantomData; use std::sync::Arc; use tree_hash::TreeHash; use types::{ - Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRefOnDisk, ProposerSlashing, - SignedBeaconBlockHeader, Slot, + Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationOnDisk, + IndexedAttestationRefOnDisk, ProposerSlashing, SignedBeaconBlockHeader, Slot, }; /// Current database schema version, to check compatibility of on-disk DB with software. diff --git a/slasher/src/migrate.rs b/slasher/src/migrate.rs index 61122907d5a..e7e8a87ac2e 100644 --- a/slasher/src/migrate.rs +++ b/slasher/src/migrate.rs @@ -1,6 +1,5 @@ use crate::{database::CURRENT_SCHEMA_VERSION, Error, SlasherDB}; -use ssz::{Decode, Encode}; -use types::{EthSpec, IndexedAttestationBase, IndexedAttestationOnDisk}; +use types::EthSpec; impl SlasherDB { /// If the database exists, and has a schema, attempt to migrate it to the current version. From a7f0ba28146fc7f885d9801e79df66b282621b0b Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 18 Jun 2024 17:10:06 +0200 Subject: [PATCH 3/3] Update slasher/src/migrate.rs Co-authored-by: ethDreamer <37123614+ethDreamer@users.noreply.github.com> --- slasher/src/migrate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slasher/src/migrate.rs b/slasher/src/migrate.rs index e7e8a87ac2e..ce298caaf2c 100644 --- a/slasher/src/migrate.rs +++ b/slasher/src/migrate.rs @@ -18,7 +18,7 @@ impl SlasherDB { }), (x, y) if x == y => Ok(self), (3, 4) => { - // TODO migration + // TODO(electra): db migration due to `IndexedAttestationOnDisk` Ok(self) } (_, _) => Err(Error::IncompatibleSchemaVersion {