2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-06-07 08:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-08 14:52:10 +00:00
|
|
|
# Licensed under either of
|
2024-06-07 08:24:32 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
{.push raises: [].}
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
import
|
|
|
|
eth/common, # GasInt
|
|
|
|
../evm_errors,
|
|
|
|
../types
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func init*(m: var GasMeter, startGas: GasInt) =
|
2021-04-08 14:52:10 +00:00
|
|
|
m.gasRemaining = startGas
|
|
|
|
m.gasRefunded = 0
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func consumeGas*(
|
|
|
|
gasMeter: var GasMeter; amount: GasInt; reason: static string): EvmResultVoid =
|
|
|
|
# TODO report reason - consumeGas is a hotspot in EVM execution so it has to
|
|
|
|
# be done carefully
|
2021-04-08 14:52:10 +00:00
|
|
|
if amount > gasMeter.gasRemaining:
|
2024-07-13 18:42:49 +00:00
|
|
|
return err(gasErr(OutOfGas))
|
2021-04-08 14:52:10 +00:00
|
|
|
gasMeter.gasRemaining -= amount
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func returnGas*(gasMeter: var GasMeter; amount: GasInt) =
|
2021-04-08 14:52:10 +00:00
|
|
|
gasMeter.gasRemaining += amount
|
|
|
|
|
2024-07-07 06:52:11 +00:00
|
|
|
func refundGas*(gasMeter: var GasMeter; amount: int64) =
|
|
|
|
# EIP-2183 Net gas metering for sstore is built upon idea
|
|
|
|
# that the refund counter is only one in an EVM like geth does.
|
|
|
|
# EIP-2183 gurantee that the counter can never go below zero.
|
|
|
|
# But nimbus, EVMC, and emvone taken different route, the refund counter
|
|
|
|
# is present at each level of recursion. That's why EVMC/evmone is using
|
|
|
|
# int64 while geth using uint64 for their gas calculation.
|
|
|
|
# After nimbus converting GasInt to uint64, the gas refund
|
|
|
|
# cannot be converted to uint64 too, because the sum of all children gas refund,
|
|
|
|
# no matter positive or negative will be >= 0 when EVM finish execution.
|
2021-04-08 14:52:10 +00:00
|
|
|
gasMeter.gasRefunded += amount
|