Correct cache race condition

This commit is contained in:
Age Manning 2019-03-31 00:08:55 +11:00
parent 9a6ecc4665
commit 4fb95d06d1
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
3 changed files with 22 additions and 6 deletions

View File

@ -2,7 +2,7 @@ use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{ use beacon_chain::{
db::ClientDB, db::ClientDB,
fork_choice::ForkChoice, fork_choice::ForkChoice,
parking_lot::RwLockReadGuard, parking_lot::{RwLockReadGuard, RwLockWriteGuard},
slot_clock::SlotClock, slot_clock::SlotClock,
types::{BeaconState, ChainSpec, Signature}, types::{BeaconState, ChainSpec, Signature},
AttestationValidationError, BlockProductionError, AttestationValidationError, BlockProductionError,
@ -16,6 +16,8 @@ pub trait BeaconChain: Send + Sync {
fn get_state(&self) -> RwLockReadGuard<BeaconState>; fn get_state(&self) -> RwLockReadGuard<BeaconState>;
fn get_mut_state(&self) -> RwLockWriteGuard<BeaconState>;
fn process_block(&self, block: BeaconBlock) fn process_block(&self, block: BeaconBlock)
-> Result<BlockProcessingOutcome, BeaconChainError>; -> Result<BlockProcessingOutcome, BeaconChainError>;
@ -46,6 +48,10 @@ where
self.state.read() self.state.read()
} }
fn get_mut_state(&self) -> RwLockWriteGuard<BeaconState> {
self.state.write()
}
fn process_block( fn process_block(
&self, &self,
block: BeaconBlock, block: BeaconBlock,

View File

@ -4,7 +4,7 @@ use futures::Future;
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
use protos::services::{ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty}; use protos::services::{ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty};
use protos::services_grpc::ValidatorService; use protos::services_grpc::ValidatorService;
use slog::{debug, info, warn, Logger}; use slog::{trace, warn};
use ssz::Decodable; use ssz::Decodable;
use std::sync::Arc; use std::sync::Arc;
use types::{Epoch, RelativeEpoch}; use types::{Epoch, RelativeEpoch};
@ -12,7 +12,7 @@ use types::{Epoch, RelativeEpoch};
#[derive(Clone)] #[derive(Clone)]
pub struct ValidatorServiceInstance { pub struct ValidatorServiceInstance {
pub chain: Arc<BeaconChain>, pub chain: Arc<BeaconChain>,
pub log: Logger, pub log: slog::Logger,
} }
//TODO: Refactor Errors //TODO: Refactor Errors
@ -27,13 +27,23 @@ impl ValidatorService for ValidatorServiceInstance {
sink: UnarySink<GetDutiesResponse>, sink: UnarySink<GetDutiesResponse>,
) { ) {
let validators = req.get_validators(); let validators = req.get_validators();
debug!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); trace!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch());
let spec = self.chain.get_spec();
// update the caches if necessary
{
let mut mut_state = self.chain.get_mut_state();
let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec);
let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec);
let _ = mut_state.update_pubkey_cache();
}
let epoch = Epoch::from(req.get_epoch()); let epoch = Epoch::from(req.get_epoch());
let mut resp = GetDutiesResponse::new(); let mut resp = GetDutiesResponse::new();
let resp_validators = resp.mut_active_validators(); let resp_validators = resp.mut_active_validators();
let spec = self.chain.get_spec();
let state = self.chain.get_state(); let state = self.chain.get_state();
let relative_epoch = let relative_epoch =

View File

@ -120,7 +120,7 @@ impl TestingBeaconStateBuilder {
}) })
.collect(); .collect();
let genesis_time = 1553932445; // arbitrary let genesis_time = 1553950542; // arbitrary
let mut state = BeaconState::genesis( let mut state = BeaconState::genesis(
genesis_time, genesis_time,