Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 945912c

Browse files
authored
Merge pull request #5385 from trufflesuite/flatted-events
Enhancement: Add `flattedEvents` selector to debugger
2 parents edae619 + 41adae7 commit 945912c

File tree

2 files changed

+120
-8
lines changed

2 files changed

+120
-8
lines changed

packages/debugger/lib/txlog/selectors/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,49 @@ let txlog = createSelectorTree({
437437
}
438438
: node;
439439
return tie(log[""]); //"" is always the root node
440+
}),
441+
442+
/**
443+
* txlog.views.flattedEvents
444+
*/
445+
flattedEvents: createLeaf(["./transactionLog"], log => {
446+
const getFlattedEvents = (node, address, codeAddress, status) => {
447+
switch (node.type) {
448+
case "transaction":
449+
return node.actions.flatMap(subNode =>
450+
getFlattedEvents(subNode, node.origin, node.origin, status)
451+
);
452+
case "callexternal":
453+
const subNodeStatus = !["revert", "unwind"].includes(
454+
node.returnKind
455+
);
456+
return node.actions.flatMap(subNode =>
457+
getFlattedEvents(
458+
subNode,
459+
node.isDelegate ? address : node.address,
460+
node.address,
461+
status && subNodeStatus
462+
)
463+
);
464+
case "callinternal":
465+
return node.actions.flatMap(subNode =>
466+
getFlattedEvents(subNode, address, codeAddress, status)
467+
);
468+
case "event":
469+
return [
470+
{
471+
decoding: node.decoding,
472+
raw: node.raw,
473+
address,
474+
codeAddress,
475+
status
476+
}
477+
];
478+
default:
479+
return [];
480+
}
481+
};
482+
return getFlattedEvents(log, null, null, true);
440483
})
441484
}
442485
});

packages/debugger/test/txlog.js

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ contract VizTest is Nothing {
3030
event TakesArgs(uint indexed x, uint y);
3131
event Confusing(uint a, bytes32 indexed b, uint c);
3232
33+
Tertiary tert;
34+
3335
function testCall(uint x) public returns (uint y) {
3436
return called(x);
3537
}
@@ -70,7 +72,16 @@ contract VizTest is Nothing {
7072
VizLibrary.confuse();
7173
}
7274
73-
constructor() payable {
75+
constructor(Tertiary _tert) payable {
76+
tert = _tert;
77+
}
78+
79+
function testFlatEvents() public {
80+
testCall(1);
81+
try tert.noisyFail() {
82+
} catch (bytes memory) {
83+
}
84+
testCall(1);
7485
}
7586
}
7687
@@ -97,6 +108,17 @@ contract Secondary {
97108
}
98109
}
99110
111+
contract Tertiary {
112+
113+
event Tert();
114+
115+
function noisyFail() public {
116+
emit Tert();
117+
VizLibrary.loudIncrement(1);
118+
revert();
119+
}
120+
}
121+
100122
library VizLibrary {
101123
event Noise();
102124
event Confusing(uint x, bytes32 y, uint indexed z);
@@ -117,13 +139,17 @@ let sources = {
117139
};
118140

119141
const __MIGRATION = `
120-
let VizTest = artifacts.require("VizTest");
121-
let VizLibrary = artifacts.require("VizLibrary");
122-
123-
module.exports = function(deployer) {
124-
deployer.deploy(VizLibrary);
125-
deployer.link(VizLibrary, VizTest);
126-
deployer.deploy(VizTest, { value: 100 });
142+
const VizTest = artifacts.require("VizTest");
143+
const VizLibrary = artifacts.require("VizLibrary");
144+
const Tertiary = artifacts.require("Tertiary");
145+
146+
module.exports = async function(deployer) {
147+
await deployer.deploy(VizLibrary);
148+
await deployer.link(VizLibrary, Tertiary);
149+
await deployer.deploy(Tertiary);
150+
const tert = await Tertiary.deployed();
151+
await deployer.link(VizLibrary, VizTest);
152+
await deployer.deploy(VizTest, tert.address, { value: 100 });
127153
};
128154
`;
129155

@@ -624,6 +650,49 @@ describe("Transaction log (visualizer)", function () {
624650
); //683 in hex
625651
});
626652

653+
it("Correctly flattens events", async function () {
654+
this.timeout(12000);
655+
const instance = await abstractions.VizTest.deployed();
656+
const tertInstance = await abstractions.Tertiary.deployed();
657+
const libInstance = await abstractions.VizLibrary.deployed();
658+
const receipt = await instance.testFlatEvents();
659+
const txHash = receipt.tx;
660+
661+
const bugger = await Debugger.forTx(txHash, {
662+
provider,
663+
compilations
664+
});
665+
666+
await bugger.runToEnd();
667+
668+
const flattedEvents = bugger.view(txlog.views.flattedEvents);
669+
assert.lengthOf(flattedEvents, 4);
670+
//event #0: Dummy()
671+
let event = flattedEvents[0];
672+
assert.strictEqual(event.decoding.abi.name, "Dummy");
673+
assert.strictEqual(event.address, instance.address);
674+
assert.strictEqual(event.codeAddress, instance.address);
675+
assert.isTrue(event.status);
676+
//event #1: Tert()
677+
event = flattedEvents[1];
678+
assert.strictEqual(event.decoding.abi.name, "Tert");
679+
assert.strictEqual(event.address, tertInstance.address);
680+
assert.strictEqual(event.codeAddress, tertInstance.address);
681+
assert.isFalse(event.status);
682+
//event #2: Noise()
683+
event = flattedEvents[2];
684+
assert.strictEqual(event.decoding.abi.name, "Noise");
685+
assert.strictEqual(event.address, tertInstance.address);
686+
assert.strictEqual(event.codeAddress, libInstance.address);
687+
assert.isFalse(event.status);
688+
//event #3: Dummy()
689+
event = flattedEvents[3];
690+
assert.strictEqual(event.decoding.abi.name, "Dummy");
691+
assert.strictEqual(event.address, instance.address);
692+
assert.strictEqual(event.codeAddress, instance.address);
693+
assert.isTrue(event.status);
694+
});
695+
627696
it("Correctly logs an event inside a library", async function () {
628697
this.timeout(12000);
629698
const instance = await abstractions.VizTest.deployed();

0 commit comments

Comments
 (0)