From b51fad5fa74c6b1ed8fecac5493f636b6f45700b Mon Sep 17 00:00:00 2001 From: jangko Date: Sun, 27 Jun 2021 20:19:22 +0700 Subject: [PATCH] EIP-3198: add baseFee op code in nim-evm2 --- nimbus/vm2/computation.nim | 3 +++ nimbus/vm2/interpreter/gas_costs.nim | 1 + nimbus/vm2/interpreter/op_codes.nim | 3 ++- .../vm2/interpreter/op_handlers/oph_blockdata.nim | 13 +++++++++++++ nimbus/vm2/state.nim | 3 +++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/nimbus/vm2/computation.nim b/nimbus/vm2/computation.nim index 2e39d55b1..db2192eb4 100644 --- a/nimbus/vm2/computation.nim +++ b/nimbus/vm2/computation.nim @@ -61,6 +61,9 @@ template getDifficulty*(c: Computation): DifficultyInt = template getGasLimit*(c: Computation): GasInt = c.vmState.gasLimit +template getBaseFee*(c: Computation): Uint256 = + c.vmState.baseFee + template getChainId*(c: Computation): uint = c.vmState.chaindb.config.chainId.uint diff --git a/nimbus/vm2/interpreter/gas_costs.nim b/nimbus/vm2/interpreter/gas_costs.nim index 70af68bda..0d7c4133c 100644 --- a/nimbus/vm2/interpreter/gas_costs.nim +++ b/nimbus/vm2/interpreter/gas_costs.nim @@ -507,6 +507,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = GasLimit: fixed GasBase, ChainIdOp: fixed GasBase, SelfBalance: fixed GasLow, + BaseFee: fixed GasBase, # 50s: Stack, Memory, Storage and Flow Operations Pop: fixed GasBase, diff --git a/nimbus/vm2/interpreter/op_codes.nim b/nimbus/vm2/interpreter/op_codes.nim index d73e0a572..76184b26d 100644 --- a/nimbus/vm2/interpreter/op_codes.nim +++ b/nimbus/vm2/interpreter/op_codes.nim @@ -103,8 +103,9 @@ type ChainIdOp = 0x46, ## Get current chain’s EIP-155 unique identifier. SelfBalance = 0x47, ## Get current contract's balance. + BaseFee = 0x48, ## Get block’s base fee. EIP-3198 - Nop0x48, Nop0x49, Nop0x4A, Nop0x4B, Nop0x4C, Nop0x4D, + Nop0x49, Nop0x4A, Nop0x4B, Nop0x4C, Nop0x4D, Nop0x4E, Nop0x4F, ## .. # 50s: Stack, Memory, Storage and Flow Operations diff --git a/nimbus/vm2/interpreter/op_handlers/oph_blockdata.nim b/nimbus/vm2/interpreter/op_handlers/oph_blockdata.nim index 60d1b1057..25779bcea 100644 --- a/nimbus/vm2/interpreter/op_handlers/oph_blockdata.nim +++ b/nimbus/vm2/interpreter/op_handlers/oph_blockdata.nim @@ -68,6 +68,11 @@ const k.cpt.stack.push: k.cpt.getBalance(k.cpt.msg.contractAddress) + baseFeeOp: Vm2OpFn = proc (k: var Vm2Ctx) = + ## 0x48, Get the block's base fee. + k.cpt.stack.push: + k.cpt.getBaseFee + # ------------------------------------------------------------------------------ # Public, op exec table entries # ------------------------------------------------------------------------------ @@ -137,6 +142,14 @@ const info: "Get current contract's balance", exec: (prep: vm2OpIgnore, run: selfBalanceOp, + post: vm2OpIgnore)), + + (opCode: BaseFee, ## 0x48, EIP-1559 Block base fee. + forks: Vm2OpLondonAndLater, + name: "baseFee", + info: "Get current block's EIP-1559 base fee", + exec: (prep: vm2OpIgnore, + run: baseFeeOp, post: vm2OpIgnore))] # ------------------------------------------------------------------------------ diff --git a/nimbus/vm2/state.nim b/nimbus/vm2/state.nim index 2388ae272..8a9351161 100644 --- a/nimbus/vm2/state.nim +++ b/nimbus/vm2/state.nim @@ -129,6 +129,9 @@ method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} = method gasLimit*(vmState: BaseVMState): GasInt {.base, gcsafe.} = vmState.blockHeader.gasLimit +method baseFee*(vmState: BaseVMState): UInt256 {.base, gcsafe.} = + vmState.blockHeader.baseFee + when defined(geth): import db/geth_db