2023-01-02 15:46:48 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2023-01-02 15:46:48 +00:00
|
|
|
# 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.}
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2023-01-02 15:46:48 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
unittest2,
|
|
|
|
beacon_chain/spec/forks,
|
|
|
|
beacon_chain/spec/datatypes/bellatrix,
|
2024-02-28 17:31:45 +00:00
|
|
|
beacon_chain /../ tests/testblockutil,
|
2023-01-02 15:46:48 +00:00
|
|
|
# Mock helpers
|
2024-02-28 17:31:45 +00:00
|
|
|
beacon_chain /../ tests/mocking/mock_genesis,
|
2024-04-17 15:58:47 +00:00
|
|
|
../network/history/beacon_chain_block_proof_bellatrix
|
2023-01-02 15:46:48 +00:00
|
|
|
|
|
|
|
# Test suite for the proofs:
|
|
|
|
# - HistoricalRootsProof
|
2024-07-17 09:32:05 +00:00
|
|
|
# - BeaconBlockProof
|
|
|
|
# and:
|
|
|
|
# - the chain of both proofs, BeaconChainBlockProof:
|
|
|
|
# BlockHash
|
|
|
|
# -> BeaconBlockProof
|
2023-01-02 15:46:48 +00:00
|
|
|
# -> HistoricalRootsProof
|
|
|
|
# historical_roots
|
|
|
|
#
|
|
|
|
# Note: The last test makes the others redundant, but keeping them all around
|
|
|
|
# for now as it might be sufficient to go with just HistoricalRootsProof (and
|
|
|
|
# perhaps BeaconBlockHeaderProof), see comments in beacon_chain_proofs.nim.
|
|
|
|
#
|
|
|
|
# TODO: Add more blocks to reach 1+ historical roots, to make sure that indexing
|
|
|
|
# is properly tested.
|
|
|
|
|
2024-04-17 15:58:47 +00:00
|
|
|
suite "Beacon Chain Block Proofs - Bellatrix":
|
2023-01-02 15:46:48 +00:00
|
|
|
let
|
|
|
|
cfg = block:
|
|
|
|
var res = defaultRuntimeConfig
|
|
|
|
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
|
|
|
|
res
|
|
|
|
state = newClone(initGenesisState(cfg = cfg))
|
|
|
|
var cache = StateCache()
|
|
|
|
|
|
|
|
var blocks: seq[bellatrix.SignedBeaconBlock]
|
|
|
|
# Note:
|
|
|
|
# Adding 8192 blocks. First block is genesis block and not one of these.
|
|
|
|
# Then one extra block is needed to get the historical roots, block
|
|
|
|
# roots and state roots processed.
|
|
|
|
# index i = 0 is second block.
|
|
|
|
# index i = 8190 is 8192th block and last one that is part of the first
|
|
|
|
# historical root
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT:
|
2023-01-02 15:46:48 +00:00
|
|
|
blocks.add(addTestBlock(state[], cache, cfg = cfg).bellatrixData)
|
|
|
|
|
|
|
|
# Starts from the block after genesis.
|
|
|
|
const blocksToTest = [
|
2024-02-28 17:31:45 +00:00
|
|
|
0'u64,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
SLOTS_PER_HISTORICAL_ROOT div 2,
|
|
|
|
SLOTS_PER_HISTORICAL_ROOT - 3,
|
|
|
|
SLOTS_PER_HISTORICAL_ROOT - 2,
|
|
|
|
]
|
2023-01-02 15:46:48 +00:00
|
|
|
|
2024-07-17 09:32:05 +00:00
|
|
|
test "HistoricalRootsProof for BeaconBlock":
|
2023-01-02 15:46:48 +00:00
|
|
|
let
|
|
|
|
# Historical batch of first historical root
|
|
|
|
batch = HistoricalBatch(
|
|
|
|
block_roots: getStateField(state[], block_roots).data,
|
2024-02-28 17:31:45 +00:00
|
|
|
state_roots: getStateField(state[], state_roots).data,
|
|
|
|
)
|
2023-01-02 15:46:48 +00:00
|
|
|
historical_roots = getStateField(state[], historical_roots)
|
|
|
|
|
|
|
|
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
|
|
|
|
for i in blocksToTest:
|
|
|
|
let
|
|
|
|
beaconBlock = blocks[i].message
|
|
|
|
historicalRootsIndex = getHistoricalRootsIndex(beaconBlock.slot)
|
2023-05-30 05:02:39 +00:00
|
|
|
blockRootIndex = getBlockRootsIndex(beaconBlock.slot)
|
2023-01-02 15:46:48 +00:00
|
|
|
|
|
|
|
let res = buildProof(batch, blockRootIndex)
|
|
|
|
check res.isOk()
|
|
|
|
let proof = res.get()
|
|
|
|
|
|
|
|
check verifyProof(
|
2024-02-28 17:31:45 +00:00
|
|
|
blocks[i].root, proof, historical_roots[historicalRootsIndex], blockRootIndex
|
|
|
|
)
|
2023-01-02 15:46:48 +00:00
|
|
|
|
2024-07-17 09:32:05 +00:00
|
|
|
test "BeaconBlockProof for BeaconBlock":
|
2023-01-02 15:46:48 +00:00
|
|
|
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
|
|
|
|
for i in blocksToTest:
|
2024-07-17 09:32:05 +00:00
|
|
|
let beaconBlock = blocks[i].message
|
2023-01-02 15:46:48 +00:00
|
|
|
|
2024-07-17 09:32:05 +00:00
|
|
|
let res = buildProof(beaconBlock)
|
2023-01-02 15:46:48 +00:00
|
|
|
check res.isOk()
|
|
|
|
let proof = res.get()
|
|
|
|
|
2024-07-17 09:32:05 +00:00
|
|
|
let leave = beaconBlock.body.execution_payload.block_hash
|
|
|
|
check verifyProof(leave, proof, blocks[i].root)
|
2023-01-02 15:46:48 +00:00
|
|
|
|
|
|
|
test "BeaconChainBlockProof for Execution BlockHeader":
|
|
|
|
let
|
|
|
|
# Historical batch of first historical root
|
|
|
|
batch = HistoricalBatch(
|
|
|
|
block_roots: getStateField(state[], block_roots).data,
|
2024-02-28 17:31:45 +00:00
|
|
|
state_roots: getStateField(state[], state_roots).data,
|
|
|
|
)
|
2023-01-02 15:46:48 +00:00
|
|
|
historical_roots = getStateField(state[], historical_roots)
|
|
|
|
|
|
|
|
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
|
|
|
|
for i in blocksToTest:
|
|
|
|
let
|
|
|
|
beaconBlock = blocks[i].message
|
|
|
|
# Normally we would have an execution BlockHeader that holds this
|
|
|
|
# value, but we skip the creation of that header for now and just take
|
|
|
|
# the blockHash from the execution payload.
|
2024-07-17 09:32:05 +00:00
|
|
|
blockHash = beaconBlock.body.execution_payload.block_hash
|
2023-01-02 15:46:48 +00:00
|
|
|
|
2024-07-17 09:32:05 +00:00
|
|
|
let proofRes = buildProof(batch, beaconBlock)
|
2023-01-02 15:46:48 +00:00
|
|
|
check proofRes.isOk()
|
|
|
|
let proof = proofRes.get()
|
|
|
|
|
|
|
|
check verifyProof(historical_roots, proof, blockHash)
|