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: Restake Permission #640

Merged
merged 1 commit into from
Nov 19, 2024
Merged

feat: Restake Permission #640

merged 1 commit into from
Nov 19, 2024

Conversation

joemonem
Copy link
Contributor

@joemonem joemonem commented Nov 19, 2024

Motivation

@crnbarr93 's suggestion

Implementation

Contract owner should permission the restake action, then set permission to actors to restake on his behalf. Normal claiming is still restricted to contract owner only.

Testing

Added integration test.

Version Changes

validator-staking: No change

Notes

The "restake" action is hard-coded, not derived from an ExecuteMsg name.

Future work

Give permission for Claim as well?

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced staking and reward claiming processes with improved access control for restaking.
    • Added permission management capabilities, allowing specified actors to perform actions.
  • Bug Fixes

    • Updated checks in the unstaking process to prevent invalid amounts.
  • Tests

    • Expanded test cases to cover permissioned actions and unauthorized claims, ensuring robust validation of staking logic.
  • Documentation

    • Introduced new constants for better clarity in staking operations.

Copy link
Contributor

coderabbitai bot commented Nov 19, 2024

Walkthrough

The pull request introduces multiple enhancements to the andromeda-validator-staking contract, focusing on the staking, unstaking, and reward claiming functionalities. Key changes include the addition of a RESTAKING_ACTION constant, modifications to the execute_claim function to allow permissioned actors to restake rewards, and improved checks in the execute_unstake function. The mock testing framework has also been updated to include new permission management methods and enhanced test cases, while some tests have been removed to reflect changes in the contract's logic.

Changes

File Path Change Summary
contracts/finance/andromeda-validator-staking/src/contract.rs - Added constant RESTAKING_ACTION.
- Updated execute_claim to allow permissioned actors to restake.
- Enhanced execute_unstake with additional checks.
contracts/finance/andromeda-validator-staking/src/mock.rs - Added methods for permission management: execute_set_permission, execute_permission_action.
- Defined mock_set_permission and mock_permission_action functions.
contracts/finance/andromeda-validator-staking/src/testing/tests.rs - Removed test_unauthorized_claim test.
- Removed constant ANYONE.
packages/andromeda-finance/src/validator_staking.rs - Added constant RESTAKING_ACTION.
tests-integration/tests/validator_staking.rs - Expanded wallet setup in tests.
- Updated tests for unauthorized claims and permission checks.
- Modified method signatures for permission actions.

Possibly related PRs

  • refactor: Validator Staking Adjustments #458: The changes in this PR involve modifications to the execute_unstake and execute_claim functions, which are directly related to the staking and reward claiming processes, similar to the enhancements made in the main PR.
  • feat: Restake & Redelegate for Validator Staking #622: This PR introduces Restake and Redelegate functionalities, which are closely aligned with the updates in the main PR regarding the execute_claim function and the handling of rewards, particularly with the new restake parameter.

Suggested labels

backport/v1.1.x

Suggested reviewers

  • crnbarr93
  • cowboy0015

Poem

🐇 In the fields where bunnies play,
Staking rewards come out to sway.
With permissions set, the restake's near,
No more claims from those we fear.
Hops of joy, the contract sings,
For every change, a new hope springs! 🌼


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

🧹 Outside diff range and nitpick comments (5)
contracts/finance/andromeda-validator-staking/src/mock.rs (2)

45-53: Consider documenting valid action strings

The implementation looks good, but since the PR mentions that "restake" is hardcoded, it would be helpful to document this.

Add a doc comment to clarify valid action strings:

+    /// Executes a permission action
+    /// Currently supported actions:
+    /// - "restake": Allows permitted actors to perform restaking

Line range hint 33-189: Consider future extensibility of permission system

The PR mentions potential future work to extend permissions to the Claim action. The current implementation provides a good foundation for this extension through the generic action string parameter.

However, to make the permission system more maintainable and type-safe, consider:

  1. Using an enum for action types instead of strings
  2. Adding a permission registry to track and validate supported actions
contracts/finance/andromeda-validator-staking/src/contract.rs (3)

298-301: Avoid unnecessary mutable reference to deps

It appears that deps is not modified in the execute_claim function. Consider passing deps as an immutable reference to avoid unnecessary mutability.

Apply this diff:

-        mut deps,
+        deps,

310-310: Remove unnecessary clone of env

Cloning env is unnecessary when accessing the contract address. Access env.contract.address directly to improve efficiency.

Apply this diff:

-        let delegator = env.clone().contract.address;
+        let delegator = env.contract.address;

346-351: Consider using immutable deps reference in is_permissioned

If is_permissioned does not require a mutable deps reference, consider passing deps.as_ref() instead of deps.branch() to avoid unnecessary cloning.

Apply this diff:

-            ADOContract::default().is_permissioned(
-                deps.branch(),
+            ADOContract::default().is_permissioned(
+                deps.as_ref(),
                 env,
                 RESTAKING_ACTION,
                 info.sender,
             )?;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ec058e3 and 6a8fbb3.

📒 Files selected for processing (5)
  • contracts/finance/andromeda-validator-staking/src/contract.rs (5 hunks)
  • contracts/finance/andromeda-validator-staking/src/mock.rs (3 hunks)
  • contracts/finance/andromeda-validator-staking/src/testing/tests.rs (0 hunks)
  • packages/andromeda-finance/src/validator_staking.rs (1 hunks)
  • tests-integration/tests/validator_staking.rs (5 hunks)
💤 Files with no reviewable changes (1)
  • contracts/finance/andromeda-validator-staking/src/testing/tests.rs
✅ Files skipped from review due to trivial changes (1)
  • packages/andromeda-finance/src/validator_staking.rs
🔇 Additional comments (10)
contracts/finance/andromeda-validator-staking/src/mock.rs (2)

12-16: LGTM: Required imports for permission functionality

The new imports appropriately bring in the necessary types for implementing the permission system.


33-43: LGTM: Permission setting implementation

The method correctly implements permission setting functionality. However, let's verify the action string handling.

✅ Verification successful

Implementation verified - Action string handling is appropriate

The execute_set_permission method correctly handles action strings by:

  • Using the standard permissioning message structure from the ADO contract base
  • Properly passing through the action string to the underlying permissioning system
  • Following the established pattern where actions are validated at runtime by the permissioning module
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the hardcoded "restake" action string usage
# as mentioned in PR objectives

# Search for hardcoded "restake" action strings
rg -l "\"restake\"" --type rust

# Search for any constants or enums defining action names
ast-grep --pattern 'const $CONST: &str = "restake"'

Length of output: 437


Script:

#!/bin/bash
# Let's try a broader search for action strings and permission handling

# Search for action string patterns in the codebase
rg -l "action.*String" --type rust

# Search for permission-related code
rg -l "Permission" --type rust

# Look for test files that might validate action strings
fd -e rs -e toml . | xargs rg -l "permission|action" 

Length of output: 10114


Script:

#!/bin/bash
# Let's examine the permission and action handling in the validator staking contract

# Check the validator staking contract implementation
rg -A 5 "action: String" contracts/finance/andromeda-validator-staking/src/contract.rs

# Look for action string validation or constants
rg -A 5 "pub const.*ACTION" contracts/finance/andromeda-validator-staking/src/

# Check test cases for permission actions
rg -A 10 "execute_set_permission" contracts/finance/andromeda-validator-staking/src/testing/tests.rs

Length of output: 270


Script:

#!/bin/bash
# Let's check the base implementation and common actions

# Check the base permissioning implementation
rg -A 5 "action: String" packages/std/src/ado_contract/permissioning.rs

# Look for common action definitions
rg -A 5 "pub const" packages/std/src/common/actions.rs

# Check the mock implementation context
rg -A 10 "mock_set_permission" contracts/finance/andromeda-validator-staking/src/mock.rs

Length of output: 1318

contracts/finance/andromeda-validator-staking/src/contract.rs (2)

13-13: Importing RESTAKING_ACTION constant

The addition of RESTAKING_ACTION is appropriate for the new restaking functionality.


361-365: Verify the necessity of the ownership check

The ownership check may be redundant if permissions are already enforced earlier. Verify whether this check is necessary or if it can be consolidated to improve code clarity.

To confirm the necessity of this check, review the authorization logic for potential redundancy.

tests-integration/tests/validator_staking.rs (6)

6-6: Use of AndrAddr for Address Handling

The addition of use andromeda_std::amp::AndrAddr; is appropriate for managing addresses within the module, ensuring consistency with the Andromeda standard.


24-27: Adding 'other' Wallet for Unauthorized Access Testing

Including the wallet "other" enhances the test's ability to verify unauthorized access scenarios, specifically when attempting to claim rewards without proper permissions.


89-96: Testing Unauthorized Claim Correctly

The implementation correctly tests that an unauthorized wallet ("other") receives a ContractError::Unauthorized when attempting to claim rewards. This ensures that access control is properly enforced.


182-186: Adding Wallets for Permissioned Action Testing

Introducing "permissioned_actor" and "random_actor" wallets allows for comprehensive testing of permissioned actions versus unauthorized attempts, improving the robustness of the test suite.


243-248: Updating execute_claim_reward with Restake Option

The addition of the Some(true) parameter enables the restake functionality within the execute_claim_reward method. Ensure that all calls to this function are updated accordingly and that the restake logic is properly handled.


266-309: Comprehensive Testing of Permissioned Restake Functionality

The test effectively verifies that only permissioned actors can perform the restake action:

  • Permissions are set using execute_set_permission for the "permissioned_actor".
  • An unauthorized attempt by "random_actor" correctly results in a ContractError::Unauthorized.
  • The "permissioned_actor" successfully performs the execute_claim_reward with restake.

This enhances the test coverage and confirms that permission checks are functioning as expected.

@joemonem joemonem added the ci: skip-changelog Skips the changelog check in CI label Nov 19, 2024
@joemonem joemonem merged commit 0453364 into development Nov 19, 2024
10 of 11 checks passed
@joemonem joemonem deleted the restake-permission branch November 19, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci: skip-changelog Skips the changelog check in CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants