Skip to content

Commit 7de0aca

Browse files
committed
1 parent d021790 commit 7de0aca

File tree

1 file changed

+102
-100
lines changed

1 file changed

+102
-100
lines changed

contracts/src/arbitration/SortitionModule.sol

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// SPDX-License-Identifier: MIT
22

33
/**
4-
* @authors: [@epiqueras, @unknownunknown1, @shotaronowhere]
5-
* @reviewers: []
6-
* @auditors: []
7-
* @bounties: []
8-
* @deployments: []
4+
* @custom:authors: [@epiqueras, @unknownunknown1, @shotaronowhere]
5+
* @custom:reviewers: []
6+
* @custom:auditors: []
7+
* @custom:bounties: []
8+
* @custom:deployments: []
99
*/
1010

1111
pragma solidity ^0.8;
@@ -15,12 +15,13 @@ import "./ISortitionModule.sol";
1515
import "../arbitration/IDisputeKit.sol";
1616
import "../rng/RNG.sol";
1717

18-
/**
19-
* @title SortitionModule
20-
* @dev A factory of trees that keeps track of staked values for sortition.
21-
*/
18+
/// @title SortitionModule
19+
/// @dev A factory of trees that keeps track of staked values for sortition.
2220
contract SortitionModule is ISortitionModule {
23-
/* Structs */
21+
// ************************************* //
22+
// * Enums / Structs * //
23+
// ************************************* //
24+
2425
struct SortitionSumTree {
2526
uint256 K; // The maximum number of children per node.
2627
// We use this to keep track of vacant positions in the tree after removing a leaf. This is for keeping the tree as balanced as possible without spending gas on moving nodes around.
@@ -38,6 +39,10 @@ contract SortitionModule is ISortitionModule {
3839
uint256 penalty; // Penalty value, in case the stake was set during execution.
3940
}
4041

42+
// ************************************* //
43+
// * Storage * //
44+
// ************************************* //
45+
4146
uint256 public constant MAX_STAKE_PATHS = 4; // The maximum number of stake paths a juror can have.
4247
uint256 public constant DEFAULT_K = 6; // Default number of children per node.
4348

@@ -57,6 +62,10 @@ contract SortitionModule is ISortitionModule {
5762
mapping(bytes32 => SortitionSumTree) sortitionSumTrees; // The mapping trees by keys.
5863
mapping(uint256 => DelayedStake) public delayedStakes; // Stores the stakes that were changed during Drawing phase, to update them when the phase is switched to Staking.
5964

65+
// ************************************* //
66+
// * Function Modifiers * //
67+
// ************************************* //
68+
6069
modifier onlyByGovernor() {
6170
require(address(governor) == msg.sender, "Access not allowed: Governor only.");
6271
_;
@@ -67,13 +76,16 @@ contract SortitionModule is ISortitionModule {
6776
_;
6877
}
6978

70-
/** @dev Constructor.
71-
* @param _core The KlerosCore.
72-
* @param _minStakingTime Minimal time to stake
73-
* @param _maxDrawingTime Time after which the drawing phase can be switched
74-
* @param _rng The random number generator.
75-
* @param _rngLookahead Lookahead value for rng.
76-
*/
79+
// ************************************* //
80+
// * Constructor * //
81+
// ************************************* //
82+
83+
/// @dev Constructor.
84+
/// @param _core The KlerosCore.
85+
/// @param _minStakingTime Minimal time to stake
86+
/// @param _maxDrawingTime Time after which the drawing phase can be switched
87+
/// @param _rng The random number generator.
88+
/// @param _rngLookahead Lookahead value for rng.
7789
constructor(
7890
address _governor,
7991
KlerosCore _core,
@@ -91,26 +103,25 @@ contract SortitionModule is ISortitionModule {
91103
rngLookahead = _rngLookahead;
92104
}
93105

94-
/* Public */
106+
// ************************************* //
107+
// * Governance * //
108+
// ************************************* //
95109

96-
/** @dev Changes the `minStakingTime` storage variable.
97-
* @param _minStakingTime The new value for the `minStakingTime` storage variable.
98-
*/
110+
/// @dev Changes the `minStakingTime` storage variable.
111+
/// @param _minStakingTime The new value for the `minStakingTime` storage variable.
99112
function changeMinStakingTime(uint256 _minStakingTime) external onlyByGovernor {
100113
minStakingTime = _minStakingTime;
101114
}
102115

103-
/** @dev Changes the `maxDrawingTime` storage variable.
104-
* @param _maxDrawingTime The new value for the `maxDrawingTime` storage variable.
105-
*/
116+
/// @dev Changes the `maxDrawingTime` storage variable.
117+
/// @param _maxDrawingTime The new value for the `maxDrawingTime` storage variable.
106118
function changeMaxDrawingTime(uint256 _maxDrawingTime) external onlyByGovernor {
107119
maxDrawingTime = _maxDrawingTime;
108120
}
109121

110-
/** @dev Changes the `_rng` and `_rngLookahead` storage variables.
111-
* @param _rng The new value for the `RNGenerator` storage variable.
112-
* @param _rngLookahead The new value for the `rngLookahead` storage variable.
113-
*/
122+
/// @dev Changes the `_rng` and `_rngLookahead` storage variables.
123+
/// @param _rng The new value for the `RNGenerator` storage variable.
124+
/// @param _rngLookahead The new value for the `rngLookahead` storage variable.
114125
function changeRandomNumberGenerator(RNG _rng, uint256 _rngLookahead) external onlyByGovernor {
115126
rng = _rng;
116127
rngLookahead = _rngLookahead;
@@ -120,6 +131,10 @@ contract SortitionModule is ISortitionModule {
120131
}
121132
}
122133

134+
// ************************************* //
135+
// * State Modifiers * //
136+
// ************************************* //
137+
123138
function passPhase() external {
124139
if (phase == Phase.staking) {
125140
require(
@@ -146,11 +161,9 @@ contract SortitionModule is ISortitionModule {
146161
emit NewPhase(phase);
147162
}
148163

149-
/**
150-
* @dev Create a sortition sum tree at the specified key.
151-
* @param _key The key of the new tree.
152-
* @param _extraData Extra data that contains the number of children each node in the tree should have.
153-
*/
164+
/// @dev Create a sortition sum tree at the specified key.
165+
/// @param _key The key of the new tree.
166+
/// @param _extraData Extra data that contains the number of children each node in the tree should have.
154167
function createTree(bytes32 _key, bytes memory _extraData) external override onlyByCore {
155168
SortitionSumTree storage tree = sortitionSumTrees[_key];
156169
uint256 K = extraDataToTreeK(_extraData);
@@ -160,9 +173,8 @@ contract SortitionModule is ISortitionModule {
160173
tree.nodes.push(0);
161174
}
162175

163-
/** @dev Executes the next delayed stakes.
164-
* @param _iterations The number of delayed stakes to execute.
165-
*/
176+
/// @dev Executes the next delayed stakes.
177+
/// @param _iterations The number of delayed stakes to execute.
166178
function executeDelayedStakes(uint256 _iterations) external {
167179
require(phase == Phase.staking, "Should be in Staking phase.");
168180

@@ -216,21 +228,17 @@ contract SortitionModule is ISortitionModule {
216228
disputesWithoutJurors--;
217229
}
218230

219-
/**
220-
* @dev Saves the random number to use it in sortition. Not used by this contract because the storing of the number is inlined in passPhase().
221-
* @param _randomNumber Random number returned by RNG contract.
222-
*/
231+
/// @dev Saves the random number to use it in sortition. Not used by this contract because the storing of the number is inlined in passPhase().
232+
/// @param _randomNumber Random number returned by RNG contract.
223233
function notifyRandomNumber(uint256 _randomNumber) public override {}
224234

225-
/**
226-
* @dev Sets the value for a particular court and its parent courts.
227-
* @param _courtID ID of the court.
228-
* @param _value The new value.
229-
* @param _account Address of the juror.
230-
* `O(log_k(n))` where
231-
* `k` is the maximum number of children per node in the tree,
232-
* and `n` is the maximum number of nodes ever appended.
233-
*/
235+
/// @dev Sets the value for a particular court and its parent courts.
236+
/// @param _courtID ID of the court.
237+
/// @param _value The new value.
238+
/// @param _account Address of the juror.
239+
/// `O(log_k(n))` where
240+
/// `k` is the maximum number of children per node in the tree,
241+
/// and `n` is the maximum number of nodes ever appended.
234242
function setStake(address _account, uint96 _courtID, uint256 _value) external override onlyByCore {
235243
bytes32 stakePathID = accountAndCourtIDToStakePathID(_account, _courtID);
236244
bool finished = false;
@@ -246,34 +254,32 @@ contract SortitionModule is ISortitionModule {
246254
}
247255
}
248256

249-
/**
250-
* @dev Unstakes the inactive juror from all courts.
251-
* `O(n * (p * log_k(j)) )` where
252-
* `n` is the number of courts the juror has staked in,
253-
* `p` is the depth of the court tree,
254-
* `k` is the minimum number of children per node of one of these courts' sortition sum tree,
255-
* and `j` is the maximum number of jurors that ever staked in one of these courts simultaneously.
256-
* @param _account The juror to unstake.
257-
*/
257+
/// @dev Unstakes the inactive juror from all courts.
258+
/// `O(n * (p * log_k(j)) )` where
259+
/// `n` is the number of courts the juror has staked in,
260+
/// `p` is the depth of the court tree,
261+
/// `k` is the minimum number of children per node of one of these courts' sortition sum tree,
262+
/// and `j` is the maximum number of jurors that ever staked in one of these courts simultaneously.
263+
/// @param _account The juror to unstake.
258264
function setJurorInactive(address _account) external override onlyByCore {
259265
uint96[] memory courtIDs = core.getJurorCourtIDs(_account);
260266
for (uint256 j = courtIDs.length; j > 0; j--) {
261267
core.setStakeBySortitionModule(_account, courtIDs[j - 1], 0, 0);
262268
}
263269
}
264270

265-
/* Public Views */
266-
267-
/**
268-
* @dev Draw an ID from a tree using a number. Note that this function reverts if the sum of all values in the tree is 0.
269-
* @param _key The key of the tree.
270-
* @param _coreDisputeID Index of the dispute in Kleros Core.
271-
* @param _voteID ID of the voter.
272-
* @return drawnAddress The drawn address.
273-
* `O(k * log_k(n))` where
274-
* `k` is the maximum number of children per node in the tree,
275-
* and `n` is the maximum number of nodes ever appended.
276-
*/
271+
// ************************************* //
272+
// * Public Views * //
273+
// ************************************* //
274+
275+
/// @dev Draw an ID from a tree using a number. Note that this function reverts if the sum of all values in the tree is 0.
276+
/// @param _key The key of the tree.
277+
/// @param _coreDisputeID Index of the dispute in Kleros Core.
278+
/// @param _voteID ID of the voter.
279+
/// @return drawnAddress The drawn address.
280+
/// `O(k * log_k(n))` where
281+
/// `k` is the maximum number of children per node in the tree,
282+
/// and `n` is the maximum number of nodes ever appended.
277283
function draw(
278284
bytes32 _key,
279285
uint256 _coreDisputeID,
@@ -306,18 +312,18 @@ contract SortitionModule is ISortitionModule {
306312
drawnAddress = stakePathIDToAccount(tree.nodeIndexesToIDs[treeIndex]);
307313
}
308314

309-
/* Private */
310-
311-
/**
312-
* @dev Update all the parents of a node.
313-
* @param _key The key of the tree to update.
314-
* @param _treeIndex The index of the node to start from.
315-
* @param _plusOrMinus Whether to add (true) or substract (false).
316-
* @param _value The value to add or substract.
317-
* `O(log_k(n))` where
318-
* `k` is the maximum number of children per node in the tree,
319-
* and `n` is the maximum number of nodes ever appended.
320-
*/
315+
// ************************************* //
316+
// * Internal * //
317+
// ************************************* //
318+
319+
/// @dev Update all the parents of a node.
320+
/// @param _key The key of the tree to update.
321+
/// @param _treeIndex The index of the node to start from.
322+
/// @param _plusOrMinus Whether to add (true) or substract (false).
323+
/// @param _value The value to add or substract.
324+
/// `O(log_k(n))` where
325+
/// `k` is the maximum number of children per node in the tree,
326+
/// and `n` is the maximum number of nodes ever appended.
321327
function updateParents(bytes32 _key, uint256 _treeIndex, bool _plusOrMinus, uint256 _value) private {
322328
SortitionSumTree storage tree = sortitionSumTrees[_key];
323329

@@ -330,10 +336,9 @@ contract SortitionModule is ISortitionModule {
330336
}
331337
}
332338

333-
/** @dev Retrieves a juror's address from the stake path ID.
334-
* @param _stakePathID The stake path ID to unpack.
335-
* @return account The account.
336-
*/
339+
/// @dev Retrieves a juror's address from the stake path ID.
340+
/// @param _stakePathID The stake path ID to unpack.
341+
/// @return account The account.
337342
function stakePathIDToAccount(bytes32 _stakePathID) internal pure returns (address account) {
338343
assembly {
339344
// solium-disable-line security/no-inline-assembly
@@ -360,15 +365,13 @@ contract SortitionModule is ISortitionModule {
360365
}
361366
}
362367

363-
/**
364-
* @dev Set a value in a tree.
365-
* @param _key The key of the tree.
366-
* @param _value The new value.
367-
* @param _ID The ID of the value.
368-
* `O(log_k(n))` where
369-
* `k` is the maximum number of children per node in the tree,
370-
* and `n` is the maximum number of nodes ever appended.
371-
*/
368+
/// @dev Set a value in a tree.
369+
/// @param _key The key of the tree.
370+
/// @param _value The new value.
371+
/// @param _ID The ID of the value.
372+
/// `O(log_k(n))` where
373+
/// `k` is the maximum number of children per node in the tree,
374+
/// and `n` is the maximum number of nodes ever appended.
372375
function _set(bytes32 _key, uint256 _value, bytes32 _ID) internal {
373376
SortitionSumTree storage tree = sortitionSumTrees[_key];
374377
uint256 treeIndex = tree.IDsToNodeIndexes[_ID];
@@ -441,11 +444,10 @@ contract SortitionModule is ISortitionModule {
441444
}
442445
}
443446

444-
/** @dev Packs an account and a court ID into a stake path ID.
445-
* @param _account The address of the juror to pack.
446-
* @param _courtID The court ID to pack.
447-
* @return stakePathID The stake path ID.
448-
*/
447+
/// @dev Packs an account and a court ID into a stake path ID.
448+
/// @param _account The address of the juror to pack.
449+
/// @param _courtID The court ID to pack.
450+
/// @return stakePathID The stake path ID.
449451
function accountAndCourtIDToStakePathID(
450452
address _account,
451453
uint96 _courtID

0 commit comments

Comments
 (0)