Skip to content

Commit cba6980

Browse files
committed
remove unnecessary pop, test against state corruption
1 parent 1daeaa4 commit cba6980

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

contracts/data/IncrementalMerkleTree.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ library IncrementalMerkleTree {
9595

9696
if (treeSize == (1 << treeHeight) >> 2) {
9797
treeHeight--;
98-
// TODO: is this pop necessary, or will the row be properly deleted?
99-
t.nodes[treeHeight].pop();
10098
t.nodes.pop();
10199
}
102100

test/data/IncrementalMerkleTree.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ describe('IncrementalMerkleTree', function () {
9595

9696
expect(await instance.callStatic.root()).to.equal(hash);
9797
});
98+
99+
it('returns result matching reference implementation regardless of previous operations', async () => {
100+
const count = 5;
101+
const hashes = [];
102+
103+
for (let i = 0; i < count; i++) {
104+
hashes.push(randomHash());
105+
await instance.push(hashes[i]);
106+
}
107+
108+
for (let i = 0; i < count; i++) {
109+
await instance.push(randomHash());
110+
}
111+
112+
for (let i = 0; i < count; i++) {
113+
await instance.pop();
114+
}
115+
116+
for (let i = 0; i < count; i++) {
117+
hashes.push(randomHash());
118+
await instance.push(hashes[count + i]);
119+
120+
const tree = new MerkleTree(hashes, keccak256);
121+
122+
expect(await instance.callStatic.root()).to.equal(tree.getHexRoot());
123+
}
124+
});
98125
});
99126

100127
describe('#at', () => {

0 commit comments

Comments
 (0)