Skip to content

Commit

Permalink
examples: add evmc_tx_context to ExampleHost
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Aug 8, 2019
1 parent bea35ea commit dd06526
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ int main(int argc, char* argv[])
const evmc_uint256be value = {{1, 0}};
const evmc_address addr = {{0, 1, 2}};
const int64_t gas = 200000;
struct evmc_context* ctx = example_host_create_context();
struct evmc_tx_context tx_context;
memset(&tx_context, 0, sizeof(tx_context));
struct evmc_context* ctx = example_host_create_context(tx_context);
struct evmc_message msg;
msg.sender = addr;
msg.destination = addr;
Expand Down
10 changes: 7 additions & 3 deletions examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ struct account
class ExampleHost : public evmc::Host
{
std::map<evmc::address, account> accounts;
evmc_tx_context tx_context{};

public:
ExampleHost() = default;
explicit ExampleHost(evmc_tx_context& _tx_context) noexcept : tx_context{_tx_context} {};

bool account_exists(const evmc::address& addr) noexcept final
{
return accounts.find(addr) != accounts.end();
Expand Down Expand Up @@ -98,7 +102,7 @@ class ExampleHost : public evmc::Host
return {EVMC_REVERT, msg.gas, msg.input_data, msg.input_size};
}

evmc_tx_context get_tx_context() noexcept final { return {}; }
evmc_tx_context get_tx_context() noexcept final { return tx_context; }

evmc::bytes32 get_block_hash(int64_t number) noexcept final
{
Expand Down Expand Up @@ -126,9 +130,9 @@ class ExampleHost : public evmc::Host

extern "C" {

evmc_context* example_host_create_context()
evmc_context* example_host_create_context(evmc_tx_context tx_context)
{
return new ExampleHost;
return new ExampleHost(tx_context);
}

void example_host_destroy_context(evmc_context* context)
Expand Down
2 changes: 1 addition & 1 deletion examples/example_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern "C" {
#endif

struct evmc_context* example_host_create_context();
struct evmc_context* example_host_create_context(struct evmc_tx_context tx_context);

void example_host_destroy_context(struct evmc_context* context);

Expand Down
4 changes: 2 additions & 2 deletions test/unittests/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ TEST(cpp, host)
{
// Use example host to execute all methods from the C++ host wrapper.

auto* host_context = example_host_create_context();
auto* host_context = example_host_create_context(evmc_tx_context{});
auto host = evmc::HostContext{host_context};

const auto a = evmc::address{{{1}}};
Expand Down Expand Up @@ -380,7 +380,7 @@ TEST(cpp, host_call)
{
// Use example host to test Host::call() method.

auto* host_context = example_host_create_context();
auto* host_context = example_host_create_context(evmc_tx_context{});
auto host = evmc::HostContext{host_context};

EXPECT_EQ(host.call({}).gas_left, 0);
Expand Down
4 changes: 2 additions & 2 deletions test/vmtester/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST_F(evmc_vm_test, capabilities)

TEST_F(evmc_vm_test, execute_call)
{
evmc_context* context = example_host_create_context();
evmc_context* context = example_host_create_context(evmc_tx_context{});
evmc_message msg{};
std::array<uint8_t, 2> code = {{0xfe, 0x00}};

Expand Down Expand Up @@ -92,7 +92,7 @@ TEST_F(evmc_vm_test, execute_call)

TEST_F(evmc_vm_test, execute_create)
{
evmc_context* context = example_host_create_context();
evmc_context* context = example_host_create_context(evmc_tx_context{});
evmc_message msg{
EVMC_CREATE, 0, 0, 65536, evmc_address{}, evmc_address{}, nullptr, 0, evmc_uint256be{},
evmc_bytes32{}};
Expand Down

0 comments on commit dd06526

Please sign in to comment.