Skip to content

Commit 8ee3285

Browse files
authored
Set finalized flag in ForkedChain importBlock (#3734)
1 parent 59aad34 commit 8ee3285

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

execution_chain/beacon/api_handler/api_newpayload.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ proc newPayload*(ben: BeaconEngineRef,
233233
trace "Importing block without sethead",
234234
hash = blockHash, number = header.number
235235

236-
let vres = await chain.queueImportBlock(blk, finalized = false)
236+
let vres = await chain.queueImportBlock(blk)
237237
if vres.isErr:
238238
warn "Error importing block",
239239
number = header.number,

execution_chain/core/block_import.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ proc importRlpBlocks*(blocksRlp:seq[byte],
5858
number=blk.header.number
5959
printBanner = true
6060

61-
let res = await chain.importBlock(blk, finalized = false)
61+
let res = await chain.importBlock(blk)
6262
if res.isErr:
6363
error "Error occured when importing block",
6464
hash=blk.header.computeBlockHash.short,

execution_chain/core/chain/forked_chain.nim

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ proc init*(
635635

636636
fc
637637

638-
proc importBlock*(c: ForkedChainRef, blk: Block, finalized = false):
638+
proc importBlock*(c: ForkedChainRef, blk: Block):
639639
Future[Result[void, string]] {.async: (raises: [CancelledError]).} =
640640
## Try to import block to canonical or side chain.
641641
## return error if the block is invalid
@@ -656,9 +656,13 @@ proc importBlock*(c: ForkedChainRef, blk: Block, finalized = false):
656656
# to a "staging area" or disk-backed memory but it must not afect `base`.
657657
# `base` is the point of no return, we only update it on finality.
658658

659-
let parent = ?(await c.validateBlock(parent, blk, finalized))
659+
# Setting the finalized flag to true here has the effect of skipping the
660+
# stateroot check for performance reasons.
661+
let
662+
isFinalized = blk.header.number <= c.latestFinalizedBlockNumber
663+
parent = ?(await c.validateBlock(parent, blk, isFinalized))
660664
if c.quarantine.hasOrphans():
661-
c.queueOrphan(parent, finalized)
665+
c.queueOrphan(parent, isFinalized)
662666

663667
else:
664668
# If its parent is an invalid block
@@ -731,9 +735,9 @@ proc stopProcessingQueue*(c: ForkedChainRef) {.async: (raises: []).} =
731735
# at the same time FC.serialize modify the state, crash can happen.
732736
await noCancel c.processingQueueLoop.cancelAndWait()
733737

734-
template queueImportBlock*(c: ForkedChainRef, blk: Block, finalized = false): auto =
738+
template queueImportBlock*(c: ForkedChainRef, blk: Block): auto =
735739
proc asyncHandler(): Future[Result[void, string]] {.async: (raises: [CancelledError], raw: true).} =
736-
c.importBlock(blk, finalized)
740+
c.importBlock(blk)
737741

738742
let item = QueueItem(
739743
responseFut: Future[Result[void, string]].Raising([CancelledError]).init(),

execution_chain/sync/beacon/worker/blocks/blocks_import.nim

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ proc importBlock*(
3737
B=ctx.chain.baseNumber.bnStr, L=ctx.chain.latestNumber.bnStr
3838
else:
3939
try:
40-
# At this point the header chain has already been verifed and so we know
41-
# the block is finalized as long as the block number is less than or equal
42-
# to the latest finalized block. Setting the finalized flag to true here
43-
# has the effect of skipping the stateroot check for performance reasons.
44-
let isFinalized = blk.header.number <= ctx.chain.latestFinalizedBlockNumber
45-
(await ctx.chain.queueImportBlock(blk, isFinalized)).isOkOr:
40+
(await ctx.chain.queueImportBlock(blk)).isOkOr:
4641
return err((ENoException, "", error, Moment.now() - start))
4742
except CancelledError as e:
4843
return err((ECancelledError,$e.name,e.msg,Moment.now()-start))

tests/test_forked_chain.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ suite "ForkedChain mainnet replay":
795795
for i in 1..<fc.baseDistance * 2:
796796
era0.getEthBlock(i.BlockNumber, blk).expect("block in test database")
797797
check:
798-
(waitFor fc.importBlock(blk, finalized = true)) == Result[void, string].ok()
798+
(waitFor fc.importBlock(blk)) == Result[void, string].ok()
799799

800800
check:
801801
(waitFor fc.forkChoice(blk.blockHash, blk.blockHash)) == Result[void, string].ok()
@@ -834,8 +834,8 @@ suite "ForkedChain mainnet replay":
834834
era0.getEthBlock(2.BlockNumber, blk2).expect("block in test database")
835835
era0.getEthBlock(3.BlockNumber, blk3).expect("block in test database")
836836

837-
check (waitFor fc.importBlock(blk1, finalized = false)).isOk()
837+
check (waitFor fc.importBlock(blk1)).isOk()
838838
for i in 1..10:
839-
check (waitFor fc.importBlock(invalidBlk, finalized = false)).isErr()
840-
check (waitFor fc.importBlock(blk2, finalized = false)).isOk()
841-
check (waitFor fc.importBlock(blk3, finalized = false)).isOk()
839+
check (waitFor fc.importBlock(invalidBlk)).isErr()
840+
check (waitFor fc.importBlock(blk2)).isOk()
841+
check (waitFor fc.importBlock(blk3)).isOk()

0 commit comments

Comments
 (0)