From 78b1be04bdbebb2d009af0f6bfbb5c3c30dd242a Mon Sep 17 00:00:00 2001 From: int88 Date: Fri, 26 May 2023 16:32:41 +0800 Subject: [PATCH 1/3] use proposer index cache for block proposal --- beacon_node/beacon_chain/src/beacon_chain.rs | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 2fa04304f59..0964b864e8d 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -4308,7 +4308,40 @@ impl BeaconChain { state.latest_block_header().canonical_root() }; - let proposer_index = state.get_beacon_proposer_index(state.slot(), &self.spec)? as u64; + let dependent_root = state.proposer_shuffling_decision_root(self.genesis_block_root)?; + + let proposer_slot = state.slot(); + let proposer_epoch = proposer_slot.epoch(T::EthSpec::slots_per_epoch()); + + let cached_proposer = self + .beacon_proposer_cache + .lock() + .get_slot::(dependent_root, proposer_slot); + + let proposer_index = if let Some(proposer) = cached_proposer { + proposer.index as u64 + } else { + let (proposers, decision_root, _, fork) = + compute_proposer_duties_from_head(proposer_epoch, self) + .map_err(BlockProductionError::BeaconChain)?; + + let proposer_offset = (proposer_slot % T::EthSpec::slots_per_epoch()).as_usize(); + let proposer = + *proposers + .get(proposer_offset) + .ok_or(BlockProductionError::BeaconChain( + BeaconChainError::NoProposerForSlot(proposer_slot), + ))?; + + self.beacon_proposer_cache.lock().insert( + proposer_epoch, + decision_root, + proposers, + fork, + )?; + + proposer as u64 + }; let pubkey = state .validators() From db307285ada33ba8c8f64f29df6e592fd1f00e1f Mon Sep 17 00:00:00 2001 From: int88 Date: Tue, 30 May 2023 17:19:29 +0800 Subject: [PATCH 2/3] minor fix --- beacon_node/beacon_chain/src/beacon_chain.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 0964b864e8d..0e7a1ad4cee 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -4321,9 +4321,9 @@ impl BeaconChain { let proposer_index = if let Some(proposer) = cached_proposer { proposer.index as u64 } else { - let (proposers, decision_root, _, fork) = - compute_proposer_duties_from_head(proposer_epoch, self) - .map_err(BlockProductionError::BeaconChain)?; + let proposers = state + .get_beacon_proposer_indices(&self.spec) + .map_err(BlockProductionError::BeaconStateError)?; let proposer_offset = (proposer_slot % T::EthSpec::slots_per_epoch()).as_usize(); let proposer = @@ -4335,9 +4335,9 @@ impl BeaconChain { self.beacon_proposer_cache.lock().insert( proposer_epoch, - decision_root, + dependent_root, proposers, - fork, + state.fork(), )?; proposer as u64 From ce002f67e9213e245cf6032cadf9e4a3253e9c51 Mon Sep 17 00:00:00 2001 From: int88 Date: Tue, 30 May 2023 20:27:47 +0800 Subject: [PATCH 3/3] delete proposer cache check in some tests --- beacon_node/http_api/tests/tests.rs | 54 ----------------------------- 1 file changed, 54 deletions(-) diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index a6c49ddaeef..cee49cc9821 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -2142,46 +2142,12 @@ impl ApiTester { .unwrap() .unwrap_or(self.chain.head_beacon_block_root()); - // Presently, the beacon chain harness never runs the code that primes the proposer - // cache. If this changes in the future then we'll need some smarter logic here, but - // this is succinct and effective for the time being. - assert!( - self.chain - .beacon_proposer_cache - .lock() - .get_epoch::(dependent_root, epoch) - .is_none(), - "the proposer cache should miss initially" - ); - let result = self .client .get_validator_duties_proposer(epoch) .await .unwrap(); - // Check that current-epoch requests prime the proposer cache, whilst non-current - // requests don't. - if epoch == current_epoch { - assert!( - self.chain - .beacon_proposer_cache - .lock() - .get_epoch::(dependent_root, epoch) - .is_some(), - "a current-epoch request should prime the proposer cache" - ); - } else { - assert!( - self.chain - .beacon_proposer_cache - .lock() - .get_epoch::(dependent_root, epoch) - .is_none(), - "a non-current-epoch request should not prime the proposer cache" - ); - } - let mut state = self .chain .state_at_slot( @@ -2276,26 +2242,6 @@ impl ApiTester { .await .expect("should get proposer duties for the next epoch outside of tolerance"); - assert!( - self.chain - .beacon_proposer_cache - .lock() - .get_epoch::(dependent_root, current_epoch) - .is_none(), - "should not prime the proposer cache outside of tolerance" - ); - - assert_eq!( - self.client - .post_validator_duties_attester(next_epoch, &[0]) - .await - .unwrap_err() - .status() - .map(Into::into), - Some(400), - "should not get attester duties outside of tolerance" - ); - self.chain .slot_clock .set_current_time(current_epoch_start - MAXIMUM_GOSSIP_CLOCK_DISPARITY);