Skip to content

Commit d876e24

Browse files
fix(SortitionModule): fix staking logic and remove instant staking
1 parent 1ca9066 commit d876e24

File tree

7 files changed

+259
-286
lines changed

7 files changed

+259
-286
lines changed

contracts/src/arbitration/KlerosCoreBase.sol

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,23 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
470470
/// @param _account The account whose stake is being set.
471471
/// @param _courtID The ID of the court.
472472
/// @param _newStake The new stake.
473-
/// @param _alreadyTransferred Whether the PNKs have already been transferred to the contract.
474473
function setStakeBySortitionModule(
475474
address _account,
476475
uint96 _courtID,
477476
uint256 _newStake,
478-
bool _alreadyTransferred
477+
bool /*_alreadyTransferred*/
479478
) external {
480479
if (msg.sender != address(sortitionModule)) revert SortitionModuleOnly();
481-
_setStake(_account, _courtID, _newStake, _alreadyTransferred, OnError.Return);
480+
_setStake(_account, _courtID, _newStake, false, OnError.Return); // alreadyTransferred is unused and DEPRECATED.
481+
}
482+
483+
/// @dev Transfers PNK to the juror by SortitionModule.
484+
/// @param _account The account of the juror whose PNK to transfer.
485+
/// @param _amount The amount to transfer.
486+
function transferBySortitionModule(address _account, uint256 _amount) external {
487+
if (msg.sender != address(sortitionModule)) revert SortitionModuleOnly();
488+
// Note eligibility is checked in SortitionModule.
489+
pinakion.safeTransfer(_account, _amount);
482490
}
483491

484492
/// @inheritdoc IArbitratorV2
@@ -772,26 +780,25 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
772780

773781
// Fully coherent jurors won't be penalized.
774782
uint256 penalty = (round.pnkAtStakePerJuror * (ALPHA_DIVISOR - degreeOfCoherence)) / ALPHA_DIVISOR;
775-
_params.pnkPenaltiesInRound += penalty;
776783

777784
// Unlock the PNKs affected by the penalty
778785
address account = round.drawnJurors[_params.repartition];
779786
sortitionModule.unlockStake(account, penalty);
780787

781788
// Apply the penalty to the staked PNKs.
782-
sortitionModule.penalizeStake(account, penalty);
789+
(uint256 pnkBalance, uint256 availablePenalty) = sortitionModule.penalizeStake(account, penalty);
790+
_params.pnkPenaltiesInRound += availablePenalty;
783791
emit TokenAndETHShift(
784792
account,
785793
_params.disputeID,
786794
_params.round,
787795
degreeOfCoherence,
788-
-int256(penalty),
796+
-int256(availablePenalty),
789797
0,
790798
round.feeToken
791799
);
792-
793-
if (!disputeKit.isVoteActive(_params.disputeID, _params.round, _params.repartition)) {
794-
// The juror is inactive, unstake them.
800+
// Unstake the juror from all courts if he was inactive or his balance can't cover penalties anymore.
801+
if (pnkBalance == 0 || !disputeKit.isVoteActive(_params.disputeID, _params.round, _params.repartition)) {
795802
sortitionModule.setJurorInactive(account);
796803
}
797804
if (_params.repartition == _params.numberOfVotesInRound - 1 && _params.coherentCount == 0) {
@@ -842,11 +849,6 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
842849
// Release the rest of the PNKs of the juror for this round.
843850
sortitionModule.unlockStake(account, pnkLocked);
844851

845-
// Give back the locked PNKs in case the juror fully unstaked earlier.
846-
if (!sortitionModule.isJurorStaked(account)) {
847-
pinakion.safeTransfer(account, pnkLocked);
848-
}
849-
850852
// Transfer the rewards
851853
uint256 pnkReward = ((_params.pnkPenaltiesInRound / _params.coherentCount) * degreeOfCoherence) / ALPHA_DIVISOR;
852854
round.sumPnkRewardPaid += pnkReward;
@@ -1072,14 +1074,13 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
10721074
/// @param _account The account to set the stake for.
10731075
/// @param _courtID The ID of the court to set the stake for.
10741076
/// @param _newStake The new stake.
1075-
/// @param _alreadyTransferred Whether the PNKs were already transferred to/from the staking contract.
10761077
/// @param _onError Whether to revert or return false on error.
10771078
/// @return Whether the stake was successfully set or not.
10781079
function _setStake(
10791080
address _account,
10801081
uint96 _courtID,
10811082
uint256 _newStake,
1082-
bool _alreadyTransferred,
1083+
bool /*_alreadyTransferred*/,
10831084
OnError _onError
10841085
) internal returns (bool) {
10851086
if (_courtID == FORKING_COURT || _courtID >= courts.length) {
@@ -1094,7 +1095,7 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
10941095
_account,
10951096
_courtID,
10961097
_newStake,
1097-
_alreadyTransferred
1098+
false // Unused parameter.
10981099
);
10991100
if (stakingResult != StakingResult.Successful) {
11001101
_stakingFailed(_onError, stakingResult);

0 commit comments

Comments
 (0)