diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index 576551403..e2a256982 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -39,7 +39,7 @@ proc newComputation*(vmState: BaseVMState, message: Message, forkOverride=none(F proc isOriginComputation*(c: Computation): bool = # Is this computation the computation initiated by a transaction - c.msg.isOrigin + c.msg.sender == c.vmState.txOrigin template isSuccess*(c: Computation): bool = c.error.isNil diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index 249a46926..12e9b5ffc 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -240,7 +240,7 @@ op balance, inline = true: op origin, inline = true: ## 0x32, Get execution origination address. - push: c.msg.origin + push: c.vmState.txOrigin op caller, inline = true: ## 0x33, Get caller address. @@ -301,7 +301,7 @@ op codeCopy, inline = false, memStartPos, copyStartPos, size: op gasprice, inline = true: ## 0x3A, Get price of gas in current environment. - push: c.msg.gasPrice + push: c.vmState.txGasPrice op extCodeSize, inline = true: ## 0x3b, Get size of an account's code @@ -569,8 +569,6 @@ proc setupCreate(c: Computation, memPos, len: int, value: Uint256, opCode: stati kind: callKind, depth: c.msg.depth + 1, gas: createMsgGas, - gasPrice: c.msg.gasPrice, - origin: c.msg.origin, sender: c.msg.contractAddress, contractAddress: contractAddress, codeAddress: CREATE_CONTRACT_ADDRESS, @@ -749,8 +747,6 @@ template genCall(callName: untyped, opCode: Op): untyped = kind: callKind, depth: c.msg.depth + 1, gas: childGasLimit, - gasPrice: c.msg.gasPrice, - origin: c.msg.origin, sender: sender, contractAddress: contractAddress, codeAddress: codeAddress, diff --git a/nimbus/vm/message.nim b/nimbus/vm/message.nim index a0e4a7177..f26adb00e 100644 --- a/nimbus/vm/message.nim +++ b/nimbus/vm/message.nim @@ -9,8 +9,5 @@ import eth/common, ../constants, ../validation, ../vm_types, chronicles -proc isOrigin*(message: Message): bool = - message.sender == message.origin - proc isCreate*(message: Message): bool = message.kind in {evmcCreate, evmcCreate2} diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index 5eb9b0657..6c9ac1bb7 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -35,13 +35,19 @@ proc init*(self: BaseVMState, prevStateRoot: Hash256, header: BlockHeader, self.tracingEnabled = TracerFlags.EnableTracing in tracerFlags self.logEntries = @[] self.accountDb = newAccountStateDB(chainDB.db, prevStateRoot, chainDB.pruneTrie) - self.touchedAccounts = initHashSet[EthAddress]() + self.touchedAccounts = initHashSet[EthAddress]() proc newBaseVMState*(prevStateRoot: Hash256, header: BlockHeader, chainDB: BaseChainDB, tracerFlags: set[TracerFlags] = {}): BaseVMState = new result result.init(prevStateRoot, header, chainDB, tracerFlags) +proc txContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt) = + ## this proc will be called each time a new transaction + ## is going to be executed + vmState.txOrigin = origin + vmState.txGasPrice = gasPrice + 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 428952bca..003e39923 100644 --- a/nimbus/vm_state_transactions.nim +++ b/nimbus/vm_state_transactions.nim @@ -44,12 +44,15 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient: debug "not enough gas to perform calculation", gas=gas return + vmState.txContext( + origin = sender, + gasPrice = tx.gasPrice + ) + let msg = Message( kind: if tx.isContractCreation: evmcCreate else: evmcCall, depth: 0, gas: gas, - gasPrice: tx.gasPrice, - origin: sender, sender: sender, contractAddress: recipient, codeAddress: tx.to, diff --git a/nimbus/vm_types.nim b/nimbus/vm_types.nim index 821b1c4bb..9f38e3b9b 100644 --- a/nimbus/vm_types.nim +++ b/nimbus/vm_types.nim @@ -28,6 +28,8 @@ type touchedAccounts*: HashSet[EthAddress] suicides* : HashSet[EthAddress] status* : bool + txOrigin* : EthAddress + txGasPrice* : GasInt AccessLogs* = ref object reads*: Table[string, string] @@ -100,8 +102,6 @@ type kind*: CallKind depth*: int gas*: GasInt - gasPrice*: GasInt - origin*: EthAddress sender*: EthAddress contractAddress*: EthAddress codeAddress*: EthAddress diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index 8de6bded9..d5c1a9c4a 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -205,13 +205,16 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress, let gasUsed = 0 #tx.payload.intrinsicGas.GasInt + gasFees[fork][GasTXCreate] + vmState.txContext( + origin = sender, + gasPrice = tx.gasPrice + ) + let contractAddress = generateAddress(sender, tx.accountNonce) let msg = Message( kind: evmcCall, depth: 0, gas: tx.gasLimit - gasUsed, - gasPrice: tx.gasPrice, - origin: sender, sender: sender, contractAddress: contractAddress, codeAddress: tx.to, diff --git a/tests/test_precompiles.nim b/tests/test_precompiles.nim index ddcda21dd..50c6b645c 100644 --- a/tests/test_precompiles.nim +++ b/tests/test_precompiles.nim @@ -29,11 +29,16 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped = gasPrice = 1.GasInt sender: EthAddress toAddress = initAddress(address) + + vmState.txContext( + origin = sender, + gasPrice = gasPrice + ) + + var message = Message( kind: evmcCall, gas: gas, - gasPrice: gasPrice, - origin: sender, sender: sender, contractAddress: toAddress, codeAddress: toAddress, diff --git a/tests/test_vm_json.nim b/tests/test_vm_json.nim index a1e9baf96..67dc81212 100644 --- a/tests/test_vm_json.nim +++ b/tests/test_vm_json.nim @@ -47,14 +47,17 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) = let address = fexec{"address"}.getStr.parseAddress code = db.getCode(address).toSeq + vmState.txContext( + origin = fexec{"origin"}.getStr.parseAddress, + gasPrice = fexec{"gasPrice"}.getHexadecimalInt + ) + code = fexec{"code"}.getStr.hexToSeqByte let toAddress = fexec{"address"}.getStr.parseAddress let message = Message( kind: if toAddress == ZERO_ADDRESS: evmcCreate else: evmcCall, # assume ZERO_ADDRESS is a contract creation depth: 0, gas: fexec{"gas"}.getHexadecimalInt, - gasPrice: fexec{"gasPrice"}.getHexadecimalInt, - origin: fexec{"origin"}.getStr.parseAddress, sender: fexec{"caller"}.getStr.parseAddress, contractAddress: toAddress, codeAddress: toAddress,