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

Migrate multiple queued withdrawals #228

Merged
merged 2 commits into from
Oct 6, 2023
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
58 changes: 33 additions & 25 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -363,35 +363,43 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg
emit WithdrawalCompleted(withdrawalRoot);
}

/// @notice Migrates an existing queued withdrawal from the StrategyManager contract to this contract.
/// @notice Migrates an array of queued withdrawals from the StrategyManager contract to this contract.
/// @dev This function is expected to be removed in the next upgrade, after all queued withdrawals have been migrated.
function migrateQueuedWithdrawal(IStrategyManager.DeprecatedStruct_QueuedWithdrawal memory strategyManagerWithdrawalToMigrate) external {
// check for existence and delete the old storage
bytes32 oldWithdrawalRoot = strategyManager.calculateWithdrawalRoot(strategyManagerWithdrawalToMigrate);
strategyManager.migrateQueuedWithdrawal(oldWithdrawalRoot);

address staker = strategyManagerWithdrawalToMigrate.staker;
// Create queue entry and increment withdrawal nonce
uint256 nonce = cumulativeWithdrawalsQueued[staker];
cumulativeWithdrawalsQueued[staker]++;

Withdrawal memory migratedWithdrawal = Withdrawal({
staker: staker,
delegatedTo: strategyManagerWithdrawalToMigrate.delegatedAddress,
withdrawer: strategyManagerWithdrawalToMigrate.withdrawerAndNonce.withdrawer,
nonce: nonce,
startBlock: strategyManagerWithdrawalToMigrate.withdrawalStartBlock,
strategies: strategyManagerWithdrawalToMigrate.strategies,
shares: strategyManagerWithdrawalToMigrate.shares
});
function migrateQueuedWithdrawals(IStrategyManager.DeprecatedStruct_QueuedWithdrawal[] memory strategyManagerWithdrawalsToMigrate) external {
for(uint256 i = 0; i < strategyManagerWithdrawalsToMigrate.length;) {
IStrategyManager.DeprecatedStruct_QueuedWithdrawal memory strategyManagerWithdrawalToMigrate = strategyManagerWithdrawalsToMigrate[i];
// Delete withdrawal root from strateyManager
(bool isDeleted, bytes32 oldWithdrawalRoot) = strategyManager.migrateQueuedWithdrawal(strategyManagerWithdrawalToMigrate);
// If old storage is deleted from strategyManager
if (isDeleted) {
address staker = strategyManagerWithdrawalToMigrate.staker;
// Create queue entry and increment withdrawal nonce
uint256 nonce = cumulativeWithdrawalsQueued[staker];
cumulativeWithdrawalsQueued[staker]++;

Withdrawal memory migratedWithdrawal = Withdrawal({
staker: staker,
delegatedTo: strategyManagerWithdrawalToMigrate.delegatedAddress,
withdrawer: strategyManagerWithdrawalToMigrate.withdrawerAndNonce.withdrawer,
nonce: nonce,
startBlock: strategyManagerWithdrawalToMigrate.withdrawalStartBlock,
strategies: strategyManagerWithdrawalToMigrate.strategies,
shares: strategyManagerWithdrawalToMigrate.shares
});

// create the new storage
bytes32 newRoot = calculateWithdrawalRoot(migratedWithdrawal);
pendingWithdrawals[newRoot] = true;
// create the new storage
bytes32 newRoot = calculateWithdrawalRoot(migratedWithdrawal);
pendingWithdrawals[newRoot] = true;

emit WithdrawalQueued(newRoot, migratedWithdrawal);
emit WithdrawalQueued(newRoot, migratedWithdrawal);

emit WithdrawalMigrated(oldWithdrawalRoot, newRoot);
emit WithdrawalMigrated(oldWithdrawalRoot, newRoot);
}
unchecked {
++i;
}
}

}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/contracts/core/StrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,15 @@ contract StrategyManager is

/// @notice Function called by the DelegationManager as part of the process of transferring existing queued withdrawals from this contract to that contract.
/// @dev This function is expected to be removed in the next upgrade, after all queued withdrawals have been migrated.
function migrateQueuedWithdrawal(bytes32 existingWithdrawalRoot) external onlyDelegationManager {
// check for existence
require(withdrawalRootPending[existingWithdrawalRoot], "StrategyManager.migrateQueuedWithdrawal: withdrawal does not exist");

// delete the withdrawal
withdrawalRootPending[existingWithdrawalRoot] = false;
function migrateQueuedWithdrawal(DeprecatedStruct_QueuedWithdrawal memory queuedWithdrawal) external onlyDelegationManager returns(bool, bytes32) {
bytes32 existingWithdrawalRoot = calculateWithdrawalRoot(queuedWithdrawal);
bool isDeleted;
// Delete the withdrawal root if it exists
if (withdrawalRootPending[existingWithdrawalRoot]) {
withdrawalRootPending[existingWithdrawalRoot] = false;
isDeleted = true;
}
return (isDeleted, existingWithdrawalRoot);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/interfaces/IStrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ interface IStrategyManager {
address delegatedAddress;
}

function migrateQueuedWithdrawal(bytes32 existingWithdrawalRoot) external;
function migrateQueuedWithdrawal(DeprecatedStruct_QueuedWithdrawal memory queuedWithdrawal) external returns (bool, bytes32);

function calculateWithdrawalRoot(DeprecatedStruct_QueuedWithdrawal memory queuedWithdrawal) external pure returns (bytes32);
}
2 changes: 1 addition & 1 deletion src/test/mocks/StrategyManagerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ contract StrategyManagerMock is

function removeStrategiesFromDepositWhitelist(IStrategy[] calldata /*strategiesToRemoveFromWhitelist*/) external pure {}

function migrateQueuedWithdrawal(bytes32 existingWithdrawalRoot) external {}
function migrateQueuedWithdrawal(DeprecatedStruct_QueuedWithdrawal memory queuedWithdrawal) external returns (bool, bytes32) {}

function calculateWithdrawalRoot(DeprecatedStruct_QueuedWithdrawal memory queuedWithdrawal) external pure returns (bytes32) {}
}
Loading