@@ -13,7 +13,7 @@ import {RNG} from "../rng/RNG.sol";
1313import "../libraries/Constants.sol " ;
1414
1515/// @title StakeControllerBase
16- /// @notice Abstract base contract for coordinating between Vault and SortitionModule
16+ /// @notice Abstract base contract for coordinating between Vault and SortitionSumTree
1717/// @dev Manages phases, delayed stakes, and coordination logic
1818abstract contract StakeControllerBase is IStakeController , Initializable , UUPSProxiable {
1919 // ************************************* //
@@ -39,7 +39,7 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
3939 address public governor; // The governor of the contract.
4040 KlerosCoreXBase public core; // The core arbitrator contract.
4141 IVault public vault; // The PNK vault for token management.
42- ISortitionSumTree public sortitionModule ; // The sortition module for drawing logic .
42+ ISortitionSumTree public sortition ; // The sortition sum tree for drawing.
4343
4444 // Phase management
4545 Phase public override phase; // The current phase. Uses Phase from IStakeController.
@@ -82,7 +82,7 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
8282 address _governor ,
8383 KlerosCoreXBase _core ,
8484 IVault _vault ,
85- ISortitionSumTree _sortitionModule ,
85+ ISortitionSumTree _sortition ,
8686 uint256 _minStakingTime ,
8787 uint256 _maxDrawingTime ,
8888 RNG _rng ,
@@ -91,7 +91,7 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
9191 governor = _governor;
9292 core = _core;
9393 vault = _vault;
94- sortitionModule = _sortitionModule ;
94+ sortition = _sortition ;
9595 minStakingTime = _minStakingTime;
9696 maxDrawingTime = _maxDrawingTime;
9797 lastPhaseChange = block .timestamp ;
@@ -116,10 +116,10 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
116116 vault = _vault;
117117 }
118118
119- /// @dev Changes the `sortitionModule ` storage variable.
120- /// @param _sortitionModule The new sortition module address.
121- function changeSortitionModule (ISortitionSumTree _sortitionModule ) external onlyByGovernor {
122- sortitionModule = _sortitionModule ;
119+ /// @dev Changes the `sortition ` storage variable.
120+ /// @param _sortition The new sortition module address.
121+ function changeSortitionSumTree (ISortitionSumTree _sortition ) external onlyByGovernor {
122+ sortition = _sortition ;
123123 }
124124
125125 /// @dev Changes the `minStakingTime` storage variable.
@@ -198,15 +198,6 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
198198 // * Stake Management * //
199199 // ************************************* //
200200
201- /// @inheritdoc IStakeController
202- function setStakeBySystem (
203- address _account ,
204- uint96 _courtID ,
205- uint256 _newStake
206- ) external override onlyByCore returns (uint256 pnkDeposit , uint256 pnkWithdrawal , StakingResult stakingResult ) {
207- return _setStakeBySystem (_account, _courtID, _newStake);
208- }
209-
210201 /// @inheritdoc IStakeController
211202 function setStake (
212203 address _account ,
@@ -243,7 +234,7 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
243234 uint96 [] storage courtIDsForJuror = jurorStakes[_account].stakedCourtIDs;
244235 while (courtIDsForJuror.length > 0 ) {
245236 uint96 courtID = courtIDsForJuror[0 ];
246- _setStakeBySystem (_account, courtID, 0 );
237+ _setStake (_account, courtID, 0 );
247238 }
248239 jurorStakes[_account].totalStake = 0 ;
249240 pnkToWithdraw = vault.getAvailableBalance (_account);
@@ -256,14 +247,14 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
256247
257248 /// @inheritdoc IStakeController
258249 function createTree (bytes32 _key , bytes memory _extraData ) external override onlyByCore {
259- sortitionModule .createTree (_key, _extraData);
250+ sortition .createTree (_key, _extraData);
260251 }
261252
262253 /// @inheritdoc IStakeController
263254 function draw (bytes32 _court , uint256 _coreDisputeID , uint256 _nonce ) external view override returns (address ) {
264255 if (phase != Phase.drawing) revert NotDrawingPhase ();
265256 if (randomNumber == 0 ) revert RandomNumberNotReadyYet ();
266- return sortitionModule .draw (_court, _coreDisputeID, _nonce, randomNumber);
257+ return sortition .draw (_court, _coreDisputeID, _nonce, randomNumber);
267258 }
268259
269260 /// @inheritdoc IStakeController
@@ -329,71 +320,54 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
329320 // * Internal * //
330321 // ************************************* //
331322
332- /// @dev Internal implementation of setStakeBySystem
323+ /// @dev Internal implementation of setStake with phase-aware delayed stake logic
333324 /// @param _account The account to set the stake for.
334325 /// @param _courtID The ID of the court to set the stake for.
335326 /// @param _newStake The new stake.
336327 /// @return pnkDeposit The amount of PNK to deposit.
337328 /// @return pnkWithdrawal The amount of PNK to withdraw.
338329 /// @return stakingResult The result of the staking operation.
339- function _setStakeBySystem (
330+ function _setStake (
340331 address _account ,
341332 uint96 _courtID ,
342333 uint256 _newStake
343334 ) internal virtual returns (uint256 pnkDeposit , uint256 pnkWithdrawal , StakingResult stakingResult ) {
344335 JurorStake storage currentJurorStake = jurorStakes[_account];
345336 uint256 currentStakeInCourt = currentJurorStake.stakes[_courtID];
346337
347- if (currentStakeInCourt == 0 ) {
348- if (_newStake == 0 )
349- // No change needed
350- return (0 , 0 , StakingResult.CannotStakeZeroWhenNoStake);
351- else if (_newStake > 0 && currentJurorStake.stakedCourtIDs.length >= MAX_STAKE_PATHS)
352- // Cannot stake in more courts
353- return (0 , 0 , StakingResult.CannotStakeInMoreCourts);
354- }
355-
356- currentJurorStake.stakes[_courtID] = _newStake;
338+ if (phase == Phase.staking) {
339+ if (currentStakeInCourt == 0 ) {
340+ if (_newStake == 0 )
341+ // No change needed
342+ return (0 , 0 , StakingResult.CannotStakeZeroWhenNoStake);
343+ else if (_newStake > 0 && currentJurorStake.stakedCourtIDs.length >= MAX_STAKE_PATHS)
344+ // Cannot stake in more courts
345+ return (0 , 0 , StakingResult.CannotStakeInMoreCourts);
346+ }
357347
358- if (_newStake > currentStakeInCourt) {
359- pnkDeposit = _newStake - currentStakeInCourt;
360- currentJurorStake.totalStake += pnkDeposit;
361- } else if (_newStake < currentStakeInCourt) {
362- pnkWithdrawal = currentStakeInCourt - _newStake;
363- currentJurorStake.totalStake -= pnkWithdrawal;
364- }
348+ currentJurorStake.stakes[_courtID] = _newStake;
365349
366- // Manage stakedCourtIDs
367- if (currentStakeInCourt == 0 && _newStake > 0 ) {
368- currentJurorStake.stakedCourtIDs.push (_courtID);
369- } else if (currentStakeInCourt > 0 && _newStake == 0 ) {
370- _removeCourt (currentJurorStake.stakedCourtIDs, _courtID);
371- }
350+ if (_newStake > currentStakeInCourt) {
351+ pnkDeposit = _newStake - currentStakeInCourt;
352+ currentJurorStake.totalStake += pnkDeposit;
353+ } else if (_newStake < currentStakeInCourt) {
354+ pnkWithdrawal = currentStakeInCourt - _newStake;
355+ currentJurorStake.totalStake -= pnkWithdrawal;
356+ }
372357
373- sortitionModule.setStake (_account, _courtID, _newStake);
358+ // Manage stakedCourtIDs
359+ if (currentStakeInCourt == 0 && _newStake > 0 ) {
360+ currentJurorStake.stakedCourtIDs.push (_courtID);
361+ } else if (currentStakeInCourt > 0 && _newStake == 0 ) {
362+ _removeCourt (currentJurorStake.stakedCourtIDs, _courtID);
363+ }
374364
375- emit StakeSet (_account, _courtID, _newStake, currentJurorStake.totalStake);
376- }
365+ sortition.setStake (_account, _courtID, _newStake);
377366
378- /// @dev Internal implementation of setStake with phase-aware delayed stake logic
379- /// @param _account The account to set the stake for.
380- /// @param _courtID The ID of the court to set the stake for.
381- /// @param _newStake The new stake.
382- /// @return pnkDeposit The amount of PNK to deposit.
383- /// @return pnkWithdrawal The amount of PNK to withdraw.
384- /// @return stakingResult The result of the staking operation.
385- function _setStake (
386- address _account ,
387- uint96 _courtID ,
388- uint256 _newStake
389- ) internal virtual returns (uint256 pnkDeposit , uint256 pnkWithdrawal , StakingResult stakingResult ) {
390- if (phase == Phase.staking) {
391- return _setStakeBySystem (_account, _courtID, _newStake);
367+ emit StakeSet (_account, _courtID, _newStake, currentJurorStake.totalStake);
368+ return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
392369 }
393370
394- JurorStake storage currentJurorStake = jurorStakes[_account];
395- uint256 currentStakeInCourt = currentJurorStake.stakes[_courtID];
396-
397371 if (_newStake > currentStakeInCourt) {
398372 pnkDeposit = _newStake - currentStakeInCourt;
399373 } else if (_newStake < currentStakeInCourt) {
@@ -470,9 +444,9 @@ abstract contract StakeControllerBase is IStakeController, Initializable, UUPSPr
470444 }
471445
472446 // _setStakeBySystem will update local juror stake mappings (jurorStakes)
473- // AND call sortitionModule .setStake.
447+ // AND call sortition .setStake.
474448 // The pnkDeposit/pnkWithdrawal are calculated but not used by this import function.
475- (, , StakingResult stakingResult ) = _setStakeBySystem (account, courtID, stakeToImport);
449+ (, , StakingResult stakingResult ) = _setStake (account, courtID, stakeToImport);
476450
477451 if (stakingResult == StakingResult.Successful) {
478452 totalImportedSuccess++ ;
0 commit comments