EVM-C: Fix C examples

This commit is contained in:
Paweł Bylica 2017-01-18 23:26:48 +01:00 committed by Alex Beregszaszi
parent da456c0ae6
commit 25a5fe0ceb
3 changed files with 20 additions and 10 deletions

View File

@ -22,16 +22,17 @@ static void query(union evm_variant* result,
const union evm_variant* arg) {
printf("EVM-C: QUERY %d\n", key);
switch (key) {
case EVM_GAS_LIMIT:
result->int64 = 314;
case EVM_CODE_BY_ADDRESS:
result->data = NULL;
result->data_size = 0;
break;
case EVM_BALANCE:
result->uint256be = balance(env, arg->address);
break;
case EVM_ADDRESS:
result->address = address(env);
case EVM_ACCOUNT_EXISTS:
result->int64 = 0;
break;
default:
@ -63,13 +64,19 @@ static int64_t call(
return EVM_CALL_FAILURE;
}
static void get_tx_context(struct evm_tx_context* result, struct evm_env* env)
{
}
/// Example how the API is supposed to be used.
int main(int argc, char *argv[]) {
struct evm_factory factory = examplevm_get_factory();
if (factory.abi_version != EVM_ABI_VERSION)
return 1; // Incompatible ABI version.
struct evm_instance* jit = factory.create(query, update, call);
struct evm_instance* jit = factory.create(query, update, call,
get_tx_context);
uint8_t const code[] = "Place some EVM bytecode here";
const size_t code_size = sizeof(code);

View File

@ -9,6 +9,7 @@ struct examplevm
evm_query_fn query_fn;
evm_update_fn update_fn;
evm_call_fn call_fn;
evm_get_tx_context_fn get_tx_context_fn;
};
static void evm_destroy(struct evm_instance* evm)
@ -93,7 +94,8 @@ static struct evm_result evm_execute(struct evm_instance* instance,
static struct evm_instance* evm_create(evm_query_fn query_fn,
evm_update_fn update_fn,
evm_call_fn call_fn)
evm_call_fn call_fn,
evm_get_tx_context_fn get_tx_context_fn)
{
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
struct evm_instance* interface = &vm->instance;
@ -103,6 +105,7 @@ static struct evm_instance* evm_create(evm_query_fn query_fn,
vm->query_fn = query_fn;
vm->update_fn = update_fn;
vm->call_fn = call_fn;
vm->get_tx_context_fn = get_tx_context_fn;
return interface;
}

View File

@ -29,6 +29,10 @@ enum {
EVM_ABI_VERSION = 0
};
/// Opaque struct representing execution enviroment managed by the host
/// application.
struct evm_env;
/// Big-endian 256-bit integer.
///
/// 32 bytes of data representing big-endian 256-bit integer. I.e. bytes[0] is
@ -169,10 +173,6 @@ enum evm_query_key {
};
/// Opaque struct representing execution enviroment managed by the host
/// application.
struct evm_env;
/// Variant type to represent possible types of values used in EVM.
///
/// Type-safety is lost around the code that uses this type. We should have