2021-09-13 16:35:13 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
import
|
2021-09-13 16:35:13 +00:00
|
|
|
# Standard library
|
|
|
|
std/[algorithm, sequtils, sets],
|
|
|
|
# Status libraries
|
|
|
|
stew/bitops2,
|
|
|
|
# Beacon chain internals
|
|
|
|
../../../beacon_chain/spec/
|
2021-11-18 12:02:43 +00:00
|
|
|
[beaconstate, forks, helpers, light_client_sync, signatures, state_transition],
|
2021-09-13 16:35:13 +00:00
|
|
|
# Mock helpers
|
|
|
|
../../mocking/[mock_blocks, mock_genesis],
|
|
|
|
# Test utilities
|
|
|
|
../../testutil, ../../testblockutil
|
|
|
|
|
2021-11-08 10:30:52 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.4/tests/core/pyspec/eth2spec/test/helpers/sync_committee.py#L27-L44
|
2021-09-13 16:35:13 +00:00
|
|
|
proc compute_aggregate_sync_committee_signature(
|
|
|
|
forked: ForkedHashedBeaconState,
|
|
|
|
participants: openArray[ValidatorIndex],
|
|
|
|
block_root = ZERO_HASH): ValidatorSig =
|
2021-10-18 16:37:27 +00:00
|
|
|
template state: untyped {.inject.} = forked.altairData.data
|
2021-09-13 16:35:13 +00:00
|
|
|
|
|
|
|
if len(participants) == 0:
|
|
|
|
return ValidatorSig.infinity
|
|
|
|
|
|
|
|
let
|
2021-10-13 14:24:36 +00:00
|
|
|
root =
|
2021-09-13 16:35:13 +00:00
|
|
|
if block_root != ZERO_HASH: block_root
|
2021-10-18 16:37:27 +00:00
|
|
|
else: mockBlockForNextSlot(forked).altairData.message.parent_root
|
2021-09-13 16:35:13 +00:00
|
|
|
signing_root = sync_committee_msg_signing_root(
|
|
|
|
state.fork, state.slot.epoch, state.genesis_validators_root, root)
|
2021-10-13 14:24:36 +00:00
|
|
|
|
|
|
|
var
|
2021-09-13 16:35:13 +00:00
|
|
|
aggregateSig {.noInit.}: AggregateSignature
|
|
|
|
initialized = false
|
|
|
|
for validator_index in participants:
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
2021-09-13 16:35:13 +00:00
|
|
|
privkey = MockPrivKeys[validator_index]
|
|
|
|
signature = blsSign(privkey, signing_root.data)
|
|
|
|
if not initialized:
|
|
|
|
initialized = true
|
|
|
|
aggregateSig.init(signature)
|
|
|
|
else:
|
|
|
|
aggregateSig.aggregate(signature)
|
|
|
|
aggregateSig.finish.toValidatorSig
|
|
|
|
|
|
|
|
proc block_for_next_slot(
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forked: var ForkedHashedBeaconState,
|
|
|
|
cache: var StateCache,
|
|
|
|
withAttestations = false): ForkedSignedBeaconBlock =
|
2021-10-18 16:37:27 +00:00
|
|
|
template state: untyped {.inject.} = forked.altairData.data
|
2021-09-13 16:35:13 +00:00
|
|
|
|
|
|
|
let attestations =
|
|
|
|
if withAttestations:
|
2021-11-18 12:02:43 +00:00
|
|
|
let block_root = withState(forked): state.latest_block_root()
|
|
|
|
makeFullAttestations(forked, block_root, state.slot, cache)
|
2021-09-13 16:35:13 +00:00
|
|
|
else:
|
|
|
|
@[]
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-09-13 16:35:13 +00:00
|
|
|
addTestBlock(
|
2021-11-18 12:02:43 +00:00
|
|
|
forked, cache, attestations = attestations, cfg = cfg)
|
2021-09-13 16:35:13 +00:00
|
|
|
|
|
|
|
let full_sync_committee_bits = block:
|
|
|
|
var res: BitArray[SYNC_COMMITTEE_SIZE]
|
|
|
|
res.bytes.fill(byte.high)
|
|
|
|
res
|
|
|
|
|
|
|
|
suite "Ethereum Foundation - Altair - Unittests - Sync protocol" & preset():
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
2021-09-13 16:35:13 +00:00
|
|
|
cfg = block:
|
|
|
|
var res = defaultRuntimeConfig
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res
|
|
|
|
genesisState = newClone(initGenesisState(cfg = cfg))
|
|
|
|
|
2021-11-08 10:30:52 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.4/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py#L22-L78
|
2021-09-13 16:35:13 +00:00
|
|
|
test "process_light_client_update_not_updated":
|
|
|
|
var forked = assignClone(genesisState[])
|
2021-10-18 16:37:27 +00:00
|
|
|
template state: untyped {.inject.} = forked[].altairData.data
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-09-13 16:35:13 +00:00
|
|
|
let pre_snapshot = LightClientSnapshot(
|
|
|
|
current_sync_committee: state.current_sync_committee,
|
|
|
|
next_sync_committee: state.next_sync_committee)
|
|
|
|
var store = LightClientStore(snapshot: pre_snapshot)
|
|
|
|
|
|
|
|
# Block at slot 1 doesn't increase sync committee period,
|
|
|
|
# so it won't update snapshot
|
|
|
|
var cache = StateCache()
|
|
|
|
let
|
2021-10-18 16:37:27 +00:00
|
|
|
signed_block = block_for_next_slot(cfg, forked[], cache).altairData
|
2021-09-13 16:35:13 +00:00
|
|
|
block_header = BeaconBlockHeader(
|
|
|
|
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
|
|
|
|
all_pubkeys = state.validators.mapIt(it.pubkey)
|
|
|
|
committee = state.current_sync_committee.pubkeys
|
|
|
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
|
|
|
sync_committee_bits = full_sync_committee_bits
|
|
|
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
|
|
|
forked[], committee)
|
2021-10-13 14:24:36 +00:00
|
|
|
var next_sync_committee_branch:
|
2021-09-13 16:35:13 +00:00
|
|
|
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
|
|
|
|
|
|
|
# Ensure that finality checkpoint is genesis
|
|
|
|
check: state.finalized_checkpoint.epoch == 0
|
|
|
|
# Finality is unchanged
|
|
|
|
let finality_header = BeaconBlockHeader()
|
|
|
|
var finality_branch: array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
|
|
|
|
|
|
|
let update = LightClientUpdate(
|
|
|
|
header: block_header,
|
|
|
|
next_sync_committee: state.next_sync_committee,
|
|
|
|
next_sync_committee_branch: next_sync_committee_branch,
|
|
|
|
finality_header: finality_header,
|
|
|
|
finality_branch: finality_branch,
|
|
|
|
sync_committee_bits: sync_committee_bits,
|
|
|
|
sync_committee_signature: sync_committee_signature,
|
|
|
|
fork_version: state.fork.current_version)
|
2021-10-13 14:24:36 +00:00
|
|
|
|
|
|
|
check:
|
2021-09-13 16:35:13 +00:00
|
|
|
process_light_client_update(
|
|
|
|
store, update, state.slot, state.genesis_validators_root)
|
|
|
|
|
|
|
|
len(store.valid_updates) == 1
|
|
|
|
store.valid_updates.pop() == update
|
|
|
|
store.snapshot == pre_snapshot
|
|
|
|
|
2021-11-09 08:54:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.5/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py#L81-L144
|
2021-09-13 16:35:13 +00:00
|
|
|
test "process_light_client_update_timeout":
|
|
|
|
var forked = assignClone(genesisState[])
|
2021-10-18 16:37:27 +00:00
|
|
|
template state: untyped {.inject.} = forked[].altairData.data
|
2021-09-13 16:35:13 +00:00
|
|
|
|
|
|
|
let pre_snapshot = LightClientSnapshot(
|
|
|
|
current_sync_committee: state.current_sync_committee,
|
|
|
|
next_sync_committee: state.next_sync_committee)
|
|
|
|
var store = LightClientStore(snapshot: pre_snapshot)
|
|
|
|
|
|
|
|
# Forward to next sync committee period
|
|
|
|
var
|
|
|
|
cache = StateCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
info = ForkedEpochInfo()
|
2021-09-13 16:35:13 +00:00
|
|
|
doAssert process_slots(
|
2021-10-13 14:24:36 +00:00
|
|
|
cfg, forked[], Slot(SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD),
|
|
|
|
cache, info, flags = {})
|
2021-09-13 16:35:13 +00:00
|
|
|
let
|
2021-11-02 20:32:34 +00:00
|
|
|
snapshot_period = sync_committee_period(pre_snapshot.header.slot)
|
|
|
|
update_period = sync_committee_period(state.slot)
|
2021-09-13 16:35:13 +00:00
|
|
|
check: snapshot_period + 1 == update_period
|
|
|
|
|
|
|
|
let
|
2021-10-18 16:37:27 +00:00
|
|
|
signed_block = block_for_next_slot(cfg, forked[], cache).altairData
|
2021-09-13 16:35:13 +00:00
|
|
|
block_header = BeaconBlockHeader(
|
|
|
|
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
|
|
|
|
all_pubkeys = state.validators.mapIt(it.pubkey)
|
|
|
|
committee = state.current_sync_committee.pubkeys
|
|
|
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
|
|
|
sync_committee_bits = full_sync_committee_bits
|
|
|
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
|
|
|
forked[], committee, block_root = block_header.hash_tree_root())
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-09-13 16:35:13 +00:00
|
|
|
# Sync committee is updated
|
2021-10-13 14:24:36 +00:00
|
|
|
var next_sync_committee_branch {.noinit.}:
|
2021-09-13 16:35:13 +00:00
|
|
|
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
|
|
|
build_proof(state, NEXT_SYNC_COMMITTEE_INDEX, next_sync_committee_branch)
|
|
|
|
# Finality is unchanged
|
|
|
|
let finality_header = BeaconBlockHeader()
|
|
|
|
var finality_branch: array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
|
|
|
|
|
|
|
let update = LightClientUpdate(
|
|
|
|
header: block_header,
|
|
|
|
next_sync_committee: state.next_sync_committee,
|
|
|
|
next_sync_committee_branch: next_sync_committee_branch,
|
|
|
|
finality_header: finality_header,
|
|
|
|
finality_branch: finality_branch,
|
|
|
|
sync_committee_bits: sync_committee_bits,
|
|
|
|
sync_committee_signature: sync_committee_signature,
|
|
|
|
fork_version: state.fork.current_version)
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
check:
|
2021-09-13 16:35:13 +00:00
|
|
|
process_light_client_update(
|
|
|
|
store, update, state.slot, state.genesis_validators_root)
|
|
|
|
|
|
|
|
# snapshot has been updated
|
|
|
|
len(store.valid_updates) == 0
|
|
|
|
store.snapshot.header == update.header
|
|
|
|
|
2021-10-26 18:42:48 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.3/tests/core/pyspec/eth2spec/test/altair/unittests/test_sync_protocol.py#L150-L218
|
2021-09-13 16:35:13 +00:00
|
|
|
test "process_light_client_update_finality_updated":
|
|
|
|
var forked = assignClone(genesisState[])
|
2021-10-18 16:37:27 +00:00
|
|
|
template state: untyped {.inject.} = forked[].altairData.data
|
2021-09-13 16:35:13 +00:00
|
|
|
|
|
|
|
let pre_snapshot = LightClientSnapshot(
|
|
|
|
current_sync_committee: state.current_sync_committee,
|
|
|
|
next_sync_committee: state.next_sync_committee)
|
|
|
|
var store = LightClientStore(snapshot: pre_snapshot)
|
|
|
|
|
|
|
|
# Change finality
|
2021-10-13 14:24:36 +00:00
|
|
|
var
|
2021-09-13 16:35:13 +00:00
|
|
|
cache = StateCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
info = ForkedEpochInfo()
|
2021-09-13 16:35:13 +00:00
|
|
|
blocks = newSeq[ForkedSignedBeaconBlock]()
|
|
|
|
doAssert process_slots(
|
2021-10-13 14:24:36 +00:00
|
|
|
cfg, forked[], Slot(SLOTS_PER_EPOCH * 2), cache, info, flags = {})
|
|
|
|
for epoch in 0 ..< 3:
|
2021-09-13 16:35:13 +00:00
|
|
|
for slot in 0 ..< SLOTS_PER_EPOCH:
|
2021-10-13 14:24:36 +00:00
|
|
|
blocks.add block_for_next_slot(cfg, forked[], cache,
|
2021-09-13 16:35:13 +00:00
|
|
|
withAttestations = true)
|
|
|
|
# Ensure that finality checkpoint has changed
|
|
|
|
check: state.finalized_checkpoint.epoch == 3
|
|
|
|
# Ensure that it's same period
|
|
|
|
let
|
2021-11-02 20:32:34 +00:00
|
|
|
snapshot_period = sync_committee_period(pre_snapshot.header.slot)
|
|
|
|
update_period = sync_committee_period(state.slot)
|
2021-09-13 16:35:13 +00:00
|
|
|
check: snapshot_period == update_period
|
|
|
|
|
|
|
|
# Updated sync_committee and finality
|
2021-10-13 14:24:36 +00:00
|
|
|
var next_sync_committee_branch:
|
2021-09-13 16:35:13 +00:00
|
|
|
array[log2trunc(NEXT_SYNC_COMMITTEE_INDEX), Eth2Digest]
|
|
|
|
let
|
2021-10-18 16:37:27 +00:00
|
|
|
finalized_block = blocks[SLOTS_PER_EPOCH - 1].altairData
|
2021-09-13 16:35:13 +00:00
|
|
|
finalized_block_header = BeaconBlockHeader(
|
|
|
|
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())
|
2021-10-13 14:24:36 +00:00
|
|
|
check:
|
|
|
|
finalized_block_header.slot ==
|
2021-09-13 16:35:13 +00:00
|
|
|
compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)
|
2021-10-13 14:24:36 +00:00
|
|
|
finalized_block_header.hash_tree_root() ==
|
2021-09-13 16:35:13 +00:00
|
|
|
state.finalized_checkpoint.root
|
2021-10-13 14:24:36 +00:00
|
|
|
var finality_branch {.noinit.}:
|
2021-09-13 16:35:13 +00:00
|
|
|
array[log2trunc(FINALIZED_ROOT_INDEX), Eth2Digest]
|
|
|
|
build_proof(state, FINALIZED_ROOT_INDEX, finality_branch)
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-09-13 16:35:13 +00:00
|
|
|
# Build block header
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
2021-10-18 16:37:27 +00:00
|
|
|
blck = mockBlock(forked[], state.slot, cfg = cfg).altairData.message
|
2021-09-13 16:35:13 +00:00
|
|
|
block_header = BeaconBlockHeader(
|
|
|
|
slot: blck.slot,
|
|
|
|
proposer_index: blck.proposer_index,
|
|
|
|
parent_root: blck.parent_root,
|
|
|
|
state_root: state.hash_tree_root(),
|
|
|
|
body_root: blck.body.hash_tree_root())
|
|
|
|
|
|
|
|
# Sync committee signing the finalized_block_header
|
|
|
|
all_pubkeys = state.validators.mapIt(it.pubkey)
|
|
|
|
committee = state.current_sync_committee.pubkeys
|
|
|
|
.mapIt(all_pubkeys.find(it).ValidatorIndex)
|
|
|
|
sync_committee_bits = full_sync_committee_bits
|
|
|
|
sync_committee_signature = compute_aggregate_sync_committee_signature(
|
|
|
|
forked[], committee, block_root = block_header.hash_tree_root())
|
|
|
|
|
|
|
|
update = LightClientUpdate(
|
|
|
|
header: finalized_block_header,
|
|
|
|
next_sync_committee: state.next_sync_committee,
|
|
|
|
next_sync_committee_branch: next_sync_committee_branch,
|
|
|
|
finality_header: block_header,
|
|
|
|
finality_branch: finality_branch,
|
|
|
|
sync_committee_bits: sync_committee_bits,
|
|
|
|
sync_committee_signature: sync_committee_signature,
|
|
|
|
fork_version: state.fork.current_version)
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
check:
|
2021-09-13 16:35:13 +00:00
|
|
|
process_light_client_update(
|
|
|
|
store, update, state.slot, state.genesis_validators_root)
|
|
|
|
|
|
|
|
# snapshot has been updated
|
|
|
|
len(store.valid_updates) == 0
|
|
|
|
store.snapshot.header == update.header
|