Skip to content

Commit

Permalink
Static compilation fixes after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 5, 2018
1 parent b08de18 commit 6d53c46
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions nimbus/vm/interpreter/gas_costs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import
math, eth_common/eth_types,
../utils/[macros_gen_opcodes, utils_numeric],
./opcode_values
./utils/[macros_gen_opcodes, utils_numeric],
./opcode_values, ./vm_forks

# Gas Fee Schedule
# Yellow Paper Appendix G - https://ethereum.github.io/yellowpaper/paper.pdf
Expand Down
25 changes: 10 additions & 15 deletions nimbus/vm/interpreter/opcodes_impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ op address, inline = true:

op balance, inline = true:
## 0x31, Get balance of the given account.
let address = computation.stack.popAddress
computation.vmState.db(readOnly=true):
push: db.getBalance(address)
let address = computation.stack.popAddress()
push: computation.vmState.readOnlyStateDB.getBalance(address)

op origin, inline = true:
## 0x32, Get execution origination address.
Expand Down Expand Up @@ -390,21 +389,17 @@ op mstore8, inline = true, memStartPos, value:
op sload, inline = true, slot:
## 0x54, Load word from storage.

# TODO: this returns 0 and does not work
# computation.vmState.db(readOnly=true):
# let (value, _) = db.getStorage(computation.msg.storageAddress, slot)
# push: value

push: 2 # Why 2? stub carry over from OO implementation
let (value, found) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.storageAddress, slot)
if found:
push: value
else:
# TODO: raise exception?
discard

op sstore, inline = false, slot, value:
## 0x55, Save word to storage.

var currentValue = 0.u256
var existing = false

computation.vmState.db(readOnly=true):
(currentValue, existing) = db.getStorage(computation.msg.storageAddress, slot)
let (currentValue, existing) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.storageAddress, slot)

let
gasParam = GasParams(kind: Op.Sstore, s_isStorageEmpty: not existing)
Expand All @@ -415,7 +410,7 @@ op sstore, inline = false, slot, value:
if gasRefund > 0:
computation.gasMeter.refundGas(gasRefund)

computation.vmState.db(readOnly=false):
computation.vmState.mutateStateDB:
db.setStorage(computation.msg.storageAddress, slot, value)

op jump, inline = true, jumpTarget:
Expand Down
2 changes: 1 addition & 1 deletion nimbus/vm/interpreter/utils/utils_numeric.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import
strformat, strutils, sequtils, endians, macros,
eth_common/eth_types, rlp,
../../constants, ../../utils/padding
../../../constants, ../../../utils/padding

# some methods based on py-evm utils/numeric

Expand Down
2 changes: 1 addition & 1 deletion nimbus/vm/stack.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import
strformat, strutils, sequtils, macros, rlp, eth_common, nimcrypto,
../errors, ../validation, ./utils/utils_numeric, ../constants, ../logging, .. / utils / bytes
../errors, ../validation, ./interpreter/utils/utils_numeric, ../constants, ../logging, .. / utils / bytes

type
Stack* = ref object of RootObj
Expand Down
4 changes: 2 additions & 2 deletions tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.

when false:
when true:
import ./test_code_stream,
./test_gas_meter,
./test_memory,
./test_stack,
./test_opcode,
./test_storage_backends

when true:
when false:
import ./test_vm_json
17 changes: 9 additions & 8 deletions tests/test_code_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ suite "parse bytecode":
discard codeStream.next
check(codeStream.next == Op.STOP)

test "seek reverts to original position on exit":
var codeStream = newCodeStream("\x01\x02\x30")
check(codeStream.pc == 0)
codeStream.seek(1):
check(codeStream.pc == 1)
check(codeStream.next == Op.MUL)
check(codeStream.pc == 0)
check(codeStream.peek == Op.ADD)
# Seek has been dommented out for future deletion
# test "seek reverts to original position on exit":
# var codeStream = newCodeStream("\x01\x02\x30")
# check(codeStream.pc == 0)
# codeStream.seek(1):
# check(codeStream.pc == 1)
# check(codeStream.next == Op.MUL)
# check(codeStream.pc == 0)
# check(codeStream.peek == Op.ADD)

test "[] returns opcode":
let codeStream = newCodeStream("\x01\x02\x30")
Expand Down
18 changes: 9 additions & 9 deletions tests/test_memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ suite "memory":
test "write":
var mem = memory32()
# Test that write creates 32byte string == value padded with zeros
mem.write(startPosition = 0, value = @[1.byte, 0.byte, 1.byte, 0.byte])
mem.write(startPos = 0, 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":
Expand All @@ -48,23 +48,23 @@ suite "memory":
test "write rejects valyes beyond memory size":
expect(ValidationError):
var mem = memory128()
mem.write(startPosition = 128, value = @[1.byte, 0.byte, 1.byte, 0.byte])
mem.write(startPos = 128, 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, size = 10)
mem.extend(startPos = 0, size = 10)
check(mem.bytes == repeat(0.byte, 32))
# Test will extend past length if params require: 32 < (start_position + size) <= 64
mem.extend(startPosition = 28, size = 32)
mem.extend(startPos = 28, size = 32)
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, size = 10)
mem.extend(startPos = 48, size = 10)
check(mem.bytes == repeat(0.byte, 64))

test "read returns correct bytes":
var mem = memory32()
mem.write(startPosition = 5, value = @[1.byte, 0.byte, 1.byte, 0.byte])
check(mem.read(startPosition = 5, size = 4) == @[1.byte, 0.byte, 1.byte, 0.byte])
check(mem.read(startPosition = 6, size = 4) == @[0.byte, 1.byte, 0.byte, 0.byte])
check(mem.read(startPosition = 1, size = 3) == @[0.byte, 0.byte, 0.byte])
mem.write(startPos = 5, value = @[1.byte, 0.byte, 1.byte, 0.byte])
check(mem.read(startPos = 5, size = 4) == @[1.byte, 0.byte, 1.byte, 0.byte])
check(mem.read(startPos = 6, size = 4) == @[0.byte, 1.byte, 0.byte, 0.byte])
check(mem.read(startPos = 1, size = 3) == @[0.byte, 0.byte, 0.byte])

0 comments on commit 6d53c46

Please sign in to comment.