Skip to content

Commit

Permalink
fix: forge fmt
Browse files Browse the repository at this point in the history
Signed-off-by: 0xsuryansh <[email protected]>
  • Loading branch information
0xsuryansh committed Jul 2, 2024
1 parent bb204d7 commit 3be0d90
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
78 changes: 39 additions & 39 deletions contracts/src/v0.8/ccip/capability/CCIPConfigArraysValidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
pragma solidity 0.8.24;

library CCIPConfigArraysValidation {
// errors
error ArrayEmpty();
error NotSorted(bytes32[] array);
error HasDuplicates(bytes32[] array);
error NotSubset(bytes32[] smaller, bytes32[] larger);
// errors
error ArrayEmpty();
error NotSorted(bytes32[] array);
error HasDuplicates(bytes32[] array);
error NotSubset(bytes32[] smaller, bytes32[] larger);

function _checkSortedNoDuplicatesAndSubset(bytes32[] memory a, bytes32[] memory b) internal pure {
if (a.length == 0 || b.length == 0) {
revert ArrayEmpty();
}
function _checkSortedNoDuplicatesAndSubset(bytes32[] memory a, bytes32[] memory b) internal pure {
if (a.length == 0 || b.length == 0) {
revert ArrayEmpty();
}

_checkSortedAndDuplicates(a);
_checkSortedAndDuplicates(b);
_checkSortedAndDuplicates(a);
_checkSortedAndDuplicates(b);

// Check if 'a' is a subset of 'b'
uint256 i = 0; // Pointer for 'a'
uint256 j = 0; // Pointer for 'b'
// Check if 'a' is a subset of 'b'
uint256 i = 0; // Pointer for 'a'
uint256 j = 0; // Pointer for 'b'

while (i < a.length && j < b.length) {
if (a[i] > b[j]) {
++j; // Move the pointer in 'b' to find a match
} else if (a[i] == b[j]) {
++i; // Found a match, move the pointer in 'a'
++j; // Also move in 'b' to continue checking
} else {
// 'a[i]' is less than 'b[j]' and no match is possible moving forward
revert NotSubset(a, b);
}
}
while (i < a.length && j < b.length) {
if (a[i] > b[j]) {
++j; // Move the pointer in 'b' to find a match
} else if (a[i] == b[j]) {
++i; // Found a match, move the pointer in 'a'
++j; // Also move in 'b' to continue checking
} else {
// 'a[i]' is less than 'b[j]' and no match is possible moving forward
revert NotSubset(a, b);
}
}

// If not all elements in 'a' were matched
if (i < a.length) {
revert NotSubset(a, b);
}
// If not all elements in 'a' were matched
if (i < a.length) {
revert NotSubset(a, b);
}
}

// Helper function to check if array is sorted and has no duplicates
function _checkSortedAndDuplicates(bytes32[] memory array) private pure {
for (uint256 i = 1; i < array.length; ++i) {
if (array[i] < array[i - 1]) {
revert NotSorted(array);
}
if (array[i] == array[i - 1]) {
revert HasDuplicates(array);
}
}
// Helper function to check if array is sorted and has no duplicates
function _checkSortedAndDuplicates(bytes32[] memory array) private pure {
for (uint256 i = 1; i < array.length; ++i) {
if (array[i] < array[i - 1]) {
revert NotSorted(array);
}
if (array[i] == array[i - 1]) {
revert HasDuplicates(array);
}
}
}
}
17 changes: 9 additions & 8 deletions contracts/src/v0.8/ccip/test/capability/CCIPConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ pragma solidity ^0.8.24;
import {Test} from "forge-std/Test.sol";

import {CCIPConfig} from "../../capability/CCIPConfig.sol";

import {CCIPConfigArraysValidation} from "../../capability/CCIPConfigArraysValidation.sol";
import {ICapabilitiesRegistry} from "../../capability/interfaces/ICapabilitiesRegistry.sol";
import {CCIPConfigHelper} from "../helpers/CCIPConfigHelper.sol";
import {CCIPConfigArraysValidation} from "../../capability/CCIPConfigArraysValidation.sol";

contract CCIPConfigSetup is Test {
address public constant OWNER = 0x82ae2B4F57CA5C1CBF8f744ADbD3697aD1a35AFe;
Expand Down Expand Up @@ -66,7 +67,7 @@ contract CCIPConfigSetup is Test {
returns (bytes32[] memory p2pIds, bytes[] memory signers, bytes[] memory transmitters)
{
p2pIds = _makeBytes32Array(numNodes, 0);
_sort(p2pIds,0, int(numNodes-1));
_sort(p2pIds, 0, int256(numNodes - 1));
signers = _makeBytesArray(numNodes, 10);
transmitters = _makeBytesArray(numNodes, 20);
for (uint256 i = 0; i < numNodes; i++) {
Expand Down Expand Up @@ -575,7 +576,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {
offchainConfig: bytes("offchainConfig")
});

vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSorted.selector,p2pIds));
vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSorted.selector, p2pIds));
s_ccipCC.validateConfig(config);
}

Expand All @@ -601,7 +602,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {
offchainConfig: bytes("offchainConfig")
});

vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSorted.selector,bootstrapP2PIds));
vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSorted.selector, bootstrapP2PIds));
s_ccipCC.validateConfig(config);
}

Expand All @@ -625,7 +626,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {
offchainConfig: bytes("offchainConfig")
});

vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.HasDuplicates.selector,p2pIds));
vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.HasDuplicates.selector, p2pIds));
s_ccipCC.validateConfig(config);
}

Expand All @@ -650,7 +651,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {
offchainConfig: bytes("offchainConfig")
});

vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.HasDuplicates.selector,bootstrapP2PIds));
vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.HasDuplicates.selector, bootstrapP2PIds));
s_ccipCC.validateConfig(config);
}

Expand All @@ -660,7 +661,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {

//forcing invalid bootstrapP2PIds where the bootstrapP2PIds is sorted, but one of the element is not in the p2pIdsSet
bytes32[] memory bootstrapP2PIds = _subset(p2pIds, 0, 2);
p2pIds[1] = bytes32(uint(p2pIds[0])+100);
p2pIds[1] = bytes32(uint256(p2pIds[0]) + 100);

CCIPConfig.OCR3Config memory config = CCIPConfig.OCR3Config({
pluginType: CCIPConfig.PluginType.Commit,
Expand All @@ -675,7 +676,7 @@ contract CCIPConfig_validateConfig is CCIPConfigSetup {
offchainConfig: bytes("offchainConfig")
});

vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSubset.selector,bootstrapP2PIds,p2pIds));
vm.expectRevert(abi.encodeWithSelector(CCIPConfigArraysValidation.NotSubset.selector, bootstrapP2PIds, p2pIds));
s_ccipCC.validateConfig(config);
}
}
Expand Down

0 comments on commit 3be0d90

Please sign in to comment.