Repository navigation
feat(contracts): add ETH withdrawal rate limit to L1CrossDomainMessenger #1082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SegueII
wants to merge
3
commits into
main
Choose a base branch
from
feat/eth-withdrawal-rate-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,650
−12
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity =0.8.24; | ||
|
|
||
| /// @title RateLimiter | ||
| /// @notice Token bucket rate limiter. The bucket holds at most `capacity` tokens and is refilled at `rate` | ||
| /// tokens per second, so the amount consumed during any period `T` never exceeds `capacity + rate * T`. | ||
| library RateLimiter { | ||
| /********** | ||
| * Errors * | ||
| **********/ | ||
|
|
||
| /// @dev Thrown when the requested amount exceeds the bucket capacity, which can never succeed. | ||
| /// @param capacity The capacity of the bucket. | ||
| /// @param requested The requested amount. | ||
| error ErrRateLimitCapacityExceeded(uint256 capacity, uint256 requested); | ||
|
|
||
| /// @dev Thrown when the bucket does not hold enough tokens for now. | ||
| /// @param minWaitInSeconds The minimum time to wait before the bucket can cover the request. | ||
| /// @param available The amount of tokens currently available. | ||
| error ErrRateLimitReached(uint256 minWaitInSeconds, uint256 available); | ||
|
|
||
| /// @dev Thrown when the config is invalid. | ||
| /// @param isEnabled Whether the rate limit is enabled. | ||
| /// @param capacity The capacity of the bucket. | ||
| /// @param rate The refill rate of the bucket. | ||
| error ErrInvalidRateLimitConfig(bool isEnabled, uint128 capacity, uint128 rate); | ||
|
|
||
| /*********** | ||
| * Structs * | ||
| ***********/ | ||
|
|
||
| struct Config { | ||
| // Whether the rate limit is enabled. | ||
| bool isEnabled; | ||
| // The maximum amount of tokens the bucket can hold. | ||
| uint128 capacity; | ||
| // The amount of tokens refilled per second. | ||
| uint128 rate; | ||
| } | ||
|
|
||
| struct TokenBucket { | ||
| // The amount of tokens in the bucket at `lastUpdated`. | ||
| uint128 tokens; | ||
| // The timestamp when the bucket was last updated. | ||
| uint32 lastUpdated; | ||
| // Whether the rate limit is enabled. | ||
| bool isEnabled; | ||
| // The maximum amount of tokens the bucket can hold. | ||
| uint128 capacity; | ||
| // The amount of tokens refilled per second. | ||
| uint128 rate; | ||
| } | ||
|
|
||
| /********************** | ||
| * Internal Functions * | ||
| **********************/ | ||
|
|
||
| /// @dev Consume tokens from the bucket, revert if the bucket cannot cover the amount. | ||
| /// @param _bucket The token bucket. | ||
| /// @param _amount The amount of tokens to consume. | ||
| function consume(TokenBucket storage _bucket, uint256 _amount) internal { | ||
| if (!_bucket.isEnabled || _amount == 0) return; | ||
|
|
||
| uint256 _capacity = _bucket.capacity; | ||
| if (_amount > _capacity) revert ErrRateLimitCapacityExceeded(_capacity, _amount); | ||
|
|
||
| uint256 _rate = _bucket.rate; | ||
| uint256 _tokens = _refill(_bucket.tokens, _capacity, _rate, _bucket.lastUpdated); | ||
| if (_tokens < _amount) { | ||
| uint256 _wait = _rate == 0 ? type(uint256).max : (_amount - _tokens + _rate - 1) / _rate; | ||
| revert ErrRateLimitReached(_wait, _tokens); | ||
| } | ||
|
|
||
| _bucket.tokens = uint128(_tokens - _amount); | ||
| _bucket.lastUpdated = uint32(block.timestamp); | ||
| } | ||
|
|
||
| /// @dev Give back tokens to the bucket when the guarded transfer did not happen. | ||
| /// @param _bucket The token bucket. | ||
| /// @param _amount The amount of tokens to give back. | ||
| function refund(TokenBucket storage _bucket, uint256 _amount) internal { | ||
| if (!_bucket.isEnabled || _amount == 0) return; | ||
|
|
||
| uint256 _capacity = _bucket.capacity; | ||
| uint256 _tokens = _refill(_bucket.tokens, _capacity, _bucket.rate, _bucket.lastUpdated) + _amount; | ||
| _bucket.tokens = uint128(_tokens < _capacity ? _tokens : _capacity); | ||
| _bucket.lastUpdated = uint32(block.timestamp); | ||
| } | ||
|
|
||
| /// @dev Update the config of the bucket. An enabled bucket is refilled with the old config and then | ||
| /// capped at the new capacity, so updating the config never grants extra tokens. A disabled bucket | ||
| /// starts full once enabled, and a disabled config must be all zero. | ||
| /// @param _bucket The token bucket. | ||
| /// @param _config The new config. | ||
| function setConfig(TokenBucket storage _bucket, Config memory _config) internal { | ||
| if (_config.isEnabled ? _config.rate > _config.capacity : (_config.capacity != 0 || _config.rate != 0)) { | ||
| revert ErrInvalidRateLimitConfig(_config.isEnabled, _config.capacity, _config.rate); | ||
| } | ||
|
|
||
| uint256 _tokens = _bucket.isEnabled | ||
| ? _refill(_bucket.tokens, _bucket.capacity, _bucket.rate, _bucket.lastUpdated) | ||
| : _config.capacity; | ||
| _bucket.tokens = uint128(_tokens < _config.capacity ? _tokens : _config.capacity); | ||
| _bucket.lastUpdated = uint32(block.timestamp); | ||
| _bucket.isEnabled = _config.isEnabled; | ||
| _bucket.capacity = _config.capacity; | ||
| _bucket.rate = _config.rate; | ||
| } | ||
|
|
||
| /// @dev Return the bucket refilled to the current block. | ||
| /// @param _bucket The token bucket. | ||
| function currentState(TokenBucket memory _bucket) internal view returns (TokenBucket memory) { | ||
| _bucket.tokens = uint128(_refill(_bucket.tokens, _bucket.capacity, _bucket.rate, _bucket.lastUpdated)); | ||
| _bucket.lastUpdated = uint32(block.timestamp); | ||
| return _bucket; | ||
| } | ||
|
|
||
| /********************* | ||
| * Private Functions * | ||
| *********************/ | ||
|
|
||
| /// @dev `_rate * elapsed` fits in uint256 since `_rate` is a uint128 and the timestamps are uint32. | ||
| function _refill( | ||
| uint256 _tokens, | ||
| uint256 _capacity, | ||
| uint256 _rate, | ||
| uint256 _lastUpdated | ||
| ) private view returns (uint256) { | ||
| uint256 _refilled = _tokens + (block.timestamp - _lastUpdated) * _rate; | ||
| return _refilled < _capacity ? _refilled : _capacity; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.