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

feat: DON'T review before merging previous PRs, (BitcoinRBF-Step3) zetaclient watches BTC mempool stuck txs and bump fee #3396

Draft
wants to merge 56 commits into
base: develop
Choose a base branch
from

Conversation

ws4charlie
Copy link
Contributor

@ws4charlie ws4charlie commented Jan 23, 2025

Description

This PR will replace #3306

The goals:

  1. Implement BIP-125, Replace-By-Fee so that every outbound is replaceable when stuck in mempool.
  2. Use the Child-Pays-for-Parent (CPFP) to bump tx fees and unblock stuck outbounds.

The fee change:

  1. User still pays withdraw fee on a BTCGasPriceMultiplierFeeCharge = 2.0 multiplier.
  2. TSS signers will pay lower gas fee on a BTCGasPriceMultiplierSendTx = 1.5 multiplier for quick finality.
  3. The system will be able to make approximately 25% profit out of the fees paid by user, without worrying about stuck txs.

How it works:

  1. Enable RBF flag in every Bitcoin outbound.
  2. Spwan a new go routine WatchMempoolTxs in zetaclient to monitor pending outbound txs, so we know how long the txs have been sitting in the Bitcoin mempool.
  3. If the pending period of the outbounds exceeds a pre-defined threshold (30 mins and 3 blocks), zetaclient will mark the outbound status as stuck and trigger tx replacement using RBF and CPFP.
  4. After tx replacement, the outbound status will be set back to normal.
  5. After tx replacement, if the outbound still doesn't move forward for another threshold period of time, RBF will triggered again with even higher fee rate until reaching a feeRateCap = 100
  6. zetaclient will ALWAYS use most recent fee rate (feed by zetacore) to initiate new outbound transactions. The reason is that using an outdated GasPrice in CCTX struct is usually the root cause (not the Bitcoin network traffic) of stuck transactions, the low-fee problem needs to be solved at its root cause to reduce the chance of stuck txs. Similarly, when GasPrice in CCTX struct is higher than market, TSS signers will also use the most recent fee rate to send outbound and save gas fees.

A few concepts and parameters introduced:

  1. LastPendingOutbound:
    Bitcoin outbounds are sequentially chained transactions by nonce. Given N pending txs [TX1, TX2, TX3, TX4] in the mempool, zetaclient only need to watch and bump the fee of TX4 in order to clear all of them. According to Bitcoin CPFP strategy, the chained pending txs are treated as a package by miners. Bumping TX4 will increase the average fee rate of the whole txs package and make it more attractive to miners.
  2. minCPFPFeeBumpPercent:
    It is set to 20% as an exercise for the initial play. It is designed to balance effectiveness in replacing stuck tx while avoiding excessive sensitivity to fee market fluctuations. For example, given a paidRate == 10, RBF will not happen until the market rate goes up to liveRate==12.
  3. feeRateCap:
    It is the maximum average fee rate for fee bumping. 100 sat/vB is a chosen heuristic based on Bitcoin mempool statistics to avoid excessive (or accidental) fees.
  4. reservedRBFFees:
    It is the amount of BTC reserved in the outbound transaction for fee bumping. It is set to 0.01 BTC by default, which can bmp 10 transactions (1KB each) by 100 sat/vB. Most of the time, we have just 1 or 2 stuck transactions in the mempool and the signers will automatically stop signing new transactions by design, so the number 0.01 BTC is good enough.

Closes: #1695

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Important

Review skipped

Draft detected.

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.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

github-actions bot commented Jan 23, 2025

!!!WARNING!!!
nosec detected in the following files: zetaclient/chains/bitcoin/observer/db.go, zetaclient/chains/bitcoin/observer/gas_price.go, zetaclient/chains/bitcoin/observer/mempool.go, zetaclient/chains/bitcoin/observer/mempool_test.go, zetaclient/chains/bitcoin/signer/outbound_data.go, zetaclient/chains/bitcoin/signer/sign.go, zetaclient/chains/bitcoin/common/fee.go

Be very careful about using #nosec in code. It can be a quick way to suppress security warnings and move forward with development, it should be employed with caution. Suppressing warnings with #nosec can hide potentially serious vulnerabilities. Only use #nosec when you're absolutely certain that the security issue is either a false positive or has been mitigated in another way.

Only suppress a single rule (or a specific set of rules) within a section of code, while continuing to scan for other problems. To do this, you can list the rule(s) to be suppressed within the #nosec annotation, e.g: /* #nosec G401 */ or //#nosec G201 G202 G203
Broad #nosec annotations should be avoided, as they can hide other vulnerabilities. The CI will block you from merging this PR until you remove #nosec annotations that do not target specific rules.

Pay extra attention to the way #nosec is being used in the files listed above.

@github-actions github-actions bot added the nosec label Jan 23, 2025
@ws4charlie ws4charlie changed the title feat: (BitcoinRBF-Step3) zetaclient watches BTC mempool stuck txs and bump fee feat: DON'T review before merging previous PRs (BitcoinRBF-Step3) zetaclient watches BTC mempool stuck txs and bump fee Jan 23, 2025
@ws4charlie ws4charlie changed the title feat: DON'T review before merging previous PRs (BitcoinRBF-Step3) zetaclient watches BTC mempool stuck txs and bump fee feat: DON'T review before merging previous PRs, (BitcoinRBF-Step3) zetaclient watches BTC mempool stuck txs and bump fee Jan 23, 2025
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 67.17764% with 364 lines in your changes missing coverage. Please review.

Project coverage is 65.38%. Comparing base (6802bf2) to head (03d366c).
Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
zetaclient/chains/bitcoin/observer/outbound.go 29.62% 75 Missing and 1 partial ⚠️
zetaclient/chains/bitcoin/client/helpers.go 0.00% 60 Missing ⚠️
zetaclient/chains/bitcoin/observer/mempool.go 66.66% 44 Missing ⚠️
zetaclient/chains/bitcoin/signer/signer.go 42.25% 39 Missing and 2 partials ⚠️
zetaclient/chains/bitcoin/observer/gas_price.go 0.00% 37 Missing ⚠️
zetaclient/chains/bitcoin/client/commands.go 0.00% 26 Missing ⚠️
zetaclient/chains/bitcoin/signer/sign.go 83.64% 18 Missing and 8 partials ⚠️
zetaclient/chains/bitcoin/observer/utxos.go 84.34% 12 Missing and 6 partials ⚠️
zetaclient/chains/bitcoin/signer/outbound_data.go 90.14% 5 Missing and 2 partials ⚠️
zetaclient/chains/bitcoin/signer/sign_rbf.go 89.09% 5 Missing and 1 partial ⚠️
... and 7 more
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3396      +/-   ##
===========================================
+ Coverage    64.40%   65.38%   +0.97%     
===========================================
  Files          436      445       +9     
  Lines        30379    30879     +500     
===========================================
+ Hits         19566    20189     +623     
+ Misses        9972     9832     -140     
- Partials       841      858      +17     
Files with missing lines Coverage Δ
pkg/math/integer.go 100.00% <100.00%> (ø)
zetaclient/chains/bitcoin/bitcoin.go 52.02% <100.00%> (+0.99%) ⬆️
zetaclient/chains/bitcoin/signer/fee_bumper.go 100.00% <100.00%> (ø)
zetaclient/chains/evm/observer/observer.go 70.28% <100.00%> (ø)
zetaclient/testutils/testdata.go 87.09% <100.00%> (+0.35%) ⬆️
zetaclient/testutils/testdata_naming.go 80.85% <100.00%> (+0.85%) ⬆️
zetaclient/zetacore/broadcast.go 59.16% <100.00%> (+0.34%) ⬆️
zetaclient/zetacore/client_vote.go 52.50% <100.00%> (ø)
zetaclient/zetacore/tx.go 83.60% <100.00%> (ø)
zetaclient/chains/evm/signer/signer.go 41.56% <0.00%> (ø)
... and 16 more

@ws4charlie ws4charlie added chain:bitcoin Bitcoin chain related zetaclient Issues related to ZetaClient labels Jan 23, 2025
ws4charlie and others added 30 commits January 28, 2025 22:13
…t-bitcoin-RBF-zetaclient-refactor-minimum
…t-bitcoin-RBF-zetaclient-refactor-minimum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chain:bitcoin Bitcoin chain related nosec zetaclient Issues related to ZetaClient
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using RBF (Replace-by-Fee) for Bitcoin outbound transactions
2 participants