diff --git a/.gitignore b/.gitignore index 05b1eb201..c85aefea6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ nimcache/ -runner -*_test +build/ diff --git a/.travis.yml b/.travis.yml index 2f58ed871..c980ec6d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ sudo: required services: - docker before_install: - - docker pull yglukhov/nim-base + - docker pull statusteam/nim-base script: - - docker run yglukhov/nim-base nim --version - - docker run -v "$(pwd):/project" -w /project yglukhov/nim-base /bin/bash -c "nimble install -y && ./tests/build_test.sh && ./tests/test.sh" + - docker run -v "$(pwd):/project" -w /project statusteam/nim-base nim --version + - docker run -v "$(pwd):/project" -w /project statusteam/nim-base sh -c "nimble refresh && nimble test" diff --git a/nimbus.nimble b/nimbus.nimble index d516bdd5a..e72f3e09d 100644 --- a/nimbus.nimble +++ b/nimbus.nimble @@ -7,10 +7,20 @@ description = "An Ethereum 2.0 Sharding Client for Resource-Restricted Devices license = "Apache License 2.0" skipDirs = @["tests"] -requires "nim >= 0.17.0", +requires "nim >= 0.18.1", "https://github.com/status-im/nim-keccak-tiny.git >= 0.1.0", - "https://github.com/alehander42/nim-rlp.git#fix-ordinal", - "https://github.com/status-im/nim-ttmath >= 0.5.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" +proc test(name: string, lang = "cpp") = + if not dirExists "build": + mkDir "build" + if not dirExists "nimcache": + mkDir "nimcache" + --run + --nimcache: "nimcache" + switch("out", ("./build/" & name)) + setCommand lang, "tests/" & name & ".nim" +task test, "Run tests": + test "all_tests" diff --git a/src/vm/value.nim b/src/vm/value.nim index e03664928..f93bb5bfb 100644 --- a/src/vm/value.nim +++ b/src/vm/value.nim @@ -8,10 +8,18 @@ type Value* = ref object case kind*: ValueKind: of VInt: - i*: Int256 + Fi: array[32, byte] #Int256 of VBinary: b*: seq[byte] +# TODO: The Int256 value is stored as array[32, byte], and we bitcast it +# back and forth. This is a hacky workaround for the problem that clang +# doesn't let you store ttmath types inside nim variant types (unions). Things +# should get better when we switch to mpint. + +proc i*(v: Value): Int256 {.inline.} = + cast[ptr Int256](unsafeAddr v.Fi)[] + proc `$`*(value: Value): string = case value.kind: of VInt: @@ -19,23 +27,16 @@ proc `$`*(value: Value): string = of VBinary: &"Binary({value.b})" -proc vint*(i: int): Value = - Value(kind: VInt, i: i.int256) +proc toArr(i: Int256): array[32, byte] {.inline.} = + cast[ptr array[32, byte]](unsafeAddr i)[] proc vint*(i: Int256): Value = - Value(kind: VInt, i: i) + Value(kind: VInt, Fi: i.toArr) + +proc vint*(i: int): Value {.inline.} = vint(i.int256) proc vbinary*(b: string): Value = Value(kind: VBinary, b: b.mapIt(it.byte)) proc vbinary*(b: seq[byte]): Value = Value(kind: VBinary, b: b) - -proc `==`*(a: Value, b: Value): bool = - if a.kind != b.kind: - return false - case a.kind: - of VInt: - a.i == b.i - of VBinary: - a.b == b.b diff --git a/tests/README.md b/tests/README.md index 9105f7de1..c44ec940c 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,7 +1,3 @@ # tests TODO: more vm tests and fixtures! - -```bash -./tests/test.sh -``` diff --git a/tests/all_tests.nim b/tests/all_tests.nim new file mode 100644 index 000000000..fc717e157 --- /dev/null +++ b/tests/all_tests.nim @@ -0,0 +1,5 @@ +import ./test_code_stream, + ./test_gas_meter, + ./test_memory, + ./test_stack + diff --git a/tests/build_test.sh b/tests/build_test.sh deleted file mode 100755 index 1b2731f0d..000000000 --- a/tests/build_test.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -nim cpp tests/code_stream_test.nim -nim cpp tests/gas_meter_test.nim -nim cpp tests/memory_test.nim -nim cpp tests/stack_test.nim diff --git a/tests/nim.cfg b/tests/nim.cfg deleted file mode 100644 index 149123bad..000000000 --- a/tests/nim.cfg +++ /dev/null @@ -1 +0,0 @@ --p:"../src" diff --git a/tests/test.sh b/tests/test.sh deleted file mode 100755 index e52aa9fed..000000000 --- a/tests/test.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -./tests/code_stream_test -./tests/gas_meter_test -./tests/memory_test -./tests/stack_test - diff --git a/tests/code_stream_test.nim b/tests/test_code_stream.nim similarity index 97% rename from tests/code_stream_test.nim rename to tests/test_code_stream.nim index 613cfc9a3..972ac90d6 100644 --- a/tests/code_stream_test.nim +++ b/tests/test_code_stream.nim @@ -1,4 +1,5 @@ -import unittest, strutils, sequtils, opcode_values, vm / code_stream +import unittest, strutils, sequtils, + ../src/opcode_values, ../src/vm/code_stream suite "parse bytecode": test "accepts bytes": diff --git a/tests/gas_meter_test.nim b/tests/test_gas_meter.nim similarity index 94% rename from tests/gas_meter_test.nim rename to tests/test_gas_meter.nim index 23061cd39..d43b7f13f 100644 --- a/tests/gas_meter_test.nim +++ b/tests/test_gas_meter.nim @@ -1,4 +1,6 @@ -import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, logging, vm / gas_meter, ttmath +import unittest, macros, strformat, strutils, sequtils, + ttmath, + ../src/[constants, opcode_values, errors, logging, vm/gas_meter] # TODO: quicktest # PS: parametrize can be easily immitated, but still quicktests would be even more useful @@ -63,7 +65,7 @@ suite "gasMeter": # expect(ValidationError): # gasMeter.returnGas(-1.i256) - # TODO: -0/+0 + # TODO: -0/+0 test "consume spends": all(gasMeter): check(gasMeter.gasRemaining == gasMeter.startGas) diff --git a/tests/memory_test.nim b/tests/test_memory.nim similarity index 94% rename from tests/memory_test.nim rename to tests/test_memory.nim index e9ac1fc0e..0e2a87bbb 100644 --- a/tests/memory_test.nim +++ b/tests/test_memory.nim @@ -1,4 +1,6 @@ -import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / memory, ttmath +import unittest, macros, strformat, strutils, sequtils, + ttmath, + ../src/[constants, opcode_values, errors, vm/memory] proc memory32: Memory = result = newMemory() @@ -27,7 +29,7 @@ suite "memory": test "write rejects invalid size": # expect(ValidationError): - # var mem = memory32() + # var mem = memory32() # mem.write(startPosition = 0.i256, size = -1.i256, value = @[1.byte, 0.byte]) expect(ValidationError): var mem = memory32() @@ -37,7 +39,7 @@ suite "memory": expect(ValidationError): var mem = memory32() mem.write(startPosition = 0.u256, size = 4.u256, value = @[1.byte, 0.byte]) - + test "write rejects valyes beyond memory size": expect(ValidationError): var mem = memory128() diff --git a/tests/opcode_test.nim b/tests/test_opcode.nim similarity index 100% rename from tests/opcode_test.nim rename to tests/test_opcode.nim diff --git a/tests/stack_test.nim b/tests/test_stack.nim similarity index 92% rename from tests/stack_test.nim rename to tests/test_stack.nim index 937a7b0e5..1d0b716ea 100644 --- a/tests/stack_test.nim +++ b/tests/test_stack.nim @@ -1,4 +1,7 @@ -import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / [stack, value], ttmath, utils / [bytes, padding], utils_numeric +import unittest, macros, strformat, strutils, sequtils, + ttmath, + ../src/[constants, opcode_values, errors, utils_numeric, vm/stack, vm/value, utils/bytes, utils/padding] + template testPush(value: untyped, expected: untyped): untyped = var stack = newStack() @@ -17,7 +20,7 @@ suite "stack": testPush("ves".toBytes, "ves".toBytes.bigEndianToInt) testFailPush("yzyzyzyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz".toBytes) - + test "push does not allow stack to exceed 1024": var stack = newStack() for z in 0 .. < 1024: @@ -37,7 +40,7 @@ suite "stack": check(stack.len == 1024) expect(FullStack): stack.dup(1) - + test "pop returns latest stack item": var stack = newStack() for element in @[1'u, 2'u, 3'u]: @@ -78,7 +81,7 @@ suite "stack": var stack = newStack() expect(InsufficientStack): stack.swap(0) - + test "dup raises InsufficientStack appropriately": var stack = newStack() expect(InsufficientStack): diff --git a/tests/vm_test.nim b/tests/test_vm.nim similarity index 100% rename from tests/vm_test.nim rename to tests/test_vm.nim diff --git a/tests/vm_json_test.nim b/tests/test_vm_json.nim similarity index 100% rename from tests/vm_json_test.nim rename to tests/test_vm_json.nim