channeldb: garbage collect link nodes with no open channels remaining

This commit is contained in:
Wilmer Paulino 2018-06-15 12:32:53 -07:00
parent 959618d596
commit 4578eec8a1
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -8,10 +8,10 @@ import (
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/coreos/bbolt"
"github.com/go-errors/errors"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/coreos/bbolt"
"github.com/go-errors/errors"
) )
const ( const (
@ -597,10 +597,39 @@ func (d *DB) MarkChanFullyClosed(chanPoint *wire.OutPoint) error {
return err return err
} }
return closedChanBucket.Put(chanID, newSummary.Bytes()) err = closedChanBucket.Put(chanID, newSummary.Bytes())
if err != nil {
return err
}
// Now that the channel is closed, we'll check if we have any
// other open channels with this peer. If we don't we'll
// garbage collect it to ensure we don't establish persistent
// connections to peers without open channels.
return d.pruneLinkNode(tx, chanSummary.RemotePub)
}) })
} }
// pruneLinkNode determines whether we should garbage collect a link node from
// the database due to no longer having any open channels with it. If there are
// any left, then this acts as a no-op.
func (db *DB) pruneLinkNode(tx *bolt.Tx, remotePub *btcec.PublicKey) error {
openChannels, err := db.fetchOpenChannels(tx, remotePub)
if err != nil {
return fmt.Errorf("unable to fetch open channels for peer %x: "+
"%v", remotePub.SerializeCompressed(), err)
}
if len(openChannels) > 0 {
return nil
}
log.Infof("Pruning link node %x with zero open channels from database",
remotePub.SerializeCompressed())
return db.deleteLinkNode(tx, remotePub)
}
// syncVersions function is used for safe db version synchronization. It // syncVersions function is used for safe db version synchronization. It
// applies migration functions to the current database and recovers the // applies migration functions to the current database and recovers the
// previous state of db if at least one error/panic appeared during migration. // previous state of db if at least one error/panic appeared during migration.