mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-01 03:33:47 +00:00
Add finalized to HTTP API responses (#3753)
## Issue Addressed #3708 ## Proposed Changes - Add `is_finalized_block` method to `BeaconChain` in `beacon_node/beacon_chain/src/beacon_chain.rs`. - Add `is_finalized_state` method to `BeaconChain` in `beacon_node/beacon_chain/src/beacon_chain.rs`. - Add `fork_and_execution_optimistic_and_finalized` in `beacon_node/http_api/src/state_id.rs`. - Add `ExecutionOptimisticFinalizedForkVersionedResponse` type in `consensus/types/src/fork_versioned_response.rs`. - Add `execution_optimistic_finalized_fork_versioned_response`function in `beacon_node/http_api/src/version.rs`. - Add `ExecutionOptimisticFinalizedResponse` type in `common/eth2/src/types.rs`. - Add `add_execution_optimistic_finalized` method in `common/eth2/src/types.rs`. - Update API response methods to include finalized. - Remove `execution_optimistic_fork_versioned_response` Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
@@ -432,6 +432,46 @@ pub struct BeaconChain<T: BeaconChainTypes> {
|
||||
type BeaconBlockAndState<T, Payload> = (BeaconBlock<T, Payload>, BeaconState<T>);
|
||||
|
||||
impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
/// Checks if a block is finalized.
|
||||
/// The finalization check is done with the block slot. The block root is used to verify that
|
||||
/// the finalized slot is in the canonical chain.
|
||||
pub fn is_finalized_block(
|
||||
&self,
|
||||
block_root: &Hash256,
|
||||
block_slot: Slot,
|
||||
) -> Result<bool, Error> {
|
||||
let finalized_slot = self
|
||||
.canonical_head
|
||||
.cached_head()
|
||||
.finalized_checkpoint()
|
||||
.epoch
|
||||
.start_slot(T::EthSpec::slots_per_epoch());
|
||||
let is_canonical = self
|
||||
.block_root_at_slot(block_slot, WhenSlotSkipped::None)?
|
||||
.map_or(false, |canonical_root| block_root == &canonical_root);
|
||||
Ok(block_slot <= finalized_slot && is_canonical)
|
||||
}
|
||||
|
||||
/// Checks if a state is finalized.
|
||||
/// The finalization check is done with the slot. The state root is used to verify that
|
||||
/// the finalized state is in the canonical chain.
|
||||
pub fn is_finalized_state(
|
||||
&self,
|
||||
state_root: &Hash256,
|
||||
state_slot: Slot,
|
||||
) -> Result<bool, Error> {
|
||||
let finalized_slot = self
|
||||
.canonical_head
|
||||
.cached_head()
|
||||
.finalized_checkpoint()
|
||||
.epoch
|
||||
.start_slot(T::EthSpec::slots_per_epoch());
|
||||
let is_canonical = self
|
||||
.state_root_at_slot(state_slot)?
|
||||
.map_or(false, |canonical_root| state_root == &canonical_root);
|
||||
Ok(state_slot <= finalized_slot && is_canonical)
|
||||
}
|
||||
|
||||
/// Persists the head tracker and fork choice.
|
||||
///
|
||||
/// We do it atomically even though no guarantees need to be made about blocks from
|
||||
|
||||
Reference in New Issue
Block a user