Skip to content

Commit 7f64aba

Browse files
committed
refactor: moved more logic to _postDrawHook() to avoid extra push/pop
1 parent f5f3079 commit 7f64aba

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

contracts/src/arbitration/dispute-kits/DisputeKitClassicBase.sol

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ abstract contract DisputeKitClassicBase is IDisputeKit, Initializable, UUPSProxi
252252
function draw(
253253
uint256 _coreDisputeID,
254254
uint256 _nonce,
255-
uint256 /*_roundNbVotes*/
255+
uint256 _roundNbVotes
256256
)
257257
public
258258
virtual
@@ -274,7 +274,7 @@ abstract contract DisputeKitClassicBase is IDisputeKit, Initializable, UUPSProxi
274274
return (drawnAddress, fromSubcourtID);
275275
}
276276

277-
if (_postDrawCheck(round, _coreDisputeID, drawnAddress)) {
277+
if (_postDrawCheck(round, _coreDisputeID, drawnAddress, _roundNbVotes)) {
278278
Vote storage vote = round.votes.push();
279279
vote.account = drawnAddress;
280280
round.alreadyDrawn[drawnAddress] = true;
@@ -786,19 +786,20 @@ abstract contract DisputeKitClassicBase is IDisputeKit, Initializable, UUPSProxi
786786
///
787787
/// @param _coreDisputeID ID of the dispute in the core contract.
788788
/// @param _juror Chosen address.
789-
/// @return result Whether the address passes the check or not.
789+
/// @return Whether the address passes the check or not.
790790
function _postDrawCheck(
791791
Round storage /*_round*/,
792792
uint256 _coreDisputeID,
793-
address _juror
794-
) internal view virtual returns (bool result) {
793+
address _juror,
794+
uint256 /*_roundNbVotes*/
795+
) internal view virtual returns (bool) {
795796
if (singleDrawPerJuror) {
796797
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];
797798
Dispute storage dispute = disputes[localDisputeID];
798799
Round storage round = dispute.rounds[dispute.rounds.length - 1];
799-
result = !round.alreadyDrawn[_juror];
800+
return !round.alreadyDrawn[_juror];
800801
} else {
801-
result = true;
802+
return true;
802803
}
803804
}
804805

contracts/src/arbitration/dispute-kits/DisputeKitGated.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ contract DisputeKitGated is DisputeKitClassicBase {
129129
function _postDrawCheck(
130130
Round storage _round,
131131
uint256 _coreDisputeID,
132-
address _juror
132+
address _juror,
133+
uint256 _roundNbVotes
133134
) internal view override returns (bool) {
134-
if (!super._postDrawCheck(_round, _coreDisputeID, _juror)) return false;
135+
if (!super._postDrawCheck(_round, _coreDisputeID, _juror, _roundNbVotes)) return false;
135136

136137
// Get the local dispute and extract token info from extraData
137138
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];

contracts/src/arbitration/dispute-kits/DisputeKitGatedArgentinaConsumerProtection.sol

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,11 @@ contract DisputeKitGatedArgentinaConsumerProtection is DisputeKitClassicBase {
9696

9797
if (drawnAddress == address(0)) return (drawnAddress, fromSubcourtID);
9898

99-
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];
100-
Dispute storage dispute = disputes[localDisputeID];
101-
uint256 localRoundID = dispute.rounds.length - 1;
10299
if (IBalanceHolder(accreditedConsumerProtectionLawyerToken).balanceOf(drawnAddress) > 0) {
103100
// The drawnAddress is a consumer protection lawyer.
101+
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];
102+
uint256 localRoundID = disputes[localDisputeID].rounds.length - 1;
104103
drawnConsumerProtectionLawyer[localDisputeID][localRoundID] = true;
105-
} else {
106-
// The drawnAddress is not a consumer protection lawyer.
107-
if (
108-
dispute.rounds[localRoundID].votes.length == _roundNbVotes &&
109-
!drawnConsumerProtectionLawyer[localDisputeID][localRoundID]
110-
) {
111-
// This is the last draw iteration and we still have not drawn a consumer protection lawyer.
112-
// Drop the last round.votes pushed by super.draw(), so that another iteration can try again.
113-
drawnAddress = address(0);
114-
dispute.rounds[localRoundID].votes.pop();
115-
// Note that round.alreadyDrawn[drawnAddress] is not cleared because we don't know if it has been drawn more than once.
116-
// It's fine because this DisputeKit does not enable singleDrawPerJuror.
117-
}
118104
}
119105
return (drawnAddress, fromSubcourtID);
120106
}
@@ -127,12 +113,28 @@ contract DisputeKitGatedArgentinaConsumerProtection is DisputeKitClassicBase {
127113
function _postDrawCheck(
128114
Round storage _round,
129115
uint256 _coreDisputeID,
130-
address _juror
116+
address _juror,
117+
uint256 _roundNbVotes
131118
) internal view override returns (bool) {
132-
if (!super._postDrawCheck(_round, _coreDisputeID, _juror)) return false;
133-
return
134-
(IBalanceHolder(accreditedLawyerToken).balanceOf(_juror) > 0) ||
135-
(IBalanceHolder(accreditedConsumerProtectionLawyerToken).balanceOf(_juror) > 0);
119+
if (IBalanceHolder(accreditedConsumerProtectionLawyerToken).balanceOf(_juror) == 0) {
120+
// The juror is not a consumer protection lawyer.
121+
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];
122+
Dispute storage dispute = disputes[localDisputeID];
123+
uint256 localRoundID = dispute.rounds.length - 1;
124+
if (
125+
dispute.rounds[localRoundID].votes.length == _roundNbVotes - 1 &&
126+
!drawnConsumerProtectionLawyer[localDisputeID][localRoundID]
127+
) {
128+
// This is the last draw iteration and we still have not drawn a consumer protection lawyer.
129+
// Reject this draw so that another iteration can try again later.
130+
return false;
131+
}
132+
if (IBalanceHolder(accreditedLawyerToken).balanceOf(_juror) == 0) {
133+
// The juror does not hold either of the tokens.
134+
return false;
135+
}
136+
}
137+
return super._postDrawCheck(_round, _coreDisputeID, _juror, _roundNbVotes);
136138
}
137139

138140
// ************************************* //

contracts/src/arbitration/dispute-kits/DisputeKitGatedShutter.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ contract DisputeKitGatedShutter is DisputeKitClassicBase {
256256
function _postDrawCheck(
257257
Round storage _round,
258258
uint256 _coreDisputeID,
259-
address _juror
259+
address _juror,
260+
uint256 _roundNbVotes
260261
) internal view override returns (bool) {
261-
if (!super._postDrawCheck(_round, _coreDisputeID, _juror)) return false;
262+
if (!super._postDrawCheck(_round, _coreDisputeID, _juror, _roundNbVotes)) return false;
262263

263264
// Get the local dispute and extract token info from extraData
264265
uint256 localDisputeID = coreDisputeIDToLocal[_coreDisputeID];

contracts/src/arbitration/dispute-kits/DisputeKitSybilResistant.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ contract DisputeKitSybilResistant is DisputeKitClassicBase {
7070
function _postDrawCheck(
7171
Round storage _round,
7272
uint256 _coreDisputeID,
73-
address _juror
73+
address _juror,
74+
uint256 _roundNbVotes
7475
) internal view override returns (bool) {
75-
return super._postDrawCheck(_round, _coreDisputeID, _juror) && poh.isRegistered(_juror);
76+
return super._postDrawCheck(_round, _coreDisputeID, _juror, _roundNbVotes) && poh.isRegistered(_juror);
7677
}
7778
}

0 commit comments

Comments
 (0)