Skip to content

Commit cc2d0b0

Browse files
0xb337r0070x-r4bbit
authored andcommitted
refactor(Marketplace): Use custom errors instead of string messages
This reduces the gas cost as custom errors always use 4 bytes.
1 parent e62ebf6 commit cc2d0b0

File tree

5 files changed

+169
-86
lines changed

5 files changed

+169
-86
lines changed

contracts/Marketplace.sol

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ import "./Endian.sol";
1212
import "./Groth16.sol";
1313

1414
contract Marketplace is Proofs, StateRetrieval, Endian {
15+
error Marketplace_RepairRewardPercentageTooHigh();
16+
error Marketplace_SlashPercentageTooHigh();
17+
error Marketplace_MaximumSlashingTooHigh();
18+
error Marketplace_InvalidExpiry();
19+
error Marketplace_InvalidMaxSlotLoss();
20+
error Marketplace_InvalidClientAddress();
21+
error Marketplace_RequestAlreadyExists();
22+
error Marketplace_InvalidSlot();
23+
error Marketplace_SlotNotFree();
24+
error Marketplace_InvalidSlotHost();
25+
error Marketplace_AlreadyPaid();
26+
error Marketplace_TransferFailed();
27+
error Marketplace_UnknownRequest();
28+
error Marketplace_RequestNotYetTimedOut();
29+
error Marketplace_InvalidState();
30+
error Marketplace_StartNotBeforeExpiry();
31+
error Marketplace_SlotNotAcceptingProofs();
32+
error Marketplace_SlotIsFree();
33+
1534
using EnumerableSet for EnumerableSet.Bytes32Set;
1635
using Requests for Request;
1736

@@ -61,20 +80,21 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
6180
) Proofs(configuration.proofs, verifier) {
6281
_token = token_;
6382

64-
require(
65-
configuration.collateral.repairRewardPercentage <= 100,
66-
"Must be less than 100"
67-
);
68-
require(
69-
configuration.collateral.slashPercentage <= 100,
70-
"Must be less than 100"
71-
);
72-
require(
83+
if (configuration.collateral.repairRewardPercentage > 100) {
84+
revert Marketplace_RepairRewardPercentageTooHigh();
85+
}
86+
87+
if (configuration.collateral.slashPercentage > 100) {
88+
revert Marketplace_SlashPercentageTooHigh();
89+
}
90+
91+
if (
7392
configuration.collateral.maxNumberOfSlashes *
74-
configuration.collateral.slashPercentage <=
75-
100,
76-
"Maximum slashing exceeds 100%"
77-
);
93+
configuration.collateral.slashPercentage >
94+
100
95+
) {
96+
revert Marketplace_MaximumSlashingTooHigh();
97+
}
7898
_config = configuration;
7999
}
80100

@@ -87,18 +107,20 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
87107
}
88108

89109
function requestStorage(Request calldata request) public {
90-
require(request.client == msg.sender, "Invalid client address");
110+
if (request.client != msg.sender) {
111+
revert Marketplace_InvalidClientAddress();
112+
}
91113

92114
RequestId id = request.id();
93-
require(_requests[id].client == address(0), "Request already exists");
94-
require(
95-
request.expiry > 0 && request.expiry < request.ask.duration,
96-
"Expiry not in range"
97-
);
98-
require(
99-
request.ask.maxSlotLoss <= request.ask.slots,
100-
"maxSlotLoss exceeds slots"
101-
);
115+
if (_requests[id].client != address(0)) {
116+
revert Marketplace_RequestAlreadyExists();
117+
}
118+
if (request.expiry == 0 || request.expiry > request.ask.duration) {
119+
revert Marketplace_InvalidExpiry();
120+
}
121+
if (request.ask.maxSlotLoss > request.ask.slots) {
122+
revert Marketplace_InvalidMaxSlotLoss();
123+
}
102124

103125
_requests[id] = request;
104126
_requestContexts[id].endsAt = block.timestamp + request.ask.duration;
@@ -120,14 +142,18 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
120142
Groth16Proof calldata proof
121143
) public requestIsKnown(requestId) {
122144
Request storage request = _requests[requestId];
123-
require(slotIndex < request.ask.slots, "Invalid slot");
145+
if (slotIndex >= request.ask.slots) {
146+
revert Marketplace_InvalidSlot();
147+
}
124148

125149
SlotId slotId = Requests.slotId(requestId, slotIndex);
126150
Slot storage slot = _slots[slotId];
127151
slot.requestId = requestId;
128152
slot.slotIndex = slotIndex;
129153

130-
require(slotState(slotId) == SlotState.Free, "Slot is not free");
154+
if (slotState(slotId) != SlotState.Free) {
155+
revert Marketplace_SlotNotFree();
156+
}
131157

132158
_startRequiringProofs(slotId, request.ask.proofProbability);
133159
submitProof(slotId, proof);
@@ -160,9 +186,14 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
160186

161187
function freeSlot(SlotId slotId) public slotIsNotFree(slotId) {
162188
Slot storage slot = _slots[slotId];
163-
require(slot.host == msg.sender, "Slot filled by other host");
189+
if (slot.host != msg.sender) {
190+
revert Marketplace_InvalidSlotHost();
191+
}
192+
164193
SlotState state = slotState(slotId);
165-
require(state != SlotState.Paid, "Already paid");
194+
if (state == SlotState.Paid) {
195+
revert Marketplace_AlreadyPaid();
196+
}
166197

167198
if (state == SlotState.Finished) {
168199
_payoutSlot(slot.requestId, slotId);
@@ -209,7 +240,10 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
209240
}
210241

211242
function markProofAsMissing(SlotId slotId, Period period) public {
212-
require(slotState(slotId) == SlotState.Filled, "Slot not accepting proofs");
243+
if (slotState(slotId) != SlotState.Filled) {
244+
revert Marketplace_SlotNotAcceptingProofs();
245+
}
246+
213247
_markProofAsMissing(slotId, period);
214248
Slot storage slot = _slots[slotId];
215249
Request storage request = _requests[slot.requestId];
@@ -296,13 +330,18 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
296330
/// @param requestId the id of the request
297331
function withdrawFunds(RequestId requestId) public {
298332
Request storage request = _requests[requestId];
299-
require(
300-
block.timestamp > requestExpiry(requestId),
301-
"Request not yet timed out"
302-
);
303-
require(request.client == msg.sender, "Invalid client address");
333+
if (block.timestamp <= requestExpiry(requestId)) {
334+
revert Marketplace_RequestNotYetTimedOut();
335+
}
336+
337+
if (request.client != msg.sender) {
338+
revert Marketplace_InvalidClientAddress();
339+
}
340+
304341
RequestContext storage context = _requestContexts[requestId];
305-
require(context.state == RequestState.New, "Invalid state");
342+
if (context.state != RequestState.New) {
343+
revert Marketplace_InvalidState();
344+
}
306345

307346
// Update request state to Cancelled. Handle in the withdraw transaction
308347
// as there needs to be someone to pay for the gas to update the state
@@ -327,7 +366,10 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
327366
}
328367

329368
modifier requestIsKnown(RequestId requestId) {
330-
require(_requests[requestId].client != address(0), "Unknown request");
369+
if (_requests[requestId].client == address(0)) {
370+
revert Marketplace_UnknownRequest();
371+
}
372+
331373
_;
332374
}
333375

@@ -338,7 +380,9 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
338380
}
339381

340382
modifier slotIsNotFree(SlotId slotId) {
341-
require(_slots[slotId].state != SlotState.Free, "Slot is free");
383+
if (_slots[slotId].state == SlotState.Free) {
384+
revert Marketplace_SlotIsFree();
385+
}
342386
_;
343387
}
344388

@@ -362,11 +406,9 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
362406
uint256 startingTimestamp
363407
) private view returns (uint256) {
364408
Request storage request = _requests[requestId];
365-
require(
366-
startingTimestamp < requestExpiry(requestId),
367-
"Start not before expiry"
368-
);
369-
409+
if (startingTimestamp >= requestExpiry(requestId)) {
410+
revert Marketplace_StartNotBeforeExpiry();
411+
}
370412
return (requestExpiry(requestId) - startingTimestamp) * request.ask.reward;
371413
}
372414

@@ -415,7 +457,9 @@ contract Marketplace is Proofs, StateRetrieval, Endian {
415457

416458
function _transferFrom(address sender, uint256 amount) internal {
417459
address receiver = address(this);
418-
require(_token.transferFrom(sender, receiver, amount), "Transfer failed");
460+
if (!_token.transferFrom(sender, receiver, amount)) {
461+
revert Marketplace_TransferFailed();
462+
}
419463
}
420464

421465
event StorageRequested(RequestId requestId, Ask ask, uint256 expiry);

contracts/Proofs.sol

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ import "./Periods.sol";
77
import "./Groth16.sol";
88

99
abstract contract Proofs is Periods {
10+
error Proofs_InsufficientBlockHeight();
11+
error Proofs_InvalidProof();
12+
error Proofs_ProofAlreadySubmitted();
13+
error Proofs_PeriodNotEnded();
14+
error Proofs_ValidationTimedOut();
15+
error Proofs_ProofNotMissing();
16+
error Proofs_ProofNotRequired();
17+
error Proofs_ProofAlreadyMarkedMissing();
18+
1019
ProofConfig private _config;
1120
IGroth16Verifier private _verifier;
1221

1322
constructor(
1423
ProofConfig memory config,
1524
IGroth16Verifier verifier
1625
) Periods(config.period) {
17-
require(block.number > 256, "Insufficient block height");
26+
if (block.number <= 256) {
27+
revert Proofs_InsufficientBlockHeight();
28+
}
29+
1830
_config = config;
1931
_verifier = verifier;
2032
}
@@ -113,19 +125,40 @@ abstract contract Proofs is Periods {
113125
Groth16Proof calldata proof,
114126
uint[] memory pubSignals
115127
) internal {
116-
require(!_received[id][_blockPeriod()], "Proof already submitted");
117-
require(_verifier.verify(proof, pubSignals), "Invalid proof");
128+
if (_received[id][_blockPeriod()]) {
129+
revert Proofs_ProofAlreadySubmitted();
130+
}
131+
132+
if (!_verifier.verify(proof, pubSignals)) {
133+
revert Proofs_InvalidProof();
134+
}
135+
118136
_received[id][_blockPeriod()] = true;
119137
emit ProofSubmitted(id);
120138
}
121139

122140
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
123141
uint256 end = _periodEnd(missedPeriod);
124-
require(end < block.timestamp, "Period has not ended yet");
125-
require(block.timestamp < end + _config.timeout, "Validation timed out");
126-
require(!_received[id][missedPeriod], "Proof was submitted, not missing");
127-
require(_isProofRequired(id, missedPeriod), "Proof was not required");
128-
require(!_missing[id][missedPeriod], "Proof already marked as missing");
142+
if (end >= block.timestamp) {
143+
revert Proofs_PeriodNotEnded();
144+
}
145+
146+
if (block.timestamp >= end + _config.timeout) {
147+
revert Proofs_ValidationTimedOut();
148+
}
149+
150+
if (_received[id][missedPeriod]) {
151+
revert Proofs_ProofNotMissing();
152+
}
153+
154+
if (!_isProofRequired(id, missedPeriod)) {
155+
revert Proofs_ProofNotRequired();
156+
}
157+
158+
if (_missing[id][missedPeriod]) {
159+
revert Proofs_ProofAlreadyMarkedMissing();
160+
}
161+
129162
_missing[id][missedPeriod] = true;
130163
_missed[id] += 1;
131164
}

deployments/taiko_test/solcInputs/9671d4e1820229dcd69b349a99feb65b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
"useLiteralContent": true
6363
}
6464
}
65-
}
65+
}

0 commit comments

Comments
 (0)