diff --git a/examples/example_host.cpp b/examples/example_host.cpp index 7f2bc8c..ed96d3b 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -94,10 +94,14 @@ public: evmc::result call(const evmc_message& msg) noexcept final { - (void)msg; // TODO: Improve C++ API for result creation. evmc_result res{}; - res.status_code = EVMC_FAILURE; + 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; }; return evmc::result{res}; } diff --git a/test/unittests/test_cpp.cpp b/test/unittests/test_cpp.cpp index 15133bd..9f7c525 100644 --- a/test/unittests/test_cpp.cpp +++ b/test/unittests/test_cpp.cpp @@ -87,7 +87,6 @@ TEST(cpp, host) EXPECT_EQ(host.copy_code(a, 0, nullptr, 0), 0); host.selfdestruct(a, a); - EXPECT_EQ(host.call({}).gas_left, 0); auto tx = host.get_tx_context(); EXPECT_EQ(host.get_tx_context().block_number, tx.block_number); @@ -98,3 +97,26 @@ TEST(cpp, host) example_host_destroy_context(host_context); } + +TEST(cpp, host_call) +{ + // Use example host to test Host::call() method. + + auto* host_context = example_host_create_context(); + auto host = evmc::HostContext{host_context}; + + EXPECT_EQ(host.call({}).gas_left, 0); + + auto msg = evmc_message{}; + msg.gas = 1; + uint8_t input[] = {0xa, 0xb, 0xc}; + msg.input_data = input; + msg.input_size = sizeof(input); + + auto res = host.call(msg); + EXPECT_EQ(res.status_code, EVMC_REVERT); + EXPECT_EQ(res.output_size, msg.input_size); + EXPECT_TRUE(std::equal(&res.output_data[0], &res.output_data[res.output_size], msg.input_data)); + + example_host_destroy_context(host_context); +}