Skip to content

Commit

Permalink
examples: Make some example Host methods a bit more interesting
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 6, 2018
1 parent cadad1f commit 5780449
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,42 @@

#include <evmc/helpers.h>

#include <cstring>
#include <map>

/// The comparator for std::map<evmc_address, ...>.
bool operator<(const evmc_address& a, const evmc_address& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a)) < 0;
}

/// The comparator for std::map<evmc_uint256be, ...>.
bool operator<(const evmc_uint256be& a, const evmc_uint256be& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a)) < 0;
}

struct account
{
evmc_uint256be balance = {};
size_t code_size = 0;
evmc_uint256be code_hash = {};
std::map<evmc_uint256be, evmc_uint256be> storage;
};

struct example_host_context : evmc_context
{
example_host_context();

evmc_tx_context tx_context = {};
};

static evmc_uint256be balance(evmc_context* context, const evmc_address* address)
{
(void)context;
(void)address;
evmc_uint256be ret = {{1, 2, 3, 4}};
return ret;
}
std::map<evmc_address, account> accounts;
};

static bool account_exists(evmc_context* context, const evmc_address* address)
{
(void)context;
(void)address;
return false;
example_host_context* host = static_cast<example_host_context*>(context);
return host->accounts.find(*address) != host->accounts.end();
}

static void get_storage(evmc_uint256be* result,
Expand Down Expand Up @@ -56,25 +72,39 @@ static enum evmc_storage_status set_storage(evmc_context* context,

static bool get_balance(evmc_uint256be* result, evmc_context* context, const evmc_address* address)
{
*result = balance(context, address);
return true;
example_host_context* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
{
*result = it->second.balance;
return true;
}
return false;
}

static bool get_code_size(size_t* result, evmc_context* context, const evmc_address* address)
{
(void)result;
(void)context;
(void)address;
example_host_context* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
{
*result = it->second.code_size;
return true;
}
return false;
}

static bool get_code_hash(evmc_uint256be* result,
evmc_context* context,
const evmc_address* address)
{
(void)result;
(void)context;
(void)address;
example_host_context* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
{
*result = it->second.code_hash;
return true;
}
return false;
}

Expand Down

0 comments on commit 5780449

Please sign in to comment.