converter function for reducing blocks to headers (#3410)

This introduces a function to convert `SignedBeaconBlock` to just their
`BeaconBlockHeader` and updates the usages for reduced code duplication.
This commit is contained in:
Etan Kissling 2022-02-18 21:35:52 +01:00 committed by GitHub
parent 555ce310cc
commit 9790c4958b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 39 deletions

View File

@ -700,13 +700,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
canonical: node.dag.isCanonical( canonical: node.dag.isCanonical(
BlockId(root: blck.root, slot: blck.message.slot)), BlockId(root: blck.root, slot: blck.message.slot)),
header: ( header: (
message: ( message: blck.toBeaconBlockHeader,
slot: blck.message.slot,
proposer_index: blck.message.proposer_index,
parent_root: blck.message.parent_root,
state_root: blck.message.state_root,
body_root: blck.message.body.hash_tree_root()
),
signature: blck.signature signature: blck.signature
) )
) )
@ -732,13 +726,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
canonical: node.dag.isCanonical( canonical: node.dag.isCanonical(
BlockId(root: blck.root, slot: blck.message.slot)), BlockId(root: blck.root, slot: blck.message.slot)),
header: ( header: (
message: ( message: blck.toBeaconBlockHeader,
slot: blck.message.slot,
proposer_index: blck.message.proposer_index,
parent_root: blck.message.parent_root,
state_root: blck.message.state_root,
body_root: blck.message.body.hash_tree_root()
),
signature: blck.signature signature: blck.signature
) )
) )

View File

@ -391,13 +391,7 @@ proc installBeaconApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
canonical: node.dag.isCanonical( canonical: node.dag.isCanonical(
BlockId(root: blck.root, slot: blck.message.slot)), BlockId(root: blck.root, slot: blck.message.slot)),
header: SignedBeaconBlockHeader( header: SignedBeaconBlockHeader(
message: BeaconBlockHeader( message: blck.toBeaconBlockHeader
slot: blck.message.slot,
proposer_index: blck.message.proposer_index,
parent_root: blck.message.parent_root,
state_root: blck.message.state_root,
body_root: blck.message.body.hash_tree_root()
)
) )
) )

View File

@ -236,6 +236,21 @@ template toFork*[T: bellatrix.TrustedSignedBeaconBlock](
t: type T): BeaconBlockFork = t: type T): BeaconBlockFork =
BeaconBlockFork.Bellatrix BeaconBlockFork.Bellatrix
func toBeaconBlockHeader*(
blck: SomeForkyBeaconBlock): BeaconBlockHeader =
## Reduce a given `BeaconBlock` to just its `BeaconBlockHeader`.
BeaconBlockHeader(
slot: blck.slot,
proposer_index: blck.proposer_index,
parent_root: blck.parent_root,
state_root: blck.state_root,
body_root: blck.body.hash_tree_root())
template toBeaconBlockHeader*(
blck: SomeForkySignedBeaconBlock): BeaconBlockHeader =
## Reduce a given `SignedBeaconBlock` to just its `BeaconBlockHeader`.
blck.message.toBeaconBlockHeader
template init*(T: type ForkedEpochInfo, info: phase0.EpochInfo): T = template init*(T: type ForkedEpochInfo, info: phase0.EpochInfo): T =
T(kind: EpochInfoFork.Phase0, phase0Data: info) T(kind: EpochInfoFork.Phase0, phase0Data: info)
template init*(T: type ForkedEpochInfo, info: altair.EpochInfo): T = template init*(T: type ForkedEpochInfo, info: altair.EpochInfo): T =

View File

@ -103,12 +103,7 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
var cache = StateCache() var cache = StateCache()
let let
signed_block = block_for_next_slot(cfg, forked[], cache).altairData signed_block = block_for_next_slot(cfg, forked[], cache).altairData
block_header = BeaconBlockHeader( block_header = signed_block.toBeaconBlockHeader
slot: signed_block.message.slot,
proposer_index: signed_block.message.proposer_index,
parent_root: signed_block.message.parent_root,
state_root: signed_block.message.state_root,
body_root: signed_block.message.body.hash_tree_root())
# Sync committee signing the header # Sync committee signing the header
all_pubkeys = state.validators.mapIt(it.pubkey) all_pubkeys = state.validators.mapIt(it.pubkey)
committee = state.current_sync_committee.pubkeys committee = state.current_sync_committee.pubkeys
@ -168,12 +163,7 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
let let
signed_block = block_for_next_slot(cfg, forked[], cache).altairData signed_block = block_for_next_slot(cfg, forked[], cache).altairData
block_header = BeaconBlockHeader( block_header = signed_block.toBeaconBlockHeader
slot: signed_block.message.slot,
proposer_index: signed_block.message.proposer_index,
parent_root: signed_block.message.parent_root,
state_root: signed_block.message.state_root,
body_root: signed_block.message.body.hash_tree_root())
# Sync committee signing the finalized_block_header # Sync committee signing the finalized_block_header
all_pubkeys = state.validators.mapIt(it.pubkey) all_pubkeys = state.validators.mapIt(it.pubkey)
@ -245,12 +235,7 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest] array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
let let
finalized_block = blocks[SLOTS_PER_EPOCH - 1].altairData finalized_block = blocks[SLOTS_PER_EPOCH - 1].altairData
finalized_block_header = BeaconBlockHeader( finalized_block_header = finalized_block.toBeaconBlockHeader
slot: finalized_block.message.slot,
proposer_index: finalized_block.message.proposer_index,
parent_root: finalized_block.message.parent_root,
state_root: finalized_block.message.state_root,
body_root: finalized_block.message.body.hash_tree_root())
check: check:
finalized_block_header.slot == finalized_block_header.slot ==
start_slot(state.finalized_checkpoint.epoch) start_slot(state.finalized_checkpoint.epoch)