2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * 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.
|
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
import
|
2019-02-05 19:15:50 +00:00
|
|
|
chronicles, strformat, eth/common, # GasInt
|
2018-08-23 03:38:00 +00:00
|
|
|
../../errors, ../../vm_types
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "vm gas"
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-07-18 12:18:17 +00:00
|
|
|
proc init*(m: var GasMeter, startGas: GasInt) =
|
|
|
|
m.startGas = startGas
|
|
|
|
m.gasRemaining = m.startGas
|
|
|
|
m.gasRefunded = 0
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
proc consumeGas*(gasMeter: var GasMeter; amount: GasInt; reason: string) =
|
2018-01-15 18:42:40 +00:00
|
|
|
if amount > gasMeter.gasRemaining:
|
|
|
|
raise newException(OutOfGas,
|
2018-01-22 22:23:07 +00:00
|
|
|
&"Out of gas: Needed {amount} - Remaining {gasMeter.gasRemaining} - Reason: {reason}")
|
2018-01-15 18:42:40 +00:00
|
|
|
gasMeter.gasRemaining -= amount
|
2018-12-06 23:16:34 +00:00
|
|
|
trace "GAS CONSUMPTION", total = gasMeter.gasRemaining + amount, amount, remaining = gasMeter.gasRemaining, reason
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
proc returnGas*(gasMeter: var GasMeter; amount: GasInt) =
|
2018-01-15 18:42:40 +00:00
|
|
|
gasMeter.gasRemaining += amount
|
2018-12-06 23:16:34 +00:00
|
|
|
trace "GAS RETURNED", consumed = gasMeter.gasRemaining - amount, amount, remaining = gasMeter.gasRemaining
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
proc refundGas*(gasMeter: var GasMeter; amount: GasInt) =
|
2018-01-15 18:42:40 +00:00
|
|
|
gasMeter.gasRefunded += amount
|
2018-12-06 23:16:34 +00:00
|
|
|
trace "GAS REFUND", consumed = gasMeter.gasRemaining - amount, amount, refunded = gasMeter.gasRefunded
|
2019-04-24 12:26:52 +00:00
|
|
|
|
|
|
|
proc resetGas*(gasMeter: var GasMeter) =
|
|
|
|
gasMeter.gasRemaining = gasMeter.startGas
|
|
|
|
gasMeter.gasRefunded = 0
|