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

[TODOs] add EventApplicationOverserviced event #669

Merged
merged 12 commits into from
Jul 17, 2024

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Jul 9, 2024

Summary

Add EventApplicationOverserviced when application stake is less than the settlement amount so that we can monitor occurrences of this scenario and compute the effective inflation.

Issue

While the TODO asks to "unstake" the application, such an action isn't precisely defined. When using the CLI, "unstaking" has the effect of moving staked tokens to the application address and then removing the application from the state. In the current context, there is no stake to move, leaving "unstake" to mean to remove the application from the state store.

Doing this automatically as the result of an action other than explicitly unstaking seems likely to me to introduce confusion. For example, imagine an application querying the network to check their staked balance. If we remove the application at the end of a session where the application was overserviced, such queries would return an app not found error. This also has the effect of wiping out information about the application's on-chain service configuration, which may be useful as an intent signal.

Once #612 is implemented, unintentional application overservicing will be mitigated. An attack vector remains which is not trivially addressed; "unstaking" overserviced application has a negligible impact on this while coming with the aforementioned trade-offs.

Type of change

Select one or more:

  • New feature, functionality or library
  • Bug fix
  • Code health or cleanup
  • Documentation
  • Other (specify)

Testing

Documentation changes (only if making doc changes)

  • make docusaurus_start; only needed if you make doc changes

Local Testing (only if making code changes)

  • Unit Tests: make go_develop_and_test
  • LocalNet E2E Tests: make test_e2e
  • See quickstart guide for instructions

PR Testing (only if making code changes)

  • DevNet E2E Tests: Add the devnet-test-e2e label to the PR.
    • THIS IS VERY EXPENSIVE, so only do it after all the reviews are complete.
    • Optionally run make trigger_ci if you want to re-trigger tests without any code changes
    • If tests fail, try re-running failed tests only using the GitHub UI as shown here

Sanity Checklist

  • I have tested my changes using the available tooling
  • I have commented my code
  • I have performed a self-review of my own code; both comments & source code
  • I create and reference any new tickets, if applicable
  • I have left TODOs throughout the codebase, if applicable

Summary by CodeRabbit

  • New Features

    • Introduced EventApplicationOverserviced message to track application over-service events, including expected and effective burns.
  • Refactor

    • Updated package imports and type declarations to align with the new naming conventions for better code readability and organization.
    • Modified error return statements for improved error handling consistency.
  • Tests

    • Added event attribute validation and coin unmarshalling to the session accounting tests for enhanced test coverage.

@bryanchriswhite bryanchriswhite added on-chain On-chain business logic code health Cleans up some code labels Jul 9, 2024
@bryanchriswhite bryanchriswhite self-assigned this Jul 9, 2024
Copy link

coderabbitai bot commented Jul 9, 2024

Walkthrough

The changes introduce a new EventApplicationOverserviced message in the poktroll.tokenomics package and refactor imports and type declarations in the settle_session_accounting.go file to use renamed packages from the Cosmos SDK. Additionally, test cases in settle_session_accounting_test.go are updated to validate event attributes and coin unmarshalling.

Changes

File Path Change Summary
proto/poktroll/tokenomics/event.proto Added import for cosmos/base/v1beta1/coin.proto and new EventApplicationOverserviced message.
x/tokenomics/keeper/settle_session_accounting.go Renamed imported packages, updated function calls and type declarations, modified error return statements.
x/tokenomics/keeper/settle_session_accounting_test.go Added JSON package import, introduced event attribute checks, and coin unmarshalling in test functions.

Poem

In code's vast garden, where functions grow,
A new event begins to show. 🌱
Tokens burn in service, neat,
Changes swift, the code complete. 🐇
Errors named with care and grace,
A brighter path for us to trace. ✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@bryanchriswhite bryanchriswhite marked this pull request as ready for review July 9, 2024 14:47
@bryanchriswhite bryanchriswhite linked an issue Jul 9, 2024 that may be closed by this pull request
15 tasks
Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bdbc365 and ae5036c.

Files ignored due to path filters (1)
  • x/tokenomics/types/event.pb.go is excluded by !**/*.pb.go
Files selected for processing (4)
  • api/poktroll/tokenomics/event.pulsar.go (6 hunks)
  • proto/poktroll/tokenomics/event.proto (2 hunks)
  • x/tokenomics/keeper/settle_session_accounting.go (10 hunks)
  • x/tokenomics/types/errors.go (1 hunks)
Files not summarized due to errors (1)
  • api/poktroll/tokenomics/event.pulsar.go: Error: Message exceeds token limit
Additional comments not posted (21)
proto/poktroll/tokenomics/event.proto (2)

7-7: Import Addition: Ensure the necessity and usage.

The import for cosmos/base/v1beta1/coin.proto seems necessary for the new message. Verify that all fields using this import are correctly defined.


39-45: New Message: EventApplicationOverserviced

The new message EventApplicationOverserviced is correctly defined and includes fields for application_addr, expected_burn, and effective_burn. Ensure that all new fields are correctly populated and used in the codebase.

x/tokenomics/types/errors.go (1)

28-28: New Error: ErrTokenomicsApplicationOverserviced

The new error ErrTokenomicsApplicationOverserviced is correctly defined with code 1121. Ensure that this error is properly used in the codebase.

x/tokenomics/keeper/settle_session_accounting.go (16)

7-8: Import Alias Renaming: Ensure clarity and consistency.

The import alias renaming from sdk to cosmostypes and tokenomics/types to tokenomicstypes improves clarity. Ensure that all references are updated accordingly.


32-32: Initialization of settlementAmt: Ensure correctness.

The initialization of settlementAmt using cosmostypes.NewCoin is appropriate. Ensure that this variable is correctly used throughout the function.


44-44: Error Handling: Nil Claim

The error handling for a nil claim is appropriate. Ensure that this error is correctly propagated and handled by the caller.


51-51: Error Handling: Nil Session Header

The error handling for a nil session header is appropriate. Ensure that this error is correctly propagated and handled by the caller.


55-55: Error Handling: Invalid Session Header

The error handling for an invalid session header is appropriate. Ensure that this error is correctly propagated and handled by the caller.


58-60: Error Handling: Invalid Supplier Address

The error handling for an invalid supplier address is appropriate. Ensure that this error is correctly propagated and handled by the caller.


63-65: Error Handling: Invalid Application Address

The error handling for an invalid application address is appropriate. Ensure that this error is correctly propagated and handled by the caller.


75-75: Error Handling: Invalid Root Hash

The error handling for an invalid root hash is appropriate. Ensure that this error is correctly propagated and handled by the caller.


93-93: Error Handling: Application Not Found

The error handling for an application not found is appropriate. Ensure that this error is correctly propagated and handled by the caller.


100-100: Initialization of settlementAmtuPOKT: Ensure correctness.

The initialization of settlementAmtuPOKT using cosmostypes.NewCoins is appropriate. Ensure that this variable is correctly used throughout the function.


119-119: Error Handling: Mint Coins Failure

The error handling for a failure to mint coins is appropriate. Ensure that this error is correctly propagated and handled by the caller.


132-132: Error Handling: Send Coins Failure

The error handling for a failure to send coins is appropriate. Ensure that this error is correctly propagated and handled by the caller.


Line range hint 142-168:
New Logic: Handle Application Overserviced

The new logic to handle the case when an application is overserviced is appropriate. Ensure that the EventApplicationOverserviced event is correctly emitted and the error is correctly propagated and handled by the caller.


176-176: Error Handling: Burn Coins Failure

The error handling for a failure to burn coins is appropriate. Ensure that this error is correctly propagated and handled by the caller.


183-183: Error Handling: Negative Application Stake

The error handling for a negative application stake is appropriate. Ensure that this error is correctly propagated and handled by the caller.


193-198: Function Signature Change: Ensure Correctness

The function signature change of getCoinFromComputeUnits to return cosmostypes.Coin is appropriate. Ensure that this change is correctly reflected in all function calls.

api/poktroll/tokenomics/event.pulsar.go (2)

5-5: Import looks good!

The new import statement for cosmossdk.io/api/cosmos/base/v1beta1 is necessary for the new EventApplicationOverserviced message.


2547-2557: New message EventApplicationOverserviced looks good!

The new message EventApplicationOverserviced with fields application_addr, expected_burn, and effective_burn is correctly defined.

@bryanchriswhite bryanchriswhite added push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 10, 2024
Copy link

The image is going to be pushed after the next commit.

You can use make trigger_ci to push an empty commit.

If you also want to run E2E tests, please add devnet-test-e2e label.

Copy link

The CI will now also run the e2e tests on devnet, which increases the time it takes to complete all CI checks.

You may need to run make trigger_ci to submit an empty commit that'll trigger the tests.

GCP workloads (requires changing the namespace to 669)
Grafana network dashboard for devnet-issue-{issue-id}

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ae5036c and 70caf94.

Files selected for processing (1)
  • x/tokenomics/keeper/settle_session_accounting.go (10 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/tokenomics/keeper/settle_session_accounting.go

…ation-overserviced

* pokt/main:
  [TODOs] refactor: proof path calculation (#659)
  [Dependencies] bump go-getter and ibc-go (#691)
  [Relayminer] refactor: `relayerSessionsManager#waitForBlock()` (#648)
  [Observables] chore: add `ReplayObservable#SubscribeFromLatestBufferedOffset()` (#647)
  [Observability] Add claim relays counter (#644)
  [Code Health] chore: log unused error when updating relay mining difficulty (#683)
  [Testing] chore: uncomment proof CLI query tests (#668)
  build(deps): bump ws from 7.5.9 to 7.5.10 in /docusaurus (#686)
  build(deps): bump webpack-dev-middleware from 5.3.3 to 5.3.4 in /docusaurus (#688)
  build(deps): bump express from 4.18.2 to 4.19.2 in /docusaurus (#687)
  build(deps): bump follow-redirects from 1.15.3 to 1.15.6 in /docusaurus (#685)
  build(deps): bump braces from 3.0.2 to 3.0.3 in /docusaurus (#689)
  [CosmosSDK] Bump to v0.50.7 (#682)
Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 70caf94 and 5dc5771.

Files selected for processing (1)
  • x/tokenomics/keeper/settle_session_accounting.go (10 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/tokenomics/keeper/settle_session_accounting.go

@bryanchriswhite bryanchriswhite removed push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 15, 2024
@github-actions github-actions bot removed the devnet label Jul 15, 2024
…ation-overserviced

* pokt/main:
  [TODO] Blockers @okdas (#674)
  [TODO_BLOCKER] chore: Remove stale TODO_BLOCKERs (#694)
  [TODO] refactor: query expiring claims w/ index (#671)
  [TODOs] `grace_period_end_offset_blocks` validation & cleanup (#667)
  [Testing] add account balance coverage to account settlement (#670)
  [Application] Auto-undelegation of unstaked gateways (#692)
  [RelayMiner] Fix LeanClient mutiple suppliers support (#662)
@bryanchriswhite bryanchriswhite added push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 16, 2024
Copy link

The CI will now also run the e2e tests on devnet, which increases the time it takes to complete all CI checks.

You may need to run make trigger_ci to submit an empty commit that'll trigger the tests.

GCP workloads (requires changing the namespace to 669)
Grafana network dashboard for devnet-issue-{issue-id}

Copy link

The image is going to be pushed after the next commit.

You can use make trigger_ci to push an empty commit.

If you also want to run E2E tests, please add devnet-test-e2e label.

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5dc5771 and 1abe74d.

Files selected for processing (3)
  • api/poktroll/tokenomics/event.pulsar.go (6 hunks)
  • x/tokenomics/keeper/settle_session_accounting.go (6 hunks)
  • x/tokenomics/keeper/settle_session_accounting_test.go (2 hunks)
Files not summarized due to errors (1)
  • api/poktroll/tokenomics/event.pulsar.go: Error: Message exceeds token limit
Files skipped from review as they are similar to previous changes (1)
  • x/tokenomics/keeper/settle_session_accounting.go
Additional comments not posted (16)
x/tokenomics/keeper/settle_session_accounting_test.go (2)

10-10: Added JSON package import.

The addition of the json package is presumably for handling JSON operations in the tests, particularly for unmarshalling event attributes. Ensure that this package is used appropriately and securely, especially in handling untrusted input, although in a test environment this is usually controlled.


256-273: Review of event attribute checks and coin unmarshalling in TestSettleSessionAccounting_AppStakeTooLow.

  1. Context Unwrapping: Line 256 unwraps the SDK context correctly which is a common pattern in Cosmos SDK tests.

  2. Event Retrieval: Lines 257-261 handle the retrieval of events and their attributes. It's important to validate that these attributes are expected to be present in the events to avoid potential runtime errors from missing attributes.

  3. Attribute Unmarshalling: Lines 267-270 handle the unmarshalling of the expectedBurn and effectiveBurn attributes into Coin types. This is crucial for ensuring that the events contain valid and correctly formatted data. The use of JSON for serialization/deserialization in events is standard, but ensure that the data format matches the expected schema.

  4. Assertions: Lines 263, 272-273 use assertions to check the lengths of attributes and compare the coins. These assertions are well-placed to ensure the integrity of the test conditions.

Overall, the changes are well-integrated and appear to follow best practices for handling events in Cosmos SDK tests. However, ensure that the event attributes are always set as expected to avoid potential issues during runtime.

Would you like to add more robust error handling or fallbacks for missing event attributes?

Verification successful

Event attribute checks and coin unmarshalling in TestSettleSessionAccounting_AppStakeTooLow are valid.

The attributes application_addr, expected_burn, and effective_burn are defined and used correctly in the codebase, particularly in the EventApplicationOverserviced event. This confirms that the attributes are set during the operations leading to the test, addressing the concern about potential runtime errors from missing attributes.

  • x/tokenomics/types/event.pb.go: Attributes are defined in the EventApplicationOverserviced event.
  • x/tokenomics/keeper/settle_session_accounting_test.go: Attributes are used in the test.
Scripts executed

The following scripts were executed for the analysis:

Script:

   # Check if the event attributes are always set during the operations leading to this test.
   rg --type go -A 5 $'events.EmitEvent'
   

Length of output: 36


Script:

#!/bin/bash
# Searching for event emission patterns in the codebase
rg --type go 'EventManager().EmitEvent' -A 5

Length of output: 46


Script:

#!/bin/bash
# Searching for any event emission methods in the codebase
rg --type go 'Emit' -A 5

Length of output: 11197


Script:

#!/bin/bash
# Searching for specific event attributes in the codebase
rg --type go 'application_addr' -A 5
rg --type go 'expected_burn' -A 5
rg --type go 'effective_burn' -A 5

Length of output: 38140

api/poktroll/tokenomics/event.pulsar.go (14)

1772-1785: Initialization of EventApplicationOverserviced

The initialization function for EventApplicationOverserviced is correctly setting up the message descriptor and field descriptors. Ensure that the message and field names match the protobuf definitions exactly.


1787-1787: Review of ProtoMessage and Interface methods

The ProtoMessage and Interface methods are standard for protobuf message types in Go. They are implemented correctly here.

Also applies to: 1840-1844


1846-1870: Field Iteration Logic in Range Method

The Range method iterates over fields correctly and uses the provided function to handle each field. This is a standard pattern in Go protobuf implementations.


1872-1897: Field Presence Check in Has Method

The Has method correctly checks for field presence based on the field's full name. It handles nullable and non-nullable fields appropriately, which is crucial for correct serialization and deserialization behavior.


1899-1919: Clear Method Implementation

The Clear method is implemented to reset fields to their zero values. This method is crucial for reusing or resetting protobuf messages in long-lived applications to avoid stale data.


1921-1944: Field Retrieval in Get Method

The Get method is implemented to retrieve field values safely. It handles both populated and unpopulated fields correctly, returning a default value for unpopulated fields. This is a critical method for robust data handling in applications.


1946-1970: Field Setting in Set Method

The Set method correctly assigns values to fields, including handling oneof fields and extensions. It's implemented to ensure thread safety, which is essential in concurrent environments.


1972-2002: Mutable Reference Retrieval in Mutable Method

The Mutable method is correctly implemented to provide mutable references to composite fields. It ensures that a new value is allocated if the field is unpopulated, which is crucial for modifying repeated fields or nested messages.


2004-2023: Field Creation in NewField Method

The NewField method is implemented to create new instances of fields, ensuring that the correct type is instantiated for complex fields like messages. This method is essential for dynamic message construction.


2025-2034: Oneof Handling in WhichOneof Method

The WhichOneof method is implemented to identify which field in a oneof group is populated. This method is crucial for correctly interpreting messages that use oneof to represent optional or mutually exclusive fields.


2036-2052: Unknown Field Handling in SetUnknown Method

The SetUnknown method is implemented to manage unknown fields in a message, allowing for forward compatibility by preserving unknown fields during deserialization. This is a best practice in protocol buffer implementations.


2054-2062: Message Validity Check in IsValid Method

The IsValid method is implemented to check if the message is a valid, non-nil instance. This is crucial for preventing runtime errors when working with potentially nil messages.


2066-2348: Protobuf Method Implementations in ProtoMethods

The ProtoMethods provides custom implementations for serialization, deserialization, and size calculation. These are optimized for performance and correctness, ensuring that the message handles data efficiently and correctly across different protobuf versions.


5-5: Ensure correct import for v1beta1

The import alias v1beta1 for cosmossdk.io/api/cosmos/base/v1beta1 is used in the file. Confirm that this is the correct version and alias as per project standards.

Verification successful

Verified: Correct import alias for v1beta1

The import alias v1beta1 for cosmossdk.io/api/cosmos/base/v1beta1 is used consistently across multiple files in the project, indicating that it is the standard alias. Therefore, the import in event.pulsar.go is correct.

  • api/poktroll/tokenomics/event.pulsar.go line 5
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the correct version and alias are used for the cosmos SDK.
grep -r "cosmossdk.io/api/cosmos/base/v1beta1" .

Length of output: 1170

Copy link
Contributor

@red-0ne red-0ne left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -29,40 +29,40 @@ func (k Keeper) SettleSessionAccounting(
) error {
logger := k.Logger().With("method", "SettleSessionAccounting")

settlementAmt := sdk.NewCoin("upokt", math.NewInt(0))
settlementCoin := cosmostypes.NewCoin("upokt", math.NewInt(0))
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on pluralizing this to Coins?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

settlementCoins is used elsewhere and is a []cosmostypes.Coin type.

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1abe74d and 2d32d58.

Files selected for processing (1)
  • x/tokenomics/keeper/settle_session_accounting.go (6 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/tokenomics/keeper/settle_session_accounting.go

@bryanchriswhite bryanchriswhite merged commit d8b4218 into main Jul 17, 2024
10 checks passed
@bryanchriswhite bryanchriswhite deleted the issues/584/event/application-overserviced branch July 17, 2024 11:20
bryanchriswhite added a commit that referenced this pull request Jul 17, 2024
…-root

* pokt/main:
  [TODOs] add `EventApplicationOverserviced` event (#669)
  [Supplier] Deny supplier staking with unknown services (#693)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code health Cleans up some code devnet devnet-test-e2e on-chain On-chain business logic push-image CI related - pushes images to ghcr.io
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

[TODO] @Olshansk's (and now @Bryan's) Blocker TODOs
3 participants