CCIP v1.5.1 RateLimiter Library API Reference
The RateLimiter
library implements Token Bucket rate limiting for CCIP operations.
error BucketOverfilled()
error OnlyCallableByAdminOrOwner()
error TokenMaxCapacityExceeded(uint256 capacity, uint256 requested, address tokenAddress)
error TokenRateLimitReached(uint256 minWaitInSeconds, uint256 available, address tokenAddress)
error AggregateValueMaxCapacityExceeded(uint256 capacity, uint256 requested)
error AggregateValueRateLimitReached(uint256 minWaitInSeconds, uint256 available)
error InvalidRateLimitRate(Config rateLimiterConfig)
error DisabledNonZeroRateLimit(Config config)
error RateLimitMustBeDisabled()
event TokensConsumed(uint256 tokens)
event ConfigChanged(Config config)
struct TokenBucket {
uint128 tokens;
uint32 lastUpdated;
bool isEnabled;
uint128 capacity;
uint128 rate;
}
Name | Type | Description |
---|
tokens | uint128 | Current number of tokens that are in the bucket |
lastUpdated | uint32 | Timestamp in seconds of the last token refill, good for 100+ years |
isEnabled | bool | Indication whether the rate limiting is enabled or not |
capacity | uint128 | Maximum number of tokens that can be in the bucket |
rate | uint128 | Number of tokens per second that the bucket is refilled |
struct Config {
bool isEnabled;
uint128 capacity;
uint128 rate;
}
Name | Type | Description |
---|
isEnabled | bool | Indication whether the rate limiting should be enabled |
capacity | uint128 | Specifies the capacity of the rate limiter |
rate | uint128 | Specifies the rate of the rate limiter |
function _consume(TokenBucket storage s_bucket, uint256 requestTokens, address tokenAddress) internal
Removes the given tokens from the pool, lowering the rate tokens allowed to be consumed for subsequent calls.
Name | Type | Description |
---|
s_bucket | TokenBucket | The token bucket to consume from |
requestTokens | uint256 | The total tokens to be consumed from the bucket |
tokenAddress | address | The token to consume capacity for, use 0x0 to indicate aggregate value capacity |
function _currentTokenBucketState(TokenBucket memory bucket) internal view returns (TokenBucket memory)
Gets the token bucket with its values for the block it was requested at.
Name | Type | Description |
---|
bucket | TokenBucket | The token bucket |
Type | Description |
---|
TokenBucket | The token bucket with current block values |
function _setTokenBucketConfig(TokenBucket storage s_bucket, Config memory config) internal
Sets the rate limited config.
Name | Type | Description |
---|
s_bucket | TokenBucket | The token bucket |
config | Config | The new config |
function _validateTokenBucketConfig(Config memory config, bool mustBeDisabled) internal pure
Validates the token bucket config.
Name | Type | Description |
---|
config | Config | The config to validate |
mustBeDisabled | bool | Whether the config must be disabled |
function _calculateRefill(uint256 capacity, uint256 tokens, uint256 timeDiff, uint256 rate) private pure returns (uint256)
Calculate refilled tokens.
Name | Type | Description |
---|
capacity | uint256 | bucket capacity |
tokens | uint256 | current bucket tokens |
timeDiff | uint256 | block time difference since last refill |
rate | uint256 | bucket refill rate |
Type | Description |
---|
uint256 | the value of tokens after refill |
function _min(uint256 a, uint256 b) internal pure returns (uint256)
Return the smallest of two integers.
Name | Type | Description |
---|
a | uint256 | first int |
b | uint256 | second int |
Type | Description |
---|
uint256 | smallest integer |