From 035ef696a67299e68afbdac0022929fcf622bcfc Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 19 Jun 2024 19:15:23 +0700 Subject: [PATCH] EVMC refundGas not breaching host/evm separation anymore (#2395) --- nimbus/evm/computation.nim | 3 --- nimbus/evm/interpreter/op_handlers/oph_call.nim | 5 +++-- nimbus/evm/interpreter/op_handlers/oph_create.nim | 5 +++-- nimbus/transaction/evmc_vm_glue.nim | 4 ++-- nimbus/transaction/host_call_nested.nim | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/nimbus/evm/computation.nim b/nimbus/evm/computation.nim index ac3451a04..5d50c4ea2 100644 --- a/nimbus/evm/computation.nim +++ b/nimbus/evm/computation.nim @@ -372,9 +372,6 @@ template chainTo*(c: Computation, c.continuation = nil after -func merge*(c, child: Computation) = - c.gasMeter.refundGas(child.gasMeter.gasRefunded) - proc execSelfDestruct*(c: Computation, beneficiary: EthAddress) = c.vmState.mutateStateDB: let localBalance = c.getBalance(c.msg.contractAddress) diff --git a/nimbus/evm/interpreter/op_handlers/oph_call.nim b/nimbus/evm/interpreter/op_handlers/oph_call.nim index 0f1b81deb..936eae77d 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_call.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_call.nim @@ -162,7 +162,8 @@ when evmc_enabled: ? c.memory.write(p.memOutPos, c.returnData.toOpenArray(0, actualOutputSize - 1)) - c.gasMeter.returnGas(GasInt c.res.gas_left) + c.gasMeter.returnGas(c.res.gas_left) + c.gasMeter.refundGas(c.res.gas_refund) if c.res.status_code == EVMC_SUCCESS: ? c.stack.top(1) @@ -185,7 +186,7 @@ else: c.gasMeter.returnGas(child.gasMeter.gasRemaining) if child.isSuccess: - c.merge(child) + c.gasMeter.refundGas(child.gasMeter.gasRefunded) ? c.stack.top(1) c.returnData = child.output diff --git a/nimbus/evm/interpreter/op_handlers/oph_create.nim b/nimbus/evm/interpreter/op_handlers/oph_create.nim index b9668a7d8..af7cc3ac3 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_create.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_create.nim @@ -45,7 +45,8 @@ when not defined(evmc_enabled): when evmc_enabled: template execSubCreate(c: Computation; msg: ref nimbus_message) = c.chainTo(msg): - c.gasMeter.returnGas(GasInt c.res.gas_left) + c.gasMeter.returnGas(c.res.gas_left) + c.gasMeter.refundGas(c.res.gas_refund) if c.res.status_code == EVMC_SUCCESS: ? c.stack.top(c.res.create_address) elif c.res.status_code == EVMC_REVERT: @@ -69,7 +70,7 @@ else: c.gasMeter.returnGas(child.gasMeter.gasRemaining) if child.isSuccess: - c.merge(child) + c.gasMeter.refundGas(child.gasMeter.gasRefunded) ? c.stack.top child.msg.contractAddress elif not child.error.burnsGas: # Means return was `REVERT`. # From create, only use `outputData` if child returned with `REVERT`. diff --git a/nimbus/transaction/evmc_vm_glue.nim b/nimbus/transaction/evmc_vm_glue.nim index 640ed8a1c..fdb7f398a 100644 --- a/nimbus/transaction/evmc_vm_glue.nim +++ b/nimbus/transaction/evmc_vm_glue.nim @@ -62,8 +62,8 @@ proc evmcExecute(vm: ptr evmc_vm, hostInterface: ptr evmc_host_interface, status_code: c.evmcStatus, # Gas left is required to be zero when not `EVMC_SUCCESS` or `EVMC_REVERT`. gas_left: if result.status_code notin {EVMC_SUCCESS, EVMC_REVERT}: 0'i64 - else: c.gasMeter.gasRemaining.int64, - gas_refund: if result.status_code == EVMC_SUCCESS: c.gasMeter.gasRefunded.int64 + else: c.gasMeter.gasRemaining, + gas_refund: if result.status_code == EVMC_SUCCESS: c.gasMeter.gasRefunded else: 0'i64, output_data: output_data, output_size: output_size.csize_t, diff --git a/nimbus/transaction/host_call_nested.nim b/nimbus/transaction/host_call_nested.nim index 084360a1b..262e33547 100644 --- a/nimbus/transaction/host_call_nested.nim +++ b/nimbus/transaction/host_call_nested.nim @@ -42,7 +42,7 @@ proc afterExecCreateEvmcNested(host: TransactionHost, child: Computation, res.gas_left = child.gasMeter.gasRemaining if child.isSuccess: - host.computation.merge(child) + res.gas_refund = child.gasMeter.gasRefunded res.status_code = EVMC_SUCCESS res.create_address = child.msg.contractAddress.toEvmc else: @@ -78,7 +78,7 @@ proc afterExecCallEvmcNested(host: TransactionHost, child: Computation, res.gas_left = child.gasMeter.gasRemaining if child.isSuccess: - host.computation.merge(child) + res.gas_refund = child.gasMeter.gasRefunded res.status_code = EVMC_SUCCESS else: res.status_code = child.evmcStatus