nimbus-eth1/tests/test_gas_meter.nim

107 lines
3.3 KiB
Nim
Raw Normal View History

# Nimbus
Core db and aristo updates for destructor and tx logic (#1894) * Disable `TransactionID` related functions from `state_db.nim` why: Functions `getCommittedStorage()` and `updateOriginalRoot()` from the `state_db` module are nowhere used. The emulation of a legacy `TransactionID` type functionality is administratively expensive to provide by `Aristo` (the legacy DB version is only partially implemented, anyway). As there is no other place where `TransactionID`s are used, they will not be provided by the `Aristo` variant of the `CoreDb`. For the legacy DB API, nothing will change. * Fix copyright headers in source code * Get rid of compiler warning * Update Aristo code, remove unused `merge()` variant, export `hashify()` why: Adapt to upcoming `CoreDb` wrapper * Remove synced tx feature from `Aristo` why: + This feature allowed to synchronise transaction methods like begin, commit, and rollback for a group of descriptors. + The feature is over engineered and not needed for `CoreDb`, neither is it complete (some convergence features missing.) * Add debugging helpers to `Kvt` also: Update database iterator, add count variable yield argument similar to `Aristo`. * Provide optional destructors for `CoreDb` API why; For the upcoming Aristo wrapper, this allows to control when certain smart destruction and update can take place. The auto destructor works fine in general when the storage/cache strategy is known and acceptable when creating descriptors. * Add update option for `CoreDb` API function `hash()` why; The hash function is typically used to get the state root of the MPT. Due to lazy hashing, this might be not available on the `Aristo` DB. So the `update` function asks for re-hashing the gurrent state changes if needed. * Update API tracking log mode: `info` => `debug * Use shared `Kvt` descriptor in new Ledger API why: No need to create a new descriptor all the time
2023-11-16 19:35:03 +00:00
# Copyright (c) 2018-2023 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
unittest2, macros, strformat,
2019-02-05 19:15:50 +00:00
eth/common/eth_types,
../nimbus/[vm_types, errors, vm_internals]
# TODO: quicktest
# PS: parametrize can be easily immitated, but still quicktests would be even more useful
# disableLogging()
2018-02-07 09:50:15 +00:00
2018-07-18 12:18:17 +00:00
proc initGasMeter(startGas: GasInt): GasMeter = result.init(startGas)
proc gasMeters: seq[GasMeter] =
2018-07-18 12:18:17 +00:00
@[initGasMeter(10), initGasMeter(100), initGasMeter(999)]
macro all(element: untyped, handler: untyped): untyped =
let name = ident(&"{element.repr}s")
2020-02-03 09:40:13 +00:00
let StartGas = ident("StartGas")
result = quote:
var res = `name`()
for `element` in res.mitems:
2020-02-03 09:40:13 +00:00
let `StartGas` = `element`.gasRemaining
`handler`
# @pytest.mark.parametrize("value", (0, 10))
# def test_start_gas_on_instantiation(value):
# meter = GasMeter(value)
2019-03-13 21:36:54 +00:00
# doAssert meter.start_gas == value
# doAssert meter.gas_remaining == value
# doAssert meter.gas_refunded == 0
# @pytest.mark.parametrize("value", (-1, 2**256, 'a'))
# def test_instantiation_invalid_value(value):
# with pytest.raises(ValidationError):
# GasMeter(value)
# @pytest.mark.parametrize("amount", (0, 1, 10))
# def test_consume_gas(gas_meter, amount):
# gas_meter.consume_gas(amount, "reason")
2019-03-13 21:36:54 +00:00
# doAssert gas_meter.gas_remaining == gas_meter.start_gas - amount
# @pytest.mark.parametrize("amount", (0, 1, 99))
# def test_return_gas(gas_meter, amount):
# gas_meter.return_gas(amount)
2019-03-13 21:36:54 +00:00
# doAssert gas_meter.gas_remaining == (gas_meter.start_gas + amount)
# @pytest.mark.parametrize("amount", (0, 1, 99))
# def test_refund_gas(gas_meter, amount):
# gas_meter.refund_gas(amount)
2019-03-13 21:36:54 +00:00
# doAssert gas_meter.gas_refunded == amount
2019-09-21 05:45:23 +00:00
proc gasMeterMain*() =
suite "gasMeter":
# types
# test "consume rejects negative":
# all(gasMeter):
# expect(ValidationError):
# gasMeter.consumeGas(-1.i256, "independent")
# test "return rejects negative":
# all(gasMeter):
# expect(ValidationError):
# gasMeter.returnGas(-1.i256)
# test "refund rejects negative":
# all(gasMeter):
# expect(ValidationError):
# gasMeter.returnGas(-1.i256)
# TODO: -0/+0
test "consume spends":
all(gasMeter):
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining == StartGas)
let consume = StartGas
2019-09-21 05:45:23 +00:00
gasMeter.consumeGas(consume, "0")
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining - (StartGas - consume) == 0)
2019-09-21 05:45:23 +00:00
test "consume errors":
all(gasMeter):
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining == StartGas)
2019-09-21 05:45:23 +00:00
expect(OutOfGas):
2020-02-03 09:40:13 +00:00
gasMeter.consumeGas(StartGas + 1, "")
2019-09-21 05:45:23 +00:00
test "return refund works correctly":
all(gasMeter):
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining == StartGas)
2019-09-21 05:45:23 +00:00
check(gasMeter.gasRefunded == 0)
gasMeter.consumeGas(5, "")
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining == StartGas - 5)
2019-09-21 05:45:23 +00:00
gasMeter.returnGas(5)
2020-02-03 09:40:13 +00:00
check(gasMeter.gasRemaining == StartGas)
2019-09-21 05:45:23 +00:00
gasMeter.refundGas(5)
check(gasMeter.gasRefunded == 5)
2020-02-03 09:40:13 +00:00
when isMainModule:
gasMeterMain()