fix: add reentrancy guards and dust absorption to distribution (#85)#124
Open
RonTuretzky wants to merge 2 commits into
Open
fix: add reentrancy guards and dust absorption to distribution (#85)#124RonTuretzky wants to merge 2 commits into
RonTuretzky wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the distribution flow by adding upgradeable reentrancy guards to distribution managers/strategies and eliminating rounding “dust” by ensuring the full yield amount is always distributed (with the last recipient/strategy absorbing any remainder).
Changes:
- Add
ReentrancyGuardUpgradeableand applynonReentranttoclaimAndDistribute()(managers) anddistribute()(strategies), with initializer wiring via__ReentrancyGuard_init(). - Fix integer-division remainder handling so the last recipient/strategy absorbs rounding remainder instead of leaving tokens stranded.
- Add a large test suite covering dust behavior and various distribution edge cases.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/DistributionStrategies.t.sol | Adds tests and mocks for dust-absorption and (partially) reentrancy behavior across strategies/managers. |
| src/abstract/AbstractDistributionStrategy.sol | Adds ReentrancyGuardUpgradeable inheritance and initializes it during strategy initialization. |
| src/implementation/strategies/EqualDistributionStrategy.sol | Makes distribute() non-reentrant and ensures last recipient receives remainder. |
| src/implementation/strategies/VotingDistributionStrategy.sol | Makes distribute() non-reentrant and ensures last recipient receives remainder. |
| src/base/MultiStrategyDistributionManager.sol | Makes claimAndDistribute() non-reentrant and ensures last strategy receives remainder. |
| src/base/BaseDistributionManager.sol | Makes claimAndDistribute() non-reentrant and initializes reentrancy guard. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+15
to
+21
| /// @dev Concrete strategies implement `distribute` to define how yield is allocated. | ||
| /// Inherits ReentrancyGuardUpgradeable to protect all distribute() implementations. | ||
| abstract contract AbstractDistributionStrategy is | ||
| Initializable, | ||
| IDistributionStrategy, | ||
| OwnableUpgradeable, | ||
| ReentrancyGuardUpgradeable |
Comment on lines
+173
to
+179
| function burn(uint256 amount, address receiver) external override { | ||
| balanceOf[msg.sender] -= amount; | ||
| totalSupply -= amount; | ||
| emit Transfer(msg.sender, address(0), amount); | ||
| totalSupply += amount; | ||
| balanceOf[receiver] += amount; | ||
| emit Transfer(address(0), receiver, amount); |
Comment on lines
+254
to
+280
| /// @dev Malicious strategy that attempts to re-enter claimAndDistribute during distribute() | ||
| contract ReentrantStrategy is IDistributionStrategy { | ||
| MultiStrategyDistributionManager public manager; | ||
| IERC20 public token; | ||
| bool public shouldReenter; | ||
|
|
||
| constructor(address _manager, address _token) { | ||
| manager = MultiStrategyDistributionManager(_manager); | ||
| token = IERC20(_token); | ||
| } | ||
|
|
||
| function setShouldReenter(bool _shouldReenter) external { | ||
| shouldReenter = _shouldReenter; | ||
| } | ||
|
|
||
| function distribute(uint256) external override { | ||
| if (shouldReenter) { | ||
| shouldReenter = false; | ||
| manager.claimAndDistribute(); | ||
| } | ||
| } | ||
|
|
||
| function withdrawToken(address to, uint256 amount) external { | ||
| token.transfer(to, amount); | ||
| } | ||
| } | ||
|
|
1afcdfe to
f51725e
Compare
- Add ReentrancyGuardUpgradeable to AbstractDistributionStrategy, BaseDistributionManager, and MultiStrategyDistributionManager - Last recipient/strategy absorbs rounding remainder (up to N-1 wei) - Remove redundant strategy length check in MultiStrategyDM - Add 35 tests covering dust absorption, reentrancy, and edge cases
8545c16 to
8f03b58
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ReentrancyGuardUpgradeableto distribution contractsCloses #85
Supersedes #108
Stack: PR 7 of 10 (0.0.2) — stacked on #123