channeldb: add new AddrsForNode method

In this commit, we add a new AddrsForNode method. This method will allow
a wrapper sturct to implement the new chanbackup.LiveChannelSource
method which is required to implement the full SCB feature set.
This commit is contained in:
Olaoluwa Osuntokun
2018-12-09 19:15:09 -08:00
parent e51c58e722
commit f4054d2a66
3 changed files with 150 additions and 29 deletions

View File

@@ -62,13 +62,13 @@ type LinkNode struct {
// NewLinkNode creates a new LinkNode from the provided parameters, which is
// backed by an instance of channeldb.
func (db *DB) NewLinkNode(bitNet wire.BitcoinNet, pub *btcec.PublicKey,
addr net.Addr) *LinkNode {
addrs ...net.Addr) *LinkNode {
return &LinkNode{
Network: bitNet,
IdentityPub: pub,
LastSeen: time.Now(),
Addresses: []net.Addr{addr},
Addresses: addrs,
db: db,
}
}
@@ -149,40 +149,44 @@ func (db *DB) deleteLinkNode(tx *bbolt.Tx, identity *btcec.PublicKey) error {
// identity public key. If a particular LinkNode for the passed identity public
// key cannot be found, then ErrNodeNotFound if returned.
func (db *DB) FetchLinkNode(identity *btcec.PublicKey) (*LinkNode, error) {
var (
node *LinkNode
err error
)
err = db.View(func(tx *bbolt.Tx) error {
// First fetch the bucket for storing node metadata, bailing
// out early if it hasn't been created yet.
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
if nodeMetaBucket == nil {
return ErrLinkNodesNotFound
var linkNode *LinkNode
err := db.View(func(tx *bbolt.Tx) error {
node, err := fetchLinkNode(tx, identity)
if err != nil {
return err
}
// If a link node for that particular public key cannot be
// located, then exit early with an ErrNodeNotFound.
pubKey := identity.SerializeCompressed()
nodeBytes := nodeMetaBucket.Get(pubKey)
if nodeBytes == nil {
return ErrNodeNotFound
}
// Finally, decode an allocate a fresh LinkNode object to be
// returned to the caller.
nodeReader := bytes.NewReader(nodeBytes)
node, err = deserializeLinkNode(nodeReader)
return err
linkNode = node
return nil
})
if err != nil {
return nil, err
return linkNode, err
}
func fetchLinkNode(tx *bbolt.Tx, targetPub *btcec.PublicKey) (*LinkNode, error) {
// First fetch the bucket for storing node metadata, bailing out early
// if it hasn't been created yet.
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
if nodeMetaBucket == nil {
return nil, ErrLinkNodesNotFound
}
return node, nil
// If a link node for that particular public key cannot be located,
// then exit early with an ErrNodeNotFound.
pubKey := targetPub.SerializeCompressed()
nodeBytes := nodeMetaBucket.Get(pubKey)
if nodeBytes == nil {
return nil, ErrNodeNotFound
}
// Finally, decode and allocate a fresh LinkNode object to be returned
// to the caller.
nodeReader := bytes.NewReader(nodeBytes)
return deserializeLinkNode(nodeReader)
}
// TODO(roasbeef): update link node addrs in server upon connection
// FetchAllLinkNodes starts a new database transaction to fetch all nodes with
// whom we have active channels with.
func (db *DB) FetchAllLinkNodes() ([]*LinkNode, error) {