Skip to content

Commit

Permalink
EVMC refundGas not breaching host/evm separation anymore (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jun 19, 2024
1 parent 0e5fd3f commit 035ef69
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
3 changes: 0 additions & 3 deletions nimbus/evm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions nimbus/evm/interpreter/op_handlers/oph_call.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions nimbus/evm/interpreter/op_handlers/oph_create.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions nimbus/transaction/evmc_vm_glue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions nimbus/transaction/host_call_nested.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 035ef69

Please sign in to comment.