From 9294358b5be27fb2ccc049e44c141b7f5bcae972 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 6 Nov 2017 16:09:02 -0800 Subject: [PATCH] routing: populate the chain filter before calling syncGraphWithChain In this commit we fix an existing bug within the ChannelRouter. Before this commit, we would sync our graph prune state, *then* update the cain filter. This is incorrect as the blocks we manually pruned may have included channel closing transactions. As a result, we would miss the pruning of a set of channels, and assume that they were still active. In this commit, we fix this by reversing the order: we first update the chain filter and THEN sync the channel graph. --- routing/router.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/routing/router.go b/routing/router.go index 904aeade..5ebaae9e 100644 --- a/routing/router.go +++ b/routing/router.go @@ -273,14 +273,10 @@ func (r *ChannelRouter) Start() error { r.newBlocks = r.cfg.ChainView.FilteredBlocks() r.staleBlocks = r.cfg.ChainView.DisconnectedBlocks() - // Before we begin normal operation of the router, we first need to - // synchronize the channel graph to the latest state of the UTXO set. - if err := r.syncGraphWithChain(); err != nil { - return err - } - - // Once we've concluded our manual block pruning, we'll constrcut and + // Before we perform our manual block pruning, we'll construct and // apply a fresh chain filter to the active FilteredChainView instance. + // We do this before, as otherwise we may miss on-chain events as the + // filter hasn't properly been applied. channelView, err := r.cfg.Graph.ChannelView() if err != nil && err != channeldb.ErrGraphNoEdgesFound { return err @@ -291,6 +287,12 @@ func (r *ChannelRouter) Start() error { return err } + // Before we begin normal operation of the router, we first need to + // synchronize the channel graph to the latest state of the UTXO set. + if err := r.syncGraphWithChain(); err != nil { + return err + } + r.wg.Add(1) go r.networkHandler() @@ -371,7 +373,7 @@ func (r *ChannelRouter) syncGraphWithChain() error { log.Infof("channel graph is stale. Disconnecting block %v "+ "(hash=%v)", pruneHeight, pruneHash) // Prune the graph for every channel that was opened at height - // >= pruneHeigth. + // >= pruneHeight. _, err := r.cfg.Graph.DisconnectBlockAtHeight(pruneHeight) if err != nil { return err @@ -431,7 +433,8 @@ func (r *ChannelRouter) syncGraphWithChain() error { // With the spent outputs gathered, attempt to prune the // channel graph, also passing in the hash+height of the block // being pruned so the prune tip can be updated. - closedChans, err := r.cfg.Graph.PruneGraph(spentOutputs, nextHash, + closedChans, err := r.cfg.Graph.PruneGraph(spentOutputs, + nextHash, nextHeight) if err != nil { return err