Skip to content

Commit ebbc748

Browse files
committed
feat: increase coverage, add more merkle tests with fixed large tree
1 parent ece49d2 commit ebbc748

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

foundry.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ via_ir = true
1010
runs = 10000
1111
max_test_rejects = 999999
1212

13+
[rpc_endpoints]
14+
sepolia = "https://rpc.ankr.com/eth_sepolia"
15+
1316
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

slither.config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"detectors_to_exclude": "solc-version",
3-
"filter_paths": "(lib/)"
3+
"filter_paths": "(lib/|test/|script/|src/mocks/)"
44
}

test/AvailBridgeTest.t.sol

+32
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ contract AvailBridgeTest is Test, MurkyBase {
4040
assertEq(address(bridge.vectorx()), address(newVectorx));
4141
}
4242

43+
function testRevertArrayLengthMismatch_updateTokens(uint8 len1, uint8 len2) external {
44+
// using len > uint8 slows tests by a *lot*
45+
vm.assume(len1 != len2);
46+
bytes32[] memory assetIds = new bytes32[](len1);
47+
address[] memory addresses = new address[](len2);
48+
vm.prank(owner);
49+
vm.expectRevert(AvailBridge.ArrayLengthMismatch.selector);
50+
bridge.updateTokens(assetIds, addresses);
51+
}
52+
53+
function testRevertInvalidMessage_receiveMessage(bytes1 prefix) external {
54+
vm.assume(prefix != 0x01);
55+
AvailBridge.Message memory message =
56+
AvailBridge.Message(prefix, bytes32(0), bytes32(0), 1, 2, "", 0);
57+
AvailBridge.MerkleProofInput memory input = AvailBridge.MerkleProofInput(
58+
new bytes32[](0), new bytes32[](0), bytes32(0), 0, bytes32(0), bytes32(0), bytes32(0), 0
59+
);
60+
vm.expectRevert(AvailBridge.InvalidMessage.selector);
61+
bridge.receiveMessage(message, input);
62+
}
63+
4364
function test_receiveMessage(bytes32 rangeHash, bytes calldata data, bytes32 from, uint64 messageId) external {
4465
MessageReceiverMock messageReceiver = new MessageReceiverMock();
4566
messageReceiver.initialize(address(bridge));
@@ -59,6 +80,17 @@ contract AvailBridgeTest is Test, MurkyBase {
5980
bridge.receiveMessage(message, input);
6081
}
6182

83+
function testRevertInvalidAssetId_receiveAvail(bytes32 assetId) external {
84+
vm.assume(assetId != 0x0);
85+
AvailBridge.Message memory message =
86+
AvailBridge.Message(0x02, bytes32(0), bytes32(0), 1, 2, abi.encode(assetId, 0), 0);
87+
AvailBridge.MerkleProofInput memory input = AvailBridge.MerkleProofInput(
88+
new bytes32[](0), new bytes32[](0), bytes32(0), 0, bytes32(0), bytes32(0), bytes32(0), 0
89+
);
90+
vm.expectRevert(AvailBridge.InvalidAssetId.selector);
91+
bridge.receiveAVL(message, input);
92+
}
93+
6294
function test_receiveAVL(bytes32 rangeHash, bytes32 from, uint256 amount, uint64 messageId) external {
6395
vm.assume(amount != 0);
6496
address to = makeAddr("to");

test/MerkleTest.t.sol

+41-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ contract MerkleTest is Test, MurkyBase {
1313
}
1414

1515
/// @notice Hashing function for Murky
16-
function hashLeafPairs(bytes32 left, bytes32 right) public pure override returns (bytes32 _hash) {
17-
_hash = keccak256(abi.encode(left, right));
16+
function hashLeafPairs(bytes32 left, bytes32 right) public pure override returns (bytes32) {
17+
return keccak256(abi.encode(left, right));
1818
}
1919

20-
function testCheckMembershipSingleLeaf(bytes32 leaf, uint256 index) external {
21-
vm.assume(index != 0);
20+
function test_checkMembershipSingleLeaf(bytes32 leaf, bytes32 wrongRoot, uint256 index) external {
21+
vm.assume(index != 0 && wrongRoot != leaf);
2222
bytes32 randomDataHash = keccak256(abi.encode(leaf));
2323
bytes32[] memory proof = new bytes32[](0);
2424

@@ -32,9 +32,11 @@ contract MerkleTest is Test, MurkyBase {
3232
assertFalse(merkleUser.checkMembership(leaf, index, leaf, proof));
3333
// check with wrong leaf and wrong index
3434
assertFalse(merkleUser.checkMembership(randomDataHash, index, leaf, proof));
35+
// check with wrong index, wrong leaf and wrong root
36+
assertFalse(merkleUser.checkMembership(randomDataHash, index, wrongRoot, proof));
3537
}
3638

37-
function testCheckMembership(bytes32[] memory leaves, uint256 index, uint256 wrongIndex, bytes32 wrongRoot)
39+
function test_checkMembership(bytes32[] memory leaves, uint256 index, uint256 wrongIndex, bytes32 wrongRoot)
3840
external
3941
{
4042
vm.assume(leaves.length > 1 && index < leaves.length && wrongIndex != index);
@@ -58,10 +60,41 @@ contract MerkleTest is Test, MurkyBase {
5860
assertFalse(merkleUser.checkMembership(randomDataHash, wrongIndex, wrongRoot, proof));
5961
}
6062

61-
function testCheckMembershipLargeTree(bytes32[] memory leaves, uint256 index, uint256 wrongIndex, bytes32 wrongRoot)
63+
function test_checkMembershipLargeTree(bytes32[] memory leaves, uint256 index, uint256 wrongIndex, bytes32 wrongRoot)
6264
external
6365
{
64-
vm.assume(leaves.length > 1 && index < leaves.length && wrongIndex != index);
66+
vm.assume(leaves.length >= 128 && index < leaves.length && wrongIndex != index);
67+
bytes32 root = getRoot(leaves);
68+
vm.assume(wrongRoot != root);
69+
bytes32[] memory proof = getProof(leaves, index);
70+
bytes32 leaf = leaves[index];
71+
bytes32 randomDataHash = keccak256(abi.encode(leaf));
72+
73+
// should return true for leaf and false for random hash
74+
assertTrue(merkleUser.checkMembership(leaf, index, root, proof));
75+
// check with wrong leaf
76+
assertFalse(merkleUser.checkMembership(randomDataHash, index, root, proof));
77+
// check with fixed wrong index
78+
assertFalse(merkleUser.checkMembership(leaf, leaves.length, root, proof));
79+
// check with wrong index
80+
assertFalse(merkleUser.checkMembership(leaf, wrongIndex, root, proof));
81+
// check with wrong index and wrong leaf
82+
assertFalse(merkleUser.checkMembership(randomDataHash, wrongIndex, root, proof));
83+
// check with wrong index, wrong leaf and wrong root
84+
assertFalse(merkleUser.checkMembership(randomDataHash, wrongIndex, wrongRoot, proof));
85+
}
86+
87+
function test_checkMembershipLargeTree2(bytes32[256] memory c_leaves, uint256 index, uint256 wrongIndex, bytes32 wrongRoot)
88+
external
89+
{
90+
vm.assume(index < c_leaves.length && wrongIndex != index);
91+
bytes32[] memory leaves = new bytes32[](c_leaves.length);
92+
for (uint256 i = 0; i < c_leaves.length; ) {
93+
leaves[i] = c_leaves[i];
94+
unchecked {
95+
++i;
96+
}
97+
}
6598
bytes32 root = getRoot(leaves);
6699
vm.assume(wrongRoot != root);
67100
bytes32[] memory proof = getProof(leaves, index);
@@ -93,7 +126,6 @@ contract MerkleUser {
93126
pure
94127
returns (bool)
95128
{
96-
bool r = Merkle.verify(proof, rootHash, index, leaf);
97-
return r;
129+
return Merkle.verify(proof, rootHash, index, leaf);
98130
}
99131
}

0 commit comments

Comments
 (0)