From 05e9b891f08975dbe0484e8ded5a2f408d556677 Mon Sep 17 00:00:00 2001 From: jangko Date: Sun, 27 Jun 2021 20:18:17 +0700 Subject: [PATCH] EIP-3198: add baseFee op code in nim-evm --- nimbus/transaction/host_services.nim | 1 + nimbus/vm/computation.nim | 6 ++++++ nimbus/vm/interpreter/gas_costs.nim | 1 + nimbus/vm/interpreter/opcode_values.nim | 1 + nimbus/vm/interpreter/opcodes_impl.nim | 4 ++++ nimbus/vm/interpreter_dispatch.nim | 1 + nimbus/vm/state.nim | 3 +++ 7 files changed, 17 insertions(+) diff --git a/nimbus/transaction/host_services.nim b/nimbus/transaction/host_services.nim index 2af43c76c..3110f1508 100644 --- a/nimbus/transaction/host_services.nim +++ b/nimbus/transaction/host_services.nim @@ -63,6 +63,7 @@ proc setupTxContext(host: TransactionHost) = # vmState.difficulty now unused host.txContext.block_difficulty = vmState.blockHeader.difficulty.toEvmc host.txContext.chain_id = vmState.chaindb.config.chainId.uint.u256.toEvmc + host.txContext.block_base_fee = vmState.blockHeader.baseFee.toEvmc const use_evmc_glue = defined(evmc_enabled) diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index 9295fa682..d600794a3 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -57,6 +57,12 @@ template getGasLimit*(c: Computation): GasInt = else: c.vmState.gasLimit +template getBaseFee*(c: Computation): Uint256 = + when evmc_enabled: + Uint256.fromEvmc c.host.getTxContext().block_base_fee + else: + c.vmState.baseFee + template getChainId*(c: Computation): uint = when evmc_enabled: Uint256.fromEvmc(c.host.getTxContext().chain_id).truncate(uint) diff --git a/nimbus/vm/interpreter/gas_costs.nim b/nimbus/vm/interpreter/gas_costs.nim index 78eb96cec..d3b2fd01e 100644 --- a/nimbus/vm/interpreter/gas_costs.nim +++ b/nimbus/vm/interpreter/gas_costs.nim @@ -537,6 +537,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/vm/interpreter/opcode_values.nim b/nimbus/vm/interpreter/opcode_values.nim index 7ecbce36a..643b5fce3 100644 --- a/nimbus/vm/interpreter/opcode_values.nim +++ b/nimbus/vm/interpreter/opcode_values.nim @@ -81,6 +81,7 @@ fill_enum_holes: 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 # 50s: Stack, Memory, Storage and Flow Operations Pop = 0x50, # Remove item from stack. diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index e0c405cac..d1fa1e8c1 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -384,6 +384,10 @@ op gasLimit, inline = true: ## 0x45, Get the block's gas limit push: c.getGasLimit() +op baseFee, inline = true: + ## 0x45, Get the block's gas limit + push: c.getBaseFee() + op chainId, inline = true: ## 0x46, Get current chain’s EIP-155 unique identifier. push: c.getChainId() diff --git a/nimbus/vm/interpreter_dispatch.nim b/nimbus/vm/interpreter_dispatch.nim index 7f8ab2312..ad818e789 100644 --- a/nimbus/vm/interpreter_dispatch.nim +++ b/nimbus/vm/interpreter_dispatch.nim @@ -251,6 +251,7 @@ let BerlinOpDispatch {.compileTime.}: array[Op, NimNode] = genBerlinJumpTable(Is proc genLondonJumpTable(ops: array[Op, NimNode]): array[Op, NimNode] {.compileTime.} = result = ops # incoming EIP-3198 and EIP-3529 + result[BaseFee] = newIdentNode "baseFee" let LondonOpDispatch {.compileTime.}: array[Op, NimNode] = genLondonJumpTable(BerlinOpDispatch) diff --git a/nimbus/vm/state.nim b/nimbus/vm/state.nim index 3bebca869..6d6f7a7e7 100644 --- a/nimbus/vm/state.nim +++ b/nimbus/vm/state.nim @@ -141,6 +141,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