From 0aa1f39af8488f3b6c597aaeaa25406d0d0a06b9 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Tue, 17 Jul 2018 16:40:14 -0700 Subject: [PATCH] channeldb+server: prune link nodes on startup In this commit, we extend the server's functionality to prune link nodes on startup. Since we currently only decide whether to prune a link node from the database based on a channel close, it's possible that we have link nodes lingering from before this functionality was added on. --- channeldb/db.go | 20 ++++++++++++++++++++ server.go | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/channeldb/db.go b/channeldb/db.go index e33a37d5..4d6958a3 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -630,6 +630,26 @@ func (db *DB) pruneLinkNode(tx *bolt.Tx, remotePub *btcec.PublicKey) error { return db.deleteLinkNode(tx, remotePub) } +// PruneLinkNodes attempts to prune all link nodes found within the databse with +// whom we no longer have any open channels with. +func (db *DB) PruneLinkNodes() error { + return db.Update(func(tx *bolt.Tx) error { + linkNodes, err := db.fetchAllLinkNodes(tx) + if err != nil { + return err + } + + for _, linkNode := range linkNodes { + err := db.pruneLinkNode(tx, linkNode.IdentityPub) + if err != nil { + return err + } + } + + return nil + }) +} + // syncVersions function is used for safe db version synchronization. It // applies migration functions to the current database and recovers the // previous state of db if at least one error/panic appeared during migration. diff --git a/server.go b/server.go index 9234cb4e..e8de45b4 100644 --- a/server.go +++ b/server.go @@ -759,7 +759,12 @@ func (s *server) Start() error { // With all the relevant sub-systems started, we'll now attempt to // establish persistent connections to our direct channel collaborators - // within the network. + // within the network. Before doing so however, we'll prune our set of + // link nodes found within the database to ensure we don't reconnect to + // any nodes we no longer have open channels with. + if err := s.chanDB.PruneLinkNodes(); err != nil { + return err + } if err := s.establishPersistentConnections(); err != nil { return err }