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
|
2019-11-13 14:49:39 +00:00
|
|
|
unittest2, ../nimbus/vm/precompiles, json, stew/byteutils, test_helpers, os, tables,
|
|
|
|
strformat, strutils, eth/trie/db, eth/common, ../nimbus/db/db_chain,
|
2020-11-25 13:42:15 +00:00
|
|
|
../nimbus/[vm_types, vm_state], ../nimbus/vm/computation, macros,
|
|
|
|
../nimbus/vm/interpreter/vm_forks
|
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-25 13:42:15 +00:00
|
|
|
expectedStr = test["Expected"].getStr
|
|
|
|
expected = if expectedStr != "error": expectedStr.hexToSeqByte else: @[]
|
|
|
|
dataStr = test["Input"].getStr
|
2018-10-10 16:27:00 +00:00
|
|
|
data = if dataStr.len > 0: dataStr.hexToSeqByte else: @[]
|
2019-02-14 15:20:41 +00:00
|
|
|
vmState = newBaseVMState(header.stateRoot, header, newBaseChainDB(newMemoryDb()))
|
2020-11-25 13:42:15 +00:00
|
|
|
gas = 1_000_000_000.GasInt
|
2018-10-10 16:27:00 +00:00
|
|
|
gasPrice = 1.GasInt
|
2020-11-25 13:42:15 +00:00
|
|
|
sender = initAddress(0x00)
|
|
|
|
toAddress = initAddress(address.byte)
|
|
|
|
gasCost = if test.hasKey("Gas"): test["Gas"].getInt else: -1
|
2020-01-16 06:36:58 +00:00
|
|
|
|
2020-01-20 17:59:15 +00:00
|
|
|
vmState.setupTxContext(
|
2020-01-16 06:36:58 +00:00
|
|
|
origin = sender,
|
|
|
|
gasPrice = gasPrice
|
|
|
|
)
|
|
|
|
|
|
|
|
var
|
2020-01-07 16:11:06 +00:00
|
|
|
message = Message(
|
2020-01-08 03:12:06 +00:00
|
|
|
kind: evmcCall,
|
2020-01-07 16:11:06 +00:00
|
|
|
gas: gas,
|
|
|
|
sender: sender,
|
|
|
|
contractAddress: toAddress,
|
|
|
|
codeAddress: toAddress,
|
|
|
|
value: 0.u256,
|
2020-01-20 14:02:06 +00:00
|
|
|
data: data
|
2020-01-07 16:11:06 +00:00
|
|
|
)
|
2020-11-25 13:42:15 +00:00
|
|
|
comp = newComputation(vmState, message)
|
2020-11-24 09:19:02 +00:00
|
|
|
|
2020-11-25 13:42:15 +00:00
|
|
|
let initialGas = comp.gasMeter.gasRemaining
|
|
|
|
discard execPrecompiles(comp, fork)
|
2020-11-24 09:19:02 +00:00
|
|
|
|
2020-11-25 13:42:15 +00:00
|
|
|
if expectedStr == "error":
|
|
|
|
check comp.isError
|
2020-11-24 09:19:02 +00:00
|
|
|
else:
|
2020-11-25 13:42:15 +00:00
|
|
|
let c = comp.output == expected
|
|
|
|
if not c: echo "Output : " & comp.output.toHex & "\nExpected: " & expected.toHex
|
|
|
|
check c
|
2018-10-10 16:27:00 +00:00
|
|
|
|
2020-11-25 13:42:15 +00:00
|
|
|
if gasCost >= 0:
|
|
|
|
let gasFee = initialGas - comp.gasMeter.gasRemaining
|
|
|
|
if gasFee != gasCost:
|
|
|
|
debugEcho "GAS: ", gasFee, " ", gasCost
|
|
|
|
check gasFee == gasCost
|
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
|
|
|
|
fork = parseEnum[Fork](fixtures["fork"].getStr)
|
|
|
|
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)
|
|
|
|
of "modexp" : data.doTest(fork, paModExp)
|
|
|
|
of "bn256add" : data.doTest(fork, paEcAdd)
|
|
|
|
of "bn256mul" : data.doTest(fork, paEcMul)
|
|
|
|
of "ecpairing": data.doTest(fork, paPairing)
|
|
|
|
of "blake2f" : data.doTest(fork, paBlake2bf)
|
|
|
|
else:
|
|
|
|
echo "Unknown test vector '" & $label & "'"
|
2019-11-11 04:21:16 +00:00
|
|
|
|
2019-09-21 05:45:23 +00:00
|
|
|
proc precompilesMain*() =
|
|
|
|
suite "Precompiles":
|
|
|
|
jsonTest("PrecompileTests", testFixture)
|
2019-11-11 04:21:16 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
precompilesMain()
|