Skip to content

Conversation

jimpo
Copy link
Contributor

@jimpo jimpo commented Nov 14, 2017

This refactors the transaction confirmation watching logic into a shared module and implements correct handling of chain reorganizations.

Depends on #360.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a dumb question, but isn't a reorg possible at a block that's not the latest? Should this call DisconnectTip at each block height between the current currentHeight and update.blockHeight (inclusive)?

Copy link
Contributor

Choose a reason for hiding this comment

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

btcd and neutrino reorgs both fire a disconnectblock notification for each block disconnected in a reorg. Makes it more convenient to work with than bitcoind's notifications, which only notify you of the latest block in such a situation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Admittedly I'm not totally familiar with how btcd callbacks fire, but if a reorg happens, I imagine that you'd probably get callbacks for new blocks and disconnected blocks around the same time. If the new block callback is processed first, won't this cause the block to get lost as the txConfNotifier doesn't know that a reorg has occurred, so will drop the update?

Copy link
Contributor

Choose a reason for hiding this comment

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

btcd fires disconnectblock notifications in reverse order, and then fires connectblock for each new connected block in order. The same goes for neutrino, which intentionally replicates the behavior.

Copy link
Contributor

@halseth halseth left a comment

Choose a reason for hiding this comment

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

Looks really good in my opinion!

Copy link
Contributor

Choose a reason for hiding this comment

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

notification

Copy link
Contributor

Choose a reason for hiding this comment

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

this can be written

select {
case <-confirmed:
case <-time.After(1 * time.Second):
}

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe check that height is at miner2's height?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean? Which height?

Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor

Choose a reason for hiding this comment

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

already

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add comment that this means the tx is unconfirmed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

delete instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

This if else statement needs some more comments, not obvious what's going on imho :P

Copy link
Contributor

Choose a reason for hiding this comment

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

an explanation would be nice

@jimpo jimpo force-pushed the notifier-reorg branch 2 times, most recently from 038cb70 to cc5a880 Compare November 27, 2017 20:30
Copy link
Member

Choose a reason for hiding this comment

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

We can eliminate the nesting here by inserting a continue after the first case completes.

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

Similar comment here about avoiding the nesting by using a continue after the first clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, technically true, but I don't think it makes sense in this context. The first block handles the case of a block connection, the second handles a block disconnection. The logic is symmetrical (like it would make sense if the if was !update.connect and the blocks were flipped), so I'd rather have them at the same level of indentation.

Copy link
Member

Choose a reason for hiding this comment

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

Removing the nesting will reduce the number of lines in the diff. It's not a block as it's mostly a style thing, but in the codebase we generally try to limit the degree of nesting in a given function.

Copy link
Member

Choose a reason for hiding this comment

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

The comments (and also the code itself) can be re-flowed now to take advantage of the increased column limit due to the elimination of the nesting.

Copy link
Member

Choose a reason for hiding this comment

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

A similar nesting optimization can be made here: we'd instead check for !ok above and continue if so.

Copy link
Member

Choose a reason for hiding this comment

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

This should also close a quit channel localized to the TxConfNotifier. All sends above would select on this quit channel to ensure things can be shutdown gracefully.

Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment here to indicate that this means the transaction wasn't yet found in the mempool? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Seems like there's no need for an additional type in this case? As is, it'll just replicate the existing struct with a diff name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is right, but I kept it for symmetry with neutrinonotify. I don't believe there's any overhead to having this wrapper struct.

Copy link
Member

Choose a reason for hiding this comment

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

First portion of sentence seems to be cut off?

Copy link
Member

Choose a reason for hiding this comment

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

👍

@jimpo jimpo force-pushed the notifier-reorg branch 2 times, most recently from e1af5da to a7a6e8a Compare December 6, 2017 22:54
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.

Nearly there, have one last round of comments.

I'll update one of my testnet nodes to start running this patch to see if any issues crop up over the next few days.

Copy link
Member

Choose a reason for hiding this comment

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

In this case, it seems like we shouldn't accept this block as it may cause us to miss notifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a continue after this line. The TxConfNotifier would have returned an error anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Ping on comment about eliminating unnecessary nesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I personally think the logic is less clear now, but it's your call.

Copy link
Member

Choose a reason for hiding this comment

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

Removing the nesting will reduce the number of lines in the diff. It's not a block as it's mostly a style thing, but in the codebase we generally try to limit the degree of nesting in a given function.

Copy link
Member

Choose a reason for hiding this comment

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

Master was recently updated to always log whenever a confirmation is dispatched. Ideally this patch replicates the existing set of logs exactly to mirror the prior operation.

Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified a great bit by using a second level of map which is based on the client ID.

Copy link
Member

Choose a reason for hiding this comment

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

Comments on these new added tests are virtually non existent.

Jim Posen added 11 commits December 10, 2017 11:27
Tests are failing for both btcd and neutrino notifiers.
This does not implement full handling of block disconnections, but
ensures that all chain updates are processed in order.
All implementations of the ChainNotifier interface support registering
notifications on transaction confirmations. This struct is intended to
be used internally by ChainNotifier implementations to handle much of
this logic.
Also fix overflow issue with reorg handling.
@jimpo jimpo force-pushed the notifier-reorg branch 2 times, most recently from a4c5985 to fa458c7 Compare December 10, 2017 19:32
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 Roasbeef merged commit bc7c834 into lightningnetwork:master Dec 15, 2017
@jimpo jimpo deleted the notifier-reorg branch December 15, 2017 03:23
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.

5 participants