2019-03-15 00:25:36 +00:00
|
|
|
/* EVMC: Ethereum Client-VM Connector API.
|
2019-04-24 14:12:13 +00:00
|
|
|
* Copyright 2016-2019 The EVMC Authors.
|
2019-03-15 00:25:36 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0.
|
|
|
|
*/
|
2018-08-30 12:49:20 +00:00
|
|
|
|
|
|
|
/// @file
|
|
|
|
/// Example implementation of an EVMC Host.
|
|
|
|
|
2018-08-30 15:38:14 +00:00
|
|
|
#include "example_host.h"
|
|
|
|
|
2019-03-28 16:07:34 +00:00
|
|
|
#include <evmc/evmc.hpp>
|
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
|
|
|
};
|
|
|
|
|
2019-03-28 16:07:34 +00:00
|
|
|
class ExampleHost : public evmc::Host
|
2018-09-03 15:24:15 +00:00
|
|
|
{
|
2018-09-06 10:20:19 +00:00
|
|
|
std::map<evmc_address, account> accounts;
|
2018-08-28 14:42:30 +00:00
|
|
|
|
2019-03-28 16:07:34 +00:00
|
|
|
public:
|
|
|
|
bool account_exists(const evmc_address& addr) noexcept final
|
|
|
|
{
|
|
|
|
return accounts.find(addr) != accounts.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_bytes32 get_storage(const evmc_address& addr, const evmc_bytes32& key) noexcept final
|
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
|
|
|
return it->second.storage[key];
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_storage_status set_storage(const evmc_address& addr,
|
|
|
|
const evmc_bytes32& key,
|
|
|
|
const evmc_bytes32& value) noexcept final
|
|
|
|
{
|
|
|
|
auto& account = accounts[addr];
|
|
|
|
auto prev_value = account.storage[key];
|
|
|
|
account.storage[key] = value;
|
|
|
|
|
|
|
|
return (prev_value == value) ? EVMC_STORAGE_UNCHANGED : EVMC_STORAGE_MODIFIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_uint256be get_balance(const evmc_address& addr) noexcept final
|
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
|
|
|
return it->second.balance;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_code_size(const evmc_address& addr) noexcept final
|
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
|
|
|
return it->second.code_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_bytes32 get_code_hash(const evmc_address& addr) noexcept final
|
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
|
|
|
return it->second.code_hash;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t copy_code(const evmc_address& addr,
|
|
|
|
size_t code_offset,
|
|
|
|
uint8_t* buffer_data,
|
|
|
|
size_t buffer_size) noexcept final
|
|
|
|
{
|
|
|
|
(void)addr;
|
|
|
|
(void)code_offset;
|
|
|
|
(void)buffer_data;
|
|
|
|
(void)buffer_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void selfdestruct(const evmc_address& addr, const evmc_address& beneficiary) noexcept final
|
|
|
|
{
|
|
|
|
(void)addr;
|
|
|
|
(void)beneficiary;
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc::result call(const evmc_message& msg) noexcept final
|
|
|
|
{
|
|
|
|
// TODO: Improve C++ API for result creation.
|
|
|
|
evmc_result res{};
|
2019-04-29 12:53:17 +00:00
|
|
|
res.status_code = EVMC_REVERT;
|
|
|
|
auto output = new uint8_t[msg.input_size];
|
|
|
|
std::copy(&msg.input_data[0], &msg.input_data[msg.input_size], output);
|
|
|
|
res.output_size = msg.input_size;
|
|
|
|
res.output_data = output;
|
|
|
|
res.release = [](const evmc_result* r) noexcept { delete[] r->output_data; };
|
2019-03-28 16:07:34 +00:00
|
|
|
return evmc::result{res};
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_tx_context get_tx_context() noexcept final { return {}; }
|
|
|
|
|
|
|
|
evmc_bytes32 get_block_hash(int64_t number) noexcept final
|
|
|
|
{
|
|
|
|
int64_t current_block_number = get_tx_context().block_number;
|
|
|
|
|
|
|
|
auto example_block_hash = evmc_bytes32{};
|
|
|
|
if (number < current_block_number && number >= current_block_number - 256)
|
|
|
|
example_block_hash = {{1, 1, 1, 1}};
|
|
|
|
return example_block_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emit_log(const evmc_address& addr,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
const evmc_bytes32 topics[],
|
|
|
|
size_t topics_count) noexcept final
|
|
|
|
{
|
|
|
|
(void)addr;
|
|
|
|
(void)data;
|
|
|
|
(void)data_size;
|
|
|
|
(void)topics;
|
|
|
|
(void)topics_count;
|
|
|
|
}
|
2018-08-28 14:42:30 +00:00
|
|
|
};
|
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()
|
|
|
|
{
|
2019-03-28 16:07:34 +00:00
|
|
|
return new ExampleHost;
|
2018-08-30 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void example_host_destroy_context(evmc_context* context)
|
|
|
|
{
|
2019-03-28 16:07:34 +00:00
|
|
|
delete static_cast<ExampleHost*>(context);
|
2018-08-30 15:38:14 +00:00
|
|
|
}
|
|
|
|
}
|