Skip to content

Commit 23c8f70

Browse files
committed
graph/db: impl ForEachChannelCacheable
1 parent 34cb6a9 commit 23c8f70

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

docs/release-notes/release-notes-0.20.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ circuit. The indices are only available for forwarding events saved after v0.20.
8989
* [7](https://github.com/lightningnetwork/lnd/pull/9937)
9090
* [8](https://github.com/lightningnetwork/lnd/pull/9938)
9191
* [9](https://github.com/lightningnetwork/lnd/pull/9939)
92+
* [10](https://github.com/lightningnetwork/lnd/pull/9971)
9293

9394
## RPC Updates
9495

graph/db/sql_store.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,103 @@ func (s *SQLStore) ForEachNodeCached(cb func(node route.Vertex,
12381238
}, sqldb.NoOpReset)
12391239
}
12401240

1241+
// ForEachChannelCacheable iterates through all the channel edges stored
1242+
// within the graph and invokes the passed callback for each edge. The
1243+
// callback takes two edges as since this is a directed graph, both the
1244+
// in/out edges are visited. If the callback returns an error, then the
1245+
// transaction is aborted and the iteration stops early.
1246+
//
1247+
// NOTE: If an edge can't be found, or wasn't advertised, then a nil
1248+
// pointer for that particular channel edge routing policy will be
1249+
// passed into the callback.
1250+
//
1251+
// NOTE: this method is like ForEachChannel but fetches only the data
1252+
// required for the graph cache.
1253+
func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo,
1254+
*models.CachedEdgePolicy,
1255+
*models.CachedEdgePolicy) error) error {
1256+
1257+
ctx := context.TODO()
1258+
1259+
handleChannel := func(db SQLQueries,
1260+
row sqlc.ListChannelsWithPoliciesPaginatedRow) error {
1261+
1262+
node1, node2, err := buildNodeVertices(
1263+
row.Node1Pubkey, row.Node2Pubkey,
1264+
)
1265+
if err != nil {
1266+
return err
1267+
}
1268+
1269+
edge := buildCacheableChannelInfo(row.Channel, node1, node2)
1270+
1271+
dbPol1, dbPol2, err := extractChannelPolicies(row)
1272+
if err != nil {
1273+
return err
1274+
}
1275+
1276+
var pol1, pol2 *models.CachedEdgePolicy
1277+
if dbPol1 != nil {
1278+
policy1, err := buildChanPolicy(
1279+
*dbPol1, edge.ChannelID, nil, node2, true,
1280+
)
1281+
if err != nil {
1282+
return err
1283+
}
1284+
1285+
pol1 = models.NewCachedPolicy(policy1)
1286+
}
1287+
if dbPol2 != nil {
1288+
policy2, err := buildChanPolicy(
1289+
*dbPol2, edge.ChannelID, nil, node1, false,
1290+
)
1291+
if err != nil {
1292+
return err
1293+
}
1294+
1295+
pol2 = models.NewCachedPolicy(policy2)
1296+
}
1297+
1298+
if err := cb(edge, pol1, pol2); err != nil {
1299+
return err
1300+
}
1301+
1302+
return nil
1303+
}
1304+
1305+
return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
1306+
lastID := int64(-1)
1307+
for {
1308+
//nolint:ll
1309+
rows, err := db.ListChannelsWithPoliciesPaginated(
1310+
ctx, sqlc.ListChannelsWithPoliciesPaginatedParams{
1311+
Version: int16(ProtocolV1),
1312+
ID: lastID,
1313+
Limit: pageSize,
1314+
},
1315+
)
1316+
if err != nil {
1317+
return err
1318+
}
1319+
1320+
if len(rows) == 0 {
1321+
break
1322+
}
1323+
1324+
for _, row := range rows {
1325+
err := handleChannel(db, row)
1326+
if err != nil {
1327+
return err
1328+
}
1329+
1330+
lastID = row.Channel.ID
1331+
}
1332+
}
1333+
1334+
return nil
1335+
}, sqldb.NoOpReset)
1336+
}
1337+
12411338
// ForEachChannel iterates through all the channel edges stored within the
12421339
// graph and invokes the passed callback for each edge. The callback takes two
12431340
// edges as since this is a directed graph, both the in/out edges are visited.

0 commit comments

Comments
 (0)