Skip to content

Fix: Update isCurrentRound for previous round on appeal #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

alcercu
Copy link
Contributor

@alcercu alcercu commented May 22, 2025

The isCurrentRound field in the Round entity was not being set to false for the previous round when a new round was created due to an appeal.

This commit modifies the handleAppealDecision handler in KlerosCore.ts to:

  1. Load the round that was current before the appeal.
  2. Set its isCurrentRound field to false and save it.
  3. Proceed to create the new round, which will have isCurrentRound set to true by the createRoundFromRoundInfo function.

A new test suite, kleros-core.test.ts, was added with a specific test case to verify this behavior. The test ensures that after an appeal:

  • The previous round's isCurrentRound is false.
  • The new round's isCurrentRound is true.
  • The dispute's currentRound and currentRoundIndex are correctly updated.
  • Court statistics are updated.

PR-Codex overview

This PR focuses on updating the handling of rounds in the KlerosCore contract. It adds functionality to manage the previous round when a new round is created, ensuring that the previous round is marked as not current.

Detailed summary

  • Added loading of the current (previous) round using dispute.currentRound.
  • Set previousRound.isCurrentRound to false if the previous round exists and saved the changes.
  • Changed variable name from roundID to newRoundID for clarity.
  • Updated dispute.currentRound to use newRoundID.

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

Summary by CodeRabbit

  • Bug Fixes

    • Ensured that only one round is marked as current at any time during appeal decisions, improving round state accuracy.
  • Tests

    • Added utilities for creating mock appeal decision events.
    • Introduced comprehensive tests to verify correct round updates and state transitions when handling appeal decisions.

The isCurrentRound field in the Round entity was not being set to false for the previous round when a new round was created due to an appeal.

This commit modifies the handleAppealDecision handler in KlerosCore.ts to:
1. Load the round that was current before the appeal.
2. Set its isCurrentRound field to false and save it.
3. Proceed to create the new round, which will have isCurrentRound set to true by the createRoundFromRoundInfo function.

A new test suite, kleros-core.test.ts, was added with a specific test case to verify this behavior. The test ensures that after an appeal:
- The previous round's isCurrentRound is false.
- The new round's isCurrentRound is true.
- The dispute's currentRound and currentRoundIndex are correctly updated.
- Court statistics are updated.
@alcercu alcercu requested a review from a team as a code owner May 22, 2025 09:19
Copy link

netlify bot commented May 22, 2025

Deploy Preview for kleros-v2-testnet ready!

Name Link
🔨 Latest commit 3925b6f
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet/deploys/683051ed0a8f590008ab04f2
😎 Deploy Preview https://deploy-preview-2007--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 May 22, 2025

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

Name Link
🔨 Latest commit 3925b6f
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-university/deploys/683051ed8882f900089da80b

Copy link

netlify bot commented May 22, 2025

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 3925b6f
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-neo/deploys/683051ed8882f900089da80f
😎 Deploy Preview https://deploy-preview-2007--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 May 22, 2025

Walkthrough

The changes introduce explicit state management for dispute rounds in the KlerosCore subgraph, ensuring only one round is marked as current at a time. A new test utility for creating mock AppealDecision events is added, and comprehensive tests are implemented to verify correct round updates and entity state after handling an appeal decision.

Changes

File(s) Change Summary
subgraph/core/src/KlerosCore.ts Modified handleAppealDecision to explicitly update the current and previous round's status, ensuring correct state.
subgraph/core/tests/kleros-core-utils.ts Added createAppealDecisionEvent utility to generate mock AppealDecision events for testing.
subgraph/core/tests/kleros-core.test.ts Added a new test suite for handleAppealDecision, including setup, teardown, and assertions for correct round updates.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Suite
    participant Handler as handleAppealDecision
    participant Store as Subgraph Store
    participant Contract as KlerosCore Contract

    Test->>Handler: Trigger with mock AppealDecision event
    Handler->>Store: Load Dispute and previous Round
    Handler->>Store: Set previous Round isCurrentRound = false
    Handler->>Contract: Fetch new round info (getRoundInfo)
    Handler->>Store: Create new Round entity (isCurrentRound = true)
    Handler->>Store: Update Dispute's currentRound and currentRoundIndex
    Handler->>Store: Update Court's numberVotes
    Handler-->>Test: Test asserts entity states
Loading

Suggested labels

Type: Bug :bug:, Package: Contracts

Suggested reviewers

  • jaybuidl

Poem

A round of appeals, a hop and a bound,
Only one "current" in the warren is found.
With tests and with mocks, the verdict is clear,
The state is consistent, no bugs to fear!
🐇✨
Code and carrots, both fresh and profound!

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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.

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

🧹 Nitpick comments (2)
subgraph/core/tests/kleros-core-utils.ts (1)

11-11: Consider using a more descriptive parameter name

The static analysis warning about shadowing the global "BigInt" property is valid. While this won't cause functional issues, it's good practice to use more descriptive parameter names.

- transactionHash: string = "0x0000000000000000000000000000000000000000000000000000000000000000" // Default transaction hash
+ txHash: string = "0x0000000000000000000000000000000000000000000000000000000000000000" // Default transaction hash
subgraph/core/tests/kleros-core.test.ts (1)

11-11: Consider using a more descriptive import name

Similar to the utility file, there's a static analysis warning about shadowing the global "BigInt" property. Consider using a more descriptive import name.

- import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
+ import { Address, BigInt as GraphBigInt, ethereum } from "@graphprotocol/graph-ts";

Then update all references to BigInt in the file.

🧰 Tools
🪛 Biome (1.9.4)

[error] 11-11: Do not shadow the global "BigInt" property.

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

(lint/suspicious/noShadowRestrictedNames)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ca9066 and 29ef9a1.

📒 Files selected for processing (3)
  • subgraph/core/src/KlerosCore.ts (1 hunks)
  • subgraph/core/tests/kleros-core-utils.ts (1 hunks)
  • subgraph/core/tests/kleros-core.test.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
subgraph/core/tests/kleros-core-utils.ts

[error] 2-2: Do not shadow the global "BigInt" property.

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

(lint/suspicious/noShadowRestrictedNames)

subgraph/core/tests/kleros-core.test.ts

[error] 11-11: Do not shadow the global "BigInt" property.

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

(lint/suspicious/noShadowRestrictedNames)

⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: SonarCloud
  • GitHub Check: contracts-testing
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Redirect rules - kleros-v2-neo
  • GitHub Check: Redirect rules - kleros-v2-university
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-neo
  • GitHub Check: Header rules - kleros-v2-university
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-neo
  • GitHub Check: Pages changed - kleros-v2-university
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Mend Security Check
🔇 Additional comments (8)
subgraph/core/tests/kleros-core-utils.ts (1)

5-30: Function implementation looks good!

This utility function properly creates a mock AppealDecision event with all the necessary parameters for testing. The default transaction hash parameter is a nice touch for test flexibility.

subgraph/core/src/KlerosCore.ts (3)

219-225: Great fix for the round state management!

This change correctly addresses the issue where previous rounds weren't being marked as non-current when appeals occurred. By explicitly loading the previous round and setting its isCurrentRound flag to false, you ensure only one round is marked as current at any given time.


228-230: Improved variable naming for better clarity

The change from roundID to newRoundID clarifies that this is the ID for the newly created round. This makes the code more readable and maintainable.


213-244:

✅ Verification successful

Verify all code paths handle round state correctly

While the current fix addresses the immediate issue, it would be good to ensure all code paths where rounds are created maintain consistent state. Let's verify that other handlers that create new rounds also manage the isCurrentRound flag properly.


🏁 Script executed:

#!/bin/bash
# Find all places where rounds are created or where isCurrentRound might be set

# Look for createRoundFromRoundInfo calls
echo "Checking all places where rounds are created:"
rg -A 3 -B 3 "createRoundFromRoundInfo" ./subgraph/core/src/

# Look for places where isCurrentRound is set
echo "Checking all places where isCurrentRound is set:"
rg "isCurrentRound" ./subgraph/core/src/

Length of output: 2541


isCurrentRound flag handling verified across all round creation paths

I’ve confirmed that:

  • The only two uses of createRoundFromRoundInfo are in the initial dispute handler and in handleAppealDecision.
  • In the initial handler, there’s no prior round to clear, and createRoundFromRoundInfo sets isCurrentRound = true.
  • In handleAppealDecision, the previous round’s isCurrentRound is explicitly set to false before creating the new round with isCurrentRound = true.
  • No other code paths or handlers invoke createRoundFromRoundInfo.

All round-creation paths correctly manage the isCurrentRound flag. No further changes are needed.

subgraph/core/tests/kleros-core.test.ts (4)

24-118: Well-structured test setup

The test setup is thorough and creates all the necessary entities with appropriate values. The initialization of Court, Dispute, and Round entities with realistic values provides a good foundation for testing the appeal decision handling.


120-163: Comprehensive contract mocking

The mocking of contract calls is well-implemented and covers all the necessary interactions for the handleAppealDecision function. The detailed comments explaining the structure of the return values make the test more maintainable.


169-224: Thorough test assertions

The test assertions comprehensively verify that the handleAppealDecision function correctly updates all relevant entities: the previous round's isCurrentRound flag is set to false, the dispute's current round and index are updated, and a new round is created with the correct attributes.


222-223: Good use of commented debugging tool

Keeping the logStore() call commented out is a good practice. It's available when needed for debugging but doesn't clutter the test output during normal runs.

appealDecisionEvent.block.number = blockNumber;
appealDecisionEvent.block.timestamp = timestamp;
appealDecisionEvent.logIndex = logIndex;
appealDecisionEvent.transaction.hash = Address.fromString(transactionHash);
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

Fix the transaction hash type conversion

The transaction hash is incorrectly converted to an Address type. Transaction hashes should be Bytes values, not Addresses.

- appealDecisionEvent.transaction.hash = Address.fromString(transactionHash);
+ appealDecisionEvent.transaction.hash = ethereum.Value.fromString(transactionHash).toBytes();
📝 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
appealDecisionEvent.transaction.hash = Address.fromString(transactionHash);
appealDecisionEvent.transaction.hash = ethereum.Value.fromString(transactionHash).toBytes();
🤖 Prompt for AI Agents
In subgraph/core/tests/kleros-core-utils.ts at line 19, the transaction hash is
incorrectly converted to an Address type. Change the conversion to use
Bytes.fromHexString or the appropriate Bytes constructor instead of
Address.fromString to correctly represent the transaction hash as a Bytes value.

Copy link

netlify bot commented May 22, 2025

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

Name Link
🔨 Latest commit 3925b6f
🔍 Latest deploy log https://app.netlify.com/projects/kleros-v2-testnet-devtools/deploys/683051ed8882f900089da811

The isCurrentRound field in the Round entity was not being set to false for the previous round when a new round was created due to an appeal.

This commit modifies the handleAppealDecision handler in KlerosCore.ts to:
1. Load the round that was current before the appeal.
2. Set its isCurrentRound field to false and save it.
3. Proceed to create the new round, which will have isCurrentRound set to true by the createRoundFromRoundInfo function.

Newly added test files (kleros-core.test.ts and kleros-core-utils.ts) were removed from this commit due to feedback regarding outdated testing tooling. The core logic fix remains.
Copy link

codeclimate bot commented May 23, 2025

Code Climate has analyzed commit 3925b6f and detected 0 issues on this pull request.

View more on Code Climate.

Copy link

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