Skip to content

Commit f954a98

Browse files
committed
Panic with RESOURCE_ERROR when inserting in a full tree
1 parent 24c829a commit f954a98

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

contracts/utils/structs/MerkleTree.sol

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity ^0.8.0;
44

55
import {MerkleProof} from "../cryptography/MerkleProof.sol";
66
import {Arrays} from "../Arrays.sol";
7+
import {Panic} from "../Panic.sol";
78

89
/**
910
* @dev A complete binary tree with the ability to sequentially insert leaves, changing them from a zero to a non-zero
@@ -33,11 +34,6 @@ library MerkleTree {
3334
*/
3435
uint256 private constant MAX_DEPTH = 255;
3536

36-
/**
37-
* @dev Leaf cannot be inserted because the tree is full.
38-
*/
39-
error MerkleTreeFull();
40-
4137
/**
4238
* @dev Merkle tree cannot be initialized because requested depth is to large.
4339
*/
@@ -115,7 +111,7 @@ library MerkleTree {
115111
uint256 leafIndex = self.nextLeafIndex++;
116112

117113
// Check if tree is full.
118-
if (leafIndex == 1 << depth) revert MerkleTreeFull();
114+
if (leafIndex == 1 << depth) Panic.panic(Panic.RESOURCE_ERROR);
119115

120116
// Rebuild branch from leaf to root
121117
uint256 currentIndex = leafIndex;

test/utils/structs/Merkletree.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { ethers } = require('hardhat');
22
const { expect } = require('chai');
33
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
4+
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
45
const { StandardMerkleTree } = require('@openzeppelin/merkle-tree');
56

67
const makeTree = (leafs = [ethers.ZeroHash]) =>
@@ -32,7 +33,7 @@ describe('Merklee tree', function () {
3233
});
3334

3435
it('setup', async function () {
35-
const merkleTree = makeTree(Array(2 ** Number(DEPTH)).fill(ethers.ZeroHash));
36+
const merkleTree = makeTree(Array.from({ length: 2 ** Number(DEPTH) }, () => ethers.ZeroHash));
3637

3738
expect(await this.mock.getDepth()).to.equal(DEPTH);
3839
expect(await this.mock.getLength()).to.equal(LENGTH);
@@ -50,7 +51,7 @@ describe('Merklee tree', function () {
5051

5152
describe('insert', function () {
5253
it('tree is correctly updated', async function () {
53-
const leafs = Array(2 ** Number(DEPTH)).fill(ethers.ZeroHash);
54+
const leafs = Array.from({ length: 2 ** Number(DEPTH) }, () => ethers.ZeroHash);
5455
const roots = [];
5556

5657
// for each leaf slot
@@ -81,10 +82,9 @@ describe('Merklee tree', function () {
8182
});
8283

8384
it('revert when tree is full', async function () {
84-
for (let i = 0; i < 2 ** Number(DEPTH); ++i) {
85-
await this.mock.insert(ethers.ZeroHash);
86-
}
87-
await expect(this.mock.insert(ethers.ZeroHash)).to.be.revertedWithCustomError(this.mock, 'MerkleTreeFull');
85+
await Promise.all(Array.from({ length: 2 ** Number(DEPTH) }).map(() => this.mock.insert(ethers.ZeroHash)));
86+
87+
await expect(this.mock.insert(ethers.ZeroHash)).to.be.revertedWithPanic(PANIC_CODES.TOO_MUCH_MEMORY_ALLOCATED);
8888
});
8989
});
9090
});

0 commit comments

Comments
 (0)