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: evictions issue in tx client #3830

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open

Conversation

ninabarbakadze
Copy link
Member

@ninabarbakadze ninabarbakadze commented Aug 28, 2024

Overview

Fixes #3768 partially and introduce safer nonce handling. This solution doesn't address the resubmission issue. Me and @cmwaters are working on redesigning nonce handling to make it more seamless and secure.

@ninabarbakadze ninabarbakadze self-assigned this Aug 28, 2024
@ninabarbakadze ninabarbakadze marked this pull request as ready for review September 2, 2024 13:27
@ninabarbakadze ninabarbakadze requested a review from a team as a code owner September 2, 2024 13:27
@ninabarbakadze ninabarbakadze requested review from staheri14 and rach-id and removed request for a team September 2, 2024 13:27
Copy link
Contributor

coderabbitai bot commented Sep 2, 2024

Walkthrough

Walkthrough

The changes introduce a new struct, txInfo, to manage transaction information within the TxClient. A local transaction tracker is established to monitor submitted transactions, enhancing nonce management and error handling. The TxClient methods are updated to incorporate this new tracker, improving transaction confirmation and recovery from evictions. Additionally, a new test file is created to validate the pruning functionality of the transaction tracker.

Changes

Files Change Summary
pkg/user/tx_client.go Introduced txInfo struct for transaction information, added txTracker map to TxClient, modified methods for transaction management and confirmation.
pkg/user/pruning_test.go Implemented tests for pruning logic in the transaction tracker, validating the removal of old transactions.

Assessment against linked issues

Objective Addressed Explanation
Improve transaction submission handling evictions (#3768)
Enhance nonce management for multiple transactions (#3768)
Improve error handling in transaction confirmation (#3768)

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

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

@ninabarbakadze ninabarbakadze marked this pull request as draft September 2, 2024 14:21
Copy link
Member Author

@ninabarbakadze ninabarbakadze left a comment

Choose a reason for hiding this comment

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

square size test started failing, investigating that.

@ninabarbakadze ninabarbakadze marked this pull request as ready for review September 3, 2024 09:15
@ninabarbakadze ninabarbakadze marked this pull request as draft September 3, 2024 09:36
@@ -137,6 +148,9 @@ type TxClient struct {
defaultGasPrice float64
defaultAccount string
defaultAddress sdktypes.AccAddress
txPool map[string]poolTxInfo
Copy link
Member Author

Choose a reason for hiding this comment

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

[Question] do we want to have a way of periodically clearing the cache?

Copy link
Member Author

Choose a reason for hiding this comment

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

@evan-forbes @cmwaters I'd appreciate your input here

Copy link
Member

Choose a reason for hiding this comment

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

ideally, it seems like we should never be a scenario where we need this to be cleared. Either a tx is included and we continually try to get it included, or we handle each case when that does not occur and remove the tx at that point

Copy link
Member Author

Choose a reason for hiding this comment

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

the only time tx doesn't get removed from this local cache is if context gets cancelled while polling for tx status which means that the said tx was never confirmed. If the transaction doesn't get confirmed it can linger in that cache until it ooms

Copy link
Member

@evan-forbes evan-forbes Sep 5, 2024

Choose a reason for hiding this comment

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

by periodically, do we mean a ttl?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes!

Copy link
Contributor

Choose a reason for hiding this comment

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

The scenario that I see as more problematic is if a user decides to use BroadcastTx which creates the map entry and doesn't use ConfirmTx which cleans it up (because maybe they don't care if it lands or not).

I think we should garbage collect, but not as a separate go routine but within BroadcastTx.

I would give at least 10 minutes before we clean up.

@ninabarbakadze ninabarbakadze marked this pull request as draft September 10, 2024 10:22
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)
pkg/user/tx_client.go (1)

470-488: LGTM!

The handleEvictions function is correctly implemented and handles evictions appropriately by:

  1. Retrieving the transaction from the local pool.
  2. Setting the signer's sequence to the sequence of the evicted transaction, so that it's ready for resubmission.
  3. Removing the evicted transaction from the local pool.

Consider adding a comment to provide more context on why the sequence is not rolled back:

// The sequence should not be rolled back to the sequence of the transaction that was evicted to be
// ready for resubmission. All transactions with a later nonce will be kicked by the nodes tx pool.

@ninabarbakadze ninabarbakadze marked this pull request as ready for review September 10, 2024 11:55
Comment on lines +333 to +347
func setupTxClient(t *testing.T, ttlDuration time.Duration) (encoding.Config, *user.TxClient, testnode.Context) {
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
defaultTmConfig := testnode.DefaultTendermintConfig()
defaultTmConfig.Mempool.TTLDuration = ttlDuration
testnodeConfig := testnode.DefaultConfig().
WithTendermintConfig(defaultTmConfig).
WithFundedAccounts("a", "b", "c").
WithAppCreator(testnode.CustomAppCreator("0utia"))
ctx, _, _ := testnode.NewNetwork(t, testnodeConfig)
_, err := ctx.WaitForHeight(1)
require.NoError(t, err)
txClient, err := user.SetupTxClient(ctx.GoContext(), ctx.Keyring, ctx.GRPCClient, encCfg, user.WithGasMultiplier(1.2))
require.NoError(t, err)
return encCfg, txClient, ctx
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this doesn't make too much sense as it's own function because it's so specific to one test.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using this logic twice so how else should i do it

Comment on lines 322 to 331
// asserts that a tx was indexed in the tx tracker and that the sequence does not increase
func assertTxInTxTracker(t *testing.T, txClient *user.TxClient, txHash string, expectedSigner string) {
nonce, signer, exists := txClient.GetTxFromTxTracker(txHash)
require.True(t, exists)
require.Equal(t, expectedSigner, signer)
seq := txClient.Signer().Account(expectedSigner).Sequence()
// Successfully broadcast transaction increases the nonce
// txInfo is indexed before the nonce is increased
require.Equal(t, seq, nonce+1)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by asserting that it doesn't increase yet expecting the sequence to be 1 ahead of the sequence of the transaction (i.e. it was incremented)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll change the naming actually to make it more clear but nonce is retrieved form tx tracker which was saved before user's sequence increased but i query seq above which will naturally be increased after broadcast

pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
Co-authored-by: Callum Waters <[email protected]>
@celestia-bot celestia-bot requested a review from a team September 18, 2024 13:07
@rootulp rootulp self-requested a review September 18, 2024 18:00
rootulp
rootulp previously approved these changes Sep 18, 2024
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

nit: consistently use the term txTracker instead of txTracker in some places and local pool in other places.

pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
pkg/user/tx_client.go Outdated Show resolved Hide resolved
@celestia-bot celestia-bot requested a review from a team September 18, 2024 18:12
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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1e1de7f and def40f2.

Files selected for processing (2)
  • pkg/user/pruning_test.go (1 hunks)
  • pkg/user/tx_client.go (9 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/user/tx_client.go
Additional comments not posted (1)
pkg/user/pruning_test.go (1)

11-51: Excellent test coverage for the transaction tracker pruning functionality!

The test is well-structured and covers the essential aspects of pruning:

  • It sets up a controlled environment with transactions of different ages.
  • It calls the pruneTxTracker method to trigger the pruning process.
  • It verifies the expected state of the transaction tracker after pruning, ensuring that only transactions older than the pruning threshold are removed.

The test provides confidence in the correctness of the pruning implementation.

txClient.pruneTxTracker()
// Prunes the transactions that are 10 minutes old
// 5 transactions will be pruned
require.Equal(t, txTrackerBeforePruning-txsToBePruned, txsToBePruned)
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix the assertion by swapping the expected and actual values.

The assertion on line 48 compares the wrong values. It should compare the difference between the tracker size before pruning and the number of transactions to be pruned with the actual size of the tracker after pruning.

Swap the expected and actual values in the assertion:

-require.Equal(t, txTrackerBeforePruning-txsToBePruned, txsToBePruned)
+require.Equal(t, txsToBePruned, txTrackerBeforePruning-txsToBePruned)
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
require.Equal(t, txTrackerBeforePruning-txsToBePruned, txsToBePruned)
require.Equal(t, txsToBePruned, txTrackerBeforePruning-txsToBePruned)

Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

Fixes #3768

The PR description will close that issue when this PR merges which doesn't seem like what you want. Since #3768 is large in scope, it is not immediately obvious which parts of that issue this PR addresses. One idea is to convert that issue into a tracking issue with several smaller issues. Then this PR can close one of the smaller issues and leave the remaining ones open.

}
numTransactions := 10

// Add 10 transactions to the tracker that are 10 minutes old
Copy link
Collaborator

Choose a reason for hiding this comment

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

this comment seems incorrect. It looks like the implementation below adds 5 transactions that are 10 minutes old and 5 transactions that are 5 minutes old.

Suggested change
// Add 10 transactions to the tracker that are 10 minutes old

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.

Improve Tx submission: handling evictions
4 participants