Skip to content

Commit cc03353

Browse files
authored
Merge pull request #1618 from kleros/fix/contracts-refactor
fix(Governor): remove CappedMath and tx order
2 parents 21236a1 + 07b07f1 commit cc03353

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

contracts/src/arbitration/KlerosGovernor.sol

+19-31
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ pragma solidity 0.8.24;
99

1010
import {IArbitrableV2, IArbitratorV2} from "./interfaces/IArbitrableV2.sol";
1111
import "./interfaces/IDisputeTemplateRegistry.sol";
12-
import "../libraries/CappedMath.sol";
1312

1413
/// @title KlerosGovernor for V2. Note that appeal functionality and evidence submission will be handled by the court.
1514
contract KlerosGovernor is IArbitrableV2 {
16-
using CappedMath for uint256;
17-
1815
// ************************************* //
1916
// * Enums / Structs * //
2017
// ************************************* //
@@ -74,16 +71,13 @@ contract KlerosGovernor is IArbitrableV2 {
7471

7572
modifier duringSubmissionPeriod() {
7673
uint256 offset = sessions[sessions.length - 1].durationOffset;
77-
require(block.timestamp - lastApprovalTime <= submissionTimeout.addCap(offset), "Submission time has ended.");
74+
require(block.timestamp - lastApprovalTime <= submissionTimeout + offset, "Submission time has ended.");
7875
_;
7976
}
8077

8178
modifier duringApprovalPeriod() {
8279
uint256 offset = sessions[sessions.length - 1].durationOffset;
83-
require(
84-
block.timestamp - lastApprovalTime > submissionTimeout.addCap(offset),
85-
"Approval time not started yet."
86-
);
80+
require(block.timestamp - lastApprovalTime > submissionTimeout + offset, "Approval time not started yet.");
8781
_;
8882
}
8983

@@ -200,7 +194,6 @@ contract KlerosGovernor is IArbitrableV2 {
200194
// ************************************* //
201195

202196
/// @dev Creates transaction list based on input parameters and submits it for potential approval and execution.
203-
/// Transactions must be ordered by their hash.
204197
/// @param _target List of addresses to call.
205198
/// @param _value List of values required for respective addresses.
206199
/// @param _data Concatenated calldata of all transactions of this list.
@@ -221,11 +214,9 @@ contract KlerosGovernor is IArbitrableV2 {
221214
// Do the assignment first to avoid creating a new variable and bypass a 'stack too deep' error.
222215
submission.deposit = submissionBaseDeposit + arbitrator.arbitrationCost(arbitratorExtraData);
223216
require(msg.value >= submission.deposit, "Not enough ETH to cover deposit");
224-
// Using an array to get around the stack limit.
225-
// 0 - List hash.
226-
// 1 - Previous transaction hash.
227-
// 2 - Current transaction hash.
228-
bytes32[3] memory hashes;
217+
218+
bytes32 listHash;
219+
bytes32 currentTxHash;
229220
uint256 readingPosition;
230221
for (uint256 i = 0; i < _target.length; i++) {
231222
bytes memory readData = new bytes(_dataSize[i]);
@@ -237,18 +228,16 @@ contract KlerosGovernor is IArbitrableV2 {
237228
}
238229
transaction.data = readData;
239230
readingPosition += _dataSize[i];
240-
hashes[2] = keccak256(abi.encodePacked(transaction.target, transaction.value, transaction.data));
241-
require(uint256(hashes[2]) >= uint256(hashes[1]), "Incorrect tx order");
242-
hashes[0] = keccak256(abi.encodePacked(hashes[2], hashes[0]));
243-
hashes[1] = hashes[2];
231+
currentTxHash = keccak256(abi.encodePacked(transaction.target, transaction.value, transaction.data));
232+
listHash = keccak256(abi.encodePacked(currentTxHash, listHash));
244233
}
245-
require(!session.alreadySubmitted[hashes[0]], "List already submitted");
246-
session.alreadySubmitted[hashes[0]] = true;
247-
submission.listHash = hashes[0];
234+
require(!session.alreadySubmitted[listHash], "List already submitted");
235+
session.alreadySubmitted[listHash] = true;
236+
submission.listHash = listHash;
248237
submission.submissionTime = block.timestamp;
249238
session.sumDeposit += submission.deposit;
250239
session.submittedLists.push(submissions.length - 1);
251-
if (session.submittedLists.length == 1) session.durationOffset = block.timestamp.subCap(lastApprovalTime);
240+
if (session.submittedLists.length == 1) session.durationOffset = block.timestamp - lastApprovalTime;
252241

253242
emit ListSubmitted(submissions.length - 1, msg.sender, sessions.length - 1, _description);
254243

@@ -273,10 +262,9 @@ contract KlerosGovernor is IArbitrableV2 {
273262
session.submittedLists[_submissionID] = session.submittedLists[session.submittedLists.length - 1];
274263
session.alreadySubmitted[_listHash] = false;
275264
session.submittedLists.pop();
276-
session.sumDeposit = session.sumDeposit.subCap(submission.deposit);
265+
session.sumDeposit -= submission.deposit;
266+
reservedETH -= submission.deposit;
277267
payable(msg.sender).transfer(submission.deposit);
278-
279-
reservedETH = reservedETH.subCap(submission.deposit);
280268
}
281269

282270
/// @dev Approves a transaction list or creates a dispute if more than one list was submitted.
@@ -299,17 +287,17 @@ contract KlerosGovernor is IArbitrableV2 {
299287
session.status = Status.Resolved;
300288
sessions.push();
301289

302-
reservedETH = reservedETH.subCap(sumDeposit);
290+
reservedETH -= sumDeposit;
303291
} else {
304292
session.status = Status.DisputeCreated;
305293
uint256 arbitrationCost = arbitrator.arbitrationCost(arbitratorExtraData);
306294
session.disputeID = arbitrator.createDispute{value: arbitrationCost}(
307295
session.submittedLists.length,
308296
arbitratorExtraData
309297
);
310-
session.sumDeposit = session.sumDeposit.subCap(arbitrationCost);
311-
312-
reservedETH = reservedETH.subCap(arbitrationCost);
298+
// Check in case arbitration cost increased after the submission. It's unlikely that its increase won't be covered by the base deposit, but technically possible.
299+
session.sumDeposit = session.sumDeposit > arbitrationCost ? session.sumDeposit - arbitrationCost : 0;
300+
reservedETH = reservedETH > arbitrationCost ? reservedETH - arbitrationCost : 0;
313301
emit DisputeRequest(arbitrator, session.disputeID, sessions.length - 1, templateId, "");
314302
}
315303
}
@@ -331,7 +319,7 @@ contract KlerosGovernor is IArbitrableV2 {
331319
submission.submitter.send(session.sumDeposit);
332320
}
333321
// If the ruling is "0" the reserved funds of this session become expendable.
334-
reservedETH = reservedETH.subCap(session.sumDeposit);
322+
reservedETH -= session.sumDeposit;
335323

336324
session.sumDeposit = 0;
337325
lastApprovalTime = block.timestamp;
@@ -370,7 +358,7 @@ contract KlerosGovernor is IArbitrableV2 {
370358
/// @dev Gets the sum of contract funds that are used for the execution of transactions.
371359
/// @return Contract balance without reserved ETH.
372360
function getExpendableFunds() public view returns (uint256) {
373-
return address(this).balance.subCap(reservedETH);
361+
return address(this).balance - reservedETH;
374362
}
375363

376364
/// @dev Gets the info of the specific transaction in the specific list.

0 commit comments

Comments
 (0)