From 53e71e8837364a2199d305b2508dceb441d37fa3 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 5 Dec 2022 15:46:37 +0700 Subject: [PATCH] better hardForkTransition usage --- nimbus/common/common.nim | 44 ++++++++++++++++++++++++---- nimbus/core/chain/persist_blocks.nim | 6 ++-- nimbus/core/pow/difficulty.nim | 2 +- nimbus/core/tx_pool.nim | 8 ----- nimbus/core/tx_pool/tx_chain.nim | 1 + tests/test_blockchain_json.nim | 2 +- tests/test_custom_network.nim | 4 +-- 7 files changed, 46 insertions(+), 21 deletions(-) diff --git a/nimbus/common/common.nim b/nimbus/common/common.nim index 77187fbe0..a50567e1e 100644 --- a/nimbus/common/common.nim +++ b/nimbus/common/common.nim @@ -185,7 +185,7 @@ proc clone*(com: CommonRef): CommonRef = # Public functions # ------------------------------------------------------------------------------ -func toFork*(com: CommonRef, number: BlockNumber): HardFork = +func toHardFork*(com: CommonRef, number: BlockNumber): HardFork = ## doesn't do transition ## only want to know a particular block number ## belongs to which fork without considering TD or TTD @@ -205,7 +205,7 @@ proc hardForkTransition(com: CommonRef, if td.isNone: # fork transition ignoring TD - let fork = com.toFork(number) + let fork = com.toHardFork(number) com.currentFork = fork com.consensusTransition(fork) return @@ -222,17 +222,51 @@ proc hardForkTransition(com: CommonRef, # should always have a match doAssert(false, "unreachable code") +proc hardForkTransition*(com: CommonRef, parentHash: Hash256, + number: BlockNumber) + {.gcsafe, raises: [Defect, CatchableError].} = + + if com.config.mergeForkBlock.isSome or + com.config.terminalTotalDifficulty.isSome: + let fork = com.toHardFork(number) + com.currentFork = fork + com.consensusTransition(fork) + return + + var td: DifficultyInt + if not com.db.getTd(parentHash, td): + # TODO: Is this really ok? + let fork = com.toHardFork(number) + com.currentFork = fork + com.consensusTransition(fork) + return + + for fork in countdown(HardFork.high, HardFork.low): + let x = com.blockToFork[fork] + if x.toFork(x.data, number, td): + com.currentFork = fork + com.consensusTransition(fork) + return + + # should always have a match + doAssert(false, "unreachable code") + +proc hardForkTransition*(com: CommonRef, header: BlockHeader) + {.gcsafe, raises: [Defect, CatchableError].} = + + com.hardForkTransition(header.parentHash, header.blockNumber) + func toEVMFork*(com: CommonRef, number: BlockNumber): EVMFork = ## similar to toFork, but produce EVMFork ## be aware that if MergeFork is not set in ## chain config, this function probably give wrong ## result because no TD is put into consideration - let fork = com.toFork(number) + let fork = com.toHardFork(number) ToEVMFork[fork] func isLondon*(com: CommonRef, number: BlockNumber): bool = # TODO: Fixme, use only London comparator - com.toFork(number) >= London + com.toHardFork(number) >= London func forkGTE*(com: CommonRef, fork: HardFork): bool = com.currentFork >= fork @@ -254,7 +288,7 @@ proc minerAddress*(com: CommonRef; header: BlockHeader): EthAddress func forkId*(com: CommonRef, number: BlockNumber): ForkID {.gcsafe.} = ## EIP 2364/2124 - let fork = com.toFork(number) + let fork = com.toHardFork(number) com.forkIds[fork] func isEIP155*(com: CommonRef, number: BlockNumber): bool = diff --git a/nimbus/core/chain/persist_blocks.nim b/nimbus/core/chain/persist_blocks.nim index 5b02b1634..0cc84c653 100644 --- a/nimbus/core/chain/persist_blocks.nim +++ b/nimbus/core/chain/persist_blocks.nim @@ -51,8 +51,7 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader]; var cliqueState = c.clique.cliqueSave defer: c.clique.cliqueRestore(cliqueState) - let td = some(c.db.getScore(headers[0].parentHash)) - c.com.hardForkTransition(headers[0].blockNumber, td) + c.com.hardForkTransition(headers[0]) # Note that `0 < headers.len`, assured when called from `persistBlocks()` let vmState = BaseVMState() @@ -70,8 +69,7 @@ proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader]; let (header, body) = (headers[i], bodies[i]) - let td = some(c.db.getScore(header.parentHash)) - c.com.hardForkTransition(header.blockNumber, td) + c.com.hardForkTransition(header) if not vmState.reinit(header): debug "Cannot update VmState", diff --git a/nimbus/core/pow/difficulty.nim b/nimbus/core/pow/difficulty.nim index c0a8b46fe..5a2917fc6 100644 --- a/nimbus/core/pow/difficulty.nim +++ b/nimbus/core/pow/difficulty.nim @@ -172,7 +172,7 @@ template calcDifficultyGrayGlacier*(timeStamp: EthTime, parent: BlockHeader): Di makeDifficultyCalculator(11_400_000, timeStamp, parent) func calcDifficulty*(com: CommonRef, timeStamp: EthTime, parent: BlockHeader): DifficultyInt = - let next = com.toFork(parent.blockNumber + bigOne) + let next = com.toHardFork(parent.blockNumber + bigOne) if next >= GrayGlacier: result = calcDifficultyGrayGlacier(timeStamp, parent) elif next >= ArrowGlacier: diff --git a/nimbus/core/tx_pool.nim b/nimbus/core/tx_pool.nim index 17199976a..634df7cc1 100644 --- a/nimbus/core/tx_pool.nim +++ b/nimbus/core/tx_pool.nim @@ -616,14 +616,6 @@ proc ethBlock*(xp: TxPoolRef): EthBlock ## Note that this getter runs *ad hoc* all the txs through the VM in ## order to build the block. - # do hardfork transition - # this transition will leak into outside txpool - # but it's ok, other subsystem will likely gladly accept the - # the transition, until proven otherwise - let parentHash = xp.chain.vmState.parent.blockHash - let td = some(xp.chain.com.db.getScore(parentHash)) - xp.chain.com.hardForkTransition(xp.chain.vmState.parent.blockNumber+1, td) - xp.packerVmExec # updates vmState result.header = xp.chain.getHeader # uses updated vmState for (_,nonceList) in xp.txDB.packingOrderAccounts(txItemPacked): diff --git a/nimbus/core/tx_pool/tx_chain.nim b/nimbus/core/tx_pool/tx_chain.nim index 39285a9f0..9d1a23940 100644 --- a/nimbus/core/tx_pool/tx_chain.nim +++ b/nimbus/core/tx_pool/tx_chain.nim @@ -95,6 +95,7 @@ proc resetTxEnv(dh: TxChainRef; parent: BlockHeader; fee: Option[UInt256]) dh.txEnv.txRoot = EMPTY_ROOT_HASH dh.txEnv.stateRoot = dh.txEnv.vmState.parent.stateRoot + dh.com.hardForkTransition(parent.blockHash, parent.blockNumber+1) proc update(dh: TxChainRef; parent: BlockHeader) {.gcsafe,raises: [Defect,CatchableError].} = diff --git a/tests/test_blockchain_json.nim b/tests/test_blockchain_json.nim index bfccd535a..87b99876a 100644 --- a/tests/test_blockchain_json.nim +++ b/tests/test_blockchain_json.nim @@ -257,7 +257,7 @@ proc runTester(tester: var Tester, com: CommonRef, testStatusIMPL: var TestStatu when defined(noisy): if res.isErr: debugEcho "blockNumber : ", preminedBlock.header.blockNumber - debugEcho "fork : ", com.toFork(preminedBlock.header.blockNumber) + debugEcho "fork : ", com.toHardFork(preminedBlock.header.blockNumber) debugEcho "error message: ", res.error debugEcho "consensusType: ", com.consensus diff --git a/tests/test_custom_network.nim b/tests/test_custom_network.nim index d786fb792..2af21a0e2 100644 --- a/tests/test_custom_network.nim +++ b/tests/test_custom_network.nim @@ -237,7 +237,7 @@ proc genesisLoadRunner(noisy = true; params = params) check mcom.ttd.get == sSpcs.termTotalDff - check mcom.toFork(sSpcs.mergeFork.toBlockNumber) == MergeFork + check mcom.toHardFork(sSpcs.mergeFork.toBlockNumber) == MergeFork test &"Construct persistent ChainDBRef on {tmpDir}, {persistPruneInfo}": if disablePersistentDB: @@ -257,7 +257,7 @@ proc genesisLoadRunner(noisy = true; params = params) check dcom.ttd.get == sSpcs.termTotalDff - check dcom.toFork(sSpcs.mergeFork.toBlockNumber) == MergeFork + check dcom.toHardFork(sSpcs.mergeFork.toBlockNumber) == MergeFork test "Initialise in-memory Genesis": mcom.initializeEmptyDb