diff --git a/nimbus/vm/computation.nim b/nimbus/vm/computation.nim index 580e71cfa..3c90a8d8e 100644 --- a/nimbus/vm/computation.nim +++ b/nimbus/vm/computation.nim @@ -18,7 +18,7 @@ proc newBaseComputation*(vmState: BaseVMState, blockNumber: UInt256, message: Me result.msg = message result.memory = Memory() result.stack = newStack() - result.gasMeter = newGasMeter(message.gas) + result.gasMeter.init(message.gas) result.children = @[] result.accountsToDelete = initTable[EthAddress, EthAddress]() result.logEntries = @[] diff --git a/nimbus/vm/interpreter/gas_meter.nim b/nimbus/vm/interpreter/gas_meter.nim index 5870527ca..68abd71bf 100644 --- a/nimbus/vm/interpreter/gas_meter.nim +++ b/nimbus/vm/interpreter/gas_meter.nim @@ -9,12 +9,11 @@ import strformat, eth_common, # GasInt ../../logging, ../../errors, ../../vm_types -proc newGasMeter*(startGas: GasInt): GasMeter = - new(result) - result.startGas = startGas - result.gasRemaining = result.startGas - result.gasRefunded = 0 - result.logger = logging.getLogger("gas") +proc init*(m: var GasMeter, startGas: GasInt) = + m.startGas = startGas + m.gasRemaining = m.startGas + m.gasRefunded = 0 + m.logger = logging.getLogger("gas") proc consumeGas*(gasMeter: var GasMeter; amount: GasInt; reason: string) = #if amount < 0.u256: diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index ba14d46bc..7c9f13400 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -565,7 +565,7 @@ op create, inline = false, value, startPosition, size: # let childComputation = applyChildBaseComputation(computation, childMsg) var childComputation: BaseComputation # TODO - stub new childComputation - childComputation.gasMeter = newGasMeter(0) # TODO GasMeter should be a normal object. + childComputation.gasMeter.init(0) if childComputation.isError: push: 0 @@ -739,7 +739,7 @@ template genCall(callName: untyped): untyped = # let childComputation = applyChildBaseComputation(computation, childMsg) var childComputation: BaseComputation # TODO - stub new childComputation - childComputation.gasMeter = newGasMeter(0) # TODO GasMeter should be a normal object. + childComputation.gasMeter.init(0) if childComputation.isError: push: 0 diff --git a/nimbus/vm_types.nim b/nimbus/vm_types.nim index e21b45a40..4565af5df 100644 --- a/nimbus/vm_types.nim +++ b/nimbus/vm_types.nim @@ -45,7 +45,7 @@ type kind*: Op runLogic*: proc(computation: var BaseComputation) - GasMeter* = ref object # TODO: use a normal object + GasMeter* = object logger*: Logger gasRefunded*: GasInt startGas*: GasInt diff --git a/tests/test_gas_meter.nim b/tests/test_gas_meter.nim index fbd870eb1..fc4f67039 100644 --- a/tests/test_gas_meter.nim +++ b/tests/test_gas_meter.nim @@ -15,8 +15,10 @@ import # disableLogging() +proc initGasMeter(startGas: GasInt): GasMeter = result.init(startGas) + proc gasMeters: seq[GasMeter] = - @[newGasMeter(10), newGasMeter(100), newGasMeter(999)] + @[initGasMeter(10), initGasMeter(100), initGasMeter(999)] macro all(element: untyped, handler: untyped): untyped = let name = ident(&"{element.repr}s")