implement constantinople bitwise opcode test

This commit is contained in:
andri lim 2019-05-06 19:09:03 +07:00 committed by zah
parent 4a7f58c7dc
commit 480a3d2cab
2 changed files with 22 additions and 7 deletions

View File

@ -15,6 +15,14 @@ import
export opcode_values, byteutils export opcode_values, byteutils
{.experimental: "dynamicBindSym".} {.experimental: "dynamicBindSym".}
# 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))
)
type type
VMWord* = array[32, byte] VMWord* = array[32, byte]
Storage* = tuple[key, val: VMWord] Storage* = tuple[key, val: VMWord]
@ -31,6 +39,7 @@ type
gasUsed*: GasInt gasUsed*: GasInt
data*: seq[byte] data*: seq[byte]
output*: seq[byte] output*: seq[byte]
fork*: Fork
const const
idToOpcode = CacheTable"NimbusMacroAssembler" idToOpcode = CacheTable"NimbusMacroAssembler"
@ -159,6 +168,10 @@ proc parseCode(codes: NimNode): seq[byte] =
else: else:
error("unknown syntax: " & line.toStrLit.strVal, line) error("unknown syntax: " & line.toStrLit.strVal, line)
proc parseFork(fork: NimNode): Fork =
fork[0].expectKind({nnkIdent, nnkStrLit})
parseEnum[Fork](strip(fork[0].strVal))
proc generateVMProxy(boa: Assembler): NimNode = proc generateVMProxy(boa: Assembler): NimNode =
let let
vmProxy = genSym(nskProc, "vmProxy") vmProxy = genSym(nskProc, "vmProxy")
@ -212,7 +225,7 @@ proc initDatabase*(): (Uint256, BaseChainDB) =
result = (blockNumber, newBaseChainDB(memoryDB, false)) result = (blockNumber, newBaseChainDB(memoryDB, false))
proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data: seq[byte]): BaseComputation = proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data: seq[byte], fork: Fork): BaseComputation =
let let
parentNumber = blockNumber - 1 parentNumber = blockNumber - 1
parent = chainDB.getBlockHeader(parentNumber) parent = chainDB.getBlockHeader(parentNumber)
@ -227,10 +240,10 @@ proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data:
tx.payload = payload tx.payload = payload
tx.gasLimit = 500000000 tx.gasLimit = 500000000
initComputation(vmState, tx, sender, data, none(Fork)) initComputation(vmState, tx, sender, data, some(fork))
proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool = proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool =
var computation = initComputation(blockNumber, chainDB, boa.code, boa.data) var computation = initComputation(blockNumber, chainDB, boa.code, boa.data, boa.fork)
let gas = computation.gasMeter.gasRemaining let gas = computation.gasMeter.gasRemaining
execComputation(computation) execComputation(computation)
@ -322,7 +335,7 @@ proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool =
result = true result = true
macro assembler*(list: untyped): untyped = macro assembler*(list: untyped): untyped =
var boa = Assembler(success: true) var boa = Assembler(success: true, fork: FkFrontier)
list.expectKind nnkStmtList list.expectKind nnkStmtList
for callSection in list: for callSection in list:
callSection.expectKind(nnkCall) callSection.expectKind(nnkCall)
@ -341,5 +354,6 @@ macro assembler*(list: untyped): untyped =
of "success": boa.success = parseSuccess(body) of "success": boa.success = parseSuccess(body)
of "data": boa.data = parseData(body) of "data": boa.data = parseData(body)
of "output": boa.output = parseData(body) of "output": boa.output = parseData(body)
of "fork": boa.fork = parseFork(body)
else: error("unknown section '" & label & "'", callSection[0]) else: error("unknown section '" & label & "'", callSection[0])
result = boa.generateVMProxy() result = boa.generateVMProxy()

View File

@ -106,7 +106,7 @@ suite "Bitwise Opcodes":
BYTE BYTE
success: false success: false
stack: "0xAABBCCDDEE3A" stack: "0xAABBCCDDEE3A"
#[
assembler: # SHL OP assembler: # SHL OP
title: "SHL_1" title: "SHL_1"
code: code:
@ -120,7 +120,8 @@ suite "Bitwise Opcodes":
title: "SHL_2" title: "SHL_2"
code: code:
PUSH32 "0x0000000000000000000000000000000000000000000000000000000000000001" PUSH32 "0x0000000000000000000000000000000000000000000000000000000000000001"
PUSH1 "0x01" SHL PUSH1 "0x01"
SHL
fork: constantinople fork: constantinople
stack: "0x0000000000000000000000000000000000000000000000000000000000000002" stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
@ -447,7 +448,7 @@ suite "Bitwise Opcodes":
SAR SAR
fork: constantinople fork: constantinople
stack: "0x0000000000000000000000000000000000000000000000000000000000000000" stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
]#
assembler: # ISZERO OP assembler: # ISZERO OP
title: "ISZERO_1" title: "ISZERO_1"
code: code: