graph/db: add buildCachedChanPolicies helper

Here we just remove some code duplication by adding a
buildCachedChanPolicies helper.
This commit is contained in:
Elle Mouton committed 2025-07-31 15:33:08 +02:00
1 parent 0f7c0ae9e7
commit 61299894e1
1 file changed
+45 -55
+45 -55
View File
@@ -1275,28 +1275,13 @@ func (s *SQLStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo,
return err return err
} }
var pol1, pol2 *models.CachedEdgePolicy pol1, pol2, err := buildCachedChanPolicies(
if dbPol1 != nil { dbPol1, dbPol2, edge.ChannelID, node1, node2,
policy1, err := buildChanPolicy(
*dbPol1, edge.ChannelID, nil, node2,
) )
if err != nil { if err != nil {
return err return err
} }
pol1 = models.NewCachedPolicy(policy1)
}
if dbPol2 != nil {
policy2, err := buildChanPolicy(
*dbPol2, edge.ChannelID, nil, node1,
)
if err != nil {
return err
}
pol2 = models.NewCachedPolicy(policy2)
}
if err := cb(edge, pol1, pol2); err != nil { if err := cb(edge, pol1, pol2); err != nil {
return err return err
} }
@@ -2646,7 +2631,8 @@ func (s *SQLStore) ChannelView() ([]EdgePoint, error) {
edgePoints []EdgePoint edgePoints []EdgePoint
) )
handleChannel := func(db SQLQueries, err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
handleChannel := func(_ context.Context,
channel sqlc.ListChannelsPaginatedRow) error { channel sqlc.ListChannelsPaginatedRow) error {
pkScript, err := genMultiSigP2WSH( pkScript, err := genMultiSigP2WSH(
@@ -2669,35 +2655,26 @@ func (s *SQLStore) ChannelView() ([]EdgePoint, error) {
return nil return nil
} }
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { queryFunc := func(ctx context.Context, lastID int64,
lastID := int64(-1) limit int32) ([]sqlc.ListChannelsPaginatedRow, error) {
for {
rows, err := db.ListChannelsPaginated( return db.ListChannelsPaginated(
ctx, sqlc.ListChannelsPaginatedParams{ ctx, sqlc.ListChannelsPaginatedParams{
Version: int16(ProtocolV1), Version: int16(ProtocolV1),
ID: lastID, ID: lastID,
Limit: s.cfg.QueryCfg.MaxPageSize, Limit: limit,
}, },
) )
if err != nil {
return err
} }
if len(rows) == 0 { extractCursor := func(row sqlc.ListChannelsPaginatedRow) int64 {
break return row.ID
} }
for _, row := range rows { return sqldb.ExecutePaginatedQuery(
err := handleChannel(db, row) ctx, s.cfg.QueryCfg, int64(-1), queryFunc,
if err != nil { extractCursor, handleChannel,
return err )
}
lastID = row.ID
}
}
return nil
}, func() { }, func() {
edgePoints = nil edgePoints = nil
}) })
@@ -3071,28 +3048,13 @@ func forEachNodeDirectedChannel(ctx context.Context, db SQLQueries,
return err return err
} }
var p1, p2 *models.CachedEdgePolicy p1, p2, err := buildCachedChanPolicies(
if dbPol1 != nil { dbPol1, dbPol2, edge.ChannelID, node1, node2,
policy1, err := buildChanPolicy(
*dbPol1, edge.ChannelID, nil, node2,
) )
if err != nil { if err != nil {
return err return err
} }
p1 = models.NewCachedPolicy(policy1)
}
if dbPol2 != nil {
policy2, err := buildChanPolicy(
*dbPol2, edge.ChannelID, nil, node1,
)
if err != nil {
return err
}
p2 = models.NewCachedPolicy(policy2)
}
// Determine the outgoing and incoming policy for this // Determine the outgoing and incoming policy for this
// channel and node combo. // channel and node combo.
outPolicy, inPolicy := p1, p2 outPolicy, inPolicy := p1, p2
@@ -4276,6 +4238,34 @@ func getAndBuildChanPolicies(ctx context.Context, db SQLQueries,
return pol1, pol2, nil return pol1, pol2, nil
} }
// buildCachedChanPolicies builds models.CachedEdgePolicy instances from the
// provided sqlc.GraphChannelPolicy objects. If a provided policy is nil,
// then nil is returned for it.
func buildCachedChanPolicies(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
channelID uint64, node1, node2 route.Vertex) (*models.CachedEdgePolicy,
*models.CachedEdgePolicy, error) {
var p1, p2 *models.CachedEdgePolicy
if dbPol1 != nil {
policy1, err := buildChanPolicy(*dbPol1, channelID, nil, node2)
if err != nil {
return nil, nil, err
}
p1 = models.NewCachedPolicy(policy1)
}
if dbPol2 != nil {
policy2, err := buildChanPolicy(*dbPol2, channelID, nil, node1)
if err != nil {
return nil, nil, err
}
p2 = models.NewCachedPolicy(policy2)
}
return p1, p2, nil
}
// buildChanPolicy builds a models.ChannelEdgePolicy instance from the // buildChanPolicy builds a models.ChannelEdgePolicy instance from the
// provided sqlc.GraphChannelPolicy and other required information. // provided sqlc.GraphChannelPolicy and other required information.
func buildChanPolicy(dbPolicy sqlc.GraphChannelPolicy, channelID uint64, func buildChanPolicy(dbPolicy sqlc.GraphChannelPolicy, channelID uint64,