Skip to content

Commit

Permalink
tools: Add --input option to evmc run
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 3, 2020
1 parent 8a04005 commit 44ed951
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
20 changes: 15 additions & 5 deletions test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
# Copyright 2019-2020 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

set(prefix ${PROJECT_NAME}/evmc-run)
set(PREFIX ${PROJECT_NAME}/evmc-run)

add_test(
NAME ${prefix}/example1
NAME ${PREFIX}/example1
COMMAND evmc::tool run --vm $<TARGET_FILE:evmc::example-vm> 30600052596000f3 --gas 99
)
set_tests_properties(
${prefix}/example1 PROPERTIES PASS_REGULAR_EXPRESSION
${PREFIX}/example1 PROPERTIES PASS_REGULAR_EXPRESSION
"Result: +success[\r\n]+Gas used: +6[\r\n]+Output: +0000000000000000000000000000000000000000000000000000000000000000[\r\n]"
)

get_property(tools_tests DIRECTORY PROPERTY TESTS)
set_tests_properties(${tools_tests} PROPERTIES ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/tools-%p.profraw)

add_test(
NAME ${PREFIX}/copy_input
COMMAND evmc::tool run --vm $<TARGET_FILE:evmc::example-vm> 600035600052596000f3 --input aabbccdd
)
set_tests_properties(
${PREFIX}/copy_input PROPERTIES PASS_REGULAR_EXPRESSION
"Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]"
)

get_property(TOOLS_TESTS DIRECTORY PROPERTY TESTS)
set_tests_properties(${TOOLS_TESTS} PROPERTIES ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/tools-%p.profraw)
19 changes: 17 additions & 2 deletions test/unittests/tool_commands_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(tool_commands, run_empty_code)
auto vm = evmc::VM{evmc_create_example_vm()};
std::ostringstream out;

const auto exit_code = cmd::run(vm, EVMC_FRONTIER, 1, "", out);
const auto exit_code = cmd::run(vm, EVMC_FRONTIER, 1, "", "", out);
EXPECT_EQ(exit_code, 0);
EXPECT_EQ(out.str(), out_pattern("Frontier", 1, "success", 0, ""));
}
Expand All @@ -41,9 +41,24 @@ TEST(tool_commands, run_return_my_address)
auto vm = evmc::VM{evmc_create_example_vm()};
std::ostringstream out;

const auto exit_code = cmd::run(vm, EVMC_HOMESTEAD, 200, "30600052596000f3", out);
const auto exit_code = cmd::run(vm, EVMC_HOMESTEAD, 200, "30600052596000f3", "", out);
EXPECT_EQ(exit_code, 0);
EXPECT_EQ(out.str(),
out_pattern("Homestead", 200, "success", 6,
"0000000000000000000000000000000000000000000000000000000000000000"));
}

TEST(tool_commands, run_copy_input_to_output)
{
// Yul: mstore(0, calldataload(0)) return(0, msize())
auto vm = evmc::VM{evmc_create_example_vm()};
std::ostringstream out;

const auto exit_code =
cmd::run(vm, EVMC_TANGERINE_WHISTLE, 200, "600035600052596000f3",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", out);
EXPECT_EQ(exit_code, 0);
EXPECT_EQ(out.str(),
out_pattern("Tangerine Whistle", 200, "success", 7,
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"));
}
1 change: 1 addition & 0 deletions tools/commands/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int run(evmc::VM& vm,
evmc_revision rev,
int64_t gas,
const std::string& code_hex,
const std::string& input_hex,
std::ostream& out);
}
} // namespace evmc
4 changes: 4 additions & 0 deletions tools/commands/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ int run(evmc::VM& vm,
evmc_revision rev,
int64_t gas,
const std::string& code_hex,
const std::string& input_hex,
std::ostream& out)
{
out << "Executing on " << rev << " with " << gas << " gas limit\n";

const auto code = from_hex(code_hex);
const auto input = from_hex(input_hex);
evmc_message msg{};
msg.gas = gas;
msg.input_data = input.data();
msg.input_size = input.size();
MockedHost host;

const auto result = vm.execute(host, rev, msg, code.data(), code.size());
Expand Down
4 changes: 3 additions & 1 deletion tools/evmc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ int main(int argc, const char** argv)
std::string code_hex;
int64_t gas = 1000000;
auto rev = EVMC_ISTANBUL;
std::string input_hex;

auto& run_cmd = *app.add_subcommand("run", "Execute EVM bytecode");
run_cmd.add_option("code", code_hex, "Hex-encoded bytecode")->required();
run_cmd.add_option("--vm", vm_config, "EVMC VM module")->required()->envname("EVMC_VM");
run_cmd.add_option("--gas", gas, "Execution gas limit", true)->check(CLI::Range(0, 1000000000));
run_cmd.add_option("--rev", rev, "EVM revision", true);
run_cmd.add_option("--input", input_hex, "Hex-encoded input bytes");

try
{
Expand All @@ -49,7 +51,7 @@ int main(int argc, const char** argv)
return static_cast<int>(ec);
}
std::cout << "Config: " << vm_config << "\n";
return cmd::run(vm, rev, gas, code_hex, std::cout);
return cmd::run(vm, rev, gas, code_hex, input_hex, std::cout);
}

return 0;
Expand Down

0 comments on commit 44ed951

Please sign in to comment.