|
12 | 12 | import |
13 | 13 | unittest2, |
14 | 14 | beacon_chain/spec/forks, |
15 | | - beacon_chain/spec/datatypes/capella, |
16 | | - beacon_chain /../ tests/testblockutil, |
17 | 15 | # Mock helpers |
| 16 | + beacon_chain /../ tests/testblockutil, |
18 | 17 | beacon_chain /../ tests/mocking/mock_genesis, |
19 | 18 | ../../eth_history/block_proofs/block_proof_historical_summaries |
20 | 19 |
|
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 |
34 | 23 | # |
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. |
40 | 26 |
|
41 | 27 | 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. |
64 | 54 | discard addTestBlock(state[], cache, cfg = cfg) |
65 | 55 |
|
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 | + ] |
121 | 66 |
|
122 | 67 | test "BlockProofHistoricalSummaries for Execution BlockHeader": |
123 | 68 | let blockRoots = getStateField(state[], block_roots).data |
|
0 commit comments