Skip to content

Commit 7e0d1bd

Browse files
committed
refactor: simplification by moving KC._deleteDelayedStake() inside SM.preStakeHook()
1 parent 4328a5f commit 7e0d1bd

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

contracts/src/arbitration/KlerosCore.sol

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
464464
/// @param _newStake The new stake.
465465
/// Note that the existing delayed stake will be nullified as non-relevant.
466466
function setStake(uint96 _courtID, uint256 _newStake) external {
467-
_deleteDelayedStake(_courtID);
468467
if (!_setStakeForAccount(msg.sender, _courtID, _newStake, false)) revert StakingFailed();
469468
}
470469

@@ -475,10 +474,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
475474
bool _alreadyTransferred
476475
) external {
477476
if (msg.sender != address(sortitionModule)) revert SortitionModuleOnly();
478-
// Always nullify the latest delayed stake before setting a new value.
479-
// Note that we call _deleteDelayedStake() here too because the one in `setStake` can be bypassed
480-
// if the stake was updated automatically during `execute` (e.g. when unstaking inactive juror).
481-
_deleteDelayedStake(_courtID);
482477
_setStakeForAccount(_account, _courtID, _newStake, _alreadyTransferred);
483478
}
484479

@@ -1061,12 +1056,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
10611056
emit DisputeKitEnabled(_courtID, _disputeKitID, _enable);
10621057
}
10631058

1064-
/// @dev Removes the latest delayed stake if there is any.
1065-
/// @param _courtID The ID of the court.
1066-
function _deleteDelayedStake(uint96 _courtID) private {
1067-
sortitionModule.deleteDelayedStake(_courtID, msg.sender);
1068-
}
1069-
10701059
/// @dev Sets the specified juror's stake in a court.
10711060
/// `O(n + p * log_k(j))` where
10721061
/// `n` is the number of courts the juror has staked in,

contracts/src/arbitration/SortitionModule.sol

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,4 @@ interface ISortitionModule {
3232
function createDisputeHook(uint256 _disputeID, uint256 _roundID) external;
3333

3434
function postDrawHook(uint256 _disputeID, uint256 _roundID) external;
35-
36-
function deleteDelayedStake(uint96 _courtID, address _juror) external;
3735
}

0 commit comments

Comments
 (0)