mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-02-17 00:47:03 +00:00
cleanup light client sync tests (#3445)
Various cleanups in the light client sync test suite without semantic impact to make the various tests more streamlined.
This commit is contained in:
parent
12ed537f75
commit
0e34c6023e
@ -13,8 +13,10 @@ import
|
|||||||
# Status libraries
|
# Status libraries
|
||||||
stew/bitops2,
|
stew/bitops2,
|
||||||
# Beacon chain internals
|
# Beacon chain internals
|
||||||
|
../../../beacon_chain/spec/datatypes/altair,
|
||||||
../../../beacon_chain/spec/
|
../../../beacon_chain/spec/
|
||||||
[beaconstate, forks, helpers, light_client_sync, signatures, state_transition],
|
[beaconstate, forks, helpers, light_client_sync, signatures,
|
||||||
|
state_transition],
|
||||||
# Mock helpers
|
# Mock helpers
|
||||||
../../mocking/[mock_blocks, mock_genesis],
|
../../mocking/[mock_blocks, mock_genesis],
|
||||||
# Test utilities
|
# Test utilities
|
||||||
@ -24,17 +26,12 @@ import
|
|||||||
proc compute_aggregate_sync_committee_signature(
|
proc compute_aggregate_sync_committee_signature(
|
||||||
forked: ForkedHashedBeaconState,
|
forked: ForkedHashedBeaconState,
|
||||||
participants: openArray[ValidatorIndex],
|
participants: openArray[ValidatorIndex],
|
||||||
block_root = ZERO_HASH): ValidatorSig =
|
block_root: Eth2Digest): ValidatorSig =
|
||||||
template state: untyped {.inject.} = forked.altairData.data
|
template state: untyped {.inject.} = forked.altairData.data
|
||||||
|
|
||||||
if len(participants) == 0:
|
if len(participants) == 0:
|
||||||
return ValidatorSig.infinity
|
return ValidatorSig.infinity
|
||||||
|
|
||||||
let
|
|
||||||
root =
|
|
||||||
if block_root != ZERO_HASH: block_root
|
|
||||||
else: mockBlockForNextSlot(forked).altairData.message.parent_root
|
|
||||||
|
|
||||||
var
|
var
|
||||||
aggregateSig {.noinit.}: AggregateSignature
|
aggregateSig {.noinit.}: AggregateSignature
|
||||||
initialized = false
|
initialized = false
|
||||||
@ -42,7 +39,11 @@ proc compute_aggregate_sync_committee_signature(
|
|||||||
let
|
let
|
||||||
privkey = MockPrivKeys[validator_index]
|
privkey = MockPrivKeys[validator_index]
|
||||||
signature = get_sync_committee_message_signature(
|
signature = get_sync_committee_message_signature(
|
||||||
state.fork, state.genesis_validators_root, state.slot, root, privkey)
|
state.fork,
|
||||||
|
state.genesis_validators_root,
|
||||||
|
state.slot,
|
||||||
|
block_root,
|
||||||
|
privkey)
|
||||||
if not initialized:
|
if not initialized:
|
||||||
initialized = true
|
initialized = true
|
||||||
aggregateSig.init(signature)
|
aggregateSig.init(signature)
|
||||||
@ -78,7 +79,7 @@ func initialize_light_client_store(state: auto): LightClientStore =
|
|||||||
finalized_header: BeaconBlockHeader(),
|
finalized_header: BeaconBlockHeader(),
|
||||||
current_sync_committee: state.current_sync_committee,
|
current_sync_committee: state.current_sync_committee,
|
||||||
next_sync_committee: state.next_sync_committee,
|
next_sync_committee: state.next_sync_committee,
|
||||||
best_valid_update: none(LightClientUpdate),
|
best_valid_update: none(altair.LightClientUpdate),
|
||||||
optimistic_header: BeaconBlockHeader(),
|
optimistic_header: BeaconBlockHeader(),
|
||||||
previous_max_active_participants: 0,
|
previous_max_active_participants: 0,
|
||||||
current_max_active_participants: 0,
|
current_max_active_participants: 0,
|
||||||
@ -104,19 +105,21 @@ 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 = signed_block.toBeaconBlockHeader
|
block_header = signed_block.toBeaconBlockHeader
|
||||||
# Sync committee signing the header
|
|
||||||
|
# Sync committee signing the block_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
|
||||||
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
||||||
sync_committee_bits = full_sync_committee_bits
|
sync_committee_bits = full_sync_committee_bits
|
||||||
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
||||||
forked[], committee)
|
forked[], committee, block_header.hash_tree_root())
|
||||||
sync_aggregate = SyncAggregate(
|
sync_aggregate = SyncAggregate(
|
||||||
sync_committee_bits: sync_committee_bits,
|
sync_committee_bits: sync_committee_bits,
|
||||||
sync_committee_signature: sync_committee_signature)
|
sync_committee_signature: sync_committee_signature)
|
||||||
|
|
||||||
|
template next_sync_committee(): auto = state.next_sync_committee
|
||||||
var next_sync_committee_branch:
|
var next_sync_committee_branch:
|
||||||
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
array[log2trunc(altair.NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
||||||
|
|
||||||
# Ensure that finality checkpoint is genesis
|
# Ensure that finality checkpoint is genesis
|
||||||
check: state.finalized_checkpoint.epoch == 0
|
check: state.finalized_checkpoint.epoch == 0
|
||||||
@ -124,21 +127,23 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
let
|
let
|
||||||
finality_header = BeaconBlockHeader()
|
finality_header = BeaconBlockHeader()
|
||||||
pre_store_finalized_header = store.finalized_header
|
pre_store_finalized_header = store.finalized_header
|
||||||
var finality_branch: array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
var finality_branch:
|
||||||
|
array[log2trunc(altair.FINALIZED_ROOT_INDEX), Eth2Digest]
|
||||||
|
|
||||||
let update = LightClientUpdate(
|
let
|
||||||
attested_header: block_header,
|
update = altair.LightClientUpdate(
|
||||||
next_sync_committee: state.next_sync_committee,
|
attested_header: block_header,
|
||||||
next_sync_committee_branch: next_sync_committee_branch,
|
next_sync_committee: next_sync_committee,
|
||||||
finalized_header: finality_header,
|
next_sync_committee_branch: next_sync_committee_branch,
|
||||||
finality_branch: finality_branch,
|
finalized_header: finality_header,
|
||||||
sync_aggregate: sync_aggregate,
|
finality_branch: finality_branch,
|
||||||
fork_version: state.fork.current_version)
|
sync_aggregate: sync_aggregate,
|
||||||
|
fork_version: state.fork.current_version)
|
||||||
check:
|
res = process_light_client_update(
|
||||||
process_light_client_update(
|
|
||||||
store, update, state.slot, state.genesis_validators_root)
|
store, update, state.slot, state.genesis_validators_root)
|
||||||
|
|
||||||
|
check:
|
||||||
|
res
|
||||||
store.current_max_active_participants > 0
|
store.current_max_active_participants > 0
|
||||||
store.optimistic_header == update.attested_header
|
store.optimistic_header == update.attested_header
|
||||||
store.finalized_header == pre_store_finalized_header
|
store.finalized_header == pre_store_finalized_header
|
||||||
@ -165,45 +170,47 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
signed_block = block_for_next_slot(cfg, forked[], cache).altairData
|
signed_block = block_for_next_slot(cfg, forked[], cache).altairData
|
||||||
block_header = signed_block.toBeaconBlockHeader
|
block_header = signed_block.toBeaconBlockHeader
|
||||||
|
|
||||||
# Sync committee signing the finalized_block_header
|
# Sync committee signing the block_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
|
||||||
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
||||||
sync_committee_bits = full_sync_committee_bits
|
sync_committee_bits = full_sync_committee_bits
|
||||||
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
||||||
forked[], committee, block_root = block_header.hash_tree_root())
|
forked[], committee, block_header.hash_tree_root())
|
||||||
sync_aggregate = SyncAggregate(
|
sync_aggregate = SyncAggregate(
|
||||||
sync_committee_bits: sync_committee_bits,
|
sync_committee_bits: sync_committee_bits,
|
||||||
sync_committee_signature: sync_committee_signature)
|
sync_committee_signature: sync_committee_signature)
|
||||||
|
|
||||||
# Sync committee is updated
|
# Sync committee is updated
|
||||||
|
template next_sync_committee(): auto = state.next_sync_committee
|
||||||
var next_sync_committee_branch {.noinit.}:
|
var next_sync_committee_branch {.noinit.}:
|
||||||
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
array[log2trunc(altair.NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
||||||
build_proof(state, NEXT_SYNC_COMMITTEE_INDEX, next_sync_committee_branch)
|
state.build_proof(
|
||||||
|
altair.NEXT_SYNC_COMMITTEE_INDEX, next_sync_committee_branch)
|
||||||
# Finality is unchanged
|
# Finality is unchanged
|
||||||
|
let finality_header = BeaconBlockHeader()
|
||||||
|
var finality_branch:
|
||||||
|
array[log2trunc(altair.FINALIZED_ROOT_INDEX), Eth2Digest]
|
||||||
|
|
||||||
let
|
let
|
||||||
finality_header = BeaconBlockHeader()
|
|
||||||
pre_store_finalized_header = store.finalized_header
|
pre_store_finalized_header = store.finalized_header
|
||||||
var finality_branch: array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
update = altair.LightClientUpdate(
|
||||||
|
attested_header: block_header,
|
||||||
let update = LightClientUpdate(
|
next_sync_committee: next_sync_committee,
|
||||||
attested_header: block_header,
|
next_sync_committee_branch: next_sync_committee_branch,
|
||||||
next_sync_committee: state.next_sync_committee,
|
finalized_header: finality_header,
|
||||||
next_sync_committee_branch: next_sync_committee_branch,
|
finality_branch: finality_branch,
|
||||||
finalized_header: finality_header,
|
sync_aggregate: sync_aggregate,
|
||||||
finality_branch: finality_branch,
|
fork_version: state.fork.current_version)
|
||||||
sync_aggregate: sync_aggregate,
|
res = process_light_client_update(
|
||||||
fork_version: state.fork.current_version)
|
|
||||||
|
|
||||||
check:
|
|
||||||
process_light_client_update(
|
|
||||||
store, update, state.slot, state.genesis_validators_root)
|
store, update, state.slot, state.genesis_validators_root)
|
||||||
|
|
||||||
# snapshot has been updated
|
check:
|
||||||
|
res
|
||||||
store.current_max_active_participants > 0
|
store.current_max_active_participants > 0
|
||||||
store.optimistic_header == update.attested_header
|
store.optimistic_header == update.attested_header
|
||||||
store.best_valid_update.get == update
|
|
||||||
store.finalized_header == pre_store_finalized_header
|
store.finalized_header == pre_store_finalized_header
|
||||||
|
store.best_valid_update.get == update
|
||||||
|
|
||||||
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py#L157-L224
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py#L157-L224
|
||||||
test "process_light_client_update_finality_updated":
|
test "process_light_client_update_finality_updated":
|
||||||
@ -218,12 +225,21 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
blocks = newSeq[ForkedSignedBeaconBlock]()
|
blocks = newSeq[ForkedSignedBeaconBlock]()
|
||||||
process_slots(
|
process_slots(
|
||||||
cfg, forked[], Slot(SLOTS_PER_EPOCH * 2), cache, info, flags = {}).expect("no failure")
|
cfg, forked[], Slot(SLOTS_PER_EPOCH * 2), cache, info, flags = {}).expect("no failure")
|
||||||
for epoch in 0 ..< 3:
|
for slot in 0 ..< SLOTS_PER_EPOCH:
|
||||||
for slot in 0 ..< SLOTS_PER_EPOCH:
|
blocks.add block_for_next_slot(cfg, forked[], cache,
|
||||||
blocks.add block_for_next_slot(cfg, forked[], cache,
|
withAttestations = true)
|
||||||
withAttestations = true)
|
let finalized = assignClone(forked[])
|
||||||
|
template finalized_state: untyped {.inject.} = finalized[].altairData.data
|
||||||
|
for slot in 0 ..< SLOTS_PER_EPOCH:
|
||||||
|
blocks.add block_for_next_slot(cfg, forked[], cache,
|
||||||
|
withAttestations = true)
|
||||||
|
for slot in 0 ..< SLOTS_PER_EPOCH:
|
||||||
|
blocks.add block_for_next_slot(cfg, forked[], cache,
|
||||||
|
withAttestations = true)
|
||||||
# Ensure that finality checkpoint has changed
|
# Ensure that finality checkpoint has changed
|
||||||
check: state.finalized_checkpoint.epoch == 3
|
check: state.finalized_checkpoint.epoch == 3
|
||||||
|
check: state.finalized_checkpoint.root ==
|
||||||
|
mockBlockForNextSlot(finalized[]).altairData.message.parent_root
|
||||||
# Ensure that it's same period
|
# Ensure that it's same period
|
||||||
let
|
let
|
||||||
snapshot_period = sync_committee_period(store.optimistic_header.slot)
|
snapshot_period = sync_committee_period(store.optimistic_header.slot)
|
||||||
@ -231,8 +247,9 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
check: snapshot_period == update_period
|
check: snapshot_period == update_period
|
||||||
|
|
||||||
# Updated sync_committee and finality
|
# Updated sync_committee and finality
|
||||||
|
template next_sync_committee(): auto = finalized_state.next_sync_committee
|
||||||
var next_sync_committee_branch:
|
var next_sync_committee_branch:
|
||||||
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
array[log2trunc(altair.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 = finalized_block.toBeaconBlockHeader
|
finalized_block_header = finalized_block.toBeaconBlockHeader
|
||||||
@ -242,8 +259,9 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
finalized_block_header.hash_tree_root() ==
|
finalized_block_header.hash_tree_root() ==
|
||||||
state.finalized_checkpoint.root
|
state.finalized_checkpoint.root
|
||||||
var finality_branch {.noinit.}:
|
var finality_branch {.noinit.}:
|
||||||
array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
array[log2trunc(altair.FINALIZED_ROOT_INDEX), Eth2Digest]
|
||||||
build_proof(state, FINALIZED_ROOT_INDEX, finality_branch)
|
state.build_proof(
|
||||||
|
altair.FINALIZED_ROOT_INDEX, finality_branch)
|
||||||
|
|
||||||
# Build block header
|
# Build block header
|
||||||
let
|
let
|
||||||
@ -261,25 +279,24 @@ suite "EF - Altair - Unittests - Sync protocol" & preset():
|
|||||||
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
||||||
sync_committee_bits = full_sync_committee_bits
|
sync_committee_bits = full_sync_committee_bits
|
||||||
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
||||||
forked[], committee, block_root = block_header.hash_tree_root())
|
forked[], committee, block_header.hash_tree_root())
|
||||||
sync_aggregate = SyncAggregate(
|
sync_aggregate = SyncAggregate(
|
||||||
sync_committee_bits: sync_committee_bits,
|
sync_committee_bits: sync_committee_bits,
|
||||||
sync_committee_signature: sync_committee_signature)
|
sync_committee_signature: sync_committee_signature)
|
||||||
|
|
||||||
update = LightClientUpdate(
|
update = altair.LightClientUpdate(
|
||||||
attested_header: block_header,
|
attested_header: block_header,
|
||||||
next_sync_committee: state.next_sync_committee,
|
next_sync_committee: next_sync_committee,
|
||||||
next_sync_committee_branch: next_sync_committee_branch,
|
next_sync_committee_branch: next_sync_committee_branch,
|
||||||
finalized_header: finalized_block_header,
|
finalized_header: finalized_block_header,
|
||||||
finality_branch: finality_branch,
|
finality_branch: finality_branch,
|
||||||
sync_aggregate: sync_aggregate,
|
sync_aggregate: sync_aggregate,
|
||||||
fork_version: state.fork.current_version)
|
fork_version: state.fork.current_version)
|
||||||
|
res = process_light_client_update(
|
||||||
check:
|
|
||||||
process_light_client_update(
|
|
||||||
store, update, state.slot, state.genesis_validators_root)
|
store, update, state.slot, state.genesis_validators_root)
|
||||||
|
|
||||||
# snapshot has been updated
|
check:
|
||||||
|
res
|
||||||
store.current_max_active_participants > 0
|
store.current_max_active_participants > 0
|
||||||
store.optimistic_header == update.attested_header
|
store.optimistic_header == update.attested_header
|
||||||
store.finalized_header == update.finalized_header
|
store.finalized_header == update.finalized_header
|
||||||
|
Loading…
x
Reference in New Issue
Block a user