Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexed att on disk #35

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions consensus/types/src/indexed_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,38 @@ mod quoted_variable_list_u64 {
}
}

#[derive(Debug, Clone, Encode, Decode, PartialEq)]
#[ssz(enum_behaviour = "union")]
pub enum IndexedAttestationOnDisk<E: EthSpec> {
Base(IndexedAttestationBase<E>),
Electra(IndexedAttestationElectra<E>),
}

#[derive(Debug, Clone, Encode, PartialEq)]
#[ssz(enum_behaviour = "union")]
pub enum IndexedAttestationRefOnDisk<'a, E: EthSpec> {
Base(&'a IndexedAttestationBase<E>),
Electra(&'a IndexedAttestationElectra<E>),
}

impl<'a, E: EthSpec> From<&'a IndexedAttestation<E>> for IndexedAttestationRefOnDisk<'a, E> {
fn from(attestation: &'a IndexedAttestation<E>) -> Self {
match attestation {
IndexedAttestation::Base(attestation) => Self::Base(attestation),
IndexedAttestation::Electra(attestation) => Self::Electra(attestation),
}
}
}

impl<E: EthSpec> From<IndexedAttestationOnDisk<E>> for IndexedAttestation<E> {
fn from(attestation: IndexedAttestationOnDisk<E>) -> Self {
match attestation {
IndexedAttestationOnDisk::Base(attestation) => Self::Base(attestation),
IndexedAttestationOnDisk::Electra(attestation) => Self::Electra(attestation),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 2 additions & 1 deletion consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions slasher/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use std::marker::PhantomData;
use std::sync::Arc;
use tree_hash::TreeHash;
use types::{
Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationBase, 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.
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";
Expand Down Expand Up @@ -458,7 +458,9 @@ impl<E: EthSpec> SlasherDB<E> {
};

let attestation_key = IndexedAttestationId::new(indexed_att_id);
let data = indexed_attestation.as_ssz_bytes();
let indexed_attestation_on_disk: IndexedAttestationRefOnDisk<E> =
indexed_attestation.into();
let data = indexed_attestation_on_disk.as_ssz_bytes();

cursor.put(attestation_key.as_ref(), &data)?;
drop(cursor);
Expand All @@ -482,11 +484,8 @@ impl<E: EthSpec> SlasherDB<E> {
.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<E> = ssz_decode(bytes)?;
Ok(IndexedAttestation::Base(indexed_attestation))
let indexed_attestation: IndexedAttestationOnDisk<E> = ssz_decode(bytes)?;
Ok(indexed_attestation.into())
}

fn get_attestation_data_root(
Expand Down
4 changes: 4 additions & 0 deletions slasher/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl<E: EthSpec> SlasherDB<E> {
software_schema_version: CURRENT_SCHEMA_VERSION,
}),
(x, y) if x == y => Ok(self),
(3, 4) => {
// TODO(electra): db migration due to `IndexedAttestationOnDisk`
Ok(self)
}
(_, _) => Err(Error::IncompatibleSchemaVersion {
database_schema_version: schema_version,
software_schema_version: CURRENT_SCHEMA_VERSION,
Expand Down
Loading