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

evmc tool CLI improvements #574

merged 3 commits into from
Mar 22, 2021

Conversation

chfast
Copy link
Member

@chfast chfast commented Mar 9, 2021

For arguments code and --input we try to guess if the value is a file path or hex string. If it is a file then we load hex string from the file.

There are some gaps here still: we assume the file contains valid hex string. We could additionally inspect the file content but that requires custom ExistingFile validator.

Closes #569.

@chfast chfast requested review from axic and gumb0 March 9, 2021 10:58
return evmc::from_hex(content);
}

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!


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();
run_cmd.add_option("code", code_hex, "Bytecode")->required()->check(Hex | CLI::ExistingFile);
Copy link
Member

Choose a reason for hiding this comment

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

Cool!

@chfast chfast marked this pull request as ready for review March 22, 2021 12:03
tools/evmc/main.cpp Outdated Show resolved Hide resolved

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.
Copy link
Member

Choose a reason for hiding this comment

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

So does the file needs to be a hex string, or it is a binary file?

Copy link
Member Author

Choose a reason for hiding this comment

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

hex string

Copy link
Member

Choose a reason for hiding this comment

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

Can it be validated? Even if just validating it here with abort, I know it is not nice, but reduces confusion should someone misuse it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It still throws "invalid hex digit" exception or similar. This is caught and reported although it is not clear where the error comes from. I added test for this case.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't it coming from somewhere inside cmd::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.

It is. But I mean user will see the error about invalid hex without information about the source. He has to guess it is code and/or input what is invalid.

Copy link
Member

Choose a reason for hiding this comment

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

I think it comes from there as it uses load_hex which throws the error and main catches that. Can you leave this as a comment here (that it is caught later in evmc::run), for now?

@chfast chfast force-pushed the tools_cli branch 2 times, most recently from 966f86d to 501e9a5 Compare March 22, 2021 14:18

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?

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.

Mark run subcommand as "fallthrough" - unknown options are passed to the
main app. This allows using it as `evmc run --vm vm.so 0x00` instead of
having only fixed order `evmc --vm vm.so run 0x00`.
@chfast chfast merged commit 6cb4814 into master Mar 22, 2021
@chfast chfast deleted the tools_cli branch March 22, 2021 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add stdin (or file input) support to evmc run
3 participants