2016-08-22 20:51:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-06-24 09:40:06 +00:00
|
|
|
#include "evm.h"
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
|
2016-08-22 20:51:43 +00:00
|
|
|
struct evm_uint256 balance(struct evm_env* env, struct evm_hash160 address)
|
|
|
|
{
|
|
|
|
struct evm_uint256 ret = { .words = { 1 } };
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct evm_hash160 address(struct evm_env* env)
|
|
|
|
{
|
|
|
|
struct evm_hash160 ret = { .bytes = { 1, 2, 3, 4 } };
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-24 09:40:06 +00:00
|
|
|
|
|
|
|
union evm_variant query(struct evm_env* env,
|
|
|
|
enum evm_query_key key,
|
|
|
|
union evm_variant arg) {
|
|
|
|
union evm_variant result;
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("EVM-C: QUERY %d\n", key);
|
2016-06-24 09:40:06 +00:00
|
|
|
switch (key) {
|
|
|
|
case EVM_GAS_LIMIT: result.int64 = 314; break;
|
|
|
|
|
|
|
|
case EVM_BALANCE:
|
|
|
|
result.uint256 = balance(env, arg.address);
|
|
|
|
break;
|
|
|
|
|
2016-08-22 20:51:43 +00:00
|
|
|
case EVM_ADDRESS:
|
|
|
|
result.address = address(env);
|
|
|
|
break;
|
|
|
|
|
2016-06-24 09:40:06 +00:00
|
|
|
default: result.int64 = 0; break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:51:43 +00:00
|
|
|
void update(struct evm_env* env,
|
|
|
|
enum evm_update_key key,
|
|
|
|
union evm_variant arg1,
|
|
|
|
union evm_variant arg2)
|
|
|
|
{
|
|
|
|
printf("EVM-C: UPDATE %d\n", key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t call(
|
|
|
|
struct evm_env* _opaqueEnv,
|
|
|
|
enum evm_call_kind _kind,
|
|
|
|
int64_t _gas,
|
|
|
|
struct evm_hash160 _address,
|
|
|
|
struct evm_uint256 _value,
|
|
|
|
uint8_t const* _inputData,
|
|
|
|
size_t _inputSize,
|
|
|
|
uint8_t* _outputData,
|
|
|
|
size_t _outputSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
printf("EVM-C: CALL %d\n", _kind);
|
|
|
|
return EVM_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2016-06-24 09:40:06 +00:00
|
|
|
/// Example how the API is supposed to be used.
|
2016-08-22 20:51:43 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
printf("Using VM: %s (%s)\n", evm_get_info(EVM_NAME), evm_get_info(EVM_VERSION));
|
|
|
|
|
2016-08-23 19:51:42 +00:00
|
|
|
struct evm_interface intf = examplevm_get_interface();
|
|
|
|
struct evm_instance* jit = intf.create(query, update, call);
|
2016-06-24 09:40:06 +00:00
|
|
|
|
|
|
|
char const code[] = "exec()";
|
2016-08-22 20:51:43 +00:00
|
|
|
const size_t code_size = sizeof(code);
|
2016-07-20 11:31:09 +00:00
|
|
|
struct evm_hash256 code_hash = {.words = {1, 2, 3}};
|
2016-07-20 12:15:04 +00:00
|
|
|
char const input[] = "Hello World!";
|
2016-06-24 09:40:06 +00:00
|
|
|
struct evm_uint256 value = {{1, 0, 0, 0}};
|
|
|
|
|
|
|
|
int64_t gas = 200000;
|
|
|
|
struct evm_result result =
|
2016-08-23 19:51:42 +00:00
|
|
|
intf.execute(jit, NULL, EVM_HOMESTEAD, code_hash, (const uint8_t *)code, code_size, gas, (const uint8_t *)input,
|
2016-07-20 12:15:04 +00:00
|
|
|
sizeof(input), value);
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("Execution result:\n");
|
2016-08-23 21:58:39 +00:00
|
|
|
if (result.outcome == EVM_RESULT_EXCEPTION) {
|
|
|
|
printf(" EVM exception\n");
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
2016-08-23 19:10:11 +00:00
|
|
|
printf(" Gas used: %ld\n", gas - result.gas_left);
|
|
|
|
printf(" Gas left: %ld\n", result.gas_left);
|
2016-08-22 20:51:43 +00:00
|
|
|
printf(" Output size: %zd\n", result.output_size);
|
|
|
|
|
|
|
|
printf(" Output: ");
|
2016-08-23 10:58:09 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < result.output_size; i++) {
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("%02x ", result.output_data[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2016-08-23 19:51:42 +00:00
|
|
|
intf.release_result(&result);
|
|
|
|
intf.destroy(jit);
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|