Skip to content

Commit 2d77b23

Browse files
authored
Stateless: Build witnesses when executing blocks in forked chain (#3548)
1 parent 9fe12d1 commit 2d77b23

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ proc validateBlock(c: ForkedChainRef,
462462
parentTxFrame=cast[uint](parentFrame),
463463
txFrame=cast[uint](txFrame)
464464

465-
var receipts = c.processBlock(parent.header, txFrame, blk, blkHash, finalized).valueOr:
465+
var receipts = c.processBlock(parent, txFrame, blk, blkHash, finalized).valueOr:
466466
txFrame.dispose()
467467
return err(error)
468468

execution_chain/core/chain/forked_chain/chain_private.nim

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import
1616
../../../common,
1717
../../../db/core_db,
1818
../../../evm/types,
19-
../../../evm/state
19+
../../../evm/state,
20+
../../../stateless/witness_generation,
21+
./chain_branch
2022

2123
proc writeBaggage*(c: ForkedChainRef,
2224
blk: Block, blkHash: Hash32,
@@ -54,7 +56,7 @@ template updateSnapshot*(c: ForkedChainRef,
5456
c.lastSnapshots[pos] = txFrame
5557

5658
proc processBlock*(c: ForkedChainRef,
57-
parent: Header,
59+
parentBlk: BlockRef,
5860
txFrame: CoreDbTxRef,
5961
blk: Block,
6062
blkHash: Hash32,
@@ -63,21 +65,39 @@ proc processBlock*(c: ForkedChainRef,
6365
blk.header
6466

6567
let vmState = BaseVMState()
66-
vmState.init(parent, header, c.com, txFrame)
68+
vmState.init(parentBlk.header, header, c.com, txFrame)
6769

6870
?c.com.validateHeaderAndKinship(blk, vmState.parent, txFrame)
6971

70-
# When processing a finalized block, we optimistically assume that the state
71-
# root will check out and delay such validation for when it's time to persist
72-
# changes to disk
73-
?vmState.processBlock(
74-
blk,
75-
skipValidation = false,
76-
skipReceipts = false,
77-
skipUncles = true,
78-
skipStateRootCheck = finalized and not c.eagerStateRoot,
79-
taskpool = c.com.taskpool,
80-
)
72+
template processBlock(): auto =
73+
# When processing a finalized block, we optimistically assume that the state
74+
# root will check out and delay such validation for when it's time to persist
75+
# changes to disk
76+
?vmState.processBlock(
77+
blk,
78+
skipValidation = false,
79+
skipReceipts = false,
80+
skipUncles = true,
81+
skipStateRootCheck = finalized and not c.eagerStateRoot,
82+
taskpool = c.com.taskpool,
83+
)
84+
85+
if not vmState.com.statelessProviderEnabled:
86+
processBlock()
87+
else:
88+
# Clear the caches before executing the block to ensure we collect the correct
89+
# witness keys and block hashes when processing the block as these will be used
90+
# when building the witness.
91+
vmState.ledger.clearWitnessKeys()
92+
vmState.ledger.clearBlockHashesCache()
93+
94+
processBlock()
95+
96+
let
97+
preStateLedger = LedgerRef.init(parentBlk.txFrame)
98+
witness = Witness.build(preStateLedger, vmState.ledger, parentBlk.header, header)
99+
100+
?vmState.ledger.txFrame.persistWitness(blkHash, witness)
81101

82102
# We still need to write header to database
83103
# because validateUncles still need it

execution_chain/core/chain/forked_chain/chain_serialize.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ proc replayBlock(fc: ForkedChainRef;
126126
parentFrame = parent.txFrame
127127
txFrame = parentFrame.txFrameBegin
128128

129-
var receipts = fc.processBlock(parent.header, txFrame, blk.blk, blk.hash, false).valueOr:
129+
var receipts = fc.processBlock(parent, txFrame, blk.blk, blk.hash, false).valueOr:
130130
txFrame.dispose()
131131
return err(error)
132132

0 commit comments

Comments
 (0)