Skip to content

Commit 6942179

Browse files
committed
refactor: naming things
1 parent dfc22a2 commit 6942179

File tree

6 files changed

+71
-60
lines changed

6 files changed

+71
-60
lines changed

contracts/src/arbitration/KlerosCore.sol

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
115115
// ************************************* //
116116

117117
event StakeSet(address indexed _address, uint256 _courtID, uint256 _amount);
118-
event StakeDelayed(address indexed _address, uint256 _courtID, uint256 _amount);
119-
event StakePartiallyDelayed(address indexed _address, uint256 _courtID, uint256 _amount);
118+
event StakeDelayedNotTransferred(address indexed _address, uint256 _courtID, uint256 _amount);
119+
event StakeDelayedAlreadyTransferred(address indexed _address, uint256 _courtID, uint256 _amount);
120+
event StakeDelayedAlreadyTransferredWithdrawn(
121+
uint96 indexed _courtID,
122+
address indexed _account,
123+
uint256 _withdrawnAmount
124+
);
120125
event NewPeriod(uint256 indexed _disputeID, Period _period);
121126
event AppealPossible(uint256 indexed _disputeID, IArbitrableV2 indexed _arbitrable);
122127
event AppealDecision(uint256 indexed _disputeID, IArbitrableV2 indexed _arbitrable);
@@ -171,7 +176,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
171176
uint256 _feeAmount,
172177
IERC20 _feeToken
173178
);
174-
event PartiallyDelayedStakeWithdrawn(uint96 indexed _courtID, address indexed _account, uint256 _withdrawnAmount);
175179

176180
// ************************************* //
177181
// * Function Modifiers * //
@@ -460,18 +464,26 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
460464
/// @param _newStake The new stake.
461465
/// Note that the existing delayed stake will be nullified as non-relevant.
462466
function setStake(uint96 _courtID, uint256 _newStake) external {
463-
removeDelayedStake(_courtID);
467+
_deleteDelayedStake(_courtID);
464468
if (!_setStakeForAccount(msg.sender, _courtID, _newStake, false)) revert StakingFailed();
465469
}
466470

467-
/// @dev Removes the latest delayed stake if there is any.
468-
/// @param _courtID The ID of the court.
469-
function removeDelayedStake(uint96 _courtID) public {
470-
sortitionModule.checkExistingDelayedStake(_courtID, msg.sender);
471+
function setStakeBySortitionModule(
472+
address _account,
473+
uint96 _courtID,
474+
uint256 _newStake,
475+
bool _alreadyTransferred
476+
) external {
477+
if (msg.sender != address(sortitionModule)) revert SortitionModuleOnly();
478+
// Always nullify the latest delayed stake before setting a new value.
479+
// Note that we check the delayed stake here too because the check in `setStake` can be bypassed
480+
// if the stake was updated automatically during `execute` (e.g. when unstaking inactive juror).
481+
_deleteDelayedStake(_courtID);
482+
_setStakeForAccount(_account, _courtID, _newStake, _alreadyTransferred);
471483
}
472484

473485
function withdrawPartiallyDelayedStake(uint96 _courtID, address _juror, uint256 _amountToWithdraw) external {
474-
if (msg.sender != address(sortitionModule)) revert WrongCaller();
486+
if (msg.sender != address(sortitionModule)) revert SortitionModuleOnly();
475487
uint256 actualAmount = _amountToWithdraw;
476488
Juror storage juror = jurors[_juror];
477489
if (juror.stakedPnk <= actualAmount) {
@@ -481,7 +493,7 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
481493
// StakePnk can become lower because of penalty, thus we adjust the amount for it. stakedPnkByCourt can't be penalized so subtract the default amount.
482494
juror.stakedPnk -= actualAmount;
483495
juror.stakedPnkByCourt[_courtID] -= _amountToWithdraw;
484-
emit PartiallyDelayedStakeWithdrawn(_courtID, _juror, _amountToWithdraw);
496+
emit StakeDelayedAlreadyTransferredWithdrawn(_courtID, _juror, _amountToWithdraw);
485497
// Note that if we don't delete court here it'll be duplicated after staking.
486498
if (juror.stakedPnkByCourt[_courtID] == 0) {
487499
for (uint256 i = juror.courtIDs.length; i > 0; i--) {
@@ -494,20 +506,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
494506
}
495507
}
496508

497-
function setStakeBySortitionModule(
498-
address _account,
499-
uint96 _courtID,
500-
uint256 _newStake,
501-
bool _alreadyTransferred
502-
) external {
503-
if (msg.sender != address(sortitionModule)) revert WrongCaller();
504-
// Always nullify the latest delayed stake before setting a new value.
505-
// Note that we check the delayed stake here too because the check in `setStake` can be bypassed
506-
// if the stake was updated automatically during `execute` (e.g. when unstaking inactive juror).
507-
removeDelayedStake(_courtID);
508-
_setStakeForAccount(_account, _courtID, _newStake, _alreadyTransferred);
509-
}
510-
511509
/// @inheritdoc IArbitratorV2
512510
function createDispute(
513511
uint256 _numberOfChoices,
@@ -1063,6 +1061,12 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
10631061
emit DisputeKitEnabled(_courtID, _disputeKitID, _enable);
10641062
}
10651063

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+
10661070
/// @dev Sets the specified juror's stake in a court.
10671071
/// `O(n + p * log_k(j))` where
10681072
/// `n` is the number of courts the juror has staked in,
@@ -1091,11 +1095,11 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
10911095
return false;
10921096
}
10931097

1094-
ISortitionModule.preStakeHookResult result = sortitionModule.preStakeHook(_account, _courtID, _newStake);
1095-
if (result == ISortitionModule.preStakeHookResult.failed) {
1098+
ISortitionModule.PreStakeHookResult result = sortitionModule.preStakeHook(_account, _courtID, _newStake);
1099+
if (result == ISortitionModule.PreStakeHookResult.failed) {
10961100
return false;
1097-
} else if (result == ISortitionModule.preStakeHookResult.delayed) {
1098-
emit StakeDelayed(_account, _courtID, _newStake);
1101+
} else if (result == ISortitionModule.PreStakeHookResult.stakeDelayedNotTransferred) {
1102+
emit StakeDelayedNotTransferred(_account, _courtID, _newStake);
10991103
return true;
11001104
}
11011105

@@ -1153,8 +1157,8 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
11531157
}
11541158

11551159
// Transfer the tokens but don't update sortition module.
1156-
if (result == ISortitionModule.preStakeHookResult.partiallyDelayed) {
1157-
emit StakePartiallyDelayed(_account, _courtID, _newStake);
1160+
if (result == ISortitionModule.PreStakeHookResult.stakeDelayedAlreadyTransferred) {
1161+
emit StakeDelayedAlreadyTransferred(_account, _courtID, _newStake);
11581162
return true;
11591163
}
11601164

@@ -1201,6 +1205,8 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
12011205
// ************************************* //
12021206

12031207
error GovernorOnly();
1208+
error DisputeKitOnly();
1209+
error SortitionModuleOnly();
12041210
error UnsuccessfulCall();
12051211
error InvalidDisputKitParent();
12061212
error DepthLevelMax();
@@ -1211,7 +1217,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
12111217
error CannotDisableClassicDK();
12121218
error ArraysLengthMismatch();
12131219
error StakingFailed();
1214-
error WrongCaller();
12151220
error ArbitrationFeesNotEnough();
12161221
error DisputeKitNotSupportedByCourt();
12171222
error MustSupportDisputeKitClassic();
@@ -1224,7 +1229,6 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
12241229
error NotEvidencePeriod();
12251230
error AppealFeesNotEnough();
12261231
error DisputeNotAppealable();
1227-
error DisputeKitOnly();
12281232
error NotExecutionPeriod();
12291233
error RulingAlreadyExecuted();
12301234
error DisputePeriodIsFinal();

contracts/src/arbitration/SortitionModule.sol

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
221221
/// @dev Checks if there is already a delayed stake. In this case consider it irrelevant and remove it.
222222
/// @param _courtID ID of the court.
223223
/// @param _juror Juror whose stake to check.
224-
function checkExistingDelayedStake(uint96 _courtID, address _juror) external override onlyByCore {
224+
function deleteDelayedStake(uint96 _courtID, address _juror) external override onlyByCore {
225225
uint256 latestIndex = latestDelayedStakeIndex[_juror][_courtID];
226226
if (latestIndex != 0) {
227227
DelayedStake storage delayedStake = delayedStakes[latestIndex];
@@ -241,28 +241,31 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
241241
address _account,
242242
uint96 _courtID,
243243
uint256 _stake
244-
) external override onlyByCore returns (preStakeHookResult) {
244+
) external override onlyByCore returns (PreStakeHookResult) {
245245
(, , uint256 currentStake, uint256 nbCourts) = core.getJurorBalance(_account, _courtID);
246246
if (currentStake == 0 && nbCourts >= MAX_STAKE_PATHS) {
247247
// Prevent staking beyond MAX_STAKE_PATHS but unstaking is always allowed.
248-
return preStakeHookResult.failed;
248+
return PreStakeHookResult.failed;
249249
} else {
250250
if (phase != Phase.staking) {
251+
// Store the stake change as delayed, to be applied when the phase switches back to Staking.
251252
DelayedStake storage delayedStake = delayedStakes[++delayedStakeWriteIndex];
252253
delayedStake.account = _account;
253254
delayedStake.courtID = _courtID;
254255
delayedStake.stake = _stake;
255256
latestDelayedStakeIndex[_account][_courtID] = delayedStakeWriteIndex;
256257
if (_stake > currentStake) {
257-
// Actual token transfer is done right after this hook.
258+
// PNK deposit: tokens to be transferred now (right after this hook),
259+
// but the stake will be added to the sortition sum tree later.
258260
delayedStake.alreadyTransferred = true;
259-
return preStakeHookResult.partiallyDelayed;
261+
return PreStakeHookResult.stakeDelayedAlreadyTransferred;
260262
} else {
261-
return preStakeHookResult.delayed;
263+
// PNK withdrawal: tokens are not transferred yet.
264+
return PreStakeHookResult.stakeDelayedNotTransferred;
262265
}
263266
}
264267
}
265-
return preStakeHookResult.ok;
268+
return PreStakeHookResult.ok;
266269
}
267270

268271
function createDisputeHook(uint256 /*_disputeID*/, uint256 /*_roundID*/) external override onlyByCore {
@@ -362,6 +365,18 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
362365
drawnAddress = _stakePathIDToAccount(tree.nodeIndexesToIDs[treeIndex]);
363366
}
364367

368+
/// @dev Get the stake of a juror in a court.
369+
/// @param _key The key of the tree, corresponding to a court.
370+
/// @param _ID The stake path ID, corresponding to a juror.
371+
/// @return value The stake of the juror in the court.
372+
function stakeOf(bytes32 _key, bytes32 _ID) public view returns (uint256 value) {
373+
SortitionSumTree storage tree = sortitionSumTrees[_key];
374+
uint treeIndex = tree.IDsToNodeIndexes[_ID];
375+
376+
if (treeIndex == 0) value = 0;
377+
else value = tree.nodes[treeIndex];
378+
}
379+
365380
// ************************************* //
366381
// * Internal * //
367382
// ************************************* //
@@ -522,12 +537,4 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
522537
stakePathID := mload(ptr)
523538
}
524539
}
525-
526-
function stakeOf(bytes32 _key, bytes32 _ID) public view returns (uint256 value) {
527-
SortitionSumTree storage tree = sortitionSumTrees[_key];
528-
uint treeIndex = tree.IDsToNodeIndexes[_ID];
529-
530-
if (treeIndex == 0) value = 0;
531-
else value = tree.nodes[treeIndex];
532-
}
533540
}

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ interface ISortitionModule {
88
drawing // Jurors can be drawn. Pass after all disputes have jurors or `maxDrawingTime` passes.
99
}
1010

11-
enum preStakeHookResult {
11+
enum PreStakeHookResult {
1212
ok, // Correct phase. All checks are passed.
13-
partiallyDelayed, // Wrong phase but stake is increased, so transfer the tokens without updating the drawing chance.
14-
delayed, // Wrong phase and stake is decreased. Delay the token transfer and drawing chance update.
13+
stakeDelayedAlreadyTransferred, // Wrong phase but stake is increased, so transfer the tokens without updating the drawing chance.
14+
stakeDelayedNotTransferred, // Wrong phase and stake is decreased. Delay the token transfer and drawing chance update.
1515
failed // Checks didn't pass. Do no changes.
1616
}
1717

@@ -27,11 +27,11 @@ interface ISortitionModule {
2727

2828
function draw(bytes32 _court, uint256 _coreDisputeID, uint256 _nonce) external view returns (address);
2929

30-
function preStakeHook(address _account, uint96 _courtID, uint256 _stake) external returns (preStakeHookResult);
30+
function preStakeHook(address _account, uint96 _courtID, uint256 _stake) external returns (PreStakeHookResult);
3131

3232
function createDisputeHook(uint256 _disputeID, uint256 _roundID) external;
3333

3434
function postDrawHook(uint256 _disputeID, uint256 _roundID) external;
3535

36-
function checkExistingDelayedStake(uint96 _courtID, address _juror) external;
36+
function deleteDelayedStake(uint96 _courtID, address _juror) external;
3737
}

contracts/test/arbitration/staking.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe("Staking", async () => {
9494
expect(await sortition.delayedStakeReadIndex()).to.be.equal(1);
9595
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(0);
9696
await expect(core.setStake(2, ONE_THOUSAND_PNK.mul(1)))
97-
.to.emit(core, "StakeDelayed")
97+
.to.emit(core, "StakeDelayedNotTransferred")
9898
.withArgs(deployer, 2, ONE_THOUSAND_PNK.mul(1));
9999
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(1);
100100
expect(await core.getJurorBalance(deployer, 2)).to.be.deep.equal([
@@ -113,7 +113,7 @@ describe("Staking", async () => {
113113
balanceBefore = await pnk.balanceOf(deployer);
114114
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(1);
115115
await expect(core.setStake(2, ONE_THOUSAND_PNK.mul(2)))
116-
.to.emit(core, "StakeDelayed")
116+
.to.emit(core, "StakeDelayedNotTransferred")
117117
.withArgs(deployer, 2, ONE_THOUSAND_PNK.mul(2));
118118
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(2);
119119
expect(await core.getJurorBalance(deployer, 2)).to.be.deep.equal([
@@ -152,7 +152,7 @@ describe("Staking", async () => {
152152
await pnk.approve(core.address, ONE_THOUSAND_PNK.mul(1));
153153
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(0);
154154
await expect(core.setStake(2, ONE_THOUSAND_PNK.mul(3)))
155-
.to.emit(core, "StakePartiallyDelayed")
155+
.to.emit(core, "StakeDelayedAlreadyTransferred")
156156
.withArgs(deployer, 2, ONE_THOUSAND_PNK.mul(3));
157157
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(1);
158158
expect(await core.getJurorBalance(deployer, 2)).to.be.deep.equal([
@@ -171,7 +171,7 @@ describe("Staking", async () => {
171171
balanceBefore = await pnk.balanceOf(deployer);
172172
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(1);
173173
await expect(core.setStake(2, ONE_THOUSAND_PNK.mul(2)))
174-
.to.emit(core, "StakeDelayed")
174+
.to.emit(core, "StakeDelayedNotTransferred")
175175
.withArgs(deployer, 2, ONE_THOUSAND_PNK.mul(2));
176176
expect(await sortition.latestDelayedStakeIndex(deployer, 2)).to.be.equal(2);
177177
expect(await core.getJurorBalance(deployer, 2)).to.be.deep.equal([

subgraph/src/KlerosCore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TokenAndETHShift as TokenAndETHShiftEvent,
1313
CourtJump,
1414
Ruling,
15-
StakeDelayed,
15+
StakeDelayedNotTransferred,
1616
AcceptedFeeToken,
1717
} from "../generated/KlerosCore/KlerosCore";
1818
import { ZERO, ONE } from "./utils";
@@ -198,7 +198,7 @@ export function handleStakeSet(event: StakeSet): void {
198198
}
199199
}
200200

201-
export function handleStakeDelayed(event: StakeDelayed): void {
201+
export function handleStakeDelayedNotTransferred(event: StakeDelayedNotTransferred): void {
202202
updateJurorDelayedStake(event.params._address.toHexString(), event.params._courtID.toString(), event.params._amount);
203203
}
204204

subgraph/subgraph.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ dataSources:
4848
handler: handleDisputeKitEnabled
4949
- event: StakeSet(indexed address,uint256,uint256)
5050
handler: handleStakeSet
51-
- event: StakeDelayed(indexed address,uint256,uint256)
52-
handler: handleStakeDelayed
51+
- event: StakeDelayedNotTransferred(indexed address,uint256,uint256)
52+
handler: handleStakeDelayedNotTransferred
5353
- event: TokenAndETHShift(indexed address,indexed uint256,indexed uint256,uint256,int256,int256,address)
5454
handler: handleTokenAndETHShift
5555
- event: Ruling(indexed address,indexed uint256,uint256)

0 commit comments

Comments
 (0)