Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add test case for precompile implementation #283

Merged
merged 3 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/example_precompiles_vm/example_precompiles_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static evmc_result execute_empty(const evmc_message* msg)
static evmc_result not_implemented()
{
auto result = evmc_result{};
result.status_code = EVMC_INTERNAL_ERROR;
result.status_code = EVMC_REJECTED;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this special case for non-supported code/message. I think it fits better.

Also we have the specialised EVMC_PRECOMPILE_FAILURE which we don't use in this example.

return result;
}

Expand Down
47 changes: 47 additions & 0 deletions test/vmtester/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,50 @@ TEST_F(evmc_vm_test, set_tracer)
if (vm->set_tracer)
vm->set_tracer(vm, tracer_callback, nullptr);
}

TEST_F(evmc_vm_test, precompile_test)
{
// This logic is based on and should match the description in EIP-2003.

if (!evmc_vm_has_capability(vm, EVMC_CAPABILITY_PRECOMPILES))
return;

// Iterate every address (as per EIP-1352)
for (size_t i = 0; i < 0xffff; i++)
{
auto destination = evmc_address{};
destination.bytes[18] = static_cast<uint8_t>(i >> 8);
destination.bytes[19] = static_cast<uint8_t>(i & 0xff);

evmc_message msg{
EVMC_CALL, 0, 0, 65536, destination, evmc_address{}, NULL, 0, evmc_uint256be{},
evmc_bytes32{}};

evmc_result result = vm->execute(vm, nullptr, EVMC_MAX_REVISION, &msg, nullptr, 0);

// Validate some constraints

// Precompiles can only return a limited subset of codes.
EXPECT_TRUE(result.status_code == EVMC_SUCCESS || result.status_code == EVMC_OUT_OF_GAS ||
result.status_code == EVMC_FAILURE || result.status_code == EVMC_REVERT ||
result.status_code == EVMC_REJECTED);

if (result.status_code != EVMC_SUCCESS && result.status_code != EVMC_REVERT)
{
EXPECT_EQ(result.gas_left, 0);
}

if (result.output_data == NULL)
{
EXPECT_EQ(result.output_size, 0);
}
else
{
EXPECT_NE(result.output_size, 0);
read_buffer(result.output_data, result.output_size);
}

if (result.release)
result.release(&result);
}
}