EIP-4399 implementation of nim-vm2

new addition:
  - `RANDOM` opcode
  - `random` field of BlockHeader(previously `mixDigest`)
  - `PostMerge` temporary name of this new EVM version
This commit is contained in:
jangko 2022-02-01 15:55:51 +07:00 committed by zah
parent 71aa7e4b5c
commit d7f1d698ce
7 changed files with 42 additions and 4 deletions

View File

@ -58,6 +58,9 @@ template getBlockNumber*(c: Computation): Uint256 =
template getDifficulty*(c: Computation): DifficultyInt = template getDifficulty*(c: Computation): DifficultyInt =
c.vmState.difficulty c.vmState.difficulty
template getRandom*(c: Computation): Hash256 =
c.vmState.random
template getGasLimit*(c: Computation): GasInt = template getGasLimit*(c: Computation): GasInt =
c.vmState.gasLimit c.vmState.gasLimit

View File

@ -730,7 +730,8 @@ const
FkPetersburg: SpuriousGasFees, FkPetersburg: SpuriousGasFees,
FkIstanbul: IstanbulGasFees, FkIstanbul: IstanbulGasFees,
FkBerlin: BerlinGasFees, FkBerlin: BerlinGasFees,
FkLondon: LondonGasFees FkLondon: LondonGasFees,
FkPostMerge: LondonGasFees
] ]
@ -742,6 +743,7 @@ gasCosts(FkConstantinople, constantinople, ConstantinopleGasCosts)
gasCosts(FkIstanbul, istanbul, IstanbulGasCosts) gasCosts(FkIstanbul, istanbul, IstanbulGasCosts)
gasCosts(FkBerlin, berlin, BerlinGasCosts) gasCosts(FkBerlin, berlin, BerlinGasCosts)
gasCosts(FkLondon, london, LondonGasCosts) gasCosts(FkLondon, london, LondonGasCosts)
gasCosts(FkPostMerge, postMerge, PostMergeGasCosts)
proc forkToSchedule*(fork: Fork): GasCosts = proc forkToSchedule*(fork: Fork): GasCosts =
if fork < FkHomestead: if fork < FkHomestead:
@ -758,8 +760,10 @@ proc forkToSchedule*(fork: Fork): GasCosts =
IstanbulGasCosts IstanbulGasCosts
elif fork < FkLondon: elif fork < FkLondon:
BerlinGasCosts BerlinGasCosts
else: elif fork < FkPostMerge:
LondonGasCosts LondonGasCosts
else:
PostMergeGasCosts
const const
## Precompile costs ## Precompile costs

View File

@ -209,6 +209,10 @@ type
SelfDestruct = 0xff ## Halt execution and register account for later SelfDestruct = 0xff ## Halt execution and register account for later
## deletion. ## deletion.
const
# EIP-4399 new opcode
Random* = Difficulty
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Verify that Op is contiguous and sym names follow some standards # Verify that Op is contiguous and sym names follow some standards
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -53,6 +53,11 @@ const
k.cpt.stack.push: k.cpt.stack.push:
k.cpt.getDifficulty 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) = gasLimitOp: Vm2OpFn = proc (k: var Vm2Ctx) =
## 0x45, Get the block's gas limit ## 0x45, Get the block's gas limit
k.cpt.stack.push: k.cpt.stack.push:
@ -113,7 +118,7 @@ const
post: vm2OpIgnore)), post: vm2OpIgnore)),
(opCode: Difficulty, ## 0x44, Block difficulty (opCode: Difficulty, ## 0x44, Block difficulty
forks: Vm2OpAllForks, forks: Vm2OpAllForks - Vm2OpPostMergeAndLater,
name: "difficulty", name: "difficulty",
info: "Get the block's difficulty", info: "Get the block's difficulty",
exec: (prep: vm2OpIgnore, exec: (prep: vm2OpIgnore,
@ -150,6 +155,14 @@ const
info: "Get current block's EIP-1559 base fee", info: "Get current block's EIP-1559 base fee",
exec: (prep: vm2OpIgnore, exec: (prep: vm2OpIgnore,
run: baseFeeOp, 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))] post: vm2OpIgnore))]
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -81,6 +81,9 @@ const
Vm2OpLondonAndLater* = Vm2OpLondonAndLater* =
Vm2OpBerlinAndLater - {FkBerlin} Vm2OpBerlinAndLater - {FkBerlin}
Vm2OpPostMergeAndLater* =
Vm2OpLondonAndLater - {FkLondon}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# End # End
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -58,6 +58,7 @@ proc init(
timestamp: EthTime; timestamp: EthTime;
gasLimit: GasInt; gasLimit: GasInt;
fee: Option[Uint256]; fee: Option[Uint256];
random: Hash256;
miner: EthAddress; miner: EthAddress;
chainDB: BaseChainDB; chainDB: BaseChainDB;
tracer: TransactionTracer) tracer: TransactionTracer)
@ -69,6 +70,7 @@ proc init(
self.timestamp = timestamp self.timestamp = timestamp
self.gasLimit = gasLimit self.gasLimit = gasLimit
self.fee = fee self.fee = fee
self.random = random
self.chaindb = chainDB self.chaindb = chainDB
self.tracer = tracer self.tracer = tracer
self.logEntries = @[] self.logEntries = @[]
@ -83,6 +85,7 @@ proc init(
timestamp: EthTime; timestamp: EthTime;
gasLimit: GasInt; gasLimit: GasInt;
fee: Option[Uint256]; fee: Option[Uint256];
random: Hash256;
miner: EthAddress; miner: EthAddress;
chainDB: BaseChainDB; chainDB: BaseChainDB;
tracerFlags: set[TracerFlags]) tracerFlags: set[TracerFlags])
@ -95,6 +98,7 @@ proc init(
timestamp = timestamp, timestamp = timestamp,
gasLimit = gasLimit, gasLimit = gasLimit,
fee = fee, fee = fee,
random = random,
miner = miner, miner = miner,
chainDB = chainDB, chainDB = chainDB,
tracer = tracer) tracer = tracer)
@ -116,6 +120,7 @@ proc new*(
timestamp: EthTime; ## tx env: time stamp timestamp: EthTime; ## tx env: time stamp
gasLimit: GasInt; ## tx env: gas limit gasLimit: GasInt; ## tx env: gas limit
fee: Option[Uint256]; ## tx env: optional base fee fee: Option[Uint256]; ## tx env: optional base fee
random: Hash256; ## tx env: POS block randomness
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
chainDB: BaseChainDB; ## block chain database chainDB: BaseChainDB; ## block chain database
tracerFlags: set[TracerFlags] = {}; tracerFlags: set[TracerFlags] = {};
@ -135,6 +140,7 @@ proc new*(
timestamp = timestamp, timestamp = timestamp,
gasLimit = gasLimit, gasLimit = gasLimit,
fee = fee, fee = fee,
random = random,
miner = miner, miner = miner,
chainDB = chainDB, chainDB = chainDB,
tracerFlags = tracerFlags) tracerFlags = tracerFlags)
@ -144,6 +150,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
timestamp: EthTime; ## tx env: time stamp timestamp: EthTime; ## tx env: time stamp
gasLimit: GasInt; ## tx env: gas limit gasLimit: GasInt; ## tx env: gas limit
fee: Option[Uint256]; ## tx env: optional base fee fee: Option[Uint256]; ## tx env: optional base fee
random: Hash256; ## tx env: POS block randomness
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA) miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
pruneTrie: bool = true): bool pruneTrie: bool = true): bool
{.gcsafe, raises: [Defect,CatchableError].} = {.gcsafe, raises: [Defect,CatchableError].} =
@ -168,6 +175,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
timestamp = timestamp, timestamp = timestamp,
gasLimit = gasLimit, gasLimit = gasLimit,
fee = fee, fee = fee,
random = random,
miner = miner, miner = miner,
chainDB = db, chainDB = db,
tracer = tracer) tracer = tracer)
@ -190,6 +198,7 @@ proc reinit*(self: BaseVMState; ## Object descriptor
timestamp = header.timestamp, timestamp = header.timestamp,
gasLimit = header.gasLimit, gasLimit = header.gasLimit,
fee = header.fee, fee = header.fee,
random = header.random,
miner = self.chainDB.getMinerAddress(header), miner = self.chainDB.getMinerAddress(header),
pruneTrie = pruneTrie) pruneTrie = pruneTrie)
@ -226,6 +235,7 @@ proc init*(
header.timestamp, header.timestamp,
header.gasLimit, header.gasLimit,
header.fee, header.fee,
header.random,
chainDB.getMinerAddress(header), chainDB.getMinerAddress(header),
chainDB, chainDB,
tracerFlags) tracerFlags)

View File

@ -29,6 +29,7 @@ type
timestamp* : EthTime timestamp* : EthTime
gasLimit* : GasInt gasLimit* : GasInt
fee* : Option[Uint256] fee* : Option[Uint256]
random* : Hash256
name* : string name* : string
flags* : set[VMFlag] flags* : set[VMFlag]
tracer* : TransactionTracer tracer* : TransactionTracer