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

Fix/revert on already set values #92

Merged
merged 14 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions src/UniversalRewardsDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ contract UniversalRewardsDistributor is IUniversalRewardsDistributor {
if (timelock == 0) {
_setRoot(newRoot, newIpfsHash);
} else {
require(newRoot != pendingRoot.root || newIpfsHash != pendingRoot.ipfsHash, ErrorsLib.ALREADY_SET);
require(newRoot != root || newIpfsHash != ipfsHash, ErrorsLib.ROOT_ALREADY_SET);

pendingRoot = PendingRoot(block.timestamp, newRoot, newIpfsHash);
emit EventsLib.RootProposed(newRoot, newIpfsHash);
}
Expand Down Expand Up @@ -147,6 +150,8 @@ contract UniversalRewardsDistributor is IUniversalRewardsDistributor {
/// @dev This function can only be called by the owner of the distribution.
/// @dev If the timelock is reduced, it can only be updated after the timelock has expired.
function setTimelock(uint256 newTimelock) external onlyOwner {
require(newTimelock != timelock, ErrorsLib.ALREADY_SET);

if (newTimelock < timelock) {
require(
pendingRoot.submittedAt == 0 || pendingRoot.submittedAt + timelock <= block.timestamp,
Expand All @@ -161,6 +166,8 @@ contract UniversalRewardsDistributor is IUniversalRewardsDistributor {
/// @param updater The address of the root updater.
/// @param active Whether the root updater should be active or not.
function setRootUpdater(address updater, bool active) external onlyOwner {
require(isUpdater[updater] != active, ErrorsLib.ALREADY_SET);

isUpdater[updater] = active;

emit EventsLib.RootUpdaterSet(updater, active);
Expand All @@ -180,6 +187,8 @@ contract UniversalRewardsDistributor is IUniversalRewardsDistributor {

/// @notice Sets the `owner` of the distribution to `newOwner`.
function setOwner(address newOwner) external onlyOwner {
require(newOwner != owner, ErrorsLib.ALREADY_SET);

_setOwner(newOwner);
}

Expand All @@ -189,6 +198,8 @@ contract UniversalRewardsDistributor is IUniversalRewardsDistributor {
/// @dev Deletes the pending root.
/// @dev Warning: The `newIpfsHash` might not correspond to the `newRoot`.
function _setRoot(bytes32 newRoot, bytes32 newIpfsHash) internal {
require(newRoot != root || newIpfsHash != ipfsHash, ErrorsLib.ROOT_ALREADY_SET);

julien-devatom marked this conversation as resolved.
Show resolved Hide resolved
root = newRoot;
ipfsHash = newIpfsHash;

Expand Down
6 changes: 6 additions & 0 deletions src/libraries/ErrorsLib.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if we should make the revert explicit when the URD has already been deployed, by adding a check in the UrdFactory. I think no because it would be an edge case and also it would not be kind of awkward to do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oumar-fall actually stumbled upon this issue today

Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ library ErrorsLib {

/// @notice Thrown when rewards have already been claimed.
string internal constant ALREADY_CLAIMED = "already claimed";

/// @notice Thrown when the value is already set.
string internal constant ALREADY_SET = "already set";

/// @notice Thrown when the submitted root (pending or not) is the same as the current one.
julien-devatom marked this conversation as resolved.
Show resolved Hide resolved
string internal constant ROOT_ALREADY_SET = "root already set";
julien-devatom marked this conversation as resolved.
Show resolved Hide resolved
}
97 changes: 96 additions & 1 deletion test/UniversalRewardsDistributorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {UniversalRewardsDistributor} from "../src/UniversalRewardsDistributor.so
import {EventsLib} from "../src/libraries/EventsLib.sol";

import {Merkle} from "../lib/murky/src/Merkle.sol";

MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
import "../lib/forge-std/src/Test.sol";

contract UniversalRewardsDistributorTest is Test {
Expand Down Expand Up @@ -153,6 +152,62 @@ contract UniversalRewardsDistributorTest is Test {
distributionWithoutTimeLock.submitRoot(DEFAULT_ROOT, DEFAULT_IPFS_HASH);
}

function testSubmitRootWithPreviousPendingRootShouldRevert(bytes32 newRoot, bytes32 newIpfsHash) public {
vm.assume(newRoot != distributionWithTimeLock.root() && newIpfsHash != distributionWithTimeLock.ipfsHash());

vm.startPrank(owner);
distributionWithTimeLock.submitRoot(newRoot, newIpfsHash);

vm.expectRevert(bytes(ErrorsLib.ALREADY_SET));
distributionWithTimeLock.submitRoot(newRoot, newIpfsHash);

vm.stopPrank();
}

function testSubmitRootWithCurrentRootAndNoTimelockShouldRevert(bytes32 newRoot, bytes32 newIpfsHash) public {
vm.assume(newRoot != distributionWithTimeLock.root() && newIpfsHash != distributionWithTimeLock.ipfsHash());

vm.startPrank(owner);
distributionWithoutTimeLock.submitRoot(newRoot, newIpfsHash);

vm.expectRevert(bytes(ErrorsLib.ROOT_ALREADY_SET));
distributionWithoutTimeLock.submitRoot(newRoot, newIpfsHash);

vm.stopPrank();
}

function testSubmitRootWithCurrentRootShouldRevert(bytes32 newRoot, bytes32 newIpfsHash) public {
vm.assume(newRoot != distributionWithTimeLock.root() && newIpfsHash != distributionWithTimeLock.ipfsHash());

vm.startPrank(owner);
distributionWithTimeLock.setRoot(newRoot, newIpfsHash);

vm.expectRevert(bytes(ErrorsLib.ROOT_ALREADY_SET));
distributionWithTimeLock.submitRoot(newRoot, newIpfsHash);
vm.stopPrank();
}

function testSubmitRootTwiceShouldWorkWhenModifyingIpfsHash(
bytes32 newRoot,
bytes32 newIpfsHash,
bytes32 secondIpfsHash
) public {
vm.assume(
newRoot != distributionWithTimeLock.root() && newIpfsHash != distributionWithTimeLock.ipfsHash()
&& secondIpfsHash != newIpfsHash
);

vm.startPrank(owner);
distributionWithTimeLock.setRoot(newRoot, newIpfsHash);

vm.expectEmit(address(distributionWithTimeLock));
emit EventsLib.RootProposed(newRoot, secondIpfsHash);
distributionWithTimeLock.submitRoot(newRoot, secondIpfsHash);
vm.stopPrank();

assertEq(_getPendingRoot(distributionWithTimeLock).ipfsHash, secondIpfsHash);
}

function testSubmitRootWithTimelockAsOwner() public {
vm.prank(owner);
vm.expectEmit(address(distributionWithTimeLock));
Expand Down Expand Up @@ -271,6 +326,8 @@ contract UniversalRewardsDistributorTest is Test {
function testSetTimelockShouldChangeTheDistributionTimelock(uint256 newTimelock) public {
newTimelock = bound(newTimelock, 0, type(uint256).max);

vm.assume(newTimelock != distributionWithoutTimeLock.timelock());

vm.prank(owner);
vm.expectEmit(address(distributionWithoutTimeLock));
emit EventsLib.TimelockSet(newTimelock);
Expand All @@ -279,6 +336,19 @@ contract UniversalRewardsDistributorTest is Test {
assertEq(distributionWithoutTimeLock.timelock(), newTimelock);
}

function testSetTimelockShouldRevertIfSameValue(uint256 newTimelock) public {
newTimelock = bound(newTimelock, 0, type(uint256).max);

vm.assume(newTimelock != distributionWithoutTimeLock.timelock());

vm.prank(owner);
distributionWithoutTimeLock.setTimelock(newTimelock);

vm.prank(owner);
vm.expectRevert(bytes(ErrorsLib.ALREADY_SET));
distributionWithoutTimeLock.setTimelock(newTimelock);
}

function testSetTimelockShouldIncreaseTheQueueTimestamp(
uint256 timeElapsed,
uint256 newTimelock,
Expand Down Expand Up @@ -352,6 +422,8 @@ contract UniversalRewardsDistributorTest is Test {
}

function testSetRootUpdaterShouldAddOrRemoveRootUpdater(address newUpdater, bool active) public {
vm.assume(distributionWithoutTimeLock.isUpdater(newUpdater) != active);

vm.prank(owner);
vm.expectEmit(address(distributionWithoutTimeLock));
emit EventsLib.RootUpdaterSet(newUpdater, active);
Expand All @@ -360,6 +432,17 @@ contract UniversalRewardsDistributorTest is Test {
assertEq(distributionWithoutTimeLock.isUpdater(newUpdater), active);
}

function testSetRootUpdaterShouldRevertIfAlreadySet(address newUpdater, bool active) public {
vm.assume(distributionWithoutTimeLock.isUpdater(newUpdater) != active);

vm.prank(owner);
distributionWithoutTimeLock.setRootUpdater(newUpdater, active);

vm.prank(owner);
vm.expectRevert(bytes(ErrorsLib.ALREADY_SET));
distributionWithoutTimeLock.setRootUpdater(newUpdater, active);
}

function testSetRootUpdaterShouldRevertIfNotOwner(address caller, bool active) public {
vm.assume(caller != owner);

Expand Down Expand Up @@ -418,6 +501,17 @@ contract UniversalRewardsDistributorTest is Test {
distributionWithTimeLock.setOwner(newOwner);
}

function testSetOwnerShouldRevertIfAlreadySet(address newOwner) public {
vm.assume(newOwner != owner);

vm.prank(owner);
distributionWithTimeLock.setOwner(newOwner);

vm.prank(newOwner);
vm.expectRevert(bytes(ErrorsLib.ALREADY_SET));
distributionWithTimeLock.setOwner(newOwner);
}

function testClaimShouldFollowTheMerkleDistribution(uint256 claimable, uint8 size) public {
claimable = bound(claimable, 1 ether, 1000 ether);
uint256 boundedSize = bound(size, 2, MAX_RECEIVERS);
Expand Down Expand Up @@ -452,6 +546,7 @@ contract UniversalRewardsDistributorTest is Test {
}

function testClaimShouldReturnsTheAmountClaimed(uint256 claimable) public {
vm.assume(claimable > 0);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
claimable = bound(claimable, 1 ether, 1000 ether);

(bytes32[] memory data, bytes32 root) = _setupRewards(claimable, 2);
Expand Down