Skip to content
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

Add reentrancy guard to claimPrizes #32

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/prb-math
Submodule prb-math updated 55 files
+0 −1 .gitattributes
+1 −1 .github/FUNDING.yml
+18 −38 .github/workflows/ci.yml
+26 −0 .github/workflows/multibuild.yml
+1 −1 .github/workflows/release.yml
+1 −1 .github/workflows/sync.yml
+2 −1 .gitignore
+1 −1 .prettierignore
+1 −0 .solhint.json
+0 −1 .vscode/settings.json
+17 −0 CHANGELOG.md
+33 −8 README.md
+ bun.lockb
+2 −3 foundry.toml
+8 −8 package.json
+0 −714 pnpm-lock.yaml
+0 −1 remappings.txt
+1 −1 src/sd1x18/Constants.sol
+8 −0 src/sd59x18/Constants.sol
+10 −2 src/sd59x18/Math.sol
+1 −1 src/ud2x18/Constants.sol
+14 −2 test/Base.t.sol
+4 −4 test/fuzz/casting/CastingUint128.t.sol
+4 −4 test/fuzz/casting/CastingUint256.t.sol
+4 −4 test/fuzz/casting/CastingUint40.t.sol
+9 −9 test/fuzz/sd1x18/casting/Casting.t.sol
+11 −11 test/fuzz/sd59x18/casting/Casting.t.sol
+19 −19 test/fuzz/sd59x18/helpers/Helpers.t.sol
+4 −4 test/fuzz/sd59x18/math/pow/pow.t.sol
+9 −9 test/fuzz/ud2x18/casting/Casting.t.sol
+10 −10 test/fuzz/ud60x18/casting/Casting.t.sol
+18 −18 test/fuzz/ud60x18/helpers/Helpers.t.sol
+4 −4 test/fuzz/ud60x18/math/pow/pow.t.sol
+1 −1 test/unit/sd59x18/math/abs/abs.t.sol
+1 −1 test/unit/sd59x18/math/avg/avg.t.sol
+1 −1 test/unit/sd59x18/math/ceil/ceil.t.sol
+6 −6 test/unit/sd59x18/math/exp/exp.t.sol
+6 −7 test/unit/sd59x18/math/exp2/exp2.t.sol
+1 −1 test/unit/sd59x18/math/floor/floor.t.sol
+1 −1 test/unit/sd59x18/math/frac/frac.t.sol
+5 −5 test/unit/sd59x18/math/pow/pow.t.sol
+1 −1 test/unit/sd59x18/math/powu/powu.t.sol
+1 −1 test/unit/sd59x18/math/sqrt/sqrt.t.sol
+4 −4 test/unit/ud60x18/UD60x18.t.sol
+1 −1 test/unit/ud60x18/math/avg/avg.t.sol
+1 −1 test/unit/ud60x18/math/ceil/ceil.t.sol
+1 −1 test/unit/ud60x18/math/exp/exp.t.sol
+1 −1 test/unit/ud60x18/math/exp2/exp2.t.sol
+1 −1 test/unit/ud60x18/math/floor/floor.t.sol
+1 −1 test/unit/ud60x18/math/frac/frac.t.sol
+5 −5 test/unit/ud60x18/math/pow/pow.t.sol
+1 −1 test/unit/ud60x18/math/powu/powu.t.sol
+1 −1 test/unit/ud60x18/math/sqrt/sqrt.t.sol
+46 −46 test/utils/Assertions.sol
+16 −16 test/utils/Utils.sol
2 changes: 1 addition & 1 deletion lib/pt-v5-claimable-interface
2 changes: 1 addition & 1 deletion lib/pt-v5-prize-pool
Submodule pt-v5-prize-pool updated 1 files
+5 −0 funding.json
2 changes: 1 addition & 1 deletion lib/solmate
Submodule solmate updated 1 files
+1 −1 lib/ds-test
5 changes: 3 additions & 2 deletions src/Claimer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UD2x18 } from "prb-math/UD2x18.sol";
import { UD60x18, convert } from "prb-math/UD60x18.sol";
import { PrizePool } from "pt-v5-prize-pool/PrizePool.sol";
import { SafeCast } from "openzeppelin/utils/math/SafeCast.sol";
import { ReentrancyGuard } from "openzeppelin/security/ReentrancyGuard.sol";

import { LinearVRGDALib } from "./libraries/LinearVRGDALib.sol";
import { IClaimable } from "pt-v5-claimable-interface/interfaces/IClaimable.sol";
Expand All @@ -32,7 +33,7 @@ error TimeToReachMaxFeeZero();
/// @title Variable Rate Gradual Dutch Auction (VRGDA) Claimer
/// @author G9 Software Inc.
/// @notice This contract uses a variable rate gradual dutch auction to incentivize prize claims on behalf of others. Fees for each canary tier is set to the respective tier's prize size.
contract Claimer {
contract Claimer is ReentrancyGuard {

/// @notice Emitted when a claim reverts
/// @param vault The vault for which the claim failed
Expand Down Expand Up @@ -94,7 +95,7 @@ contract Claimer {
uint32[][] calldata _prizeIndices,
address _feeRecipient,
uint256 _minFeePerClaim
) external returns (uint256 totalFees) {
) external nonReentrant returns (uint256 totalFees) {
bool feeRecipientZeroAddress = address(0) == _feeRecipient;
if (feeRecipientZeroAddress && _minFeePerClaim != 0) {
revert FeeRecipientZeroAddress();
Expand Down
27 changes: 27 additions & 0 deletions test/Claimer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SD59x18 } from "prb-math/SD59x18.sol";
import { PrizePool, AlreadyClaimed } from "pt-v5-prize-pool/PrizePool.sol";
import { IClaimable } from "pt-v5-claimable-interface/interfaces/IClaimable.sol";
import { LinearVRGDALib } from "../src/libraries/LinearVRGDALib.sol";
import { ReentrancyMock } from "./mock/ReentrancyMock.sol";

// Custom Errors
error ClaimArraySizeMismatch(uint256 winnersLength, uint256 prizeIndicesLength);
Expand Down Expand Up @@ -126,6 +127,32 @@ contract ClaimerTest is Test {
assertEq(totalFees, NO_SALES_100_SECONDS_BEHIND_SCHEDULE_FEE, "Total fees");
}

function testClaimPrizes_reentrancyGuard() public {
address[] memory winners = newWinners(winner1, winner2);
uint32[][] memory prizeIndices = newPrizeIndices(2, 1);

address[] memory reentrancyWinners = newWinners(winner3);
uint32[][] memory reentrancyPrizeIndices = newPrizeIndices(1, 1);

ReentrancyMock reentrancyVault = new ReentrancyMock(address(claimer));
reentrancyVault.setReentrancyClaimInfo(
winner1, // only triggerred by winner1
IClaimable(address(reentrancyVault)),
1,
reentrancyWinners,
reentrancyPrizeIndices,
address(this),
0
);

mockPrizePool(1, -100, 0);

vm.expectEmit();
emit ClaimError(IClaimable(address(reentrancyVault)), 1, winner1, 0, abi.encodeWithSignature("Error(string)", "ReentrancyGuard: reentrant call"));
uint256 totalFees = claimer.claimPrizes(reentrancyVault, 1, winners, prizeIndices, address(this), 0);
assertLt(totalFees, NO_SALES_100_SECONDS_BEHIND_SCHEDULE_FEE, "Total fees"); // 2 fee-split expected, but one fails so the received fees are slightly less
}

function testClaimPrizes_singleNoFeeSavesGas() public {
// With fee
address[] memory winners = newWinners(winner1);
Expand Down
56 changes: 56 additions & 0 deletions test/mock/ReentrancyMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { IClaimable } from "pt-v5-claimable-interface/interfaces/IClaimable.sol";
import { Claimer } from "../../src/Claimer.sol";

contract ReentrancyMock is IClaimable {

address public claimer;
address public badGuy;
bytes public reentrancyCalldata;

constructor(address claimer_) {
claimer = claimer_;
}

function claimPrize(
address _winner,
uint8 _tier,
uint32 _prizeIndex,
uint96 _reward,
address _rewardRecipient
) external returns (uint256) {
if (_winner == badGuy) {
(bool success, bytes memory data) = claimer.call(reentrancyCalldata);
require(success == false, "reentrancy succeeded...");
assembly {
revert(add(32, data), mload(data))
}
}
return 1;
}

function setReentrancyClaimInfo(
address _badGuy,
IClaimable _vault,
uint8 _tier,
address[] calldata _winners,
uint32[][] calldata _prizeIndices,
address _feeRecipient,
uint256 _minFeePerClaim
) external returns (uint256) {
badGuy = _badGuy;
reentrancyCalldata = abi.encodeWithSelector(
Claimer.claimPrizes.selector,
_vault,
_tier,
_winners,
_prizeIndices,
_feeRecipient,
_minFeePerClaim
);
}

}
Loading