Skip to content

Commit

Permalink
lint: run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
QEDK committed Jan 27, 2024
1 parent 1e942ec commit 55c0b41
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ runs = 10000

[rpc_endpoints]
sepolia = "https://ethereum-sepolia.publicnode.com"
goerli = "https://ethereum-goerli.publicnode.com"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
4 changes: 1 addition & 3 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ contract GetProofMockScript is Script {
address impl = address(new AvailBridge());
AvailBridge bridge = AvailBridge(address(new TransparentUpgradeableProxy(impl, address(proxyAdmin), "")));
WrappedAvail avail = new WrappedAvail(address(bridge));
bridge.initialize(
0, admin, IWrappedAvail(address(avail)), admin, admin, IVectorx(vectorx)
);
bridge.initialize(0, admin, IWrappedAvail(address(avail)), admin, admin, IVectorx(vectorx));
vm.stopBroadcast();
}
}
7 changes: 3 additions & 4 deletions script/DeployMock.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ contract GetProofMockScript is Script {
address vectorx = vm.envAddress("VECTORX");
ProxyAdmin proxyAdmin = new ProxyAdmin(admin);
address impl = address(new DummyAvailBridge());
DummyAvailBridge bridge = DummyAvailBridge(address(new TransparentUpgradeableProxy(impl, address(proxyAdmin), "")));
DummyAvailBridge bridge =
DummyAvailBridge(address(new TransparentUpgradeableProxy(impl, address(proxyAdmin), "")));
WrappedAvail avail = new WrappedAvail(address(bridge));
bridge.initialize(
0, admin, IWrappedAvail(address(avail)), admin, admin, IDummyVectorx(vectorx)
);
bridge.initialize(0, admin, IWrappedAvail(address(avail)), admin, admin, IDummyVectorx(vectorx));
vm.stopBroadcast();
}
}
6 changes: 5 additions & 1 deletion src/lib/Merkle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ library Merkle {
}
}

function verifySha2(bytes32[] calldata proof, bytes32 root, uint256 index, bytes32 leaf) internal view returns (bool isValid) {
function verifySha2(bytes32[] calldata proof, bytes32 root, uint256 index, bytes32 leaf)
internal
view
returns (bool isValid)
{
assembly ("memory-safe") {
if proof.length {
// set end to be the end of the proof array, shl(5, proof.length) is equivalent to proof.length * 32
Expand Down
55 changes: 49 additions & 6 deletions test/AvailBridgeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,14 @@ contract AvailBridgeTest is Test, MurkyBase {
assertEq(avail.totalSupply(), amount);
}

function test_receiveAVAIL_2(bytes32 rangeHash, uint64 messageId, bytes32[16] calldata c_leaves, bytes32[16] calldata c_dataRoots, uint256 rand, bytes32 blobRoot) external {
function test_receiveAVAIL_2(
bytes32 rangeHash,
uint64 messageId,
bytes32[16] calldata c_leaves,
bytes32[16] calldata c_dataRoots,
uint256 rand,
bytes32 blobRoot
) external {
// this function is a bit unreadable because forge coverage does not support IR compilation which results
// in stack too deep errors
bytes32[] memory dataRoots = new bytes32[](c_dataRoots.length);
Expand All @@ -423,17 +430,53 @@ contract AvailBridgeTest is Test, MurkyBase {
}
}
address to = makeAddr("to");
leaves[rand % leaves.length] = keccak256(abi.encode(IAvailBridge.Message(0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId)));
leaves[rand % leaves.length] = keccak256(
abi.encode(
IAvailBridge.Message(
0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId
)
)
);
// set dataRoot at this point in the array
dataRoots[rand % dataRoots.length] = hashLeafPairs(blobRoot, getRoot(leaves));
vectorx.set(rangeHash, sha2merkle.getRoot(dataRoots));

vm.expectCall(address(avail), abi.encodeCall(avail.mint, (to, 1)));
{
bridge.receiveAVAIL(IAvailBridge.Message(0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId), IAvailBridge.MerkleProofInput(sha2merkle.getProof(dataRoots, rand % dataRoots.length), getProof(leaves, rand % leaves.length), rangeHash, rand % dataRoots.length, blobRoot, getRoot(leaves), keccak256(abi.encode(IAvailBridge.Message(0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId))), rand % leaves.length));
bridge.receiveAVAIL(
IAvailBridge.Message(
0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId
),
IAvailBridge.MerkleProofInput(
sha2merkle.getProof(dataRoots, rand % dataRoots.length),
getProof(leaves, rand % leaves.length),
rangeHash,
rand % dataRoots.length,
blobRoot,
getRoot(leaves),
keccak256(
abi.encode(
IAvailBridge.Message(
0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId
)
)
),
rand % leaves.length
)
);
}
{
assertTrue(bridge.isBridged(keccak256(abi.encode(IAvailBridge.Message(0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId)))));
{
assertTrue(
bridge.isBridged(
keccak256(
abi.encode(
IAvailBridge.Message(
0x02, bytes32("1"), bytes32(bytes20(to)), 1, 2, abi.encode(bytes32(0), 1), messageId
)
)
)
)
);
}
{
assertEq(avail.totalSupply(), 1);
Expand Down
62 changes: 54 additions & 8 deletions test/MerkleSha2Test.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,74 @@ contract MerkleTest is Test, MurkyBase {

function test_checkMembershipHardcoded(uint256 wrongIndex, bytes32 wrongRoot) external {
vm.assume(wrongIndex != 65 && wrongRoot != 0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15);
bytes32[8] memory c_proof = [bytes32(0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5),0x51f84e7279cdf6acb81af77aec64f618f71029b7d9c6d37c035c37134e517af2,0x69c8458dd62d27ea9abd40586ce53e5220d43b626c27f76468a57e94347f0d6b,0x5a021e65ea5c6b76469b68db28c7a390836e22c21c6f95cdef4d3408eb6b8050,0x676a0d0fab94c57be20667b57cd0800d7e5afc9b1c039a3c89995d527fbcf6c4,0x9efde052aa15429fae05bad4d0b1d7c64da64d03d7a1854a588c2cb8430c0d30,0xe51e1602448430542788cabb952ab87348561d146fe366b2525e581c0530c77e,0x87eb0ddba57e35f6d286673802a4af5975e22506c7cf4c64bb6be5ee11527f2c];
bytes32[8] memory c_proof = [
bytes32(0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5),
0x51f84e7279cdf6acb81af77aec64f618f71029b7d9c6d37c035c37134e517af2,
0x69c8458dd62d27ea9abd40586ce53e5220d43b626c27f76468a57e94347f0d6b,
0x5a021e65ea5c6b76469b68db28c7a390836e22c21c6f95cdef4d3408eb6b8050,
0x676a0d0fab94c57be20667b57cd0800d7e5afc9b1c039a3c89995d527fbcf6c4,
0x9efde052aa15429fae05bad4d0b1d7c64da64d03d7a1854a588c2cb8430c0d30,
0xe51e1602448430542788cabb952ab87348561d146fe366b2525e581c0530c77e,
0x87eb0ddba57e35f6d286673802a4af5975e22506c7cf4c64bb6be5ee11527f2c
];
bytes32[] memory proof = new bytes32[](8);
for (uint256 i = 0; i < 8;) {
proof[i] = c_proof[i];
unchecked {
++i;
}
}
bytes32 randomDataHash = sha256(abi.encode(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23)));
assertTrue(merkleUser.checkMembershipMemory(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), 65, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof));
bytes32 randomDataHash =
sha256(abi.encode(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23)));
assertTrue(
merkleUser.checkMembershipMemory(
bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23),
65,
bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15),
proof
)
);
// checked with fixed wrong index
assertFalse(merkleUser.checkMembershipMemory(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), 66, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof));
assertFalse(
merkleUser.checkMembershipMemory(
bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23),
66,
bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15),
proof
)
);
// check with fuzzed wrong index
assertFalse(merkleUser.checkMembershipMemory(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), wrongIndex, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof));
assertFalse(
merkleUser.checkMembershipMemory(
bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23),
wrongIndex,
bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15),
proof
)
);
// check with fuzzed leaf
assertFalse(merkleUser.checkMembershipMemory(randomDataHash, 65, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof));
assertFalse(
merkleUser.checkMembershipMemory(
randomDataHash, 65, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof
)
);
// check with fuzzed root
assertFalse(merkleUser.checkMembershipMemory(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), 65, wrongRoot, proof));
assertFalse(
merkleUser.checkMembershipMemory(
bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), 65, wrongRoot, proof
)
);
// check with fuzzed root and leaf
assertFalse(merkleUser.checkMembershipMemory(randomDataHash, 65, wrongRoot, proof));
proof[wrongIndex % proof.length] = keccak256(abi.encode(proof[wrongIndex % proof.length]));
assertFalse(merkleUser.checkMembershipMemory(bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23), 65, bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15), proof));
assertFalse(
merkleUser.checkMembershipMemory(
bytes32(0x2bd651601ffb95b9346c4867848e9621b53236baa08bfb29c9da28e7be7aeb23),
65,
bytes32(0x8eebcc756e5fd418501eff745f180ff16f151b82f823623b1b656bde0599fa15),
proof
)
);
}
}

Expand Down

0 comments on commit 55c0b41

Please sign in to comment.