Skip to content

Commit

Permalink
v0 override and realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeNervoXS committed Dec 4, 2024
1 parent e985361 commit 94b422a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
65 changes: 31 additions & 34 deletions contracts/DistributionCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract DistributionCreator is UUPSHelper, ReentrancyGuardUpgradeable {
event FeeRecipientUpdated(address indexed _feeRecipient);
event FeesSet(uint256 _fees);
event CampaignOverride(bytes32 _campaignId, CampaignParameters campaign);
event CampaignReallocation(bytes32 _campaignId, address indexed from, address indexed to);
event CampaignReallocation(bytes32 _campaignId, address[] indexed from, address indexed to);
event CampaignSpecificFeesSet(uint32 campaignType, uint256 _fees);
event MessageUpdated(bytes32 _messageHash);
event NewCampaign(CampaignParameters campaign);
Expand Down Expand Up @@ -277,7 +277,15 @@ contract DistributionCreator is UUPSHelper, ReentrancyGuardUpgradeable {
/// @dev Some fields in the new campaign parameters will be disregarded anyway (like the amount)
function overrideCampaign(bytes32 _campaignId, CampaignParameters memory newCampaign) external {
CampaignParameters memory _campaign = campaign(_campaignId);
if (_campaign.creator != msg.sender) revert InvalidOverride();
if (
_campaign.creator != msg.sender ||
newCampaign.rewardToken != _campaign.rewardToken ||
newCampaign.amount != _campaign.amount ||
newCampaign.startTimestamp != _campaign.startTimestamp ||
// TODO we may want to be able to override the duration
// but we need to make sure that rewards/s is not an invariant in the engine
newCampaign.duration != _campaign.duration
) revert InvalidOverride();
campaignOverrides[_campaignId] = newCampaign;
campaignOverridesBlocks[_campaignId].push(block.number);
emit CampaignOverride(_campaignId, newCampaign);
Expand All @@ -286,12 +294,29 @@ contract DistributionCreator is UUPSHelper, ReentrancyGuardUpgradeable {
/// @notice Reallocates rewards of a given campaign from one address to another
/// @dev To prevent manipulations by campaign creators, this function can only be called by the
/// initial campaign creator if the `from` address has never claimed any reward on the chain
/// @dev Compute engine should also make sure when reallocating rewards that `from` claimed amount
/// is still 0 - otherwise double allocation can happen
/// @dev It is meant to be used for the case of addresses accruing rewards but unable to claim them
function reallocateCampaignRewards(bytes32 _campaignId, address from, address to) external {
function reallocateCampaignRewards(bytes32 _campaignId, address[] memory froms, address to) external {
CampaignParameters memory _campaign = campaign(_campaignId);
(uint208 amount, uint48 timestamp, ) = Distributor(distributor).claimed(from, _campaign.rewardToken);
if (_campaign.creator != msg.sender || amount != 0 || timestamp != 0) revert InvalidOverride();
emit CampaignReallocation(_campaignId, from, to);
if (_campaign.creator != msg.sender) revert InvalidOverride();

uint256 fromsLength = froms.length;
address[] memory successfullFrom = new address[](fromsLength);
uint256 count = 0;
for (uint256 i; i < fromsLength; i++) {
(uint208 amount, uint48 timestamp, ) = Distributor(distributor).claimed(froms[i], _campaign.rewardToken);
if (amount == 0 && timestamp == 0) {
successfullFrom[count] = froms[i];
count++;
}
}
assembly {
mstore(successfullFrom, count)
}

if (count == 0) revert InvalidOverride();
emit CampaignReallocation(_campaignId, successfullFrom, to);
}

/*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -316,34 +341,6 @@ contract DistributionCreator is UUPSHelper, ReentrancyGuardUpgradeable {
return campaignList[campaignLookup(_campaignId)];
}

/// @notice Returns the block numbers at which a campaign has been overriden
/// @dev Reverts if the campaign has not been overriden
/// @dev There cannot be two overrides at the same block
function getCampaignOverrideBlocks(bytes32 _campaignId) public view returns (uint256[] memory) {
uint256 length;
uint256 returnSize = campaignOverridesBlocks[_campaignId].length;
if (returnSize == 0) revert NoOverrideForCampaign();
uint256[] memory blockOverrides = new uint256[](returnSize);
uint32 i;
uint256 lastBlockNumber;
while (i < returnSize) {
uint256 blockOverrideNumber = campaignOverridesBlocks[_campaignId][i];
if (blockOverrideNumber != lastBlockNumber) {
blockOverrides[length] = blockOverrideNumber;
length += 1;
lastBlockNumber = blockOverrideNumber;
}
unchecked {
++i;
}
if (length == returnSize) break;
}
assembly {
mstore(blockOverrides, length)
}
return blockOverrides;
}

/// @notice Returns the campaign ID for a given campaign
/// @dev The campaign ID is computed as the hash of the following parameters:
/// - `campaign.chainId`
Expand Down
24 changes: 13 additions & 11 deletions contracts/Distributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ contract Distributor is UUPSHelper {
uint256[] calldata amounts,
bytes32[][] calldata proofs
) external {
bytes memory data;
address[] memory recipients = new address[](users.length);
_claim(users, tokens, amounts, proofs, recipients, data);
bytes[] memory datas = new bytes[](users.length);
_claim(users, tokens, amounts, proofs, recipients, datas);
}

/// @notice Same as the function above except that for each token claimed, the caller may set different
Expand All @@ -234,9 +234,9 @@ contract Distributor is UUPSHelper {
uint256[] calldata amounts,
bytes32[][] calldata proofs,
address[] calldata recipients,
bytes memory data
bytes[] memory datas
) external {
_claim(users, tokens, amounts, proofs, recipients, data);
_claim(users, tokens, amounts, proofs, recipients, datas);
}

/// @notice Returns the Merkle root that is currently live for the contract
Expand Down Expand Up @@ -305,11 +305,11 @@ contract Distributor is UUPSHelper {
emit TreeUpdated(_tree.merkleRoot, _tree.ipfsHash, _endOfPeriod);
}

/// @notice Adds or removes EOAs which are trusted to update the Merkle root
function toggleTrusted(address eoa) external onlyGovernor {
uint256 trustedStatus = 1 - canUpdateMerkleRoot[eoa];
canUpdateMerkleRoot[eoa] = trustedStatus;
emit TrustedToggled(eoa, trustedStatus == 1);
/// @notice Adds or removes addresses which are trusted to update the Merkle root
function toggleTrusted(address trustAddress) external onlyGovernor {
uint256 trustedStatus = 1 - canUpdateMerkleRoot[trustAddress];
canUpdateMerkleRoot[trustAddress] = trustedStatus;
emit TrustedToggled(trustAddress, trustedStatus == 1);
}

/// @notice Prevents future contract upgrades
Expand Down Expand Up @@ -384,21 +384,23 @@ contract Distributor is UUPSHelper {
uint256[] calldata amounts,
bytes32[][] calldata proofs,
address[] memory recipients,
bytes memory data
bytes[] memory datas
) internal nonReentrant {
uint256 usersLength = users.length;
if (
usersLength == 0 ||
usersLength != tokens.length ||
usersLength != amounts.length ||
usersLength != proofs.length ||
usersLength != recipients.length
usersLength != recipients.length ||
usersLength != datas.length
) revert InvalidLengths();

for (uint256 i; i < usersLength; ) {
address user = users[i];
address token = tokens[i];
uint256 amount = amounts[i];
bytes memory data = datas[i];

// Only approved operator can claim for `user`
if (msg.sender != user && tx.origin != user && operators[user][msg.sender] == 0) revert NotWhitelisted();
Expand Down
2 changes: 1 addition & 1 deletion lib/forge-std
Submodule forge-std updated 49 files
+134 −0 .github/workflows/ci.yml
+29 −0 .github/workflows/sync.yml
+0 −27 .github/workflows/tests.yml
+1 −1 .gitignore
+1 −1 LICENSE-APACHE
+1 −1 LICENSE-MIT
+8 −4 README.md
+19 −0 foundry.toml
+1 −1 lib/ds-test
+4 −4 package.json
+35 −0 src/Base.sol
+24 −41 src/Script.sol
+376 −0 src/StdAssertions.sol
+236 −0 src/StdChains.sol
+817 −0 src/StdCheats.sol
+15 −0 src/StdError.sol
+107 −0 src/StdInvariant.sol
+126 −61 src/StdJson.sol
+43 −0 src/StdMath.sol
+378 −0 src/StdStorage.sol
+333 −0 src/StdStyle.sol
+198 −0 src/StdUtils.sol
+29 −1,134 src/Test.sol
+516 −195 src/Vm.sol
+406 −386 src/console2.sol
+105 −0 src/interfaces/IERC1155.sol
+12 −0 src/interfaces/IERC165.sol
+43 −0 src/interfaces/IERC20.sol
+190 −0 src/interfaces/IERC4626.sol
+164 −0 src/interfaces/IERC721.sol
+73 −0 src/interfaces/IMulticall3.sol
+13,248 −0 src/safeconsole.sol
+0 −20 src/test/Script.t.sol
+0 −602 src/test/StdAssertions.t.sol
+0 −282 src/test/StdCheats.t.sol
+0 −200 src/test/StdMath.t.sol
+1,015 −0 test/StdAssertions.t.sol
+220 −0 test/StdChains.t.sol
+610 −0 test/StdCheats.t.sol
+15 −21 test/StdError.t.sol
+212 −0 test/StdMath.t.sol
+120 −126 test/StdStorage.t.sol
+110 −0 test/StdStyle.t.sol
+342 −0 test/StdUtils.t.sol
+10 −0 test/compilation/CompilationScript.sol
+10 −0 test/compilation/CompilationScriptBase.sol
+10 −0 test/compilation/CompilationTest.sol
+10 −0 test/compilation/CompilationTestBase.sol
+0 −0 test/fixtures/broadcast.log.json
4 changes: 3 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
forge-std/=lib/forge-std/src/
oz/=node_modules/@openzeppelin/contracts/
utils/=lib/utils

0 comments on commit 94b422a

Please sign in to comment.