Skip to content

Commit

Permalink
Merge a7a02c6 into a4b734a
Browse files Browse the repository at this point in the history
  • Loading branch information
defistar authored Aug 12, 2024
2 parents a4b734a + a7a02c6 commit 231c1d9
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 64 deletions.
48 changes: 24 additions & 24 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -545,42 +545,42 @@ MultiAggregateRateLimiter_applyRateLimiterConfigUpdates:test_UpdateExistingConfi
MultiAggregateRateLimiter_applyRateLimiterConfigUpdates:test_UpdateExistingConfig_Success() (gas: 53092)
MultiAggregateRateLimiter_applyRateLimiterConfigUpdates:test_ZeroChainSelector_Revert() (gas: 17019)
MultiAggregateRateLimiter_applyRateLimiterConfigUpdates:test_ZeroConfigs_Success() (gas: 12295)
MultiAggregateRateLimiter_constructor:test_ConstructorNoAuthorizedCallers_Success() (gas: 2006714)
MultiAggregateRateLimiter_constructor:test_Constructor_Success() (gas: 2122927)
MultiAggregateRateLimiter_constructor:test_ConstructorNoAuthorizedCallers_Success() (gas: 2148815)
MultiAggregateRateLimiter_constructor:test_Constructor_Success() (gas: 2262263)
MultiAggregateRateLimiter_getTokenBucket:test_GetTokenBucket_Success() (gas: 30248)
MultiAggregateRateLimiter_getTokenBucket:test_Refill_Success() (gas: 47358)
MultiAggregateRateLimiter_getTokenBucket:test_TimeUnderflow_Revert() (gas: 15821)
MultiAggregateRateLimiter_getTokenValue:test_GetTokenValue_Success() (gas: 19690)
MultiAggregateRateLimiter_getTokenValue:test_NoTokenPrice_Reverts() (gas: 21275)
MultiAggregateRateLimiter_getTokenBucket:test_TimeUnderflow_Revert() (gas: 15806)
MultiAggregateRateLimiter_getTokenValue:test_GetTokenValue_Success() (gas: 19617)
MultiAggregateRateLimiter_getTokenValue:test_NoTokenPrice_Reverts() (gas: 21202)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageFromUnauthorizedCaller_Revert() (gas: 14527)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithDifferentTokensOnDifferentChains_Success() (gas: 189472)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 59949)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithDifferentTokensOnDifferentChains_Success() (gas: 213961)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 60028)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithNoTokens_Success() (gas: 17593)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithRateLimitDisabled_Success() (gas: 44895)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithRateLimitExceeded_Revert() (gas: 50598)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithRateLimitReset_Success() (gas: 78780)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 263532)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithTokens_Success() (gas: 54784)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithRateLimitExceeded_Revert() (gas: 50452)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithRateLimitReset_Success() (gas: 78488)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 312662)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithTokens_Success() (gas: 54638)
MultiAggregateRateLimiter_onOutboundMessage:test_RateLimitValueDifferentLanes_Success() (gas: 9223372036854754743)
MultiAggregateRateLimiter_onOutboundMessage:test_ValidateMessageWithNoTokens_Success() (gas: 19104)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageFromUnauthorizedCaller_Revert() (gas: 15778)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithDifferentTokensOnDifferentChains_Success() (gas: 189460)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 61684)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithDifferentTokensOnDifferentChains_Success() (gas: 213949)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 61763)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithRateLimitDisabled_Success() (gas: 46683)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithRateLimitExceeded_Revert() (gas: 52371)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithRateLimitReset_Success() (gas: 79845)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 263746)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithTokens_Success() (gas: 56541)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithRateLimitExceeded_Revert() (gas: 52225)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithRateLimitReset_Success() (gas: 79553)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 312876)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithTokens_Success() (gas: 56395)
MultiAggregateRateLimiter_setPriceRegistry:test_OnlyOwner_Revert() (gas: 11358)
MultiAggregateRateLimiter_setPriceRegistry:test_Owner_Success() (gas: 19146)
MultiAggregateRateLimiter_setPriceRegistry:test_ZeroAddress_Revert() (gas: 10630)
MultiAggregateRateLimiter_updateRateLimitTokens:test_NonOwner_Revert() (gas: 16107)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensMultipleChains_Success() (gas: 225533)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensSingleChain_Success() (gas: 200148)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_AddsAndRemoves_Success() (gas: 162036)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_RemoveNonExistentToken_Success() (gas: 28465)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroDestToken_Revert() (gas: 17452)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroSourceToken_Revert() (gas: 17507)
MultiAggregateRateLimiter_updateRateLimitTokens:test_NonOwner_Revert() (gas: 18850)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensMultipleChains_Success() (gas: 236263)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensSingleChain_Success() (gas: 210881)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_AddsAndRemoves_Success() (gas: 169395)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_RemoveNonExistentToken_Success() (gas: 28637)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroDestToken_Revert() (gas: 18317)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroSourceToken_Revert() (gas: 18769)
MultiOCR3Base_setOCR3Configs:test_FMustBePositive_Revert() (gas: 59331)
MultiOCR3Base_setOCR3Configs:test_FTooHigh_Revert() (gas: 44298)
MultiOCR3Base_setOCR3Configs:test_RepeatSignerAddress_Revert() (gas: 283711)
Expand Down
19 changes: 11 additions & 8 deletions contracts/src/v0.8/ccip/MultiAggregateRateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, IT
using RateLimiter for RateLimiter.TokenBucket;
using USDPriceWith18Decimals for uint224;
using EnumerableMapAddresses for EnumerableMapAddresses.AddressToBytes32Map;
using EnumerableMapAddresses for EnumerableMapAddresses.AddressToBytesMap;

error PriceNotFoundForToken(address token);
error ZeroChainSelectorNotAllowed();

event RateLimiterConfigUpdated(uint64 indexed remoteChainSelector, bool isOutboundLane, RateLimiter.Config config);
event PriceRegistrySet(address newPriceRegistry);
event TokenAggregateRateLimitAdded(uint64 remoteChainSelector, bytes32 remoteToken, address localToken);
event TokenAggregateRateLimitAdded(uint64 remoteChainSelector, bytes remoteToken, address localToken);
event TokenAggregateRateLimitRemoved(uint64 remoteChainSelector, address localToken);

/// @notice RemoteRateLimitToken struct containing the local token address with the chain selector
Expand All @@ -39,7 +40,7 @@ contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, IT
/// @notice RateLimitToken struct containing both the local and remote token addresses
struct RateLimitTokenArgs {
LocalRateLimitToken localTokenArgs; // Local token update args scoped to one remote chain
bytes32 remoteToken; // Token on the remote chain (for OnRamp - dest, of OffRamp - source)
bytes remoteToken; // Token on the remote chain (for OnRamp - dest, of OffRamp - source)
}

/// @notice Update args for a single rate limiter config update
Expand All @@ -59,7 +60,7 @@ contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, IT

/// @dev Tokens that should be included in Aggregate Rate Limiting (from local chain (this chain) -> remote),
/// grouped per-remote chain.
mapping(uint64 remoteChainSelector => EnumerableMapAddresses.AddressToBytes32Map tokensLocalToRemote) internal
mapping(uint64 remoteChainSelector => EnumerableMapAddresses.AddressToBytesMap tokensLocalToRemote) internal
s_rateLimitedTokensLocalToRemote;

/// @notice The address of the PriceRegistry used to query token values for ratelimiting
Expand Down Expand Up @@ -198,15 +199,15 @@ contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, IT
function getAllRateLimitTokens(uint64 remoteChainSelector)
external
view
returns (address[] memory localTokens, bytes32[] memory remoteTokens)
returns (address[] memory localTokens, bytes[] memory remoteTokens)
{
uint256 tokenCount = s_rateLimitedTokensLocalToRemote[remoteChainSelector].length();

localTokens = new address[](tokenCount);
remoteTokens = new bytes32[](tokenCount);
remoteTokens = new bytes[](tokenCount);

for (uint256 i = 0; i < tokenCount; ++i) {
(address localToken, bytes32 remoteToken) = s_rateLimitedTokensLocalToRemote[remoteChainSelector].at(i);
(address localToken, bytes memory remoteToken) = s_rateLimitedTokensLocalToRemote[remoteChainSelector].at(i);
localTokens[i] = localToken;
remoteTokens[i] = remoteToken;
}
Expand All @@ -231,10 +232,12 @@ contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, IT

for (uint256 i = 0; i < adds.length; ++i) {
LocalRateLimitToken memory localTokenArgs = adds[i].localTokenArgs;
bytes32 remoteToken = adds[i].remoteToken;
bytes memory remoteToken = adds[i].remoteToken;
address localToken = localTokenArgs.localToken;

if (localToken == address(0) || remoteToken == bytes32("")) {
if (
localToken == address(0) || keccak256(abi.encodePacked(remoteToken)) == keccak256(abi.encodePacked(address(0)))
) {
revert ZeroAddressNotAllowed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[0]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[0]))
remoteToken: abi.encodePacked(s_sourceTokens[0])
});
adds[1] = MultiAggregateRateLimiter.RateLimitTokenArgs({
localTokenArgs: MultiAggregateRateLimiter.LocalRateLimitToken({
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[1]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[1]))
remoteToken: abi.encodePacked(s_sourceTokens[1])
});

for (uint256 i = 0; i < adds.length; ++i) {
Expand All @@ -502,8 +502,7 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi

s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), adds);

(address[] memory localTokens, bytes32[] memory remoteTokens) =
s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);
(address[] memory localTokens, bytes[] memory remoteTokens) = s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);

assertEq(localTokens.length, adds.length);
assertEq(localTokens.length, remoteTokens.length);
Expand All @@ -521,14 +520,14 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[0]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[0]))
remoteToken: abi.encodePacked(s_sourceTokens[0])
});
adds[1] = MultiAggregateRateLimiter.RateLimitTokenArgs({
localTokenArgs: MultiAggregateRateLimiter.LocalRateLimitToken({
remoteChainSelector: CHAIN_SELECTOR_2,
localToken: s_destTokens[1]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[1]))
remoteToken: abi.encodePacked(s_sourceTokens[1])
});

for (uint256 i = 0; i < adds.length; ++i) {
Expand All @@ -540,15 +539,15 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi

s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), adds);

(address[] memory localTokensChain1, bytes32[] memory remoteTokensChain1) =
(address[] memory localTokensChain1, bytes[] memory remoteTokensChain1) =
s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);

assertEq(localTokensChain1.length, 1);
assertEq(localTokensChain1.length, remoteTokensChain1.length);
assertEq(localTokensChain1[0], adds[0].localTokenArgs.localToken);
assertEq(remoteTokensChain1[0], adds[0].remoteToken);

(address[] memory localTokensChain2, bytes32[] memory remoteTokensChain2) =
(address[] memory localTokensChain2, bytes[] memory remoteTokensChain2) =
s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_2);

assertEq(localTokensChain2.length, 1);
Expand All @@ -564,14 +563,14 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[0]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[0]))
remoteToken: abi.encodePacked(s_sourceTokens[0])
});
adds[1] = MultiAggregateRateLimiter.RateLimitTokenArgs({
localTokenArgs: MultiAggregateRateLimiter.LocalRateLimitToken({
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[1]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[1]))
remoteToken: abi.encodePacked(s_sourceTokens[1])
});

MultiAggregateRateLimiter.LocalRateLimitToken[] memory removes =
Expand All @@ -594,8 +593,7 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi

s_rateLimiter.updateRateLimitTokens(removes, new MultiAggregateRateLimiter.RateLimitTokenArgs[](0));

(address[] memory localTokens, bytes32[] memory remoteTokens) =
s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);
(address[] memory localTokens, bytes[] memory remoteTokens) = s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);

assertEq(1, remoteTokens.length);
assertEq(adds[1].remoteToken, remoteTokens[0]);
Expand All @@ -621,8 +619,7 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
Vm.Log[] memory logEntries = vm.getRecordedLogs();
assertEq(logEntries.length, 0);

(address[] memory localTokens, bytes32[] memory remoteTokens) =
s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);
(address[] memory localTokens, bytes[] memory remoteTokens) = s_rateLimiter.getAllRateLimitTokens(CHAIN_SELECTOR_1);

assertEq(localTokens.length, 0);
assertEq(localTokens.length, remoteTokens.length);
Expand All @@ -637,7 +634,7 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[0]
}),
remoteToken: bytes32(bytes20(address(0)))
remoteToken: abi.encodePacked(address(0))
});

vm.expectRevert(AuthorizedCallers.ZeroAddressNotAllowed.selector);
Expand All @@ -651,7 +648,7 @@ contract MultiAggregateRateLimiter_updateRateLimitTokens is MultiAggregateRateLi
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: address(0)
}),
remoteToken: bytes32(bytes20(s_destTokens[0]))
remoteToken: abi.encodePacked(s_destTokens[0])
});

vm.expectRevert(AuthorizedCallers.ZeroAddressNotAllowed.selector);
Expand Down Expand Up @@ -682,7 +679,7 @@ contract MultiAggregateRateLimiter_onInboundMessage is MultiAggregateRateLimiter
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_destTokens[i]
}),
remoteToken: bytes32(bytes20(s_sourceTokens[i]))
remoteToken: abi.encodePacked(s_sourceTokens[i])
});

Internal.PriceUpdates memory priceUpdates =
Expand Down Expand Up @@ -772,7 +769,7 @@ contract MultiAggregateRateLimiter_onInboundMessage is MultiAggregateRateLimiter
localToken: s_destTokens[i]
}),
// Create a remote token address that is different from CHAIN_SELECTOR_1
remoteToken: bytes32(uint256(uint160(s_sourceTokens[i])) + type(uint160).max + 1)
remoteToken: abi.encodePacked(uint256(uint160(s_sourceTokens[i])) + type(uint160).max + 1)
});
}
s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), tokensToAdd);
Expand Down Expand Up @@ -821,7 +818,7 @@ contract MultiAggregateRateLimiter_onInboundMessage is MultiAggregateRateLimiter
localToken: s_destTokens[0]
}),
// Create a remote token address that is different from CHAIN_SELECTOR_1
remoteToken: bytes32(uint256(uint160(s_sourceTokens[0])) + type(uint160).max + 1)
remoteToken: abi.encodePacked(uint256(uint160(s_sourceTokens[0])) + type(uint160).max + 1)
});
s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), tokensToAdd);

Expand Down Expand Up @@ -918,7 +915,7 @@ contract MultiAggregateRateLimiter_onOutboundMessage is MultiAggregateRateLimite
remoteChainSelector: CHAIN_SELECTOR_1,
localToken: s_sourceTokens[i]
}),
remoteToken: bytes32(bytes20(s_destTokenBySourceToken[s_sourceTokens[i]]))
remoteToken: abi.encodePacked(bytes20(s_destTokenBySourceToken[s_sourceTokens[i]]))
});

Internal.PriceUpdates memory priceUpdates =
Expand Down Expand Up @@ -1006,7 +1003,9 @@ contract MultiAggregateRateLimiter_onOutboundMessage is MultiAggregateRateLimite
localToken: s_sourceTokens[i]
}),
// Create a remote token address that is different from CHAIN_SELECTOR_1
remoteToken: bytes32(uint256(uint160(s_destTokenBySourceToken[s_sourceTokens[i]])) + type(uint160).max + 1)
remoteToken: abi.encodePacked(
uint256(uint160(s_destTokenBySourceToken[s_sourceTokens[i]])) + type(uint160).max + 1
)
});
}
s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), tokensToAdd);
Expand Down Expand Up @@ -1055,7 +1054,7 @@ contract MultiAggregateRateLimiter_onOutboundMessage is MultiAggregateRateLimite
localToken: s_sourceTokens[0]
}),
// Create a remote token address that is different from CHAIN_SELECTOR_1
remoteToken: bytes32(uint256(uint160(s_destTokenBySourceToken[s_sourceTokens[0]])) + type(uint160).max + 1)
remoteToken: abi.encodePacked(uint256(uint160(s_destTokenBySourceToken[s_sourceTokens[0]])) + type(uint160).max + 1)
});
s_rateLimiter.updateRateLimitTokens(new MultiAggregateRateLimiter.LocalRateLimitToken[](0), tokensToAdd);

Expand Down
Loading

0 comments on commit 231c1d9

Please sign in to comment.