mirror of https://github.com/status-im/evmc.git
Move examplevm to its own dir
This commit is contained in:
parent
4578e80c38
commit
8830ab0d7d
|
@ -1,11 +1,8 @@
|
||||||
add_library(example-vm STATIC examplevm.c)
|
|
||||||
target_link_libraries(example-vm PRIVATE evmc)
|
add_subdirectory(examplevm)
|
||||||
if(NOT MSVC)
|
|
||||||
target_compile_options(example-vm PRIVATE -Wno-extra)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_executable(example-capi capi.c)
|
add_executable(example-capi capi.c)
|
||||||
target_link_libraries(example-capi PRIVATE evmc example-vm)
|
target_link_libraries(example-capi PRIVATE evmc evmc-examplevm)
|
||||||
if(NOT MSVC)
|
if(NOT MSVC)
|
||||||
target_compile_options(example-capi PRIVATE -Wno-extra)
|
target_compile_options(example-capi PRIVATE -Wno-extra)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
# EVMC -- Ethereum Client-VM Connector API
|
||||||
|
# Copyright 2018 Pawel Bylica.
|
||||||
|
# Licensed under the MIT License. See the LICENSE file.
|
||||||
|
|
||||||
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
|
add_library(evmc-examplevm examplevm.c)
|
||||||
|
target_link_libraries(evmc-examplevm PRIVATE evmc)
|
||||||
|
set_target_properties(evmc-examplevm PROPERTIES DEBUG_POSTFIX "")
|
||||||
|
|
||||||
|
install(TARGETS evmc-examplevm
|
||||||
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
|
@ -19,12 +19,11 @@ static void evmc_destroy(struct evmc_instance* evm)
|
||||||
/// Example options.
|
/// Example options.
|
||||||
///
|
///
|
||||||
/// VMs are allowed to omit this function implementation.
|
/// VMs are allowed to omit this function implementation.
|
||||||
int evmc_set_option(struct evmc_instance* instance,
|
int evmc_set_option(struct evmc_instance* instance, char const* name, char const* value)
|
||||||
char const* name,
|
|
||||||
char const* value)
|
|
||||||
{
|
{
|
||||||
struct examplevm* vm = (struct examplevm*)instance;
|
struct examplevm* vm = (struct examplevm*)instance;
|
||||||
if (strcmp(name, "verbose") == 0) {
|
if (strcmp(name, "verbose") == 0)
|
||||||
|
{
|
||||||
long int v = strtol(value, NULL, 0);
|
long int v = strtol(value, NULL, 0);
|
||||||
if (v > INT_MAX || v < INT_MIN)
|
if (v > INT_MAX || v < INT_MIN)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -48,12 +47,11 @@ static void free_result_output_data(struct evmc_result const* result)
|
||||||
static struct evmc_result execute(struct evmc_instance* instance, struct evmc_context* context,
|
static struct evmc_result execute(struct evmc_instance* instance, struct evmc_context* context,
|
||||||
enum evmc_revision rev, const struct evmc_message* msg, const uint8_t* code, size_t code_size)
|
enum evmc_revision rev, const struct evmc_message* msg, const uint8_t* code, size_t code_size)
|
||||||
{
|
{
|
||||||
struct evmc_result ret = {EVMC_INTERNAL_ERROR};
|
struct evmc_result ret = {.status_code = EVMC_INTERNAL_ERROR};
|
||||||
if (code_size == 0)
|
if (code_size == 0)
|
||||||
{
|
{
|
||||||
// In case of empty code return a fancy error message.
|
// In case of empty code return a fancy error message.
|
||||||
const char* error = rev == EVMC_BYZANTIUM ?
|
const char* error = rev == EVMC_BYZANTIUM ? "Welcome to Byzantium!" : "Hello Ethereum!";
|
||||||
"Welcome to Byzantium!" : "Hello Ethereum!";
|
|
||||||
ret.output_data = (const uint8_t*)error;
|
ret.output_data = (const uint8_t*)error;
|
||||||
ret.output_size = strlen(error);
|
ret.output_size = strlen(error);
|
||||||
ret.status_code = EVMC_FAILURE;
|
ret.status_code = EVMC_FAILURE;
|
||||||
|
@ -73,10 +71,12 @@ static struct evmc_result execute(struct evmc_instance* instance, struct evmc_co
|
||||||
const char counter[] = "600160005401600055";
|
const char counter[] = "600160005401600055";
|
||||||
|
|
||||||
if (code_size == strlen(return_address) &&
|
if (code_size == strlen(return_address) &&
|
||||||
strncmp((const char*)code, return_address, code_size) == 0) {
|
strncmp((const char*)code, return_address, code_size) == 0)
|
||||||
|
{
|
||||||
static const size_t address_size = sizeof(msg->destination);
|
static const size_t address_size = sizeof(msg->destination);
|
||||||
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
||||||
if (!output_data) {
|
if (!output_data)
|
||||||
|
{
|
||||||
// malloc failed, report internal error.
|
// malloc failed, report internal error.
|
||||||
ret.status_code = EVMC_INTERNAL_ERROR;
|
ret.status_code = EVMC_INTERNAL_ERROR;
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -88,10 +88,10 @@ static struct evmc_result execute(struct evmc_instance* instance, struct evmc_co
|
||||||
ret.release = &free_result_output_data;
|
ret.release = &free_result_output_data;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else if (code_size == strlen(counter) &&
|
else if (code_size == strlen(counter) && strncmp((const char*)code, counter, code_size) == 0)
|
||||||
strncmp((const char*)code, counter, code_size) == 0) {
|
{
|
||||||
struct evmc_uint256be value;
|
struct evmc_uint256be value;
|
||||||
const struct evmc_uint256be index = {{0,}};
|
const struct evmc_uint256be index = {{0}};
|
||||||
context->fn_table->get_storage(&value, context, &msg->destination, &index);
|
context->fn_table->get_storage(&value, context, &msg->destination, &index);
|
||||||
value.bytes[31] += 1;
|
value.bytes[31] += 1;
|
||||||
context->fn_table->set_storage(context, &msg->destination, &index, &value);
|
context->fn_table->set_storage(context, &msg->destination, &index, &value);
|
||||||
|
@ -115,7 +115,7 @@ struct evmc_instance* examplevm_create()
|
||||||
.abi_version = EVMC_ABI_VERSION,
|
.abi_version = EVMC_ABI_VERSION,
|
||||||
.destroy = evmc_destroy,
|
.destroy = evmc_destroy,
|
||||||
.execute = execute,
|
.execute = execute,
|
||||||
.set_option = evmc_set_option
|
.set_option = evmc_set_option,
|
||||||
};
|
};
|
||||||
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
|
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
|
||||||
struct evmc_instance* interface = &vm->instance;
|
struct evmc_instance* interface = &vm->instance;
|
Loading…
Reference in New Issue