Skip to content

Conversation

jaybuidl
Copy link
Member

@jaybuidl jaybuidl commented Oct 6, 2025

PR-Codex overview

This PR focuses on improving the handling of juror coherence and rewards in the KlerosCore contract by refining how juror accounts are accessed and how rewards are processed based on the repartition parameter.

Detailed summary

  • Introduced a variable repartition to simplify calculations based on _params.repartition.
  • Updated the calculation of account to use repartition instead of the previous modulo operation.
  • Changed how rewardedInCourtID is determined, using repartition for retrieving the court ID.
  • Modified the setStakeReward method call to use the new rewardedInCourtID.

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed incorrect mapping of juror votes during reward and penalty processing to ensure accurate rewards.
    • Prevented out-of-bounds errors that could cause reward execution failures.
    • Ensured staking rewards are applied to the correct court and fallback to direct transfers if staking updates fail.
    • Aligned penalty handling with reward distribution for consistent outcomes.
  • Reliability

    • Increased stability and predictability of juror reward execution.

Copy link

netlify bot commented Oct 6, 2025

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

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

Copy link

netlify bot commented Oct 6, 2025

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit bc906e5
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-neo/deploys/68e544cae79cbf00081684ab
😎 Deploy Preview https://deploy-preview-2165--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 Oct 6, 2025

Deploy Preview for kleros-v2-testnet ready!

Name Link
🔨 Latest commit bc906e5
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet/deploys/68e544ca2436ee00094dfc47
😎 Deploy Preview https://deploy-preview-2165--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
Contributor

coderabbitai bot commented Oct 6, 2025

Walkthrough

Introduces a local repartition variable in KlerosCore.execute path computed as _params.repartition % _params.numberOfVotesInRound, and replaces repeated ad-hoc modulo uses with this variable for juror lookup, coherence reward calculation, and court-derived stake reward application (with fallback transfer).

Changes

Cohort / File(s) Summary
Rewards & penalties repartition centralization
contracts/src/arbitration/KlerosCore.sol
Add repartition = _params.repartition % _params.numberOfVotesInRound; replace scattered % usages with repartition for getDegreeOfCoherenceReward, juror resolution, and rewarded/penalized-in-court ID derivation. Use setStakeReward with derived court ID and fallback to token transfer on failure. Control flow otherwise unchanged.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller as Caller
    participant Core as KlerosCore
    participant Draws as Draws/DisputeData
    participant Court as SortitionModule/Court
    participant Token as Token

    Caller->>Core: executeRewards(_params,...)
    activate Core

    Note over Core: repartition = _params.repartition % _params.numberOfVotesInRound

    Core->>Draws: getDrawnJuror(repartition)
    Draws-->>Core: jurorAddress

    Core->>Draws: getCourtIdForJuror(repartition)
    Draws-->>Core: courtId

    Core->>Core: reward = getDegreeOfCoherenceReward(repartition,...)

    alt stake via setStakeReward available
        Core->>Court: setStakeReward(jurorAddress, courtId, reward)
        Court-->>Core: success / fail
        alt success
            Note right of Core: reward staked in courtId
        else fail
            Core->>Token: transfer(jurorAddress, reward)
            Token-->>Core: ok
        end
    else no staking
        Core->>Token: transfer(jurorAddress, reward)
        Token-->>Core: ok
    end

    deactivate Core
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Type: Feature🗿

Suggested reviewers

  • unknownunknown1

Poem

A rabbit hops through code so neat,
Counts each vote with careful feet.
Repartitioned hops, no index stray,
Stakes now land where jurors lay.
Carrots safe — rewards convey. 🥕

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 succinctly captures the primary enhancement—automatically staking token rewards in the originating court. It directly reflects the modification of setStakeReward to use the original court ID rather than the dispute’s courtID. This provides a clear and specific summary of the pull request’s main feature.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch autostake-reward-in-court-stake-origin

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

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: 0

🧹 Nitpick comments (1)
contracts/src/arbitration/KlerosCore.sol (1)

972-1011: LGTM! Rewards are now correctly staked in the juror's origin court.

The changes correctly implement the PR objective by:

  1. Computing a local repartition variable to map the second-pass iteration index back to the juror array index
  2. Using this repartition consistently to resolve the correct juror's address and origin court
  3. Staking rewards in penalizedInCourtID (the court where the juror's stake originated) rather than the current dispute court

This is a significant improvement because jurors who were drawn from a subcourt will now have their rewards auto-staked in that subcourt, maintaining their staking position and avoiding the need for manual restaking.

The fallback to direct transfer (lines 1011-1013) provides appropriate error handling.

Optional: Consider renaming repartition to jurorIndex for clarity.

While the current implementation is correct, the variable name repartition might be confusing since it represents a juror index rather than a repartition action. Consider:

-        uint256 repartition = _params.repartition % _params.numberOfVotesInRound;
+        uint256 jurorIndex = _params.repartition % _params.numberOfVotesInRound;

And update all references accordingly. This would make the code more immediately understandable.

Additionally, the empty line at 1009 could be removed to maintain consistency with the surrounding code style.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7cb1275 and cd70d8f.

📒 Files selected for processing (1)
  • contracts/src/arbitration/KlerosCore.sol (3 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). (14)
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-neo
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-neo
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-neo
  • GitHub Check: hardhat-tests
  • GitHub Check: Analyze (javascript)

coderabbitai[bot]
coderabbitai bot previously approved these changes Oct 6, 2025
@jaybuidl jaybuidl force-pushed the autostake-reward-in-court-stake-origin branch from cd70d8f to bc906e5 Compare October 7, 2025 16:50
Copy link

sonarqubecloud bot commented Oct 7, 2025

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: 0

🧹 Nitpick comments (1)
contracts/src/arbitration/KlerosCore.sol (1)

972-978: Consider adding a comment to explain the repartition calculation.

The repartition variable maps the reward iteration index back to the vote index range. This is a key aspect of the two-pass execution model (penalties first, then rewards), and a brief inline comment would help future maintainers understand this logic.

For example:

+        // Map reward iteration index back to vote index (rewards are processed in second pass: i >= numberOfVotesInRound)
         uint256 repartition = _params.repartition % _params.numberOfVotesInRound;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd70d8f and bc906e5.

📒 Files selected for processing (1)
  • contracts/src/arbitration/KlerosCore.sol (3 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). (14)
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-neo
  • GitHub Check: hardhat-tests
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-neo
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-neo
🔇 Additional comments (2)
contracts/src/arbitration/KlerosCore.sol (2)

1008-1014: LGTM! Correctly implements autostaking in the origin court.

The change correctly implements the PR objective by staking rewards in the court where the juror's stake originates (rewardedInCourtID from round.drawnJurorFromCourtIDs[repartition]) rather than the dispute's court. This aligns with the penalty handling in _executePenalties (line 923) and ensures consistency. The fallback to direct transfer if staking fails (line 1012) provides good defensive behavior.

Based on learnings.


1008-1008: No action needed: arrays are updated together
drawnJurors and drawnJurorFromCourtIDs are only ever pushed side-by-side (KlerosCore.sol 759–760; KlerosCoreUniversity.sol 608–609) and never modified independently, so their lengths always match.

@jaybuidl jaybuidl merged commit 4d14f47 into dev Oct 7, 2025
16 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewards to be auto-staked in the origin court rather than where the dispute was created

1 participant