mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 04:01:51 +00:00
## Issue Addressed
Closes https://github.com/sigp/lighthouse/issues/2112
Closes https://github.com/sigp/lighthouse/issues/1861
## Proposed Changes
Collect attestations by validator index in the slasher, and use the magic of reference counting to automatically discard redundant attestations. This results in us storing only 1-2% of the attestations observed when subscribed to all subnets, which carries over to a 50-100x reduction in data stored 🎉
## Additional Info
There's some nuance to the configuration of the `slot-offset`. It has a profound effect on the effictiveness of de-duplication, see the docs added to the book for an explanation: 5442e695e5/book/src/slasher.md (slot-offset)
80 lines
2.9 KiB
Rust
80 lines
2.9 KiB
Rust
use crate::{
|
|
config::{DEFAULT_BROADCAST, DEFAULT_SLOT_OFFSET},
|
|
database::CURRENT_SCHEMA_VERSION,
|
|
Config, Error, SlasherDB,
|
|
};
|
|
use lmdb::RwTransaction;
|
|
use serde_derive::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
use types::EthSpec;
|
|
|
|
/// Config from schema version 1, for migration to version 2+.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ConfigV1 {
|
|
database_path: PathBuf,
|
|
chunk_size: usize,
|
|
validator_chunk_size: usize,
|
|
history_length: usize,
|
|
update_period: u64,
|
|
max_db_size_mbs: usize,
|
|
}
|
|
|
|
type ConfigV2 = Config;
|
|
|
|
impl Into<ConfigV2> for ConfigV1 {
|
|
fn into(self) -> ConfigV2 {
|
|
Config {
|
|
database_path: self.database_path,
|
|
chunk_size: self.chunk_size,
|
|
validator_chunk_size: self.validator_chunk_size,
|
|
history_length: self.history_length,
|
|
update_period: self.update_period,
|
|
slot_offset: DEFAULT_SLOT_OFFSET,
|
|
max_db_size_mbs: self.max_db_size_mbs,
|
|
broadcast: DEFAULT_BROADCAST,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> SlasherDB<E> {
|
|
/// If the database exists, and has a schema, attempt to migrate it to the current version.
|
|
pub fn migrate(&self, txn: &mut RwTransaction<'_>) -> Result<(), Error> {
|
|
if let Some(schema_version) = self.load_schema_version(txn)? {
|
|
match (schema_version, CURRENT_SCHEMA_VERSION) {
|
|
// The migration from v1 to v2 is a bit messy because v1.0.5 silently
|
|
// changed the schema to v2, so a v1 schema could have either a v1 or v2
|
|
// config.
|
|
(1, 2) => {
|
|
match self.load_config::<ConfigV1>(txn) {
|
|
Ok(Some(config_v1)) => {
|
|
// Upgrade to v2 config and store on disk.
|
|
let config_v2 = config_v1.into();
|
|
self.store_config(&config_v2, txn)?;
|
|
}
|
|
Ok(None) => {
|
|
// Impossible to have schema version and no config.
|
|
return Err(Error::ConfigMissing);
|
|
}
|
|
Err(_) => {
|
|
// If loading v1 config failed, ensure loading v2 config succeeds.
|
|
// No further action is needed.
|
|
let _config_v2 = self.load_config::<ConfigV2>(txn)?;
|
|
}
|
|
}
|
|
}
|
|
(x, y) if x == y => {}
|
|
(_, _) => {
|
|
return Err(Error::IncompatibleSchemaVersion {
|
|
database_schema_version: schema_version,
|
|
software_schema_version: CURRENT_SCHEMA_VERSION,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// If the migration succeeded, update the schema version on-disk.
|
|
self.store_schema_version(txn)?;
|
|
Ok(())
|
|
}
|
|
}
|