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

fix(baseapp): align block header when query with latest height #21003

Merged
merged 10 commits into from
Oct 7, 2024

Conversation

mmsqe
Copy link
Contributor

@mmsqe mmsqe commented Jul 21, 2024

  • lastBlockHeight bump when commit before header change in checkState
  • use finalize state block header when CreateQueryContext with latest height and checkState block header height is fall behind

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced the x/validate module for transaction validation and handler management.
    • Added a new Linux-only backend for the crypto/keyring module.
    • Enhanced simulation framework with a new sims2 framework and simplified message factories.
  • Bug Fixes

    • Improved handling of validator sign status in simulations.
    • Updated error handling in the BaseApp.Init method and types/mempool interface.
  • Tests

    • Added new tests for query context creation and validation of block heights.
    • Enhanced testing framework with a new mock ABCI listener for commit events.

use finalize state block header when CreateQueryContext
with latest height and checkState block header height is fall behind
@mmsqe mmsqe requested a review from a team as a code owner July 21, 2024 05:43
Copy link
Contributor

coderabbitai bot commented Jul 21, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

This update introduces significant enhancements to the Cosmos SDK, particularly with the addition of the x/validate module for transaction validation and improved handling of ante and post handlers. The baseapp module has been updated to refine query context creation, including better block header validation. Furthermore, various bug fixes and optimizations have been implemented across modules to enhance overall functionality and reliability.

Changes

Files Change Summary
CHANGELOG.md Documented new features, bug fixes, and structural changes across the SDK, including the x/validate module.
baseapp/abci.go Added CreateQueryContextWithCheckHeader method for improved query context creation and header validation. Updated existing methods for better error handling.
baseapp/baseapp_test.go Implemented mockABCIListener for testing; added tests for new query context methods and refined existing test cases.
x/auth/tx/config Updated method signatures for transaction configuration handling to include error returns.
x/validate New module introduced for transaction validation, including updated method signatures for validation and handler management.
types/mempool Updated interface to accept decoded transactions, preventing double decoding issues.

Sequence Diagram(s)

sequenceDiagram
    participant App as BaseApp
    participant Validator as Transaction Validator
    participant Context as Query Context

    App->>Validator: Validate transaction
    Validator-->>App: Return validation result
    alt Valid transaction
        App->>Context: Create query context
    else Invalid transaction
        App-->>Context: Return error
    end
Loading

Possibly related PRs

Suggested labels

C:server/v2, Type: CI, backport/v0.52.x

Suggested reviewers

  • kocubinski
  • sontrinh16
  • facundomedica
  • auricom
  • testinginprod
  • julienrbrt

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.

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 748352e and 185e49d.

Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci.go (1 hunks)
  • baseapp/baseapp_test.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
Additional context used
Path-based instructions (2)
baseapp/baseapp_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (5)
baseapp/baseapp_test.go (4)

691-693: LGTM!

The mockABCIListener struct is correctly implemented for testing purposes.


695-697: LGTM!

The ListenFinalizeBlock method is correctly implemented to return nil.


699-701: LGTM!

The ListenCommit method correctly uses the provided ListenCommitFn function to handle commit responses.


744-786: LGTM!

The test function TestABCI_CreateQueryContext_Before_Set_CheckState is correctly implemented to validate the behavior of the CreateQueryContext method.

baseapp/abci.go (1)

1248-1269: LGTM!

The updated logic in the CreateQueryContext method enhances the robustness of the context creation by ensuring that it only proceeds with a valid block header.

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 185e49d and f2d145f.

Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci.go (3 hunks)
  • baseapp/baseapp_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • baseapp/baseapp_test.go
Additional context used
Path-based instructions (1)
baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (10)
baseapp/abci.go (10)

1203-1204: Refactor: Delegate to CreateQueryContextWithCheckHeader.

The CreateQueryContext function now delegates to CreateQueryContextWithCheckHeader with an additional checkHeader argument set to true. This refactor is correct and aligns with the new logic introduced.


1206-1208: Function Documentation: Ensure Clarity.

The function documentation clearly describes the purpose and parameters of CreateQueryContextWithCheckHeader. This is a good practice.


Line range hint 1209-1213:
Error Handling: Check for Negative Height.

The function correctly checks for a negative height and returns an appropriate error. This is a good practice for input validation.


Line range hint 1214-1216:
Use Custom Query Multi-Store if Provided.

The function uses a custom query multi-store if provided, otherwise defaults to the commit multi-store. This adds flexibility to the function.


Line range hint 1217-1224:
Error Handling: Validate Height Against Latest Version.

The function validates the provided height against the latest version of the multi-store, ensuring that queries are not made for future heights. This is a good practice.


Line range hint 1225-1231:
Error Handling: Validate Proof for Heights <= 1.

The function correctly handles the case where proof is requested for heights less than or equal to 1, which is invalid.


Line range hint 1232-1240:
Cache Multi-Store with Version: Error Handling.

The function attempts to cache the multi-store with the specified version and handles errors appropriately. This ensures that the state at the specified height is correctly loaded.


1254-1267: Header Validation: Loop Through States.

The function loops through the available states (checkState and finalizeBlockState) to find a valid block header. This ensures that the correct header is used for the query context.


1269-1275: Error Handling: No Valid Header Found.

The function returns an error if no valid header is found in the available states. This ensures that the query context is only created with a valid header.


1276-1284: Create Query Context: Set Context Parameters.

The function sets various parameters in the context, including gas meter, header info, and block height. This ensures that the context is correctly configured for the query.

@facundomedica
Copy link
Member

Can you expand a bit more on why this is needed and if there were issues caused by this? Thank you!

@mmsqe
Copy link
Contributor Author

mmsqe commented Jul 23, 2024

Can you expand a bit more on why this is needed and if there were issues caused by this? Thank you!

I was trying to use block timestamp as random seed for smart contract. Currently a query result of new block height may be returned with the previous block timestamp if the block is committed but state not being set.@facundomedica you can see the case in TestABCI_CreateQueryContext_Before_Set_CheckState.

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

Outside diff range, codebase verification and nitpick comments (1)
baseapp/abci.go (1)

1203-1204: Update all instances of CreateQueryContext to use CreateQueryContextWithCheckHeader

The following instances of CreateQueryContext need to be updated to use the new CreateQueryContextWithCheckHeader function:

  • baseapp/baseapp_test.go (multiple occurrences)
  • baseapp/abci.go (multiple occurrences)
  • baseapp/grpcserver.go

Please ensure these updates are made to maintain consistency and avoid potential errors.

Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to CreateQueryContext are correctly updated to use the new CreateQueryContextWithCheckHeader function.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContext` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'CreateQueryContext'

Length of output: 4019

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f2d145f and 2e7cac3.

Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci.go (5 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
Additional context used
Path-based instructions (1)
baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (1)
baseapp/abci.go (1)

Line range hint 1208-1282:
LGTM! Robust error handling and context management.

The code changes are approved.

However, ensure that all function calls to CreateQueryContextWithCheckHeader are correctly updated and tested in the codebase.

Verification successful

Function calls to CreateQueryContextWithCheckHeader have been correctly updated.

The function calls in baseapp/abci.go and baseapp/baseapp_test.go match the new signature, ensuring consistency across the codebase.

  • baseapp/abci.go
  • baseapp/baseapp_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContextWithCheckHeader` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'CreateQueryContextWithCheckHeader'

Length of output: 1744

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2e7cac3 and 3ab599d.

Files selected for processing (1)
  • baseapp/baseapp_test.go (3 hunks)
Additional context used
Path-based instructions (1)
baseapp/baseapp_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Additional comments not posted (6)
baseapp/baseapp_test.go (6)

691-693: LGTM! The mock listener is well-defined.

The mockABCIListener struct and its ListenCommitFn function pointer are correctly implemented for mocking purposes.


695-697: LGTM! The ListenFinalizeBlock method is correctly implemented.

The method correctly returns nil, as expected for a mock implementation.


699-701: LGTM! The ListenCommit method is correctly implemented.

The method correctly delegates to the ListenCommitFn function pointer.


Line range hint 709-742:
LGTM! The test cases are comprehensive and well-structured.

The function TestABCI_CreateQueryContext covers various scenarios for creating query contexts, ensuring robust error handling.


744-773: LGTM! The test cases for header checks are well-structured.

The function TestABCI_CreateQueryContextWithCheckHeader covers scenarios with and without header checks, ensuring correct behavior.


775-817: LGTM! The test case ensures correct integration and query context creation.

The function TestABCI_CreateQueryContext_Before_Set_CheckState ensures that the query context is created successfully and the block height matches expectations after the commit phase.

@facundomedica
Copy link
Member

Hey I tried TestABCI_CreateQueryContext_Before_Set_CheckState in the current main, but it doesn't fail. I want to replicate first in order to try moving stuff around to try to simplify it.

baseapp/abci.go Outdated
Comment on lines 1270 to 1280
defer func() {
if err == nil {
rms, ok := app.cms.(*rootmulti.Store)
if ok {
cInfo, err := rms.GetCommitInfo(height)
if cInfo != nil && err == nil {
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height, Time: cInfo.Timestamp})
}
}
}
}()
Copy link
Member

Choose a reason for hiding this comment

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

why is this defer necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just replaced it with isLatest to avoid confusion

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3ab599d and 246e258.

Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci.go (4 hunks)
  • baseapp/baseapp_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • baseapp/baseapp_test.go
Additional context used
Path-based instructions (1)
baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (8)
baseapp/abci.go (8)

Line range hint 142-146:
Ensure the correctness of the new function usage.

The Info function now uses CreateQueryContextWithCheckHeader. Verify that this change aligns with the intended logic and that the checkHeader parameter is correctly set to false.


1203-1204: Delegation to new function looks good.

The CreateQueryContext function correctly delegates to CreateQueryContextWithCheckHeader with the checkHeader parameter set to true.


1208-1209: Check for negative height.

The function starts by checking for negative height values, which is a good practice to ensure valid input.


1240-1244: Iterate over state contexts to determine the appropriate header.

The loop iterates over various state contexts to determine the appropriate header. This enhances the robustness of the context creation process.


1258-1264: Handle missing header scenario.

The function handles the scenario where the header is not found in any state context, returning an explicit error. This improves error handling.


1270-1280: Deferred function for setting header info.

The deferred function ensures that the header info is set correctly after the main logic executes, which is a good practice for maintaining context integrity.


1292-1299: Create new context with appropriate settings.

The function creates a new context with the appropriate settings, including gas meter and header info. This ensures that the context is correctly initialized for queries.


Line range hint 1197-1199:
Verify the function usage.

The handleQueryGRPC function calls CreateQueryContext, which now delegates to CreateQueryContextWithCheckHeader. Ensure that this change aligns with the intended logic.

@mmsqe
Copy link
Contributor Author

mmsqe commented Aug 1, 2024

Hey I tried TestABCI_CreateQueryContext_Before_Set_CheckState in the current main, but it doesn't fail. I want to replicate first in order to try moving stuff around to try to simplify it.

Might need some check in main to reproduce the fail test

diff --git a/baseapp/abci.go b/baseapp/abci.go
index 513d8667fc..2278013f02 100644
--- a/baseapp/abci.go
+++ b/baseapp/abci.go
@@ -1245,6 +1245,15 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
 			)
 	}
 
+	header := app.checkState.Context().BlockHeader()
+	if height == lastBlockHeight && header.Height != lastBlockHeight {
+		return sdk.Context{},
+			errorsmod.Wrapf(
+				sdkerrors.ErrInvalidHeight,
+				"header height in check state context is not latest height (%d)", lastBlockHeight,
+			)
+	}
+
 	// branch the commit multi-store for safety
 	ctx := sdk.NewContext(cacheMS, true, app.logger).
 		WithMinGasPrices(app.minGasPrices).
@@ -1253,7 +1262,7 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
 			ChainID: app.chainID,
 			Height:  height,
 		}).
-		WithBlockHeader(app.checkState.Context().BlockHeader()).
+		WithBlockHeader(header).
 		WithBlockHeight(height)
 
 	if height != lastBlockHeight {

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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 246e258 and d568439.

Files selected for processing (1)
  • baseapp/baseapp_test.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • baseapp/baseapp_test.go

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

Outside diff range, codebase verification and nitpick comments (1)
baseapp/abci.go (1)

1203-1204: Update function calls to match the new signature

The following function calls to CreateQueryContext need to be updated to match the new signature or replaced by CreateQueryContextWithCheckHeader:

  • baseapp/abci.go
    • Line 1203: ctx, err := app.CreateQueryContext(req.Height, req.Prove)
  • baseapp/grpcserver.go
    • Line 15: sdkCtx, err := app.CreateQueryContext(height, false)
  • baseapp/baseapp_test.go
    • Line 220: ctx, err := app.CreateQueryContext(height, tc.prove)
    • Line 303: qCtx, qErr := app.CreateQueryContext(0, true)
    • Line 350: ctx, err := bapp.CreateQueryContext(1, false)

Ensure these calls are updated to use CreateQueryContextWithCheckHeader or the correct parameters are passed to CreateQueryContext.

Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to CreateQueryContext match the new signature.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContext` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'CreateQueryContext'

Length of output: 4006


Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContext` match the new signature or need to be updated.

# Check the lines where `CreateQueryContext` is used in the codebase
rg --type go -C 3 'CreateQueryContext' baseapp/baseapp_test.go baseapp/grpcserver.go baseapp/abci.go

Length of output: 4696

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d568439 and 5e6c8ea.

Files selected for processing (1)
  • baseapp/abci.go (5 hunks)
Additional context used
Path-based instructions (1)
baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (2)
baseapp/abci.go (2)

Line range hint 1206-1295:
LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to CreateQueryContextWithCheckHeader match the new signature.

Verification successful

Function usage verification successful.

All instances of CreateQueryContextWithCheckHeader match the new function signature with three parameters: height, prove, and checkHeader.

  • baseapp/baseapp_test.go
  • baseapp/abci.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContextWithCheckHeader` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'CreateQueryContextWithCheckHeader'

Length of output: 1739


142-144: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to CreateQueryContextWithCheckHeader match the new signature.

Verification successful

Verified: The function CreateQueryContextWithCheckHeader is used consistently with the new signature.

The code changes are correctly implemented, and all function calls match the new signature.

  • baseapp/abci.go
  • baseapp/baseapp_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `CreateQueryContextWithCheckHeader` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'CreateQueryContextWithCheckHeader'

Length of output: 1739

Copy link
Contributor

github-actions bot commented Sep 9, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Sep 9, 2024
@julienrbrt
Copy link
Member

Could you create an issue about this explaining the problem? Then we'll assign people to review this.

@mmsqe
Copy link
Contributor Author

mmsqe commented Sep 9, 2024

Could you create an issue about this explaining the problem? Then we'll assign people to review this.

Just add #21615, see if it's ok.

@julienrbrt julienrbrt removed the Stale label Sep 9, 2024
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: 4

🧹 Outside diff range and nitpick comments (3)
CHANGELOG.md (1)

The changes to UnbondingDelegation in the v0.47.0 release of the Cosmos SDK introduce significant updates to the staking module, particularly in relation to unbonding delegations. Here are the key points:

  1. New functionality: A new message type, MsgCancelUnbondingDelegation, has been introduced. This allows delegators to cancel an ongoing unbonding delegation, which is a significant new feature in the staking module.

  2. Query improvements: There are numerous new or updated query functions related to UnbondingDelegation, such as ValidatorUnbondingDelegations, DelegatorUnbondingDelegations, and UnbondingDelegation. This suggests improved capabilities for querying unbonding delegation information.

  3. Breaking changes: The CHANGELOG.md file explicitly mentions both API Breaking Changes and State Breaking changes. This indicates that there are significant changes that may affect existing implementations and potentially the state of the blockchain.

  4. Potential structural changes: While we couldn't directly observe the UnbondingDelegation structure, the presence of many related functions suggests that there might be changes to how unbonding delegations are handled internally.

Developers working with the staking module, particularly those dealing with unbonding delegations, should carefully review these changes. The new cancellation feature and updated querying capabilities may require updates to existing implementations. Additionally, the mention of breaking changes in the changelog emphasizes the need for thorough testing and potentially migration strategies when upgrading to this version.

🔗 Analysis chain

Line range hint 145-240: Significant API changes require careful migration

The v0.47.0 release introduces numerous API breaking changes that will require careful attention from developers updating their applications. Some key points to note:

  1. Many modules have undergone significant restructuring, including x/auth, x/bank, x/gov, and x/staking.
  2. Several function signatures and type definitions have changed, often to improve consistency or add new functionality.
  3. Some modules have been moved to their own go.mod files, which may affect dependency management.
  4. There are changes to how certain operations are performed, such as signing transactions and handling capabilities.

Developers should thoroughly review these changes and plan for a comprehensive migration process when upgrading to v0.47.0. It's recommended to update applications in a staged manner and thoroughly test all functionality after the upgrade.

To help verify the impact of these changes, you can run the following command to check for usage of deprecated or removed APIs:

This will help identify areas of your codebase that may need updating.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of removed or changed APIs
rg -i -e "RegisterCodec" -e "GetSignBytes" -e "Route" -e "Type" --type go

Length of output: 380853


Script:

#!/bin/bash
# Search for changes in UnbondingDelegation related APIs
grep -R "func.*UnbondingDelegation" x/staking/
grep -R "type.*UnbondingDelegation" x/staking/
grep -R "MsgCancelUnbondingDelegation" x/staking/

Length of output: 50406


Script:

#!/bin/bash
# Compare UnbondingDelegation structure and check for removed APIs
git show v0.46.0:x/staking/types/delegation.go | grep -A 20 "type UnbondingDelegation struct"
echo "---"
cat x/staking/types/delegation.go | grep -A 20 "type UnbondingDelegation struct"
echo "---"
git diff v0.46.0..HEAD x/staking/keeper/keeper.go | grep "func.*Unbonding.*Delegation.*("

Length of output: 532


Script:

#!/bin/bash
# Examine current UnbondingDelegation structure and related functions
echo "UnbondingDelegation structure:"
grep -n -A 20 "type UnbondingDelegation struct" x/staking/types/delegation.go

echo -e "\nUnbondingDelegation related functions:"
grep -n "func.*Unbonding.*Delegation" x/staking/types/*.go

echo -e "\nChecking for breaking change comments:"
grep -n -i "breaking change" x/staking/CHANGELOG.md

Length of output: 24003

baseapp/baseapp_test.go (1)

770-784: Improve test naming for better clarity in subtests

In TestABCI_CreateQueryContextWithCheckHeader, the subtests use the constant name "valid height with different initial height" in t.Run. This can make it difficult to identify which specific test case fails during execution. To enhance clarity, consider adding a name field to your test cases and using it in t.Run, so that each subtest is uniquely identified.

Apply this diff to improve subtest naming:

 testCases := []struct {
-	checkHeader bool
-	expErr      bool
+	name        string
+	checkHeader bool
+	expErr      bool
 }{
-	{true, true},
-	{false, false},
+	{"checkHeader true, expect error", true, true},
+	{"checkHeader false, expect no error", false, false},
 }
 
 for _, tc := range testCases {
-	t.Run("valid height with different initial height", func(t *testing.T) {
+	t.Run(tc.name, func(t *testing.T) {
baseapp/abci.go (1)

1212-1214: Add detailed documentation for the checkHeader parameter.

The new function CreateQueryContextWithCheckHeader introduces a checkHeader boolean parameter. Providing more detailed comments or examples on when to set checkHeader to true or false would improve code readability and help other developers understand the intended usage.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5e6c8ea and c2a099e.

📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci.go (4 hunks)
  • baseapp/baseapp_test.go (4 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

baseapp/abci.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

baseapp/baseapp_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

🔇 Additional comments (9)
CHANGELOG.md (5)

Line range hint 1-39: Impressive list of new features!

The v0.47.0 release introduces several important features that enhance the Cosmos SDK's functionality and developer experience. Some notable additions include:

  1. New grpc-only flag for query-only mode.
  2. Improved transaction simulation and block results querying.
  3. Support for creating permanent locked accounts.
  4. Enhanced governance features, including expedited proposals.
  5. New CLI commands for various modules.

These features demonstrate a strong focus on improving the SDK's capabilities and usability.


Line range hint 41-108: Significant improvements across the board

The v0.47.0 release includes a wide range of improvements that enhance the Cosmos SDK's performance, maintainability, and developer experience. Some notable improvements include:

  1. Optimization of iteration on nested cached KV stores.
  2. Improved error messages and logging throughout the codebase.
  3. Refactoring of various modules to use collections for state management.
  4. Enhanced CLI commands and query capabilities.
  5. Improved genesis validation and export functionality.

These improvements demonstrate a commitment to continually refining and optimizing the Cosmos SDK.


Line range hint 110-143: Critical bug fixes enhance stability

The v0.47.0 release includes several important bug fixes that address various issues across the Cosmos SDK. Some notable fixes include:

  1. Resolving data races in store trace components and mempools.
  2. Fixing panics in various scenarios, such as when parsing telemetry configuration.
  3. Correcting behavior in modules like x/gov, x/bank, and x/staking.
  4. Addressing issues with genesis state handling and migration.

These bug fixes contribute to the overall stability and reliability of the Cosmos SDK, which is crucial for developers building applications on top of it.


Line range hint 1-266: Comprehensive update with significant improvements and breaking changes

The v0.47.0 release of the Cosmos SDK represents a major update with far-reaching implications for developers and network operators. This release introduces numerous new features, performance improvements, and bug fixes that enhance the overall functionality and reliability of the SDK.

However, it's crucial to note the extensive API and state machine breaking changes. These changes will require significant effort to migrate existing applications and coordinate network upgrades. Developers should plan for a thorough review and testing process when upgrading to this version.

Key takeaways:

  1. New features improve functionality across various modules.
  2. Performance optimizations and bug fixes enhance stability.
  3. Extensive API changes require careful code updates.
  4. State machine breaking changes necessitate coordinated network upgrades.

Overall, while the upgrade process may be challenging, the improvements offered by v0.47.0 provide a solid foundation for building more robust and efficient blockchain applications.


Line range hint 242-266: Critical state machine changes require coordinated upgrade

The v0.47.0 release includes several state machine breaking changes that are crucial to understand:

  1. Changes to how various modules store and process state, including x/bank, x/gov, and x/staking.
  2. Updates to consensus parameters and how they are managed.
  3. Modifications to how certain operations, like unbonding in x/staking, are handled.

These changes are critical and require careful planning for a coordinated upgrade across all nodes in a network. Failure to properly handle these changes could result in consensus failures or other serious issues.

Network operators should:

  1. Thoroughly test the upgrade process on a testnet before applying to mainnet.
  2. Ensure all validators and nodes are prepared to upgrade simultaneously.
  3. Have a rollback plan in case of unexpected issues during the upgrade.

To help verify the impact of these changes, you can use the following command to check for any remaining usage of old state keys or deprecated state handling:

This will help identify any areas of your codebase that may need updating to align with the new state machine changes.

baseapp/baseapp_test.go (2)

787-829: Questioning the order of InitChain after block commitments

In TestABCI_CreateQueryContext_Before_Set_CheckState, the call to InitChain is made after finalizing and committing blocks. Typically, InitChain is called before processing any blocks to initialize the application state. Verify whether calling InitChain after committing blocks is intentional and necessary for this test scenario, as it may not reflect the actual initialization sequence in production.


756-785: Unit tests adequately cover new functionality

The newly added test TestABCI_CreateQueryContextWithCheckHeader effectively tests the behavior of CreateQueryContextWithCheckHeader with different checkHeader values. The test ensures that when checkHeader is true, an error is expected, and when false, no error occurs and the block height is correctly set. This enhances the test coverage for the new functionality introduced.

baseapp/abci.go (2)

145-145: Verify the implications of disabling header checks in CreateQueryContextWithCheckHeader.

In the Info method, CreateQueryContextWithCheckHeader is called with checkHeader set to false. Please ensure that bypassing the header check here does not introduce any security vulnerabilities or inconsistent state, especially when retrieving the application version.


1209-1210: Ensure consistent behavior when checkHeader is set to true.

The CreateQueryContext method now calls CreateQueryContextWithCheckHeader with checkHeader set to true. Please verify that this change maintains the intended behavior across all uses of CreateQueryContext, and that header checks are performed correctly.

Comment on lines +703 to +709
func (m mockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
return nil
}

func (m *mockABCIListener) ListenCommit(ctx context.Context, commit abci.CommitResponse, pairs []*storetypes.StoreKVPair) error {
return m.ListenCommitFn(ctx, commit, pairs)
}
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

Inconsistent method receiver types in mockABCIListener methods

The methods ListenFinalizeBlock and ListenCommit of mockABCIListener use different receiver types: ListenFinalizeBlock uses a value receiver (m mockABCIListener), whereas ListenCommit uses a pointer receiver (m *mockABCIListener). According to Go best practices and the Uber Go Style Guide, methods on the same type should have consistent receiver types to avoid confusion and potential bugs. Consider changing ListenFinalizeBlock to use a pointer receiver for consistency.

Apply this diff to fix the inconsistent receiver:

-func (m mockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
+func (m *mockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
📝 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
func (m mockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
return nil
}
func (m *mockABCIListener) ListenCommit(ctx context.Context, commit abci.CommitResponse, pairs []*storetypes.StoreKVPair) error {
return m.ListenCommitFn(ctx, commit, pairs)
}
func (m *mockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
return nil
}
func (m *mockABCIListener) ListenCommit(ctx context.Context, commit abci.CommitResponse, pairs []*storetypes.StoreKVPair) error {
return m.ListenCommitFn(ctx, commit, pairs)
}

baseapp/abci.go Show resolved Hide resolved
Comment on lines +1295 to +1298
WithBlockHeader(*header).
WithBlockHeight(height)

if height != lastBlockHeight {
if !isLatest {
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

Handle errors when retrieving commit info for historical queries.

At lines 1295-1298, when attempting to retrieve the commit info to get the timestamp for historical queries, the code assumes GetCommitInfo succeeds if err == nil and cInfo != nil. However, it's possible for GetCommitInfo to return an error or nil cInfo. Ensure that errors are properly handled, and default values are used if necessary.

Apply this diff to handle potential errors:

 if !isLatest {
     rms, ok := app.cms.(*rootmulti.Store)
     if ok {
         cInfo, err := rms.GetCommitInfo(height)
-        if cInfo != nil && err == nil {
+        if err != nil {
+            return ctx, errorsmod.Wrapf(err, "failed to get commit info for height %d", height)
+        }
+        if cInfo != nil {
             ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height, Time: cInfo.Timestamp})
+        } else {
+            ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height})
+        }
     }
 }
📝 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
WithBlockHeader(*header).
WithBlockHeight(height)
if height != lastBlockHeight {
if !isLatest {
WithBlockHeader(*header).
WithBlockHeight(height)
if !isLatest {
rms, ok := app.cms.(*rootmulti.Store)
if ok {
cInfo, err := rms.GetCommitInfo(height)
if err != nil {
return ctx, errorsmod.Wrapf(err, "failed to get commit info for height %d", height)
}
if cInfo != nil {
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height, Time: cInfo.Timestamp})
} else {
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height})
}
}
}

Comment on lines +1246 to +1271
var header *cmtproto.Header
isLatest := height == 0
for _, state := range []*state{
app.checkState,
app.finalizeBlockState,
} {
if state != nil {
// branch the commit multi-store for safety
h := state.Context().BlockHeader()
if isLatest {
lastBlockHeight = qms.LatestVersion()
}
if !checkHeader || !isLatest || isLatest && h.Height == lastBlockHeight {
header = &h
break
}
}
}

if header == nil {
return sdk.Context{},
errorsmod.Wrapf(
sdkerrors.ErrInvalidHeight,
"header height in all state context is not latest height (%d)", lastBlockHeight,
)
}
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

Ensure header is properly assigned to prevent nil dereference.

In the loop starting at line 1246, if neither app.checkState nor app.finalizeBlockState is non-nil or meet the conditions, header may remain nil, potentially leading to a nil pointer dereference later in the code. Please ensure that header is always assigned a valid value or add error handling for the case when it remains nil.

Apply this diff to add a check for header being nil:

     }

+    if header == nil {
+        return sdk.Context{},
+            errorsmod.Wrapf(
+                sdkerrors.ErrInvalidHeight,
+                "failed to retrieve header for height %d", height,
+            )
+    }

     // when a client did not provide a query height, manually inject the latest

Committable suggestion was skipped due to low confidence.

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Oct 3, 2024
@tac0turtle tac0turtle added this pull request to the merge queue Oct 7, 2024
Merged via the queue into cosmos:main with commit 6cc6a6c Oct 7, 2024
71 of 77 checks passed
mergify bot pushed a commit that referenced this pull request Oct 7, 2024
(cherry picked from commit 6cc6a6c)

# Conflicts:
#	CHANGELOG.md
julienrbrt pushed a commit that referenced this pull request Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants