2018-10-10 16:27:00 +00:00
|
|
|
# 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
|
2021-03-31 13:29:33 +00:00
|
|
|
unittest2, ../nimbus/vm_precompiles, json, stew/byteutils, test_helpers, os, tables,
|
2021-05-17 05:50:05 +00:00
|
|
|
strformat, strutils, eth/trie/db, eth/common, ../nimbus/db/db_chain, ../nimbus/constants,
|
2021-06-01 10:54:13 +00:00
|
|
|
../nimbus/[vm_computation, vm_state, forks], macros,
|
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
|
|
|
test_allowed_to_fail,
|
|
|
|
../nimbus/transaction/call_evm, options
|
2018-10-10 16:27:00 +00:00
|
|
|
|
|
|
|
proc initAddress(i: byte): EthAddress = result[19] = i
|
|
|
|
|
2020-11-25 13:42:15 +00:00
|
|
|
template doTest(fixture: JsonNode, fork: Fork, address: PrecompileAddresses): untyped =
|
2018-10-10 16:27:00 +00:00
|
|
|
for test in fixture:
|
|
|
|
let
|
|
|
|
blockNum = 1.u256 # TODO: Check other forks
|
|
|
|
header = BlockHeader(blockNumber: blockNum)
|
2020-11-27 14:42:17 +00:00
|
|
|
expectedErr = test.hasKey("ExpectedError")
|
|
|
|
expected = if test.hasKey("Expected"): hexToSeqByte(test["Expected"].getStr) else: @[]
|
2020-11-25 13:42:15 +00:00
|
|
|
dataStr = test["Input"].getStr
|
2019-02-14 15:20:41 +00:00
|
|
|
vmState = newBaseVMState(header.stateRoot, header, newBaseChainDB(newMemoryDb()))
|
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
|
|
|
|
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
|
|
|
var call: RpcCallData
|
2021-05-17 05:50:05 +00:00
|
|
|
call.source = ZERO_ADDRESS
|
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
|
|
|
call.to = initAddress(address.byte)
|
|
|
|
call.gas = 1_000_000_000.GasInt
|
|
|
|
call.gasPrice = 1.GasInt
|
|
|
|
call.value = 0.u256
|
|
|
|
call.data = if dataStr.len > 0: dataStr.hexToSeqByte else: @[]
|
|
|
|
call.contractCreation = false
|
2020-01-16 06:36:58 +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
|
|
|
let fixtureResult = fixtureCallEvm(vmState, call, call.source, some(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
|
|
|
|
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
|
2020-12-08 16:44:39 +00:00
|
|
|
fork = parseEnum[Fork](fixtures["fork"].getStr.toLowerAscii)
|
2020-11-25 13:42:15 +00:00
|
|
|
data = fixtures["data"]
|
|
|
|
|
|
|
|
case toLowerAscii(label)
|
|
|
|
of "ecrecover": data.doTest(fork, paEcRecover)
|
|
|
|
of "sha256" : data.doTest(fork, paSha256)
|
|
|
|
of "ripemd" : data.doTest(fork, paRipeMd160)
|
|
|
|
of "identity" : data.doTest(fork, paIdentity)
|
2021-01-11 08:33:30 +00:00
|
|
|
of "modexp" : data.doTest(fork, paModExp)
|
2020-11-25 13:42:15 +00:00
|
|
|
of "bn256add" : data.doTest(fork, paEcAdd)
|
|
|
|
of "bn256mul" : data.doTest(fork, paEcMul)
|
|
|
|
of "ecpairing": data.doTest(fork, paPairing)
|
|
|
|
of "blake2f" : data.doTest(fork, paBlake2bf)
|
2021-05-16 12:44:24 +00:00
|
|
|
# EIP 2537: disabled
|
|
|
|
# reason: not included in berlin
|
|
|
|
#of "blsg1add" : data.doTest(fork, paBlsG1Add)
|
|
|
|
#of "blsg1mul" : data.doTest(fork, paBlsG1Mul)
|
|
|
|
#of "blsg1multiexp" : data.doTest(fork, paBlsG1MultiExp)
|
|
|
|
#of "blsg2add" : data.doTest(fork, paBlsG2Add)
|
|
|
|
#of "blsg2mul" : data.doTest(fork, paBlsG2Mul)
|
|
|
|
#of "blsg2multiexp": data.doTest(fork, paBlsG2MultiExp)
|
|
|
|
#of "blspairing": data.doTest(fork, paBlsPairing)
|
|
|
|
#of "blsmapg1": data.doTest(fork, paBlsMapG1)
|
|
|
|
#of "blsmapg2": data.doTest(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-06-08 13:44:35 +00:00
|
|
|
# TODO: For now, EVMC is incompatible with these tests.
|
|
|
|
when defined(evmc_enabled):
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
jsonTest("PrecompileTests", testFixture, skipPrecompilesTests)
|
2019-11-11 04:21:16 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
precompilesMain()
|