ForkedChain become ForkedChainRef (#2417)

* ForkedChain become ForkedChainRef

It will be shared between engine API, RPC, and txPool

* Fix ForkedChainRef constructor
This commit is contained in:
andri lim 2024-06-27 12:54:52 +07:00 committed by GitHub
parent 27339e9520
commit b80521a84d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 22 deletions

View File

@ -36,7 +36,7 @@ type
cursorHash: Hash256 cursorHash: Hash256
header: BlockHeader header: BlockHeader
ForkedChain* = object ForkedChainRef* = ref object
stagingTx: CoreDbTxRef stagingTx: CoreDbTxRef
db: CoreDbRef db: CoreDbRef
com: CommonRef com: CommonRef
@ -59,7 +59,7 @@ template shouldNotKeyError(body: untyped) =
except KeyError as exc: except KeyError as exc:
raiseAssert exc.msg raiseAssert exc.msg
proc processBlock(c: ForkedChain, proc processBlock(c: ForkedChainRef,
parent: BlockHeader, parent: BlockHeader,
blk: EthBlock): Result[seq[Receipt], string] = blk: EthBlock): Result[seq[Receipt], string] =
template header(): BlockHeader = template header(): BlockHeader =
@ -89,7 +89,7 @@ proc processBlock(c: ForkedChain,
ok(move(vmState.receipts)) ok(move(vmState.receipts))
func updateCursorHeads(c: var ForkedChain, func updateCursorHeads(c: ForkedChainRef,
cursorHash: Hash256, cursorHash: Hash256,
header: BlockHeader) = header: BlockHeader) =
# Example of cursorHeads and cursor # Example of cursorHeads and cursor
@ -115,7 +115,7 @@ func updateCursorHeads(c: var ForkedChain,
forkJunction: header.number, forkJunction: header.number,
) )
func updateCursor(c: var ForkedChain, func updateCursor(c: ForkedChainRef,
blk: EthBlock, blk: EthBlock,
receipts: sink seq[Receipt]) = receipts: sink seq[Receipt]) =
template header(): BlockHeader = template header(): BlockHeader =
@ -129,7 +129,7 @@ func updateCursor(c: var ForkedChain,
) )
c.updateCursorHeads(c.cursorHash, header) c.updateCursorHeads(c.cursorHash, header)
proc validateBlock(c: var ForkedChain, proc validateBlock(c: ForkedChainRef,
parent: BlockHeader, parent: BlockHeader,
blk: EthBlock, blk: EthBlock,
updateCursor: bool = true): Result[void, string] = updateCursor: bool = true): Result[void, string] =
@ -148,7 +148,7 @@ proc validateBlock(c: var ForkedChain,
ok() ok()
proc replaySegment(c: var ForkedChain, target: Hash256) = proc replaySegment(c: ForkedChainRef, target: Hash256) =
# Replay from base+1 to target block # Replay from base+1 to target block
var var
prevHash = target prevHash = target
@ -167,7 +167,7 @@ proc replaySegment(c: var ForkedChain, target: Hash256) =
updateCursor = false).expect("have been validated before") updateCursor = false).expect("have been validated before")
c.cursorHeader = chain[i].header c.cursorHeader = chain[i].header
proc writeBaggage(c: var ForkedChain, target: Hash256) = proc writeBaggage(c: ForkedChainRef, target: Hash256) =
# Write baggage from base+1 to target block # Write baggage from base+1 to target block
shouldNotKeyError: shouldNotKeyError:
var prevHash = target var prevHash = target
@ -180,7 +180,7 @@ proc writeBaggage(c: var ForkedChain, target: Hash256) =
c.db.persistWithdrawals(blk.blk.withdrawals.get) c.db.persistWithdrawals(blk.blk.withdrawals.get)
prevHash = blk.blk.header.parentHash prevHash = blk.blk.header.parentHash
func updateBase(c: var ForkedChain, func updateBase(c: ForkedChainRef,
newBaseHash: Hash256, newBaseHash: Hash256,
newBaseHeader: BlockHeader, newBaseHeader: BlockHeader,
canonicalCursorHash: Hash256) = canonicalCursorHash: Hash256) =
@ -229,7 +229,7 @@ func updateBase(c: var ForkedChain,
c.baseHeader = newBaseHeader c.baseHeader = newBaseHeader
c.baseHash = newBaseHash c.baseHash = newBaseHash
func findCanonicalHead(c: ForkedChain, func findCanonicalHead(c: ForkedChainRef,
hash: Hash256): Result[CanonicalDesc, string] = hash: Hash256): Result[CanonicalDesc, string] =
if hash == c.baseHash: if hash == c.baseHash:
# The cursorHash here should not be used for next step # The cursorHash here should not be used for next step
@ -248,7 +248,7 @@ func findCanonicalHead(c: ForkedChain,
err("Block hash is not part of any active chain") err("Block hash is not part of any active chain")
func canonicalChain(c: ForkedChain, func canonicalChain(c: ForkedChainRef,
hash: Hash256, hash: Hash256,
headHash: Hash256): Result[BlockHeader, string] = headHash: Hash256): Result[BlockHeader, string] =
if hash == c.baseHash: if hash == c.baseHash:
@ -264,7 +264,7 @@ func canonicalChain(c: ForkedChain,
err("Block hash not in canonical chain") err("Block hash not in canonical chain")
func calculateNewBase(c: ForkedChain, func calculateNewBase(c: ForkedChainRef,
finalizedHeader: BlockHeader, finalizedHeader: BlockHeader,
headHash: Hash256, headHash: Hash256,
headHeader: BlockHeader): BaseDesc = headHeader: BlockHeader): BaseDesc =
@ -288,7 +288,7 @@ func calculateNewBase(c: ForkedChain,
doAssert(false, "Unreachable code") doAssert(false, "Unreachable code")
func trimCanonicalChain(c: var ForkedChain, head: CanonicalDesc) = func trimCanonicalChain(c: ForkedChainRef, head: CanonicalDesc) =
# Maybe the current active chain is longer than canonical chain # Maybe the current active chain is longer than canonical chain
shouldNotKeyError: shouldNotKeyError:
var prevHash = head.cursorHash var prevHash = head.cursorHash
@ -304,7 +304,8 @@ func trimCanonicalChain(c: var ForkedChain, head: CanonicalDesc) =
# Public functions # Public functions
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc initForkedChain*(com: CommonRef, baseHeader: BlockHeader): ForkedChain = proc newForkedChain*(com: CommonRef, baseHeader: BlockHeader): ForkedChainRef =
new(result)
result.com = com result.com = com
result.db = com.db result.db = com.db
result.baseHeader = baseHeader result.baseHeader = baseHeader
@ -312,7 +313,7 @@ proc initForkedChain*(com: CommonRef, baseHeader: BlockHeader): ForkedChain =
result.baseHash = result.cursorHash result.baseHash = result.cursorHash
result.cursorHeader = result.baseHeader result.cursorHeader = result.baseHeader
proc importBlock*(c: var ForkedChain, blk: EthBlock): Result[void, string] = proc importBlock*(c: ForkedChainRef, blk: EthBlock): Result[void, string] =
# Try to import block to canonical or side chain. # Try to import block to canonical or side chain.
# return error if the block is invalid # return error if the block is invalid
if c.stagingTx.isNil: if c.stagingTx.isNil:
@ -343,7 +344,7 @@ proc importBlock*(c: var ForkedChain, blk: EthBlock): Result[void, string] =
c.replaySegment(header.parentHash) c.replaySegment(header.parentHash)
c.validateBlock(c.cursorHeader, blk) c.validateBlock(c.cursorHeader, blk)
proc forkChoice*(c: var ForkedChain, proc forkChoice*(c: ForkedChainRef,
headHash: Hash256, headHash: Hash256,
finalizedHash: Hash256): Result[void, string] = finalizedHash: Hash256): Result[void, string] =

View File

@ -32,8 +32,6 @@ cliBuilder:
./test_state_db, ./test_state_db,
./test_difficulty, ./test_difficulty,
./test_transaction_json, ./test_transaction_json,
# TODO: some of test_blockchain_json's test cases failing
# see issue #2260
./test_blockchain_json, ./test_blockchain_json,
./test_forkid, ./test_forkid,
./test_multi_keys, ./test_multi_keys,

View File

@ -84,10 +84,10 @@ proc executeCase(node: JsonNode): bool =
return false return false
if com.db.getCanonicalHead().blockHash != env.genesisHeader.blockHash: if com.db.getCanonicalHead().blockHash != env.genesisHeader.blockHash:
debugEcho "Genesis block hash is database different with expected genesis block hash" debugEcho "Genesis block hash in database is different with expected genesis block hash"
return false return false
var c = initForkedChain(com, env.genesisHeader) var c = newForkedChain(com, env.genesisHeader)
var lastStateRoot = env.genesisHeader.stateRoot var lastStateRoot = env.genesisHeader.stateRoot
for blk in env.blocks: for blk in env.blocks:
let res = c.importBlock(blk.blk) let res = c.importBlock(blk.blk)
@ -130,9 +130,8 @@ proc blockchainJsonMain*() =
legacyFolder = "eth_tests/LegacyTests/Constantinople/BlockchainTests" legacyFolder = "eth_tests/LegacyTests/Constantinople/BlockchainTests"
newFolder = "eth_tests/BlockchainTests" newFolder = "eth_tests/BlockchainTests"
let res = loadKzgTrustedSetup() loadKzgTrustedSetup().isOkOr:
if res.isErr: echo "FATAL: ", error
echo "FATAL: ", res.error
quit(QuitFailure) quit(QuitFailure)
if false: if false:
@ -145,6 +144,10 @@ proc blockchainJsonMain*() =
when isMainModule: when isMainModule:
when debugMode: when debugMode:
proc executeFile(name: string) = proc executeFile(name: string) =
loadKzgTrustedSetup().isOkOr:
echo "FATAL: ", error
quit(QuitFailure)
var testStatusIMPL: TestStatus var testStatusIMPL: TestStatus
let node = json.parseFile(name) let node = json.parseFile(name)
executeFile(node, testStatusIMPL) executeFile(node, testStatusIMPL)