mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-18 04:13:00 +00:00
Avoid changing slasher schema for Electra
This commit is contained in:
@@ -13,13 +13,15 @@ use parking_lot::Mutex;
|
||||
use serde::de::DeserializeOwned;
|
||||
use slog::{info, Logger};
|
||||
use ssz::{Decode, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use std::borrow::{Borrow, Cow};
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use tree_hash::TreeHash;
|
||||
use types::{
|
||||
Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationOnDisk,
|
||||
IndexedAttestationRefOnDisk, ProposerSlashing, SignedBeaconBlockHeader, Slot,
|
||||
AggregateSignature, AttestationData, ChainSpec, Epoch, EthSpec, ForkName, Hash256,
|
||||
IndexedAttestation, IndexedAttestationBase, IndexedAttestationElectra, ProposerSlashing,
|
||||
SignedBeaconBlockHeader, Slot, VariableList,
|
||||
};
|
||||
|
||||
/// Current database schema version, to check compatibility of on-disk DB with software.
|
||||
@@ -70,6 +72,7 @@ pub struct SlasherDB<E: EthSpec> {
|
||||
/// LRU cache mapping indexed attestation IDs to their attestation data roots.
|
||||
attestation_root_cache: Mutex<LruCache<IndexedAttestationId, Hash256>>,
|
||||
pub(crate) config: Arc<Config>,
|
||||
pub(crate) spec: Arc<ChainSpec>,
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
@@ -236,6 +239,43 @@ impl AsRef<[u8]> for IndexedAttestationId {
|
||||
}
|
||||
}
|
||||
|
||||
/// Indexed attestation that abstracts over Phase0 and Electra variants by using a plain `Vec` for
|
||||
/// the attesting indices.
|
||||
///
|
||||
/// This allows us to avoid rewriting the entire indexed attestation database at Electra, which
|
||||
/// saves a lot of execution time. The bytes that it encodes to are the same as the bytes that a
|
||||
/// regular IndexedAttestation encodes to, because SSZ doesn't care about the length-bound.
|
||||
#[derive(Debug, PartialEq, Decode, Encode)]
|
||||
pub struct IndexedAttestationOnDisk {
|
||||
attesting_indices: Vec<u64>,
|
||||
data: AttestationData,
|
||||
signature: AggregateSignature,
|
||||
}
|
||||
|
||||
impl IndexedAttestationOnDisk {
|
||||
fn into_indexed_attestation<E: EthSpec>(
|
||||
self,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<IndexedAttestation<E>, Error> {
|
||||
let fork_at_target_epoch = spec.fork_name_at_epoch(self.data.target.epoch);
|
||||
if fork_at_target_epoch >= ForkName::Electra {
|
||||
let attesting_indices = VariableList::new(self.attesting_indices)?;
|
||||
Ok(IndexedAttestation::Electra(IndexedAttestationElectra {
|
||||
attesting_indices,
|
||||
data: self.data,
|
||||
signature: self.signature,
|
||||
}))
|
||||
} else {
|
||||
let attesting_indices = VariableList::new(self.attesting_indices)?;
|
||||
Ok(IndexedAttestation::Base(IndexedAttestationBase {
|
||||
attesting_indices,
|
||||
data: self.data,
|
||||
signature: self.signature,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bincode deserialization specialised to `Cow<[u8]>`.
|
||||
fn bincode_deserialize<T: DeserializeOwned>(bytes: Cow<[u8]>) -> Result<T, Error> {
|
||||
Ok(bincode::deserialize(bytes.borrow())?)
|
||||
@@ -246,7 +286,7 @@ fn ssz_decode<T: Decode>(bytes: Cow<[u8]>) -> Result<T, Error> {
|
||||
}
|
||||
|
||||
impl<E: EthSpec> SlasherDB<E> {
|
||||
pub fn open(config: Arc<Config>, log: Logger) -> Result<Self, Error> {
|
||||
pub fn open(config: Arc<Config>, spec: Arc<ChainSpec>, log: Logger) -> Result<Self, Error> {
|
||||
info!(log, "Opening slasher database"; "backend" => %config.backend);
|
||||
|
||||
std::fs::create_dir_all(&config.database_path)?;
|
||||
@@ -269,6 +309,7 @@ impl<E: EthSpec> SlasherDB<E> {
|
||||
databases,
|
||||
attestation_root_cache,
|
||||
config,
|
||||
spec,
|
||||
_phantom: PhantomData,
|
||||
};
|
||||
|
||||
@@ -458,9 +499,8 @@ impl<E: EthSpec> SlasherDB<E> {
|
||||
};
|
||||
|
||||
let attestation_key = IndexedAttestationId::new(indexed_att_id);
|
||||
let indexed_attestation_on_disk: IndexedAttestationRefOnDisk<E> =
|
||||
indexed_attestation.into();
|
||||
let data = indexed_attestation_on_disk.as_ssz_bytes();
|
||||
// IndexedAttestationOnDisk and IndexedAttestation have compatible encodings.
|
||||
let data = indexed_attestation.as_ssz_bytes();
|
||||
|
||||
cursor.put(attestation_key.as_ref(), &data)?;
|
||||
drop(cursor);
|
||||
@@ -484,8 +524,8 @@ impl<E: EthSpec> SlasherDB<E> {
|
||||
.ok_or(Error::MissingIndexedAttestation {
|
||||
id: indexed_attestation_id.as_u64(),
|
||||
})?;
|
||||
let indexed_attestation: IndexedAttestationOnDisk<E> = ssz_decode(bytes)?;
|
||||
Ok(indexed_attestation.into())
|
||||
let indexed_attestation_on_disk: IndexedAttestationOnDisk = ssz_decode(bytes)?;
|
||||
indexed_attestation_on_disk.into_indexed_attestation(&self.spec)
|
||||
}
|
||||
|
||||
fn get_attestation_data_root(
|
||||
@@ -775,3 +815,84 @@ impl<E: EthSpec> SlasherDB<E> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use types::{Checkpoint, MainnetEthSpec};
|
||||
|
||||
type E = MainnetEthSpec;
|
||||
|
||||
fn indexed_attestation_on_disk_roundtrip_test(
|
||||
spec: &ChainSpec,
|
||||
make_attestation: fn(
|
||||
Vec<u64>,
|
||||
AttestationData,
|
||||
AggregateSignature,
|
||||
) -> IndexedAttestation<E>,
|
||||
) {
|
||||
let attestation_data = AttestationData {
|
||||
slot: Slot::new(1000),
|
||||
index: 0,
|
||||
beacon_block_root: Hash256::repeat_byte(0xaa),
|
||||
source: Checkpoint {
|
||||
epoch: Epoch::new(0),
|
||||
root: Hash256::repeat_byte(0xbb),
|
||||
},
|
||||
target: Checkpoint {
|
||||
epoch: Epoch::new(31),
|
||||
root: Hash256::repeat_byte(0xcc),
|
||||
},
|
||||
};
|
||||
|
||||
let attesting_indices = vec![1, 14, 160, 812737];
|
||||
let signature = AggregateSignature::infinity();
|
||||
|
||||
let fork_attestation = make_attestation(
|
||||
attesting_indices.clone(),
|
||||
attestation_data.clone(),
|
||||
signature.clone(),
|
||||
);
|
||||
|
||||
let on_disk = IndexedAttestationOnDisk {
|
||||
attesting_indices,
|
||||
data: attestation_data,
|
||||
signature,
|
||||
};
|
||||
let encoded = on_disk.as_ssz_bytes();
|
||||
assert_eq!(encoded, fork_attestation.as_ssz_bytes());
|
||||
|
||||
let decoded_on_disk = IndexedAttestationOnDisk::from_ssz_bytes(&encoded).unwrap();
|
||||
assert_eq!(decoded_on_disk, on_disk);
|
||||
|
||||
let decoded = on_disk.into_indexed_attestation(&spec).unwrap();
|
||||
assert_eq!(decoded, fork_attestation);
|
||||
}
|
||||
|
||||
/// Check that `IndexedAttestationOnDisk` and `IndexedAttestation` have compatible encodings.
|
||||
#[test]
|
||||
fn indexed_attestation_on_disk_roundtrip_base() {
|
||||
let spec = ForkName::Base.make_genesis_spec(E::default_spec());
|
||||
let make_attestation = |attesting_indices, data, signature| {
|
||||
IndexedAttestation::<E>::Base(IndexedAttestationBase {
|
||||
attesting_indices: VariableList::new(attesting_indices).unwrap(),
|
||||
data,
|
||||
signature,
|
||||
})
|
||||
};
|
||||
indexed_attestation_on_disk_roundtrip_test(&spec, make_attestation)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_attestation_on_disk_roundtrip_electra() {
|
||||
let spec = ForkName::Electra.make_genesis_spec(E::default_spec());
|
||||
let make_attestation = |attesting_indices, data, signature| {
|
||||
IndexedAttestation::<E>::Electra(IndexedAttestationElectra {
|
||||
attesting_indices: VariableList::new(attesting_indices).unwrap(),
|
||||
data,
|
||||
signature,
|
||||
})
|
||||
};
|
||||
indexed_attestation_on_disk_roundtrip_test(&spec, make_attestation)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user