From fcf8419c90e0922161451d006047e36aa3915097 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 30 Sep 2020 04:42:52 +0000 Subject: [PATCH] Allow truncation of pubkey cache on creation (#1686) ## Issue Addressed Closes #1680 ## Proposed Changes This PR fixes a race condition in beacon node start-up whereby the pubkey cache could be created by the beacon chain builder before the `PersistedBeaconChain` was stored to disk. When the node restarted, it would find the persisted chain missing, and attempt to start from scratch, creating a new pubkey cache in the process. This call to `ValidatorPubkeyCache::new` would fail if the file already existed (which it did). I changed the behaviour so that pubkey cache initialization now doesn't care whether there's a file already in existence (it's only a cache after all). Instead it will truncate and recreate the file in the race scenario described. --- beacon_node/beacon_chain/src/validator_pubkey_cache.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs index 02d35acaa8..48ec5d1b2b 100644 --- a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs +++ b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs @@ -39,13 +39,6 @@ impl ValidatorPubkeyCache { state: &BeaconState, persistence_path: P, ) -> Result { - if persistence_path.as_ref().exists() { - return Err(BeaconChainError::ValidatorPubkeyCacheFileError(format!( - "Persistence file already exists: {:?}", - persistence_path.as_ref() - ))); - } - let mut cache = Self { persitence_file: ValidatorPubkeyCacheFile::create(persistence_path)?, pubkeys: vec![], @@ -159,8 +152,9 @@ impl ValidatorPubkeyCacheFile { /// Creates a file for reading and writing. pub fn create>(path: P) -> Result { OpenOptions::new() - .create_new(true) + .create(true) .write(true) + .truncate(true) .open(path) .map(Self) .map_err(Error::Io)