Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Adding "codeFile" parameter into aleth-vm options #5848

Merged
merged 5 commits into from
Nov 27, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Changed: [#5807](https://github.com/ethereum/aleth/pull/5807) Optimize selfdestruct opcode in LegacyVM by reducing state accesses in certain out-of-gas scenarios.
- Changed: [#5806](https://github.com/ethereum/aleth/pull/5806) Optimize selfdestruct opcode in aleth-interpreter by reducing state accesses in certain out-of-gas scenarios.
- Changed: [#5837](https://github.com/ethereum/aleth/pull/5837) [#5839](https://github.com/ethereum/aleth/pull/5839) [#5845](https://github.com/ethereum/aleth/pull/5845) [#5846](https://github.com/ethereum/aleth/pull/5846) Output format of `testeth --jsontrace` command changed to better match output of geth's evm tool and to integrate with evmlab project.
- Changed: [#5848](https://github.com/ethereum/aleth/pull/5848) `aleth-vm --codefile <PATH>` now reads bytecode file from path and `aleth-vm --codefile - <bytecode>` now reads bytecode from standard input.
- Removed: [#5760](https://github.com/ethereum/aleth/pull/5760) Official support for Visual Studio 2015 has been dropped. Compilation with this compiler is expected to stop working after migration to C++14.
- Fixed: [#5792](https://github.com/ethereum/aleth/pull/5792) Faster and cheaper execution of RPC functions which query blockchain state (e.g. getBalance).
- Fixed: [#5811](https://github.com/ethereum/aleth/pull/5811) RPC methods querying transactions (`eth_getTransactionByHash`, `eth_getBlockByNumber`) return correct `v` value.
Expand Down
54 changes: 30 additions & 24 deletions aleth-vm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class LastBlockHashes : public eth::LastBlockHashesFace
int main(int argc, char** argv)
{
setDefaultOrCLocale();
string inputFile;
string codeFile;
Mode mode = Mode::Statistics;
State state(0);
Address sender = Address(69);
Expand All @@ -89,7 +89,7 @@ int main(int argc, char** argv)
blockHeader.setGasLimit(maxBlockGasLimit());
blockHeader.setTimestamp(0);
bytes data;
bytes code;
string code;

Ethash::init();
NoProof::init();
Expand All @@ -113,6 +113,8 @@ int main(int argc, char** argv)
addTransactionOption("input", po::value<string>(), "<d> Transaction code should be <d>");
addTransactionOption("code", po::value<string>(),
"<d> Contract code <d>. Makes transaction a call to this contract");
addTransactionOption("codefile", po::value<string>(),
"<path> File containing contract code. If '-' is specified, code is read from stdin");

po::options_description networkOptions("Network options", c_lineWidth);
networkOptions.add_options()("network", po::value<string>(),
Expand Down Expand Up @@ -145,8 +147,7 @@ int main(int argc, char** argv)
->notifier([&](int64_t _t) { blockHeader.setTimestamp(_t); }),
"<n> Set timestamp");

po::options_description allowedOptions(
"Usage ethvm <options> [trace|stats|output|test] (<file>|-)");
po::options_description allowedOptions("Usage ethvm <options> [trace|stats|output|test]");
allowedOptions.add(vmProgramOptions(c_lineWidth))
.add(networkOptions)
.add(optionsForTrace)
Expand Down Expand Up @@ -174,8 +175,6 @@ int main(int argc, char** argv)
mode = Mode::Trace;
else if (arg == "test")
mode = Mode::Test;
else if (inputFile.empty())
inputFile = arg; // Assign input file name only once.
else
{
cerr << "Unknown argument: " << arg << '\n';
Expand Down Expand Up @@ -239,29 +238,24 @@ int main(int argc, char** argv)
if (vm.count("input"))
data = fromHex(vm["input"].as<string>());
if (vm.count("code"))
code = fromHex(vm["code"].as<string>());
code = vm["code"].as<string>();
if (vm.count("codefile"))
codeFile = vm["codefile"].as<string>();

// Read code from input file.
if (!inputFile.empty())
if (!codeFile.empty())
{
if (!code.empty())
cerr << "--code argument overwritten by input file " << inputFile << '\n';

if (inputFile == "-")
for (int i = cin.get(); i != -1; i = cin.get())
code.push_back(static_cast<byte>(i));
else
code = contents(inputFile);

try // Try decoding from hex.
{
std::string strCode{reinterpret_cast<char const*>(code.data()), code.size()};
strCode.erase(strCode.find_last_not_of(" \t\n\r") + 1); // Right trim.
code = fromHex(strCode, WhenError::Throw);
cerr << "Options --code and --codefile shouldn't be used at the same time" << '\n';
return AlethErrors::ArgumentProcessingFailure;
}
catch (BadHexCharacter const&)
{
} // Ignore decoding errors.

if (codeFile == "-")
std::getline(std::cin, code);
else
code = contentsString(codeFile);
code.erase(code.find_last_not_of(" \t\n\r") + 1); // Right trim.
}

unique_ptr<SealEngineFace> se(ChainParams(genesisInfo(networkName)).createSealEngine());
Expand All @@ -275,7 +269,19 @@ int main(int argc, char** argv)
// Deploy the code on some fake account to be called later.
Account account(0, 0);
auto const latestVersion = se->evmSchedule(envInfo.number()).accountVersion;
account.setCode(bytes{code}, latestVersion);

bytes codeBytes;
try
{
codeBytes = fromHex(code, WhenError::Throw);
}
catch (BadHexCharacter const&)
{
cerr << "Provided code contains invalid characters.\n";
return AlethErrors::ArgumentProcessingFailure;
}

account.setCode(bytes{codeBytes}, latestVersion);
std::unordered_map<Address, Account> map;
map[contractDestination] = account;
state.populateFrom(map);
Expand Down