mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
channeldb: add method to lookup a channel ID by it's funding outpoint
This commit adds a utility method whcih utilizes the edge index bucket and allows caller to look up the channel ID of a channel by its funding outpoint. This can be used to populate RPC’s with additional information and also to provide users with an additional query interface to build channel explorers, etc.
This commit is contained in:
parent
68ed4f3661
commit
1103e252be
@ -630,6 +630,42 @@ func (c *ChannelGraph) DeleteChannelEdge(chanPoint *wire.OutPoint) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ChannelID attempt to lookup the 8-byte compact channel ID which maps to the
|
||||
// passed channel point (outpoint). If the passed channel doesn't exist within
|
||||
// the database, then ErrEdgeNotFound is returned.
|
||||
func (c *ChannelGraph) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
|
||||
var chanID uint64
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := writeOutpoint(&b, chanPoint); err != nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if err := c.db.View(func(tx *bolt.Tx) error {
|
||||
edges := tx.Bucket(edgeBucket)
|
||||
if edges == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
chanIndex := edges.Bucket(channelPointBucket)
|
||||
if edges == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
|
||||
chanIDBytes := chanIndex.Get(b.Bytes())
|
||||
if chanIDBytes == nil {
|
||||
return ErrEdgeNotFound
|
||||
}
|
||||
|
||||
chanID = byteOrder.Uint64(chanIDBytes)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return chanID, nil
|
||||
}
|
||||
|
||||
func delChannelByEdge(edges *bolt.Bucket, edgeIndex *bolt.Bucket,
|
||||
chanIndex *bolt.Bucket, chanPoint *wire.OutPoint) error {
|
||||
var b bytes.Buffer
|
||||
|
@ -335,6 +335,17 @@ func TestEdgeInfoUpdates(t *testing.T) {
|
||||
t.Fatalf("graph should have of inserted edge")
|
||||
}
|
||||
|
||||
// We should also be able to retrieved the channelID only knowing the
|
||||
// channel point of the channel.
|
||||
dbChanID, err := graph.ChannelID(&outpoint)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to retrieve channel ID: %v", err)
|
||||
}
|
||||
if dbChanID != chanID {
|
||||
t.Fatalf("chan ID's mismatch, expected %v got %v", dbChanID,
|
||||
chanID)
|
||||
}
|
||||
|
||||
// With the edges inserted, perform some queries to ensure that they've
|
||||
// been inserted properly.
|
||||
dbEdge1, dbEdge2, err := graph.FetchChannelEdgesByID(chanID)
|
||||
|
Loading…
Reference in New Issue
Block a user