Skip to content

Conversation

jaybuidl
Copy link
Member

@jaybuidl jaybuidl commented Aug 27, 2025

PR-Codex overview

This PR primarily focuses on enhancing the staking functionality within the Kleros arbitration system. It introduces a new DelayedStakes library for managing delayed stakes, updates function signatures for clarity, and improves gas efficiency.

Detailed summary

  • Reduced runs in hardhat.config.ts optimizer from 10,000 to 2,000.
  • Increased gasLimit in setStake from 300,000 to 500,000.
  • Updated draw function in IDisputeKit to return fromSubcourtID.
  • Added setStakePenalty function in ISortitionModule.
  • Removed penalizeStake function.
  • Updated draw function in ISortitionModule to return fromSubcourtID.
  • Modified getJurorBalance to include fromSubcourtID.
  • Introduced DelayedStakes library for handling delayed stakes.
  • Replaced direct delayed stake management with DelayedStakes library methods.
  • Updated tests to reflect changes in delayed stake handling and assertions.

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

Summary by CodeRabbit

  • New Features
    • Per-subcourt juror tracking enables accurate penalties, unstaking, and rewards based on the juror’s origin court.
    • Expanded visibility: query juror balances per court and inspect delayed stakes, including queue length and individual entries.
  • Refactor
    • Migrated delayed-stake processing to a queue for more reliable and scalable stake execution.
  • Chores
    • Updated Solidity compiler optimizer runs for improved build characteristics.
  • Tests
    • Test suites updated to the new delayed-stakes queue and struct-based accessors.

Copy link

netlify bot commented Aug 27, 2025

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

Name Link
🔨 Latest commit c603f4b
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-university/deploys/68ae6d630c331a0008869a9a

Copy link

netlify bot commented Aug 27, 2025

Deploy Preview for kleros-v2-testnet-devtools ready!

Name Link
🔨 Latest commit c603f4b
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet-devtools/deploys/68ae6d632fcd7e0008a4b64a
😎 Deploy Preview https://deploy-preview-2111--kleros-v2-testnet-devtools.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 Aug 27, 2025

Deploy Preview for kleros-v2-neo ready!

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

coderabbitai bot commented Aug 27, 2025

Caution

Review failed

Failed to post review comments.

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9618a6f and c603f4b.

📒 Files selected for processing (13)
  • contracts/hardhat.config.ts (1 hunks)
  • contracts/src/arbitration/KlerosCoreBase.sol (4 hunks)
  • contracts/src/arbitration/SortitionModuleBase.sol (16 hunks)
  • contracts/src/arbitration/dispute-kits/DisputeKitClassicBase.sol (2 hunks)
  • contracts/src/arbitration/interfaces/IDisputeKit.sol (1 hunks)
  • contracts/src/arbitration/interfaces/ISortitionModule.sol (1 hunks)
  • contracts/src/arbitration/university/KlerosCoreUniversity.sol (4 hunks)
  • contracts/src/arbitration/university/SortitionModuleUniversity.sol (2 hunks)
  • contracts/src/libraries/DelayedStakes.sol (1 hunks)
  • contracts/test/arbitration/dispute-kit-gated.ts (1 hunks)
  • contracts/test/arbitration/staking-neo.ts (14 hunks)
  • contracts/test/arbitration/staking.ts (14 hunks)
  • contracts/test/foundry/KlerosCore.t.sol (6 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). (9)
  • GitHub Check: Redirect rules - kleros-v2-neo
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-university
  • GitHub Check: Header rules - kleros-v2-neo
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Header rules - kleros-v2-university
  • GitHub Check: Pages changed - kleros-v2-neo
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-university

Walkthrough

Adjusts Hardhat optimizer runs. Introduces DelayedStakes queue library and integrates it into Sortition modules. Changes draw to return (address, subcourtID) across interfaces and implementations. Adds per-subcourt juror tracking and penalty application in cores. Replaces penalizeStake with setStakePenalty. Updates tests to new queue/struct APIs.

Changes

Cohort / File(s) Summary
Build config
contracts/hardhat.config.ts
Reduce Solidity optimizer runs from 10000 to 2000 for compiler 0.8.30.
Core (Base + University)
contracts/src/arbitration/KlerosCoreBase.sol, contracts/src/arbitration/university/KlerosCoreUniversity.sol
Track per-draw subcourt IDs via Round.drawnJurorFromCourtIDs. Consume new IDisputeKit.draw returning (address, uint96). Replace penalizeStake with setStakePenalty, apply penalties per subcourt, handle inactivity/unstake based on new stake and minStake.
Sortition modules (Base + University)
contracts/src/arbitration/SortitionModuleBase.sol, contracts/src/arbitration/university/SortitionModuleUniversity.sol
Integrate DelayedStakes.Queue for delayed stakes (add/execute/counters). Replace ID mappings to use stakePathID. Update draw to return (address, uint96); add setStakePenalty; provide delayed-stake accessors and juror balance helper; refactor stake setting via _setStake. Remove legacy delayed stake indices and penalizeStake.
Dispute kit
contracts/src/arbitration/dispute-kits/DisputeKitClassicBase.sol
Propagate new draw signature, returning (address, uint96) and early-return on zero address with both values.
Interfaces
contracts/src/arbitration/interfaces/IDisputeKit.sol, contracts/src/arbitration/interfaces/ISortitionModule.sol
ABI updates: draw now returns (address, uint96); add setStakePenalty; remove penalizeStake; expose delayed stakes queue/struct returns and related view helpers.
Library
contracts/src/libraries/DelayedStakes.sol
New library implementing a queue for delayed stakes with add/execute/iterate/count helpers; returns DelayedStakes.Stake structs.
Tests (JS/TS)
contracts/test/arbitration/staking.ts, contracts/test/arbitration/staking-neo.ts, contracts/test/arbitration/dispute-kit-gated.ts
Migrate tests to queue-based API: use delayedStakesQueue() (writeIndex/readIndex) and struct-based delayedStakes(...). Increase gas limit in helper for setStake. Remove deprecated getters/usages.
Tests (Foundry)
contracts/test/foundry/KlerosCore.t.sol
Import/use DelayedStakes types. Replace index getters with delayedStakesQueue(); switch tuple reads to DelayedStakes.Stake struct; adjust loops and event expectations accordingly.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Core as KlerosCore(Base/University)
  participant DK as DisputeKitClassicBase
  participant Sort as SortitionModule(Base/University)

  Core->>DK: draw(coreDisputeID, nonce)
  DK->>Sort: draw(courtKey, coreDisputeID, nonce)
  Sort-->>DK: (drawnAddress, fromSubcourtID)
  DK-->>Core: (drawnAddress, fromSubcourtID)
  alt drawnAddress != 0
    Core->>Core: record Round.drawnJurorFromCourtIDs[idx] = fromSubcourtID!=0?fromSubcourtID:courtID
  else drawnAddress == 0
    Core->>Core: no-op (propagate zeros)
  end
Loading
sequenceDiagram
  autonumber
  actor Core as KlerosCore(Base/University)
  participant Sort as SortitionModule(Base/University)
  participant Courts as Courts Registry

  Core->>Core: determine penalizedInCourtID from Round.drawnJurorFromCourtIDs[idx]
  Core->>Sort: setStakePenalty(account, penalizedInCourtID, penalty)
  Sort-->>Core: (pnkBalance, newCourtStake, availablePenalty)
  alt pnkBalance == 0 or vote not active
    Core->>Sort: setJurorInactive(account)
  else newCourtStake < Courts.minStake(penalizedInCourtID)
    Core->>Sort: setStake(account, penalizedInCourtID, 0, 0, 0)  %% unstake in subcourt
  else
    Core->>Core: continue
  end
Loading
sequenceDiagram
  autonumber
  actor Ext as External Caller
  participant Sort as SortitionModuleBase
  participant Lib as DelayedStakes.Queue
  participant Core as KlerosCore

  Ext->>Sort: setStake(account, courtID, deposit/withdraw)
  alt drawing phase
    Sort->>Lib: add(account, courtID, stake)
  else not drawing
    Sort->>Core: setStakeBySortitionModule(account, courtID, stake)
  end

  Ext->>Sort: executeDelayedStakes(iterations)
  Sort->>Lib: execute(Core, iterations)
  Lib->>Core: setStakeBySortitionModule(...) per item
  Lib-->>Sort: executed? (bool)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested labels

Type: Maintenance :construction:, Compatibility: ABI change 🗯, Package: Contracts

Suggested reviewers

  • clesaege
  • unknownunknown1

Poem

A hop, a skip, a queue anew,
I nibble stakes and push them through.
Subcourts mapped, penalties neat—
Two-value draws make justice sweet.
Thump-thump goes my fluffy heart,
As structs and queues perform their part. 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/sortition-libraries

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jaybuidl jaybuidl changed the base branch from dev to fix/penalties-reduce-drawing-odds August 27, 2025 02:29
Copy link

netlify bot commented Aug 27, 2025

Deploy Preview for kleros-v2-testnet ready!

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

@jaybuidl jaybuidl changed the title Sortition module: externalized some logic to libraries Sortition module: externalize delayed stakes to a library Aug 27, 2025
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.

1 participant