@@ -18,9 +18,9 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
1818 // ************************************* //
1919
2020 struct SubCourtStakes {
21- uint256 totalStakedInCourts ;
22- uint96 [MAX_STAKE_PATHS] courtIDs ;
23- uint256 [MAX_STAKE_PATHS] stakedInCourts ;
21+ uint256 totalStakedInSubCourts ;
22+ uint96 [MAX_STAKE_PATHS] subCourtIDs ;
23+ uint256 [MAX_STAKE_PATHS] stakedInSubCourts ;
2424 }
2525
2626 struct SortitionSumTree {
@@ -312,6 +312,17 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
312312 _setStake (_account, _courtID, _pnkDeposit, _pnkWithdrawal, _newStake);
313313 }
314314
315+ /// @dev Update the state of the stakes with a PNK reward deposit, called by KC during rewards execution.
316+ /// `O(n + p * log_k(j))` where
317+ /// `n` is the number of courts the juror has staked in,
318+ /// `p` is the depth of the court tree,
319+ /// `k` is the minimum number of children per node of one of these courts' sortition sum tree,
320+ /// and `j` is the maximum number of jurors that ever staked in one of these courts simultaneously.
321+ /// @param _account The address of the juror.
322+ /// @param _courtID The ID of the court.
323+ /// @param _penalty The amount of PNK to be deducted.
324+ /// @return pnkBalance The updated total PNK balance of the juror, including the penalty.
325+ /// @return availablePenalty The amount of PNK that was actually deducted.
315326 function setStakePenalty (
316327 address _account ,
317328 uint96 _courtID ,
@@ -331,7 +342,7 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
331342 newStake = currentStake - availablePenalty;
332343 }
333344 _setStake (_account, _courtID, 0 , availablePenalty, newStake);
334- pnkBalance = juror.stakedPnk; // updated by _setStake()
345+ pnkBalance = juror.stakedPnk; // Updated by _setStake().
335346 }
336347
337348 function _setStake (
@@ -482,21 +493,21 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
482493
483494 // The current court stake is the node value minus all subcourt stakes
484495 uint256 currentCourtStake = 0 ;
485- if (tree.nodes[treeIndex] > subcourtStakes.totalStakedInCourts ) {
486- currentCourtStake = tree.nodes[treeIndex] - subcourtStakes.totalStakedInCourts ;
496+ if (tree.nodes[treeIndex] > subcourtStakes.totalStakedInSubCourts ) {
497+ currentCourtStake = tree.nodes[treeIndex] - subcourtStakes.totalStakedInSubCourts ;
487498 }
488499
489500 // Check if the drawn number falls within current court range
490501 if (currentDrawnNumber >= currentCourtStake) {
491502 // Find which subcourt range contains the drawn number
492503 uint256 accumulatedStake = currentCourtStake;
493504 for (uint256 i = 0 ; i < MAX_STAKE_PATHS; i++ ) {
494- if (subcourtStakes.stakedInCourts [i] > 0 ) {
495- if (currentDrawnNumber < accumulatedStake + subcourtStakes.stakedInCourts [i]) {
496- fromSubcourtID = subcourtStakes.courtIDs [i];
505+ if (subcourtStakes.stakedInSubCourts [i] > 0 ) {
506+ if (currentDrawnNumber < accumulatedStake + subcourtStakes.stakedInSubCourts [i]) {
507+ fromSubcourtID = subcourtStakes.subCourtIDs [i];
497508 break ;
498509 }
499- accumulatedStake += subcourtStakes.stakedInCourts [i];
510+ accumulatedStake += subcourtStakes.stakedInSubCourts [i];
500511 }
501512 }
502513 }
@@ -602,24 +613,24 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
602613 ) internal {
603614 // Update existing stake item if found
604615 for (uint256 i = 0 ; i < MAX_STAKE_PATHS; i++ ) {
605- if (_subcourtStakes.courtIDs [i] == _fromSubCourtID) {
616+ if (_subcourtStakes.subCourtIDs [i] == _fromSubCourtID) {
606617 if (_value == 0 ) {
607- delete _subcourtStakes.courtIDs [i];
608- delete _subcourtStakes.stakedInCourts [i];
618+ delete _subcourtStakes.subCourtIDs [i];
619+ delete _subcourtStakes.stakedInSubCourts [i];
609620 } else {
610- _subcourtStakes.totalStakedInCourts += _value;
611- _subcourtStakes.totalStakedInCourts -= _subcourtStakes.stakedInCourts [i];
612- _subcourtStakes.stakedInCourts [i] = _value;
621+ _subcourtStakes.totalStakedInSubCourts += _value;
622+ _subcourtStakes.totalStakedInSubCourts -= _subcourtStakes.stakedInSubCourts [i];
623+ _subcourtStakes.stakedInSubCourts [i] = _value;
613624 }
614625 return ;
615626 }
616627 }
617628 // Not found so add a new stake item
618629 for (uint256 i = 0 ; i < MAX_STAKE_PATHS; i++ ) {
619- if (_subcourtStakes.courtIDs [i] == 0 ) {
620- _subcourtStakes.courtIDs [i] = _fromSubCourtID;
621- _subcourtStakes.totalStakedInCourts += _value;
622- _subcourtStakes.stakedInCourts [i] = _value;
630+ if (_subcourtStakes.subCourtIDs [i] == 0 ) {
631+ _subcourtStakes.subCourtIDs [i] = _fromSubCourtID;
632+ _subcourtStakes.totalStakedInSubCourts += _value;
633+ _subcourtStakes.stakedInSubCourts [i] = _value;
623634 return ;
624635 }
625636 }
0 commit comments