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 = proc `$`*(db: BaseChainDB): string =
result = "BaseChainDB" result = "BaseChainDB"
proc ttd*(db: BaseChainDB): DifficultyInt =
if db.config.terminalTotalDifficulty.isSome:
db.config.terminalTotalDifficulty.get()
else:
high(DifficultyInt)
proc networkParams*(db: BaseChainDB): NetworkParams = proc networkParams*(db: BaseChainDB): NetworkParams =
NetworkParams(config: db.config, genesis: db.genesis) NetworkParams(config: db.config, genesis: db.genesis)

View File

@ -44,9 +44,9 @@ template safeExecutor(info: string; code: untyped) =
let e = getCurrentException() let e = getCurrentException()
raise newException(VmStateError, info & "(): " & $e.name & " -- " & e.msg) 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].} = {.gcsafe, raises: [Defect,CatchableError].} =
if not chainDB.config.poaEngine: if not chainDB.config.poaEngine or ttdReached:
return header.coinbase return header.coinbase
let account = header.ecRecover let account = header.ecRecover
@ -195,12 +195,6 @@ proc reinit*(self: BaseVMState; ## Object descriptor
return true return true
# else: false # 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 proc reinit*(self: BaseVMState; ## Object descriptor
parent: BlockHeader; ## parent header, account sync pos. parent: BlockHeader; ## parent header, account sync pos.
header: BlockHeader; ## header with tx environment data fields 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 ## It requires the `header` argument properly initalised so that for PoA
## networks, the miner address is retrievable via `ecRecover()`. ## networks, the miner address is retrievable via `ecRecover()`.
let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd
let miner = self.chainDB.getMinerAddress(header, ttdReached)
self.reinit( self.reinit(
parent = parent, parent = parent,
timestamp = header.timestamp, timestamp = header.timestamp,
gasLimit = header.gasLimit, gasLimit = header.gasLimit,
fee = header.fee, fee = header.fee,
prevRandao= header.prevRandao, prevRandao= header.prevRandao,
miner = self.chainDB.getMinerAddress(header), miner = miner,
ttdReached= ttdReached, ttdReached= ttdReached,
pruneTrie = pruneTrie) pruneTrie = pruneTrie)
@ -252,13 +248,14 @@ proc init*(
## It requires the `header` argument properly initalised so that for PoA ## It requires the `header` argument properly initalised so that for PoA
## networks, the miner address is retrievable via `ecRecover()`. ## networks, the miner address is retrievable via `ecRecover()`.
let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd
let miner = chainDB.getMinerAddress(header, ttdReached)
self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie), self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
parent, parent,
header.timestamp, header.timestamp,
header.gasLimit, header.gasLimit,
header.fee, header.fee,
header.prevRandao, header.prevRandao,
chainDB.getMinerAddress(header), miner,
chainDB, chainDB,
ttdReached, ttdReached,
tracerFlags) tracerFlags)

View File

@ -43,9 +43,9 @@ template safeExecutor(info: string; code: untyped) =
let e = getCurrentException() let e = getCurrentException()
raise newException(VmStateError, info & "(): " & $e.name & " -- " & e.msg) 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].} = {.gcsafe, raises: [Defect,CatchableError].} =
if not chainDB.config.poaEngine: if not chainDB.config.poaEngine or ttdReached:
return header.coinbase return header.coinbase
let account = header.ecRecover let account = header.ecRecover
@ -194,12 +194,6 @@ proc reinit*(self: BaseVMState; ## Object descriptor
return true return true
# else: false # 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 proc reinit*(self: BaseVMState; ## Object descriptor
parent: BlockHeader; ## parent header, account sync pos. parent: BlockHeader; ## parent header, account sync pos.
header: BlockHeader; ## header with tx environment data fields 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 ## It requires the `header` argument properly initalised so that for PoA
## networks, the miner address is retrievable via `ecRecover()`. ## networks, the miner address is retrievable via `ecRecover()`.
let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd let ttdReached = self.chainDB.totalDifficulty + header.difficulty > self.chainDB.ttd
let miner = self.chainDB.getMinerAddress(header, ttdReached)
self.reinit( self.reinit(
parent = parent, parent = parent,
timestamp = header.timestamp, timestamp = header.timestamp,
gasLimit = header.gasLimit, gasLimit = header.gasLimit,
fee = header.fee, fee = header.fee,
prevRandao= header.prevRandao, prevRandao= header.prevRandao,
miner = self.chainDB.getMinerAddress(header), miner = miner,
ttdReached= ttdReached, ttdReached= ttdReached,
pruneTrie = pruneTrie) pruneTrie = pruneTrie)
@ -251,13 +246,14 @@ proc init*(
## It requires the `header` argument properly initalised so that for PoA ## It requires the `header` argument properly initalised so that for PoA
## networks, the miner address is retrievable via `ecRecover()`. ## networks, the miner address is retrievable via `ecRecover()`.
let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd let ttdReached = chainDB.totalDifficulty + header.difficulty > chainDB.ttd
let miner = chainDB.getMinerAddress(header, ttdReached)
self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie), self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
parent, parent,
header.timestamp, header.timestamp,
header.gasLimit, header.gasLimit,
header.fee, header.fee,
header.prevRandao, header.prevRandao,
chainDB.getMinerAddress(header), miner,
chainDB, chainDB,
ttdReached, ttdReached,
tracerFlags) tracerFlags)