-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish the first memory impl and tests, stack tests, fixes validation
- Loading branch information
1 parent
04b1228
commit 211d46e
Showing
10 changed files
with
209 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
nimcache/ | ||
runner | ||
*_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Nimbus | ||
|
||
An Ethereum 2.0 Sharding Client for Resource-Restricted Devices. | ||
|
||
https://docs.google.com/document/d/14u65XVNLOd83cq3t7wNC9UPweZ6kPWvmXwRTWWn0diQ/edit# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / memory, bigints | ||
|
||
proc memory32: Memory = | ||
result = newMemory() | ||
result.extend(0.i256, 32.i256) | ||
|
||
proc memory128: Memory = | ||
result = newMemory() | ||
result.extend(0.i256, 128.i256) | ||
|
||
suite "memory": | ||
test "write": | ||
var mem = memory32() | ||
# Test that write creates 32byte string == value padded with zeros | ||
mem.write(startPosition = 0.i256, size = 4.i256, value = @[1.byte, 0.byte, 1.byte, 0.byte]) | ||
check(mem.bytes == @[1.byte, 0.byte, 1.byte, 0.byte].concat(repeat(0.byte, 28))) | ||
|
||
test "write rejects invalid position": | ||
expect(ValidationError): | ||
var mem = memory32() | ||
mem.write(startPosition = -1.i256, size = 2.i256, value = @[1.byte, 0.byte]) | ||
expect(ValidationError): | ||
var mem = memory32() | ||
mem.write(startPosition = pow(2.i256, 256), size = 2.i256, value = @[1.byte, 0.byte]) | ||
|
||
test "write rejects invalid size": | ||
expect(ValidationError): | ||
var mem = memory32() | ||
mem.write(startPosition = 0.i256, size = -1.i256, value = @[1.byte, 0.byte]) | ||
expect(ValidationError): | ||
var mem = memory32() | ||
mem.write(startPosition = 0.i256, size = pow(2.i256, 256), value = @[1.byte, 0.byte]) | ||
|
||
test "write rejects invalid value": | ||
expect(ValidationError): | ||
var mem = memory32() | ||
mem.write(startPosition = 0.i256, size = 4.i256, value = @[1.byte, 0.byte]) | ||
|
||
test "write rejects valyes beyond memory size": | ||
expect(ValidationError): | ||
var mem = memory128() | ||
mem.write(startPosition = 128.i256, size = 4.i256, value = @[1.byte, 0.byte, 1.byte, 0.byte]) | ||
|
||
test "extends appropriately extends memory": | ||
var mem = newMemory() | ||
# Test extends to 32 byte array: 0 < (start_position + size) <= 32 | ||
mem.extend(startPosition = 0.i256, size = 10.i256) | ||
check(mem.bytes == repeat(0.byte, 32)) | ||
# Test will extend past length if params require: 32 < (start_position + size) <= 64 | ||
mem.extend(startPosition = 28.i256, size = 32.i256) | ||
check(mem.bytes == repeat(0.byte, 64)) | ||
# Test won't extend past length unless params require: 32 < (start_position + size) <= 64 | ||
mem.extend(startPosition = 48.i256, size = 10.i256) | ||
check(mem.bytes == repeat(0.byte, 64)) | ||
|
||
test "read returns correct bytes": | ||
var mem = memory32() | ||
mem.write(startPosition = 5.i256, size = 4.i256, value = @[1.byte, 0.byte, 1.byte, 0.byte]) | ||
check(mem.read(startPosition = 5.i256, size = 4.i256) == @[1.byte, 0.byte, 1.byte, 0.byte]) | ||
check(mem.read(startPosition = 6.i256, size = 4.i256) == @[0.byte, 1.byte, 0.byte, 0.byte]) | ||
check(mem.read(startPosition = 1.i256, size = 3.i256) == @[0.byte, 0.byte, 0.byte]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / [stack, value], bigints | ||
|
||
suite "stack": | ||
test "push only valid": | ||
for value in @[0.vint, (pow(2.i256, 256) - 1.i256).vint, "ves".vbinary]: | ||
var stack = newStack() | ||
stack.push(value) | ||
check(stack.values == @[value]) | ||
|
||
for value in @[(-1).vint, (-2).vint, "yzyzyzyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz".vbinary]: | ||
var stack = newStack() | ||
expect(ValidationError): | ||
stack.push(value) | ||
|
||
test "push does not allow stack to exceed 1024": | ||
var stack = newStack() | ||
for z in 0 .. < 1024: | ||
stack.push(z) | ||
check(stack.len == 1024) | ||
expect(FullStack): | ||
stack.push(1025) | ||
|
||
test "dup does not allow stack to exceed 1024": | ||
var stack = newStack() | ||
stack.push(1.i256) | ||
for z in 0 ..< 1023: | ||
stack.dup(1.i256) | ||
check(stack.len == 1024) | ||
expect(FullStack): | ||
stack.dup(1.i256) | ||
|
||
test "pop returns latest stack item": | ||
var stack = newStack() | ||
for element in @[1.vint, 2.vint, 3.vint]: | ||
stack.push(element) | ||
check(stack.popInt == 3) | ||
|
||
stack = newStack() | ||
for element in @["1".vbinary]: | ||
stack.push(element) | ||
check(stack.popBinary == "1") | ||
|
||
|
||
test "swap correct": | ||
var stack = newStack() | ||
for z in 0 ..< 5: | ||
stack.push(z) | ||
check(stack.values == @[0.vint, 1.vint, 2.vint, 3.vint, 4.vint]) | ||
stack.swap(3) | ||
check(stack.values == @[0.vint, 4.vint, 2.vint, 3.vint, 1.vint]) | ||
stack.swap(1) | ||
check(stack.values == @[0.vint, 4.vint, 2.vint, 1.vint, 3.vint]) | ||
|
||
test "dup correct": | ||
var stack = newStack() | ||
for z in 0 ..< 5: | ||
stack.push(z) | ||
check(stack.values == @[0.vint, 1.vint, 2.vint, 3.vint, 4.vint]) | ||
stack.dup(1) | ||
check(stack.values == @[0.vint, 1.vint, 2.vint, 3.vint, 4.vint, 4.vint]) | ||
stack.dup(5) | ||
check(stack.values == @[0.vint, 1.vint, 2.vint, 3.vint, 4.vint, 4.vint, 1.vint]) | ||
|
||
test "pop raises InsufficientStack appropriately": | ||
var stack = newStack() | ||
expect(InsufficientStack): | ||
discard stack.popInt() | ||
|
||
test "swap raises InsufficientStack appropriately": | ||
var stack = newStack() | ||
expect(InsufficientStack): | ||
stack.swap(0) | ||
|
||
test "dup raises InsufficientStack appropriately": | ||
var stack = newStack() | ||
expect(InsufficientStack): | ||
stack.dup(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters