Skip to content

Commit f7591d2

Browse files
fix(KC): update the state after transfer
1 parent f4ae6a5 commit f7591d2

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

contracts/src/arbitration/KlerosCoreBase.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,11 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
10991099
_newStake,
11001100
false // Unused parameter.
11011101
);
1102-
if (stakingResult != StakingResult.Successful) {
1102+
if (stakingResult != StakingResult.Successful && stakingResult != StakingResult.Delayed) {
11031103
_stakingFailed(_onError, stakingResult);
11041104
return false;
1105+
} else if (stakingResult == StakingResult.Delayed) {
1106+
return true;
11051107
}
11061108
if (pnkDeposit > 0) {
11071109
if (!pinakion.safeTransferFrom(_account, address(this), pnkDeposit)) {
@@ -1115,6 +1117,8 @@ abstract contract KlerosCoreBase is IArbitratorV2, Initializable, UUPSProxiable
11151117
return false;
11161118
}
11171119
}
1120+
sortitionModule.updateState(_account, _courtID, pnkDeposit, pnkWithdrawal, _newStake);
1121+
11181122
return true;
11191123
}
11201124

contracts/src/arbitration/SortitionModuleBase.sol

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,41 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
312312
delayedStake.courtID = _courtID;
313313
delayedStake.stake = _newStake;
314314
emit StakeDelayed(_account, _courtID, _newStake);
315-
return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
315+
return (pnkDeposit, pnkWithdrawal, StakingResult.Delayed);
316316
}
317317

318318
// Current phase is Staking: set normal stakes or delayed stakes.
319319
if (_newStake >= currentStake) {
320320
pnkDeposit = _newStake - currentStake;
321-
if (currentStake == 0) {
322-
juror.courtIDs.push(_courtID);
323-
}
324-
// Increase juror's balance by deposited amount.
325-
juror.stakedPnk += pnkDeposit;
326321
} else {
327322
pnkWithdrawal = currentStake - _newStake;
328323
// Ensure locked tokens remain in the contract. They can only be released during Execution.
329324
uint256 possibleWithdrawal = juror.stakedPnk > juror.lockedPnk ? juror.stakedPnk - juror.lockedPnk : 0;
330325
if (pnkWithdrawal > possibleWithdrawal) {
331326
pnkWithdrawal = possibleWithdrawal;
332327
}
333-
juror.stakedPnk -= pnkWithdrawal;
328+
}
329+
return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
330+
}
331+
332+
/// @dev Called by KC at the end of setStake flow.
333+
function updateState(
334+
address _account,
335+
uint96 _courtID,
336+
uint256 _pnkDeposit,
337+
uint256 _pnkWithdrawal,
338+
uint256 _newStake
339+
) external override onlyByCore {
340+
Juror storage juror = jurors[_account];
341+
if (_pnkDeposit > 0) {
342+
uint256 currentStake = stakeOf(_account, _courtID);
343+
if (currentStake == 0) {
344+
juror.courtIDs.push(_courtID);
345+
}
346+
// Increase juror's balance by deposited amount.
347+
juror.stakedPnk += _pnkDeposit;
348+
} else {
349+
juror.stakedPnk -= _pnkWithdrawal;
334350
if (_newStake == 0) {
335351
// Cleanup
336352
for (uint256 i = juror.courtIDs.length; i > 0; i--) {
@@ -342,6 +358,7 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
342358
}
343359
}
344360
}
361+
345362
// Update the sortition sum tree.
346363
bytes32 stakePathID = _accountAndCourtIDToStakePathID(_account, _courtID);
347364
bool finished = false;
@@ -356,7 +373,6 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
356373
}
357374
}
358375
emit StakeSet(_account, _courtID, _newStake, juror.stakedPnk);
359-
return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
360376
}
361377

362378
function lockStake(address _account, uint256 _relativeAmount) external override onlyByCore {

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@ interface ISortitionModule {
5050
function postDrawHook(uint256 _disputeID, uint256 _roundID) external;
5151

5252
function withdrawLeftoverPNK(address _account) external;
53+
54+
function updateState(
55+
address _account,
56+
uint96 _courtID,
57+
uint256 _pnkDeposit,
58+
uint256 _pnkWithdrawal,
59+
uint256 _newStake
60+
) external;
5361
}

contracts/src/arbitration/university/SortitionModuleUniversity.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ contract SortitionModuleUniversity is ISortitionModuleUniversity, UUPSProxiable,
317317
return jurors[_juror].stakedPnk > 0;
318318
}
319319

320+
function updateState(
321+
address _account,
322+
uint96 _courtID,
323+
uint256 _pnkDeposit,
324+
uint256 _pnkWithdrawal,
325+
uint256 _newStake
326+
) external {}
327+
320328
// ************************************* //
321329
// * Internal * //
322330
// ************************************* //

contracts/src/libraries/Constants.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum OnError {
2727

2828
enum StakingResult {
2929
Successful,
30+
Delayed,
3031
StakingTransferFailed,
3132
UnstakingTransferFailed,
3233
CannotStakeInMoreCourts,

0 commit comments

Comments
 (0)