Skip to content

Commit

Permalink
feat: rateLimiterConfig mapping of localToRemoteTokens to take bytes …
Browse files Browse the repository at this point in the history
…instead of bytes32 (#1207)

## Description:
rateLimiterConfig mapping of localToRemoteTokens to take bytes instead
of bytes32

## Details

- In the MultiAggregateRateLimiter contract, the the following mapping
might be simplifiable to
- (uint64 remoteChainSelector -> address token) , 
- since it is only used as an isEnabled check (i.e. the (address token
-> bytes32 remoteToken) is never used on-chain)
- mapping being necessary off-chain, we need to convert the (address ->
bytes32) mapping to (address -> bytes) to be consistent with using bytes
for all family-agnostic addresses

```js
mapping(uint64 remoteChainSelector => EnumerableMapAddresses.AddressToBytes32Map tokensLocalToRemote) internal
    s_rateLimitedTokensLocalToRemote;
```

this has to been changed to

```js
  mapping(uint64 remoteChainSelector => EnumerableMapAddresses.AddressToBytesMap tokensLocalToRemote) internal
    s_rateLimitedTokensLocalToRemote;
```

## Change List:

1. New solidity library `EnumerableMapBytes32` which contains
`Bytes32ToBytes` Mapping and enumerate it
2. `EnumerableMapAddresses.sol` library has added support for the new
library cherrypicked from chainlink repo `EnumerableMapBytes32`
3. `MultiAggregateRateLimiter` with mapping for remoteSelector ->
localTokenAddress -> remoteTokenAddress is updated to contain
remoteTokenAddress as `bytes` instead of `bytes32`

---------

Co-authored-by: app-token-issuer-infra-releng[bot] <120227048+app-token-issuer-infra-releng[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 03ae3bb commit deec42f
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 90 deletions.
5 changes: 5 additions & 0 deletions contracts/.changeset/nice-planets-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/contracts': minor
---

EnumerableMap Library for an Address to Bytes mapping
40 changes: 20 additions & 20 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: 2149637)
MultiAggregateRateLimiter_constructor:test_Constructor_Success() (gas: 2265894)
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_getTokenValue:test_GetTokenValue_Success() (gas: 19712)
MultiAggregateRateLimiter_getTokenValue:test_NoTokenPrice_Reverts() (gas: 21297)
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: 213744)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 60487)
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_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 312061)
MultiAggregateRateLimiter_onInboundMessage:test_ValidateMessageWithTokens_Success() (gas: 54784)
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: 213732)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithDisabledRateLimitToken_Success() (gas: 62222)
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_ValidateMessageWithTokensOnDifferentChains_Success() (gas: 312275)
MultiAggregateRateLimiter_onOutboundMessage:test_onOutboundMessage_ValidateMessageWithTokens_Success() (gas: 56541)
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_setPriceRegistry:test_OnlyOwner_Revert() (gas: 11292)
MultiAggregateRateLimiter_setPriceRegistry:test_Owner_Success() (gas: 19080)
MultiAggregateRateLimiter_setPriceRegistry:test_ZeroAddress_Revert() (gas: 10564)
MultiAggregateRateLimiter_updateRateLimitTokens:test_NonOwner_Revert() (gas: 18872)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensMultipleChains_Success() (gas: 279906)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokensSingleChain_Success() (gas: 254501)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_AddsAndRemoves_Success() (gas: 204488)
MultiAggregateRateLimiter_updateRateLimitTokens:test_UpdateRateLimitTokens_RemoveNonExistentToken_Success() (gas: 28681)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroDestToken_Revert() (gas: 18309)
MultiAggregateRateLimiter_updateRateLimitTokens:test_ZeroSourceToken_Revert() (gas: 18238)
MultiOCR3Base_setOCR3Configs:test_FMustBePositive_Revert() (gas: 59331)
MultiOCR3Base_setOCR3Configs:test_FTooHigh_Revert() (gas: 44298)
MultiOCR3Base_setOCR3Configs:test_RepeatSignerAddress_Revert() (gas: 283711)
Expand Down
28 changes: 18 additions & 10 deletions contracts/gas-snapshots/shared.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,29 @@ CallWithExactGas__callWithExactGasSafeReturnData:test_NoContractReverts() (gas:
CallWithExactGas__callWithExactGasSafeReturnData:test_NoGasForCallExactCheckReverts() (gas: 16139)
CallWithExactGas__callWithExactGasSafeReturnData:test_NotEnoughGasForCallReverts() (gas: 16547)
CallWithExactGas__callWithExactGasSafeReturnData:test_callWithExactGasSafeReturnData_ThrowOOGError_Revert() (gas: 36752)
EnumerableMapAddresses_at:testAtSuccess() (gas: 95001)
EnumerableMapAddresses_at:testBytes32AtSuccess() (gas: 94770)
EnumerableMapAddresses_at:testAtSuccess() (gas: 95086)
EnumerableMapAddresses_at:testBytes32AtSuccess() (gas: 94877)
EnumerableMapAddresses_contains:testBytes32ContainsSuccess() (gas: 93518)
EnumerableMapAddresses_contains:testContainsSuccess() (gas: 93696)
EnumerableMapAddresses_get:testBytes32GetSuccess() (gas: 94249)
EnumerableMapAddresses_get:testGetSuccess() (gas: 94436)
EnumerableMapAddresses_get_errorMessage:testGetErrorMessageSuccess() (gas: 94477)
EnumerableMapAddresses_length:testBytes32LengthSuccess() (gas: 72404)
EnumerableMapAddresses_length:testLengthSuccess() (gas: 72582)
EnumerableMapAddresses_remove:testBytes32RemoveSuccess() (gas: 73408)
EnumerableMapAddresses_get:testBytes32GetSuccess() (gas: 94278)
EnumerableMapAddresses_get:testGetSuccess() (gas: 94453)
EnumerableMapAddresses_get_errorMessage:testGetErrorMessageSuccess() (gas: 94489)
EnumerableMapAddresses_length:testBytes32LengthSuccess() (gas: 72445)
EnumerableMapAddresses_length:testLengthSuccess() (gas: 72640)
EnumerableMapAddresses_remove:testBytes32RemoveSuccess() (gas: 73462)
EnumerableMapAddresses_remove:testRemoveSuccess() (gas: 73686)
EnumerableMapAddresses_set:testBytes32SetSuccess() (gas: 94496)
EnumerableMapAddresses_set:testSetSuccess() (gas: 94685)
EnumerableMapAddresses_tryGet:testBytes32TryGetSuccess() (gas: 94604)
EnumerableMapAddresses_tryGet:testTryGetSuccess() (gas: 94864)
EnumerableMapAddresses_tryGet:testBytes32TryGetSuccess() (gas: 94622)
EnumerableMapAddresses_tryGet:testTryGetSuccess() (gas: 94893)
EnumerableMapAddresses_at:testBytesAtSuccess() (gas: 96564)
EnumerableMapAddresses_contains:testBytesContainsSuccess() (gas: 94012)
EnumerableMapAddresses_get:testBytesGetSuccess() (gas: 95879)
EnumerableMapAddresses_get_errorMessage:testBytesGetErrorMessageSuccess() (gas: 95878)
EnumerableMapAddresses_length:testBytesLengthSuccess() (gas: 73011)
EnumerableMapAddresses_remove:testBytesRemoveSuccess() (gas: 74249)
EnumerableMapAddresses_set:testBytesSetSuccess() (gas: 95428)
EnumerableMapAddresses_tryGet:testBytesTryGetSuccess() (gas: 96279)
OpStackBurnMintERC677_constructor:testConstructorSuccess() (gas: 1743649)
OpStackBurnMintERC677_interfaceCompatibility:testBurnCompatibility() (gas: 298649)
OpStackBurnMintERC677_interfaceCompatibility:testMintCompatibility() (gas: 137957)
Expand Down
18 changes: 9 additions & 9 deletions contracts/src/v0.8/ccip/MultiAggregateRateLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import {USDPriceWith18Decimals} from "./libraries/USDPriceWith18Decimals.sol";
contract MultiAggregateRateLimiter is IMessageInterceptor, AuthorizedCallers, ITypeAndVersion {
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 +39,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 +59,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 +198,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 +231,10 @@ 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) || remoteToken.length == 0) {
revert ZeroAddressNotAllowed();
}

Expand Down
Loading

0 comments on commit deec42f

Please sign in to comment.