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
/
debugStorage.js
104 lines (87 loc) · 3.94 KB
/
debugStorage.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
const assert = require("assert");
const bootstrap = require("../helpers/contract/bootstrap");
// Thanks solc. At least this works!
// This removes solc's overzealous uncaughtException event handler.
process.removeAllListeners("uncaughtException");
describe("Debug Storage", function() {
let options = {};
let context;
const gas = 3141592;
let hashToTrace = null;
const expectedValueBeforeTrace = "5";
before("set up web3 and contract", async() => {
this.timeout(10000);
const contractRef = {
contractFiles: ["DebugContractStorage", "DebugContract"],
contractSubdirectory: "debug"
};
context = await bootstrap(contractRef);
});
before("set up transaction that should be traced", async() => {
const { accounts, instance } = context;
options = {
from: accounts[0],
gas
};
// check initial value and initial other value to ensure we know what we are starting with
let initialValue = await instance.methods.getValue().call(options);
let initialOtherValue = await instance.methods.getOtherValue().call(options);
assert.strictEqual(initialValue, expectedValueBeforeTrace);
assert.strictEqual(initialOtherValue, expectedValueBeforeTrace);
let tx = await instance.methods.set().send(options);
hashToTrace = tx.transactionHash;
// check value and other value to see if it updated
let updatedValue = await instance.methods.getValue().call(options);
let updatedOtherValue = await instance.methods.getOtherValue().call(options);
assert.strictEqual(updatedValue, "2");
// otherValue should be the total of the initial value (5) plus 3 from calling set()
assert.strictEqual(updatedOtherValue, "8");
});
it("should successfully trace a transaction with multiple calls to an instantiated contract", async() => {
const { web3 } = context;
const provider = web3.currentProvider;
let arrayOfStorageKeyValues = [];
provider.send(
{
jsonrpc: "2.0",
method: "debug_traceTransaction",
params: [hashToTrace, []],
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, making it 6
assert.strictEqual(
arrayOfStorageKeyValues[1]["0000000000000000000000000000000000000000000000000000000000000001"],
"0000000000000000000000000000000000000000000000000000000000000006"
);
assert.strictEqual(
arrayOfStorageKeyValues[1]["0000000000000000000000000000000000000000000000000000000000000001"],
"0000000000000000000000000000000000000000000000000000000000000006"
);
// 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, making it 8
assert.strictEqual(
arrayOfStorageKeyValues[4]["0000000000000000000000000000000000000000000000000000000000000001"],
"0000000000000000000000000000000000000000000000000000000000000008"
);
}
);
});
});