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

chore: migrate OutgoingTxBatchBlockKey with batch nonce #773

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

zakir-code
Copy link
Contributor

@zakir-code zakir-code commented Oct 23, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced testing for application upgrades, including checks for outgoing transaction batches.
    • Improved migration functionality for processing outgoing transaction batches during upgrades.
  • Bug Fixes

    • Addressed potential key collisions in outgoing transaction batch management by incorporating batch nonce into key generation.
  • Documentation

    • Updated function signatures to reflect new parameters and improve clarity on migration processes.

Copy link

coderabbitai bot commented Oct 23, 2024

Walkthrough

The pull request introduces several modifications across multiple files to enhance cross-chain functionality and testing within the application. Key changes include the addition of a new function to verify outgoing transaction batches, updates to migration methods to accommodate a codec parameter, and adjustments to key generation for outgoing transaction batches. These enhancements aim to improve the robustness of the upgrade process and ensure that transaction batches are correctly managed and validated during application upgrades.

Changes

File Path Change Summary
app/upgrade_test.go - Added import for crosschain/types.
- Updated checkAppUpgrade to include checkOutgoingBatch function.
- Added method: func checkOutgoingBatch(t *testing.T, ctx sdk.Context, myApp *app.App).
x/crosschain/keeper/migrations.go - Updated Migrate7to8 method to include m.keeper.cdc in the call to v8.Migrate.
- Method signature updated: func (m Migrator) Migrate7to8(ctx sdk.Context) error.
x/crosschain/keeper/outgoing_tx.go - Updated StoreBatch and DeleteBatch methods to include batch.BatchNonce in GetOutgoingTxBatchBlockKey calls.
- Method signatures updated for both methods.
x/crosschain/migrations/v8/migrate.go - Updated Migrate function signature to include cdc codec.BinaryCodec.
- Added new function: func migrateOutgoingTxBatchBlockKey(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec).
x/crosschain/types/key.go - Updated GetOutgoingTxBatchBlockKey function to accept batchNonce as an additional parameter.
- Method signature updated: func GetOutgoingTxBatchBlockKey(blockHeight, batchNonce uint64) []byte.

Possibly related PRs

🐰 In the meadow where bunnies play,
New tests hop in, brightening the day.
With batches checked and keys unique,
Our upgrades now are strong and sleek!
Through cross-chain paths, we leap and bound,
In code we trust, where joy is found! 🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9368653 and 606a386.

📒 Files selected for processing (1)
  • x/crosschain/migrations/v8/migrate.go (2 hunks)
🧰 Additional context used
🔇 Additional comments (4)
x/crosschain/migrations/v8/migrate.go (4)

5-5: LGTM: Required imports added correctly

The new imports for codec and crosschain types are necessary for the added functionality.

Also applies to: 9-9


36-40: LGTM: Clean migration function update

The function signature has been properly updated to include the codec parameter, and the implementation maintains good error handling practices.


42-59: Previous review comments are still applicable

The implementation still has the same issues that were highlighted in the previous review:

  1. Using MustUnmarshal/MustMarshal which can panic
  2. Loading all batches into memory instead of processing them one by one

56-57: Verify the new block key format

Let's verify that the new block key format with batch nonce is being used consistently across the codebase.

✅ Verification successful

Based on the search results, I can now generate the final response as we have sufficient information about the usage of GetOutgoingTxBatchBlockKey across the codebase.

Block key format is consistently implemented with batch nonce

The verification shows that the new block key format is being used correctly and consistently throughout the codebase:

  • The key format is properly defined in types/key.go with both block height and batch nonce parameters
  • All usages of GetOutgoingTxBatchBlockKey across the codebase consistently pass both block and batchNonce parameters
  • The raw OutgoingTxBatchBlockKey is only used as a prefix in the key construction function and not directly manipulated elsewhere
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent usage of GetOutgoingTxBatchBlockKey with batch nonce

# Test: Search for all usages of GetOutgoingTxBatchBlockKey
# Expect: All calls should include both block and batch nonce parameters
rg -A 2 "GetOutgoingTxBatchBlockKey"

# Test: Search for any old key format usage
# Expect: No direct manipulation of OutgoingTxBatchBlockKey without batch nonce
ast-grep --pattern 'OutgoingTxBatchBlockKey'

Length of output: 1568


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

@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: 3

🧹 Outside diff range and nitpick comments (1)
app/upgrade_test.go (1)

151-160: LGTM with minor suggestions!

The implementation correctly verifies the presence of outgoing transaction batches after upgrade.

Consider these improvements:

  1. Add function documentation to explain its purpose
  2. Add assertion for non-nil batch
+// checkOutgoingBatch verifies that all outgoing transaction batches are correctly
+// migrated during the upgrade by checking their presence in the store using the
+// new key format that includes both block height and batch nonce.
 func checkOutgoingBatch(t *testing.T, ctx sdk.Context, myApp *app.App) {
 	t.Helper()
 	for _, keeper := range myApp.CrosschainKeepers.ToSlice() {
 		kvStore := ctx.KVStore(myApp.GetKey(keeper.ModuleName()))
 		keeper.IterateOutgoingTxBatches(ctx, func(batch *types.OutgoingTxBatch) bool {
+			require.NotNil(t, batch)
 			assert.True(t, kvStore.Has(types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce)))
 			return false
 		})
 	}
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 12b35b8 and 9368653.

📒 Files selected for processing (5)
  • app/upgrade_test.go (3 hunks)
  • x/crosschain/keeper/migrations.go (1 hunks)
  • x/crosschain/keeper/outgoing_tx.go (2 hunks)
  • x/crosschain/migrations/v8/migrate.go (2 hunks)
  • x/crosschain/types/key.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (9)
x/crosschain/keeper/migrations.go (1)

20-20: LGTM! Verify v8.Migrate implementation.

The addition of codec parameter aligns with the PR objective of migrating OutgoingTxBatchBlockKey with batch nonce, as it's needed for proper serialization of batch data during migration.

Let's verify the v8.Migrate implementation:

✅ Verification successful

Let me verify the implementation of migrateOutgoingTxBatchBlockKey since it's the core function handling the batch nonce migration.


Verified: Migration implementation is correct and properly uses codec

The verification confirms that:

  • v8.Migrate function signature matches the call with correct parameters (ctx, storeKey, codec)
  • migrateOutgoingTxBatchBlockKey properly uses the codec for:
    • Unmarshaling existing batch data from store
    • Marshaling batch data with the new key format that includes batch nonce
    • Correctly iterates and migrates all existing batches
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify v8.Migrate function signature matches the new call
ast-grep --pattern 'func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error'

Length of output: 534


Script:

#!/bin/bash
# Check implementation of migrateOutgoingTxBatchBlockKey
ast-grep --pattern 'func migrateOutgoingTxBatchBlockKey(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec)'

Length of output: 1438

app/upgrade_test.go (2)

28-28: LGTM!

The added import is necessary for accessing crosschain types in the new test function.


109-110: LGTM!

The addition of checkOutgoingBatch verification is appropriately placed alongside other upgrade verification steps.

x/crosschain/keeper/outgoing_tx.go (3)

131-131: LGTM!

The deletion of the block key is correctly updated to match the new key structure using both block height and batch nonce.


Line range hint 118-124: Update the comment to reflect current implementation.

The implementation looks good, but the comment "Only one OutgoingTxBatch can be submitted in a block" appears to be outdated since the block key now includes the batch nonce, allowing multiple batches per block.

-	// Note: Only one OutgoingTxBatch can be submitted in a block
+	// Note: Only one OutgoingTxBatch can be submitted per batch nonce in a block

Let's verify if there are any assumptions about single batch per block in tests:

#!/bin/bash
# Search for test cases that might assume single batch per block
rg -A 5 "OutgoingTxBatch.*block" "*_test.go"

Line range hint 214-234: Update IterateBatchByBlockHeight to handle new key format.

The IterateBatchByBlockHeight method needs to be updated to handle the new key format that includes batch nonce. The current implementation might miss batches or provide incorrect results since it assumes a simpler key structure.

Consider updating the method to handle the new key format:

 func (k Keeper) IterateBatchByBlockHeight(ctx sdk.Context, start, end uint64, cb func(*types.OutgoingTxBatch) bool) {
 	store := ctx.KVStore(k.storeKey)
-	startKey := append(types.OutgoingTxBatchBlockKey, sdk.Uint64ToBigEndian(start)...)
-	endKey := append(types.OutgoingTxBatchBlockKey, sdk.Uint64ToBigEndian(end)...)
+	// Use only block height prefix to catch all batches in the block range
+	startKey := types.GetOutgoingTxBatchBlockPrefix(start)
+	endKey := types.GetOutgoingTxBatchBlockPrefix(end)
 	iter := store.Iterator(startKey, endKey)
 	defer iter.Close()

Let's verify the usage of this method:

x/crosschain/migrations/v8/migrate.go (3)

5-5: Imports are updated appropriately

The necessary packages codec and types are correctly imported to support serialization and type definitions required for the migration process.

Also applies to: 9-9


36-36: Updated Migrate function signature to include cdc parameter

Including cdc codec.BinaryCodec allows for proper serialization and deserialization within the migration, which is essential for processing store data.


38-38: Calling new migrateOutgoingTxBatchBlockKey function

The Migrate function now invokes migrateOutgoingTxBatchBlockKey to handle the migration of outgoing transaction batch keys, ensuring that batch keys are updated correctly.

x/crosschain/types/key.go Show resolved Hide resolved
x/crosschain/migrations/v8/migrate.go Show resolved Hide resolved
x/crosschain/migrations/v8/migrate.go Outdated Show resolved Hide resolved
Co-authored-by: nulnut <151493716+nulnut@users.noreply.github.com>
@nulnut nulnut merged commit 22f4b6e into main Oct 23, 2024
10 checks passed
@nulnut nulnut deleted the nulnut/migrate-batch-key branch October 23, 2024 08:22
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.

2 participants