mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-10 12:26:02 +00:00
db202dc35f
This blindly changes logging to nim-chronicles - issues that ensue: * keeps gas cost computation logs hidden behind flag * unclear if logScope is practical - for example, since vm is split over many files, topics get lost when using simple top-level per-module topics * when passing named object around, scope should incliude the name of the object but this is caught neither by logScope nor by dynamicLogScope
53 lines
2.4 KiB
Nim
53 lines
2.4 KiB
Nim
# 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.
|
|
|
|
import
|
|
chronicles, strformat, eth_common, # GasInt
|
|
../../errors, ../../vm_types
|
|
|
|
logScope:
|
|
topics = "vm gas"
|
|
|
|
proc init*(m: var GasMeter, startGas: GasInt) =
|
|
m.startGas = startGas
|
|
m.gasRemaining = m.startGas
|
|
m.gasRefunded = 0
|
|
|
|
proc consumeGas*(gasMeter: var GasMeter; amount: GasInt; reason: string) =
|
|
#if amount < 0.u256:
|
|
# raise newException(ValidationError, "Gas consumption amount must be positive")
|
|
# Alternatively: use a range type `range[0'i64 .. high(int64)]`
|
|
# https://github.com/status-im/nimbus/issues/35#issuecomment-391726518
|
|
if amount > gasMeter.gasRemaining:
|
|
raise newException(OutOfGas,
|
|
&"Out of gas: Needed {amount} - Remaining {gasMeter.gasRemaining} - Reason: {reason}")
|
|
gasMeter.gasRemaining -= amount
|
|
|
|
when defined(nimbusTrace): # XXX: https://github.com/status-im/nim-chronicles/issues/26
|
|
debug(
|
|
"GAS CONSUMPTION", total = gasMeter.gasRemaining + amount, amount, remaining = gasMeter.gasRemaining, reason)
|
|
|
|
proc returnGas*(gasMeter: var GasMeter; amount: GasInt) =
|
|
#if amount < 0.int256:
|
|
# raise newException(ValidationError, "Gas return amount must be positive")
|
|
# Alternatively: use a range type `range[0'i64 .. high(int64)]`
|
|
# https://github.com/status-im/nimbus/issues/35#issuecomment-391726518
|
|
gasMeter.gasRemaining += amount
|
|
when defined(nimbusTrace): # XXX: https://github.com/status-im/nim-chronicles/issues/26
|
|
debug(
|
|
"GAS RETURNED", consumed = gasMeter.gasRemaining - amount, amount, remaining = gasMeter.gasRemaining)
|
|
|
|
proc refundGas*(gasMeter: var GasMeter; amount: GasInt) =
|
|
#if amount < 0.int256:
|
|
# raise newException(ValidationError, "Gas refund amount must be positive")
|
|
# Alternatively: use a range type `range[0'i64 .. high(int64)]`
|
|
# https://github.com/status-im/nimbus/issues/35#issuecomment-391726518
|
|
gasMeter.gasRefunded += amount
|
|
when defined(nimbusTrace): # XXX: https://github.com/status-im/nim-chronicles/issues/26
|
|
debug(
|
|
"GAS REFUND", consumed = gasMeter.gasRemaining - amount, amount, refunded = gasMeter.gasRefunded)
|