2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-06-07 08:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2018-04-06 14:52:10 +00:00
|
|
|
# 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-07-05 12:41:01 +00:00
|
|
|
import
|
2019-05-30 16:42:55 +00:00
|
|
|
unittest2, macros, strformat,
|
2019-02-05 19:15:50 +00:00
|
|
|
eth/common/eth_types,
|
2024-06-17 07:56:39 +00:00
|
|
|
../nimbus/[evm/types, evm/internals, evm/evm_errors]
|
2018-01-31 13:28:25 +00:00
|
|
|
|
|
|
|
# TODO: quicktest
|
|
|
|
# PS: parametrize can be easily immitated, but still quicktests would be even more useful
|
|
|
|
|
2018-06-15 12:20:42 +00:00
|
|
|
# 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)
|
|
|
|
|
2018-01-31 13:28:25 +00:00
|
|
|
proc gasMeters: seq[GasMeter] =
|
2018-07-18 12:18:17 +00:00
|
|
|
@[initGasMeter(10), initGasMeter(100), initGasMeter(999)]
|
2018-01-31 13:28:25 +00:00
|
|
|
|
|
|
|
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")
|
2018-01-31 13:28:25 +00:00
|
|
|
result = quote:
|
|
|
|
var res = `name`()
|
|
|
|
for `element` in res.mitems:
|
2020-02-03 09:40:13 +00:00
|
|
|
let `StartGas` = `element`.gasRemaining
|
2018-01-31 13:28:25 +00:00
|
|
|
`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
|
2018-01-31 13:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
# @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
|
2018-01-31 13:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
# @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)
|
2018-01-31 13:28:25 +00:00
|
|
|
|
|
|
|
# @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
|
2018-01-31 13:28:25 +00:00
|
|
|
|
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
|
2024-06-07 08:24:32 +00:00
|
|
|
check gasMeter.consumeGas(consume, "0").isOk
|
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)
|
2024-06-07 08:24:32 +00:00
|
|
|
check gasMeter.consumeGas(StartGas + 1, "").error.code == EvmErrorCode.OutOfGas
|
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)
|
2024-06-07 08:24:32 +00:00
|
|
|
check gasMeter.consumeGas(5, "").isOk
|
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()
|