Skip to content

Commit 3bb20aa

Browse files
committed
add IncrementalMerkleTree at function and revert tests
1 parent 65f824d commit 3bb20aa

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

contracts/data/IncrementalMerkleTree.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ library IncrementalMerkleTree {
4545
}
4646
}
4747

48+
function at(Tree storage t, uint256 index)
49+
internal
50+
view
51+
returns (bytes32 hash)
52+
{
53+
hash = t.nodes[0][index];
54+
}
55+
4856
/**
4957
* @notice add new element to tree
5058
* @param t Tree struct storage reference

contracts/data/IncrementalMerkleTreeMock.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ contract IncrementalMerkleTreeMock {
2323
return tree.root();
2424
}
2525

26+
function at(uint256 index) external view returns (bytes32) {
27+
return tree.at(index);
28+
}
29+
2630
function push(bytes32 hash) external {
2731
tree.push(hash);
2832
}

test/data/IncrementalMerkleTree.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PANIC_CODES } from '@nomicfoundation/hardhat-chai-matchers/panic';
12
import {
23
IncrementalMerkleTreeMock,
34
IncrementalMerkleTreeMock__factory,
@@ -96,6 +97,24 @@ describe('IncrementalMerkleTree', function () {
9697
});
9798
});
9899

100+
describe('#at', () => {
101+
it('returns element at given index', async () => {
102+
const hash = randomHash();
103+
104+
await instance.push(hash);
105+
106+
expect(await instance.callStatic.at(0)).to.equal(hash);
107+
});
108+
109+
describe('reverts if', () => {
110+
it('index is out of bounds', async () => {
111+
await expect(instance.callStatic.at(0)).to.be.revertedWithPanic(
112+
PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
113+
);
114+
});
115+
});
116+
});
117+
99118
describe('#push', () => {
100119
it('updates Merkle root', async () => {
101120
const hashes = [];
@@ -135,5 +154,13 @@ describe('IncrementalMerkleTree', function () {
135154
expect(await instance.callStatic.root()).to.equal(tree.getHexRoot());
136155
}
137156
});
157+
158+
describe('reverts if', () => {
159+
it('index is out of bounds', async () => {
160+
await expect(
161+
instance.callStatic.set(0, ethers.constants.HashZero),
162+
).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
163+
});
164+
});
138165
});
139166
});

0 commit comments

Comments
 (0)