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

lnwallet: perform mempool acceptance check before publishing #8345

Merged
merged 10 commits into from Jan 27, 2024

Conversation

yyforyongyu
Copy link
Collaborator

@yyforyongyu yyforyongyu commented Jan 5, 2024

This PR adds a mempool acceptance check before broadcasting a given transaction. To maintain the current behavior from
BtcWallet.PublishTransaction, the two errors, ErrInMempool and ErrAlreadyConfirmed returned from TestMempoolAccept are ignored.

Note that we choose to implement this method as an interface method on chain.Interface in btcwallet as we want to have this TestMempoolAccept being used as an independent check and can be used in other cases where we don't wanna publish transactions. However, we could implement this check in btcwallet instead in PublishTransaction here,
https://github.com/btcsuite/btcwallet/blob/5df09dd4335865dde2a9a6d94a26a7f5779af825/wallet/wallet.go#L3657-L3659
since every tx must pass this mempool acceptance check before it can be broadcast. This alternative approach, tho, do have a problem, that we need to pass the max feerate used by testmempoolaccept to PublishTransaction in btcwallet, which seems to be a larger change.

Depends on:

Fixes #4760

Summary by CodeRabbit

  • New Features

    • Implemented a new mempool acceptance check for transactions to enhance reliability of transaction broadcasting.
  • Improvements

    • Improved error logging for issues encountered during the republishing of closing transactions.
  • Documentation

    • Updated release notes to document the new mempool acceptance feature.
  • Refactor

    • Renamed a method for better clarity in the transaction republishing process.

@yyforyongyu yyforyongyu added wallet The wallet (lnwallet) which LND uses mempool labels Jan 5, 2024
@yyforyongyu yyforyongyu mentioned this pull request Jan 5, 2024
4 tasks
@yyforyongyu yyforyongyu force-pushed the use-testmempoolaccept branch 2 times, most recently from 085de24 to c93ce9d Compare January 5, 2024 12:04
@yyforyongyu
Copy link
Collaborator Author

weird the CI isn't running

@yyforyongyu yyforyongyu force-pushed the use-testmempoolaccept branch 2 times, most recently from e7283c2 to 7c84e38 Compare January 5, 2024 21:49
contractcourt/chain_arbitrator.go Outdated Show resolved Hide resolved
docs/release-notes/release-notes-0.18.0.md Show resolved Hide resolved
@yyforyongyu yyforyongyu force-pushed the use-testmempoolaccept branch 2 times, most recently from a531def to 25831f8 Compare January 6, 2024 20:58
Copy link
Collaborator

@saubyk saubyk left a comment

Choose a reason for hiding this comment

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

concept ack

@saubyk saubyk added this to the v0.18.0 milestone Jan 7, 2024
lnwallet/btcwallet/btcwallet.go Outdated Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Outdated Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
docs/release-notes/release-notes-0.18.0.md Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Outdated Show resolved Hide resolved
contractcourt/chain_arbitrator.go Show resolved Hide resolved
@yyforyongyu yyforyongyu force-pushed the use-testmempoolaccept branch 2 times, most recently from 7791dbc to f4b782c Compare January 15, 2024 12:23
Copy link
Collaborator

@ProofOfKeags ProofOfKeags left a comment

Choose a reason for hiding this comment

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

Looks decent to me. I think there are opportunities for cleaning up error translation responsibility so that we can keep the error boundaries clean but afaict those issues precede this PR and don't need to be fixed here.

lnutils/errors.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
Copy link

coderabbitai bot commented Jan 19, 2024

Important

Auto Review Skipped

Auto reviews are limited to the following labels: llm-review. Please add one of these labels to enable auto reviews.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

The updates involve enhancing transaction handling in a Bitcoin Lightning Network Daemon (lnd). A new method for testing mempool acceptance (TestMempoolAccept) ensures transactions conform to mempool rules before broadcast. Error handling improvements are made in the Start method and a new utility function (ErrorAs) simplifies error type assertions. The PublishTransaction method is updated for better error management and backend-specific behavior.

Changes

File Path Change Summary
chainreg/no_chain_backend.go Added TestMempoolAccept method.
contractcourt/chain_arbitrator.go Improved error logging in Start, renamed a method.
docs/release-notes/release-notes-0.18.0.md Documented new mempool acceptance check.
lnutils/errors.go Introduced ErrorAs function.
lnwallet/btcwallet/btcwallet.go Enhanced PublishTransaction with error handling logic.

Poem

In the code where the bits all play,
A rabbit hopped by, refining the way.
🐇💻 With a tweak and a fix, it made transactions stick,
Ensuring the mempool's nod, quick and slick.

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@yyforyongyu
Copy link
Collaborator Author

@coderabbitai review

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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ec5b824 and 020daf5.
Files ignored due to path filters (2)
  • go.mod is excluded by: !**/*.mod
  • go.sum is excluded by: !**/*.sum
Files selected for processing (5)
  • chainreg/no_chain_backend.go (2 hunks)
  • contractcourt/chain_arbitrator.go (2 hunks)
  • docs/release-notes/release-notes-0.18.0.md (1 hunks)
  • lnutils/errors.go (1 hunks)
  • lnwallet/btcwallet/btcwallet.go (2 hunks)
Additional comments: 7
lnutils/errors.go (1)
  • 5-21: The implementation of ErrorAs is correct and follows best practices by simplifying error handling with generics. This is a good use of Go's type parameters feature.
chainreg/no_chain_backend.go (1)
  • 217-221: The TestMempoolAccept method is correctly added as a stub to the NoChainSource type. Ensure that it is documented as a placeholder implementation since it returns nil for both the result and error.
docs/release-notes/release-notes-0.18.0.md (1)
  • 110-115: The release notes have been updated correctly to describe the new mempool acceptance check feature for transactions in lnd. The explanation is clear and includes a link to the relevant PR.
lnwallet/btcwallet/btcwallet.go (4)
  • 1204-1206: The handleErr closure is introduced to handle errors from PublishTransaction and TestMempoolAccept. This is a good use of a closure to avoid code duplication.
  • 1232-1238: The early return for the "neutrino" backend in PublishTransaction is a good separation of logic for different backends.
  • 1240-1245: The use of TestMempoolAccept with a max feerate of 0 to use the default value is a good implementation detail that aligns with the PR's objective to check mempool acceptance before broadcasting.
  • 1250-1254: The sanity check for the length of results from TestMempoolAccept ensures that the expected number of results is returned, which is a good defensive programming practice.

contractcourt/chain_arbitrator.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
Comment on lines +1291 to +1296
case lnutils.ErrorAs[*base.ErrInMempool](err),
lnutils.ErrorAs[*base.ErrAlreadyConfirmed](err):

err := b.wallet.PublishTransaction(tx, label)

return handleErr(err)
Copy link

Choose a reason for hiding this comment

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

The switch case that handles ErrInMempool and ErrAlreadyConfirmed by re-publishing the transaction to mark the label in the wallet is a temporary workaround. This is not ideal as it may lead to redundant network traffic and should be refactored once the rebroadcaster is implemented.

Refactor this logic to avoid broadcasting the transaction twice once the new rebroadcaster is in place.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

Latest changes looking pretty good. I think we can cherry-pick this commit from my old PR as well to get us some extra coverage: 81b132f#diff-dce12550c3c3d6c8e301e5fd1341b398c7a0f4680f752acabeefb42a5a1f61d5R2036

Might need to be modified slightly tho for this new version

This commit adds a mempool acceptance check before broadcasting a given
transaction. To maintain the current behavior from
`BtcWallet.PublishTransaction`, the two errors, `ErrInMempool` and
`ErrAlreadyConfirmed` returned from `TestMempoolAccept` are ignored.
Previously, `PublishTransaction` in `btcwallet` will first mark the tx
label in db first before broadcasting it to the network. Even though the
broadcast may fail with the error already in mempool or already
confirmed, this tx label updating is still performed. To maintain the
old behavior, we now catch the errors from `TestMempoolAccept`, and make
sure to call the `PublishTransaction` to mark the tx label even there
are errors from mempool acceptance check.
yyforyongyu and others added 4 commits January 25, 2024 07:54
We should not fail to start the node if a republish attempt failed for a
channel's closing tx. Instead, we log an error to continue the startup
and let other channels continue their operations.
bitcoind v25.0 updated the `sendrawtransaction` RPC to have an optional
argument `maxburnamount` with a default value of 0. This means our
existing test that uses burning output cannot be published, hence, we
remove the usage of it in our tests and replace it with a normal tx.
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🥐

lnwallet/btcwallet/btcwallet.go Show resolved Hide resolved
@Roasbeef Roasbeef merged commit d44c72b into lightningnetwork:master Jan 27, 2024
24 of 25 checks passed
@yyforyongyu yyforyongyu deleted the use-testmempoolaccept branch January 29, 2024 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mempool wallet The wallet (lnwallet) which LND uses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wallet: respect policy-level transaction size limits when crafting regular and funding transactions
4 participants