fixes 32 bit failure

This commit is contained in:
andri lim 2019-12-16 17:11:06 +07:00
parent d5d3e6f0be
commit e2b88bbc0b
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 47 additions and 37 deletions

View File

@ -254,7 +254,7 @@ type
# @param context The pointer to the Host execution context.
# @param address The address of the account the query is about.
# @return true if exists, false otherwise.
evmc_account_exists_fn* = proc(context: evmc_host_context, address: evmc_address): c99bool {.cdecl.}
evmc_account_exists_fn* = proc(context: evmc_host_context, address: ptr evmc_address): c99bool {.cdecl.}
# Get storage callback function.
#
@ -265,7 +265,7 @@ type
# @param key The index of the account's storage entry.
# @return The storage value at the given storage key or null bytes
# if the account does not exist.
evmc_get_storage_fn* = proc(context: evmc_host_context, address: evmc_address, key: evmc_bytes32): evmc_bytes32 {.cdecl.}
evmc_get_storage_fn* = proc(context: evmc_host_context, address: ptr evmc_address, key: ptr evmc_bytes32): evmc_bytes32 {.cdecl.}
# The effect of an attempt to modify a contract storage item.
#
@ -304,8 +304,8 @@ type
# @param key The index of the storage entry.
# @param value The value to be stored.
# @return The effect on the storage item.
evmc_set_storage_fn* = proc(context: evmc_host_context, address: evmc_address,
key, value: evmc_bytes32): evmc_storage_status {.cdecl.}
evmc_set_storage_fn* = proc(context: evmc_host_context, address: ptr evmc_address,
key, value: ptr evmc_bytes32): evmc_storage_status {.cdecl.}
# Get balance callback function.
#
@ -314,7 +314,7 @@ type
# @param context The pointer to the Host execution context.
# @param address The address of the account.
# @return The balance of the given account or 0 if the account does not exist.
evmc_get_balance_fn* = proc(context: evmc_host_context, address: evmc_address): evmc_uint256be {.cdecl.}
evmc_get_balance_fn* = proc(context: evmc_host_context, address: ptr evmc_address): evmc_uint256be {.cdecl.}
# Get code size callback function.
#
@ -324,7 +324,7 @@ type
# @param context The pointer to the Host execution context.
# @param address The address of the account.
# @return The size of the code in the account or 0 if the account does not exist.
evmc_get_code_size_fn* = proc(context: evmc_host_context, address: evmc_address): uint {.cdecl.}
evmc_get_code_size_fn* = proc(context: evmc_host_context, address: ptr evmc_address): uint {.cdecl.}
# Get code hash callback function.
#
@ -335,7 +335,7 @@ type
# @param context The pointer to the Host execution context.
# @param address The address of the account.
# @return The hash of the code in the account or null bytes if the account does not exist.
evmc_get_code_hash_fn* = proc(context: evmc_host_context, address: evmc_address): evmc_bytes32 {.cdecl.}
evmc_get_code_hash_fn* = proc(context: evmc_host_context, address: ptr evmc_address): evmc_bytes32 {.cdecl.}
# Copy code callback function.
#
@ -352,7 +352,7 @@ type
# to store a copy of the requested code.
# @param buffer_size The size of the memory buffer.
# @return The number of bytes copied to the buffer by the Client.
evmc_copy_code_fn* = proc(context: evmc_host_context, address: evmc_address,
evmc_copy_code_fn* = proc(context: evmc_host_context, address: ptr evmc_address,
code_offset: uint, buffer_data: ptr byte,
buffer_size: uint): uint {.cdecl.}
@ -364,7 +364,7 @@ type
# @param context The pointer to the Host execution context. See ::evmc_host_context.
# @param address The address of the contract to be selfdestructed.
# @param beneficiary The address where the remaining ETH is going to be transferred.
evmc_selfdestruct_fn* = proc(context: evmc_host_context, address, beneficiary: evmc_address) {.cdecl.}
evmc_selfdestruct_fn* = proc(context: evmc_host_context, address, beneficiary: ptr evmc_address) {.cdecl.}
# Log callback function.
#
@ -377,7 +377,7 @@ type
# @param data_size The length of the data.
# @param topics The pointer to the array of topics attached to the log.
# @param topics_count The number of the topics. Valid values are between 0 and 4 inclusively.
evmc_emit_log_fn* = proc(context: evmc_host_context, address: evmc_address,
evmc_emit_log_fn* = proc(context: evmc_host_context, address: ptr evmc_address,
data: ptr byte, data_size: uint,
topics: ptr evmc_bytes32, topics_count: uint) {.cdecl.}
@ -386,7 +386,7 @@ type
# @param context The pointer to the Host execution context.
# @param msg The call parameters.
# @return The result of the call.
evmc_call_fn* = proc(context: evmc_host_context, msg: evmc_message): evmc_result {.cdecl.}
evmc_call_fn* = proc(context: evmc_host_context, msg: ptr evmc_message): evmc_result {.cdecl.}
# The Host interface.
#

View File

@ -23,42 +23,42 @@ proc getBlockHash*(ctx: HostContext, number: int64): evmc_bytes32 =
ctx.host.get_block_hash(ctx.context, number)
proc accountExists*(ctx: HostContext, address: evmc_address): bool =
ctx.host.account_exists(ctx.context, address).bool
ctx.host.account_exists(ctx.context, address.unsafeAddr).bool
proc getStorage*(ctx: HostContext, address: evmc_address, key: evmc_bytes32): evmc_bytes32 =
ctx.host.get_storage(ctx.context, address, key)
ctx.host.get_storage(ctx.context, address.unsafeAddr, key.unsafeAddr)
proc setStorage*(ctx: HostContext, address: evmc_address,
key, value: evmc_bytes32): evmc_storage_status =
ctx.host.set_storage(ctx.context, address, key, value)
ctx.host.set_storage(ctx.context, address.unsafeAddr, key.unsafeAddr, value.unsafeAddr)
proc getBalance*(ctx: HostContext, address: evmc_address): evmc_uint256be =
ctx.host.get_balance(ctx.context, address)
ctx.host.get_balance(ctx.context, address.unsafeAddr)
proc getCodeSize*(ctx: HostContext, address: evmc_address): int =
ctx.host.get_code_size(ctx.context, address).int
ctx.host.get_code_size(ctx.context, address.unsafeAddr).int
proc getCodeHash*(ctx: HostContext, address: evmc_address): evmc_bytes32 =
ctx.host.get_code_hash(ctx.context, address)
ctx.host.get_code_hash(ctx.context, address.unsafeAddr)
proc copyCode*(ctx: HostContext, address: evmc_address, codeOffset: int = 0): seq[byte] =
let size = ctx.getCodeSize(address)
if size - codeOffset > 0:
result = newSeq[byte](size - codeOffset)
let read = ctx.host.copy_code(ctx.context, address, code_offset.uint, result[0].addr, result.len.uint).int
let read = ctx.host.copy_code(ctx.context, address.unsafeAddr, code_offset.uint, result[0].addr, result.len.uint).int
doAssert(read == result.len)
proc copyCode*(ctx: HostContext, address: evmc_address, codeOffset: int, output: ptr byte, output_len: int): int =
ctx.host.copy_code(ctx.context, address, code_offset.uint, output, output_len.uint).int
ctx.host.copy_code(ctx.context, address.unsafeAddr, code_offset.uint, output, output_len.uint).int
proc selfdestruct*(ctx: HostContext, address, beneficiary: evmc_address) =
ctx.host.selfdestruct(ctx.context, address, beneficiary)
ctx.host.selfdestruct(ctx.context, address.unsafeAddr, beneficiary.unsafeAddr)
proc emitLog*(ctx: HostContext, address: evmc_address, data: openArray[byte], topics: openArray[evmc_bytes32]) =
ctx.host.emit_log(ctx.context, address, data[0].unsafeAddr, data.len.uint, topics[0].unsafeAddr, topics.len.uint)
ctx.host.emit_log(ctx.context, address.unsafeAddr, data[0].unsafeAddr, data.len.uint, topics[0].unsafeAddr, topics.len.uint)
proc call*(ctx: HostContext, msg: evmc_message): evmc_result =
ctx.host.call(ctx.context, msg)
ctx.host.call(ctx.context, msg.unsafeAddr)
proc init*(x: var EvmcVM, vm: ptr evmc_vm, hc: HostContext) =
x.vm = vm

View File

@ -166,7 +166,7 @@ const evmc_host_interface* example_host_get_interface()
return &evmc::Host::get_interface();
}
evmc_host_context* example_host_create_context(evmc_tx_context tx_context)
evmc_host_context* example_host_create_context(evmc_tx_context &tx_context)
{
evmc::accounts accounts;
evmc::account acc;

View File

@ -36,15 +36,15 @@ proc evmcGetBlockHashImpl(ctx: HostContext, number: int64): evmc_bytes32 {.cdecl
if number < current_block_number and number >= current_block_number - 256:
result.bytes = hash
proc evmcAccountExistsImpl(ctx: HostContext, address: evmc_address): c99bool {.cdecl.} =
proc evmcAccountExistsImpl(ctx: HostContext, address: var evmc_address): c99bool {.cdecl.} =
address in ctx.accounts
proc evmcGetStorageImpl(ctx: HostContext, address: evmc_address, key: evmc_bytes32): evmc_bytes32 {.cdecl.} =
proc evmcGetStorageImpl(ctx: HostContext, address: var evmc_address, key: var evmc_bytes32): evmc_bytes32 {.cdecl.} =
if address in ctx.accounts:
result = ctx.accounts[address].storage[key]
proc evmcSetStorageImpl(ctx: HostContext, address: evmc_address,
key, value: evmc_bytes32): evmc_storage_status {.cdecl.} =
proc evmcSetStorageImpl(ctx: HostContext, address: var evmc_address,
key, value: var evmc_bytes32): evmc_storage_status {.cdecl.} =
if address in ctx.accounts:
var acc = ctx.accounts[address]
@ -57,19 +57,19 @@ proc evmcSetStorageImpl(ctx: HostContext, address: evmc_address,
ctx.accounts[address] = acc
result = EVMC_STORAGE_MODIFIED
proc evmcGetBalanceImpl(ctx: HostContext, address: evmc_address): evmc_uint256be {.cdecl.} =
proc evmcGetBalanceImpl(ctx: HostContext, address: var evmc_address): evmc_uint256be {.cdecl.} =
if address in ctx.accounts:
result = ctx.accounts[address].balance
proc evmcGetCodeSizeImpl(ctx: HostContext, address: evmc_address): uint {.cdecl.} =
proc evmcGetCodeSizeImpl(ctx: HostContext, address: var evmc_address): uint {.cdecl.} =
if address in ctx.accounts:
result = ctx.accounts[address].code.len.uint
proc evmcGetCodeHashImpl(ctx: HostContext, address: evmc_address): evmc_bytes32 {.cdecl.} =
proc evmcGetCodeHashImpl(ctx: HostContext, address: var evmc_address): evmc_bytes32 {.cdecl.} =
if address in ctx.accounts:
result = ctx.accounts[address].codeHash()
proc evmcCopyCodeImpl(ctx: HostContext, address: evmc_address,
proc evmcCopyCodeImpl(ctx: HostContext, address: var evmc_address,
code_offset: uint, buffer_data: ptr byte,
buffer_size: uint): uint {.cdecl.} =
@ -85,15 +85,15 @@ proc evmcCopyCodeImpl(ctx: HostContext, address: evmc_address,
copyMem(buffer_data, acc.code[code_offset].addr, n)
result = n.uint
proc evmcSelfdestructImpl(ctx: HostContext, address, beneficiary: evmc_address) {.cdecl.} =
proc evmcSelfdestructImpl(ctx: HostContext, address, beneficiary: var evmc_address) {.cdecl.} =
discard
proc evmcEmitLogImpl(ctx: HostContext, address: evmc_address,
proc evmcEmitLogImpl(ctx: HostContext, address: var evmc_address,
data: ptr byte, data_size: uint,
topics: ptr evmc_bytes32, topics_count: uint) {.cdecl.} =
discard
proc evmcCallImpl(ctx: HostContext, msg: evmc_message): evmc_result {.cdecl.} =
proc evmcCallImpl(ctx: HostContext, msg: var evmc_message): evmc_result {.cdecl.} =
result = evmc_result(status_code: EVMC_REVERT, gas_left: msg.gas, output_data: msg.input_data, output_size: msg.input_size)
proc evmcSetOptionImpl(vm: ptr evmc_vm, name, value: cstring): evmc_set_option_result {.cdecl.} =
@ -120,7 +120,8 @@ proc evmcExecuteImpl(vm: ptr evmc_vm, host: ptr evmc_host_interface,
let output_size = 20
var value, key: evmc_bytes32
value.bytes[31] = byte(tx_context.block_number)
discard ctx.evmcSetStorageImpl(msg.destination, key, value)
var dest = msg.destination
discard ctx.evmcSetStorageImpl(dest, key, value)
var output_data = alloc(output_size)
var bn = $tx_context.block_number
copyMem(output_data, bn[0].addr, bn.len)

View File

@ -6,13 +6,16 @@ import stew/byteutils
{.compile: "evmc_c/example_vm.c".}
{.passL: "-lstdc++"}
when defined(posix):
{.passC: "-std=c++11".}
proc example_host_get_interface(): ptr evmc_host_interface {.importc, cdecl.}
proc example_host_create_context(tx_context: evmc_tx_context): evmc_host_context {.importc, cdecl.}
proc example_host_create_context(tx_context: var evmc_tx_context): evmc_host_context {.importc, cdecl.}
proc example_host_destroy_context(context: evmc_host_context) {.importc, cdecl.}
proc evmc_create_example_vm(): ptr evmc_vm {.importc, cdecl.}
proc nim_host_get_interface(): ptr evmc_host_interface {.importc, cdecl.}
proc nim_host_create_context(tx_context: evmc_tx_context): evmc_host_context {.importc, cdecl.}
proc nim_host_create_context(tx_context: var evmc_tx_context): evmc_host_context {.importc, cdecl.}
proc nim_host_destroy_context(context: evmc_host_context) {.importc, cdecl.}
proc nim_create_example_vm(): ptr evmc_vm {.importc, cdecl.}
@ -59,6 +62,12 @@ template runTest(testName: string, create_vm, get_host_interface, create_host_co
test "getTxContext":
let txc = hc.getTxContext()
debugEcho tx_context.block_number, " " , txc.block_number
debugEcho tx_context.block_timestamp, " " , txc.block_timestamp
debugEcho tx_context.block_gas_limit, " " , txc.block_gas_limit
check tx_context.block_number == txc.block_number
check tx_context.block_timestamp == txc.block_timestamp
check tx_context.block_gas_limit == txc.block_gas_limit