2018-10-10 16:27:00 +00:00
|
|
|
# Nimbus
|
2024-05-20 10:17:51 +00:00
|
|
|
# Copyright (c) 2018-2024 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,
|
2024-05-28 12:49:35 +00:00
|
|
|
eth/[keys, trie],
|
2024-10-04 14:34:31 +00:00
|
|
|
eth/common/transaction_utils,
|
2022-12-02 04:39:12 +00:00
|
|
|
../nimbus/common/common,
|
2024-07-04 13:48:36 +00:00
|
|
|
../tools/common/helpers as chp,
|
2024-06-17 07:56:39 +00:00
|
|
|
../nimbus/[evm/computation,
|
|
|
|
evm/state,
|
|
|
|
evm/types,
|
2021-10-14 06:08:40 +00:00
|
|
|
constants,
|
2024-06-17 07:56:39 +00:00
|
|
|
evm/precompiles {.all.},
|
2021-10-14 06:08:40 +00:00
|
|
|
transaction,
|
|
|
|
transaction/call_evm
|
|
|
|
],
|
|
|
|
|
|
|
|
./test_helpers, ./test_allowed_to_fail
|
2018-10-10 16:27:00 +00:00
|
|
|
|
2024-09-29 12:37:09 +00:00
|
|
|
proc initAddress(i: byte): EthAddress = result.data[19] = i
|
2018-10-10 16:27:00 +00:00
|
|
|
|
2024-07-04 13:48:36 +00:00
|
|
|
template doTest(fixture: JsonNode; vmState: BaseVMState; 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
|
2024-06-14 07:31:08 +00:00
|
|
|
gasExpected = if test.hasKey("Gas"):
|
|
|
|
Opt.some(GasInt test["Gas"].getInt)
|
|
|
|
else:
|
|
|
|
Opt.none(GasInt)
|
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,
|
2024-06-14 07:31:08 +00:00
|
|
|
to: Opt.some initAddress(address.byte),
|
2021-10-14 06:08:40 +00:00
|
|
|
value: 0.u256,
|
2024-10-04 14:34:31 +00:00
|
|
|
chainId: ChainId(1),
|
2021-10-14 06:08:40 +00:00
|
|
|
payload: if dataStr.len > 0: dataStr.hexToSeqByte else: @[]
|
|
|
|
)
|
2024-10-04 14:34:31 +00:00
|
|
|
let tx = signTransaction(unsignedTx, privateKey, false)
|
|
|
|
let fixtureResult = testCallEvm(tx, tx.recoverSender().expect("valid signature"), vmState)
|
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
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
if gasExpected.isSome:
|
|
|
|
if fixtureResult.gasUsed != gasExpected.get:
|
|
|
|
debugEcho "GAS: ", fixtureResult.gasUsed, " ", gasExpected.get
|
|
|
|
check fixtureResult.gasUsed == gasExpected.get
|
2020-11-24 09:19:02 +00:00
|
|
|
|
2024-07-04 13:48:36 +00:00
|
|
|
proc parseFork(x: string): string =
|
2024-09-10 06:56:08 +00:00
|
|
|
result = x.capitalizeAscii
|
|
|
|
|
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
|
2024-07-04 13:48:36 +00:00
|
|
|
conf = getChainConfig(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")[]
|
2024-07-04 13:48:36 +00:00
|
|
|
com = CommonRef.new(newCoreDbRef DefaultDbMemory, config = conf)
|
2022-01-18 16:19:32 +00:00
|
|
|
vmState = BaseVMState.new(
|
2024-06-14 07:31:08 +00:00
|
|
|
BlockHeader(number: 1'u64, stateRoot: emptyRlpHash),
|
2022-01-18 16:19:32 +00:00
|
|
|
BlockHeader(),
|
2024-05-28 12:49:35 +00:00
|
|
|
com
|
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)
|
2024-07-04 13:48:36 +00:00
|
|
|
of "ecrecover": data.doTest(vmState, paEcRecover)
|
|
|
|
of "sha256" : data.doTest(vmState, paSha256)
|
|
|
|
of "ripemd" : data.doTest(vmState, paRipeMd160)
|
|
|
|
of "identity" : data.doTest(vmState, paIdentity)
|
|
|
|
of "modexp" : data.doTest(vmState, paModExp)
|
|
|
|
of "bn256add" : data.doTest(vmState, paEcAdd)
|
|
|
|
of "bn256mul" : data.doTest(vmState, paEcMul)
|
|
|
|
of "ecpairing": data.doTest(vmState, paPairing)
|
|
|
|
of "blake2f" : data.doTest(vmState, paBlake2bf)
|
|
|
|
of "blsg1add" : data.doTest(vmState, paBlsG1Add)
|
|
|
|
of "blsg1mul" : data.doTest(vmState, paBlsG1Mul)
|
|
|
|
of "blsg1multiexp" : data.doTest(vmState, paBlsG1MultiExp)
|
|
|
|
of "blsg2add" : data.doTest(vmState, paBlsG2Add)
|
|
|
|
of "blsg2mul" : data.doTest(vmState, paBlsG2Mul)
|
2024-09-10 06:56:08 +00:00
|
|
|
of "blsg2multiexp": data.doTest(vmState, paBlsG2MultiExp)
|
|
|
|
of "blspairing": data.doTest(vmState, paBlsPairing)
|
|
|
|
of "blsmapg1": data.doTest(vmState, paBlsMapG1)
|
|
|
|
of "blsmapg2": data.doTest(vmState, 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()
|