2018-04-06 14:52:10 +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.
|
|
|
|
|
2018-01-17 11:24:09 +00:00
|
|
|
import
|
2018-09-07 19:44:17 +00:00
|
|
|
os, macros, json, sequtils, strformat, strutils, parseutils, ospaths, tables,
|
2018-07-05 12:41:01 +00:00
|
|
|
byteutils, eth_common, eth_keys, ranges/typedranges,
|
2018-06-15 09:11:25 +00:00
|
|
|
../nimbus/[vm_state, constants],
|
|
|
|
../nimbus/db/[db_chain, state_db],
|
2018-09-07 19:44:17 +00:00
|
|
|
../nimbus/transaction,
|
|
|
|
../nimbus/vm/interpreter/[gas_costs, vm_forks]
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-02-27 18:10:34 +00:00
|
|
|
type
|
|
|
|
Status* {.pure.} = enum OK, Fail, Skip
|
|
|
|
|
2018-09-07 19:44:17 +00:00
|
|
|
func slowTest*(folder: string, name: string): bool =
|
|
|
|
# TODO: add vmPerformance and loop check here
|
|
|
|
result = name in @["randomStatetest352.json", "randomStatetest1.json",
|
|
|
|
"randomStatetest32.json", "randomStatetest347.json",
|
|
|
|
"randomStatetest393.json", "randomStatetest626.json",
|
|
|
|
"CALLCODE_Bounds.json", "DELEGATECALL_Bounds3.json",
|
|
|
|
"CALLCODE_Bounds4.json", "CALL_Bounds.json",
|
|
|
|
"DELEGATECALL_Bounds2.json", "CALL_Bounds3.json",
|
|
|
|
"CALLCODE_Bounds2.json", "CALLCODE_Bounds3.json",
|
|
|
|
"DELEGATECALL_Bounds.json", "CALL_Bounds2a.json",
|
|
|
|
"CALL_Bounds2.json",
|
|
|
|
"CallToNameRegistratorMemOOGAndInsufficientBalance.json",
|
|
|
|
"CallToNameRegistratorTooMuchMemory0.json"]
|
|
|
|
|
2018-09-05 23:48:06 +00:00
|
|
|
func validTest*(folder: string, name: string): bool =
|
2018-02-27 18:10:34 +00:00
|
|
|
# tests we want to skip or which segfault will be skipped here
|
2018-09-04 12:30:15 +00:00
|
|
|
result = (folder != "vmPerformance" or "loop" notin name) and
|
|
|
|
(folder notin @["stTransitionTest", "stStackTests", "stDelegatecallTestHomestead"] and
|
2018-09-07 19:44:17 +00:00
|
|
|
not slowTest(folder, name) and
|
2018-09-04 12:30:15 +00:00
|
|
|
name notin @["static_Call1024BalanceTooLow.json",
|
|
|
|
"Call1024BalanceTooLow.json", "ExtCodeCopyTests.json"])
|
2018-02-27 18:10:34 +00:00
|
|
|
|
|
|
|
macro jsonTest*(s: static[string], handler: untyped): untyped =
|
2018-07-06 15:08:31 +00:00
|
|
|
let
|
|
|
|
testStatusIMPL = ident("testStatusIMPL")
|
|
|
|
# workaround for strformat in quote do: https://github.com/nim-lang/Nim/issues/8220
|
|
|
|
symbol = newIdentNode"symbol"
|
|
|
|
final = newIdentNode"final"
|
|
|
|
name = newIdentNode"name"
|
|
|
|
formatted = newStrLitNode"{symbol[final]} {name:<64}{$final}{'\n'}"
|
2018-02-13 17:18:08 +00:00
|
|
|
result = quote:
|
2018-02-27 18:10:34 +00:00
|
|
|
var z = 0
|
|
|
|
var filenames: seq[(string, string, string)] = @[]
|
|
|
|
var status = initOrderedTable[string, OrderedTable[string, Status]]()
|
|
|
|
for filename in walkDirRec("tests" / "fixtures" / `s`):
|
|
|
|
var (folder, name) = filename.splitPath()
|
|
|
|
let last = folder.splitPath().tail
|
|
|
|
if not status.hasKey(last):
|
|
|
|
status[last] = initOrderedTable[string, Status]()
|
|
|
|
status[last][name] = Status.Skip
|
|
|
|
if last.validTest(name):
|
|
|
|
filenames.add((filename, last, name))
|
|
|
|
for child in filenames:
|
|
|
|
let (filename, folder, name) = child
|
|
|
|
test filename:
|
|
|
|
echo folder, name
|
|
|
|
status[folder][name] = Status.FAIL
|
|
|
|
`handler`(parseJSON(readFile(filename)), `testStatusIMPL`)
|
|
|
|
if `testStatusIMPL` == OK:
|
|
|
|
status[folder][name] = Status.OK
|
|
|
|
z += 1
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-05-11 16:16:35 +00:00
|
|
|
status.sort do (a: (string, OrderedTable[string, Status]),
|
|
|
|
b: (string, OrderedTable[string, Status])) -> int: cmp(a[0], b[0])
|
2018-02-27 18:10:34 +00:00
|
|
|
|
2018-07-06 15:08:31 +00:00
|
|
|
let `symbol`: array[Status, string] = ["+", "-", " "]
|
2018-02-27 18:10:34 +00:00
|
|
|
var raw = ""
|
|
|
|
raw.add(`s` & "\n")
|
|
|
|
raw.add("===\n")
|
|
|
|
for folder, statuses in status:
|
|
|
|
raw.add("## " & folder & "\n")
|
|
|
|
raw.add("```diff\n")
|
|
|
|
var sortedStatuses = statuses
|
|
|
|
sortedStatuses.sort do (a: (string, Status), b: (string, Status)) -> int:
|
|
|
|
cmp(a[0], b[0])
|
|
|
|
var okCount = 0
|
|
|
|
var failCount = 0
|
|
|
|
var skipCount = 0
|
2018-07-06 15:08:31 +00:00
|
|
|
for `name`, `final` in sortedStatuses:
|
|
|
|
raw.add(&`formatted`)
|
|
|
|
case `final`:
|
2018-02-27 18:10:34 +00:00
|
|
|
of Status.OK: okCount += 1
|
|
|
|
of Status.Fail: failCount += 1
|
|
|
|
of Status.Skip: skipCount += 1
|
|
|
|
raw.add("```\n")
|
|
|
|
let sum = okCount + failCount + skipCount
|
|
|
|
raw.add("OK: " & $okCount & "/" & $sum & " Fail: " & $failCount & "/" & $sum & " Skip: " & $skipCount & "/" & $sum & "\n")
|
|
|
|
writeFile(`s` & ".md", raw)
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-09-07 19:44:17 +00:00
|
|
|
proc ethAddressFromHex*(s: string): EthAddress = hexToByteArray(s, result)
|
2018-05-30 16:11:15 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc setupStateDB*(wantedState: JsonNode, stateDB: var AccountStateDB) =
|
|
|
|
for ac, accountData in wantedState:
|
|
|
|
let account = ethAddressFromHex(ac)
|
2018-02-13 17:18:08 +00:00
|
|
|
for slot, value in accountData{"storage"}:
|
2018-09-04 12:30:15 +00:00
|
|
|
stateDB.setStorage(account, fromHex(UInt256, slot), fromHex(UInt256, value.getStr))
|
|
|
|
|
|
|
|
let nonce = accountData{"nonce"}.getStr.parseHexInt.AccountNonce
|
|
|
|
|
|
|
|
# Keep workaround local until another case needing it is found,
|
|
|
|
# to ensure failure modes obvious.
|
|
|
|
let rawCode = accountData{"code"}.getStr
|
|
|
|
let code = hexToSeqByte(if rawCode == "": "0x" else: rawCode).toRange
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-07-17 08:57:14 +00:00
|
|
|
let balance = UInt256.fromHex accountData{"balance"}.getStr
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-02-13 17:18:08 +00:00
|
|
|
stateDB.setNonce(account, nonce)
|
|
|
|
stateDB.setCode(account, code)
|
|
|
|
stateDB.setBalance(account, balance)
|
2018-02-14 16:38:01 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc verifyStateDB*(wantedState: JsonNode, stateDB: AccountStateDB) =
|
|
|
|
for ac, accountData in wantedState:
|
|
|
|
let account = ethAddressFromHex(ac)
|
|
|
|
for slot, value in accountData{"storage"}:
|
|
|
|
let
|
2018-07-17 08:57:14 +00:00
|
|
|
slotId = UInt256.fromHex slot
|
2018-07-05 12:41:01 +00:00
|
|
|
wantedValue = UInt256.fromHex value.getStr
|
|
|
|
|
|
|
|
let (actualValue, found) = stateDB.getStorage(account, slotId)
|
|
|
|
# echo "FOUND ", found
|
|
|
|
# echo "ACTUAL VALUE ", actualValue.toHex
|
2018-07-17 08:57:14 +00:00
|
|
|
doAssert found
|
2018-08-09 15:54:00 +00:00
|
|
|
doAssert actualValue == wantedValue, &"{actualValue.toHex} != {wantedValue.toHex}"
|
2018-07-05 12:41:01 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
wantedCode = hexToSeqByte(accountData{"code"}.getStr).toRange
|
2018-07-17 08:57:14 +00:00
|
|
|
wantedBalance = UInt256.fromHex accountData{"balance"}.getStr
|
2018-09-02 02:26:22 +00:00
|
|
|
wantedNonce = accountData{"nonce"}.getInt.AccountNonce
|
2018-07-05 12:41:01 +00:00
|
|
|
|
|
|
|
actualCode = stateDB.getCode(account)
|
|
|
|
actualBalance = stateDB.getBalance(account)
|
|
|
|
actualNonce = stateDB.getNonce(account)
|
|
|
|
|
2018-09-04 12:30:15 +00:00
|
|
|
# XXX: actualCode is sourced from wrong location currently, incompatible with
|
|
|
|
# state hash root. Can/should be fixed, but blocks further progress as-is.
|
|
|
|
# doAssert wantedCode == actualCode, &"{wantedCode} != {actualCode}"
|
2018-08-09 15:54:00 +00:00
|
|
|
doAssert wantedBalance == actualBalance, &"{wantedBalance.toHex} != {actualBalance.toHex}"
|
|
|
|
doAssert wantedNonce == actualNonce, &"{wantedNonce.toHex} != {actualNonce.toHex}"
|
2018-07-05 12:41:01 +00:00
|
|
|
|
2018-08-08 18:37:02 +00:00
|
|
|
proc getHexadecimalInt*(j: JsonNode): int64 =
|
|
|
|
# parseutils.parseHex works with int which will overflow in 32 bit
|
|
|
|
var data: StUInt[64]
|
|
|
|
data = fromHex(StUInt[64], j.getStr)
|
|
|
|
result = cast[int64](data)
|
2018-09-05 23:48:06 +00:00
|
|
|
|
|
|
|
proc getFixtureTransaction*(j: JsonNode): Transaction =
|
|
|
|
var transaction : Transaction
|
|
|
|
transaction.accountNonce = j["nonce"].getStr.parseHexInt.AccountNonce
|
|
|
|
transaction.gasPrice = j["gasPrice"].getStr.parseHexInt
|
|
|
|
transaction.gasLimit = j["gasLimit"][0].getStr.parseHexInt
|
|
|
|
transaction.to = j["to"].getStr.parseAddress
|
|
|
|
transaction.value = j["value"][0].getStr.parseHexInt.u256
|
|
|
|
|
|
|
|
# Another, slightly distinct, case of this "" as special-cased hex string
|
|
|
|
# One possibility's a string prefix func which adds only if 0x is missing
|
|
|
|
# which can be used across the various hex-string-parsing utility funcs.
|
|
|
|
let rawData = j["data"][0].getStr
|
|
|
|
transaction.payload = (if rawData == "": "0x" else: rawData).hexToSeqByte
|
|
|
|
|
|
|
|
return transaction
|
|
|
|
|
2018-09-07 19:44:17 +00:00
|
|
|
proc getFixtureTransactionSender*(j: JsonNode): EthAddress =
|
2018-09-05 23:48:06 +00:00
|
|
|
var secretKey = j["secretKey"].getStr
|
|
|
|
removePrefix(secretKey, "0x")
|
|
|
|
let privateKey = initPrivateKey(secretKey)
|
|
|
|
|
|
|
|
var pubKey: PublicKey
|
|
|
|
let transaction = j.getFixtureTransaction
|
|
|
|
if recoverSignatureKey(signMessage(privateKey, transaction.rlpEncode.toOpenArray),
|
2018-09-07 19:44:17 +00:00
|
|
|
transaction.hash.data,
|
|
|
|
pubKey) == EthKeysStatus.Success:
|
2018-09-05 23:48:06 +00:00
|
|
|
return pubKey.toCanonicalAddress()
|
|
|
|
else:
|
|
|
|
# XXX: appropriate failure mode; probably raise something
|
|
|
|
discard
|
2018-09-07 19:44:17 +00:00
|
|
|
|
|
|
|
proc getFixtureCode*(pre: JsonNode, targetAccount: EthAddress) : seq[byte] =
|
|
|
|
# XXX: Workaround for broken setCode/getCode. Remove when feasible.
|
|
|
|
for ac, preState in pre:
|
|
|
|
if ethAddressFromHex(ac) == targetAccount:
|
|
|
|
return preState["code"].getStr.hexToSeqByte
|
|
|
|
|
|
|
|
# Fail loudly if it falls off the end (by default)
|
|
|
|
|
|
|
|
proc getFixtureIntrinsicGas*(transaction: Transaction) : auto =
|
|
|
|
# Py-EVM has _get_homestead_intrinsic_gas and _get_frontier_intrinsic_gas
|
|
|
|
# Using former.
|
|
|
|
|
|
|
|
# TODO: refactor and pull from nimbus/vm/interpreter/gas_costs.nim
|
|
|
|
let
|
|
|
|
gasTransaction = 21_000
|
|
|
|
gasTXDataZero = 4
|
|
|
|
gasTXDataNonZero = 68
|
|
|
|
numZeroBytes = transaction.payload.count(0)
|
|
|
|
gasCosts = forkToSchedule(FkHomestead)
|
|
|
|
|
|
|
|
result = gasTransaction + gasTXDataZero * numZeroBytes + gasTXDataNonZero * (transaction.payload.len - numZeroBytes)
|