@@ -1990,6 +1990,92 @@ func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
1990
1990
return channelID , nil
1991
1991
}
1992
1992
1993
+ // FetchChanInfos returns the set of channel edges that correspond to the passed
1994
+ // channel ID's. If an edge is the query is unknown to the database, it will
1995
+ // skipped and the result will contain only those edges that exist at the time
1996
+ // of the query. This can be used to respond to peer queries that are seeking to
1997
+ // fill in gaps in their view of the channel graph.
1998
+ //
1999
+ // NOTE: part of the V1Store interface.
2000
+ func (s * SQLStore ) FetchChanInfos (chanIDs []uint64 ) ([]ChannelEdge , error ) {
2001
+ var (
2002
+ ctx = context .TODO ()
2003
+ edges []ChannelEdge
2004
+ )
2005
+ err := s .db .ExecTx (ctx , sqldb .ReadTxOpt (), func (db SQLQueries ) error {
2006
+ for _ , chanID := range chanIDs {
2007
+ var chanIDB [8 ]byte
2008
+ byteOrder .PutUint64 (chanIDB [:], chanID )
2009
+
2010
+ // TODO(elle): potentially optimize this by using
2011
+ // sqlc.slice() once that works for both SQLite and
2012
+ // Postgres.
2013
+ row , err := db .GetChannelBySCIDWithPolicies (
2014
+ ctx , sqlc.GetChannelBySCIDWithPoliciesParams {
2015
+ Scid : chanIDB [:],
2016
+ Version : int16 (ProtocolV1 ),
2017
+ },
2018
+ )
2019
+ if errors .Is (err , sql .ErrNoRows ) {
2020
+ continue
2021
+ } else if err != nil {
2022
+ return fmt .Errorf ("unable to fetch channel: %w" ,
2023
+ err )
2024
+ }
2025
+
2026
+ node1 , node2 , err := buildNodes (
2027
+ ctx , db , row .Node , row .Node_2 ,
2028
+ )
2029
+ if err != nil {
2030
+ return fmt .Errorf ("unable to fetch nodes: %w" ,
2031
+ err )
2032
+ }
2033
+
2034
+ edge , err := getAndBuildEdgeInfo (
2035
+ ctx , db , s .cfg .ChainHash , row .Channel .ID ,
2036
+ row .Channel , node1 .PubKeyBytes ,
2037
+ node2 .PubKeyBytes ,
2038
+ )
2039
+ if err != nil {
2040
+ return fmt .Errorf ("unable to build " +
2041
+ "channel info: %w" , err )
2042
+ }
2043
+
2044
+ dbPol1 , dbPol2 , err := extractChannelPolicies (row )
2045
+ if err != nil {
2046
+ return fmt .Errorf ("unable to extract channel " +
2047
+ "policies: %w" , err )
2048
+ }
2049
+
2050
+ p1 , p2 , err := getAndBuildChanPolicies (
2051
+ ctx , db , dbPol1 , dbPol2 , edge .ChannelID ,
2052
+ node1 .PubKeyBytes , node2 .PubKeyBytes ,
2053
+ )
2054
+ if err != nil {
2055
+ return fmt .Errorf ("unable to build channel " +
2056
+ "policies: %w" , err )
2057
+ }
2058
+
2059
+ edges = append (edges , ChannelEdge {
2060
+ Info : edge ,
2061
+ Policy1 : p1 ,
2062
+ Policy2 : p2 ,
2063
+ Node1 : node1 ,
2064
+ Node2 : node2 ,
2065
+ })
2066
+ }
2067
+
2068
+ return nil
2069
+ }, func () {
2070
+ edges = nil
2071
+ })
2072
+ if err != nil {
2073
+ return nil , fmt .Errorf ("unable to fetch channels: %w" , err )
2074
+ }
2075
+
2076
+ return edges , nil
2077
+ }
2078
+
1993
2079
// FilterKnownChanIDs takes a set of channel IDs and return the subset of chan
1994
2080
// ID's that we don't know and are not known zombies of the passed set. In other
1995
2081
// words, we perform a set difference of our set of chan ID's and the ones
0 commit comments