2019-03-15 01:25:36 +01:00
|
|
|
/* EVMC: Ethereum Client-VM Connector API.
|
2019-04-24 16:12:13 +02:00
|
|
|
* Copyright 2016-2019 The EVMC Authors.
|
2019-03-15 01:25:36 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0.
|
|
|
|
*/
|
2018-08-30 14:49:20 +02:00
|
|
|
|
|
|
|
/// @file
|
|
|
|
/// Example implementation of an EVMC Host.
|
|
|
|
|
2018-08-30 17:38:14 +02:00
|
|
|
#include "example_host.h"
|
|
|
|
|
2019-03-28 17:07:34 +01:00
|
|
|
#include <evmc/evmc.hpp>
|
2018-08-28 15:42:30 +01:00
|
|
|
|
2019-10-30 19:03:57 +01:00
|
|
|
#include <algorithm>
|
2018-09-06 12:20:19 +02:00
|
|
|
#include <map>
|
2019-08-08 18:33:53 +02:00
|
|
|
#include <vector>
|
2018-09-06 12:20:19 +02:00
|
|
|
|
2019-07-22 17:30:42 +02:00
|
|
|
using namespace evmc::literals;
|
|
|
|
|
2019-08-08 18:45:31 +02:00
|
|
|
namespace evmc
|
|
|
|
{
|
2018-09-06 12:20:19 +02:00
|
|
|
struct account
|
|
|
|
{
|
2019-07-22 15:11:40 +02:00
|
|
|
evmc::uint256be balance = {};
|
2019-08-08 18:33:53 +02:00
|
|
|
std::vector<uint8_t> code;
|
2019-07-22 15:11:40 +02:00
|
|
|
std::map<evmc::bytes32, evmc::bytes32> storage;
|
2019-08-08 18:43:36 +02:00
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
virtual evmc::bytes32 code_hash() const
|
2019-08-08 18:43:36 +02:00
|
|
|
{
|
|
|
|
// Extremely dumb "hash" function.
|
|
|
|
evmc::bytes32 ret{};
|
|
|
|
for (std::vector<uint8_t>::size_type i = 0; i != code.size(); i++)
|
|
|
|
{
|
|
|
|
auto v = code[i];
|
|
|
|
ret.bytes[v % sizeof(ret.bytes)] ^= v;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-09-06 12:20:19 +02:00
|
|
|
};
|
|
|
|
|
2019-08-08 18:46:10 +02:00
|
|
|
using accounts = std::map<evmc::address, account>;
|
|
|
|
|
2019-08-08 18:45:31 +02:00
|
|
|
} // namespace evmc
|
|
|
|
|
2019-03-28 17:07:34 +01:00
|
|
|
class ExampleHost : public evmc::Host
|
2018-09-03 17:24:15 +02:00
|
|
|
{
|
2019-08-08 18:46:10 +02:00
|
|
|
evmc::accounts accounts;
|
2019-08-08 17:18:20 +02:00
|
|
|
evmc_tx_context tx_context{};
|
2018-08-28 15:42:30 +01:00
|
|
|
|
2019-03-28 17:07:34 +01:00
|
|
|
public:
|
2019-08-08 17:18:20 +02:00
|
|
|
ExampleHost() = default;
|
|
|
|
explicit ExampleHost(evmc_tx_context& _tx_context) noexcept : tx_context{_tx_context} {};
|
2019-08-08 18:51:34 +02:00
|
|
|
ExampleHost(evmc_tx_context& _tx_context, evmc::accounts& _accounts) noexcept
|
|
|
|
: accounts{_accounts}, tx_context{_tx_context} {};
|
2019-08-08 17:18:20 +02:00
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
bool account_exists(const evmc::address& addr) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
return accounts.find(addr) != accounts.end();
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
evmc::bytes32 get_storage(const evmc::address& addr, const evmc::bytes32& key) const
|
|
|
|
noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
2019-11-26 23:27:56 +01:00
|
|
|
const auto account_iter = accounts.find(addr);
|
|
|
|
if (account_iter == accounts.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto storage_iter = account_iter->second.storage.find(key);
|
|
|
|
if (storage_iter != account_iter->second.storage.end())
|
|
|
|
return storage_iter->second;
|
2019-03-28 17:07:34 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-23 16:09:52 +02:00
|
|
|
evmc_storage_status set_storage(const evmc::address& addr,
|
|
|
|
const evmc::bytes32& key,
|
|
|
|
const evmc::bytes32& value) noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
auto& account = accounts[addr];
|
|
|
|
auto prev_value = account.storage[key];
|
|
|
|
account.storage[key] = value;
|
|
|
|
|
|
|
|
return (prev_value == value) ? EVMC_STORAGE_UNCHANGED : EVMC_STORAGE_MODIFIED;
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
evmc::uint256be get_balance(const evmc::address& addr) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
|
|
|
return it->second.balance;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
size_t get_code_size(const evmc::address& addr) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
2019-08-08 18:43:36 +02:00
|
|
|
return it->second.code.size();
|
2019-03-28 17:07:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
evmc::bytes32 get_code_hash(const evmc::address& addr) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
auto it = accounts.find(addr);
|
|
|
|
if (it != accounts.end())
|
2019-08-08 18:43:36 +02:00
|
|
|
return it->second.code_hash();
|
2019-03-28 17:07:34 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-23 16:09:52 +02:00
|
|
|
size_t copy_code(const evmc::address& addr,
|
2019-03-28 17:07:34 +01:00
|
|
|
size_t code_offset,
|
|
|
|
uint8_t* buffer_data,
|
2019-11-26 21:49:56 +01:00
|
|
|
size_t buffer_size) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
2019-10-30 19:03:57 +01:00
|
|
|
const auto it = accounts.find(addr);
|
2019-08-08 18:33:53 +02:00
|
|
|
if (it == accounts.end())
|
|
|
|
return 0;
|
2019-10-30 19:03:57 +01:00
|
|
|
|
|
|
|
const auto& code = it->second.code;
|
|
|
|
|
2019-08-08 18:33:53 +02:00
|
|
|
if (code_offset >= code.size())
|
|
|
|
return 0;
|
2019-10-30 19:03:57 +01:00
|
|
|
|
|
|
|
const auto n = std::min(buffer_size, code.size() - code_offset);
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
std::copy_n(&code[code_offset], n, buffer_data);
|
|
|
|
return n;
|
2019-03-28 17:07:34 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 16:09:52 +02:00
|
|
|
void selfdestruct(const evmc::address& addr, const evmc::address& beneficiary) noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
|
|
|
(void)addr;
|
|
|
|
(void)beneficiary;
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc::result call(const evmc_message& msg) noexcept final
|
|
|
|
{
|
2019-06-26 13:51:45 +02:00
|
|
|
return {EVMC_REVERT, msg.gas, msg.input_data, msg.input_size};
|
2019-03-28 17:07:34 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
evmc_tx_context get_tx_context() const noexcept final { return tx_context; }
|
2019-03-28 17:07:34 +01:00
|
|
|
|
2019-11-26 21:49:56 +01:00
|
|
|
evmc::bytes32 get_block_hash(int64_t number) const noexcept final
|
2019-03-28 17:07:34 +01:00
|
|
|
{
|
2019-07-22 17:30:42 +02:00
|
|
|
const int64_t current_block_number = get_tx_context().block_number;
|
2019-03-28 17:07:34 +01:00
|
|
|
|
2019-07-22 17:30:42 +02:00
|
|
|
return (number < current_block_number && number >= current_block_number - 256) ?
|
2019-07-23 14:45:36 +02:00
|
|
|
0xb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5f_bytes32 :
|
|
|
|
0_bytes32;
|
2019-03-28 17:07:34 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 16:09:52 +02:00
|
|
|
void emit_log(const evmc::address& addr,
|
2019-03-28 17:07:34 +01:00
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
2019-07-23 16:09:52 +02:00
|
|
|
const evmc::bytes32 topics[],
|
2019-03-28 17:07:34 +01:00
|
|
|
size_t topics_count) noexcept final
|
|
|
|
{
|
|
|
|
(void)addr;
|
|
|
|
(void)data;
|
|
|
|
(void)data_size;
|
|
|
|
(void)topics;
|
|
|
|
(void)topics_count;
|
|
|
|
}
|
2018-08-28 15:42:30 +01:00
|
|
|
};
|
2018-08-30 15:15:39 +02:00
|
|
|
|
|
|
|
|
2018-08-30 17:38:14 +02:00
|
|
|
extern "C" {
|
|
|
|
|
2019-10-31 10:38:17 +01:00
|
|
|
const evmc_host_interface* example_host_get_interface()
|
|
|
|
{
|
2019-11-06 13:20:06 +01:00
|
|
|
return &evmc::Host::get_interface();
|
2019-10-31 10:38:17 +01:00
|
|
|
}
|
|
|
|
|
2019-09-20 11:29:46 +02:00
|
|
|
evmc_host_context* example_host_create_context(evmc_tx_context tx_context)
|
2018-08-30 15:15:39 +02:00
|
|
|
{
|
2019-10-31 10:38:17 +01:00
|
|
|
auto host = new ExampleHost{tx_context};
|
|
|
|
return host->to_context();
|
2018-08-30 15:15:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 11:29:46 +02:00
|
|
|
void example_host_destroy_context(evmc_host_context* context)
|
2018-08-30 15:15:39 +02:00
|
|
|
{
|
2019-10-31 10:38:17 +01:00
|
|
|
delete evmc::Host::from_context<ExampleHost>(context);
|
2018-08-30 17:38:14 +02:00
|
|
|
}
|
|
|
|
}
|