Skip to content

Commit

Permalink
Switch to Arc<Snapshot> for CachedHead
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 65ead81 commit 7cc0d27
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
.try_read_for(VALIDATOR_PUBKEY_CACHE_LOCK_TIMEOUT)
.ok_or(BeaconChainError::ValidatorPubkeyCacheLockTimeout)?;

let fork = chain.canonical_head.cached_head_read_lock().head_fork();
let fork = chain.canonical_head.cached_head().head_fork();

let mut signature_sets = Vec::with_capacity(num_indexed * 3);

Expand Down Expand Up @@ -169,7 +169,7 @@ where
&metrics::ATTESTATION_PROCESSING_BATCH_UNAGG_SIGNATURE_SETUP_TIMES,
);

let fork = chain.canonical_head.cached_head_read_lock().head_fork();
let fork = chain.canonical_head.cached_head().head_fork();

let pubkey_cache = chain
.validator_pubkey_cache
Expand Down
27 changes: 11 additions & 16 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
where
E: From<Error>,
{
let head_lock = self.canonical_head.cached_head_read_lock();
let head_lock = self.canonical_head.cached_head();
f(&head_lock.snapshot)
}

Expand Down Expand Up @@ -1194,7 +1194,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {

/// Returns the slot of the highest block in the canonical chain.
pub fn best_slot(&self) -> Slot {
self.canonical_head.cached_head_read_lock().head_slot()
self.canonical_head.cached_head().head_slot()
}

/// Returns the validator index (if any) for the given public key.
Expand Down Expand Up @@ -1483,7 +1483,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let beacon_block_root;
let beacon_state_root;
let head_timer = metrics::start_timer(&metrics::ATTESTATION_PRODUCTION_HEAD_SCRAPE_SECONDS);
let cached_head_lock = self.canonical_head.cached_head_read_lock();
let cached_head_lock = self.canonical_head.cached_head();
let head = &cached_head_lock.snapshot;
let head_state = &head.beacon_state;
let head_state_slot = head_state.slot();
Expand Down Expand Up @@ -1904,7 +1904,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// If there's no eth1 chain then it's impossible to produce blocks and therefore
// useless to put things in the op pool.
if self.eth1_chain.is_some() {
let fork = self.canonical_head.cached_head_read_lock().head_fork();
let fork = self.canonical_head.cached_head().head_fork();

self.op_pool
.insert_attestation(
Expand Down Expand Up @@ -2099,7 +2099,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if self.eth1_chain.is_some() {
self.op_pool.insert_attester_slashing(
attester_slashing,
self.canonical_head.cached_head_read_lock().head_fork(),
self.canonical_head.cached_head().head_fork(),
)
}
}
Expand Down Expand Up @@ -2633,10 +2633,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// We are doing this to ensure that we detect changes in finalization. It's possible
// that fork choice has already been updated to the finalized checkpoint in the block
// we're importing.
let current_head_finalized_checkpoint = self
.canonical_head
.cached_head_read_lock()
.finalized_checkpoint();
let current_head_finalized_checkpoint =
self.canonical_head.cached_head().finalized_checkpoint();

let block = signed_block.message();

Expand Down Expand Up @@ -3122,7 +3120,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// Atomically read some values from the head whilst avoiding holding the read-lock any
// longer than necessary.
let (head_slot, head_block_root) = {
let head = self.canonical_head.cached_head_read_lock();
let head = self.canonical_head.cached_head();
(head.head_slot(), head.head_block_root())
};
let (state, state_root_opt) = if head_slot < slot {
Expand Down Expand Up @@ -3676,7 +3674,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let (head_slot, head_root, head_decision_root, head_random, forkchoice_update_params) =
self.spawn_blocking_handle(
move || {
let cached_head = chain.canonical_head.cached_head_read_lock();
let cached_head = chain.canonical_head.cached_head();
let head_block_root = cached_head.head_block_root();
let decision_root = cached_head
.snapshot
Expand Down Expand Up @@ -4416,7 +4414,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let mut dump = vec![];

let mut last_slot = {
let head = self.canonical_head.cached_head_read_lock();
let head = self.canonical_head.cached_head();
BeaconSnapshot {
beacon_block: Arc::new(head.snapshot.beacon_block.clone_as_blinded()),
beacon_block_root: head.snapshot.beacon_block_root,
Expand Down Expand Up @@ -4486,10 +4484,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}

pub fn dump_as_dot<W: Write>(&self, output: &mut W) {
let canonical_head_hash = self
.canonical_head
.cached_head_read_lock()
.head_block_root();
let canonical_head_hash = self.canonical_head.cached_head().head_block_root();
let mut visited: HashSet<Hash256> = HashSet::new();
let mut finalized_blocks: HashSet<Hash256> = HashSet::new();
let mut justified_blocks: HashSet<Hash256> = HashSet::new();
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_proposer_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn compute_proposer_duties_from_head<T: BeaconChainTypes>(
// Atomically collect information about the head whilst hogging the `canonical_head_lock` as
// little as possible.
let (mut state, head_state_root, head_block_root) = {
let head = chain.canonical_head.cached_head_read_lock();
let head = chain.canonical_head.cached_head();
// Take a copy of the head state.
let head_state = head
.snapshot
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ fn check_block_against_finalized_slot<T: BeaconChainTypes>(
) -> Result<(), BlockError<T::EthSpec>> {
let finalized_slot = chain
.canonical_head
.cached_head_read_lock()
.cached_head()
.finalized_checkpoint()
.epoch
.start_slot(T::EthSpec::slots_per_epoch());
Expand Down Expand Up @@ -1772,7 +1772,7 @@ fn verify_header_signature<T: BeaconChainTypes>(
.get(header.message.proposer_index as usize)
.cloned()
.ok_or(BlockError::UnknownValidator(header.message.proposer_index))?;
let head_fork = chain.canonical_head.cached_head_read_lock().head_fork();
let head_fork = chain.canonical_head.cached_head().head_fork();

if header.verify_signature::<T::EthSpec>(
&proposer_pubkey,
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ where
let genesis_validators_root = head_snapshot.beacon_state.genesis_validators_root();
let genesis_time = head_snapshot.beacon_state.genesis_time();
let head_for_snapshot_cache = head_snapshot.clone();
let canonical_head = CanonicalHead::new(fork_choice, head_snapshot)
let canonical_head = CanonicalHead::new(fork_choice, Arc::new(head_snapshot))
.map_err(|e| format!("Error creating canonical head: {:?}", e))?;

let beacon_chain = BeaconChain {
Expand Down
Loading

0 comments on commit 7cc0d27

Please sign in to comment.