Skip to content

Commit 73dda7d

Browse files
authored
♻️ Refactor MerkleTreeLib (#1440)
1 parent 8c5dce1 commit 73dda7d

File tree

3 files changed

+183
-149
lines changed

3 files changed

+183
-149
lines changed

docs/utils/merkletreelib.md

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Library for generating Merkle trees.
55

66
<b>Note:</b>
77

8-
- Leafs are NOT auto hashed. Note that some libraries hash the leafs by default.
8+
- Leaves are NOT auto hashed. Note that some libraries hash the leaves by default.
99
We leave it up to you to decide if this is needed.
10-
If your leafs are 64 bytes long, do hash them first for safety.
10+
If your leaves are 64 bytes long, do hash them first for safety.
1111
See: https://www.rareskills.io/post/merkle-tree-second-preimage-attack
12-
- Leafs are NOT auto globally sorted. Note that some libraries sort the leafs by default.
12+
- Leaves are NOT auto globally sorted. Note that some libraries sort the leaves by default.
1313
- The pair hash is pair-sorted-keccak256, which works out-of-the-box with `MerkleProofLib`.
1414
- This library is NOT equivalent to OpenZeppelin or Murky.
1515
Equivalence is NOT required if you are just using this for pure Solidity testing.
@@ -21,10 +21,10 @@ May be relevant for differential testing between Solidity vs external libraries.
2121

2222
## Custom Errors
2323

24-
### MerkleTreeLeafsEmpty()
24+
### MerkleTreeLeavesEmpty()
2525

2626
```solidity
27-
error MerkleTreeLeafsEmpty()
27+
error MerkleTreeLeavesEmpty()
2828
```
2929

3030
At least 1 leaf is required to build the tree.
@@ -36,7 +36,7 @@ error MerkleTreeOutOfBoundsAccess()
3636
```
3737

3838
Attempt to access a node with an out-of-bounds index.
39-
Check if the tree has been built and has sufficient leafs and nodes.
39+
Check if the tree has been built and has sufficient leaves and nodes.
4040

4141
### MerkleTreeInvalidLeafIndices()
4242

@@ -51,35 +51,38 @@ Leaf indices for multi proof must be strictly ascending and not empty.
5151
### build(bytes32[])
5252

5353
```solidity
54-
function build(bytes32[] memory leafs)
54+
function build(bytes32[] memory leaves)
5555
internal
5656
pure
57-
returns (bytes32[] memory result)
57+
returns (bytes32[] memory tree)
5858
```
5959

6060
Builds and return a complete Merkle tree.
61-
To make it a full Merkle tree, use `build(pad(leafs))`.
61+
To make it a full Merkle tree, use `build(pad(leaves))`.
6262

6363
### root(bytes32[])
6464

6565
```solidity
66-
function root(bytes32[] memory t) internal pure returns (bytes32 result)
66+
function root(bytes32[] memory tree)
67+
internal
68+
pure
69+
returns (bytes32 result)
6770
```
6871

6972
Returns the root.
7073

71-
### numLeafs(bytes32[])
74+
### numLeaves(bytes32[])
7275

7376
```solidity
74-
function numLeafs(bytes32[] memory t) internal pure returns (uint256)
77+
function numLeaves(bytes32[] memory tree) internal pure returns (uint256)
7578
```
7679

77-
Returns the number of leafs.
80+
Returns the number of leaves.
7881

7982
### numInternalNodes(bytes32[])
8083

8184
```solidity
82-
function numInternalNodes(bytes32[] memory t)
85+
function numInternalNodes(bytes32[] memory tree)
8386
internal
8487
pure
8588
returns (uint256)
@@ -90,18 +93,29 @@ Returns the number of internal nodes.
9093
### leaf(bytes32[],uint256)
9194

9295
```solidity
93-
function leaf(bytes32[] memory t, uint256 leafIndex)
96+
function leaf(bytes32[] memory tree, uint256 leafIndex)
9497
internal
9598
pure
9699
returns (bytes32 result)
97100
```
98101

99102
Returns the leaf at `leafIndex`.
100103

104+
### gatherLeaves(bytes32[],uint256[])
105+
106+
```solidity
107+
function gatherLeaves(bytes32[] memory tree, uint256[] memory leafIndices)
108+
internal
109+
pure
110+
returns (bytes32[] memory result)
111+
```
112+
113+
Returns the leaves at `leafIndices`.
114+
101115
### leafProof(bytes32[],uint256)
102116

103117
```solidity
104-
function leafProof(bytes32[] memory t, uint256 leafIndex)
118+
function leafProof(bytes32[] memory tree, uint256 leafIndex)
105119
internal
106120
pure
107121
returns (bytes32[] memory result)
@@ -112,7 +126,7 @@ Returns the proof for the leaf at `leafIndex`.
112126
### nodeProof(bytes32[],uint256)
113127

114128
```solidity
115-
function nodeProof(bytes32[] memory t, uint256 nodeIndex)
129+
function nodeProof(bytes32[] memory tree, uint256 nodeIndex)
116130
internal
117131
pure
118132
returns (bytes32[] memory result)
@@ -121,36 +135,36 @@ function nodeProof(bytes32[] memory t, uint256 nodeIndex)
121135
Returns the proof for the node at `nodeIndex`.
122136
This function can be used to prove the existence of internal nodes.
123137

124-
### leafsMultiProof(bytes32[],uint256[])
138+
### multiProofForLeaves(bytes32[],uint256[])
125139

126140
```solidity
127-
function leafsMultiProof(bytes32[] memory t, uint256[] memory leafIndices)
128-
internal
129-
pure
130-
returns (bytes32[] memory proof, bool[] memory flags)
141+
function multiProofForLeaves(
142+
bytes32[] memory tree,
143+
uint256[] memory leafIndices
144+
) internal pure returns (bytes32[] memory proof, bool[] memory flags)
131145
```
132146

133-
Returns proof and corresponding flags for multiple leafs.
147+
Returns proof and corresponding flags for multiple leaves.
134148
The `leafIndices` must be non-empty and sorted in strictly ascending order.
135149

136150
### pad(bytes32[],bytes32)
137151

138152
```solidity
139-
function pad(bytes32[] memory leafs, bytes32 defaultFill)
153+
function pad(bytes32[] memory leaves, bytes32 defaultFill)
140154
internal
141155
pure
142156
returns (bytes32[] memory result)
143157
```
144158

145-
Returns a copy of leafs, with the length padded to a power of 2.
159+
Returns a copy of leaves, with the length padded to a power of 2.
146160

147161
### pad(bytes32[])
148162

149163
```solidity
150-
function pad(bytes32[] memory leafs)
164+
function pad(bytes32[] memory leaves)
151165
internal
152166
pure
153167
returns (bytes32[] memory result)
154168
```
155169

156-
Equivalent to `pad(leafs, bytes32(0))`.
170+
Equivalent to `pad(leaves, bytes32(0))`.

0 commit comments

Comments
 (0)