This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 677
/
debug.js
286 lines (243 loc) · 10.4 KB
/
debug.js
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
const assert = require("assert");
const bootstrap = require("../helpers/contract/bootstrap");
const { promisify } = require("util");
var Ganache = require(process.env.TEST_BUILD
? "../../build/ganache.core." + process.env.TEST_BUILD + ".js"
: "../../index.js");
// Thanks solc. At least this works!
// This removes solc's overzealous uncaughtException event handler.
process.removeAllListeners("uncaughtException");
function test(forked) {
let context;
const mnemonic = "sweet candy treat";
const gas = 3141592;
let hashToTrace = null;
let multipleCallsHashToTrace = null;
const expectedValueBeforeTrace = "1234";
const val = "26";
const targetPort = 21345;
const forkedTargetUrl = "ws://localhost:" + targetPort;
let forkedTransactionHash;
let mainContext;
const contractRef = {
contractFiles: ["DebugContract"],
contractSubdirectory: "debug"
};
// steps:
/*
setValue(26)
this sets .value to 26 and otherValue to 31 (26 + 5)
then setValue(1234)
this sets .value to 1234 and otherValue to 1265 (31 + 1234)
then trace the first tx
we want to make sure the data set by the traced transaction:
26 and 31.
and that it didn't modify the original data
*/
if (forked) {
before("init forkedServer", async function() {
this.timeout(10000);
const contractRef = {
contractFiles: ["DebugContract"],
contractSubdirectory: "debug"
};
let forkedServer = Ganache.server({ mnemonic });
await promisify(forkedServer.listen)(targetPort);
mainContext = await bootstrap(contractRef, {
provider: forkedServer.provider,
mnemonic
});
mainContext.server = forkedServer;
});
before("set up transaction that should be traced", async() => {
const { accounts, instance } = mainContext;
const options = { from: accounts[0], gas };
const result = await instance.methods.setValue(val).send(options);
forkedTransactionHash = result.transactionHash;
});
after("shutdown forkedServer", () => {
mainContext.server.close();
});
}
before("set up web3 and contract", async function() {
this.timeout(10000);
// forked = false;
const options = forked
? {
fork: forkedTargetUrl.replace("ws", "http"),
unlocked_accounts: mainContext.accounts
}
: { mnemonic };
context = await bootstrap(contractRef, options);
});
describe("Trace a successful transaction", function() {
let options;
let originalStoredBlockNumber;
let latestStoredBlockNumber;
let latestBlockNumber;
before("set up transaction that should be traced", async() => {
const { accounts, instance } = context;
options = { from: accounts[0], gas };
const tx = await instance.methods.setValue(val).send(options);
// set hashToTrace to the tx we made, so we know preconditions are correctly set
hashToTrace = tx.transactionHash;
});
it("sets the blockNumber in storage", async() => {
const { instance, web3 } = context;
// check the value is what we expect it to be: 26
originalStoredBlockNumber = await instance.methods.currentBlock().call(options);
const blockNumber = await web3.eth.getBlockNumber();
assert.strictEqual(parseInt(originalStoredBlockNumber, 10), blockNumber);
});
it("sets the value to 26", async() => {
const { instance } = context;
// check the value is what we expect it to be: 26
const value = await instance.methods.value().call(options);
assert.strictEqual(value, val);
});
it("changes state of contract to ensure trace doesn't overwrite data", async() => {
const { accounts, instance, web3 } = context;
options = { from: accounts[0], gas };
await instance.methods.setValue(expectedValueBeforeTrace).send(options);
latestStoredBlockNumber = await instance.methods.currentBlock().call(options);
latestBlockNumber = await web3.eth.getBlockNumber();
// check the value is what we expect it to be: 1234
const value = await instance.methods.value().call(options);
assert.strictEqual(value, expectedValueBeforeTrace);
});
it("should trace a successful transaction without changing state", async function() {
// We want to trace the transaction that sets the value to 26
const { accounts, instance, send, web3 } = context;
const response = await send("debug_traceTransaction", hashToTrace, []);
if (response.error) {
assert.fail(response.error);
}
const structLogs = response.result.structLogs;
// To at least assert SOMETHING, let's assert the last opcode
assert(structLogs.length > 0);
for (let op of structLogs) {
if (op.stack.length > 0) {
// check formatting of stack - it was broken when updating to ethereumjs-vm v2.3.3
assert.strictEqual(op.stack[0].length, 64);
assert.notStrictEqual(op.stack[0].substr(0, 2), "0x");
break;
}
}
const lastop = structLogs[structLogs.length - 1];
assert.strictEqual(lastop.op, "STOP");
assert.strictEqual(lastop.gasCost, 1);
assert.strictEqual(lastop.pc, 220);
assert.strictEqual(
lastop.storage["0000000000000000000000000000000000000000000000000000000000000000"],
"000000000000000000000000000000000000000000000000000000000000001a"
);
assert.strictEqual(
lastop.storage["0000000000000000000000000000000000000000000000000000000000000001"],
"000000000000000000000000000000000000000000000000000000000000001f"
);
assert.strictEqual(
lastop.storage["0000000000000000000000000000000000000000000000000000000000000002"],
originalStoredBlockNumber.padStart(64, "0")
);
console.log("--------------------------------------------------");
const value = await instance.methods.value().call({ from: accounts[0], gas });
assert.strictEqual(value, expectedValueBeforeTrace);
const otherValue = await instance.methods.otherValue().call({ from: accounts[0], gas });
assert.strictEqual(otherValue, "1265");
// stored block number should not have changed:
const storedBlockNumber = await instance.methods.currentBlock().call({ from: accounts[0], gas });
assert.strictEqual(storedBlockNumber, latestStoredBlockNumber);
// block number should not have incremented because of `debug_traceTransaction`
const currentBlockNumber = await web3.eth.getBlockNumber();
assert.strictEqual(currentBlockNumber, latestBlockNumber);
});
});
if (forked) {
describe("Trace a transaction on the main chain through the forked chain", function() {
before("Increment nonce on original chain", async() => {
// we need to make sure we trace the transaction at the correct block number
// with the right state root. By incrementing the nonce we can ensure
// we don't try to read off the main chain at after the fork.
const { send, accounts } = mainContext;
// increment the account's nonce on the main chain by sending a transaction
await send("eth_sendTransaction", {
from: accounts[0],
to: accounts[1],
value: 100,
gas
});
});
it("traces it", async() => {
const result = await context.send("debug_traceTransaction", forkedTransactionHash, []);
assert(result, "Result should be defined");
});
});
}
describe("Trace a successful transaction with multiple calls", function() {
let options;
before("set up transaction with multiple calls to the same contract to be traced", async() => {
const { accounts, instance } = context;
options = { from: accounts[0], gas };
// from previous tests, otherValue should be 26 + 1234
let otherValue = await instance.methods.otherValue().call(options);
assert.strictEqual(otherValue, "1265");
let tx = await instance.methods.callSetValueTwice().send(options);
multipleCallsHashToTrace = tx.transactionHash;
// we add 1 + 2 to otherValue, so now it should be 1268
let updatedValue = await instance.methods.otherValue().call(options);
assert.strictEqual(updatedValue, "1268");
});
it("should trace a transaction with multiple calls to the same contract", function(done) {
const { web3 } = context;
const provider = web3.currentProvider;
let arrayOfStorageKeyValues = [];
provider.send(
{
jsonrpc: "2.0",
method: "debug_traceTransaction",
params: [multipleCallsHashToTrace, []],
id: new Date().getTime()
},
function(_, result) {
for (var i = 0; i < result.result.structLogs.length; i++) {
let op = result.result.structLogs[i];
let nextOp = result.result.structLogs[i + 1];
if (op.op === "SSTORE") {
// we want the nextOp because the storage changes doesn't take affect until after the SSTORE opcode
arrayOfStorageKeyValues.push(nextOp.storage);
}
}
// ensure the call to setValue with 1 was successfully stored for value
assert.strictEqual(
arrayOfStorageKeyValues[0]["0000000000000000000000000000000000000000000000000000000000000000"],
"0000000000000000000000000000000000000000000000000000000000000001"
);
// ensure the call to setValue with 1 was successfully stored for otherValue
assert.strictEqual(
arrayOfStorageKeyValues[1]["0000000000000000000000000000000000000000000000000000000000000001"],
"00000000000000000000000000000000000000000000000000000000000004f2"
);
// ensure the call to setValue with 2 was successfully stored for value
assert.strictEqual(
arrayOfStorageKeyValues[3]["0000000000000000000000000000000000000000000000000000000000000000"],
"0000000000000000000000000000000000000000000000000000000000000002"
);
// ensure the call to setValue with 2 was successfully stored for otherValue
assert.strictEqual(
arrayOfStorageKeyValues[4]["0000000000000000000000000000000000000000000000000000000000000001"],
"00000000000000000000000000000000000000000000000000000000000004f4"
);
done();
}
);
});
});
}
describe("Debug", function() {
describe("Direct", function() {
test();
});
describe("Forked", function() {
test(true);
});
});