@@ -218,30 +218,13 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
218218 delayedStakeReadIndex = newDelayedStakeReadIndex;
219219 }
220220
221- /// @dev Checks if there is already a delayed stake. In this case consider it irrelevant and remove it.
222- /// @param _courtID ID of the court.
223- /// @param _juror Juror whose stake to check.
224- function deleteDelayedStake (uint96 _courtID , address _juror ) external override onlyByCore {
225- uint256 latestIndex = latestDelayedStakeIndex[_juror][_courtID];
226- if (latestIndex != 0 ) {
227- DelayedStake storage delayedStake = delayedStakes[latestIndex];
228- if (delayedStake.alreadyTransferred) {
229- bytes32 stakePathID = _accountAndCourtIDToStakePathID (_juror, _courtID);
230- // Sortition stake represents the stake value that was last updated during Staking phase.
231- uint256 sortitionStake = stakeOf (bytes32 (uint256 (_courtID)), stakePathID);
232- // Withdraw the tokens that were added with the latest delayed stake.
233- core.withdrawPartiallyDelayedStake (_courtID, _juror, delayedStake.stake - sortitionStake);
234- }
235- delete delayedStakes[latestIndex];
236- delete latestDelayedStakeIndex[_juror][_courtID];
237- }
238- }
239-
240221 function preStakeHook (
241222 address _account ,
242223 uint96 _courtID ,
243224 uint256 _stake
244225 ) external override onlyByCore returns (PreStakeHookResult) {
226+ _deleteDelayedStake (_courtID, _account);
227+
245228 (, , uint256 currentStake , uint256 nbCourts ) = core.getJurorBalance (_account, _courtID);
246229 if (currentStake == 0 && nbCourts >= MAX_STAKE_PATHS) {
247230 // Prevent staking beyond MAX_STAKE_PATHS but unstaking is always allowed.
@@ -268,6 +251,25 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
268251 return PreStakeHookResult.ok;
269252 }
270253
254+ /// @dev Checks if there is already a delayed stake. In this case consider it irrelevant and remove it.
255+ /// @param _courtID ID of the court.
256+ /// @param _juror Juror whose stake to check.
257+ function _deleteDelayedStake (uint96 _courtID , address _juror ) internal {
258+ uint256 latestIndex = latestDelayedStakeIndex[_juror][_courtID];
259+ if (latestIndex != 0 ) {
260+ DelayedStake storage delayedStake = delayedStakes[latestIndex];
261+ if (delayedStake.alreadyTransferred) {
262+ bytes32 stakePathID = _accountAndCourtIDToStakePathID (_juror, _courtID);
263+ // Sortition stake represents the stake value that was last updated during Staking phase.
264+ uint256 sortitionStake = stakeOf (bytes32 (uint256 (_courtID)), stakePathID);
265+ // Withdraw the tokens that were added with the latest delayed stake.
266+ core.withdrawPartiallyDelayedStake (_courtID, _juror, delayedStake.stake - sortitionStake);
267+ }
268+ delete delayedStakes[latestIndex];
269+ delete latestDelayedStakeIndex[_juror][_courtID];
270+ }
271+ }
272+
271273 function createDisputeHook (uint256 /*_disputeID*/ , uint256 /*_roundID*/ ) external override onlyByCore {
272274 disputesWithoutJurors++ ;
273275 }
0 commit comments