Skip to content

Commit 1cfc89f

Browse files
authored
portal: cleanup of block proof tests (#3600)
- Use setup for prepration of test (blocks creation) - Remove intermediate proof testing as is redudant and would take up too much time with setup - Start immediatly from Capella fork for historical summaries test to win some time - Cleanup comments
1 parent b9e01e2 commit 1cfc89f

File tree

4 files changed

+136
-260
lines changed

4 files changed

+136
-260
lines changed

portal/tests/all_portal_tests.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ import
1313
./legacy_history_network_tests/all_history_network_tests,
1414
./history_network_tests/all_history_network_tests,
1515
./beacon_network_tests/all_beacon_network_tests,
16+
./eth_history_tests/all_eth_history_tests,
1617
./rpc_tests/all_rpc_tests

portal/tests/eth_history_tests/test_block_proof_historical_roots.nim

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2022-2024 Status Research & Development GmbH
2+
# Copyright (c) 2022-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -12,96 +12,56 @@
1212
import
1313
unittest2,
1414
beacon_chain/spec/forks,
15-
beacon_chain/spec/datatypes/bellatrix,
16-
beacon_chain /../ tests/testblockutil,
1715
# Mock helpers
16+
beacon_chain /../ tests/testblockutil,
1817
beacon_chain /../ tests/mocking/mock_genesis,
1918
../../eth_history/block_proofs/block_proof_historical_roots
2019

21-
# Test suite for the proofs:
22-
# - HistoricalRootsProof
23-
# - BeaconBlockProof
24-
# and:
25-
# - the chain of both proofs, BeaconChainBlockProof:
26-
# BlockHash
27-
# -> BeaconBlockProof
28-
# -> HistoricalRootsProof
29-
# historical_roots
20+
# Test suite for the chain of proofs BlockProofHistoricalRoots:
21+
# -> BeaconBlockProofHistoricalRoots
22+
# -> ExecutionBlockProof
3023
#
31-
# Note: The last test makes the others redundant, but keeping them all around
32-
# for now as it might be sufficient to go with just HistoricalRootsProof (and
33-
# perhaps BeaconBlockHeaderProof), see comments in beacon_chain_proofs.nim.
34-
#
35-
# TODO: Add more blocks to reach 1+ historical roots, to make sure that indexing
36-
# is properly tested.
24+
# Note: Only the full chain of proofs is tested here. The setup takes a lot of time
25+
# and testing the individual proofs is redundant.
3726

3827
suite "History Block Proofs - Historical Roots":
39-
let
40-
cfg = block:
41-
var res = defaultRuntimeConfig
42-
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
43-
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
44-
res
45-
state = newClone(initGenesisState(cfg = cfg))
46-
var cache = StateCache()
47-
48-
var blocks: seq[bellatrix.SignedBeaconBlock]
49-
# Note:
50-
# Adding 8192 blocks. First block is genesis block and not one of these.
51-
# Then one extra block is needed to get the historical roots, block
52-
# roots and state roots processed.
53-
# index i = 0 is second block.
54-
# index i = 8190 is 8192th block and last one that is part of the first
55-
# historical root
56-
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT:
57-
blocks.add(addTestBlock(state[], cache, cfg = cfg).bellatrixData)
58-
59-
# Starts from the block after genesis.
60-
const blocksToTest = [
61-
0'u64,
62-
1,
63-
2,
64-
3,
65-
SLOTS_PER_HISTORICAL_ROOT div 2,
66-
SLOTS_PER_HISTORICAL_ROOT - 3,
67-
SLOTS_PER_HISTORICAL_ROOT - 2,
68-
]
69-
70-
test "BeaconBlockProofHistoricalRoots for BeaconBlock":
28+
setup:
7129
let
72-
# Historical batch of first historical root
73-
batch = HistoricalBatch(
74-
block_roots: getStateField(state[], block_roots).data,
75-
state_roots: getStateField(state[], state_roots).data,
76-
)
77-
historical_roots = getStateField(state[], historical_roots)
78-
79-
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
80-
for i in blocksToTest:
81-
let
82-
beaconBlock = blocks[i].message
83-
historicalRootsIndex = getHistoricalRootsIndex(beaconBlock.slot)
84-
blockRootIndex = getBlockRootsIndex(beaconBlock.slot)
85-
86-
let res = buildProof(batch, blockRootIndex)
87-
check res.isOk()
88-
let proof = res.get()
89-
90-
check verifyProof(
91-
blocks[i].root, proof, historical_roots[historicalRootsIndex], blockRootIndex
92-
)
93-
94-
test "ExecutionBlockProof for Execution BlockHeader":
95-
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
96-
for i in blocksToTest:
97-
let beaconBlock = blocks[i].message
98-
99-
let res = buildProof(beaconBlock)
100-
check res.isOk()
101-
let proof = res.get()
102-
103-
let leave = beaconBlock.body.execution_payload.block_hash
104-
check verifyProof(leave, proof, blocks[i].root)
30+
cfg = block:
31+
var res = defaultRuntimeConfig
32+
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
33+
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
34+
res
35+
state = newClone(initGenesisState(cfg = cfg))
36+
var cache = StateCache()
37+
38+
var blocks: seq[bellatrix.SignedBeaconBlock]
39+
# Note:
40+
# Adding 8192 blocks. First block is genesis block and not one of these.
41+
# Then one extra block is needed to get the historical roots, block
42+
# roots and state roots processed.
43+
# index i = 0 is second block.
44+
# index i = 8190 is 8192th block and last one that is part of the first
45+
# historical root
46+
47+
# genesis + 8191 slots
48+
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT - 1:
49+
blocks.add(addTestBlock(state[], cache, cfg = cfg).bellatrixData)
50+
51+
# One more slot to hit second SLOTS_PER_HISTORICAL_ROOT, hitting first
52+
# historical root.
53+
discard addTestBlock(state[], cache, cfg = cfg)
54+
55+
# Starts from the block after genesis.
56+
const blocksToTest = [
57+
0'u64,
58+
1,
59+
2,
60+
3,
61+
SLOTS_PER_HISTORICAL_ROOT div 2,
62+
SLOTS_PER_HISTORICAL_ROOT - 3,
63+
SLOTS_PER_HISTORICAL_ROOT - 2,
64+
]
10565

10666
test "BlockProofHistoricalRoots for Execution BlockHeader":
10767
let

portal/tests/eth_history_tests/test_block_proof_historical_summaries.nim

Lines changed: 42 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,112 +12,57 @@
1212
import
1313
unittest2,
1414
beacon_chain/spec/forks,
15-
beacon_chain/spec/datatypes/capella,
16-
beacon_chain /../ tests/testblockutil,
1715
# Mock helpers
16+
beacon_chain /../ tests/testblockutil,
1817
beacon_chain /../ tests/mocking/mock_genesis,
1918
../../eth_history/block_proofs/block_proof_historical_summaries
2019

21-
# Test suite for the proofs:
22-
# - historicalSummariesProof
23-
# - BeaconBlockProof
24-
# and as last
25-
# - the chain of proofs, BeaconChainBlockProof:
26-
# BlockHash
27-
# -> BeaconBlockProof
28-
# -> historicalSummariesProof
29-
# historical_summaries
30-
#
31-
# Note: The last test makes the others redundant, but keeping them all around
32-
# for now as it might be sufficient to go with just historicalSummariesProof
33-
# (and perhaps BeaconBlockHeaderProof), see comments in beacon_chain_proofs.nim.
20+
# Test suite for the chain of proofs BlockProofHistoricalSummaries:
21+
# -> BeaconBlockProofHistoricalSummaries
22+
# -> ExecutionBlockProof
3423
#
35-
# TODO:
36-
# - Add more blocks to reach 1+ historical summaries, to make sure that
37-
# indexing is properly tested.
38-
# - Adjust tests to test usage of historical_summaries and historical_roots
39-
# together.
24+
# Note: Only the full chain of proofs is tested here. The setup takes a lot of time
25+
# and testing the individual proofs is redundant.
4026

4127
suite "History Block Proofs - Historical Summaries":
42-
let
43-
cfg = block:
44-
var res = defaultRuntimeConfig
45-
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
46-
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
47-
# res.CAPELLA_FORK_EPOCH = GENESIS_EPOCH
48-
res.CAPELLA_FORK_EPOCH = Epoch(256)
49-
res
50-
state = newClone(initGenesisState(cfg = cfg))
51-
var cache = StateCache()
52-
53-
var blocks: seq[capella.SignedBeaconBlock]
54-
# Note:
55-
# Adding 8192*2 blocks. First block is genesis block and not one of these.
56-
# Then one extra block is needed to get the historical roots, block
57-
# roots and state roots processed.
58-
# index i = 0 is second block.
59-
# index i = 8190 is 8192th block and last one that is part of the first
60-
# historical root
61-
62-
# genesis + 8191 slots, next one will be capella fork
63-
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT - 1:
28+
setup:
29+
let
30+
cfg = block:
31+
var res = defaultRuntimeConfig
32+
res.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
33+
res.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
34+
res.CAPELLA_FORK_EPOCH = GENESIS_EPOCH
35+
res
36+
state = newClone(initGenesisState(cfg = cfg))
37+
var cache = StateCache()
38+
39+
var blocks: seq[capella.SignedBeaconBlock]
40+
# Note:
41+
# Adding 8192 blocks. First block is genesis block and not one of these.
42+
# Then one extra block is needed to get the historical roots, block
43+
# roots and state roots processed.
44+
# index i = 0 is second block.
45+
# index i = 8190 is 8192th block and last one that is part of the first
46+
# historical summaries root
47+
48+
# genesis + 8191 slots
49+
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT - 1:
50+
blocks.add(addTestBlock(state[], cache, cfg = cfg).capellaData)
51+
52+
# One more slot to hit second SLOTS_PER_HISTORICAL_ROOT, hitting first
53+
# historical_summary root.
6454
discard addTestBlock(state[], cache, cfg = cfg)
6555

66-
# slot 8192 -> 16383
67-
for i in 0 ..< SLOTS_PER_HISTORICAL_ROOT:
68-
blocks.add(addTestBlock(state[], cache, cfg = cfg).capellaData)
69-
70-
# One more slot to hit second SLOTS_PER_HISTORICAL_ROOT, hitting first
71-
# historical_summary.
72-
discard addTestBlock(state[], cache, cfg = cfg)
73-
74-
# Starts from the block after genesis.
75-
const blocksToTest = [
76-
0'u64,
77-
1,
78-
2,
79-
3,
80-
SLOTS_PER_HISTORICAL_ROOT div 2,
81-
SLOTS_PER_HISTORICAL_ROOT - 3,
82-
SLOTS_PER_HISTORICAL_ROOT - 2,
83-
]
84-
85-
test "BeaconBlockProofHistoricalSummaries for BeaconBlock":
86-
let blockRoots = getStateField(state[], block_roots).data
87-
88-
withState(state[]):
89-
when consensusFork >= ConsensusFork.Capella:
90-
let historical_summaries = forkyState.data.historical_summaries
91-
92-
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
93-
for i in blocksToTest:
94-
let
95-
beaconBlock = blocks[i].message
96-
historicalRootsIndex = getHistoricalSummariesIndex(beaconBlock.slot, cfg)
97-
blockRootIndex = getBlockRootsIndex(beaconBlock.slot)
98-
99-
let res = buildProof(blockRoots, blockRootIndex)
100-
check res.isOk()
101-
let proof = res.get()
102-
103-
check verifyProof(
104-
blocks[i].root,
105-
proof,
106-
historical_summaries[historicalRootsIndex].block_summary_root,
107-
blockRootIndex,
108-
)
109-
110-
test "ExecutionBlockProof for Execution BlockHeader":
111-
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
112-
for i in blocksToTest:
113-
let beaconBlock = blocks[i].message
114-
115-
let res = block_proof_historical_summaries.buildProof(beaconBlock)
116-
check res.isOk()
117-
let proof = res.get()
118-
119-
let leave = beaconBlock.body.execution_payload.block_hash
120-
check verifyProof(leave, proof, blocks[i].root)
56+
# Starts from the block after genesis.
57+
const blocksToTest = [
58+
0'u64,
59+
1,
60+
2,
61+
3,
62+
SLOTS_PER_HISTORICAL_ROOT div 2,
63+
SLOTS_PER_HISTORICAL_ROOT - 3,
64+
SLOTS_PER_HISTORICAL_ROOT - 2,
65+
]
12166

12267
test "BlockProofHistoricalSummaries for Execution BlockHeader":
12368
let blockRoots = getStateField(state[], block_roots).data

0 commit comments

Comments
 (0)