Skip to content

Commit ad9f77c

Browse files
committed
chore: shrank KlerosCore by consolidating some governance functions and with custom errors
1 parent 5cbb2d0 commit ad9f77c

File tree

1 file changed

+124
-105
lines changed

1 file changed

+124
-105
lines changed

contracts/src/arbitration/KlerosCore.sol

Lines changed: 124 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,15 @@ contract KlerosCore is IArbitrator {
140140
uint256[4] _timesPerPeriod,
141141
uint256[] _supportedDisputeKits
142142
);
143-
event CourtModified(uint96 indexed _courtID, string _param);
143+
event CourtModified(
144+
uint96 indexed _courtID,
145+
bool _hiddenVotes,
146+
uint256 _minStake,
147+
uint256 _alpha,
148+
uint256 _feeForJuror,
149+
uint256 _jurorsForCourtJump,
150+
uint256[4] _timesPerPeriod
151+
);
144152
event DisputeKitCreated(
145153
uint256 indexed _disputeKitID,
146154
IDisputeKit indexed _disputeKitAddress,
@@ -181,7 +189,7 @@ contract KlerosCore is IArbitrator {
181189
// ************************************* //
182190

183191
modifier onlyByGovernor() {
184-
require(governor == msg.sender, "Governor only");
192+
if (governor != msg.sender) revert GovernorOnly();
185193
_;
186194
}
187195

@@ -272,7 +280,7 @@ contract KlerosCore is IArbitrator {
272280
bytes memory _data
273281
) external onlyByGovernor {
274282
(bool success, ) = _destination.call{value: _amount}(_data);
275-
require(success, "Unsuccessful call");
283+
if (!success) revert UnsuccessfulCall();
276284
}
277285

278286
/// @dev Changes the `governor` storage variable.
@@ -306,12 +314,12 @@ contract KlerosCore is IArbitrator {
306314
/// Note that the root DK must be supported by the general court.
307315
function addNewDisputeKit(IDisputeKit _disputeKitAddress, uint256 _parent) external onlyByGovernor {
308316
uint256 disputeKitID = disputeKitNodes.length;
309-
require(_parent < disputeKitID, "!Parent");
317+
if (_parent >= disputeKitID) revert InvalidDisputKitParent();
310318
uint256 depthLevel;
311319
if (_parent != NULL_DISPUTE_KIT) {
312320
depthLevel = disputeKitNodes[_parent].depthLevel + 1;
313321
// It should be always possible to reach the root from the leaf with the defined number of search iterations.
314-
require(depthLevel < SEARCH_ITERATIONS, "Depth level max");
322+
if (depthLevel >= SEARCH_ITERATIONS) revert DepthLevelMax();
315323
}
316324
disputeKitNodes.push(
317325
DisputeKitNode({
@@ -352,18 +360,17 @@ contract KlerosCore is IArbitrator {
352360
bytes memory _sortitionExtraData,
353361
uint256[] memory _supportedDisputeKits
354362
) external onlyByGovernor {
355-
require(courts[_parent].minStake <= _minStake, "MinStake lower than parent court");
356-
require(_supportedDisputeKits.length > 0, "!Supported DK");
357-
require(_parent != FORKING_COURT, "Invalid: Forking court as parent");
363+
if (courts[_parent].minStake > _minStake) revert MinStakeLowerThanParentCourt();
364+
if (_supportedDisputeKits.length == 0) revert UnsupportedDisputeKit();
365+
if (_parent == FORKING_COURT) revert InvalidForkingCourtAsParent();
358366

359367
uint256 courtID = courts.length;
360368
Court storage court = courts.push();
361369

362370
for (uint256 i = 0; i < _supportedDisputeKits.length; i++) {
363-
require(
364-
_supportedDisputeKits[i] > 0 && _supportedDisputeKits[i] < disputeKitNodes.length,
365-
"Wrong DK index"
366-
);
371+
if (_supportedDisputeKits[i] == 0 || _supportedDisputeKits[i] >= disputeKitNodes.length) {
372+
revert WrongDisputeKitIndex();
373+
}
367374
court.supportedDisputeKits[_supportedDisputeKits[i]] = true;
368375
}
369376

@@ -393,60 +400,39 @@ contract KlerosCore is IArbitrator {
393400
);
394401
}
395402

396-
/// @dev Changes the `minStake` property value of a specified court. Don't set to a value lower than its parent's `minStake` property value.
397-
/// @param _courtID The ID of the court.
398-
/// @param _minStake The new value for the `minStake` property value.
399-
function changeCourtMinStake(uint96 _courtID, uint256 _minStake) external onlyByGovernor {
400-
require(
401-
_courtID == GENERAL_COURT || courts[courts[_courtID].parent].minStake <= _minStake,
402-
"MinStake lower than parent court"
403-
);
403+
function changeCourtParameters(
404+
uint96 _courtID,
405+
bool _hiddenVotes,
406+
uint256 _minStake,
407+
uint256 _alpha,
408+
uint256 _feeForJuror,
409+
uint256 _jurorsForCourtJump,
410+
uint256[4] memory _timesPerPeriod
411+
) external onlyByGovernor {
412+
Court storage court = courts[_courtID];
413+
if (_courtID != GENERAL_COURT && courts[courts[_courtID].parent].minStake > _minStake) {
414+
revert MinStakeLowerThanParentCourt();
415+
}
404416
for (uint256 i = 0; i < courts[_courtID].children.length; i++) {
405-
require(courts[courts[_courtID].children[i]].minStake >= _minStake, "MinStake lower than parent court");
417+
if (courts[courts[_courtID].children[i]].minStake < _minStake) {
418+
revert MinStakeLowerThanParentCourt();
419+
}
406420
}
407-
408421
courts[_courtID].minStake = _minStake;
409-
emit CourtModified(_courtID, "minStake");
410-
}
411-
412-
/// @dev Changes the `alpha` property value of a specified court.
413-
/// @param _courtID The ID of the court.
414-
/// @param _alpha The new value for the `alpha` property value.
415-
function changeCourtAlpha(uint96 _courtID, uint256 _alpha) external onlyByGovernor {
422+
courts[_courtID].hiddenVotes = _hiddenVotes;
416423
courts[_courtID].alpha = _alpha;
417-
emit CourtModified(_courtID, "alpha");
418-
}
419-
420-
/// @dev Changes the `feeForJuror` property value of a specified court.
421-
/// @param _courtID The ID of the court.
422-
/// @param _feeForJuror The new value for the `feeForJuror` property value.
423-
function changeCourtJurorFee(uint96 _courtID, uint256 _feeForJuror) external onlyByGovernor {
424424
courts[_courtID].feeForJuror = _feeForJuror;
425-
emit CourtModified(_courtID, "feeForJuror");
426-
}
427-
428-
/// @dev Changes the `jurorsForCourtJump` property value of a specified court.
429-
/// @param _courtID The ID of the court.
430-
/// @param _jurorsForCourtJump The new value for the `jurorsForCourtJump` property value.
431-
function changeCourtJurorsForJump(uint96 _courtID, uint256 _jurorsForCourtJump) external onlyByGovernor {
432425
courts[_courtID].jurorsForCourtJump = _jurorsForCourtJump;
433-
emit CourtModified(_courtID, "jurorsForCourtJump");
434-
}
435-
436-
/// @dev Changes the `hiddenVotes` property value of a specified court.
437-
/// @param _courtID The ID of the court.
438-
/// @param _hiddenVotes The new value for the `hiddenVotes` property value.
439-
function changeCourtHiddenVotes(uint96 _courtID, bool _hiddenVotes) external onlyByGovernor {
440-
courts[_courtID].hiddenVotes = _hiddenVotes;
441-
emit CourtModified(_courtID, "hiddenVotes");
442-
}
443-
444-
/// @dev Changes the `timesPerPeriod` property value of a specified court.
445-
/// @param _courtID The ID of the court.
446-
/// @param _timesPerPeriod The new value for the `timesPerPeriod` property value.
447-
function changeCourtTimesPerPeriod(uint96 _courtID, uint256[4] memory _timesPerPeriod) external onlyByGovernor {
448426
courts[_courtID].timesPerPeriod = _timesPerPeriod;
449-
emit CourtModified(_courtID, "timesPerPeriod");
427+
emit CourtModified(
428+
_courtID,
429+
_hiddenVotes,
430+
_minStake,
431+
_alpha,
432+
_feeForJuror,
433+
_jurorsForCourtJump,
434+
_timesPerPeriod
435+
);
450436
}
451437

452438
/// @dev Adds/removes court's support for specified dispute kits.
@@ -456,13 +442,14 @@ contract KlerosCore is IArbitrator {
456442
function enableDisputeKits(uint96 _courtID, uint256[] memory _disputeKitIDs, bool _enable) external onlyByGovernor {
457443
for (uint256 i = 0; i < _disputeKitIDs.length; i++) {
458444
if (_enable) {
459-
require(_disputeKitIDs[i] > 0 && _disputeKitIDs[i] < disputeKitNodes.length, "Wrong DK index");
445+
if (_disputeKitIDs[i] == 0 || _disputeKitIDs[i] >= disputeKitNodes.length) {
446+
revert WrongDisputeKitIndex();
447+
}
460448
_enableDisputeKit(_courtID, _disputeKitIDs[i], true);
461449
} else {
462-
require(
463-
!(_courtID == GENERAL_COURT && disputeKitNodes[_disputeKitIDs[i]].parent == NULL_DISPUTE_KIT),
464-
"Can't disable Root DK in General"
465-
);
450+
if (_courtID == GENERAL_COURT && disputeKitNodes[_disputeKitIDs[i]].parent == NULL_DISPUTE_KIT) {
451+
revert CannotDisableRootDKInGeneral();
452+
}
466453
_enableDisputeKit(_courtID, _disputeKitIDs[i], false);
467454
}
468455
}
@@ -472,7 +459,7 @@ contract KlerosCore is IArbitrator {
472459
/// @param _feeTokens The new value for the `supportedFeeTokens` storage variable.
473460
/// @param _accepted Whether the token is supported or not as a method of fee payment.
474461
function changeAcceptedFeeTokens(IERC20[] calldata _feeTokens, bool[] calldata _accepted) external onlyByGovernor {
475-
require(_feeTokens.length == _accepted.length, "Arrays length mismatch");
462+
if (_feeTokens.length != _accepted.length) revert ArraysLengthMismatch();
476463
for (uint256 i = 0; i < _feeTokens.length; i++) {
477464
currencyRates[address(_feeTokens[i])].feePaymentAccepted = _accepted[i];
478465
emit AcceptedFeeToken(address(_feeTokens[i]), _accepted[i]);
@@ -484,8 +471,8 @@ contract KlerosCore is IArbitrator {
484471
uint64[] calldata _rateInEth,
485472
uint8[] calldata _rateDecimals
486473
) external onlyByGovernor {
487-
require(_feeTokens.length == _rateInEth.length, "Arrays length mismatch");
488-
require(_feeTokens.length == _rateDecimals.length, "Arrays length mismatch");
474+
if (_feeTokens.length != _rateInEth.length) revert ArraysLengthMismatch();
475+
if (_feeTokens.length != _rateDecimals.length) revert ArraysLengthMismatch();
489476
for (uint256 i = 0; i < _feeTokens.length; i++) {
490477
CurrencyRate storage rate = currencyRates[address(_feeTokens[i])];
491478
rate.rateInEth = _rateInEth[i];
@@ -501,11 +488,11 @@ contract KlerosCore is IArbitrator {
501488
/// @param _courtID The ID of the court.
502489
/// @param _stake The new stake.
503490
function setStake(uint96 _courtID, uint256 _stake) external {
504-
require(_setStakeForAccount(msg.sender, _courtID, _stake, 0), "Staking failed");
491+
if (!_setStakeForAccount(msg.sender, _courtID, _stake, 0)) revert StakingFailed();
505492
}
506493

507494
function setStakeBySortitionModule(address _account, uint96 _courtID, uint256 _stake, uint256 _penalty) external {
508-
require(msg.sender == address(sortitionModule), "Wrong caller");
495+
if (msg.sender != address(sortitionModule)) revert WrongCaller();
509496
_setStakeForAccount(_account, _courtID, _stake, _penalty);
510497
}
511498

@@ -514,10 +501,10 @@ contract KlerosCore is IArbitrator {
514501
uint256 _numberOfChoices,
515502
bytes memory _extraData
516503
) external payable override returns (uint256 disputeID) {
517-
require(msg.value >= arbitrationCost(_extraData), "Arbitration fees: not enough");
504+
if (msg.value < arbitrationCost(_extraData)) revert ArbitrationFeesNotEnough();
518505

519506
(uint96 courtID, , uint256 disputeKitID) = _extraDataToCourtIDMinJurorsDisputeKit(_extraData);
520-
require(courts[courtID].supportedDisputeKits[disputeKitID], "DK unsupported by court");
507+
if (!courts[courtID].supportedDisputeKits[disputeKitID]) revert DisputeKitNotSupportedByCourt();
521508

522509
disputeID = disputes.length;
523510
Dispute storage dispute = disputes.push();
@@ -546,14 +533,12 @@ contract KlerosCore is IArbitrator {
546533
address _feeToken,
547534
uint256 _feeAmount
548535
) external override returns (uint256 disputeID) {
549-
require(currencyRates[_feeToken].feePaymentAccepted, "Token not accepted");
550-
require(
551-
_feeAmount >= convertEthToTokenAmount(_feeToken, arbitrationCost(_extraData)),
552-
"Arbitration fees: not enough"
553-
);
536+
if (!currencyRates[_feeToken].feePaymentAccepted) revert TokenNotAccepted();
537+
if (_feeAmount < convertEthToTokenAmount(_feeToken, arbitrationCost(_extraData)))
538+
revert ArbitrationFeesNotEnough();
554539

555540
(uint96 courtID, , uint256 disputeKitID) = _extraDataToCourtIDMinJurorsDisputeKit(_extraData);
556-
require(courts[courtID].supportedDisputeKits[disputeKitID], "DK unsupported by court");
541+
if (!courts[courtID].supportedDisputeKits[disputeKitID]) revert DisputeKitNotSupportedByCourt();
557542

558543
disputeID = disputes.length;
559544
Dispute storage dispute = disputes.push();
@@ -587,36 +572,38 @@ contract KlerosCore is IArbitrator {
587572
uint256 currentRound = dispute.rounds.length - 1;
588573
Round storage round = dispute.rounds[currentRound];
589574
if (dispute.period == Period.evidence) {
590-
require(
591-
currentRound > 0 ||
592-
block.timestamp - dispute.lastPeriodChange >= court.timesPerPeriod[uint256(dispute.period)],
593-
"Evidence not passed && !Appeal"
594-
);
595-
require(round.drawnJurors.length == round.nbVotes, "Dispute still drawing");
575+
if (
576+
currentRound == 0 &&
577+
block.timestamp - dispute.lastPeriodChange < court.timesPerPeriod[uint256(dispute.period)]
578+
) {
579+
revert EvidenceNotPassedAndNotAppeal();
580+
}
581+
if (round.drawnJurors.length != round.nbVotes) revert DisputeStillDrawing();
596582
dispute.period = court.hiddenVotes ? Period.commit : Period.vote;
597583
} else if (dispute.period == Period.commit) {
598-
require(
599-
block.timestamp - dispute.lastPeriodChange >= court.timesPerPeriod[uint256(dispute.period)] ||
600-
disputeKitNodes[round.disputeKitID].disputeKit.areCommitsAllCast(_disputeID),
601-
"Commit period not passed"
602-
);
584+
if (
585+
block.timestamp - dispute.lastPeriodChange < court.timesPerPeriod[uint256(dispute.period)] &&
586+
!disputeKitNodes[round.disputeKitID].disputeKit.areCommitsAllCast(_disputeID)
587+
) {
588+
revert CommitPeriodNotPassed();
589+
}
603590
dispute.period = Period.vote;
604591
} else if (dispute.period == Period.vote) {
605-
require(
606-
block.timestamp - dispute.lastPeriodChange >= court.timesPerPeriod[uint256(dispute.period)] ||
607-
disputeKitNodes[round.disputeKitID].disputeKit.areVotesAllCast(_disputeID),
608-
"Vote period not passed"
609-
);
592+
if (
593+
block.timestamp - dispute.lastPeriodChange < court.timesPerPeriod[uint256(dispute.period)] &&
594+
disputeKitNodes[round.disputeKitID].disputeKit.areVotesAllCast(_disputeID)
595+
) {
596+
revert VotePeriodNotPassed();
597+
}
610598
dispute.period = Period.appeal;
611599
emit AppealPossible(_disputeID, dispute.arbitrated);
612600
} else if (dispute.period == Period.appeal) {
613-
require(
614-
block.timestamp - dispute.lastPeriodChange >= court.timesPerPeriod[uint256(dispute.period)],
615-
"Appeal period not passed"
616-
);
601+
if (block.timestamp - dispute.lastPeriodChange < court.timesPerPeriod[uint256(dispute.period)]) {
602+
revert AppealPeriodNotPassed();
603+
}
617604
dispute.period = Period.execution;
618605
} else if (dispute.period == Period.execution) {
619-
revert("Dispute period is final");
606+
revert DisputePeriodIsFinal();
620607
}
621608

622609
dispute.lastPeriodChange = block.timestamp;
@@ -630,7 +617,7 @@ contract KlerosCore is IArbitrator {
630617
Dispute storage dispute = disputes[_disputeID];
631618
uint256 currentRound = dispute.rounds.length - 1;
632619
Round storage round = dispute.rounds[currentRound];
633-
require(dispute.period == Period.evidence, "!Evidence period");
620+
if (dispute.period != Period.evidence) revert NotEvidencePeriod();
634621

635622
IDisputeKit disputeKit = disputeKitNodes[round.disputeKitID].disputeKit;
636623

@@ -657,13 +644,13 @@ contract KlerosCore is IArbitrator {
657644
/// @param _numberOfChoices Number of choices for the dispute. Can be required during court jump.
658645
/// @param _extraData Extradata for the dispute. Can be required during court jump.
659646
function appeal(uint256 _disputeID, uint256 _numberOfChoices, bytes memory _extraData) external payable {
660-
require(msg.value >= appealCost(_disputeID), "ETH too low for appeal cost");
647+
if (msg.value < appealCost(_disputeID)) revert AppealFeesNotEnough();
661648

662649
Dispute storage dispute = disputes[_disputeID];
663-
require(dispute.period == Period.appeal, "Dispute not appealable");
650+
if (dispute.period != Period.appeal) revert DisputeNotAppealable();
664651

665652
Round storage round = dispute.rounds[dispute.rounds.length - 1];
666-
require(msg.sender == address(disputeKitNodes[round.disputeKitID].disputeKit), "Dispute Kit only");
653+
if (msg.sender != address(disputeKitNodes[round.disputeKitID].disputeKit)) revert DisputeKitOnly();
667654

668655
uint96 newCourtID = dispute.courtID;
669656
uint256 newDisputeKitID = round.disputeKitID;
@@ -727,7 +714,7 @@ contract KlerosCore is IArbitrator {
727714
/// @param _iterations The number of iterations to run.
728715
function execute(uint256 _disputeID, uint256 _round, uint256 _iterations) external {
729716
Dispute storage dispute = disputes[_disputeID];
730-
require(dispute.period == Period.execution, "!Execution period");
717+
if (dispute.period != Period.execution) revert NotExecutionPeriod();
731718

732719
Round storage round = dispute.rounds[_round];
733720
IDisputeKit disputeKit = disputeKitNodes[round.disputeKitID].disputeKit;
@@ -921,8 +908,8 @@ contract KlerosCore is IArbitrator {
921908
/// @param _disputeID The ID of the dispute.
922909
function executeRuling(uint256 _disputeID) external {
923910
Dispute storage dispute = disputes[_disputeID];
924-
require(dispute.period == Period.execution, "!Execution period");
925-
require(!dispute.ruled, "Ruling already executed");
911+
if (dispute.period != Period.execution) revert NotExecutionPeriod();
912+
if (dispute.ruled) revert RulingAlreadyExecuted();
926913

927914
(uint256 winningChoice, , ) = currentRuling(_disputeID);
928915
dispute.ruled = true;
@@ -1252,4 +1239,36 @@ contract KlerosCore is IArbitrator {
12521239
);
12531240
return (success && (data.length == 0 || abi.decode(data, (bool))));
12541241
}
1242+
1243+
// ************************************* //
1244+
// * Errors * //
1245+
// ************************************* //
1246+
1247+
error GovernorOnly();
1248+
error UnsuccessfulCall();
1249+
error InvalidDisputKitParent();
1250+
error DepthLevelMax();
1251+
error MinStakeLowerThanParentCourt();
1252+
error UnsupportedDisputeKit();
1253+
error InvalidForkingCourtAsParent();
1254+
error WrongDisputeKitIndex();
1255+
error CannotDisableRootDKInGeneral();
1256+
error ArraysLengthMismatch();
1257+
error StakingFailed();
1258+
error WrongCaller();
1259+
error ArbitrationFeesNotEnough();
1260+
error DisputeKitNotSupportedByCourt();
1261+
error TokenNotAccepted();
1262+
error EvidenceNotPassedAndNotAppeal();
1263+
error DisputeStillDrawing();
1264+
error CommitPeriodNotPassed();
1265+
error VotePeriodNotPassed();
1266+
error AppealPeriodNotPassed();
1267+
error NotEvidencePeriod();
1268+
error AppealFeesNotEnough();
1269+
error DisputeNotAppealable();
1270+
error DisputeKitOnly();
1271+
error NotExecutionPeriod();
1272+
error RulingAlreadyExecuted();
1273+
error DisputePeriodIsFinal();
12551274
}

0 commit comments

Comments
 (0)