2018-10-10 16:27:00 +00:00
|
|
|
# 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
|
2018-10-10 16:27:00 +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.
|
|
|
|
|
|
|
|
import
|
2022-12-02 04:39:12 +00:00
|
|
|
std/[strformat, strutils, json, os, tables, macros],
|
2021-10-14 06:08:40 +00:00
|
|
|
unittest2, stew/byteutils,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/keys,
|
|
|
|
../nimbus/common/common,
|
2021-10-14 06:08:40 +00:00
|
|
|
../nimbus/[vm_computation,
|
|
|
|
vm_state,
|
2022-01-18 16:19:32 +00:00
|
|
|
vm_types,
|
2021-10-14 06:08:40 +00:00
|
|
|
constants,
|
|
|
|
vm_precompiles,
|
|
|
|
transaction,
|
|
|
|
transaction/call_evm
|
|
|
|
],
|
|
|
|
|
|
|
|
./test_helpers, ./test_allowed_to_fail
|
2018-10-10 16:27:00 +00:00
|
|
|
|
|
|
|
proc initAddress(i: byte): EthAddress = result[19] = i
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
template doTest(fixture: JsonNode; vmState: BaseVMState; fork: EVMFork, address: PrecompileAddresses): untyped =
|
2018-10-10 16:27:00 +00:00
|
|
|
for test in fixture:
|
|
|
|
let
|
2020-11-27 14:42:17 +00:00
|
|
|
expectedErr = test.hasKey("ExpectedError")
|
|
|
|
expected = if test.hasKey("Expected"): hexToSeqByte(test["Expected"].getStr) else: @[]
|
2021-10-28 09:42:39 +00:00
|
|
|
dataStr = test["Input"].getStr
|
Precompiles: Change precompile tests to use fixtureCallEvm
Move the EVM setup and call in precompile tests to `fixtureCallEvm` in
`call_evm`. Extra return values needed for testing are returned specially, and
the convention for reporting gas used is changed to match `asmCallEvm`.
Although the precompile tests used `execPrecompiles` before, `executeOpcodes`
does perfectly well as a substitute, allowing `fixtureCallEvm` to be shared.
_Significantly, this patch also makes `Computation` more or less an internal
type of the EVM now._
Nothing outside the EVM (except `call_evm`) needs access any more to
`Computation`, `execComputation`, `executeOpcodes` or `execPrecompiles`.
Many imports can be trimmed, some files removed, and EVMC is much closer.
(As a bonus, the functions in `call_evm` reveal what capabilities parts of the
program have needed over time, makes certain bugs and inconsistencies clearer,
and suggests how to refactor into a more useful shared entry point.)
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-05 00:55:49 +00:00
|
|
|
gasExpected = if test.hasKey("Gas"): test["Gas"].getInt else: -1
|
2020-01-16 06:36:58 +00:00
|
|
|
|
2021-10-14 06:08:40 +00:00
|
|
|
let unsignedTx = Transaction(
|
|
|
|
txType: TxLegacy,
|
|
|
|
nonce: 0,
|
|
|
|
gasPrice: 1.GasInt,
|
|
|
|
gasLimit: 1_000_000_000.GasInt,
|
|
|
|
to: initAddress(address.byte).some,
|
|
|
|
value: 0.u256,
|
|
|
|
payload: if dataStr.len > 0: dataStr.hexToSeqByte else: @[]
|
|
|
|
)
|
|
|
|
let tx = signTransaction(unsignedTx, privateKey, ChainId(1), false)
|
|
|
|
let fixtureResult = testCallEvm(tx, tx.getSender, vmState, fork)
|
2020-11-24 09:19:02 +00:00
|
|
|
|
2020-11-27 14:42:17 +00:00
|
|
|
if expectedErr:
|
Precompiles: Change precompile tests to use fixtureCallEvm
Move the EVM setup and call in precompile tests to `fixtureCallEvm` in
`call_evm`. Extra return values needed for testing are returned specially, and
the convention for reporting gas used is changed to match `asmCallEvm`.
Although the precompile tests used `execPrecompiles` before, `executeOpcodes`
does perfectly well as a substitute, allowing `fixtureCallEvm` to be shared.
_Significantly, this patch also makes `Computation` more or less an internal
type of the EVM now._
Nothing outside the EVM (except `call_evm`) needs access any more to
`Computation`, `execComputation`, `executeOpcodes` or `execPrecompiles`.
Many imports can be trimmed, some files removed, and EVMC is much closer.
(As a bonus, the functions in `call_evm` reveal what capabilities parts of the
program have needed over time, makes certain bugs and inconsistencies clearer,
and suggests how to refactor into a more useful shared entry point.)
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-05 00:55:49 +00:00
|
|
|
check fixtureResult.isError
|
2020-11-24 09:19:02 +00:00
|
|
|
else:
|
Precompiles: Change precompile tests to use fixtureCallEvm
Move the EVM setup and call in precompile tests to `fixtureCallEvm` in
`call_evm`. Extra return values needed for testing are returned specially, and
the convention for reporting gas used is changed to match `asmCallEvm`.
Although the precompile tests used `execPrecompiles` before, `executeOpcodes`
does perfectly well as a substitute, allowing `fixtureCallEvm` to be shared.
_Significantly, this patch also makes `Computation` more or less an internal
type of the EVM now._
Nothing outside the EVM (except `call_evm`) needs access any more to
`Computation`, `execComputation`, `executeOpcodes` or `execPrecompiles`.
Many imports can be trimmed, some files removed, and EVMC is much closer.
(As a bonus, the functions in `call_evm` reveal what capabilities parts of the
program have needed over time, makes certain bugs and inconsistencies clearer,
and suggests how to refactor into a more useful shared entry point.)
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-05 00:55:49 +00:00
|
|
|
check not fixtureResult.isError
|
|
|
|
let c = fixtureResult.output == expected
|
|
|
|
if not c: echo "Output : " & fixtureResult.output.toHex & "\nExpected: " & expected.toHex
|
2020-11-25 13:42:15 +00:00
|
|
|
check c
|
2018-10-10 16:27:00 +00:00
|
|
|
|
Precompiles: Change precompile tests to use fixtureCallEvm
Move the EVM setup and call in precompile tests to `fixtureCallEvm` in
`call_evm`. Extra return values needed for testing are returned specially, and
the convention for reporting gas used is changed to match `asmCallEvm`.
Although the precompile tests used `execPrecompiles` before, `executeOpcodes`
does perfectly well as a substitute, allowing `fixtureCallEvm` to be shared.
_Significantly, this patch also makes `Computation` more or less an internal
type of the EVM now._
Nothing outside the EVM (except `call_evm`) needs access any more to
`Computation`, `execComputation`, `executeOpcodes` or `execPrecompiles`.
Many imports can be trimmed, some files removed, and EVMC is much closer.
(As a bonus, the functions in `call_evm` reveal what capabilities parts of the
program have needed over time, makes certain bugs and inconsistencies clearer,
and suggests how to refactor into a more useful shared entry point.)
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-05 00:55:49 +00:00
|
|
|
if gasExpected >= 0:
|
|
|
|
if fixtureResult.gasUsed != gasExpected:
|
|
|
|
debugEcho "GAS: ", fixtureResult.gasUsed, " ", gasExpected
|
|
|
|
check fixtureResult.gasUsed == gasExpected
|
2020-11-24 09:19:02 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc parseFork(x: string): EVMFork =
|
|
|
|
let x = x.toLowerAscii
|
|
|
|
for name, fork in nameToFork:
|
|
|
|
if name.toLowerAscii == x:
|
|
|
|
return fork
|
|
|
|
doAssert(false, "unsupported fork name " & x)
|
|
|
|
|
2018-10-10 16:27:00 +00:00
|
|
|
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
|
2020-11-25 13:42:15 +00:00
|
|
|
let
|
|
|
|
label = fixtures["func"].getStr
|
2022-12-02 04:39:12 +00:00
|
|
|
fork = parseFork(fixtures["fork"].getStr)
|
2020-11-25 13:42:15 +00:00
|
|
|
data = fixtures["data"]
|
2021-10-14 06:08:40 +00:00
|
|
|
privateKey = PrivateKey.fromHex("7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d")[]
|
2022-01-18 16:19:32 +00:00
|
|
|
vmState = BaseVMState.new(
|
|
|
|
BlockHeader(blockNumber: 1.u256),
|
|
|
|
BlockHeader(),
|
2023-08-04 11:10:09 +00:00
|
|
|
CommonRef.new(newCoreDbRef LegacyDbMemory, config = ChainConfig())
|
2022-11-12 04:12:17 +00:00
|
|
|
)
|
2021-10-28 09:42:39 +00:00
|
|
|
|
2020-11-25 13:42:15 +00:00
|
|
|
case toLowerAscii(label)
|
2022-01-18 16:19:32 +00:00
|
|
|
of "ecrecover": data.doTest(vmState, fork, paEcRecover)
|
|
|
|
of "sha256" : data.doTest(vmState, fork, paSha256)
|
|
|
|
of "ripemd" : data.doTest(vmState, fork, paRipeMd160)
|
|
|
|
of "identity" : data.doTest(vmState, fork, paIdentity)
|
|
|
|
of "modexp" : data.doTest(vmState, fork, paModExp)
|
|
|
|
of "bn256add" : data.doTest(vmState, fork, paEcAdd)
|
|
|
|
of "bn256mul" : data.doTest(vmState, fork, paEcMul)
|
|
|
|
of "ecpairing": data.doTest(vmState, fork, paPairing)
|
|
|
|
of "blake2f" : data.doTest(vmState, fork, paBlake2bf)
|
2021-05-16 12:44:24 +00:00
|
|
|
# EIP 2537: disabled
|
|
|
|
# reason: not included in berlin
|
2022-01-18 16:19:32 +00:00
|
|
|
#of "blsg1add" : data.doTest(vmState, fork, paBlsG1Add)
|
|
|
|
#of "blsg1mul" : data.doTest(vmState, fork, paBlsG1Mul)
|
|
|
|
#of "blsg1multiexp" : data.doTest(vmState, fork, paBlsG1MultiExp)
|
|
|
|
#of "blsg2add" : data.doTest(vmState, fork, paBlsG2Add)
|
|
|
|
#of "blsg2mul" : data.doTest(vmState, fork, paBlsG2Mul)
|
|
|
|
#of "blsg2multiexp": data.doTest(vmState, fork, paBlsG2MultiExp)
|
|
|
|
#of "blspairing": data.doTest(vmState, fork, paBlsPairing)
|
|
|
|
#of "blsmapg1": data.doTest(vmState, fork, paBlsMapG1)
|
|
|
|
#of "blsmapg2": data.doTest(vmState, fork, paBlsMapG2)
|
2020-11-25 13:42:15 +00:00
|
|
|
else:
|
|
|
|
echo "Unknown test vector '" & $label & "'"
|
2020-11-25 13:55:53 +00:00
|
|
|
testStatusIMPL = SKIPPED
|
2019-11-11 04:21:16 +00:00
|
|
|
|
2019-09-21 05:45:23 +00:00
|
|
|
proc precompilesMain*() =
|
|
|
|
suite "Precompiles":
|
2021-10-14 06:08:40 +00:00
|
|
|
jsonTest("PrecompileTests", testFixture, skipPrecompilesTests)
|
2019-11-11 04:21:16 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
precompilesMain()
|