From 284a026e51643a23bbe53a2482cb14343581145d Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Fri, 6 Apr 2018 16:25:01 +0200 Subject: [PATCH] Add opcode tests (#4) --- nimbus.nimble | 2 +- src/{block.nim => block_obj.nim} | 0 src/logic/storage.nim | 7 +++++-- src/vm/base.nim | 4 ++-- src/vm/forks/frontier/frontier_block.nim | 4 +++- src/vm/forks/frontier/frontier_validation.nim | 4 ++-- src/vm/forks/frontier/frontier_vm_state.nim | 5 +++-- src/vm/forks/frontier/vm.nim | 9 ++++++--- src/vm_state_transactions.nim | 4 ++-- tests/all_tests.nim | 4 +++- tests/test_helpers.nim | 8 ++++++-- tests/test_opcode.nim | 18 ++++++++++++------ 12 files changed, 45 insertions(+), 24 deletions(-) rename src/{block.nim => block_obj.nim} (100%) diff --git a/nimbus.nimble b/nimbus.nimble index e72f3e09d..dd3acf251 100644 --- a/nimbus.nimble +++ b/nimbus.nimble @@ -8,7 +8,7 @@ license = "Apache License 2.0" skipDirs = @["tests"] requires "nim >= 0.18.1", - "https://github.com/status-im/nim-keccak-tiny.git >= 0.1.0", + "https://github.com/status-im/nim-keccak-tiny.git >= 0.2.0", "https://github.com/alehander42/nim-rlp#fix-ordinal", #TODO switching to the Status repo throws: "Error: undeclared identifier: 'Range'" "https://github.com/status-im/nim-ttmath#master" diff --git a/src/block.nim b/src/block_obj.nim similarity index 100% rename from src/block.nim rename to src/block_obj.nim diff --git a/src/logic/storage.nim b/src/logic/storage.nim index d6982124c..97f72b903 100644 --- a/src/logic/storage.nim +++ b/src/logic/storage.nim @@ -1,5 +1,8 @@ import - ../constants, ../types, ../errors, ../computation, ../vm_state, .. / db / [db_chain, state_db], .. / vm / [stack, gas_meter, message], strformat, ttmath, utils / header + ../constants, ../types, ../errors, ../computation, ../vm_state, + ../utils/header, + ../db/[db_chain, state_db], ../vm/[stack, gas_meter, message], + strformat, ttmath {.this: computation.} {.experimental.} @@ -23,7 +26,7 @@ proc sstore*(computation) = let gasCost = if isCurrentlyEmpty and not isGoingToBeEmpty: GAS_SSET else: GAS_SRESET computation.gasMeter.consumeGas(gasCost, &"SSTORE: {computation.msg.storageAddress}[slot] -> {value} ({currentValue})") - + if gasRefund > 0: computation.gasMeter.refundGas(gasRefund) computation.vmState.db(readOnly=false): diff --git a/src/vm/base.nim b/src/vm/base.nim index 0864edc1b..7c05b4051 100644 --- a/src/vm/base.nim +++ b/src/vm/base.nim @@ -1,5 +1,5 @@ import - ../logging, ../constants, ../errors, ../transaction, ../types, ../computation, "../block", ../vm_state, ../vm_state_transactions, ../db/db_chain, ../utils/header + ../logging, ../constants, ../errors, ../transaction, ../types, ../computation, ../block_obj, ../vm_state, ../vm_state_transactions, ../db/db_chain, ../utils/header type VM* = ref object of RootObj @@ -7,7 +7,7 @@ type # such as the Frontier or Homestead network. Defining an Chain defining # individual VM classes for each fork of the protocol rules within that # network - + chainDB*: BaseChainDB isStateless*: bool state*: BaseVMState diff --git a/src/vm/forks/frontier/frontier_block.nim b/src/vm/forks/frontier/frontier_block.nim index a9d3c72ca..8528f52bb 100644 --- a/src/vm/forks/frontier/frontier_block.nim +++ b/src/vm/forks/frontier/frontier_block.nim @@ -1,5 +1,7 @@ import - logging, constants, errors, transaction, "block", utils/header + ../../../logging, ../../../constants, ../../../errors, ../../../transaction, + ../../../block_obj, + ../../../utils/header type FrontierBlock* = object of Block diff --git a/src/vm/forks/frontier/frontier_validation.nim b/src/vm/forks/frontier/frontier_validation.nim index 6f950e132..4e0b9dad3 100644 --- a/src/vm/forks/frontier/frontier_validation.nim +++ b/src/vm/forks/frontier/frontier_validation.nim @@ -1,6 +1,6 @@ import - strformat, - constants, errors, ttmath, vm_state, transaction, utils/header + strformat, ttmath, + ../../../constants, ../../../errors, ../../../vm_state, ../../../transaction, ../../../utils/header proc validateFrontierTransaction*(vmState: BaseVmState, transaction: BaseTransaction) = let gasCost = transaction.gas * transaction.gasPrice diff --git a/src/vm/forks/frontier/frontier_vm_state.nim b/src/vm/forks/frontier/frontier_vm_state.nim index 2612cef17..b0e863dba 100644 --- a/src/vm/forks/frontier/frontier_vm_state.nim +++ b/src/vm/forks/frontier/frontier_vm_state.nim @@ -1,9 +1,10 @@ import - logging, constants, errors, vm_state, utils/header, db/db_chain + ../../../logging, ../../../constants, ../../../errors, ../../../vm_state, + ../../../utils/header, ../../../db/db_chain type FrontierVMState* = ref object of BaseVMState - # receipts*: + # receipts*: # computationClass*: Any # accessLogs*: AccessLogs diff --git a/src/vm/forks/frontier/vm.nim b/src/vm/forks/frontier/vm.nim index e7f9c81db..6a711f381 100644 --- a/src/vm/forks/frontier/vm.nim +++ b/src/vm/forks/frontier/vm.nim @@ -1,9 +1,12 @@ import - logging, constants, errors, ttmath, "block", vm / [base, stack], db / db_chain, utils / header, - frontier_block, frontier_vm_state, frontier_validation + ../../../logging, ../../../constants, ../../../errors, + ttmath, + ../../../block_obj, + ../../../vm/[base, stack], ../../../db/db_chain, ../../../utils/header, + ./frontier_block, ./frontier_vm_state, ./frontier_validation -type +type FrontierVM* = ref object of VM method name*(vm: FrontierVM): string = diff --git a/src/vm_state_transactions.nim b/src/vm_state_transactions.nim index e9be8ecc0..2e85632fc 100644 --- a/src/vm_state_transactions.nim +++ b/src/vm_state_transactions.nim @@ -1,6 +1,6 @@ import strformat, tables, - logging, constants, errors, computation, transaction, types, vm_state, "block", db / db_chain, utils / [state, header] + logging, constants, errors, computation, transaction, types, vm_state, block_obj, db / db_chain, utils / [state, header] method executeTransaction(vmState: var BaseVMState, transaction: BaseTransaction): (BaseComputation, Header) = # Execute the transaction in the vm @@ -45,7 +45,7 @@ method applyTransaction*( # transaction: the transaction need to be applied # b: the block which the transaction applies on # isStateless: if isStateless, call vmState.addTransaction to set block - + if isStateless: var ourBlock = b # deepcopy vmState.blockHeader = b.header diff --git a/tests/all_tests.nim b/tests/all_tests.nim index fc717e157..f42629fa7 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -1,5 +1,7 @@ import ./test_code_stream, ./test_gas_meter, ./test_memory, - ./test_stack + ./test_stack, + ./test_opcode + diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index 83d791a2c..f1853486f 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -1,12 +1,16 @@ import - os, macros, json, strformat, strutils, ttmath, utils / [hexadecimal, address, padding], chain, vm_state, constants, db / [db_chain, state_db], vm / forks / frontier / vm, parseutils, ospaths, tables + os, macros, json, strformat, strutils, parseutils, ospaths, tables, + ttmath, + ../src/utils/[hexadecimal, address, padding], + ../src/[chain, vm_state, constants], + ../src/db/[db_chain, state_db], ../src/vm/forks/frontier/vm type Status* {.pure.} = enum OK, Fail, Skip proc validTest*(folder: string, name: string): bool = # tests we want to skip or which segfault will be skipped here - # TODO fix + # TODO fix result = "calldatacopy" notin name and "balanceAddressInputTooBigRightMyAddress." notin name and "callstatelessToReturn1" notin name and diff --git a/tests/test_opcode.nim b/tests/test_opcode.nim index 6101fdc29..3e4325fc8 100644 --- a/tests/test_opcode.nim +++ b/tests/test_opcode.nim @@ -1,7 +1,13 @@ -import - unittest, test_helpers, - constants, types, errors, logging, ttmath, - tables, parseutils, chain, vm_state, computation, opcode, opcode_table, utils / [header, padding], vm / [gas_meter, message, code_stream, stack], vm / forks / frontier / vm, db / [db_chain, state_db], db / backends / memory_backend +import + unittest, ttmath, tables, parseutils, + ../src/[constants, types, errors, logging], + ../src/[chain, vm_state, computation, opcode, opcode_table], + ../src/[utils/header, utils/padding], + ../src/vm/[gas_meter, message, code_stream, stack], + ../src/vm/forks/frontier/vm, + ../src/db/[db_chain, state_db, backends/memory_backend], + test_helpers + proc testCode(code: string, gas: UInt256): BaseComputation = var vm = newFrontierVM(Header(), newBaseChainDB(newMemoryDB())) @@ -11,7 +17,7 @@ proc testCode(code: string, gas: UInt256): BaseComputation = # blockNumber: fixture{"env"}{"currentNumber"}.getHexadecimalInt.u256, # gasLimit: fixture{"env"}{"currentGasLimit"}.getHexadecimalInt.u256, # timestamp: fixture{"env"}{"currentTimestamp"}.getHexadecimalInt) - + let message = newMessage( to="", #fixture{"exec"}{"address"}.getStr, sender="", #fixture{"exec"}{"caller"}.getStr, @@ -57,4 +63,4 @@ suite "opcodes": # assert_eq!(gas_left, U256::from(79_988)); # assert_store(&ext, 0, "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"); -# } \ No newline at end of file +# }