Skip to content

Commit

Permalink
fix: CCIP-2594 fix pagination logic and tests for CCIPConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
defistar committed Jul 2, 2024
1 parent 237907b commit 00bf5cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions contracts/src/v0.8/ccip/capability/CCIPConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ contract CCIPConfig is ITypeAndVersion, ICapabilityConfiguration, OwnerIsCreator

/// @notice Returns all the chain configurations.
/// @return paginatedChainConfigs chain configurations.
function getAllChainConfigs(uint256 pageIndex, uint256 pageSize) external view returns (ChainConfigInfo[] memory paginatedChainConfigs) {
function getAllChainConfigs(uint256 pageIndex, uint256 pageSize) external view returns (ChainConfigInfo[] memory) {
uint256 totalItems = s_remoteChainSelectors.length(); // Total number of chain selectors
if (pageSize == 0 || pageIndex * pageSize >= totalItems) {
return new ChainConfigInfo[](0); // Return an empty array if pageSize is 0 or pageIndex is out of bounds
Expand All @@ -158,14 +158,16 @@ contract CCIPConfig is ITypeAndVersion, ICapabilityConfiguration, OwnerIsCreator
uint256 startIndex = pageIndex * pageSize;
uint256 endIndex = startIndex + pageSize > totalItems ? totalItems : startIndex + pageSize;

paginatedChainConfigs = new ChainConfigInfo[](endIndex - startIndex);
ChainConfigInfo[] memory paginatedChainConfigs = new ChainConfigInfo[](endIndex - startIndex);

uint256[] memory chainSelectors = s_remoteChainSelectors.values();
for (uint256 i = startIndex; i < endIndex; ++i) {
uint64 chainSelector = uint64(chainSelectors[i]);
paginatedChainConfigs[i - startIndex] =
ChainConfigInfo({chainSelector: chainSelector, chainConfig: s_chainConfigurations[chainSelector]});
}

return paginatedChainConfigs;
}

/// @notice Returns the OCR configuration for the given don ID and plugin type.
Expand Down
5 changes: 4 additions & 1 deletion contracts/src/v0.8/ccip/test/capability/CCIPConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ contract CCIPConfig_chainConfig is CCIPConfigSetup {
emit CCIPConfig.ChainConfigSet(2, adds[1].chainConfig);
s_ccipCC.applyChainConfigUpdates(new uint64[](0), adds);

CCIPCapabilityConfiguration.ChainConfigInfo[] memory configs = s_ccipCC.getAllChainConfigs(0, 2);
CCIPConfig.ChainConfigInfo[] memory configs = s_ccipCC.getAllChainConfigs(0, 2);
assertEq(configs.length, 2, "chain configs length must be 2");
assertEq(configs[0].chainSelector, 1, "chain selector must match");
assertEq(configs[1].chainSelector, 2, "chain selector must match");
Expand All @@ -139,6 +139,9 @@ contract CCIPConfig_chainConfig is CCIPConfigSetup {
assertEq(configs[1].chainSelector, 2, "chain selector must match");

configs = s_ccipCC.getAllChainConfigs(1, 1);
assertEq(configs.length, 1, "chain configs length must be 1");

configs = s_ccipCC.getAllChainConfigs(1, 2);
assertEq(configs.length, 0, "chain configs length must be 0");
}

Expand Down

0 comments on commit 00bf5cf

Please sign in to comment.