Skip to content

Commit 9d87373

Browse files
committed
refactor: merged StakeController._setStake() and _setStakeBySystem(), removed SortitionModule refs
1 parent 57d5261 commit 9d87373

File tree

5 files changed

+52
-93
lines changed

5 files changed

+52
-93
lines changed

contracts/src/arbitration/KlerosCoreXBase.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ abstract contract KlerosCoreXBase is IArbitratorV2, Initializable, UUPSProxiable
11311131
uint96 _courtID,
11321132
uint256 _newStake
11331133
) internal virtual returns (bool success) {
1134-
(uint256 pnkDeposit, uint256 pnkWithdrawal, StakingResult stakingResult) = stakeController.setStakeBySystem(
1134+
(uint256 pnkDeposit, uint256 pnkWithdrawal, StakingResult stakingResult) = stakeController.setStake(
11351135
_account,
11361136
_courtID,
11371137
_newStake
@@ -1213,7 +1213,6 @@ abstract contract KlerosCoreXBase is IArbitratorV2, Initializable, UUPSProxiable
12131213
error GovernorOnly();
12141214
error GuardianOrGovernorOnly();
12151215
error DisputeKitOnly();
1216-
error SortitionModuleOnly();
12171216
error StakeControllerOnly();
12181217
error UnsuccessfulCall();
12191218
error InvalidDisputKitParent();

contracts/src/arbitration/StakeController.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {RNG} from "../rng/RNG.sol";
1010

1111
/// @title StakeController
1212
/// @notice Basic implementation of the Stake Controller
13-
/// @dev Coordinates between Vault and SortitionModule for the new architecture
13+
/// @dev Coordinates between Vault and SortitionSumTree for the new architecture
1414
contract StakeController is StakeControllerBase {
1515
string public constant override version = "1.0.0";
1616

@@ -27,7 +27,7 @@ contract StakeController is StakeControllerBase {
2727
/// @param _governor The governor's address.
2828
/// @param _core The KlerosCore contract.
2929
/// @param _vault The Vault contract.
30-
/// @param _sortitionModule The SortitionModule contract.
30+
/// @param _sortition The SortitionSumTree contract.
3131
/// @param _minStakingTime The minimum staking time.
3232
/// @param _maxDrawingTime The maximum drawing time.
3333
/// @param _rng The random number generator.
@@ -36,7 +36,7 @@ contract StakeController is StakeControllerBase {
3636
address _governor,
3737
KlerosCoreXBase _core,
3838
IVault _vault,
39-
ISortitionSumTree _sortitionModule,
39+
ISortitionSumTree _sortition,
4040
uint256 _minStakingTime,
4141
uint256 _maxDrawingTime,
4242
RNG _rng,
@@ -46,7 +46,7 @@ contract StakeController is StakeControllerBase {
4646
_governor,
4747
_core,
4848
_vault,
49-
_sortitionModule,
49+
_sortition,
5050
_minStakingTime,
5151
_maxDrawingTime,
5252
_rng,

contracts/src/arbitration/StakeControllerBase.sol

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {RNG} from "../rng/RNG.sol";
1313
import "../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
1818
abstract 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++;

contracts/src/arbitration/dispute-kits/DisputeKitClassicBase.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ abstract contract DisputeKitClassicBase is IDisputeKit, Initializable, UUPSProxi
207207
emit DisputeCreation(_coreDisputeID, _numberOfChoices, _extraData);
208208
}
209209

210-
/// @dev Draws the juror from the sortition tree. The drawn address is picked up by Kleros Core.
210+
/// @dev Draws the juror from the Stake Controller.
211211
/// Note: Access restricted to Kleros Core only.
212212
/// @param _coreDisputeID The ID of the dispute in Kleros Core.
213213
/// @param _nonce Nonce of the drawing iteration.
@@ -221,11 +221,11 @@ abstract contract DisputeKitClassicBase is IDisputeKit, Initializable, UUPSProxi
221221
uint256 localRoundID = dispute.rounds.length - 1;
222222
Round storage round = dispute.rounds[localRoundID];
223223

224-
IStakeController sortitionModule = core.stakeController();
224+
IStakeController stakeController = core.stakeController();
225225
(uint96 courtID, , , , ) = core.disputes(_coreDisputeID);
226226
bytes32 key = bytes32(uint256(courtID)); // Get the ID of the tree.
227227

228-
drawnAddress = sortitionModule.draw(key, _coreDisputeID, _nonce);
228+
drawnAddress = stakeController.draw(key, _coreDisputeID, _nonce);
229229

230230
if (_postDrawCheck(round, _coreDisputeID, drawnAddress)) {
231231
round.votes.push(Vote({account: drawnAddress, commit: bytes32(0), choice: 0, voted: false}));

contracts/src/arbitration/interfaces/IStakeController.sol

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {KlerosCoreXBase} from "../KlerosCoreXBase.sol";
66
import "../../libraries/Constants.sol";
77

88
/// @title IStakeController
9-
/// @notice Interface for the Stake Controller that coordinates between Vault and SortitionModule
9+
/// @notice Interface for the Stake Controller that coordinates between Vault and SortitionSumTree
1010
/// @dev Combines phase management, delayed stakes, and coordination between vault and sortition
1111
interface IStakeController {
1212
// ************************************* //
@@ -62,20 +62,6 @@ interface IStakeController {
6262
// * Stake Management * //
6363
// ************************************* //
6464

65-
/// @notice System-level update to a juror's stake directly in the SortitionModule.
66-
/// @dev Called by KlerosCoreXBase for executing delayed stakes. Skips regular phase checks and delayed stake creation.
67-
/// @param _account The juror's account
68-
/// @param _courtID The ID of the court
69-
/// @param _newStake The new stake amount
70-
/// @return pnkDeposit The amount of PNK to deposit
71-
/// @return pnkWithdrawal The amount of PNK to withdraw
72-
/// @return stakingResult The result of the staking operation
73-
function setStakeBySystem(
74-
address _account,
75-
uint96 _courtID,
76-
uint256 _newStake
77-
) external returns (uint256 pnkDeposit, uint256 pnkWithdrawal, StakingResult stakingResult);
78-
7965
/// @notice Set stake for a juror with vault coordination
8066
/// @param _account The juror's account
8167
/// @param _courtID The ID of the court
@@ -128,12 +114,12 @@ interface IStakeController {
128114
// * Sortition Delegation * //
129115
// ************************************* //
130116

131-
/// @notice Create a sortition tree (delegated to SortitionModule)
117+
/// @notice Create a sortition tree (delegated to SortitionSumTree)
132118
/// @param _key The key of the tree
133119
/// @param _extraData Extra data for tree configuration
134120
function createTree(bytes32 _key, bytes memory _extraData) external;
135121

136-
/// @notice Draw a juror for dispute (delegated to SortitionModule)
122+
/// @notice Draw a juror for dispute (delegated to SortitionSumTree)
137123
/// @param _court The court identifier
138124
/// @param _coreDisputeID The core dispute ID
139125
/// @param _nonce The drawing nonce

0 commit comments

Comments
 (0)