-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the database to persist the pubkey cache (#2234)
## Issue Addressed Closes #1787 ## Proposed Changes * Abstract the `ValidatorPubkeyCache` over a "backing" which is either a file (legacy), or the database. * Implement a migration from schema v2 to schema v3, whereby the contents of the cache file are copied to the DB, and then the file is deleted. The next release to include this change must be a minor version bump, and we will need to warn users of the inability to downgrade (this is our first DB schema change since mainnet genesis). * Move the schema migration code from the `store` crate into the `beacon_chain` crate so that it can access the datadir and the `ValidatorPubkeyCache`, etc. It gets injected back into the `store` via a closure (similar to what we do in fork choice).
- Loading branch information
1 parent
eea09d0
commit 20c395b
Showing
17 changed files
with
263 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Utilities for managing database schema changes. | ||
use crate::beacon_chain::BeaconChainTypes; | ||
use crate::validator_pubkey_cache::ValidatorPubkeyCache; | ||
use std::fs; | ||
use std::path::Path; | ||
use std::sync::Arc; | ||
use store::hot_cold_store::{HotColdDB, HotColdDBError}; | ||
use store::metadata::{SchemaVersion, CURRENT_SCHEMA_VERSION}; | ||
use store::Error as StoreError; | ||
|
||
const PUBKEY_CACHE_FILENAME: &str = "pubkey_cache.ssz"; | ||
|
||
/// Migrate the database from one schema version to another, applying all requisite mutations. | ||
pub fn migrate_schema<T: BeaconChainTypes>( | ||
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>, | ||
datadir: &Path, | ||
from: SchemaVersion, | ||
to: SchemaVersion, | ||
) -> Result<(), StoreError> { | ||
match (from, to) { | ||
// Migrating from the current schema version to iself is always OK, a no-op. | ||
(_, _) if from == to && to == CURRENT_SCHEMA_VERSION => Ok(()), | ||
// Migrate across multiple versions by recursively migrating one step at a time. | ||
(_, _) if from.as_u64() + 1 < to.as_u64() => { | ||
let next = SchemaVersion(from.as_u64() + 1); | ||
migrate_schema::<T>(db.clone(), datadir, from, next)?; | ||
migrate_schema::<T>(db, datadir, next, to) | ||
} | ||
// Migration from v0.3.0 to v0.3.x, adding the temporary states column. | ||
// Nothing actually needs to be done, but once a DB uses v2 it shouldn't go back. | ||
(SchemaVersion(1), SchemaVersion(2)) => { | ||
db.store_schema_version(to)?; | ||
Ok(()) | ||
} | ||
// Migration for removing the pubkey cache. | ||
(SchemaVersion(2), SchemaVersion(3)) => { | ||
let pk_cache_path = datadir.join(PUBKEY_CACHE_FILENAME); | ||
|
||
// Load from file, store to DB. | ||
ValidatorPubkeyCache::<T>::load_from_file(&pk_cache_path) | ||
.and_then(|cache| ValidatorPubkeyCache::convert(cache, db.clone())) | ||
.map_err(|e| StoreError::SchemaMigrationError(format!("{:?}", e)))?; | ||
|
||
db.store_schema_version(to)?; | ||
|
||
// Delete cache file now that keys are stored in the DB. | ||
fs::remove_file(&pk_cache_path).map_err(|e| { | ||
StoreError::SchemaMigrationError(format!( | ||
"unable to delete {}: {:?}", | ||
pk_cache_path.display(), | ||
e | ||
)) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
// Anything else is an error. | ||
(_, _) => Err(HotColdDBError::UnsupportedSchemaVersion { | ||
target_version: to, | ||
current_version: from, | ||
} | ||
.into()), | ||
} | ||
} |
Oops, something went wrong.