From c5f97a17d539b2f17e28600c8ae1c3b40f352488 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 20 Jun 2016 21:41:00 -0700 Subject: [PATCH] channeldb: introduce FetchOpenChannels This commit introduces/re-writes a method FetchOpenChannels for channeldb, which returns all currently active/open channels for a particular peerID. --- channeldb/db.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index a647bd31..f384f6bc 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -10,6 +10,7 @@ import ( "github.com/boltdb/bolt" "github.com/roasbeef/btcd/chaincfg" + "github.com/roasbeef/btcd/wire" ) const ( @@ -129,8 +130,8 @@ func fileExists(path string) bool { } // FetchOpenChannel... -func (d *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) { - var channel *OpenChannel +func (d *DB) FetchOpenChannels(nodeID *wire.ShaHash) ([]*OpenChannel, error) { + var channels []*OpenChannel err := d.store.View(func(tx *bolt.Tx) error { // Get the bucket dedicated to storing the meta-data for open // channels. @@ -139,13 +140,39 @@ func (d *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) { return fmt.Errorf("open channel bucket does not exist") } - oChannel, err := fetchOpenChannel(openChanBucket, nodeID, d.cryptoSystem) + // Within this top level bucket, fetch the bucket dedicated to storing + // open channel data specific to the remote node. + nodeChanBucket := openChanBucket.Bucket(nodeID[:]) + if nodeChanBucket == nil { + return nil + } + + // Once we have the node's channel bucket, iterate through each + // item in the inner chan ID bucket. This bucket acts as an + // index for all channels we currently have open with this node. + nodeChanIDBucket := nodeChanBucket.Bucket(chanIDBucket[:]) + err := nodeChanIDBucket.ForEach(func(k, v []byte) error { + outBytes := bytes.NewReader(k) + chanID := &wire.OutPoint{} + if err := readOutpoint(outBytes, chanID); err != nil { + return err + } + + oChannel, err := fetchOpenChannel(openChanBucket, + nodeChanBucket, chanID, d.cryptoSystem) + if err != nil { + return err + } + + channels = append(channels, oChannel) + return nil + }) if err != nil { return err } - channel = oChannel + return nil }) - return channel, err + return channels, err }