fix EIP-4399 'random' opcode
- fix previous implementation of EIP-4399 - now `random` opcode can be used with evmc_enabled
This commit is contained in:
parent
20068de838
commit
28cdfcaf6b
|
@ -19,6 +19,7 @@ type
|
|||
networkId*: NetworkId
|
||||
config* : ChainConfig
|
||||
genesis* : Genesis
|
||||
totalDifficulty*: DifficultyInt
|
||||
|
||||
# startingBlock, currentBlock, and highestBlock
|
||||
# are progress indicator
|
||||
|
@ -30,6 +31,16 @@ type
|
|||
blockNumber: BlockNumber
|
||||
index: int
|
||||
|
||||
proc getTotalDifficulty*(self: BaseChainDB): UInt256 =
|
||||
# this is actually a combination of `getHash` and `getScore`
|
||||
const key = canonicalHeadHashKey()
|
||||
let data = self.db.get(key.toOpenArray)
|
||||
if data.len == 0:
|
||||
return 0.u256
|
||||
|
||||
let blockHash = rlp.decode(data, Hash256)
|
||||
rlp.decode(self.db.get(blockHashToScoreKey(blockHash).toOpenArray), Uint256)
|
||||
|
||||
proc newBaseChainDB*(
|
||||
db: TrieDatabaseRef,
|
||||
pruneTrie: bool = true,
|
||||
|
@ -42,6 +53,7 @@ proc newBaseChainDB*(
|
|||
result.networkId = id
|
||||
result.config = params.config
|
||||
result.genesis = params.genesis
|
||||
result.totalDifficulty = result.getTotalDifficulty()
|
||||
|
||||
proc `$`*(db: BaseChainDB): string =
|
||||
result = "BaseChainDB"
|
||||
|
@ -335,6 +347,7 @@ proc persistHeaderToDb*(self: BaseChainDB; header: BlockHeader): seq[BlockHeader
|
|||
return self.setAsCanonicalChainHead(headerHash)
|
||||
|
||||
if score > headScore:
|
||||
self.totalDifficulty = score
|
||||
result = self.setAsCanonicalChainHead(headerHash)
|
||||
|
||||
proc persistUncles*(self: BaseChainDB, uncles: openarray[BlockHeader]): Hash256 =
|
||||
|
|
|
@ -18,6 +18,3 @@ type
|
|||
FkIstanbul = "Istanbul"
|
||||
FkBerlin = "Berlin"
|
||||
FkLondon = "London"
|
||||
# TODO: PostMerge is a temporary name
|
||||
# until we have an official name
|
||||
FkPostMerge = "PostMerge"
|
||||
|
|
|
@ -38,9 +38,8 @@ const
|
|||
eth2, # FkPetersburg
|
||||
eth2, # FkIstanbul
|
||||
eth2, # FkBerlin
|
||||
eth2, # FkLondon
|
||||
eth0 # FkPostMerge
|
||||
]
|
||||
eth2 # FkLondon
|
||||
]
|
||||
|
||||
{.push raises: [Defect].}
|
||||
|
||||
|
|
|
@ -73,11 +73,7 @@ proc setupTxContext(host: TransactionHost) =
|
|||
# EIP-4399
|
||||
# Transfer block randomness to difficulty OPCODE
|
||||
let difficulty = vmState.difficulty.toEvmc
|
||||
if difficulty.isZero:
|
||||
# no flipping, because hash is a 32 bytes array
|
||||
host.txContext.block_difficulty = vmState.random.toEvmc
|
||||
else:
|
||||
host.txContext.block_difficulty = flip256(difficulty)
|
||||
host.txContext.block_difficulty = flip256(difficulty)
|
||||
|
||||
host.cachedTxContext = true
|
||||
|
||||
|
|
|
@ -66,6 +66,10 @@ type
|
|||
limits: TxChainGasLimits ## Gas limits for packer and next header
|
||||
txEnv: TxChainPackerEnv ## Assorted parameters, tx packer environment
|
||||
|
||||
# EIP-4399 and EIP-3675
|
||||
ttdReached: bool ## Total Terminal Difficulty reached
|
||||
random: Hash256 ## POS block randomness
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Private functions
|
||||
# ------------------------------------------------------------------------------
|
||||
|
@ -78,13 +82,11 @@ proc resetTxEnv(dh: TxChainRef; parent: BlockHeader; fee: Option[UInt256])
|
|||
parent = parent,
|
||||
timestamp = getTime().utc.toTime,
|
||||
gasLimit = (if dh.maxMode: dh.limits.maxLimit else: dh.limits.trgLimit),
|
||||
fee = fee,
|
||||
# EIP-4399 extra complexity
|
||||
# TODO: make sure from where or what value
|
||||
# this `random` param should be
|
||||
random = Hash256(),
|
||||
fee = fee,
|
||||
random = dh.random,
|
||||
miner = dh.miner,
|
||||
chainDB = dh.db)
|
||||
chainDB = dh.db,
|
||||
ttdReached= dh.ttdReached)
|
||||
|
||||
dh.txEnv.txRoot = BLANK_ROOT_HASH
|
||||
dh.txEnv.stateRoot = dh.txEnv.vmState.parent.stateRoot
|
||||
|
@ -307,6 +309,14 @@ proc `txRoot=`*(dh: TxChainRef; val: Hash256) =
|
|||
## Setter
|
||||
dh.txEnv.txRoot = val
|
||||
|
||||
proc `ttdReached=`*(dh: TxChainRef; val: bool) =
|
||||
## Setter
|
||||
dh.ttdReached = val
|
||||
|
||||
proc `random=`*(dh: TxChainRef; val: Hash256) =
|
||||
## Setter
|
||||
dh.random = val
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# End
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -51,15 +51,6 @@ template getDifficulty*(c: Computation): DifficultyInt =
|
|||
else:
|
||||
c.vmState.difficulty
|
||||
|
||||
template getRandom*(c: Computation): Hash256 =
|
||||
when evmc_enabled:
|
||||
# EIP-4399
|
||||
# no flipping because `block_difficulty` in this context
|
||||
# is a 32 bytes array
|
||||
Hash256.fromEvmc c.host.getTxContext().block_difficulty
|
||||
else:
|
||||
c.vmState.random
|
||||
|
||||
template getGasLimit*(c: Computation): GasInt =
|
||||
when evmc_enabled:
|
||||
c.host.getTxContext().block_gas_limit.GasInt
|
||||
|
|
|
@ -20,10 +20,7 @@ proc hostGetTxContextImpl(ctx: Computation): nimbus_tx_context {.cdecl.} =
|
|||
# EIP-4399
|
||||
# Transfer block randomness to difficulty OPCODE
|
||||
let difficulty = toEvmc(vmstate.difficulty)
|
||||
if difficulty == default(evmc_bytes32): # or difficulty.isZero
|
||||
result.block_difficulty = vmState.random.toEvmc
|
||||
else:
|
||||
result.block_difficulty = difficulty
|
||||
result.block_difficulty = difficulty
|
||||
|
||||
result.chain_id = toEvmc(vmstate.chaindb.config.chainId.uint.u256)
|
||||
result.block_base_fee = toEvmc(vmstate.baseFee)
|
||||
|
|
|
@ -761,8 +761,7 @@ const
|
|||
FkPetersburg: SpuriousGasFees,
|
||||
FkIstanbul: IstanbulGasFees,
|
||||
FkBerlin: BerlinGasFees,
|
||||
FkLondon: LondonGasFees,
|
||||
FkPostMerge: LondonGasFees
|
||||
FkLondon: LondonGasFees
|
||||
]
|
||||
|
||||
|
||||
|
@ -774,7 +773,6 @@ gasCosts(FkConstantinople, constantinople, ConstantinopleGasCosts)
|
|||
gasCosts(FkIstanbul, istanbul, IstanbulGasCosts)
|
||||
gasCosts(FkBerlin, berlin, BerlinGasCosts)
|
||||
gasCosts(FkLondon, london, LondonGasCosts)
|
||||
gasCosts(FkPostMerge, postMerge, PostMergeGasCosts)
|
||||
|
||||
proc forkToSchedule*(fork: Fork): GasCosts =
|
||||
if fork < FkHomestead:
|
||||
|
@ -791,10 +789,8 @@ proc forkToSchedule*(fork: Fork): GasCosts =
|
|||
IstanbulGasCosts
|
||||
elif fork < FkLondon:
|
||||
BerlinGasCosts
|
||||
elif fork < FkPostMerge:
|
||||
LondonGasCosts
|
||||
else:
|
||||
PostMergeGasCosts
|
||||
LondonGasCosts
|
||||
|
||||
const
|
||||
## Precompile costs
|
||||
|
|
|
@ -412,11 +412,6 @@ op difficulty, inline = true:
|
|||
## 0x44, Get the block's difficulty
|
||||
push: c.getDifficulty()
|
||||
|
||||
op randomEIP4399, inline = true:
|
||||
## since EIP-4399 0x44 renamed from `DIFFICULTY` to `RANDOM`
|
||||
## 0x44, Get the block's randomness
|
||||
push: c.getRandom()
|
||||
|
||||
op gasLimit, inline = true:
|
||||
## 0x45, Get the block's gas limit
|
||||
push: c.getGasLimit()
|
||||
|
|
|
@ -254,13 +254,6 @@ proc genLondonJumpTable(ops: array[Op, NimNode]): array[Op, NimNode] {.compileTi
|
|||
|
||||
let LondonOpDispatch {.compileTime.}: array[Op, NimNode] = genLondonJumpTable(BerlinOpDispatch)
|
||||
|
||||
proc genPostMergeJumpTable(ops: array[Op, NimNode]): array[Op, NimNode] {.compileTime.} =
|
||||
result = ops
|
||||
# EIP-4399
|
||||
result[Random] = newIdentNode "randomEIP4399"
|
||||
|
||||
let PostMergeOpDispatch {.compileTime.}: array[Op, NimNode] = genPostMergeJumpTable(LondonOpDispatch)
|
||||
|
||||
proc opTableToCaseStmt(opTable: array[Op, NimNode], c: NimNode): NimNode =
|
||||
|
||||
let instr = quote do: `c`.instr
|
||||
|
@ -352,9 +345,6 @@ macro genBerlinDispatch(c: Computation): untyped =
|
|||
macro genLondonDispatch(c: Computation): untyped =
|
||||
result = opTableToCaseStmt(LondonOpDispatch, c)
|
||||
|
||||
macro genPostMergeDispatch(c: Computation): untyped =
|
||||
result = opTableToCaseStmt(PostMergeOpDispatch, c)
|
||||
|
||||
proc frontierVM(c: Computation) =
|
||||
genFrontierDispatch(c)
|
||||
|
||||
|
@ -385,9 +375,6 @@ proc berlinVM(c: Computation) {.gcsafe.} =
|
|||
proc londonVM(c: Computation) {.gcsafe.} =
|
||||
genLondonDispatch(c)
|
||||
|
||||
proc postMergeVM(c: Computation) {.gcsafe.} =
|
||||
genPostMergeDispatch(c)
|
||||
|
||||
proc selectVM(c: Computation, fork: Fork) {.gcsafe.} =
|
||||
# TODO: Optimise getting fork and updating opCodeExec only when necessary
|
||||
case fork
|
||||
|
@ -409,10 +396,8 @@ proc selectVM(c: Computation, fork: Fork) {.gcsafe.} =
|
|||
c.istanbulVM()
|
||||
of FkBerlin:
|
||||
c.berlinVM()
|
||||
of FkLondon:
|
||||
c.londonVM()
|
||||
else:
|
||||
c.postMergeVM()
|
||||
c.londonVM()
|
||||
|
||||
proc executeOpcodes(c: Computation) =
|
||||
let fork = c.fork
|
||||
|
|
|
@ -62,6 +62,7 @@ proc init(
|
|||
random: Hash256;
|
||||
miner: EthAddress;
|
||||
chainDB: BaseChainDB;
|
||||
ttdReached: bool;
|
||||
tracer: TransactionTracer)
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
## Initialisation helper
|
||||
|
@ -73,6 +74,7 @@ proc init(
|
|||
self.fee = fee
|
||||
self.random = random
|
||||
self.chaindb = chainDB
|
||||
self.ttdReached = ttdReached
|
||||
self.tracer = tracer
|
||||
self.logEntries = @[]
|
||||
self.stateDB = ac
|
||||
|
@ -89,6 +91,7 @@ proc init(
|
|||
random: Hash256;
|
||||
miner: EthAddress;
|
||||
chainDB: BaseChainDB;
|
||||
ttdReached: bool;
|
||||
tracerFlags: set[TracerFlags])
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
var tracer: TransactionTracer
|
||||
|
@ -102,6 +105,7 @@ proc init(
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = chainDB,
|
||||
ttdReached= ttdReached,
|
||||
tracer = tracer)
|
||||
|
||||
# --------------
|
||||
|
@ -121,9 +125,10 @@ proc new*(
|
|||
timestamp: EthTime; ## tx env: time stamp
|
||||
gasLimit: GasInt; ## tx env: gas limit
|
||||
fee: Option[Uint256]; ## tx env: optional base fee
|
||||
random: Hash256; ## tx env: POS block randomness
|
||||
random: Hash256; ## tx env: POS block randomness
|
||||
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
||||
chainDB: BaseChainDB; ## block chain database
|
||||
ttdReached: bool; ## total terminal difficulty reached
|
||||
tracerFlags: set[TracerFlags] = {};
|
||||
pruneTrie: bool = true): T
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
|
@ -144,6 +149,7 @@ proc new*(
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = chainDB,
|
||||
ttdReached = ttdReached,
|
||||
tracerFlags = tracerFlags)
|
||||
|
||||
proc reinit*(self: BaseVMState; ## Object descriptor
|
||||
|
@ -153,6 +159,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
fee: Option[Uint256]; ## tx env: optional base fee
|
||||
random: Hash256; ## tx env: POS block randomness
|
||||
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
||||
ttdReached:bool; ## total terminal difficulty reached
|
||||
pruneTrie: bool = true): bool
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
## Re-initialise state descriptor. The `AccountsCache` database is
|
||||
|
@ -179,10 +186,17 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = db,
|
||||
ttdReached = ttdReached,
|
||||
tracer = tracer)
|
||||
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
|
||||
|
@ -194,6 +208,7 @@ 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
|
||||
self.reinit(
|
||||
parent = parent,
|
||||
timestamp = header.timestamp,
|
||||
|
@ -201,6 +216,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
fee = header.fee,
|
||||
random = header.random,
|
||||
miner = self.chainDB.getMinerAddress(header),
|
||||
ttdReached= ttdReached,
|
||||
pruneTrie = pruneTrie)
|
||||
|
||||
proc reinit*(self: BaseVMState; ## Object descriptor
|
||||
|
@ -231,6 +247,7 @@ 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
|
||||
self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
|
||||
parent,
|
||||
header.timestamp,
|
||||
|
@ -239,6 +256,7 @@ proc init*(
|
|||
header.random,
|
||||
chainDB.getMinerAddress(header),
|
||||
chainDB,
|
||||
ttdReached,
|
||||
tracerFlags)
|
||||
|
||||
proc new*(
|
||||
|
@ -308,9 +326,9 @@ method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} =
|
|||
vmState.parent.blockNumber + 1
|
||||
|
||||
method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
||||
if vmState.fork >= FkPostMerge:
|
||||
if vmState.ttdReached:
|
||||
# EIP-4399/EIP-3675
|
||||
0.u256
|
||||
UInt256.fromBytesBE(vmState.random.data, allowPadding = false)
|
||||
else:
|
||||
vmState.chainDB.config.calcDifficulty(vmState.timestamp, vmState.parent)
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ type
|
|||
gasLimit* : GasInt
|
||||
fee* : Option[Uint256]
|
||||
random* : Hash256
|
||||
ttdReached* : bool
|
||||
name* : string
|
||||
flags* : set[VMFlag]
|
||||
tracer* : TransactionTracer
|
||||
|
|
|
@ -58,9 +58,6 @@ template getBlockNumber*(c: Computation): Uint256 =
|
|||
template getDifficulty*(c: Computation): DifficultyInt =
|
||||
c.vmState.difficulty
|
||||
|
||||
template getRandom*(c: Computation): Hash256 =
|
||||
c.vmState.random
|
||||
|
||||
template getGasLimit*(c: Computation): GasInt =
|
||||
c.vmState.gasLimit
|
||||
|
||||
|
|
|
@ -730,11 +730,9 @@ const
|
|||
FkPetersburg: SpuriousGasFees,
|
||||
FkIstanbul: IstanbulGasFees,
|
||||
FkBerlin: BerlinGasFees,
|
||||
FkLondon: LondonGasFees,
|
||||
FkPostMerge: LondonGasFees
|
||||
FkLondon: LondonGasFees
|
||||
]
|
||||
|
||||
|
||||
gasCosts(FkFrontier, base, BaseGasCosts)
|
||||
gasCosts(FkHomestead, homestead, HomesteadGasCosts)
|
||||
gasCosts(FkTangerine, tangerine, TangerineGasCosts)
|
||||
|
@ -743,7 +741,6 @@ gasCosts(FkConstantinople, constantinople, ConstantinopleGasCosts)
|
|||
gasCosts(FkIstanbul, istanbul, IstanbulGasCosts)
|
||||
gasCosts(FkBerlin, berlin, BerlinGasCosts)
|
||||
gasCosts(FkLondon, london, LondonGasCosts)
|
||||
gasCosts(FkPostMerge, postMerge, PostMergeGasCosts)
|
||||
|
||||
proc forkToSchedule*(fork: Fork): GasCosts =
|
||||
if fork < FkHomestead:
|
||||
|
@ -760,10 +757,8 @@ proc forkToSchedule*(fork: Fork): GasCosts =
|
|||
IstanbulGasCosts
|
||||
elif fork < FkLondon:
|
||||
BerlinGasCosts
|
||||
elif fork < FkPostMerge:
|
||||
LondonGasCosts
|
||||
else:
|
||||
PostMergeGasCosts
|
||||
LondonGasCosts
|
||||
|
||||
const
|
||||
## Precompile costs
|
||||
|
|
|
@ -53,11 +53,6 @@ const
|
|||
k.cpt.stack.push:
|
||||
k.cpt.getDifficulty
|
||||
|
||||
randomOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
||||
## 0x44, Get the block's randomness
|
||||
k.cpt.stack.push:
|
||||
k.cpt.getRandom
|
||||
|
||||
gasLimitOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
||||
## 0x45, Get the block's gas limit
|
||||
k.cpt.stack.push:
|
||||
|
@ -118,7 +113,7 @@ const
|
|||
post: vm2OpIgnore)),
|
||||
|
||||
(opCode: Difficulty, ## 0x44, Block difficulty
|
||||
forks: Vm2OpAllForks - Vm2OpPostMergeAndLater,
|
||||
forks: Vm2OpAllForks,
|
||||
name: "difficulty",
|
||||
info: "Get the block's difficulty",
|
||||
exec: (prep: vm2OpIgnore,
|
||||
|
@ -155,14 +150,6 @@ const
|
|||
info: "Get current block's EIP-1559 base fee",
|
||||
exec: (prep: vm2OpIgnore,
|
||||
run: baseFeeOp,
|
||||
post: vm2OpIgnore)),
|
||||
|
||||
(opCode: Random, ## 0x44, EIP-4399 Block randomness.
|
||||
forks: Vm2OpPostMergeAndLater,
|
||||
name: "random",
|
||||
info: "Get current block's EIP-4399 randomness",
|
||||
exec: (prep: vm2OpIgnore,
|
||||
run: randomOp,
|
||||
post: vm2OpIgnore))]
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -81,9 +81,6 @@ const
|
|||
Vm2OpLondonAndLater* =
|
||||
Vm2OpBerlinAndLater - {FkBerlin}
|
||||
|
||||
Vm2OpPostMergeAndLater* =
|
||||
Vm2OpLondonAndLater - {FkLondon}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# End
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -61,6 +61,7 @@ proc init(
|
|||
random: Hash256;
|
||||
miner: EthAddress;
|
||||
chainDB: BaseChainDB;
|
||||
ttdReached: bool;
|
||||
tracer: TransactionTracer)
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
## Initialisation helper
|
||||
|
@ -72,6 +73,7 @@ proc init(
|
|||
self.fee = fee
|
||||
self.random = random
|
||||
self.chaindb = chainDB
|
||||
self.ttdReached = ttdReached
|
||||
self.tracer = tracer
|
||||
self.logEntries = @[]
|
||||
self.stateDB = ac
|
||||
|
@ -88,6 +90,7 @@ proc init(
|
|||
random: Hash256;
|
||||
miner: EthAddress;
|
||||
chainDB: BaseChainDB;
|
||||
ttdReached: bool;
|
||||
tracerFlags: set[TracerFlags])
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
var tracer: TransactionTracer
|
||||
|
@ -101,6 +104,7 @@ proc init(
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = chainDB,
|
||||
ttdReached= ttdReached,
|
||||
tracer = tracer)
|
||||
|
||||
# --------------
|
||||
|
@ -123,6 +127,7 @@ proc new*(
|
|||
random: Hash256; ## tx env: POS block randomness
|
||||
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
||||
chainDB: BaseChainDB; ## block chain database
|
||||
ttdReached: bool; ## total terminal difficulty reached
|
||||
tracerFlags: set[TracerFlags] = {};
|
||||
pruneTrie: bool = true): T
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
|
@ -143,6 +148,7 @@ proc new*(
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = chainDB,
|
||||
ttdReached = ttdReached,
|
||||
tracerFlags = tracerFlags)
|
||||
|
||||
proc reinit*(self: BaseVMState; ## Object descriptor
|
||||
|
@ -152,6 +158,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
fee: Option[Uint256]; ## tx env: optional base fee
|
||||
random: Hash256; ## tx env: POS block randomness
|
||||
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
||||
ttdReached:bool; ## total terminal difficulty reached
|
||||
pruneTrie: bool = true): bool
|
||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
||||
## Re-initialise state descriptor. The `AccountsCache` database is
|
||||
|
@ -178,10 +185,17 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
random = random,
|
||||
miner = miner,
|
||||
chainDB = db,
|
||||
ttdReached = ttdReached,
|
||||
tracer = tracer)
|
||||
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
|
||||
|
@ -193,6 +207,7 @@ 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
|
||||
self.reinit(
|
||||
parent = parent,
|
||||
timestamp = header.timestamp,
|
||||
|
@ -200,6 +215,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
|
|||
fee = header.fee,
|
||||
random = header.random,
|
||||
miner = self.chainDB.getMinerAddress(header),
|
||||
ttdReached= ttdReached,
|
||||
pruneTrie = pruneTrie)
|
||||
|
||||
proc reinit*(self: BaseVMState; ## Object descriptor
|
||||
|
@ -230,6 +246,7 @@ 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
|
||||
self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
|
||||
parent,
|
||||
header.timestamp,
|
||||
|
@ -238,6 +255,7 @@ proc init*(
|
|||
header.random,
|
||||
chainDB.getMinerAddress(header),
|
||||
chainDB,
|
||||
ttdReached,
|
||||
tracerFlags)
|
||||
|
||||
proc new*(
|
||||
|
@ -295,9 +313,9 @@ method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} =
|
|||
vmState.parent.blockNumber + 1
|
||||
|
||||
method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
||||
if vmState.fork >= FkPostMerge:
|
||||
if vmState.ttdReached:
|
||||
# EIP-4399/EIP-3675
|
||||
0.u256
|
||||
UInt256.fromBytesBE(vmState.random.data, allowPadding = false)
|
||||
else:
|
||||
vmState.chainDB.config.calcDifficulty(vmState.timestamp, vmState.parent)
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ type
|
|||
gasLimit* : GasInt
|
||||
fee* : Option[Uint256]
|
||||
random* : Hash256
|
||||
ttdReached* : bool
|
||||
name* : string
|
||||
flags* : set[VMFlag]
|
||||
tracer* : TransactionTracer
|
||||
|
|
|
@ -324,27 +324,27 @@ proc opEnvMain*() =
|
|||
fork: berlin
|
||||
gasused: 2603
|
||||
|
||||
when not defined(evmc_enabled):
|
||||
# TODO: enable this test for EVMC when EVMC
|
||||
# support this EVM version
|
||||
assembler:
|
||||
title: "EIP-4399 RANDOM 0"
|
||||
code:
|
||||
Random
|
||||
STOP
|
||||
stack:
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000000"
|
||||
fork: postmerge
|
||||
vmState.ttdReached = true
|
||||
assembler:
|
||||
title: "EIP-4399 RANDOM 0"
|
||||
code:
|
||||
Random
|
||||
STOP
|
||||
stack:
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000000"
|
||||
fork: london
|
||||
|
||||
vmState.random = EMPTY_UNCLE_HASH
|
||||
assembler:
|
||||
title: "EIP-4399 RANDOM: EMPTY_UNCLE_HASH"
|
||||
code:
|
||||
Random
|
||||
STOP
|
||||
stack:
|
||||
"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
|
||||
fork: postmerge
|
||||
vmState.random = EMPTY_UNCLE_HASH
|
||||
assembler:
|
||||
title: "EIP-4399 RANDOM: EMPTY_UNCLE_HASH"
|
||||
code:
|
||||
Random
|
||||
STOP
|
||||
stack:
|
||||
"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
|
||||
fork: london
|
||||
|
||||
vmState.ttdReached = false
|
||||
|
||||
when isMainModule:
|
||||
opEnvMain()
|
||||
|
|
Loading…
Reference in New Issue