From c1ef8632b29bb1271d31cf1b9a4a34a03f120197 Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 16 Jan 2020 14:01:59 +0700 Subject: [PATCH] move fork and gasCosts from Computation to vmState --- nimbus/rpc/p2p.nim | 8 ++++++-- nimbus/vm/computation.nim | 14 +++++++------- nimbus/vm/interpreter/opcodes_impl.nim | 4 ++-- nimbus/vm_state.nim | 11 +++++++++-- nimbus/vm_state_transactions.nim | 5 +++-- nimbus/vm_types.nim | 4 ++-- tests/macro_assembler.nim | 5 +++-- 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/nimbus/rpc/p2p.nim b/nimbus/rpc/p2p.nim index 1bc8a81b6..ef4d89e34 100644 --- a/nimbus/rpc/p2p.nim +++ b/nimbus/rpc/p2p.nim @@ -283,8 +283,6 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = kind: if contractCreation: evmcCreate else: evmcCall, depth: 0, gas: gasLimit, - gasPrice: gasPrice, - origin: sender, sender: sender, contractAddress: destination, codeAddress: CREATE_CONTRACT_ADDRESS, @@ -292,6 +290,12 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) = data: data, code: vmState.readOnlyStateDB.getCode(destination).toSeq ) + + vmState.txContext( + origin = sender, + gasPrice = gasPrice + ) + result = newComputation(vmState, message) rpcsrv.rpc("eth_call") do(call: EthCall, quantityTag: string) -> HexDataStr: diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index e2a256982..6756bd53a 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -17,7 +17,7 @@ import logScope: topics = "vm computation" -proc newComputation*(vmState: BaseVMState, message: Message, forkOverride=none(Fork)): Computation = +proc newComputation*(vmState: BaseVMState, message: Message): Computation = new result result.vmState = vmState result.msg = message @@ -27,16 +27,16 @@ proc newComputation*(vmState: BaseVMState, message: Message, forkOverride=none(F result.touchedAccounts = initHashSet[EthAddress]() result.suicides = initHashSet[EthAddress]() result.code = newCodeStream(message.code) - result.fork = - if forkOverride.isSome: - forkOverride.get - else: - vmState.blockNumber.toFork - result.gasCosts = result.fork.forkToSchedule # a dummy/terminus continuation proc result.nextProc = proc() = discard +template gasCosts*(c: Computation): untyped = + c.vmState.gasCosts + +template fork*(c: Computation): untyped = + c.vmState.fork + proc isOriginComputation*(c: Computation): bool = # Is this computation the computation initiated by a transaction c.msg.sender == c.vmState.txOrigin diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index 12e9b5ffc..ed19dc8c4 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -577,7 +577,7 @@ proc setupCreate(c: Computation, memPos, len: int, value: Uint256, opCode: stati code: callData ) - result = newComputation(c.vmState, childMsg, some(c.fork)) + result = newComputation(c.vmState, childMsg) template genCreate(callName: untyped, opCode: Op): untyped = op callName, inline = false, val, startPosition, size: @@ -755,7 +755,7 @@ template genCall(callName: untyped, opCode: Op): untyped = code: code.toSeq, flags: flags) - var child = newComputation(c.vmState, childMsg, some(c.fork)) + var child = newComputation(c.vmState, childMsg) c.memOutPos = memOutPos c.memOutLen = memOutLen diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index 6c9ac1bb7..8e55e1d81 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -6,8 +6,9 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - macros, strformat, tables, sets, + macros, strformat, tables, sets, options, eth/common, + vm/interpreter/[vm_forks, gas_costs], ./constants, ./db/[db_chain, state_db], ./utils, json, vm_types, vm/transaction_tracer @@ -42,11 +43,17 @@ proc newBaseVMState*(prevStateRoot: Hash256, header: BlockHeader, new result result.init(prevStateRoot, header, chainDB, tracerFlags) -proc txContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt) = +proc txContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt, forkOverride=none(Fork)) = ## this proc will be called each time a new transaction ## is going to be executed vmState.txOrigin = origin vmState.txGasPrice = gasPrice + vmState.fork = + if forkOverride.isSome: + forkOverride.get + else: + vmState.blockHeader.blockNumber.toFork + vmState.gasCosts = vmState.fork.forkToSchedule method blockhash*(vmState: BaseVMState): Hash256 {.base, gcsafe.} = vmState.blockHeader.hash diff --git a/nimbus/vm_state_transactions.nim b/nimbus/vm_state_transactions.nim index 003e39923..0a0920d8e 100644 --- a/nimbus/vm_state_transactions.nim +++ b/nimbus/vm_state_transactions.nim @@ -46,7 +46,8 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient: vmState.txContext( origin = sender, - gasPrice = tx.gasPrice + gasPrice = tx.gasPrice, + forkOverride = some(fork) ) let msg = Message( @@ -61,7 +62,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient: code: code ) - result = newComputation(vmState, msg, some(fork)) + result = newComputation(vmState, msg) doAssert result.isOriginComputation proc execComputation*(c: Computation) = diff --git a/nimbus/vm_types.nim b/nimbus/vm_types.nim index 9f38e3b9b..dbb6c6066 100644 --- a/nimbus/vm_types.nim +++ b/nimbus/vm_types.nim @@ -30,6 +30,8 @@ type status* : bool txOrigin* : EthAddress txGasPrice* : GasInt + gasCosts* : GasCosts + fork* : Fork AccessLogs* = ref object reads*: Table[string, string] @@ -67,8 +69,6 @@ type error*: Error touchedAccounts*: HashSet[EthAddress] suicides*: HashSet[EthAddress] - gasCosts*: GasCosts # TODO - will be hidden at a lower layer - fork*: Fork logEntries*: seq[Log] dbsnapshot*: Snapshot instr*: Op diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index d5c1a9c4a..30ff929d1 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -207,7 +207,8 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress, vmState.txContext( origin = sender, - gasPrice = tx.gasPrice + gasPrice = tx.gasPrice, + forkOverride = some(fork) ) let contractAddress = generateAddress(sender, tx.accountNonce) @@ -223,7 +224,7 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress, code: tx.payload ) - newComputation(vmState, msg, some(fork)) + newComputation(vmState, msg) proc initDatabase*(): (Uint256, BaseChainDB) = let