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

Beat [1/4]: handle sweeper's broadcast error #8893

Open
wants to merge 11 commits into
base: yy-feature-blockbeat
Choose a base branch
from

Conversation

yyforyongyu
Copy link
Collaborator

@yyforyongyu yyforyongyu commented Jul 4, 2024

Depends on #8892.

This PR prepares the incoming blockbeat PRs, the changes are,

  • BlockEpoch now has the block data instead of the block header. This block data is used in the incoming blockbeat PR to query spending transactions.
  • Added a new sweeping state, TxError. Inputs resulting in this state will be removed from the sweeper.
  • check input's CSV and CLTV in the sweeper, and skip sweeping them if not matured.
  • When calculating the deadline, make sure it's derived from mature height, not current height.

@yyforyongyu yyforyongyu added this to the v0.18.2 milestone Jul 4, 2024
Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

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 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
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Very nice! Did a quick drive-by review to load up on context. Not super familiar with the new sweeper system, so best not to count this review at all. I'm mostly interested in the block beat implementation itself (which seems to be part 2 of 3).

chainntnfs/best_block_view_test.go Outdated Show resolved Hide resolved
sweep/sweeper.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

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

high level pass looks good! I need to spend a bit more time getting familiar with sweeper stuff on the next pass though

chainntnfs/interface.go Outdated Show resolved Hide resolved
log.Debugf("Removing confirmed monitor record=%v, tx=%v", id,
result.Tx.TxHash())

case TxError:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the select's comment should probably be updated explaining this third case

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool updated the docs to make it more clear.

@@ -124,6 +124,10 @@ type BumpRequest struct {
// StartingFeeRate is an optional parameter that can be used to specify
// the initial fee rate to use for the fee function.
Copy link
Collaborator

Choose a reason for hiding this comment

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

may help with review just to say in the commit msg why it will not work with the upcoming change? just cause this commit still does allow immediate broadcast if Immediate is true so it is unclear just looking at this commit why that then would work later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool updated!

sweep/fee_bumper.go Outdated Show resolved Hide resolved
sweep/fee_bumper_test.go Outdated Show resolved Hide resolved
inp := createTestInput(1000, input.WitnessKeyHash)

// Create a testing bump request.
req := &BumpRequest{
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we add a test case for when BumpRequest.Immediate is true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good call, added!

@yyforyongyu yyforyongyu force-pushed the yy-itest-miner branch 4 times, most recently from 1e898e2 to 4548971 Compare July 18, 2024 01:36
@yyforyongyu yyforyongyu changed the title Beat [1/3]: handle sweeper's broadcast error Beat [1/4]: handle sweeper's broadcast error Jul 18, 2024
@saubyk saubyk modified the milestones: v0.18.3, v0.19.0 Jul 18, 2024
@yyforyongyu yyforyongyu force-pushed the yy-itest-miner branch 2 times, most recently from e0ec893 to 9180762 Compare July 23, 2024 16:49
@guggero guggero deleted the branch lightningnetwork:yy-feature-blockbeat July 23, 2024 18:12
@guggero guggero closed this Jul 23, 2024
@guggero
Copy link
Collaborator

guggero commented Jul 23, 2024

Aarrgh.... Sorry, I deleted the base branch before GitHub could re-base it...

@guggero
Copy link
Collaborator

guggero commented Jul 23, 2024

Testing re-open

@guggero guggero reopened this Jul 23, 2024
@yyforyongyu yyforyongyu deleted the branch lightningnetwork:yy-feature-blockbeat July 23, 2024 18:14
@yyforyongyu yyforyongyu reopened this Jul 23, 2024
@yyforyongyu yyforyongyu changed the base branch from yy-itest-miner to master July 23, 2024 18:19
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

My main question and/or concern is with the first commit, that could have a big negative impact on Neutrino. Not sure how consequential that is for the rest of the series of PRs?

The rest looks good to me 🎉

startingHeader, err := n.p2pNode.GetBlockHeader(
&startingPoint.Hash,
)
startingBlock, err := n.p2pNode.GetBlock(startingPoint.Hash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, doesn't this change mean that in a Neutrino environment we're suddenly fetching way more blocks (if not all) from our peers instead of just the header that we have anyway?

Maybe we need to find some middle ground where we have a new struct that has the hash, height and header and then fn.Option[Block] that is fn.None for Neutrino. Then when we decide we actually want to fetch the block we do it lazily?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it means we will fetch the block once from one of the peers then it will be cached? Will investigate more.

t.records.ForEach(visitor)

// Handle the initial broadcast.
for requestID, r := range initialRecords {
rec := r
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this is needed anymore. First, because we're using Go 1.22 and second because the loop variable is passed to a method call, so it's placed on the stack anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed - also added a commit to remove other occurrence

@@ -178,7 +178,8 @@ func (b *BitcoindNotifier) startNotifier() error {
if err != nil {
return err
}
blockHeader, err := b.chainConn.GetBlockHeader(currentHash)

block, err := b.GetBlock(currentHash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the difference between b.GetBlock and b.chainConn.GetBlock?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

b.GetBlock wraps the b.chainConn.GetBlock inside a cache.

startingHeader, err := n.p2pNode.GetBlockHeader(
&startingPoint.Hash,
)
startingBlock, err := n.p2pNode.GetBlock(startingPoint.Hash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this skips the locking that's normally done in n.GetBlock.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Replaced with n.GetBlock

func (n *NeutrinoChainConn) GetBlock(
blockHash *chainhash.Hash) (*wire.MsgBlock, error) {

utilBlock, err := n.p2pNode.GetBlock(*blockHash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might also be a locking problem here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you elaborate a bit? Think the lock is only used in NeutrinoNotifier tho

sweep/fee_bumper.go Outdated Show resolved Hide resolved
Comment on lines +352 to +395
if req.Immediate {
t.handleInitialBroadcast(record, requestID)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

So for the typical case (Immediate: false) we now wait an extra block before broadcasting? We already wait one block for the sweeper to process the transaction, and now this waits another before broadcasting...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We actually save two blocks (worst case) when sweeping inputs now. So this PR is some commits cherry-picked from the final PR so it may not make sense to look at it alone, but the full pic is, suppose a force close happens at block X, contractcourt will process block X, prepare all the necessary resolutions, offer them to the sweeper. Then sweeper processes block X by broadcasting the sweeping txns.

Comment on lines 890 to 980
// Create a tx so the caller knows which inputs have failed.
sweepTx := wire.NewMsgTx(2)
for _, o := range r.req.Inputs {
sweepTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: o.OutPoint(),
Sequence: o.BlocksToMaturity(),
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems hacky to create a fake tx to tell the caller info it should already know.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated the flow here - we now no longer need to get the inputs from the tx!

event = TxFailed

default:
event = TxError
Copy link
Collaborator

Choose a reason for hiding this comment

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

In what circumstances would we hit this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated the comments

sweep/sweeper.go Outdated Show resolved Hide resolved
@@ -221,6 +221,30 @@ func (p *SweeperInput) terminated() bool {
}
}

// isMature returns a boolean indicating whether the input has a timelock that
// has been reached or not. The locktime found is also returned.
func (p *SweeperInput) isMature(currentHeight uint32) (bool, uint32) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How do timelocked inputs get to the sweeper? I thought they were only supposed to be offered to the sweeper once timelocks are fulfilled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah it's changed in a later PR.

@lightninglabs-deploy
Copy link

@ellemouton: review reminder
@yyforyongyu, remember to re-request review from reviewers when ready

Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

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

@yyforyongyu - am assuming this is not yet ready for another round yeah?

@yyforyongyu
Copy link
Collaborator Author

@yyforyongyu - am assuming this is not yet ready for another round yeah?

Thanks for checking in! Nope not yet, will request once ready!

So the block data can be used by subsystems without calling `GetBlock`.
Also updated the loggings. This new state will be used in the following
commit.
@yyforyongyu yyforyongyu changed the base branch from master to yy-feature-blockbeat October 25, 2024 07:13
yyforyongyu and others added 9 commits October 25, 2024 15:27
This prepares the following commit where we now let the fee bumpr
decides whether to broadcast immediately or not.
This commit changes how inputs are handled upon receiving a bump result.
Previously the inputs are taken from the `BumpResult.Tx`, which is now
instead being handled locally as we will remember the input set when
sending the bump request, and handle this input set when a result is
received.
This commit adds a new method `handleInitialBroadcast` to handle the
initial broadcast. Previously we'd broadcast immediately inside
`Broadcast`, which soon will not work after the `blockbeat` is
implemented as the action to publish is now always triggered by a new
block. Meanwhile, we still keep the option to bypass the block trigger
so users can broadcast immediately by setting `Immediate` to true.
Previously in `markInputFailed`, we'd remove all inputs under the same
group via `removeExclusiveGroup`. This is wrong as when the current
sweep fails for this input, it shouldn't affect other inputs.
Also updated `handlePendingSweepsReq` to skip immature inputs so the
returned results are the same as those in pre-0.18.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 MUST be fixed or reviewed utxo sweeping
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

6 participants