2018-08-30 12:49:20 +00:00
|
|
|
// EVMC -- Ethereum Client-VM Connector API
|
|
|
|
// Copyright 2018 The EVMC Authors.
|
|
|
|
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
|
|
|
|
|
|
|
|
/// @file
|
|
|
|
/// Example implementation of an EVMC Host.
|
|
|
|
|
2018-08-30 15:38:14 +00:00
|
|
|
#include "example_host.h"
|
|
|
|
|
2018-08-28 14:42:30 +00:00
|
|
|
#include <evmc/helpers.h>
|
2018-09-08 14:08:37 +00:00
|
|
|
#include <evmc/helpers.hpp>
|
2018-08-28 14:42:30 +00:00
|
|
|
|
2018-09-06 10:20:19 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
struct account
|
|
|
|
{
|
|
|
|
evmc_uint256be balance = {};
|
|
|
|
size_t code_size = 0;
|
2018-09-06 22:08:13 +00:00
|
|
|
evmc_bytes32 code_hash = {};
|
|
|
|
std::map<evmc_bytes32, evmc_bytes32> storage;
|
2018-09-06 10:20:19 +00:00
|
|
|
};
|
|
|
|
|
2018-09-03 15:24:15 +00:00
|
|
|
struct example_host_context : evmc_context
|
|
|
|
{
|
|
|
|
example_host_context();
|
|
|
|
|
|
|
|
evmc_tx_context tx_context = {};
|
|
|
|
|
2018-09-06 10:20:19 +00:00
|
|
|
std::map<evmc_address, account> accounts;
|
|
|
|
};
|
2018-08-28 14:42:30 +00:00
|
|
|
|
2018-09-05 21:23:56 +00:00
|
|
|
static bool account_exists(evmc_context* context, const evmc_address* address)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 10:20:19 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
return host->accounts.find(*address) != host->accounts.end();
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 23:42:24 +00:00
|
|
|
static evmc_bytes32 get_storage(evmc_context* context,
|
|
|
|
const evmc_address* address,
|
|
|
|
const evmc_bytes32* key)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 16:33:06 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
auto it = host->accounts.find(*address);
|
|
|
|
if (it != host->accounts.end())
|
2018-09-08 23:42:24 +00:00
|
|
|
return it->second.storage[*key];
|
|
|
|
return {};
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum evmc_storage_status set_storage(evmc_context* context,
|
|
|
|
const evmc_address* address,
|
2018-09-06 22:08:13 +00:00
|
|
|
const evmc_bytes32* key,
|
|
|
|
const evmc_bytes32* value)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 16:21:05 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
2018-09-09 00:02:18 +00:00
|
|
|
auto& account = host->accounts[*address];
|
|
|
|
auto prevValue = account.storage[*key];
|
|
|
|
account.storage[*key] = *value;
|
2018-09-06 16:21:05 +00:00
|
|
|
|
2018-09-09 00:02:18 +00:00
|
|
|
if (prevValue == *value)
|
2018-09-06 16:21:05 +00:00
|
|
|
return EVMC_STORAGE_UNCHANGED;
|
|
|
|
else
|
|
|
|
return EVMC_STORAGE_MODIFIED;
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 00:13:53 +00:00
|
|
|
static evmc_uint256be get_balance(evmc_context* context, const evmc_address* address)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 10:20:19 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
auto it = host->accounts.find(*address);
|
|
|
|
if (it != host->accounts.end())
|
2018-09-09 00:13:53 +00:00
|
|
|
return it->second.balance;
|
|
|
|
return {};
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 08:00:15 +00:00
|
|
|
static size_t get_code_size(evmc_context* context, const evmc_address* address)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 10:20:19 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
auto it = host->accounts.find(*address);
|
|
|
|
if (it != host->accounts.end())
|
2018-09-09 08:00:15 +00:00
|
|
|
return it->second.code_size;
|
|
|
|
return 0;
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 08:11:43 +00:00
|
|
|
static evmc_bytes32 get_code_hash(evmc_context* context, const evmc_address* address)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-06 10:20:19 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
auto it = host->accounts.find(*address);
|
|
|
|
if (it != host->accounts.end())
|
2018-09-09 08:11:43 +00:00
|
|
|
return it->second.code_hash;
|
|
|
|
return {};
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t copy_code(evmc_context* context,
|
|
|
|
const evmc_address* address,
|
|
|
|
size_t code_offset,
|
|
|
|
uint8_t* buffer_data,
|
|
|
|
size_t buffer_size)
|
|
|
|
{
|
|
|
|
(void)context;
|
|
|
|
(void)address;
|
|
|
|
(void)code_offset;
|
|
|
|
(void)buffer_data;
|
|
|
|
(void)buffer_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void selfdestruct(evmc_context* context,
|
|
|
|
const evmc_address* address,
|
|
|
|
const evmc_address* beneficiary)
|
|
|
|
{
|
|
|
|
(void)context;
|
|
|
|
(void)address;
|
|
|
|
(void)beneficiary;
|
|
|
|
}
|
|
|
|
|
2018-08-29 21:00:11 +00:00
|
|
|
static evmc_result call(evmc_context* context, const evmc_message* msg)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
|
|
|
(void)context;
|
|
|
|
(void)msg;
|
2018-08-29 21:00:11 +00:00
|
|
|
evmc_result result{};
|
|
|
|
result.status_code = EVMC_FAILURE;
|
|
|
|
return result;
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 11:29:11 +00:00
|
|
|
static evmc_tx_context get_tx_context(evmc_context* context)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
|
|
|
(void)context;
|
2018-08-30 11:29:11 +00:00
|
|
|
evmc_tx_context result{};
|
|
|
|
return result;
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 09:06:55 +00:00
|
|
|
static evmc_bytes32 get_block_hash(evmc_context* context, int64_t number)
|
2018-08-28 14:42:30 +00:00
|
|
|
{
|
2018-09-03 15:24:15 +00:00
|
|
|
example_host_context* host = static_cast<example_host_context*>(context);
|
|
|
|
int64_t current_block_number = host->tx_context.block_number;
|
|
|
|
|
2018-09-09 09:06:55 +00:00
|
|
|
evmc_bytes32 example_block_hash;
|
|
|
|
if (number < current_block_number && number >= current_block_number - 256)
|
|
|
|
example_block_hash = {{1, 1, 1, 1}};
|
|
|
|
else
|
|
|
|
example_block_hash = {};
|
|
|
|
return example_block_hash;
|
2018-08-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_log(evmc_context* context,
|
|
|
|
const evmc_address* address,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
2018-09-06 22:08:13 +00:00
|
|
|
const evmc_bytes32 topics[],
|
2018-08-28 14:42:30 +00:00
|
|
|
size_t topics_count)
|
|
|
|
{
|
|
|
|
(void)context;
|
|
|
|
(void)address;
|
|
|
|
(void)data;
|
|
|
|
(void)data_size;
|
|
|
|
(void)topics;
|
|
|
|
(void)topics_count;
|
|
|
|
}
|
|
|
|
|
2018-08-31 10:15:37 +00:00
|
|
|
static const evmc_host_interface interface = {
|
2018-08-28 14:42:30 +00:00
|
|
|
account_exists, get_storage, set_storage, get_balance, get_code_size, get_code_hash,
|
|
|
|
copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log,
|
|
|
|
};
|
2018-08-30 13:15:39 +00:00
|
|
|
|
2018-09-03 15:24:15 +00:00
|
|
|
example_host_context::example_host_context() : evmc_context{&interface} {}
|
2018-08-30 13:15:39 +00:00
|
|
|
|
2018-08-30 15:38:14 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2018-08-30 13:15:39 +00:00
|
|
|
evmc_context* example_host_create_context()
|
|
|
|
{
|
|
|
|
return new example_host_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void example_host_destroy_context(evmc_context* context)
|
|
|
|
{
|
|
|
|
delete context;
|
2018-08-30 15:38:14 +00:00
|
|
|
}
|
|
|
|
}
|