From 73e28694b558c28fc983f100b3b1c6620f8b2c8a Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 15 Feb 2022 10:24:38 +0700 Subject: [PATCH] move ttd from vm/state to chain_db --- nimbus/db/db_chain.nim | 6 ++++++ nimbus/vm/state.nim | 17 +++++++---------- nimbus/vm2/state.nim | 18 +++++++----------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index f985bce98..41ed7bbbc 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -58,6 +58,12 @@ proc newBaseChainDB*( proc `$`*(db: BaseChainDB): string = result = "BaseChainDB" +proc ttd*(db: BaseChainDB): DifficultyInt = + if db.config.terminalTotalDifficulty.isSome: + db.config.terminalTotalDifficulty.get() + else: + high(DifficultyInt) + proc networkParams*(db: BaseChainDB): NetworkParams = NetworkParams(config: db.config, genesis: db.genesis) diff --git a/nimbus/vm/state.nim b/nimbus/vm/state.nim index eb20c7e87..66f8f4beb 100644 --- a/nimbus/vm/state.nim +++ b/nimbus/vm/state.nim @@ -44,9 +44,9 @@ template safeExecutor(info: string; code: untyped) = let e = getCurrentException() raise newException(VmStateError, info & "(): " & $e.name & " -- " & e.msg) -proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader): EthAddress +proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader, ttdReached: bool): EthAddress {.gcsafe, raises: [Defect,CatchableError].} = - if not chainDB.config.poaEngine: + if not chainDB.config.poaEngine or ttdReached: return header.coinbase let account = header.ecRecover @@ -195,12 +195,6 @@ proc reinit*(self: BaseVMState; ## Object descriptor return true # else: false -proc ttd(chainDB: BaseChainDB): DifficultyInt = - if chainDB.config.terminalTotalDifficulty.isSome: - chainDB.config.terminalTotalDifficulty.get() - else: - high(DifficultyInt) - proc reinit*(self: BaseVMState; ## Object descriptor parent: BlockHeader; ## parent header, account sync pos. header: BlockHeader; ## header with tx environment data fields @@ -213,13 +207,15 @@ proc reinit*(self: BaseVMState; ## Object descriptor ## It requires the `header` argument properly initalised so that for PoA ## networks, the miner address is retrievable via `ecRecover()`. let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd + let miner = self.chainDB.getMinerAddress(header, ttdReached) + self.reinit( parent = parent, timestamp = header.timestamp, gasLimit = header.gasLimit, fee = header.fee, prevRandao= header.prevRandao, - miner = self.chainDB.getMinerAddress(header), + miner = miner, ttdReached= ttdReached, pruneTrie = pruneTrie) @@ -252,13 +248,14 @@ proc init*( ## It requires the `header` argument properly initalised so that for PoA ## networks, the miner address is retrievable via `ecRecover()`. let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd + let miner = chainDB.getMinerAddress(header, ttdReached) self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie), parent, header.timestamp, header.gasLimit, header.fee, header.prevRandao, - chainDB.getMinerAddress(header), + miner, chainDB, ttdReached, tracerFlags) diff --git a/nimbus/vm2/state.nim b/nimbus/vm2/state.nim index 9263a3214..2fac3309b 100644 --- a/nimbus/vm2/state.nim +++ b/nimbus/vm2/state.nim @@ -24,7 +24,7 @@ import # a temporary solution until nim-eth bumped when not compiles(common.prevRandao): import ../merge/eth_common_overload - + {.push raises: [Defect].} const @@ -43,9 +43,9 @@ template safeExecutor(info: string; code: untyped) = let e = getCurrentException() raise newException(VmStateError, info & "(): " & $e.name & " -- " & e.msg) -proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader): EthAddress +proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader, ttdReached: bool): EthAddress {.gcsafe, raises: [Defect,CatchableError].} = - if not chainDB.config.poaEngine: + if not chainDB.config.poaEngine or ttdReached: return header.coinbase let account = header.ecRecover @@ -194,12 +194,6 @@ proc reinit*(self: BaseVMState; ## Object descriptor return true # else: false -proc ttd(chainDB: BaseChainDB): DifficultyInt = - if chainDB.config.terminalTotalDifficulty.isSome: - chainDB.config.terminalTotalDifficulty.get() - else: - high(DifficultyInt) - proc reinit*(self: BaseVMState; ## Object descriptor parent: BlockHeader; ## parent header, account sync pos. header: BlockHeader; ## header with tx environment data fields @@ -212,13 +206,14 @@ proc reinit*(self: BaseVMState; ## Object descriptor ## It requires the `header` argument properly initalised so that for PoA ## networks, the miner address is retrievable via `ecRecover()`. let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd + let miner = self.chainDB.getMinerAddress(header, ttdReached) self.reinit( parent = parent, timestamp = header.timestamp, gasLimit = header.gasLimit, fee = header.fee, prevRandao= header.prevRandao, - miner = self.chainDB.getMinerAddress(header), + miner = miner, ttdReached= ttdReached, pruneTrie = pruneTrie) @@ -251,13 +246,14 @@ proc init*( ## It requires the `header` argument properly initalised so that for PoA ## networks, the miner address is retrievable via `ecRecover()`. let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd + let miner = chainDB.getMinerAddress(header, ttdReached) self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie), parent, header.timestamp, header.gasLimit, header.fee, header.prevRandao, - chainDB.getMinerAddress(header), + miner, chainDB, ttdReached, tracerFlags)