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

evmc tool CLI improvements #574

Merged
merged 3 commits into from
Mar 22, 2021
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
44 changes: 43 additions & 1 deletion test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add_evmc_tool_test(

add_evmc_tool_test(
copy_input
"--vm $<TARGET_FILE:evmc::example-vm> run 600035600052596000f3 --input aabbccdd"
"--vm $<TARGET_FILE:evmc::example-vm> run 600035600052596000f3 --input 0xaabbccdd"
"Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]"
)

Expand All @@ -39,5 +39,47 @@ add_evmc_tool_test(
"Result: +success[\r\n]+Gas used: +6[\r\n]+Output: +02[\r\n]"
)

add_test(NAME ${PROJECT_NAME}/evmc-tool/empty_code COMMAND evmc::tool --vm $<TARGET_FILE:evmc::example-vm> run "")
set_tests_properties(${PROJECT_NAME}/evmc-tool/empty_code PROPERTIES PASS_REGULAR_EXPRESSION "Result: +success[\r\n]+Gas used: +0[\r\n]+Output: +[\r\n]")

add_test(NAME ${PROJECT_NAME}/evmc-tool/explicit_empty_input COMMAND evmc::tool --vm $<TARGET_FILE:evmc::example-vm> run 0x6000 --input "")
set_tests_properties(${PROJECT_NAME}/evmc-tool/explicit_empty_input PROPERTIES PASS_REGULAR_EXPRESSION "Result: +success[\r\n]+Gas used: +1[\r\n]+Output: +[\r\n]")

add_evmc_tool_test(
invalid_hex_code
"--vm $<TARGET_FILE:evmc::example-vm> run 0x600"
"code: \\(incomplete hex byte pair\\) OR \\(File does not exist: 0x600\\)"
Copy link
Member

Choose a reason for hiding this comment

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

Error messge is a bit weird... Both statements are true, it's incomplete hex pair AND file does not exist.
Is there a way to customize the message somehow maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is CLI11 default but probably can be changed if you re-implement many things. I'm not going to do it now.

)

add_evmc_tool_test(
invalid_hex_input
"--vm $<TARGET_FILE:evmc::example-vm> run 0x --input aa0y"
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, we accept this weird Ethereum concept of 0x for empty input? Does it accept run '' or run "" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

All should work

Copy link
Member

Choose a reason for hiding this comment

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

Could you add one more test with that? I hope we can remove the run 0x case at some point in the future :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I meant adding a test for run "".

And yes, I know it currently accepts 0x as empty and not asking to remove it right now :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried 30 mins, but I don't know how to do this in CMake.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I thought "--vm $<TARGET_FILE:evmc::example-vm> run '' --input aa0y" would have been enough?

"--input: \\(invalid hex digit\\) OR \\(File does not exist: aa0y\\)"
)

add_evmc_tool_test(
code_from_file
"--vm $<TARGET_FILE:evmc::example-vm> run ${CMAKE_CURRENT_SOURCE_DIR}/code.hex --input 0xaabbccdd"
"Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]"
)

add_evmc_tool_test(
input_from_file
"--vm $<TARGET_FILE:evmc::example-vm> run 600035600052596000f3 --input ${CMAKE_CURRENT_SOURCE_DIR}/input.hex"
"Result: +success[\r\n]+Gas used: +7[\r\n]+Output: +aabbccdd00000000000000000000000000000000000000000000000000000000[\r\n]"
)

add_evmc_tool_test(
invalid_code_file
"--vm $<TARGET_FILE:evmc::example-vm> run ${CMAKE_CURRENT_SOURCE_DIR}/invalid_code.evm"
"Error: invalid hex digit"
)

add_evmc_tool_test(
vm_option_fallthrough
"run --vm $<TARGET_FILE:evmc::example-vm> 0x600030"
"Result: +success[\r\n]+Gas used: +2[\r\n]+Output: +[\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)
1 change: 1 addition & 0 deletions test/tools/code.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x600035600052596000f3
1 change: 1 addition & 0 deletions test/tools/input.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aabbccdd
1 change: 1 addition & 0 deletions test/tools/invalid_code.evm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
evm
49 changes: 44 additions & 5 deletions tools/evmc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,64 @@

#include "tools/commands/commands.hpp"
#include <CLI/CLI.hpp>
#include <evmc/hex.hpp>
#include <evmc/loader.h>
#include <fstream>

namespace
{
/// Returns the input str if already valid hex string. Otherwise, interprets the str as a file
/// name and loads the file content.
/// @todo The file content is expected to be a hex string but not validated.
std::string load_hex(const std::string& str)
{
const auto error_code = evmc::validate_hex(str);
if (!error_code)
return str;

// Must be a file path.
std::ifstream file{str};
return std::string(std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{});
}

struct HexValidator : public CLI::Validator
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

{
HexValidator() : CLI::Validator{"HEX"}
{
name_ = "HEX";
func_ = [](const std::string& str) -> std::string {
const auto error_code = evmc::validate_hex(str);
if (error_code)
return error_code.message();
return {};
};
}
};
} // namespace

int main(int argc, const char** argv)
{
using namespace evmc;

static HexValidator Hex;

std::string vm_config;
std::string code_hex;
std::string code_arg;
int64_t gas = 1000000;
auto rev = EVMC_ISTANBUL;
std::string input_hex;
std::string input_arg;
auto create = false;

CLI::App app{"EVMC tool"};
const auto& version_flag = *app.add_flag("--version", "Print version information and exit");
const auto& vm_option =
*app.add_option("--vm", vm_config, "EVMC VM module")->envname("EVMC_VM");

auto& run_cmd = *app.add_subcommand("run", "Execute EVM bytecode");
run_cmd.add_option("code", code_hex, "Hex-encoded bytecode")->required();
auto& run_cmd = *app.add_subcommand("run", "Execute EVM bytecode")->fallthrough();
run_cmd.add_option("code", code_arg, "Bytecode")->required()->check(Hex | CLI::ExistingFile);
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");
run_cmd.add_option("--input", input_arg, "Input bytes")->check(Hex | CLI::ExistingFile);
run_cmd.add_flag(
"--create", create,
"Create new contract out of the code and then execute this contract with the input");
Expand Down Expand Up @@ -71,6 +106,10 @@ int main(int argc, const char** argv)
throw CLI::RequiredError{vm_option.get_name()};

std::cout << "Config: " << vm_config << "\n";

const auto code_hex = load_hex(code_arg);
const auto input_hex = load_hex(input_arg);
// If code_hex or input_hex is not valid hex string an exception is thrown.
return cmd::run(vm, rev, gas, code_hex, input_hex, create, std::cout);
}

Expand Down