move ttd from vm/state to chain_db

This commit is contained in:
jangko 2022-02-15 10:24:38 +07:00
parent 9a1c8fc779
commit 73e28694b5
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 20 additions and 21 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)