2019-01-30 12:29:04 +00:00
|
|
|
import
|
2019-05-30 16:42:55 +00:00
|
|
|
macrocache, strutils, unittest2,
|
2020-07-21 06:15:06 +00:00
|
|
|
stew/byteutils, chronicles, eth/common,
|
2019-01-31 16:25:21 +00:00
|
|
|
../nimbus/vm/interpreter/opcode_values,
|
2020-04-12 10:33:17 +00:00
|
|
|
stew/shims/macros, ../nimbus/config
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
import
|
2019-02-05 19:15:50 +00:00
|
|
|
options, json, os, eth/trie/[db, hexary],
|
2019-11-13 14:49:39 +00:00
|
|
|
../nimbus/[vm_state, vm_types, transaction, utils],
|
2020-06-01 06:45:32 +00:00
|
|
|
../nimbus/db/[db_chain, accounts_cache],
|
2019-01-30 12:29:04 +00:00
|
|
|
../nimbus/vm_state_transactions,
|
2019-11-13 14:49:39 +00:00
|
|
|
../nimbus/vm/interpreter/vm_forks,
|
2019-01-30 12:29:04 +00:00
|
|
|
../nimbus/vm/[message, computation, memory]
|
|
|
|
|
|
|
|
export opcode_values, byteutils
|
|
|
|
{.experimental: "dynamicBindSym".}
|
|
|
|
|
2019-05-06 12:09:03 +00:00
|
|
|
# backported from Nim 0.19.9
|
|
|
|
# remove this when we use newer Nim
|
|
|
|
proc newLitFixed*(arg: enum): NimNode {.compileTime.} =
|
|
|
|
result = newCall(
|
|
|
|
arg.type.getTypeInst[1],
|
|
|
|
newLit(int(arg))
|
|
|
|
)
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
type
|
|
|
|
VMWord* = array[32, byte]
|
|
|
|
Storage* = tuple[key, val: VMWord]
|
|
|
|
|
|
|
|
Assembler* = object
|
|
|
|
title*: string
|
|
|
|
stack*: seq[VMWord]
|
|
|
|
memory*: seq[VMWord]
|
|
|
|
storage*: seq[Storage]
|
|
|
|
code*: seq[byte]
|
|
|
|
logs*: seq[Log]
|
|
|
|
success*: bool
|
|
|
|
gasLimit*: GasInt
|
|
|
|
gasUsed*: GasInt
|
|
|
|
data*: seq[byte]
|
|
|
|
output*: seq[byte]
|
2019-05-06 12:09:03 +00:00
|
|
|
fork*: Fork
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2019-01-31 06:35:07 +00:00
|
|
|
const
|
|
|
|
idToOpcode = CacheTable"NimbusMacroAssembler"
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
static:
|
2019-01-31 06:35:07 +00:00
|
|
|
for n in Op:
|
|
|
|
idToOpcode[$n] = newLit(ord(n))
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
proc validateVMWord(val: string, n: NimNode): VMWord =
|
2019-01-31 06:35:07 +00:00
|
|
|
if val.len <= 2 or val.len > 66: error("invalid hex string", n)
|
|
|
|
if not (val[0] == '0' and val[1] == 'x'): error("invalid hex string", n)
|
2019-01-30 12:29:04 +00:00
|
|
|
let zerosLen = 64 - (val.len - 2)
|
|
|
|
let value = repeat('0', zerosLen) & val.substr(2)
|
|
|
|
hexToByteArray(value, result)
|
|
|
|
|
|
|
|
proc validateVMWord(val: NimNode): VMWord =
|
|
|
|
val.expectKind(nnkStrLit)
|
|
|
|
validateVMWord(val.strVal, val)
|
|
|
|
|
|
|
|
proc parseVMWords(list: NimNode): seq[VMWord] =
|
|
|
|
result = @[]
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
for val in list:
|
|
|
|
result.add validateVMWord(val)
|
|
|
|
|
|
|
|
proc validateStorage(val: NimNode): Storage =
|
|
|
|
val.expectKind(nnkCall)
|
|
|
|
val[0].expectKind(nnkStrLit)
|
|
|
|
val[1].expectKind(nnkStmtList)
|
2019-03-13 21:36:54 +00:00
|
|
|
doAssert(val[1].len == 1)
|
2019-01-30 12:29:04 +00:00
|
|
|
val[1][0].expectKind(nnkStrLit)
|
|
|
|
result = (validateVMWord(val[0]), validateVMWord(val[1][0]))
|
|
|
|
|
|
|
|
proc parseStorage(list: NimNode): seq[Storage] =
|
|
|
|
result = @[]
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
for val in list:
|
|
|
|
result.add validateStorage(val)
|
|
|
|
|
|
|
|
proc parseSuccess(list: NimNode): bool =
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
list[0].expectKind(nnkIdent)
|
|
|
|
$list[0] == "true"
|
|
|
|
|
|
|
|
proc parseData(list: NimNode): seq[byte] =
|
|
|
|
result = @[]
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
for n in list:
|
|
|
|
n.expectKind(nnkStrLit)
|
|
|
|
result.add hexToSeqByte(n.strVal)
|
|
|
|
|
2019-01-31 06:35:07 +00:00
|
|
|
proc parseLog(node: NimNode): Log =
|
|
|
|
node.expectKind(nnkPar)
|
|
|
|
for item in node:
|
|
|
|
item.expectKind(nnkExprColonExpr)
|
|
|
|
let label = item[0].strVal
|
|
|
|
let body = item[1]
|
|
|
|
case label.normalize
|
|
|
|
of "address":
|
|
|
|
body.expectKind(nnkStrLit)
|
|
|
|
let value = body.strVal
|
|
|
|
if value.len < 20:
|
|
|
|
error("bad address format", body)
|
|
|
|
hexToByteArray(value, result.address)
|
|
|
|
of "topics":
|
|
|
|
body.expectKind(nnkBracket)
|
|
|
|
for x in body:
|
|
|
|
result.topics.add validateVMWord(x.strVal, x)
|
|
|
|
of "data":
|
|
|
|
result.data = hexToSeqByte(body.strVal)
|
|
|
|
else:error("unknown log section '" & label & "'", item[0])
|
|
|
|
|
|
|
|
proc parseLogs(list: NimNode): seq[Log] =
|
|
|
|
result = @[]
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
for n in list:
|
|
|
|
result.add parseLog(n)
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
proc validateOpcode(sym: NimNode) =
|
|
|
|
let typ = getTypeInst(sym)
|
|
|
|
typ.expectKind(nnkSym)
|
|
|
|
if $typ != "Op":
|
|
|
|
error("unknown opcode '" & $sym & "'", sym)
|
|
|
|
|
2019-01-31 06:35:07 +00:00
|
|
|
proc addOpCode(code: var seq[byte], node, params: NimNode) =
|
|
|
|
node.expectKind nnkSym
|
|
|
|
let opcode = Op(idToOpcode[node.strVal].intVal)
|
2019-01-30 12:29:04 +00:00
|
|
|
case opcode
|
|
|
|
of Push1..Push32:
|
|
|
|
if params.len != 1:
|
|
|
|
error("expect 1 param, but got " & $params.len, node)
|
|
|
|
let paramWidth = (opcode.ord - 95) * 2
|
2019-01-31 06:35:07 +00:00
|
|
|
params[0].expectKind nnkStrLit
|
|
|
|
var val = params[0].strVal
|
2019-01-30 12:29:04 +00:00
|
|
|
if val[0] == '0' and val[1] == 'x':
|
|
|
|
val = val.substr(2)
|
|
|
|
if val.len != paramWidth:
|
|
|
|
error("expected param with " & $paramWidth & " hex digits, got " & $val.len, node)
|
2019-01-31 06:35:07 +00:00
|
|
|
code.add byte(opcode)
|
|
|
|
code.add hexToSeqByte(val)
|
2019-01-30 12:29:04 +00:00
|
|
|
else:
|
|
|
|
error("invalid hex format", node)
|
|
|
|
else:
|
|
|
|
if params.len > 0:
|
2019-01-31 06:35:07 +00:00
|
|
|
error("there should be no param for this instruction", node)
|
|
|
|
code.add byte(opcode)
|
|
|
|
|
|
|
|
proc parseCode(codes: NimNode): seq[byte] =
|
|
|
|
let emptyNode = newEmptyNode()
|
|
|
|
codes.expectKind nnkStmtList
|
|
|
|
for pc, line in codes:
|
|
|
|
line.expectKind({nnkCommand, nnkIdent, nnkStrLit})
|
|
|
|
if line.kind == nnkStrLit:
|
|
|
|
result.add hexToSeqByte(line.strVal)
|
|
|
|
elif line.kind == nnkIdent:
|
|
|
|
let sym = bindSym(line)
|
|
|
|
validateOpcode(sym)
|
|
|
|
result.addOpCode(sym, emptyNode)
|
|
|
|
elif line.kind == nnkCommand:
|
|
|
|
let sym = bindSym(line[0])
|
|
|
|
validateOpcode(sym)
|
|
|
|
var params = newNimNode(nnkBracket)
|
|
|
|
for i in 1 ..< line.len:
|
|
|
|
params.add line[i]
|
|
|
|
result.addOpCode(sym, params)
|
|
|
|
else:
|
|
|
|
error("unknown syntax: " & line.toStrLit.strVal, line)
|
|
|
|
|
2019-05-06 12:09:03 +00:00
|
|
|
proc parseFork(fork: NimNode): Fork =
|
|
|
|
fork[0].expectKind({nnkIdent, nnkStrLit})
|
|
|
|
parseEnum[Fork](strip(fork[0].strVal))
|
|
|
|
|
2020-11-25 11:23:02 +00:00
|
|
|
proc parseGasUsed(gas: NimNode): GasInt =
|
|
|
|
gas[0].expectKind(nnkIntLit)
|
|
|
|
result = gas[0].intVal
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
proc generateVMProxy(boa: Assembler): NimNode =
|
|
|
|
let
|
2019-01-31 16:25:21 +00:00
|
|
|
vmProxy = genSym(nskProc, "vmProxy")
|
2019-01-30 12:29:04 +00:00
|
|
|
blockNumber = ident("blockNumber")
|
|
|
|
chainDB = ident("chainDB")
|
2019-01-31 16:25:21 +00:00
|
|
|
title = boa.title
|
|
|
|
body = newLitFixed(boa)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
result = quote do:
|
|
|
|
test `title`:
|
|
|
|
proc `vmProxy`(): bool =
|
2019-01-31 16:25:21 +00:00
|
|
|
let boa = `body`
|
|
|
|
runVM(`blockNumber`, `chainDB`, boa)
|
2019-05-30 16:42:55 +00:00
|
|
|
{.gcsafe.}:
|
|
|
|
check `vmProxy`()
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
when defined(macro_assembler_debug):
|
|
|
|
echo result.toStrLit.strVal
|
|
|
|
|
|
|
|
const
|
2019-01-30 15:13:59 +00:00
|
|
|
blockFile = "tests" / "fixtures" / "PersistBlockTests" / "block47205.json"
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2020-01-10 01:26:17 +00:00
|
|
|
proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress, data: seq[byte], forkOverride=none(Fork)) : Computation =
|
2019-03-13 21:36:54 +00:00
|
|
|
doAssert tx.isContractCreation
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
let fork =
|
|
|
|
if forkOverride.isSome:
|
|
|
|
forkOverride.get
|
|
|
|
else:
|
2020-04-12 10:33:17 +00:00
|
|
|
vmState.chainDB.config.toFork(vmState.blockNumber)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
let gasUsed = 0 #tx.payload.intrinsicGas.GasInt + gasFees[fork][GasTXCreate]
|
|
|
|
|
2020-01-20 17:59:15 +00:00
|
|
|
vmState.setupTxContext(
|
2020-01-16 06:36:58 +00:00
|
|
|
origin = sender,
|
2020-01-16 07:01:59 +00:00
|
|
|
gasPrice = tx.gasPrice,
|
|
|
|
forkOverride = some(fork)
|
2020-01-16 06:36:58 +00:00
|
|
|
)
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
let contractAddress = generateAddress(sender, tx.accountNonce)
|
2020-01-07 16:26:20 +00:00
|
|
|
let msg = Message(
|
2020-01-08 03:12:06 +00:00
|
|
|
kind: evmcCall,
|
2020-01-07 16:26:20 +00:00
|
|
|
depth: 0,
|
|
|
|
gas: tx.gasLimit - gasUsed,
|
|
|
|
sender: sender,
|
|
|
|
contractAddress: contractAddress,
|
2020-01-20 14:02:06 +00:00
|
|
|
codeAddress: contractAddress,
|
2020-01-07 16:26:20 +00:00
|
|
|
value: tx.value,
|
2020-01-20 14:02:06 +00:00
|
|
|
data: data
|
2020-01-07 16:26:20 +00:00
|
|
|
)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2020-01-20 14:02:06 +00:00
|
|
|
vmState.mutateStateDb:
|
2020-04-20 18:12:44 +00:00
|
|
|
db.setCode(contractAddress, tx.payload)
|
2020-04-12 10:33:17 +00:00
|
|
|
|
2020-01-16 07:01:59 +00:00
|
|
|
newComputation(vmState, msg)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
proc initDatabase*(): (Uint256, BaseChainDB) =
|
|
|
|
let
|
2019-01-30 15:13:59 +00:00
|
|
|
node = json.parseFile(blockFile)
|
2019-01-30 12:29:04 +00:00
|
|
|
blockNumber = UInt256.fromHex(node["blockNumber"].getStr())
|
|
|
|
memoryDB = newMemoryDB()
|
|
|
|
state = node["state"]
|
|
|
|
|
|
|
|
for k, v in state:
|
|
|
|
let key = hexToSeqByte(k)
|
|
|
|
let value = hexToSeqByte(v.getStr())
|
|
|
|
memoryDB.put(key, value)
|
|
|
|
|
|
|
|
result = (blockNumber, newBaseChainDB(memoryDB, false))
|
|
|
|
|
2020-01-20 14:02:06 +00:00
|
|
|
proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, code, data: seq[byte], fork: Fork): Computation =
|
2019-01-30 12:29:04 +00:00
|
|
|
let
|
|
|
|
parentNumber = blockNumber - 1
|
|
|
|
parent = chainDB.getBlockHeader(parentNumber)
|
|
|
|
header = chainDB.getBlockHeader(blockNumber)
|
|
|
|
headerHash = header.blockHash
|
|
|
|
body = chainDB.getBlockBody(headerHash)
|
2019-02-14 15:20:41 +00:00
|
|
|
vmState = newBaseVMState(parent.stateRoot, header, chainDB)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
tx = body.transactions[0]
|
2019-02-26 07:04:12 +00:00
|
|
|
sender = transaction.getSender(tx)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2020-01-20 14:02:06 +00:00
|
|
|
tx.payload = code
|
2019-01-30 12:29:04 +00:00
|
|
|
tx.gasLimit = 500000000
|
2019-05-06 12:09:03 +00:00
|
|
|
initComputation(vmState, tx, sender, data, some(fork))
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool =
|
2019-05-06 12:09:03 +00:00
|
|
|
var computation = initComputation(blockNumber, chainDB, boa.code, boa.data, boa.fork)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2020-11-25 11:23:02 +00:00
|
|
|
let gas = computation.gasMeter.gasRemaining
|
2019-04-24 06:58:56 +00:00
|
|
|
execComputation(computation)
|
2020-11-25 11:23:02 +00:00
|
|
|
let gasUsed = gas - computation.gasMeter.gasRemaining
|
2019-01-30 12:29:04 +00:00
|
|
|
|
2019-04-24 06:58:56 +00:00
|
|
|
if computation.isSuccess:
|
2019-01-30 12:29:04 +00:00
|
|
|
if boa.success == false:
|
|
|
|
error "different success value", expected=boa.success, actual=true
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
if boa.success == true:
|
|
|
|
error "different success value", expected=boa.success, actual=false
|
|
|
|
return false
|
|
|
|
|
2020-11-25 11:23:02 +00:00
|
|
|
if boa.gasUsed != -1:
|
|
|
|
if boa.gasUsed != gasUsed:
|
|
|
|
error "different gasUsed", expected=boa.gasUsed, actual=gasUsed
|
|
|
|
return false
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
if boa.stack.len != computation.stack.values.len:
|
|
|
|
error "different stack len", expected=boa.stack.len, actual=computation.stack.values.len
|
|
|
|
return false
|
|
|
|
|
|
|
|
for i, v in computation.stack.values:
|
|
|
|
let actual = v.dumpHex()
|
|
|
|
let val = boa.stack[i].toHex()
|
|
|
|
if actual != val:
|
|
|
|
error "different stack value", idx=i, expected=val, actual=actual
|
|
|
|
return false
|
|
|
|
|
|
|
|
const chunkLen = 32
|
|
|
|
let numChunks = computation.memory.len div chunkLen
|
|
|
|
|
|
|
|
if numChunks != boa.memory.len:
|
|
|
|
error "different memory len", expected=boa.memory.len, actual=numChunks
|
|
|
|
return false
|
|
|
|
|
|
|
|
for i in 0 ..< numChunks:
|
|
|
|
let actual = computation.memory.bytes.toOpenArray(i * chunkLen, (i + 1) * chunkLen - 1).toHex()
|
|
|
|
let mem = boa.memory[i].toHex()
|
|
|
|
if mem != actual:
|
|
|
|
error "different memory value", idx=i, expected=mem, actual=actual
|
|
|
|
return false
|
|
|
|
|
2020-06-01 06:58:14 +00:00
|
|
|
var stateDB = computation.vmState.accountDb
|
|
|
|
stateDB.persist()
|
|
|
|
|
2019-01-30 12:29:04 +00:00
|
|
|
var
|
2020-06-01 06:45:32 +00:00
|
|
|
storageRoot = stateDB.getStorageRoot(computation.msg.contractAddress)
|
|
|
|
trie = initSecureHexaryTrie(chainDB.db, storageRoot)
|
2019-01-30 12:29:04 +00:00
|
|
|
|
|
|
|
for kv in boa.storage:
|
|
|
|
let key = kv[0].toHex()
|
|
|
|
let val = kv[1].toHex()
|
2020-04-20 18:12:44 +00:00
|
|
|
let keyBytes = (@(kv[0]))
|
|
|
|
let actual = trie.get(keyBytes).toHex()
|
2019-01-30 12:29:04 +00:00
|
|
|
let zerosLen = 64 - (actual.len)
|
|
|
|
let value = repeat('0', zerosLen) & actual
|
|
|
|
if val != value:
|
|
|
|
error "storage has different value", key=key, expected=val, actual=value
|
|
|
|
return false
|
|
|
|
|
|
|
|
let logs = computation.vmState.logEntries
|
|
|
|
if logs.len != boa.logs.len:
|
|
|
|
error "different logs len", expected=boa.logs.len, actual=logs.len
|
|
|
|
return false
|
|
|
|
|
|
|
|
for i, log in boa.logs:
|
|
|
|
let eAddr = log.address.toHex()
|
|
|
|
let aAddr = logs[i].address.toHex()
|
|
|
|
if eAddr != aAddr:
|
|
|
|
error "different address", expected=eAddr, actual=aAddr, idx=i
|
|
|
|
return false
|
|
|
|
let eData = log.data.toHex()
|
|
|
|
let aData = logs[i].data.toHex()
|
|
|
|
if eData != aData:
|
|
|
|
error "different data", expected=eData, actual=aData, idx=i
|
|
|
|
return false
|
|
|
|
if log.topics.len != logs[i].topics.len:
|
|
|
|
error "different topics len", expected=log.topics.len, actual=logs[i].topics.len, idx=i
|
|
|
|
return false
|
|
|
|
for x, t in log.topics:
|
|
|
|
let eTopic = t.toHex()
|
|
|
|
let aTopic = logs[i].topics[x].toHex()
|
|
|
|
if eTopic != aTopic:
|
|
|
|
error "different topic in log entry", expected=eTopic, actual=aTopic, logIdx=i, topicIdx=x
|
|
|
|
return false
|
|
|
|
|
|
|
|
if boa.output.len > 0:
|
|
|
|
let actual = computation.output.toHex()
|
|
|
|
let expected = boa.output.toHex()
|
|
|
|
if expected != actual:
|
|
|
|
error "different output detected", expected=expected, actual=actual
|
|
|
|
return false
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
macro assembler*(list: untyped): untyped =
|
2020-11-25 11:23:02 +00:00
|
|
|
var boa = Assembler(success: true, fork: FkFrontier, gasUsed: -1)
|
2019-01-30 12:29:04 +00:00
|
|
|
list.expectKind nnkStmtList
|
|
|
|
for callSection in list:
|
|
|
|
callSection.expectKind(nnkCall)
|
|
|
|
let label = callSection[0].strVal
|
|
|
|
let body = callSection[1]
|
|
|
|
case label.normalize
|
|
|
|
of "title":
|
|
|
|
let title = body[0]
|
|
|
|
title.expectKind(nnkStrLit)
|
|
|
|
boa.title = title.strVal
|
2019-01-31 06:35:07 +00:00
|
|
|
of "code" : boa.code = parseCode(body)
|
2019-01-30 12:29:04 +00:00
|
|
|
of "memory": boa.memory = parseVMWords(body)
|
|
|
|
of "stack" : boa.stack = parseVMWords(body)
|
|
|
|
of "storage": boa.storage = parseStorage(body)
|
|
|
|
of "logs": boa.logs = parseLogs(body)
|
|
|
|
of "success": boa.success = parseSuccess(body)
|
|
|
|
of "data": boa.data = parseData(body)
|
|
|
|
of "output": boa.output = parseData(body)
|
2019-05-06 12:09:03 +00:00
|
|
|
of "fork": boa.fork = parseFork(body)
|
2020-11-25 11:23:02 +00:00
|
|
|
of "gasused": boa.gasUsed = parseGasUsed(body)
|
2019-01-30 12:29:04 +00:00
|
|
|
else: error("unknown section '" & label & "'", callSection[0])
|
2019-01-31 06:35:07 +00:00
|
|
|
result = boa.generateVMProxy()
|
2020-07-24 12:54:27 +00:00
|
|
|
|
|
|
|
macro evmByteCode*(list: untyped): untyped =
|
|
|
|
list.expectKind nnkStmtList
|
|
|
|
var code = parseCode(list)
|
|
|
|
result = newLitFixed(code)
|