From 6d6b2c1928989dd635f5a1de5f1788aa511a546c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 27 Jan 2017 16:27:34 +0100 Subject: [PATCH] EVM-C: Include code hash in the message --- examples/capi.c | 7 ++++--- examples/examplevm.c | 25 ++++++++++++------------- include/evm.h | 7 ++++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/examples/capi.c b/examples/capi.c index e9d13cc..41033ab 100644 --- a/examples/capi.c +++ b/examples/capi.c @@ -91,14 +91,15 @@ int main(int argc, char *argv[]) { const size_t code_size = sizeof(code); struct evm_uint256be code_hash = {.bytes = {1, 2, 3,}}; uint8_t const input[] = "Hello World!"; - struct evm_uint256be value = {{1, 0, 0, 0}}; + struct evm_uint256be value = {{1, 0,}}; struct evm_uint160be addr = {{0, 1, 2,}}; int64_t gas = 200000; - struct evm_message msg = {addr, addr, value, input, sizeof(input), gas, 0}; + struct evm_message msg = {addr, addr, value, input, sizeof(input), + code_hash, gas, 0}; struct evm_result result = - jit->execute(jit, NULL, EVM_HOMESTEAD, code_hash, code, code_size, msg); + jit->execute(jit, NULL, EVM_HOMESTEAD, &msg, code, code_size); printf("Execution result:\n"); if (result.code != EVM_SUCCESS) { diff --git a/examples/examplevm.c b/examples/examplevm.c index 7a2a40c..6d97780 100644 --- a/examples/examplevm.c +++ b/examples/examplevm.c @@ -39,21 +39,20 @@ static void free_result_output_data(struct evm_result const* result) free((uint8_t*)result->output_data); } -static struct evm_result evm_execute(struct evm_instance* instance, - struct evm_env* env, - enum evm_mode mode, - struct evm_uint256be code_hash, - uint8_t const* code, - size_t code_size, - struct evm_message message) +static struct evm_result execute(struct evm_instance* instance, + struct evm_env* env, + enum evm_mode mode, + const struct evm_message* msg, + const uint8_t* code, + size_t code_size) { struct evm_result ret = {}; if (code_size == 0) { // In case of empty code return a fancy error message. - const char* msg = mode == EVM_METROPOLIS ? - "Welcome to Metropolis!" : "Hello Ethereum!"; - ret.output_data = (const uint8_t*)msg; - ret.output_size = strlen(msg); + const char* error = mode == EVM_METROPOLIS ? + "Welcome to Metropolis!" : "Hello Ethereum!"; + ret.output_data = (const uint8_t*)error; + ret.output_size = strlen(error); ret.code = EVM_FAILURE; ret.release = NULL; // We don't need to release the constant messages. return ret; @@ -77,7 +76,7 @@ static struct evm_result evm_execute(struct evm_instance* instance, ret.code = EVM_INTERNAL_ERROR; return ret; } - memcpy(output_data, &message.address, address_size); + memcpy(output_data, &msg->address, address_size); ret.code = EVM_SUCCESS; ret.output_data = output_data; ret.output_size = address_size; @@ -102,7 +101,7 @@ static struct evm_instance* evm_create(evm_query_state_fn query_fn, struct examplevm* vm = calloc(1, sizeof(struct examplevm)); struct evm_instance* interface = &vm->instance; interface->destroy = evm_destroy; - interface->execute = evm_execute; + interface->execute = execute; interface->set_option = evm_set_option; vm->query_fn = query_fn; vm->update_fn = update_fn; diff --git a/include/evm.h b/include/evm.h index 55e850d..df5c16f 100644 --- a/include/evm.h +++ b/include/evm.h @@ -45,6 +45,7 @@ struct evm_uint256be { }; /// Big-endian 160-bit hash suitable for keeping an Ethereum address. +/// TODO: Rename to "address". struct evm_uint160be { /// The 20 bytes of the hash. uint8_t bytes[20]; @@ -56,6 +57,7 @@ struct evm_message { struct evm_uint256be value; const uint8_t* input; size_t input_size; + struct evm_uint256be code_hash; int64_t gas; int32_t depth; }; @@ -437,10 +439,9 @@ enum evm_mode { typedef struct evm_result (*evm_execute_fn)(struct evm_instance* instance, struct evm_env* env, enum evm_mode mode, - struct evm_uint256be code_hash, + const struct evm_message* msg, uint8_t const* code, - size_t code_size, - struct evm_message message); + size_t code_size); /// Status of a code in VM. Useful for JIT-like implementations.