From 21e6605f987cacdafc0fbe31f4182619e3e0b8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Sun, 9 Sep 2018 01:42:24 +0200 Subject: [PATCH] Return bytes32 from get_storage() directly --- bindings/go/evmc/host.c | 22 +++++++++++----------- bindings/go/evmc/host.go | 11 +++-------- examples/example_host.cpp | 14 +++++--------- examples/example_vm.c | 3 +-- include/evmc/evmc.h | 20 ++++++++------------ 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/bindings/go/evmc/host.c b/bindings/go/evmc/host.c index 7b7f88b..75751f9 100644 --- a/bindings/go/evmc/host.c +++ b/bindings/go/evmc/host.c @@ -41,7 +41,7 @@ static inline void go_exported_functions_type_checks() { struct evmc_context* context = NULL; evmc_address* address = NULL; - evmc_bytes32* bytes32 = NULL; + evmc_bytes32 bytes32; evmc_uint256be* uint256be = NULL; uint8_t* data = NULL; size_t size = 0; @@ -62,12 +62,12 @@ static inline void go_exported_functions_type_checks() bool_flag = accountExists(context, address); evmc_get_storage_fn get_storage_fn = NULL; - get_storage_fn(bytes32, context, address, bytes32); - getStorage(bytes32, context, address, bytes32); + bytes32 = get_storage_fn(context, address, &bytes32); + bytes32 = getStorage(context, address, &bytes32); evmc_set_storage_fn set_storage_fn = NULL; - storage_status = set_storage_fn(context, address, bytes32, bytes32); - storage_status = setStorage(context, address, bytes32, bytes32); + storage_status = set_storage_fn(context, address, &bytes32, &bytes32); + storage_status = setStorage(context, address, &bytes32, &bytes32); evmc_get_balance_fn get_balance_fn = NULL; bool_flag = get_balance_fn(uint256be, context, address); @@ -78,8 +78,8 @@ static inline void go_exported_functions_type_checks() bool_flag = getCodeSize(&size, context, address); evmc_get_code_hash_fn get_code_hash_fn = NULL; - bool_flag = get_code_hash_fn(bytes32, context, address); - bool_flag = getCodeHash(bytes32, context, address); + bool_flag = get_code_hash_fn(&bytes32, context, address); + bool_flag = getCodeHash(&bytes32, context, address); evmc_copy_code_fn copy_code_fn = NULL; size = copy_code_fn(context, address, size, data, size); @@ -98,10 +98,10 @@ static inline void go_exported_functions_type_checks() tx_context = getTxContext(context); evmc_get_block_hash_fn get_block_hash_fn = NULL; - bool_flag = get_block_hash_fn(bytes32, context, number); - bool_flag = getBlockHash(bytes32, context, number); + bool_flag = get_block_hash_fn(&bytes32, context, number); + bool_flag = getBlockHash(&bytes32, context, number); evmc_emit_log_fn emit_log_fn = NULL; - emit_log_fn(context, address, data, size, bytes32, size); - emitLog(context, address, data, size, bytes32, size); + emit_log_fn(context, address, data, size, &bytes32, size); + emitLog(context, address, data, size, &bytes32, size); } diff --git a/bindings/go/evmc/host.go b/bindings/go/evmc/host.go index 0a6357f..033f7fa 100644 --- a/bindings/go/evmc/host.go +++ b/bindings/go/evmc/host.go @@ -71,7 +71,7 @@ func goByteSlice(data *C.uint8_t, size C.size_t) []byte { type HostContext interface { AccountExists(addr common.Address) bool - GetStorage(addr common.Address, key common.Hash) (common.Hash, error) + GetStorage(addr common.Address, key common.Hash) common.Hash SetStorage(addr common.Address, key common.Hash, value common.Hash) (StorageStatus, error) GetBalance(addr common.Address) (common.Hash, error) GetCodeSize(addr common.Address) (int, error) @@ -95,15 +95,10 @@ func accountExists(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.bool { } //export getStorage -func getStorage(pResult *C.evmc_bytes32, pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.evmc_bytes32) C.bool { +func getStorage(pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.evmc_bytes32) C.evmc_bytes32 { idx := int((*C.struct_extended_context)(pCtx).index) ctx := getHostContext(idx) - value, err := ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)) - if err != nil { - return false - } - *pResult = evmcBytes32(value) - return true + return evmcBytes32(ctx.GetStorage(goAddress(*pAddr), goHash(*pKey))) } //export setStorage diff --git a/examples/example_host.cpp b/examples/example_host.cpp index af15e37..0903c94 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -35,19 +35,15 @@ static bool account_exists(evmc_context* context, const evmc_address* address) return host->accounts.find(*address) != host->accounts.end(); } -static bool get_storage(evmc_bytes32* result, - evmc_context* context, - const evmc_address* address, - const evmc_bytes32* key) +static evmc_bytes32 get_storage(evmc_context* context, + const evmc_address* address, + const evmc_bytes32* key) { example_host_context* host = static_cast(context); auto it = host->accounts.find(*address); if (it != host->accounts.end()) - { - *result = it->second.storage[*key]; - return true; - } - return false; + return it->second.storage[*key]; + return {}; } static enum evmc_storage_status set_storage(evmc_context* context, diff --git a/examples/example_vm.c b/examples/example_vm.c index 6af5841..1a0b3d9 100644 --- a/examples/example_vm.c +++ b/examples/example_vm.c @@ -120,9 +120,8 @@ static struct evmc_result execute(struct evmc_instance* instance, } else if (code_size == strlen(counter) && strncmp((const char*)code, counter, code_size) == 0) { - evmc_bytes32 value; const evmc_bytes32 key = {{0}}; - context->host->get_storage(&value, context, &msg->destination, &key); + evmc_bytes32 value = context->host->get_storage(context, &msg->destination, &key); value.bytes[31]++; context->host->set_storage(context, &msg->destination, &key, &value); ret.status_code = EVMC_SUCCESS; diff --git a/include/evmc/evmc.h b/include/evmc/evmc.h index 51496ef..d2ad7ef 100644 --- a/include/evmc/evmc.h +++ b/include/evmc/evmc.h @@ -413,19 +413,15 @@ typedef bool (*evmc_account_exists_fn)(struct evmc_context* context, const evmc_ * * This callback function is used by a VM to query the given contract storage entry. * - * @param[out] result The pointer to the place where to put the result value. - * @param context The pointer to the Host execution context. - * @param address The address of the account. - * @param key The index of the account's storage entry. - * @return If the account exists the value is put at the location - * pointed by @p result and true is returned. - * If the account does not exist false is returned without - * modifying the memory pointed by @p result. + * @param context The Host execution context. + * @param address The address of the account. + * @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. */ -typedef bool (*evmc_get_storage_fn)(evmc_bytes32* result, - struct evmc_context* context, - const evmc_address* address, - const evmc_bytes32* key); +typedef evmc_bytes32 (*evmc_get_storage_fn)(struct evmc_context* context, + const evmc_address* address, + const evmc_bytes32* key); /**