Skip to content

Commit 5799a3b

Browse files
fix(SortitionModule): sync totalStaked with stakedPnk
1 parent 719ea3e commit 5799a3b

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed

contracts/src/arbitration/KlerosCore.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,6 @@ contract KlerosCore is IArbitratorV2, Initializable, UUPSProxiable {
13701370
}
13711371
}
13721372
sortitionModule.setStake(_account, _courtID, pnkDeposit, pnkWithdrawal, _newStake);
1373-
sortitionModule.updateTotalStake(pnkDeposit, pnkWithdrawal);
13741373

13751374
return true;
13761375
}

contracts/src/arbitration/SortitionModule.sol

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,6 @@ contract SortitionModule is ISortitionModule, Initializable, UUPSProxiable {
322322
return (pnkDeposit, pnkWithdrawal, StakingResult.Successful);
323323
}
324324

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-
335325
/// @inheritdoc ISortitionModule
336326
function setStake(
337327
address _account,
@@ -399,8 +389,10 @@ contract SortitionModule is ISortitionModule, Initializable, UUPSProxiable {
399389
}
400390
// Increase juror's balance by deposited amount.
401391
juror.stakedPnk += _pnkDeposit;
392+
totalStaked += _pnkDeposit;
402393
} else {
403394
juror.stakedPnk -= _pnkWithdrawal;
395+
totalStaked -= _pnkWithdrawal;
404396
if (_newStake == 0) {
405397
// Cleanup
406398
for (uint256 i = juror.courtIDs.length; i > 0; i--) {
@@ -468,6 +460,7 @@ contract SortitionModule is ISortitionModule, Initializable, UUPSProxiable {
468460
uint256 amount = getJurorLeftoverPNK(_account);
469461
if (amount == 0) revert NotEligibleForWithdrawal();
470462
jurors[_account].stakedPnk = 0;
463+
totalStaked -= amount;
471464
core.transferBySortitionModule(_account, amount);
472465
emit LeftoverPNKWithdrawn(_account, amount);
473466
}

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ 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-
6560
/// @notice Update the state of the stakes, called by KC at the end of setStake flow.
6661
///
6762
/// @dev `O(n + p * log_k(j))` where

contracts/src/arbitration/university/SortitionModuleUniversity.sol

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ 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-
138133
/// @inheritdoc ISortitionModule
139134
function createDisputeHook(uint256 /*_disputeID*/, uint256 /*_roundID*/) external override onlyByCore {
140135
disputesWithoutJurors++;

contracts/test/foundry/KlerosCore_Execution.t.sol

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ contract KlerosCore_ExecutionTest is KlerosCore_TestBase {
9898
"Wrong penalty coherence 2 vote ID"
9999
);
100100

101+
assertEq(pinakion.balanceOf(address(core)), 22000, "Wrong token balance of the core");
102+
assertEq(sortitionModule.totalStaked(), 22000, "Total staked should be equal to the balance in this test");
103+
101104
vm.expectEmit(true, true, true, true);
102105
emit SortitionModule.StakeLocked(staker1, 1000, true);
103106
vm.expectEmit(true, true, true, true);
@@ -143,9 +146,11 @@ contract KlerosCore_ExecutionTest is KlerosCore_TestBase {
143146
assertEq(staker1.balance, 0, "Wrong balance of the staker1");
144147
assertEq(staker2.balance, 0.09 ether, "Wrong balance of the staker2");
145148

146-
assertEq(pinakion.balanceOf(address(core)), 22000, "Wrong token balance of the core"); // Was 21500. 1000 was transferred to staker2
149+
assertEq(pinakion.balanceOf(address(core)), 22000, "Token balance of the core shouldn't change after rewards");
150+
assertEq(sortitionModule.totalStaked(), 22000, "Total staked shouldn't change after rewards");
151+
147152
assertEq(pinakion.balanceOf(staker1), 999999999999998000, "Wrong token balance of staker1");
148-
assertEq(pinakion.balanceOf(staker2), 999999999999980000, "Wrong token balance of staker2"); // 20k stake and 1k added as a reward, thus -19k from the default
153+
assertEq(pinakion.balanceOf(staker2), 999999999999980000, "Wrong token balance of staker2");
149154
}
150155

151156
function test_execute_NoCoherence() public {
@@ -479,13 +484,19 @@ contract KlerosCore_ExecutionTest is KlerosCore_TestBase {
479484
vm.prank(owner);
480485
core.transferBySortitionModule(staker1, 1000);
481486

487+
assertEq(pinakion.balanceOf(address(core)), 1000, "Wrong token balance of the core");
488+
assertEq(sortitionModule.totalStaked(), 1000, "Wrong totalStaked before withdrawal");
489+
482490
vm.expectEmit(true, true, true, true);
483491
emit SortitionModule.LeftoverPNKWithdrawn(staker1, 1000);
484492
sortitionModule.withdrawLeftoverPNK(staker1);
485493

486494
(totalStaked, , , ) = sortitionModule.getJurorBalance(staker1, GENERAL_COURT);
487495
assertEq(totalStaked, 0, "Should be unstaked fully");
488496

497+
assertEq(pinakion.balanceOf(address(core)), 0, "Wrong token balance of the core");
498+
assertEq(sortitionModule.totalStaked(), 0, "Wrong totalStaked after withdrawal");
499+
489500
// Check that everything is withdrawn now
490501
assertEq(pinakion.balanceOf(address(core)), 0, "Core balance should be empty");
491502
assertEq(pinakion.balanceOf(staker1), 1 ether, "All PNK should be withdrawn");

0 commit comments

Comments
 (0)