Skip to content

Commit 1006d82

Browse files
fix(SortitionModule): total stake update
1 parent 592243f commit 1006d82

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

contracts/src/arbitration/KlerosCore.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ contract KlerosCore is IArbitratorV2, Initializable, UUPSProxiable {
13631363
}
13641364
}
13651365
sortitionModule.setStake(_account, _courtID, pnkDeposit, pnkWithdrawal, _newStake);
1366+
sortitionModule.updateTotalStake(pnkDeposit, pnkWithdrawal);
13661367

13671368
return true;
13681369
}

contracts/src/arbitration/SortitionModule.sol

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,27 @@ contract SortitionModule is ISortitionModule, Initializable, UUPSProxiable {
311311
// Current phase is Staking: set stakes.
312312
if (stakeIncrease) {
313313
pnkDeposit = stakeChange;
314-
totalStaked += stakeChange;
315314
} else {
316315
pnkWithdrawal = stakeChange;
317316
uint256 possibleWithdrawal = juror.stakedPnk > juror.lockedPnk ? juror.stakedPnk - juror.lockedPnk : 0;
318317
if (pnkWithdrawal > possibleWithdrawal) {
319318
// Ensure locked tokens remain in the contract. They can only be released during Execution.
320319
pnkWithdrawal = possibleWithdrawal;
321320
}
322-
totalStaked -= pnkWithdrawal;
323321
}
324322
return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
325323
}
326324

325+
/// @inheritdoc ISortitionModule
326+
function updateTotalStake(uint256 _pnkDeposit, uint256 _pnkWithdrawal) external override onlyByCore {
327+
// Note that we don't update totalStake in setStake() function because it doesn't always change total (e.g. during rewards/penalties).
328+
if (_pnkDeposit > 0) {
329+
totalStaked += _pnkDeposit;
330+
} else {
331+
totalStaked -= _pnkWithdrawal;
332+
}
333+
}
334+
327335
/// @inheritdoc ISortitionModule
328336
function setStake(
329337
address _account,

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ interface ISortitionModule {
5757
bool _noDelay
5858
) external returns (uint256 pnkDeposit, uint256 pnkWithdrawal, StakingResult stakingResult);
5959

60+
/// @notice Updates the total amount staked in all courts.
61+
/// @param _pnkDeposit The amount of PNK that increases total stake.
62+
/// @param _pnkWithdrawal The amount of PNK that decreases total stake.
63+
function updateTotalStake(uint256 _pnkDeposit, uint256 _pnkWithdrawal) external;
64+
6065
/// @notice Update the state of the stakes, called by KC at the end of setStake flow.
6166
///
6267
/// @dev `O(n + p * log_k(j))` where

contracts/src/arbitration/university/SortitionModuleUniversity.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ contract SortitionModuleUniversity is ISortitionModuleUniversity, UUPSProxiable,
130130
// NOP
131131
}
132132

133+
/// @inheritdoc ISortitionModule
134+
function updateTotalStake(uint256 _pnkDeposit, uint256 _pnkWithdrawal) external override onlyByCore {
135+
// NOP
136+
}
137+
133138
/// @inheritdoc ISortitionModule
134139
function createDisputeHook(uint256 /*_disputeID*/, uint256 /*_roundID*/) external override onlyByCore {
135140
disputesWithoutJurors++;

contracts/test/foundry/KlerosCore_Staking.t.sol

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,32 @@ contract KlerosCore_StakingTest is KlerosCore_TestBase {
141141
);
142142
}
143143

144+
function test_setStake_totalStaked() public {
145+
// Increase
146+
vm.prank(staker1);
147+
core.setStake(GENERAL_COURT, 4000);
148+
vm.prank(staker1);
149+
core.setStake(GENERAL_COURT, 5001);
150+
vm.prank(staker2);
151+
core.setStake(GENERAL_COURT, 1000);
152+
vm.prank(staker2);
153+
core.setStake(GENERAL_COURT, 1500);
154+
155+
assertEq(sortitionModule.totalStaked(), 6501, "Wrong totalStaked");
156+
157+
// Decrease
158+
vm.prank(staker1);
159+
core.setStake(GENERAL_COURT, 3000);
160+
vm.prank(staker1);
161+
core.setStake(GENERAL_COURT, 2500);
162+
vm.prank(staker2);
163+
core.setStake(GENERAL_COURT, 1400);
164+
vm.prank(staker2);
165+
core.setStake(GENERAL_COURT, 1200);
166+
167+
assertEq(sortitionModule.totalStaked(), 3700, "Wrong totalStaked");
168+
}
169+
144170
function test_setStake_maxStakePathCheck() public {
145171
uint256[] memory supportedDK = new uint256[](1);
146172
supportedDK[0] = DISPUTE_KIT_CLASSIC;

0 commit comments

Comments
 (0)