better hardForkTransition usage

This commit is contained in:
jangko 2022-12-05 15:46:37 +07:00
parent 4cf2ab661c
commit 53e71e8837
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
7 changed files with 46 additions and 21 deletions

View File

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

View File

@ -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",

View File

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

View File

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

View File

@ -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].} =

View File

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

View File

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