Skip to content

Commit 6acdf62

Browse files
committed
natspec
1 parent 6d3ea84 commit 6acdf62

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/test/unit/libraries/Merkle.t.sol

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,35 @@ import "src/contracts/libraries/Merkle.sol";
66
import "src/test/utils/Murky.sol";
77

88
abstract contract MerkleBaseTest is Test, MurkyBase {
9+
/// @dev Whether to use Keccak or Sha256 for tree + proof generation.
910
bool usingSha;
11+
/// @notice The contents of the merkle tree (unsorted).
1012
bytes32[] leaves;
13+
/// @notice The root of the merkle tree.
1114
bytes32 root;
15+
/// @notice The proofs for each leaf in the tree.
1216
bytes[] proofs;
1317

18+
/// -----------------------------------------------------------------------
19+
/// Keccak + Sha256 Tests
20+
/// -----------------------------------------------------------------------
21+
22+
/// @notice Verifies that Murky's proofs are compatible with our tree and proof verification.
1423
function test_verifyInclusion_ValidProof() public {
1524
assertValidProofs();
1625
}
1726

27+
/// @notice Verifies that an empty proof(s) is invalid.
1828
function test_verifyInclusion_EmptyProofs() public {
1929
proofs = new bytes[](proofs.length);
2030
assertInvalidProofs();
2131
}
2232

33+
/// -----------------------------------------------------------------------
34+
/// Assertions
35+
/// -----------------------------------------------------------------------
36+
37+
/// @dev Checks that all proofs are valid for their respective leaves.
2338
function assertValidProofs() internal virtual {
2439
function (bytes memory proof, bytes32 root, bytes32 leaf, uint256 index) returns (bool) verifyInclusion =
2540
usingSha ? Merkle.verifyInclusionSha256 : Merkle.verifyInclusionKeccak;
@@ -28,6 +43,7 @@ abstract contract MerkleBaseTest is Test, MurkyBase {
2843
}
2944
}
3045

46+
/// @dev Checks that all proofs are invalid for their respective leaves.
3147
function assertInvalidProofs() internal virtual {
3248
function (bytes memory proof, bytes32 root, bytes32 leaf, uint256 index) returns (bool) verifyInclusion =
3349
usingSha ? Merkle.verifyInclusionSha256 : Merkle.verifyInclusionKeccak;
@@ -36,6 +52,11 @@ abstract contract MerkleBaseTest is Test, MurkyBase {
3652
}
3753
}
3854

55+
/// -----------------------------------------------------------------------
56+
/// Helpers
57+
/// -----------------------------------------------------------------------
58+
59+
/// @dev Effeciently generates a random list of leaves without iterative hashing.
3960
function getLeaves(uint numLeaves) internal view virtual returns (bytes32[] memory leaves) {
4061
bytes memory _leavesAsBytes = vm.randomBytes(numLeaves * 32);
4162
/// @solidity memory-safe-assembly
@@ -45,6 +66,8 @@ abstract contract MerkleBaseTest is Test, MurkyBase {
4566
}
4667
}
4768

69+
/// @dev Generates proofs for each leaf in the tree.
70+
/// Intended to be overridden by the below child contracts.
4871
function getProofs(bytes32[] memory leaves) public view virtual returns (bytes[] memory proofs);
4972
}
5073

@@ -78,7 +101,7 @@ contract MerkleKeccakTest is MerkleBaseTest, MerkleKeccak {
78101
// Merkle.merkleizeKeccak pads to next power of 2, so we need to match that.
79102
uint numLeaves = nextPowerOf2(leaves.length);
80103
bytes32[] memory paddedLeaves = new bytes32[](numLeaves);
81-
for (uint i = 0; i < leaves.length; ++i) {
104+
for (uint i = 0; i < leaves.length; ++i) { // TODO: Point leaves to paddedLeaves using assembly to avoid loop.
82105
paddedLeaves[i] = leaves[i];
83106
}
84107

0 commit comments

Comments
 (0)