EVM-C: Include code hash in the message

This commit is contained in:
Paweł Bylica 2017-01-27 16:27:34 +01:00 committed by Alex Beregszaszi
parent cc35c5d169
commit 6d6b2c1928
3 changed files with 20 additions and 19 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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.