Persist `ChannelMonitor`s after new blocks are connected
authorMatt Corallo <git@bluematt.me>
Wed, 13 Oct 2021 20:05:48 +0000 (20:05 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 20 Oct 2021 00:06:17 +0000 (00:06 +0000)
commit5c2ff2cb30ef1639c80b275eea209a289dd91b77
tree2e58a9a48bfb3dc8c9b94c003f1d5e5bcc352647
parent89ad05954891276dfb8524be904af78a8ec7ee82
Persist `ChannelMonitor`s after new blocks are connected

This resolves several user complaints (and issues in the sample
node) where startup is substantially delayed as we're always
waiting for the chain data to sync.

Further, in an upcoming PR, we'll be reloading pending payments
from ChannelMonitors on restart, at which point we'll need the
change here which avoids handling events until after the user
has confirmed the `ChannelMonitor` has been persisted to disk.
It will avoid a race where we
 * send a payment/HTLC (persisting the monitor to disk with the
   HTLC pending),
 * force-close the channel, removing the channel entry from the
   ChannelManager entirely,
 * persist the ChannelManager,
 * connect a block which contains a fulfill of the HTLC, generating
   a claim event,
 * handle the claim event while the `ChannelMonitor` is being
   persisted,
 * persist the ChannelManager (before the CHannelMonitor is
   persisted fully),
 * restart, reloading the HTLC as a pending payment in the
   ChannelManager, which now has no references to it except from
   the ChannelMonitor which still has the pending HTLC,
 * replay the block connection, generating a duplicate PaymentSent
   event.
fuzz/src/utils/test_persister.rs
lightning-persister/src/lib.rs
lightning/src/chain/chainmonitor.rs
lightning/src/chain/channelmonitor.rs
lightning/src/chain/mod.rs
lightning/src/ln/channelmanager.rs
lightning/src/util/test_utils.rs