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.
This commit is contained in:
Michael Sproul 2020-09-30 04:42:52 +00:00
parent a1a6b01acb
commit fcf8419c90

View File

@ -39,13 +39,6 @@ impl ValidatorPubkeyCache {
state: &BeaconState<T>, state: &BeaconState<T>,
persistence_path: P, persistence_path: P,
) -> Result<Self, BeaconChainError> { ) -> Result<Self, BeaconChainError> {
if persistence_path.as_ref().exists() {
return Err(BeaconChainError::ValidatorPubkeyCacheFileError(format!(
"Persistence file already exists: {:?}",
persistence_path.as_ref()
)));
}
let mut cache = Self { let mut cache = Self {
persitence_file: ValidatorPubkeyCacheFile::create(persistence_path)?, persitence_file: ValidatorPubkeyCacheFile::create(persistence_path)?,
pubkeys: vec![], pubkeys: vec![],
@ -159,8 +152,9 @@ impl ValidatorPubkeyCacheFile {
/// Creates a file for reading and writing. /// Creates a file for reading and writing.
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self, Error> { pub fn create<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
OpenOptions::new() OpenOptions::new()
.create_new(true) .create(true)
.write(true) .write(true)
.truncate(true)
.open(path) .open(path)
.map(Self) .map(Self)
.map_err(Error::Io) .map_err(Error::Io)