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

Move funding tx broadcasting to Fundingmanager #1635

Merged
merged 7 commits into from
Aug 10, 2018

Conversation

halseth
Copy link
Contributor

@halseth halseth commented Jul 26, 2018

Two commits to improve the handling of funding tx broadcasting errors.

fundingmanager: delete active reservation after channel is in DB

This commit makes sure we delete a pending channel from the set of
activeReservations within the fundingmanager immediately after the
channel is moved to the openChannelBucket in the DB. Previously we
wouldn't do this before the funding tx was confirmed, making it possible
that failing the funding flow at a later point would try to cancel a
non-existent reservation context.

lnwallet + funding: move funding tx publish to fundingmgr

This commit moves the responsibility for publishing the funding tx to
the network from the wallet to the funding manager. This is done to
distinguish the failure of completing the reservation within the wallet
and failure of publishing the transaction.

Earlier we could fail to broadcast the transaction, which would cause us
to fail the funding flow. This is not something we can do directly,
since the CompeteReservation call will mark the channel IsPending in the
databas.e

@offerm
Copy link
Contributor

offerm commented Jul 26, 2018

The Travis failure in this Build has to do with #1592

@Roasbeef Roasbeef added funding Related to the opening of new channels with funding transactions on the blockchain P2 should be fixed if one has time needs review PR needs review by regular contributors needs testing PR hasn't yet been actively tested on testnet/mainnet labels Jul 30, 2018
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.

Nice! This is a great step towards being able to properly clean up the database state for a channel whose funding transaction fails to broadcast. I commented below about also ensuring we remove the transaction from the database depending on the error we get back for the failed broadcast. However, I think this can safely be done as a follow up since it requires us to properly obtain granular errors so we can know if it's a case where we'll never be able to get the funding transaction into the mempool.

if err != nil {
fndgLog.Errorf("unable to broadcast funding "+
"txn: %v", err)
// We failed to broadcast the funding transaction, but watch
Copy link
Member

Choose a reason for hiding this comment

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

If we fail to broadcast the transaction, depending on what the failure is, we should remove it from the database immediately. The internal implementation of PublishTransaction for btcwallet will remove the transaction from the txstore if it fails to broadcast.

Copy link
Member

Choose a reason for hiding this comment

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

This is part of the issue we have atm, where if we can't ever get the node to accept our transaction, we account for the pending balance of the channel in channelbalance incorrectly as the utxo has already been marked as unspent again in the wallet internals.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to still store the tx in the db, but in a failed open state? That way we can not count them towards the max pending channels, but still resume if somehow they do confirm (and prune if something else uses the utxo). Seems dangerous to delete them entirely, as we wouldn't have the information to recover

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm worried that just deleting the channel from the DB completely at this point could mean loss of funds if the transaction actually do confirm. If we did, we basically would trust the chain backend to guarantee that a failure here means that the tx cannot ever "leak" to the network.

A better approach is, as we talked about, at this point increase the fee of the funding tx and rebroadcast. We can then go on, with all the state needed intact.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah ideally (assuming these other bugs are fixed), the only reason it's rejected is due to insufficient fees. With the fixes in btcwallet to allow replacement, etc we should no longer see cases where we accidentally double spend an input, or something similar.

alice, bob := setupFundingManagers(t, maxPending)
defer tearDownFundingManagers(t, alice, bob)

// Create openChaneReqs for maxPending+1 channels.
Copy link
Member

Choose a reason for hiding this comment

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

openChaneReqs -> openChanReqs

@Roasbeef Roasbeef removed the needs review PR needs review by regular contributors label Jul 31, 2018
Copy link
Contributor

@cfromknecht cfromknecht left a comment

Choose a reason for hiding this comment

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

LGTM apart from final decision on what to do on failure to publish. Great PR and easy to review!


// TestFundingManagerMaxPendingChannels checks that trying to open another
// channel with the same peer when MaxPending channels are pending fails.
func TestFundingManagerMaxPendingChannels(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome test 🔥

if err != nil {
fndgLog.Errorf("unable to broadcast funding "+
"txn: %v", err)
// We failed to broadcast the funding transaction, but watch
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to still store the tx in the db, but in a failed open state? That way we can not count them towards the max pending channels, but still resume if somehow they do confirm (and prune if something else uses the utxo). Seems dangerous to delete them entirely, as we wouldn't have the information to recover

This commit makes sure we delete a pending channel from the set of
activeReservations within the fundingmanager immediately after the
channel is moved to the openChannelBucket in the DB. Previously we
wouldn't do this before the funding tx was confirmed, making it possible
that failing the funding flow at a later point would try to cancel a
non-existent reservation context.
This commit moves the responsibility for publishing the funding tx to
the network from the wallet to the funding manager. This is done to
distinguish the failure of completing the reservation within the wallet
and failure of publishing the transaction.

Earlier we could fail to broadcast the transaction, which would cause us
to fail the funding flow. This is not something we can do directly,
since the CompeteReservation call will mark the channel IsPending in the
databas.e
@Roasbeef Roasbeef added this to the 0.5 milestone Aug 1, 2018
Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

LGTM. Perhaps we should mark the channel as pending even if we fail to broadcast just for the sake of maintaining the existing functionality?

@cfromknecht
Copy link
Contributor

@wpaulino that's not a bad idea, we could have a state such as PendingUnlikely to signal that we don't expect it to confirm, but acknowledge that it still can

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 🌈

@Roasbeef
Copy link
Member

Noted down to make issues for the set of follow up changes.

@Roasbeef Roasbeef merged commit d64bb59 into lightningnetwork:master Aug 10, 2018
@halseth
Copy link
Contributor Author

halseth commented Aug 10, 2018

Note that this change still will keep the channel marked as pending if the funding tx fails to publish; the difference is that we now can distinguish the publishing failure from other errors in CompleteReservation . Earlier we would try to fail the funding flow regardless of what failed within CompleteReservation, which wouldn't work for reservations that had successfully been moved into the openChannel bucket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
funding Related to the opening of new channels with funding transactions on the blockchain needs testing PR hasn't yet been actively tested on testnet/mainnet P2 should be fixed if one has time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants