Skip to content

Commit

Permalink
tools: Load code/input from a file if path provided
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 22, 2021
1 parent 390ced7 commit 2b497c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
16 changes: 14 additions & 2 deletions test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,25 @@ add_evmc_tool_test(
add_evmc_tool_test(
invalid_hex_code
"--vm $<TARGET_FILE:evmc::example-vm> run 0x600"
"code: incomplete hex byte pair"
"code: \\(incomplete hex byte pair\\) OR \\(File does not exist: 0x600\\)"
)

add_evmc_tool_test(
invalid_hex_input
"--vm $<TARGET_FILE:evmc::example-vm> run 0x --input aa0y"
"--input: invalid hex digit"
"--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]"
)

get_property(TOOLS_TESTS DIRECTORY PROPERTY TESTS)
Expand Down
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
26 changes: 22 additions & 4 deletions tools/evmc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@
#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 hex string from this file.
/// @todo The file content is 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
{
HexValidator() : CLI::Validator{"HEX"}
Expand All @@ -31,10 +46,10 @@ int main(int argc, const char** argv)
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"};
Expand All @@ -43,10 +58,10 @@ int main(int argc, const char** argv)
*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, "Bytecode")->required()->check(Hex);
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, "Input bytes")->check(Hex);
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 @@ -91,6 +106,9 @@ 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);
return cmd::run(vm, rev, gas, code_hex, input_hex, create, std::cout);
}

Expand Down

0 comments on commit 2b497c7

Please sign in to comment.