This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
main.cpp
388 lines (354 loc) · 13.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Aleth: Ethereum C++ client, tools and libraries.
// Copyright 2015-2019 Aleth Authors.
// Licensed under the GNU General Public License, Version 3.
#include <libdevcore/CommonIO.h>
#include <libdevcore/LoggingProgramOptions.h>
#include <libdevcore/SHA3.h>
#include <libethashseal/Ethash.h>
#include <libethashseal/GenesisInfo.h>
#include <libethcore/Common.h>
#include <libethcore/SealEngine.h>
#include <libethereum/Block.h>
#include <libethereum/ChainParams.h>
#include <libethereum/Executive.h>
#include <libethereum/LastBlockHashesFace.h>
#include <libethereum/StandardTrace.h>
#include <libevm/VMFactory.h>
#include <aleth/buildinfo.h>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <ctime>
#include <fstream>
#include <iostream>
using namespace std;
using namespace dev;
using namespace eth;
namespace po = boost::program_options;
namespace
{
int64_t maxBlockGasLimit()
{
static int64_t limit =
ChainParams(genesisInfo(Network::MainNetwork)).maxGasLimit.convert_to<int64_t>();
return limit;
}
void version()
{
const auto* buildinfo = aleth_get_buildinfo();
cout << "aleth-vm " << buildinfo->project_version << "\n";
cout << "Build: " << buildinfo->system_name << "/" << buildinfo->build_type << "\n";
exit(AlethErrors::Success);
}
enum class Mode
{
Trace,
Statistics,
OutputOnly,
/// Test mode -- output information needed for test verification and
/// benchmarking. The execution is not introspected not to degrade
/// performance.
Test
};
}
class LastBlockHashes : public eth::LastBlockHashesFace
{
public:
h256s precedingHashes(h256 const& /* _mostRecentHash */) const override
{
return h256s(256, h256());
}
void clear() override {}
};
int main(int argc, char** argv)
{
setDefaultOrCLocale();
string codeFile;
Mode mode = Mode::Statistics;
State state(0);
Address sender = Address(69);
Address origin = Address(69);
u256 value = 0;
u256 gas = maxBlockGasLimit();
u256 gasPrice = 0;
bool styledJson = true;
Json::Value traceJson{Json::arrayValue};
StandardTrace st{traceJson};
Network networkName = Network::MainNetworkTest;
BlockHeader blockHeader; // fake block to be executed in
blockHeader.setGasLimit(maxBlockGasLimit());
blockHeader.setTimestamp(0);
bytes data;
string code;
Ethash::init();
NoProof::init();
NoReward::init();
po::options_description transactionOptions("Transaction options", c_lineWidth);
string const gasLimitDescription =
"<n> Block gas limit (default: " + to_string(maxBlockGasLimit()) + ").";
auto addTransactionOption = transactionOptions.add_options();
addTransactionOption(
"value", po::value<u256>(), "<n> Transaction should transfer the <n> wei (default: 0).");
addTransactionOption("gas", po::value<u256>(),
"<n> Transaction should be given <n> gas (default: block gas limit).");
addTransactionOption("gas-limit", po::value<u256>(), gasLimitDescription.c_str());
addTransactionOption(
"gas-price", po::value<u256>(), "<n> Transaction's gas price' should be <n> (default: 0).");
addTransactionOption("sender", po::value<Address>(),
"<a> Transaction sender should be <a> (default: 0000...0069).");
addTransactionOption("origin", po::value<Address>(),
"<a> Transaction origin should be <a> (default: 0000...0069).");
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>(),
"Main|Ropsten|Homestead|Frontier|Byzantium|Constantinople|ConstantinopleFix|Istanbul\n");
po::options_description optionsForTrace("Options for trace", c_lineWidth);
auto addTraceOption = optionsForTrace.add_options();
addTraceOption("flat", "Minimal whitespace in the JSON.");
addTraceOption("mnemonics", "Show instruction mnemonics in the trace (non-standard).\n");
LoggingOptions loggingOptions;
po::options_description loggingProgramOptions(
createLoggingProgramOptions(c_lineWidth, loggingOptions));
po::options_description generalOptions("General options", c_lineWidth);
auto addGeneralOption = generalOptions.add_options();
addGeneralOption("version,v", "Show the version and exit.");
addGeneralOption("help,h", "Show this help message and exit.");
addGeneralOption("author", po::value<Address>(), "<a> Set author");
addGeneralOption("difficulty", po::value<u256>(), "<n> Set difficulty");
addGeneralOption("number",
po::value<int64_t>()->default_value(0)->value_name("<number>")->notifier([&](int64_t _n) {
blockHeader.setNumber(_n);
}),
"<n> Set number");
addGeneralOption("timestamp",
po::value<int64_t>()
->default_value(0)
->value_name("<timestamp>")
->notifier([&](int64_t _t) { blockHeader.setTimestamp(_t); }),
"<n> Set timestamp");
po::options_description allowedOptions("Usage ethvm <options> [trace|stats|output|test]");
allowedOptions.add(vmProgramOptions(c_lineWidth))
.add(networkOptions)
.add(optionsForTrace)
.add(loggingProgramOptions)
.add(generalOptions)
.add(transactionOptions);
po::parsed_options parsed =
po::command_line_parser(argc, argv).options(allowedOptions).allow_unregistered().run();
vector<string> unrecognisedOptions =
collect_unrecognized(parsed.options, po::include_positional);
po::variables_map vm;
po::store(parsed, vm);
po::notify(vm);
setupLogging(loggingOptions);
// handling mode and input file options separately, as they don't have option name
for (auto const& arg : unrecognisedOptions)
{
if (arg == "stats")
mode = Mode::Statistics;
else if (arg == "output")
mode = Mode::OutputOnly;
else if (arg == "trace")
mode = Mode::Trace;
else if (arg == "test")
mode = Mode::Test;
else
{
cerr << "Unknown argument: " << arg << '\n';
return AlethErrors::UnknownArgument;
}
}
if (vm.count("help"))
{
cout << allowedOptions;
return AlethErrors::Success;
}
if (vm.count("version"))
{
version();
}
if (vm.count("mnemonics"))
st.setShowMnemonics();
if (vm.count("flat"))
styledJson = false;
if (vm.count("sender"))
sender = vm["sender"].as<Address>();
if (vm.count("origin"))
origin = vm["origin"].as<Address>();
if (vm.count("gas"))
gas = vm["gas"].as<u256>();
if (vm.count("gas-price"))
gasPrice = vm["gas-price"].as<u256>();
if (vm.count("author"))
blockHeader.setAuthor(vm["author"].as<Address>());
if (vm.count("difficulty"))
blockHeader.setDifficulty(vm["difficulty"].as<u256>());
if (vm.count("gas-limit"))
blockHeader.setGasLimit((vm["gas-limit"].as<u256>()).convert_to<int64_t>());
if (vm.count("value"))
value = vm["value"].as<u256>();
if (vm.count("network"))
{
string network = vm["network"].as<string>();
if (network == "Istanbul")
networkName = Network::IstanbulTest;
else if (network == "ConstantinopleFix")
networkName = Network::ConstantinopleFixTest;
else if (network == "Constantinople")
networkName = Network::ConstantinopleTest;
else if (network == "Byzantium")
networkName = Network::ByzantiumTest;
else if (network == "Frontier")
networkName = Network::FrontierTest;
else if (network == "Ropsten")
networkName = Network::Ropsten;
else if (network == "Homestead")
networkName = Network::HomesteadTest;
else if (network == "Main")
networkName = Network::MainNetwork;
else
{
cerr << "Unknown network type: " << network << "\n";
return AlethErrors::UnknownNetworkType;
}
}
if (vm.count("input"))
data = fromHex(vm["input"].as<string>());
if (vm.count("code"))
code = vm["code"].as<string>();
if (vm.count("codefile"))
codeFile = vm["codefile"].as<string>();
// Read code from input file.
if (!codeFile.empty())
{
if (!code.empty())
{
cerr << "Options --code and --codefile shouldn't be used at the same time" << '\n';
return AlethErrors::ArgumentProcessingFailure;
}
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());
LastBlockHashes lastBlockHashes;
EnvInfo const envInfo(blockHeader, lastBlockHashes, 0 /* gasUsed */, se->chainParams().chainID);
Transaction t;
Address contractDestination("1122334455667788991011121314151617181920");
if (!code.empty())
{
// Deploy the code on some fake account to be called later.
Account account(0, 0);
auto const latestVersion = se->evmSchedule(envInfo.number()).accountVersion;
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);
t = Transaction(value, gasPrice, gas, contractDestination, data, 0);
}
else
// If not code provided construct "create" transaction out of the input
// data.
t = Transaction(value, gasPrice, gas, data, 0);
state.addBalance(sender, value);
Executive executive(state, envInfo, *se);
ExecutionResult res;
executive.setResultRecipient(res);
t.forceSender(sender);
unordered_map<byte, pair<unsigned, bigint>> counts;
unsigned total = 0;
bigint memTotal;
executive.initialize(t);
if (!code.empty())
executive.call(contractDestination, sender, value, gasPrice, &data, gas);
else
executive.create(sender, value, gasPrice, gas, &data, origin);
OnOpFunc onOp;
if (mode == Mode::Statistics)
{
onOp = [&](uint64_t, uint64_t, Instruction inst, bigint m, bigint gasCost, bigint,
VMFace const*, ExtVMFace const*) {
byte b = static_cast<byte>(inst);
counts[b].first++;
counts[b].second += gasCost;
total++;
if (m > memTotal)
memTotal = m;
};
}
else if (mode == Mode::Trace)
onOp = st.onOp();
Timer timer;
executive.go(onOp);
double execTime = timer.elapsed();
executive.finalize();
bytes output = std::move(res.output);
if (mode == Mode::Statistics)
{
cout << "Gas used: " << res.gasUsed << " (+"
<< t.baseGasRequired(se->evmSchedule(envInfo.number())) << " for transaction, -"
<< res.gasRefunded << " refunded)\n";
cout << "Output: " << toHex(output) << "\n";
LogEntries logs = executive.logs();
cout << logs.size() << " logs" << (logs.empty() ? "." : ":") << "\n";
for (LogEntry const& l : logs)
{
cout << " " << l.address.hex() << ": " << toHex(t.data()) << "\n";
for (h256 const& topic : l.topics)
cout << " " << topic.hex() << "\n";
}
cout << total << " operations in " << execTime << " seconds.\n";
cout << "Maximum memory usage: " << memTotal * 32 << " bytes\n";
cout << "Expensive operations:\n";
for (auto const inst : {Instruction::SSTORE, Instruction::SLOAD, Instruction::CALL,
Instruction::CREATE, Instruction::CALLCODE, Instruction::DELEGATECALL,
Instruction::MSTORE8, Instruction::MSTORE, Instruction::MLOAD, Instruction::SHA3})
{
auto const& count = counts[static_cast<byte>(inst)];
if (count.first != 0)
cout << " " << instructionInfo(inst).name << " x " << count.first << " ("
<< count.second << " gas)\n";
}
}
else if (mode == Mode::Trace)
{
if (styledJson)
cout << Json::StyledWriter().write(traceJson);
else
{
Json::FastWriter writer;
for (auto const& traceOp : traceJson)
cout << writer.write(traceOp);
}
}
else if (mode == Mode::OutputOnly)
cout << toHex(output) << '\n';
else if (mode == Mode::Test)
{
// Output information needed for test verification and benchmarking
// in YAML-like dictionaly format.
auto exception = res.excepted != TransactionException::None;
cout << "output: '" << toHex(output) << "'\n";
cout << "exception: " << boolalpha << exception << '\n';
cout << "gas used: " << res.gasUsed << '\n';
cout << "gas/sec: " << scientific << setprecision(3) << uint64_t(res.gasUsed) / execTime
<< '\n';
cout << "exec time: " << fixed << setprecision(6) << execTime << '\n';
}
return AlethErrors::Success;
}