Skip to content
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

feat: snapshot proxy #1872

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

feat: snapshot proxy #1872

wants to merge 3 commits into from

Conversation

unknownunknown1
Copy link
Contributor

@unknownunknown1 unknownunknown1 commented Feb 1, 2025

PR-Codex overview

This PR introduces the KlerosCoreSnapshotProxy contract, enabling Snapshot voting by exposing the staked PNK balance. It also updates the test suite to validate the new functionality and adjusts some imports.

Detailed summary

  • Added KlerosCoreSnapshotProxy contract with functions for managing governance and querying staked PNK.
  • Implemented balanceOf function to retrieve staked PNK for an address.
  • Updated tests in KlerosCore.t.sol to verify the new proxy's behavior and governance functionality.
  • Adjusted imports to reflect the new contract structure.

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

Summary by CodeRabbit

  • New Features

    • Introduced a new governance proxy enabling Snapshot voting by providing staked token balance information.
    • Added secure mechanisms to update key governance parameters.
  • Tests

    • Expanded test coverage to ensure reliable performance of the new proxy and related governance operations.
    • Updated scenarios to validate dispute handling and stakeholder interactions.

Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

Walkthrough

A new Solidity proxy contract (KlerosCoreSnapshotProxy.sol) has been introduced to interface with the Kleros V2 system. This contract exposes the staked Pinakion (PNK) balance for Snapshot voting and enforces governor-restricted access to certain functions. The proxy utilizes an interface (IKlerosCore) to retrieve the sortition module for balance queries. Additionally, test files were updated to adjust import paths, validate the proxy’s initialization, and verify proper event emissions in line with updated base classes.

Changes

File Change Summary
contracts/src/snapshot-proxy/KlerosCoreSnapshotProxy.sol - Introduced new proxy contract for Kleros V2 snapshot integration.
- Added interface IKlerosCore with sortitionModule().
- Defined state variables for core, governor, and token metadata.
- Implemented functions: changeGovernor, changeCore, and balanceOf, along with an access control modifier.
contracts/test/foundry/KlerosCore.t.sol - Updated import statements: replaced imports of IArbitratorV2 and DisputeKitClassic with updated paths referencing KlerosCoreBase and DisputeKitClassicBase.
- Added the test_setStake_snapshotProxyCheck function to test the proxy’s initialization, governance-controlled updates, and event emissions.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Proxy as SnapshotProxy
    participant Core as IKlerosCore
    participant Sortition as SortitionModule

    Caller->>Proxy: balanceOf(account)
    Proxy->>Core: sortitionModule()
    Core-->>Proxy: Return SortitionModule instance
    Proxy->>Sortition: getJurorBalance(0, account)
    Sortition-->>Proxy: Return staked balance
    Proxy-->>Caller: Return staked balance
Loading
sequenceDiagram
    participant Gov as Governor
    participant Proxy as SnapshotProxy

    Gov->>Proxy: changeGovernor(newGovernor)
    Proxy-->>Gov: Governor updated
Loading

Poem

Oh, what a hop in the code tonight,
A Proxy springs forth with governance might.
Staked balances revealed under blockchain light,
With tests ensuring every function’s just right.
I, the little rabbit, dance with pure delight,
Celebrating changes that make our smart contracts ignite! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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

netlify bot commented Feb 1, 2025

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

Name Link
🔨 Latest commit a703fdd
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet-devtools/deploys/679de505e4e69a000860c691

Copy link

netlify bot commented Feb 1, 2025

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

Name Link
🔨 Latest commit a703fdd
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet/deploys/679de5054527ad0009dfbc07

Copy link

netlify bot commented Feb 1, 2025

Deploy Preview for kleros-v2-university ready!

Name Link
🔨 Latest commit a703fdd
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-university/deploys/679de505337efb00082786fd
😎 Deploy Preview https://deploy-preview-1872--kleros-v2-university.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 site configuration.

Copy link

codeclimate bot commented Feb 1, 2025

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

View more on Code Climate.

Copy link

sonarqubecloud bot commented Feb 1, 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: 2

🧹 Nitpick comments (4)
contracts/src/snapshot-proxy/KlerosCoreSnapshotProxy.sol (3)

20-24: Consider using immutable state variables.

The name and symbol state variables could be made immutable since they are set at deployment and never change.

Apply this diff to optimize gas usage:

-    string public name = "Staked Pinakion";
-    string public symbol = "stPNK";
+    string public immutable name = "Staked Pinakion";
+    string public immutable symbol = "stPNK";

26-29: Consider adding events for access control.

The onlyByGovernor modifier should emit events when access is denied for better transparency and monitoring.

Add an event and emit it in the modifier:

+    event AccessDenied(address indexed caller, string reason);

     modifier onlyByGovernor() {
-        require(governor == msg.sender, "Access not allowed: Governor only.");
+        if (governor != msg.sender) {
+            emit AccessDenied(msg.sender, "Access not allowed: Governor only.");
+            revert("Access not allowed: Governor only.");
+        }
         _;
     }

34-37: Add events for initialization.

The constructor should emit events when setting the initial governor and core contract addresses.

Add events and emit them in the constructor:

+    event GovernorChanged(address indexed previousGovernor, address indexed newGovernor);
+    event CoreChanged(address indexed previousCore, address indexed newCore);

     constructor(address _governor, IKlerosCore _core) {
+        emit GovernorChanged(address(0), _governor);
+        emit CoreChanged(address(0), address(_core));
         governor = _governor;
         core = _core;
     }
contracts/test/foundry/KlerosCore.t.sol (1)

1283-1308: Enhance test coverage for snapshot proxy.

The test function test_setStake_snapshotProxyCheck should include additional test cases:

  1. Verify behavior when core contract is not set
  2. Test error handling for zero addresses
  3. Test balance updates after stake changes

Add these test cases to improve coverage:

     function test_setStake_snapshotProxyCheck() public {
         vm.prank(staker1);
         core.setStake(GENERAL_COURT, 12346);

         KlerosCoreSnapshotProxy snapshotProxy = new KlerosCoreSnapshotProxy(governor, IKlerosCore(address(core)));
+        // Test zero address validation
+        vm.expectRevert("New governor cannot be zero address");
+        vm.prank(governor);
+        snapshotProxy.changeGovernor(address(0));
+
+        vm.expectRevert("New core cannot be zero address");
+        vm.prank(governor);
+        snapshotProxy.changeCore(IKlerosCore(address(0)));
+
+        // Test balance updates after stake changes
+        vm.prank(staker1);
+        core.setStake(GENERAL_COURT, 20000);
+        assertEq(snapshotProxy.balanceOf(staker1), 20000, "Balance not updated after stake increase");
+
         // ... rest of the test
     }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2730163 and a703fdd.

📒 Files selected for processing (2)
  • contracts/src/snapshot-proxy/KlerosCoreSnapshotProxy.sol (1 hunks)
  • contracts/test/foundry/KlerosCore.t.sol (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (14)
  • GitHub Check: Redirect rules - kleros-v2-university
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-university
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-university
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Analyze (javascript)
  • GitHub Check: contracts-testing
🔇 Additional comments (2)
contracts/src/snapshot-proxy/KlerosCoreSnapshotProxy.sol (1)

13-15: LGTM! Clean interface definition.

The interface is minimal and focused on the required functionality.

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

1358-1358: LGTM! Event emission updates.

The event emissions have been correctly updated to use base classes.

Also applies to: 1624-1624, 1640-1640, 1945-1945, 1954-1954, 1956-1956, 2724-2724, 2728-2728

Comment on lines +41 to +43
function changeGovernor(address _governor) external onlyByGovernor {
governor = _governor;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add zero-address validation in admin functions.

The changeGovernor and changeCore functions should validate that the new addresses are not zero addresses.

Apply this diff to add validation:

     function changeGovernor(address _governor) external onlyByGovernor {
+        require(_governor != address(0), "New governor cannot be zero address");
+        emit GovernorChanged(governor, _governor);
         governor = _governor;
     }

     function changeCore(IKlerosCore _core) external onlyByGovernor {
+        require(address(_core) != address(0), "New core cannot be zero address");
+        emit CoreChanged(address(core), address(_core));
         core = _core;
     }

Also applies to: 47-49

Comment on lines +55 to +57
function balanceOf(address _account) external view returns (uint256 totalStaked) {
(totalStaked, , , ) = core.sortitionModule().getJurorBalance(_account, 0);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation and error handling for external calls.

The balanceOf function should handle potential failures from external calls to the core contract.

Apply this diff to add validation and error handling:

     function balanceOf(address _account) external view returns (uint256 totalStaked) {
+        require(_account != address(0), "Account cannot be zero address");
+        require(address(core) != address(0), "Core contract not set");
+        ISortitionModule sortitionModule = core.sortitionModule();
+        require(address(sortitionModule) != address(0), "Sortition module not set");
         (totalStaked, , , ) = core.sortitionModule().getJurorBalance(_account, 0);
     }
📝 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 balanceOf(address _account) external view returns (uint256 totalStaked) {
(totalStaked, , , ) = core.sortitionModule().getJurorBalance(_account, 0);
}
function balanceOf(address _account) external view returns (uint256 totalStaked) {
require(_account != address(0), "Account cannot be zero address");
require(address(core) != address(0), "Core contract not set");
ISortitionModule sortitionModule = core.sortitionModule();
require(address(sortitionModule) != address(0), "Sortition module not set");
(totalStaked, , , ) = core.sortitionModule().getJurorBalance(_account, 0);
}

Copy link

netlify bot commented Feb 1, 2025

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

Name Link
🔨 Latest commit a703fdd
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-neo/deploys/679de50582f2e100088e59e1

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