From 16049f2a8fc88a18a866a18c27dd6e43aee8316c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 6 Sep 2018 18:09:13 +0200 Subject: [PATCH 1/3] Add missing documentation about EVMC_STORAGE_MODIFIED_AGAIN cc @gumb0 --- include/evmc/evmc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/evmc/evmc.h b/include/evmc/evmc.h index 12e2bef..ca18da5 100644 --- a/include/evmc/evmc.h +++ b/include/evmc/evmc.h @@ -431,6 +431,7 @@ typedef void (*evmc_get_storage_fn)(struct evmc_uint256be* result, * - 0 is zero value, * - X != 0 (X is any value other than 0), * - Y != X, Y != 0 (Y is any value other than X and 0), + * - Z != Y (Z is any value other than Y), * - the "->" means the change from one value to another. */ enum evmc_storage_status From d128fe45d7bccfdb675e50afe2e0bfaac7310f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 6 Sep 2018 18:21:05 +0200 Subject: [PATCH 2/3] Introduce EVMC_STORAGE_NON_EXISTING_ACCOUNT for set_storage() Host method --- bindings/go/evmc/host.go | 8 ++++++-- examples/example_host.cpp | 30 +++++++++++++++++++++++++----- include/evmc/evmc.h | 14 +++++++++----- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/bindings/go/evmc/host.go b/bindings/go/evmc/host.go index 0a97aa8..7a3dea0 100644 --- a/bindings/go/evmc/host.go +++ b/bindings/go/evmc/host.go @@ -72,7 +72,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 - SetStorage(addr common.Address, key common.Hash, value common.Hash) StorageStatus + 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) GetCodeHash(addr common.Address) (common.Hash, error) @@ -106,7 +106,11 @@ func getStorage(pResult *C.struct_evmc_uint256be, pCtx unsafe.Pointer, pAddr *C. func setStorage(pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.struct_evmc_uint256be, pVal *C.struct_evmc_uint256be) C.enum_evmc_storage_status { idx := int((*C.struct_extended_context)(pCtx).index) ctx := getHostContext(idx) - return C.enum_evmc_storage_status(ctx.SetStorage(goAddress(*pAddr), goHash(*pKey), goHash(*pVal))) + status, err := ctx.SetStorage(goAddress(*pAddr), goHash(*pKey), goHash(*pVal)) + if err != nil { + return C.EVMC_STORAGE_NON_EXISTING_ACCOUNT + } + return C.enum_evmc_storage_status(status) } //export getBalance diff --git a/examples/example_host.cpp b/examples/example_host.cpp index e675f86..af285c0 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -24,6 +24,11 @@ bool operator<(const evmc_uint256be& a, const evmc_uint256be& b) return std::memcmp(a.bytes, b.bytes, sizeof(a)) < 0; } +bool operator==(const evmc_uint256be& a, const evmc_uint256be& b) +{ + return std::memcmp(a.bytes, b.bytes, sizeof(a)) == 0; +} + struct account { evmc_uint256be balance = {}; @@ -63,11 +68,26 @@ static enum evmc_storage_status set_storage(evmc_context* context, const evmc_uint256be* key, const evmc_uint256be* value) { - (void)context; - (void)address; - (void)key; - (void)value; - return EVMC_STORAGE_UNCHANGED; + example_host_context* host = static_cast(context); + auto accountIt = host->accounts.find(*address); + if (accountIt == host->accounts.end()) + return EVMC_STORAGE_NON_EXISTING_ACCOUNT; + + auto storageIt = accountIt->second.storage.find(*key); + if (storageIt == accountIt->second.storage.end()) + { + accountIt->second.storage.emplace(std::make_pair(*key, *value)); + return EVMC_STORAGE_ADDED; + } + else if (storageIt->second == *value) + { + return EVMC_STORAGE_UNCHANGED; + } + else + { + storageIt->second = *value; + return EVMC_STORAGE_MODIFIED; + } } static bool get_balance(evmc_uint256be* result, evmc_context* context, const evmc_address* address) diff --git a/include/evmc/evmc.h b/include/evmc/evmc.h index ca18da5..e71585a 100644 --- a/include/evmc/evmc.h +++ b/include/evmc/evmc.h @@ -459,21 +459,25 @@ enum evmc_storage_status /** * A storage item has been deleted: X -> 0. */ - EVMC_STORAGE_DELETED = 4 + EVMC_STORAGE_DELETED = 4, + + /** + * An attempt to modify storage of an non-existing account. + */ + EVMC_STORAGE_NON_EXISTING_ACCOUNT = 5 }; /** * Set storage callback function. * - * This callback function is used by an EVM to update the given contract - * storage entry. + * This callback function is used by an EVM to update the given contract storage entry. + * * @param context The pointer to the Host execution context. - * @see ::evmc_context. * @param address The address of the contract. * @param key The index of the storage entry. * @param value The value to be stored. - * @return The effect on the storage item, @see ::evmc_storage_status. + * @return The effect on the storage item. @see ::evmc_storage_status. */ typedef enum evmc_storage_status (*evmc_set_storage_fn)(struct evmc_context* context, const struct evmc_address* address, From a48893437f68fba84e11afbb6e560f5797ea4ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 6 Sep 2018 18:33:06 +0200 Subject: [PATCH 3/3] Return bool from get_storage() to indicate non-existing accounts --- bindings/go/evmc/host.go | 10 +++++++--- examples/example_host.cpp | 14 +++++++++----- include/evmc/evmc.h | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/bindings/go/evmc/host.go b/bindings/go/evmc/host.go index 7a3dea0..8e6faca 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 + GetStorage(addr common.Address, key common.Hash) (common.Hash, error) 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,11 +95,15 @@ func accountExists(pCtx unsafe.Pointer, pAddr *C.struct_evmc_address) C.bool { } //export getStorage -func getStorage(pResult *C.struct_evmc_uint256be, pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.struct_evmc_uint256be) { +func getStorage(pResult *C.struct_evmc_uint256be, pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.struct_evmc_uint256be) C.bool { idx := int((*C.struct_extended_context)(pCtx).index) ctx := getHostContext(idx) - value := ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)) + value, err := ctx.GetStorage(goAddress(*pAddr), goHash(*pKey)) + if err != nil { + return false + } *pResult = evmcUint256be(value) + return true } //export setStorage diff --git a/examples/example_host.cpp b/examples/example_host.cpp index af285c0..3be2077 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -52,15 +52,19 @@ static bool account_exists(evmc_context* context, const evmc_address* address) return host->accounts.find(*address) != host->accounts.end(); } -static void get_storage(evmc_uint256be* result, +static bool get_storage(evmc_uint256be* result, evmc_context* context, const evmc_address* address, const evmc_uint256be* key) { - (void)result; - (void)context; - (void)address; - (void)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; } static enum evmc_storage_status set_storage(evmc_context* context, diff --git a/include/evmc/evmc.h b/include/evmc/evmc.h index e71585a..2a937fa 100644 --- a/include/evmc/evmc.h +++ b/include/evmc/evmc.h @@ -409,15 +409,18 @@ typedef bool (*evmc_account_exists_fn)(struct evmc_context* context, /** * Get storage callback function. * - * This callback function is used by an EVM to query the given contract - * storage entry. - * @param[out] result The returned storage value. - * @param context The pointer to the Host execution context. - * @see ::evmc_context. - * @param address The address of the contract. - * @param key The index of the storage entry. + * 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. */ -typedef void (*evmc_get_storage_fn)(struct evmc_uint256be* result, +typedef bool (*evmc_get_storage_fn)(struct evmc_uint256be* result, struct evmc_context* context, const struct evmc_address* address, const struct evmc_uint256be* key);