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

Commit fa45a92

Browse files
authored
Move StandardTrace class to separate file (#5836)
Move StandardTrace class to separate file
2 parents 5368421 + feb9d43 commit fa45a92

File tree

9 files changed

+203
-179
lines changed

9 files changed

+203
-179
lines changed

aleth-vm/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <libethereum/ChainParams.h>
1414
#include <libethereum/Executive.h>
1515
#include <libethereum/LastBlockHashesFace.h>
16+
#include <libethereum/StandardTrace.h>
1617
#include <libevm/VMFactory.h>
1718

1819
#include <aleth/buildinfo.h>

libethereum/Executive.cpp

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33
// Licensed under the GNU General Public License, Version 3.
44

55
#include "Executive.h"
6-
76
#include "Block.h"
87
#include "BlockChain.h"
98
#include "ExtVM.h"
109
#include "Interface.h"
10+
#include "StandardTrace.h"
1111
#include "State.h"
12-
1312
#include <libdevcore/CommonIO.h>
1413
#include <libethcore/CommonJS.h>
1514
#include <libevm/LegacyVM.h>
1615
#include <libevm/VMFactory.h>
1716

18-
#include <json/json.h>
19-
20-
#include <numeric>
21-
2217
using namespace std;
2318
using namespace dev;
2419
using namespace dev::eth;
@@ -47,129 +42,8 @@ std::string dumpStorage(ExtVM const& _ext)
4742
o << showbase << hex << i.second.first << ": " << i.second.second << "\n";
4843
return o.str();
4944
};
50-
51-
5245
} // namespace
5346

54-
StandardTrace::StandardTrace():
55-
m_trace(Json::arrayValue)
56-
{}
57-
58-
bool changesMemory(Instruction _inst)
59-
{
60-
return
61-
_inst == Instruction::MSTORE ||
62-
_inst == Instruction::MSTORE8 ||
63-
_inst == Instruction::MLOAD ||
64-
_inst == Instruction::CREATE ||
65-
_inst == Instruction::CALL ||
66-
_inst == Instruction::CALLCODE ||
67-
_inst == Instruction::SHA3 ||
68-
_inst == Instruction::CALLDATACOPY ||
69-
_inst == Instruction::CODECOPY ||
70-
_inst == Instruction::EXTCODECOPY ||
71-
_inst == Instruction::DELEGATECALL;
72-
}
73-
74-
bool changesStorage(Instruction _inst)
75-
{
76-
return _inst == Instruction::SSTORE;
77-
}
78-
79-
void StandardTrace::operator()(uint64_t _steps, uint64_t PC, Instruction inst, bigint newMemSize,
80-
bigint gasCost, bigint gas, VMFace const* _vm, ExtVMFace const* voidExt)
81-
{
82-
(void)_steps;
83-
84-
ExtVM const& ext = dynamic_cast<ExtVM const&>(*voidExt);
85-
auto vm = dynamic_cast<LegacyVM const*>(_vm);
86-
87-
Json::Value r(Json::objectValue);
88-
89-
Json::Value stack(Json::arrayValue);
90-
if (vm && !m_options.disableStack)
91-
{
92-
// Try extracting information about the stack from the VM is supported.
93-
for (auto const& i : vm->stack())
94-
stack.append(toCompactHexPrefixed(i, 1));
95-
r["stack"] = stack;
96-
}
97-
98-
bool newContext = false;
99-
Instruction lastInst = Instruction::STOP;
100-
101-
if (m_lastInst.size() == ext.depth)
102-
{
103-
// starting a new context
104-
assert(m_lastInst.size() == ext.depth);
105-
m_lastInst.push_back(inst);
106-
newContext = true;
107-
}
108-
else if (m_lastInst.size() == ext.depth + 2)
109-
{
110-
m_lastInst.pop_back();
111-
lastInst = m_lastInst.back();
112-
}
113-
else if (m_lastInst.size() == ext.depth + 1)
114-
{
115-
// continuing in previous context
116-
lastInst = m_lastInst.back();
117-
m_lastInst.back() = inst;
118-
}
119-
else
120-
{
121-
cwarn << "GAA!!! Tracing VM and more than one new/deleted stack frame between steps!";
122-
cwarn << "Attmepting naive recovery...";
123-
m_lastInst.resize(ext.depth + 1);
124-
}
125-
126-
Json::Value memJson(Json::arrayValue);
127-
if (vm && !m_options.disableMemory && (changesMemory(lastInst) || newContext))
128-
{
129-
for (unsigned i = 0; i < vm->memory().size(); i += 32)
130-
{
131-
bytesConstRef memRef(vm->memory().data() + i, 32);
132-
memJson.append(toHex(memRef));
133-
}
134-
r["memory"] = memJson;
135-
}
136-
137-
if (!m_options.disableStorage && (m_options.fullStorage || changesStorage(lastInst) || newContext))
138-
{
139-
Json::Value storage(Json::objectValue);
140-
for (auto const& i: ext.state().storage(ext.myAddress))
141-
storage[toCompactHexPrefixed(i.second.first, 1)] = toCompactHexPrefixed(i.second.second, 1);
142-
r["storage"] = storage;
143-
}
144-
145-
if (m_showMnemonics)
146-
r["op"] = instructionInfo(inst).name;
147-
r["pc"] = toString(PC);
148-
r["gas"] = toString(gas);
149-
r["gasCost"] = toString(gasCost);
150-
r["depth"] = toString(ext.depth);
151-
if (!!newMemSize)
152-
r["memexpand"] = toString(newMemSize);
153-
154-
m_trace.append(r);
155-
}
156-
157-
std::string StandardTrace::styledJson() const
158-
{
159-
return Json::StyledWriter().write(m_trace);
160-
}
161-
162-
string StandardTrace::multilineTrace() const
163-
{
164-
if (m_trace.empty())
165-
return {};
166-
167-
// Each opcode trace on a separate line
168-
return std::accumulate(std::next(m_trace.begin()), m_trace.end(),
169-
Json::FastWriter().write(m_trace[0]),
170-
[](std::string a, Json::Value b) { return a + Json::FastWriter().write(b); });
171-
}
172-
17347
Executive::Executive(Block& _s, BlockChain const& _bc, unsigned _level)
17448
: m_s(_s.mutableState()),
17549
m_envInfo(_s.info(), _bc.lastBlockHashes(), 0, _bc.chainID()),

libethereum/Executive.h

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
#pragma once
66

77
#include "Transaction.h"
8-
98
#include <libdevcore/Log.h>
109
#include <libethcore/Common.h>
11-
#include <libevm/VMFace.h>
12-
13-
#include <json/json.h>
10+
#include <libevm/ExtVMFace.h>
1411
#include <functional>
1512

16-
namespace Json
17-
{
18-
class Value;
19-
}
20-
2113
namespace dev
2214
{
2315

@@ -33,43 +25,6 @@ class ExtVM;
3325
class SealEngineFace;
3426
struct Manifest;
3527

36-
class StandardTrace
37-
{
38-
public:
39-
struct DebugOptions
40-
{
41-
bool disableStorage = false;
42-
bool disableMemory = false;
43-
bool disableStack = false;
44-
bool fullStorage = false;
45-
};
46-
47-
StandardTrace();
48-
void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
49-
bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM);
50-
51-
void setShowMnemonics() { m_showMnemonics = true; }
52-
void setOptions(DebugOptions _options) { m_options = _options; }
53-
54-
Json::Value jsonValue() const { return m_trace; }
55-
std::string styledJson() const;
56-
std::string multilineTrace() const;
57-
58-
OnOpFunc onOp()
59-
{
60-
return [=](uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
61-
bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM) {
62-
(*this)(_steps, _PC, _inst, _newMemSize, _gasCost, _gas, _vm, _extVM);
63-
};
64-
}
65-
66-
private:
67-
bool m_showMnemonics = false;
68-
std::vector<Instruction> m_lastInst;
69-
Json::Value m_trace;
70-
DebugOptions m_options;
71-
};
72-
7328
/**
7429
* @brief Message-call/contract-creation executor; useful for executing transactions.
7530
*

libethereum/StandardTrace.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Aleth: Ethereum C++ client, tools and libraries.
2+
// Copyright 2014-2019 Aleth Authors.
3+
// Licensed under the GNU General Public License, Version 3.
4+
5+
#include "StandardTrace.h"
6+
#include "ExtVM.h"
7+
#include <libevm/LegacyVM.h>
8+
#include <numeric>
9+
10+
namespace dev
11+
{
12+
namespace eth
13+
{
14+
namespace
15+
{
16+
bool changesMemory(Instruction _inst)
17+
{
18+
return _inst == Instruction::MSTORE || _inst == Instruction::MSTORE8 ||
19+
_inst == Instruction::MLOAD || _inst == Instruction::CREATE ||
20+
_inst == Instruction::CALL || _inst == Instruction::CALLCODE ||
21+
_inst == Instruction::SHA3 || _inst == Instruction::CALLDATACOPY ||
22+
_inst == Instruction::CODECOPY || _inst == Instruction::EXTCODECOPY ||
23+
_inst == Instruction::DELEGATECALL;
24+
}
25+
} // namespace
26+
27+
StandardTrace::StandardTrace() : m_trace(Json::arrayValue) {}
28+
29+
bool changesStorage(Instruction _inst)
30+
{
31+
return _inst == Instruction::SSTORE;
32+
}
33+
34+
void StandardTrace::operator()(uint64_t _steps, uint64_t PC, Instruction inst, bigint newMemSize,
35+
bigint gasCost, bigint gas, VMFace const* _vm, ExtVMFace const* voidExt)
36+
{
37+
(void)_steps;
38+
39+
ExtVM const& ext = dynamic_cast<ExtVM const&>(*voidExt);
40+
auto vm = dynamic_cast<LegacyVM const*>(_vm);
41+
42+
Json::Value r(Json::objectValue);
43+
44+
Json::Value stack(Json::arrayValue);
45+
if (vm && !m_options.disableStack)
46+
{
47+
// Try extracting information about the stack from the VM is supported.
48+
for (auto const& i : vm->stack())
49+
stack.append(toCompactHexPrefixed(i, 1));
50+
r["stack"] = stack;
51+
}
52+
53+
bool newContext = false;
54+
Instruction lastInst = Instruction::STOP;
55+
56+
if (m_lastInst.size() == ext.depth)
57+
{
58+
// starting a new context
59+
assert(m_lastInst.size() == ext.depth);
60+
m_lastInst.push_back(inst);
61+
newContext = true;
62+
}
63+
else if (m_lastInst.size() == ext.depth + 2)
64+
{
65+
m_lastInst.pop_back();
66+
lastInst = m_lastInst.back();
67+
}
68+
else if (m_lastInst.size() == ext.depth + 1)
69+
{
70+
// continuing in previous context
71+
lastInst = m_lastInst.back();
72+
m_lastInst.back() = inst;
73+
}
74+
else
75+
{
76+
cwarn << "GAA!!! Tracing VM and more than one new/deleted stack frame between steps!";
77+
cwarn << "Attmepting naive recovery...";
78+
m_lastInst.resize(ext.depth + 1);
79+
}
80+
81+
Json::Value memJson(Json::arrayValue);
82+
if (vm && !m_options.disableMemory && (changesMemory(lastInst) || newContext))
83+
{
84+
for (unsigned i = 0; i < vm->memory().size(); i += 32)
85+
{
86+
bytesConstRef memRef(vm->memory().data() + i, 32);
87+
memJson.append(toHex(memRef));
88+
}
89+
r["memory"] = memJson;
90+
}
91+
92+
if (!m_options.disableStorage &&
93+
(m_options.fullStorage || changesStorage(lastInst) || newContext))
94+
{
95+
Json::Value storage(Json::objectValue);
96+
for (auto const& i : ext.state().storage(ext.myAddress))
97+
storage[toCompactHexPrefixed(i.second.first, 1)] =
98+
toCompactHexPrefixed(i.second.second, 1);
99+
r["storage"] = storage;
100+
}
101+
102+
if (m_showMnemonics)
103+
r["op"] = instructionInfo(inst).name;
104+
r["pc"] = toString(PC);
105+
r["gas"] = toString(gas);
106+
r["gasCost"] = toString(gasCost);
107+
r["depth"] = toString(ext.depth);
108+
if (!!newMemSize)
109+
r["memexpand"] = toString(newMemSize);
110+
111+
m_trace.append(r);
112+
}
113+
114+
std::string StandardTrace::styledJson() const
115+
{
116+
return Json::StyledWriter().write(m_trace);
117+
}
118+
119+
std::string StandardTrace::multilineTrace() const
120+
{
121+
if (m_trace.empty())
122+
return {};
123+
124+
// Each opcode trace on a separate line
125+
return std::accumulate(std::next(m_trace.begin()), m_trace.end(),
126+
Json::FastWriter().write(m_trace[0]),
127+
[](std::string a, Json::Value b) { return a + Json::FastWriter().write(b); });
128+
}
129+
} // namespace eth
130+
} // namespace dev

0 commit comments

Comments
 (0)