Skip to content

Conversation

unknownunknown1
Copy link
Contributor

@unknownunknown1 unknownunknown1 commented Sep 23, 2025

PR-Codex overview

This PR introduces enhancements to the ArbitrableExample contract by adding functionality to manage the number of ruling options in disputes and includes tests for various scenarios related to disputes, voting, and stakes in the Kleros arbitration system.

Detailed summary

  • Added numberOfRulingOptions variable to ArbitrableExample.
  • Implemented changeNumberOfRulingOptions function to update ruling options.
  • Removed local numberOfRulingOptions declarations in createDispute functions.
  • Introduced fuzz tests for creating disputes, casting votes, and executing rulings.
  • Enhanced existing tests to verify functionalities related to disputes and appeals.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Ruling options are now configurable at runtime (default remains 2). Dispute creation and appeal flows consistently use the configured value. Governance can update the number of ruling options.
  • Tests

    • Extensive fuzz tests added across appeals, disputes, drawing, execution, staking, and voting to validate variable ruling options, funding, iterations, and stake scenarios.
    • Improved assertion messages and coverage for end-to-end flows, balances, and juror state transitions.

Copy link

netlify bot commented Sep 23, 2025

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 6752cad
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-neo/deploys/68d2e0f65519900008cd5900
😎 Deploy Preview https://deploy-preview-2144--kleros-v2-neo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Sep 23, 2025

Deploy Preview for kleros-v2-testnet ready!

Name Link
🔨 Latest commit 6752cad
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet/deploys/68d2e0f6eae3a40007cc4d35
😎 Deploy Preview https://deploy-preview-2144--kleros-v2-testnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Sep 23, 2025

Deploy Preview for kleros-v2-testnet-devtools failed. Why did it fail? →

Name Link
🔨 Latest commit 6752cad
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet-devtools/deploys/68d2e0f675f2c700080c1ed4

Copy link
Contributor

coderabbitai bot commented Sep 23, 2025

Walkthrough

Introduces a configurable numberOfRulingOptions state in ArbitrableExample with a governance setter, replacing hard-coded values in dispute creation. Adds multiple fuzz tests across appeals, disputes, drawing, execution, staking, and voting to exercise variable ruling options, amounts, iterations, and balances. Minor assertion text tweak. Possible duplicate tests in execution.

Changes

Cohort / File(s) Summary
Arbitrable config
contracts/src/arbitration/arbitrables/ArbitrableExample.sol
Adds uint256 public numberOfRulingOptions = 2; replaces literals with state usage; introduces changeNumberOfRulingOptions(uint256) restricted to owner.
Appeals fuzzing
contracts/test/foundry/KlerosCore_Appeals.t.sol
Adds fuzz tests for appeals: variable ruling options/choices and variable appeal funding amounts; covers dispute creation, voting, funding, assertions.
Disputes fuzzing
contracts/test/foundry/KlerosCore_Disputes.t.sol
Adds fuzz test for dispute creation with variable msg.value; updates one assertion message string; progresses through full lifecycle with parameterized fees/votes.
Drawing fuzzing
contracts/test/foundry/KlerosCore_Drawing.t.sol
Adds two fuzz tests for draw iterations and juror counts; asserts drawIterations, nbVotes, locked/staked balances, and disputesWithoutJurors.
Execution fuzzing
contracts/test/foundry/KlerosCore_Execution.t.sol
Adds two fuzz tests for execution repartitions vs iterations and juror counts; verifies balances post-execution; duplicate copies of the added tests appear in file.
Staking fuzzing
contracts/test/foundry/KlerosCore_Staking.t.sol
Imports console; adds fuzz tests for staking sequences, cross-court staking, and delayed stakes; adjusts time warps (minStakingTime → maxDrawingTime in places).
Voting fuzzing and imports
contracts/test/foundry/KlerosCore_Voting.t.sol
Extends import to include IArbitratorV2 and IArbitrableV2; adds fuzz test for casting votes with variable ruling options and choices; end-to-end ruling path.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Owner
  actor User
  participant Arbitrable as ArbitrableExample
  participant Core as KlerosCore (Arbitrator)
  participant Jurors

  Owner->>Arbitrable: changeNumberOfRulingOptions(n)
  note right of Arbitrable: Stores n in numberOfRulingOptions

  User->>Arbitrable: createDispute(...)
  Arbitrable->>Core: createDispute(numberOfRulingOptions)
  Core-->>Arbitrable: disputeID

  Core->>Jurors: Draw and notify
  Jurors->>Core: Cast votes (1..n)
  User->>Core: (optional) Fund appeal(s)
  Core->>Jurors: Appeal rounds (as needed)
  Core-->>Arbitrable: Ruling (1..n)
  Arbitrable-->>User: Enforce ruling
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Type: Feature🗿, Compatibility: ABI change 🗯, Package: Contracts

Suggested reviewers

  • jaybuidl
  • kemuru

Poem

A hare taps keys with gentle hops,
Swaps hard-coded twos for toggled props.
Jurors draw, appeals unfurl,
Stakes and votes in fuzzy whirl.
With n options set by paw,
The warren codifies the law. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "feat: fuzzing tests" is concise, follows conventional commit style, and accurately summarizes the primary change in the PR (adding multiple fuzzing tests across the test suite). It is a single short sentence without extraneous file lists or emojis, so a reviewer scanning history will understand the main intent. The title is specific enough for this changeset while remaining suitably brief.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/fuzzing-test

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (10)
contracts/test/foundry/KlerosCore_Disputes.t.sol (1)

150-193: Bound fuzzed value instead of assuming; reduces discarded runs and flakiness.

Use bound to keep runs in-range deterministically.

Apply this diff:

-        // Cap it to 10 eth, so the number of jurors is not astronomical.
-        vm.assume(disputeValue >= arbitrationCost && disputeValue <= 10 ether);
-        vm.deal(disputer, 10 ether);
+        // Cap it to 10 eth, so the number of jurors is not astronomical.
+        disputeValue = bound(disputeValue, arbitrationCost, 10 ether);
+        vm.deal(disputer, 10 ether);
contracts/test/foundry/KlerosCore_Appeals.t.sol (1)

569-628: Prefer bound over assume for appealValue.

Keeps runs within limits and reduces discarded cases.

Apply this diff:

-        vm.assume(appealValue <= 10 ether);
-        vm.deal(crowdfunder1, 10 ether);
+        appealValue = bound(appealValue, 0, 10 ether);
+        vm.deal(crowdfunder1, 10 ether);
contracts/test/foundry/KlerosCore_Staking.t.sol (4)

10-10: Remove unused console import.

Apply this diff:

-import {console} from "forge-std/console.sol";

446-501: Use bound to constrain stake amounts; avoids discarded fuzz runs.

Apply this diff:

-        vm.assume(firstStake >= minStake && firstStake <= stakerSupply);
-        vm.assume(secondStake >= minStake && secondStake <= stakerSupply);
+        firstStake = bound(firstStake, minStake, stakerSupply);
+        secondStake = bound(secondStake, minStake, stakerSupply);

502-579: Bound stake amounts for the second court scenario as well.

Apply this diff:

-        vm.assume(firstStake >= minStake && firstStake <= stakerSupply / 2); // Split the supply into two because courts are different now
-        vm.assume(secondStake >= minStake && secondStake <= stakerSupply / 2);
+        firstStake = bound(firstStake, minStake, stakerSupply / 2); // Split the supply into two because courts are different now
+        secondStake = bound(secondStake, minStake, stakerSupply / 2);

580-620: Constrain iterations to realistic bounds to speed up fuzzing.

Only up to 4 delayed stakes exist here.

Apply this diff:

-        // Test with large numbers but do not trigger possible overflow
-        vm.assume(iterations < 2 ** 128);
+        // Keep within a practical bound; there are only 4 delayed stakes in this test.
+        iterations = bound(iterations, 0, 8);
contracts/test/foundry/KlerosCore_Drawing.t.sol (2)

125-164: Bound iterations to avoid pathological values and OOG risk.

Apply this diff:

-        core.draw(disputeID, iterations);
+        iterations = bound(iterations, 0, DEFAULT_NB_OF_JURORS * 8);
+        core.draw(disputeID, iterations);

165-209: Prefer bound over assume; also constrain iterations relative to jurors.

Apply this diff:

-        // Cap it to 10 eth, so the number of jurors is not astronomical.
-        vm.assume(disputeValue >= arbitrationCost && disputeValue <= 10 ether);
-        vm.deal(disputer, 10 ether);
+        // Cap it to 10 eth, so the number of jurors is not astronomical.
+        disputeValue = bound(disputeValue, arbitrationCost, 10 ether);
+        vm.deal(disputer, 10 ether);
@@
-        core.draw(disputeID, iterations);
+        iterations = bound(iterations, 0, (disputeValue / feeForJuror) * 8);
+        core.draw(disputeID, iterations);
contracts/test/foundry/KlerosCore_Execution.t.sol (2)

752-789: Constrain iterations to a sane range for execute().

Prevents excessive loop requests and speeds up fuzzing.

Apply this diff:

-        core.execute(disputeID, roundID, iterations);
+        iterations = bound(iterations, 0, DEFAULT_NB_OF_JURORS * 16);
+        core.execute(disputeID, roundID, iterations);

790-850: Bound disputeValue and iterations; keep iterations relative to juror count.

Apply this diff:

-        // Cap it to 10 eth, so the number of jurors is not astronomical.
-        vm.assume(disputeValue >= arbitrationCost && disputeValue <= 10 ether);
-        vm.deal(disputer, 10 ether);
+        // Cap it to 10 eth, so the number of jurors is not astronomical.
+        disputeValue = bound(disputeValue, arbitrationCost, 10 ether);
+        vm.deal(disputer, 10 ether);
@@
-        core.execute(disputeID, roundID, iterations);
+        iterations = bound(iterations, 0, nbJurors * 16);
+        core.execute(disputeID, roundID, iterations);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0e96a87 and 6752cad.

📒 Files selected for processing (7)
  • contracts/src/arbitration/arbitrables/ArbitrableExample.sol (2 hunks)
  • contracts/test/foundry/KlerosCore_Appeals.t.sol (1 hunks)
  • contracts/test/foundry/KlerosCore_Disputes.t.sol (2 hunks)
  • contracts/test/foundry/KlerosCore_Drawing.t.sol (1 hunks)
  • contracts/test/foundry/KlerosCore_Execution.t.sol (1 hunks)
  • contracts/test/foundry/KlerosCore_Staking.t.sol (3 hunks)
  • contracts/test/foundry/KlerosCore_Voting.t.sol (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-neo
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-neo
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-neo
  • GitHub Check: Analyze (javascript)
  • GitHub Check: hardhat-tests
🔇 Additional comments (3)
contracts/src/arbitration/arbitrables/ArbitrableExample.sol (1)

43-44: Good move: centralize ruling options in storage.

This reduces duplication and aligns dispute creation with runtime configuration.

contracts/test/foundry/KlerosCore_Disputes.t.sol (1)

97-97: Assertion message tweak is fine.

contracts/test/foundry/KlerosCore_Voting.t.sol (1)

5-5: Import consolidation LGTM.

Comment on lines +105 to +108
function changeNumberOfRulingOptions(uint256 _numberOfRulingOptions) external onlyByOwner {
numberOfRulingOptions = _numberOfRulingOptions;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Guard against zero ruling options and emit an event on change.

Zero would make future dispute creations likely revert (and voting choices invalid). Add validation and an event for observability.

Apply this diff:

 function changeNumberOfRulingOptions(uint256 _numberOfRulingOptions) external onlyByOwner {
-        numberOfRulingOptions = _numberOfRulingOptions;
+        if (_numberOfRulingOptions == 0) revert InvalidNumberOfRulingOptions();
+        uint256 previous = numberOfRulingOptions;
+        numberOfRulingOptions = _numberOfRulingOptions;
+        emit NumberOfRulingOptionsChanged(previous, _numberOfRulingOptions);
 }

And add these declarations in their respective sections:

// Events section
event NumberOfRulingOptionsChanged(uint256 previous, uint256 current);

// Errors section
error InvalidNumberOfRulingOptions();
🤖 Prompt for AI Agents
In contracts/src/arbitration/arbitrables/ArbitrableExample.sol around lines 105
to 108, the setter changeNumberOfRulingOptions currently allows zero and
provides no observable signal; update it to revert when _numberOfRulingOptions
is zero (use a custom error InvalidNumberOfRulingOptions()) and emit a
NumberOfRulingOptionsChanged(previous, current) event after updating
numberOfRulingOptions; also add the event declaration in the Events section and
the error declaration in the Errors section.

Comment on lines +504 to +567
function testFuzz_appeal(uint256 numberOfOptions, uint256 choice1, uint256 choice2, uint256 choice3) public {
uint256 disputeID = 0;

arbitrable.changeNumberOfRulingOptions(numberOfOptions);

// Have only 2 options for 3 jurors to create a majority
vm.assume(choice1 <= numberOfOptions);
vm.assume(choice2 <= numberOfOptions);
vm.assume(choice3 <= numberOfOptions); // Will be used for appeal

vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");

(uint256 numberOfChoices, , ) = disputeKit.disputes(disputeID);

assertEq(numberOfChoices, numberOfOptions, "Wrong numberOfChoices");

vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase

// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);

vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase

core.draw(disputeID, 2); // Assign leftover votes to staker2

vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote

uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake

voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal

vm.assume(choice3 != choice2);
vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.63 ether}(disputeID, choice3); // Fund the losing choice. Total cost will be 0.63 (0.21 + 0.21 * (20000/10000))

assertEq((disputeKit.getFundedChoices(disputeID)).length, 1, "1 choice should be funded");

vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.42 ether}(disputeID, choice2); // Fund the winning choice. Total cost will be 0.42 (0.21 + 0.21 * (10000/10000))

assertEq((disputeKit.getFundedChoices(disputeID)).length, 0, "No funded choices in a fresh round");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fuzz safety: enforce nonzero options and choices in [1..N].

Prevents castVote/fundAppeal from reverting on choice 0 or N=0.

Apply this diff:

-        // Have only 2 options for 3 jurors to create a majority
-        vm.assume(choice1 <= numberOfOptions);
-        vm.assume(choice2 <= numberOfOptions);
-        vm.assume(choice3 <= numberOfOptions); // Will be used for appeal
+        // Have only 2 options for 3 jurors to create a majority
+        vm.assume(numberOfOptions > 0);
+        choice1 = bound(choice1, 1, numberOfOptions);
+        choice2 = bound(choice2, 1, numberOfOptions);
+        choice3 = bound(choice3, 1, numberOfOptions); // Will be used for appeal
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function testFuzz_appeal(uint256 numberOfOptions, uint256 choice1, uint256 choice2, uint256 choice3) public {
uint256 disputeID = 0;
arbitrable.changeNumberOfRulingOptions(numberOfOptions);
// Have only 2 options for 3 jurors to create a majority
vm.assume(choice1 <= numberOfOptions);
vm.assume(choice2 <= numberOfOptions);
vm.assume(choice3 <= numberOfOptions); // Will be used for appeal
vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");
(uint256 numberOfChoices, , ) = disputeKit.disputes(disputeID);
assertEq(numberOfChoices, numberOfOptions, "Wrong numberOfChoices");
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);
vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
core.draw(disputeID, 2); // Assign leftover votes to staker2
vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote
uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake
voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal
vm.assume(choice3 != choice2);
vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.63 ether}(disputeID, choice3); // Fund the losing choice. Total cost will be 0.63 (0.21 + 0.21 * (20000/10000))
assertEq((disputeKit.getFundedChoices(disputeID)).length, 1, "1 choice should be funded");
vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.42 ether}(disputeID, choice2); // Fund the winning choice. Total cost will be 0.42 (0.21 + 0.21 * (10000/10000))
assertEq((disputeKit.getFundedChoices(disputeID)).length, 0, "No funded choices in a fresh round");
}
function testFuzz_appeal(uint256 numberOfOptions, uint256 choice1, uint256 choice2, uint256 choice3) public {
uint256 disputeID = 0;
arbitrable.changeNumberOfRulingOptions(numberOfOptions);
// Have only 2 options for 3 jurors to create a majority
vm.assume(numberOfOptions > 0);
choice1 = bound(choice1, 1, numberOfOptions);
choice2 = bound(choice2, 1, numberOfOptions);
choice3 = bound(choice3, 1, numberOfOptions); // Will be used for appeal
vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");
(uint256 numberOfChoices, , ) = disputeKit.disputes(disputeID);
assertEq(numberOfChoices, numberOfOptions, "Wrong numberOfChoices");
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);
vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
core.draw(disputeID, 2); // Assign leftover votes to staker2
vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote
uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake
voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal
vm.assume(choice3 != choice2);
vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.63 ether}(disputeID, choice3); // Fund the losing choice. Total cost will be 0.63 (0.21 + 0.21 * (20000/10000))
assertEq((disputeKit.getFundedChoices(disputeID)).length, 1, "1 choice should be funded");
vm.prank(crowdfunder1);
disputeKit.fundAppeal{value: 0.42 ether}(disputeID, choice2); // Fund the winning choice. Total cost will be 0.42 (0.21 + 0.21 * (10000/10000))
assertEq((disputeKit.getFundedChoices(disputeID)).length, 0, "No funded choices in a fresh round");
}
🤖 Prompt for AI Agents
In contracts/test/foundry/KlerosCore_Appeals.t.sol around lines 504 to 567, the
fuzz test does not guard against numberOfOptions being zero or choices being
zero/outside 1..numberOfOptions which can cause castVote/fundAppeal to revert;
add vm.assume(numberOfOptions > 0) and replace the existing vm.assume(choiceX <=
numberOfOptions) checks with vm.assume(choiceX >= 1 && choiceX <=
numberOfOptions) for choice1, choice2 and choice3 so all choices are in the
valid 1..N range and N is nonzero.

Comment on lines +487 to +540
function testFuzz_castVote(uint256 numberOfOptions, uint256 choice1, uint256 choice2) public {
uint256 disputeID = 0;

arbitrable.changeNumberOfRulingOptions(numberOfOptions);

// Have only 2 options for 3 jurors to create a majority
vm.assume(choice1 <= numberOfOptions);
vm.assume(choice2 <= numberOfOptions);

vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase

// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);

vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase

core.draw(disputeID, 2); // Assign leftover votes to staker2

vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote

uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake

voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal

vm.warp(block.timestamp + timesPerPeriod[3]);
core.passPeriod(disputeID); // Execution

vm.expectEmit(true, true, true, true);
emit IArbitrableV2.Ruling(IArbitratorV2(address(core)), disputeID, choice2);
core.executeRuling(disputeID);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fuzz safety: enforce choice lower bound and nonzero options.

choice == 0 will revert in castVote; also guard against numberOfOptions == 0.

Apply this diff:

-        // Have only 2 options for 3 jurors to create a majority
-        vm.assume(choice1 <= numberOfOptions);
-        vm.assume(choice2 <= numberOfOptions);
+        // Have only 2 options for 3 jurors to create a majority
+        vm.assume(numberOfOptions > 0);
+        choice1 = bound(choice1, 1, numberOfOptions);
+        choice2 = bound(choice2, 1, numberOfOptions);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function testFuzz_castVote(uint256 numberOfOptions, uint256 choice1, uint256 choice2) public {
uint256 disputeID = 0;
arbitrable.changeNumberOfRulingOptions(numberOfOptions);
// Have only 2 options for 3 jurors to create a majority
vm.assume(choice1 <= numberOfOptions);
vm.assume(choice2 <= numberOfOptions);
vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);
vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
core.draw(disputeID, 2); // Assign leftover votes to staker2
vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote
uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake
voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal
vm.warp(block.timestamp + timesPerPeriod[3]);
core.passPeriod(disputeID); // Execution
vm.expectEmit(true, true, true, true);
emit IArbitrableV2.Ruling(IArbitratorV2(address(core)), disputeID, choice2);
core.executeRuling(disputeID);
}
function testFuzz_castVote(uint256 numberOfOptions, uint256 choice1, uint256 choice2) public {
uint256 disputeID = 0;
arbitrable.changeNumberOfRulingOptions(numberOfOptions);
// Have only 2 options for 3 jurors to create a majority
vm.assume(numberOfOptions > 0);
choice1 = bound(choice1, 1, numberOfOptions);
choice2 = bound(choice2, 1, numberOfOptions);
vm.prank(staker1);
core.setStake(GENERAL_COURT, 2000);
vm.prank(disputer);
arbitrable.createDispute{value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action");
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
// Split the stakers' votes. The first staker will get VoteID 0 and the second will take the rest.
core.draw(disputeID, 1);
vm.warp(block.timestamp + maxDrawingTime);
sortitionModule.passPhase(); // Staking phase to stake the 2nd voter
vm.prank(staker2);
core.setStake(GENERAL_COURT, 20000);
vm.warp(block.timestamp + minStakingTime);
sortitionModule.passPhase(); // Generating
vm.warp(block.timestamp + rngLookahead);
sortitionModule.passPhase(); // Drawing phase
core.draw(disputeID, 2); // Assign leftover votes to staker2
vm.warp(block.timestamp + timesPerPeriod[0]);
core.passPeriod(disputeID); // Vote
uint256[] memory voteIDs = new uint256[](1);
voteIDs[0] = 0;
vm.prank(staker1);
disputeKit.castVote(disputeID, voteIDs, choice1, 0, "XYZ"); // Staker1 only got 1 vote because of low stake
voteIDs = new uint256[](2);
voteIDs[0] = 1;
voteIDs[1] = 2;
vm.prank(staker2);
disputeKit.castVote(disputeID, voteIDs, choice2, 0, "XYZ");
core.passPeriod(disputeID); // Appeal
vm.warp(block.timestamp + timesPerPeriod[3]);
core.passPeriod(disputeID); // Execution
vm.expectEmit(true, true, true, true);
emit IArbitrableV2.Ruling(IArbitratorV2(address(core)), disputeID, choice2);
core.executeRuling(disputeID);
}
🤖 Prompt for AI Agents
In contracts/test/foundry/KlerosCore_Voting.t.sol around lines 487 to 540, the
fuzzed inputs allow numberOfOptions == 0 and choice1/choice2 == 0 which cause
castVote to revert; add preconditions to the fuzz assumptions: require
numberOfOptions > 0, and constrain choice1 and choice2 to be between 1 and
numberOfOptions inclusive (e.g., vm.assume(numberOfOptions > 0);
vm.assume(choice1 >= 1 && choice1 <= numberOfOptions); vm.assume(choice2 >= 1 &&
choice2 <= numberOfOptions)), leaving the rest of the test flow unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant