diff --git a/execution_chain/core/chain/forked_chain.nim b/execution_chain/core/chain/forked_chain.nim index dc9771f882..bac4e395be 100644 --- a/execution_chain/core/chain/forked_chain.nim +++ b/execution_chain/core/chain/forked_chain.nim @@ -315,6 +315,15 @@ proc updateBase(c: ForkedChainRef, base: BlockRef): uint = return let startTime = Moment.now() + block: + # Write block contents to txFrame at the last moment - otherwise, they would + # stay both in BlockRef and TxFrame memory + # TODO probably makes sense to do it the other way around, removing blk + # from BlockRef + var blk = base + while blk.isOk: + c.writeBaggage(blk.blk, blk.hash, blk.txFrame, blk.receipts) + blk = blk.parent # State root sanity check is performed to verify, before writing to disk, # that optimistically checked blocks indeed end up being stored with a @@ -513,7 +522,7 @@ proc validateBlock(c: ForkedChainRef, # Update the snapshot before processing the block so that any vertexes in snapshots # from lower levels than the baseTxFrame are removed from the snapshot before running # the stateroot computation. - c.updateSnapshot(parent.blk, parentFrame) + c.updateSnapshot(parent.blk.header.number, parentFrame) var receipts = c.processBlock(parent, txFrame, blk, blkHash, finalized).valueOr: txFrame.dispose() diff --git a/execution_chain/core/chain/forked_chain/chain_private.nim b/execution_chain/core/chain/forked_chain/chain_private.nim index 4407dff9a4..2037165faf 100644 --- a/execution_chain/core/chain/forked_chain/chain_private.nim +++ b/execution_chain/core/chain/forked_chain/chain_private.nim @@ -35,8 +35,8 @@ proc writeBaggage*(c: ForkedChainRef, header.withdrawalsRoot.expect("WithdrawalsRoot should be verified before"), blk.withdrawals.get) -template updateSnapshot*(c: ForkedChainRef, - blk: Block, +proc updateSnapshot*(c: ForkedChainRef, + number: BlockNumber, txFrame: CoreDbTxRef) = let pos = c.lastSnapshotPos c.lastSnapshotPos = (c.lastSnapshotPos + 1) mod c.lastSnapshots.len @@ -51,7 +51,7 @@ template updateSnapshot*(c: ForkedChainRef, # Checkpoint creates a snapshot of ancestor changes in txFrame - it is an # expensive operation, specially when creating a new branch (ie when blk # is being applied to a block that is currently not a head) - txFrame.checkpoint(blk.header.number) + txFrame.checkpoint(number) c.lastSnapshots[pos] = txFrame diff --git a/execution_chain/core/chain/forked_chain/chain_serialize.nim b/execution_chain/core/chain/forked_chain/chain_serialize.nim index a18e72532a..4e1473b21e 100644 --- a/execution_chain/core/chain/forked_chain/chain_serialize.nim +++ b/execution_chain/core/chain/forked_chain/chain_serialize.nim @@ -129,7 +129,7 @@ proc replayBlock(fc: ForkedChainRef; # Update the snapshot before processing the block so that any vertexes in snapshots # from lower levels than the baseTxFrame are removed from the snapshot before running # the stateroot computation. - fc.updateSnapshot(parent.blk, parentFrame) + fc.updateSnapshot(parent.blk.header.number, parentFrame) # Set finalized to true in order to skip the stateroot check when replaying the # block because the blocks should have already been checked previously during