From 363b5c769df37d8eb75828344c0f48651b20885c Mon Sep 17 00:00:00 2001 From: Yash Patil Date: Mon, 30 Sep 2024 13:57:54 -0700 Subject: [PATCH] feat: push --- src/contracts/core/AllocationManager.sol | 86 ++++++++++--------- .../core/AllocationManagerStorage.sol | 8 +- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/contracts/core/AllocationManager.sol b/src/contracts/core/AllocationManager.sol index e374bf348..8b6b7c32f 100644 --- a/src/contracts/core/AllocationManager.sol +++ b/src/contracts/core/AllocationManager.sol @@ -309,14 +309,10 @@ contract AllocationManager is ); bytes32 operatorSetKey = _encodeOperatorSet(allocation.operatorSets[i]); - // prep magnitudeUpdates in storage // Check that there is no pending allocation or deallocation - (bool hasPendingAllocationOrDeallocation) = - _hasPendingAllocationOrDeallocation(operator, allocation.strategy, operatorSetKey); - require( - !hasPendingAllocationOrDeallocation + !_hasPendingAllocationOrDeallocation(operator, allocation.strategy, operatorSetKey) PendingAllocationOrDeallocation() ); @@ -332,13 +328,14 @@ contract AllocationManager is // Newly configured magnitude is less than current value. // Therefore we handle this as a deallocation - // Note: MAX_PENDING_UPDATES == 1, so we do not have to decrement any allocations - - // 1. push PendingFreeMagnitude and respective array index into (op,opSet,Strategy) queued deallocations + // 1. Get the magnitude to deallocate and push to PendingFreeMagnitude uint64 magnitudeToDeallocate = uint64(currentMagnitude) - allocation.magnitudes[i]; - _queuedDeallocationIndices[operator][allocation.strategy][operatorSetKey].push( - _pendingFreeMagnitude[operator][allocation.strategy].length - ); + + _pendingMagnitudeUpdate[operator][allocation.strategy][operatorSetKey].push({ + key: deallocationCompletableTimestamp, + value: allocation.magnitudes[i] + }); + _pendingFreeMagnitude[operator][allocation.strategy].push( PendingFreeMagnitude({ magnitudeDiff: magnitudeToDeallocate, @@ -348,21 +345,28 @@ contract AllocationManager is // 2. decrement allocated magnitude // TODO: don't need to decrement since can have at most 1 pending allocation/deallocation - magnitudeUpdates.decrementAtAndFutureSnapshots({ - key: uint32(block.timestamp), - decrementValue: magnitudeToDeallocate - }); + // magnitudeUpdates.decrementAtAndFutureSnapshots({ + // key: uint32(block.timestamp), + // decrementValue: magnitudeToDeallocate + // }); } else if (allocation.magnitudes[i] > uint64(currentMagnitude)) { // Newly configured magnitude is greater than current value. // Therefore we handle this as an allocation - // 1. decrement free magnitude by incremented amount + // 1. Get the magnitude to allocate and push PendingMagnitudeUpdate uint64 magnitudeToAllocate = allocation.magnitudes[i] - uint64(currentMagnitude); + + _pendingMagnitudeUpdate[operator][allocation.strategy][operatorSetKey].push({ + key: allocationEffectTimestamp, + value: allocation.magnitudes[i] + }); + + // 2. decrement free magnitude by incremented amount OperatorMagnitudeInfo storage info = operatorMagnitudeInfo[operator][allocation.strategy]; require(info.freeMagnitude >= magnitudeToAllocate, InsufficientAllocatableMagnitude()); info.freeMagnitude -= magnitudeToAllocate; - // 2. allocate magnitude which will take effect + // 3. allocate magnitude which will take effect magnitudeUpdates.push({key: allocationEffectTimestamp, value: allocation.magnitudes[i]}); } } @@ -391,31 +395,29 @@ contract AllocationManager is hasPendingAllocationOrDeallocation = true; } - - - // Read current magnitude's respective array index and length. - (,uint256 latestAllocPos, uint256 allocLength) = - magnitudeUpdates.upperLookupRecentWithPos(uint32(block.timestamp)); - - if (latestAllocPos < allocLength - 1) { - numPendingAllocations = allocLength - 1 - latestAllocPos; - } - // Else, latestAllocPos >= allocLength - 1; - // latestAllocPos cannot be greater than length - 1, thus when pos == length - 1 there are no pending allocations - - // Get pending deallocations - uint256 deallocationsLength = _queuedDeallocationIndices[operator][strategy][operatorSetKey].length; - for (uint256 i = deallocationsLength; i > 0; --i) { - // index of pendingFreeMagnitude/deallocation to check for slashing - uint256 index = _queuedDeallocationIndices[operator][strategy][operatorSetKey][i - 1]; - - // If completableTimestamp is greater than completeUntilTimestamp, break - if (block.timestamp < _pendingFreeMagnitude[operator][strategy][index].completableTimestamp) { - ++numPendingDeallocations; - } else { - break; - } - } + // // Read current magnitude's respective array index and length. + // (,uint256 latestAllocPos, uint256 allocLength) = + // magnitudeUpdates.upperLookupRecentWithPos(uint32(block.timestamp)); + + // if (latestAllocPos < allocLength - 1) { + // numPendingAllocations = allocLength - 1 - latestAllocPos; + // } + // // Else, latestAllocPos >= allocLength - 1; + // // latestAllocPos cannot be greater than length - 1, thus when pos == length - 1 there are no pending allocations + + // // Get pending deallocations + // uint256 deallocationsLength = _queuedDeallocationIndices[operator][strategy][operatorSetKey].length; + // for (uint256 i = deallocationsLength; i > 0; --i) { + // // index of pendingFreeMagnitude/deallocation to check for slashing + // uint256 index = _queuedDeallocationIndices[operator][strategy][operatorSetKey][i - 1]; + + // // If completableTimestamp is greater than completeUntilTimestamp, break + // if (block.timestamp < _pendingFreeMagnitude[operator][strategy][index].completableTimestamp) { + // ++numPendingDeallocations; + // } else { + // break; + // } + // } } /// @dev gets the latest total magnitude or overwrites it if it is not set diff --git a/src/contracts/core/AllocationManagerStorage.sol b/src/contracts/core/AllocationManagerStorage.sol index 32734fb31..89112addd 100644 --- a/src/contracts/core/AllocationManagerStorage.sol +++ b/src/contracts/core/AllocationManagerStorage.sol @@ -48,14 +48,14 @@ abstract contract AllocationManagerStorage is IAllocationManager { /// @notice Mapping: operator => strategy => operatorSet (encoded) => snapshotted magnitude mapping(address => mapping(IStrategy => mapping(bytes32 => Snapshots.History))) internal _magnitudeUpdate; - /// @notice Mapping: operator => strategy => operatorSet (encoded) => Pending Magnitude Update - mapping(address => mapping(IStrategy => mapping(bytes32 => Snapshots.History))) internal _pendingMagnitudeUpdate; + // /// @notice Mapping: operator => strategy => operatorSet (encoded) => Pending Magnitude Update + // mapping(address => mapping(IStrategy => mapping(bytes32 => Snapshots.History))) internal _pendingMagnitudeUpdate; /// @notice Mapping: operator => strategy => OperatorMagnitudeInfo to keep track of info regarding pending magnitude allocations. mapping(address => mapping(IStrategy => OperatorMagnitudeInfo)) public operatorMagnitudeInfo; - // /// @notice Mapping: operator => strategy => PendingFreeMagnitude[] to keep track of pending free magnitude from deallocations - // mapping(address => mapping(IStrategy => PendingFreeMagnitude[])) internal _pendingFreeMagnitude; + /// @notice Mapping: operator => strategy => PendingFreeMagnitude[] to keep track of pending free magnitude from deallocations + mapping(address => mapping(IStrategy => PendingFreeMagnitude[])) internal _pendingFreeMagnitude; // /// @notice Mapping: operator => strategy => operatorSet (encoded) => list of queuedDeallocation indices // mapping(address => mapping(IStrategy => mapping(bytes32 => uint256[]))) internal _queuedDeallocationIndices;