2022-03-17 22:26:56 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2022 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.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Status libraries
|
|
|
|
chronos, eth/keys,
|
|
|
|
# Beacon chain internals
|
|
|
|
../beacon_chain/consensus_object_pools/
|
|
|
|
[block_clearance, block_quarantine, blockchain_dag],
|
|
|
|
../beacon_chain/gossip_processing/light_client_processor,
|
|
|
|
../beacon_chain/spec/[beacon_time, light_client_sync, state_transition],
|
|
|
|
# Test utilities
|
|
|
|
./testutil, ./testdbutil
|
|
|
|
|
|
|
|
suite "Light client processor" & preset():
|
|
|
|
let
|
|
|
|
cfg = block:
|
|
|
|
var res = defaultRuntimeConfig
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH + 1
|
|
|
|
res
|
|
|
|
|
|
|
|
const numValidators = SLOTS_PER_EPOCH
|
|
|
|
let
|
|
|
|
validatorMonitor = newClone(ValidatorMonitor.init())
|
|
|
|
dag = ChainDAGRef.init(
|
|
|
|
cfg, makeTestDB(numValidators), validatorMonitor, {},
|
2022-06-14 09:03:39 +00:00
|
|
|
lightClientDataServe = true,
|
|
|
|
lightClientDataImportMode = LightClientDataImportMode.OnlyNew)
|
2022-03-17 22:26:56 +00:00
|
|
|
quarantine = newClone(Quarantine.init())
|
2022-04-08 16:22:49 +00:00
|
|
|
taskpool = Taskpool.new()
|
2022-03-17 22:26:56 +00:00
|
|
|
var verifier = BatchVerifier(rng: keys.newRng(), taskpool: taskpool)
|
|
|
|
|
|
|
|
var cache: StateCache
|
|
|
|
proc addBlocks(blocks: uint64, syncCommitteeRatio: float) =
|
|
|
|
for blck in makeTestBlocks(dag.headState, cache, blocks.int,
|
|
|
|
attested = true, syncCommitteeRatio, cfg):
|
|
|
|
let added =
|
|
|
|
case blck.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
const nilCallback = OnPhase0BlockAdded(nil)
|
|
|
|
dag.addHeadBlock(verifier, blck.phase0Data, nilCallback)
|
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
const nilCallback = OnAltairBlockAdded(nil)
|
|
|
|
dag.addHeadBlock(verifier, blck.altairData, nilCallback)
|
|
|
|
of BeaconBlockFork.Bellatrix:
|
|
|
|
const nilCallback = OnBellatrixBlockAdded(nil)
|
|
|
|
dag.addHeadBlock(verifier, blck.bellatrixData, nilCallback)
|
|
|
|
doAssert added.isOk()
|
|
|
|
dag.updateHead(added[], quarantine[])
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
addBlocks(SLOTS_PER_EPOCH, 0.82)
|
2022-03-17 22:26:56 +00:00
|
|
|
let
|
2022-04-08 16:22:49 +00:00
|
|
|
genesis_validators_root = dag.genesis_validators_root
|
2022-03-17 22:26:56 +00:00
|
|
|
trustedBlockRoot = dag.head.root
|
2022-05-31 10:45:37 +00:00
|
|
|
proc getTrustedBlockRoot(): Option[Eth2Digest] =
|
|
|
|
some trustedBlockRoot
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
lowPeriod = 0.SyncCommitteePeriod
|
|
|
|
lastPeriodWithSupermajority = 3.SyncCommitteePeriod
|
|
|
|
highPeriod = 5.SyncCommitteePeriod
|
|
|
|
for period in lowPeriod .. highPeriod:
|
|
|
|
const numFilledEpochsPerPeriod = 3
|
|
|
|
let slot = ((period + 1).start_epoch - numFilledEpochsPerPeriod).start_slot
|
|
|
|
var info: ForkedEpochInfo
|
|
|
|
doAssert process_slots(cfg, dag.headState, slot,
|
|
|
|
cache, info, flags = {}).isOk()
|
|
|
|
let syncCommitteeRatio =
|
|
|
|
if period > lastPeriodWithSupermajority:
|
2022-05-23 12:02:54 +00:00
|
|
|
0.62
|
2022-03-17 22:26:56 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
0.82
|
2022-03-17 22:26:56 +00:00
|
|
|
addBlocks(numFilledEpochsPerPeriod * SLOTS_PER_EPOCH, syncCommitteeRatio)
|
|
|
|
|
|
|
|
setup:
|
|
|
|
var time = chronos.seconds(0)
|
|
|
|
proc getBeaconTime(): BeaconTime =
|
|
|
|
BeaconTime(ns_since_genesis: time.nanoseconds)
|
|
|
|
func setTimeToSlot(slot: Slot) =
|
|
|
|
time = chronos.seconds((slot * SECONDS_PER_SLOT).int64)
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
var numOnStoreInitializedCalls = 0
|
|
|
|
proc onStoreInitialized() = inc numOnStoreInitializedCalls
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
let store = (ref Option[LightClientStore])()
|
|
|
|
var
|
|
|
|
processor = LightClientProcessor.new(
|
2022-05-31 10:45:37 +00:00
|
|
|
false, "", "", cfg, genesis_validators_root,
|
|
|
|
store, getBeaconTime, getTrustedBlockRoot, onStoreInitialized)
|
|
|
|
res: Result[bool, BlockError]
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
test "Sync" & preset():
|
2022-03-17 22:26:56 +00:00
|
|
|
let bootstrap = dag.getLightClientBootstrap(trustedBlockRoot)
|
2022-03-20 10:58:59 +00:00
|
|
|
check bootstrap.isOk
|
2022-03-17 22:26:56 +00:00
|
|
|
setTimeToSlot(bootstrap.get.header.slot)
|
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), bootstrap.get)
|
|
|
|
check:
|
|
|
|
res.isOk
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 1
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
for period in lowPeriod .. lastPeriodWithSupermajority:
|
2022-05-23 12:02:54 +00:00
|
|
|
let update = dag.getLightClientUpdateForPeriod(period)
|
2022-03-17 22:26:56 +00:00
|
|
|
check update.isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
setTimeToSlot(update.get.signature_slot)
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), update.get)
|
|
|
|
check:
|
|
|
|
res.isOk
|
|
|
|
store[].isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
if update.get.finalized_header.slot > bootstrap.get.header.slot:
|
|
|
|
store[].get.finalized_header == update.get.finalized_header
|
|
|
|
else:
|
|
|
|
store[].get.finalized_header == bootstrap.get.header
|
2022-03-17 22:26:56 +00:00
|
|
|
store[].get.optimistic_header == update.get.attested_header
|
|
|
|
|
|
|
|
for period in lastPeriodWithSupermajority + 1 .. highPeriod:
|
2022-05-23 12:02:54 +00:00
|
|
|
let update = dag.getLightClientUpdateForPeriod(period)
|
2022-03-17 22:26:56 +00:00
|
|
|
check update.isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
setTimeToSlot(update.get.signature_slot)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
for i in 0 ..< 2:
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), update.get)
|
|
|
|
check:
|
2022-05-23 12:02:54 +00:00
|
|
|
res.isOk
|
2022-03-17 22:26:56 +00:00
|
|
|
store[].isSome
|
|
|
|
store[].get.best_valid_update.isSome
|
|
|
|
store[].get.best_valid_update.get == update.get
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
proc applyDuplicate() = # Reduce stack size by making this a `proc`
|
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), update.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.Duplicate
|
|
|
|
store[].isSome
|
|
|
|
store[].get.best_valid_update.isSome
|
|
|
|
store[].get.best_valid_update.get == update.get
|
|
|
|
|
|
|
|
applyDuplicate()
|
|
|
|
time += chronos.minutes(15)
|
|
|
|
for _ in 0 ..< 150:
|
|
|
|
applyDuplicate()
|
|
|
|
time += chronos.seconds(5)
|
|
|
|
time += chronos.minutes(15)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), update.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.Duplicate
|
|
|
|
store[].isSome
|
|
|
|
store[].get.best_valid_update.isNone
|
|
|
|
if store[].get.finalized_header == update.get.attested_header:
|
|
|
|
break
|
|
|
|
check store[].get.finalized_header == update.get.finalized_header
|
|
|
|
check store[].get.finalized_header == update.get.attested_header
|
|
|
|
|
|
|
|
let
|
|
|
|
previousFinalized = store[].get.finalized_header
|
|
|
|
finalityUpdate = dag.getLightClientFinalityUpdate()
|
|
|
|
check finalityUpdate.isSome
|
|
|
|
setTimeToSlot(finalityUpdate.get.signature_slot)
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
2022-05-23 12:02:54 +00:00
|
|
|
MsgSource.gossip, getBeaconTime(), finalityUpdate.get)
|
2022-03-17 22:26:56 +00:00
|
|
|
if res.isOk:
|
|
|
|
check:
|
|
|
|
store[].isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
store[].get.finalized_header == previousFinalized
|
|
|
|
store[].get.best_valid_update.isSome
|
|
|
|
store[].get.best_valid_update.get.matches(finalityUpdate.get)
|
|
|
|
store[].get.optimistic_header == finalityUpdate.get.attested_header
|
2022-03-17 22:26:56 +00:00
|
|
|
else:
|
|
|
|
check res.error == BlockError.Duplicate
|
2022-05-31 10:45:37 +00:00
|
|
|
check numOnStoreInitializedCalls == 1
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
test "Invalid bootstrap" & preset():
|
|
|
|
var bootstrap = dag.getLightClientBootstrap(trustedBlockRoot)
|
2022-03-20 10:58:59 +00:00
|
|
|
check bootstrap.isOk
|
2022-03-17 22:26:56 +00:00
|
|
|
bootstrap.get.header.slot.inc()
|
|
|
|
setTimeToSlot(bootstrap.get.header.slot)
|
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), bootstrap.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.Invalid
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 0
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
test "Duplicate bootstrap" & preset():
|
|
|
|
let bootstrap = dag.getLightClientBootstrap(trustedBlockRoot)
|
2022-03-20 10:58:59 +00:00
|
|
|
check bootstrap.isOk
|
2022-03-17 22:26:56 +00:00
|
|
|
setTimeToSlot(bootstrap.get.header.slot)
|
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), bootstrap.get)
|
|
|
|
check:
|
|
|
|
res.isOk
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 1
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), bootstrap.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.Duplicate
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 1
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
test "Missing bootstrap (update)" & preset():
|
2022-05-23 12:02:54 +00:00
|
|
|
let update = dag.getLightClientUpdateForPeriod(lowPeriod)
|
2022-03-17 22:26:56 +00:00
|
|
|
check update.isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
setTimeToSlot(update.get.signature_slot)
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), update.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.MissingParent
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 0
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
test "Missing bootstrap (finality update)" & preset():
|
|
|
|
let finalityUpdate = dag.getLightClientFinalityUpdate()
|
|
|
|
check finalityUpdate.isSome
|
|
|
|
setTimeToSlot(finalityUpdate.get.signature_slot)
|
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), finalityUpdate.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.MissingParent
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 0
|
2022-05-23 12:02:54 +00:00
|
|
|
|
2022-03-17 22:26:56 +00:00
|
|
|
test "Missing bootstrap (optimistic update)" & preset():
|
2022-05-23 12:02:54 +00:00
|
|
|
let optimisticUpdate = dag.getLightClientOptimisticUpdate()
|
2022-03-17 22:26:56 +00:00
|
|
|
check optimisticUpdate.isSome
|
2022-05-23 12:02:54 +00:00
|
|
|
setTimeToSlot(optimisticUpdate.get.signature_slot)
|
2022-03-17 22:26:56 +00:00
|
|
|
res = processor[].storeObject(
|
|
|
|
MsgSource.gossip, getBeaconTime(), optimisticUpdate.get)
|
|
|
|
check:
|
|
|
|
res.isErr
|
|
|
|
res.error == BlockError.MissingParent
|
2022-05-31 10:45:37 +00:00
|
|
|
numOnStoreInitializedCalls == 0
|