• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 19080946931

04 Nov 2025 07:47PM UTC coverage: 66.65% (+0.009%) from 66.641%
19080946931

Pull #10340

github

web-flow
Merge c364eae9c into 4131a8e58
Pull Request #10340: graph: fix zombie chan pruning

9 of 25 new or added lines in 1 file covered. (36.0%)

291 existing lines in 18 files now uncovered.

137293 of 205990 relevant lines covered (66.65%)

21217.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sqldb/sqlc/graph.sql.go
1
// Code generated by sqlc. DO NOT EDIT.
2
// versions:
3
//   sqlc v1.29.0
4
// source: graph.sql
5

6
package sqlc
7

8
import (
9
        "context"
10
        "database/sql"
11
        "strings"
12
)
13

14
const addSourceNode = `-- name: AddSourceNode :exec
15
/* ─────────────────────────────────────────────
16
   graph_source_nodes table queries
17
   ─────────────────────────────────────────────
18
*/
19

20
INSERT INTO graph_source_nodes (node_id)
21
VALUES ($1)
22
ON CONFLICT (node_id) DO NOTHING
23
`
24

25
func (q *Queries) AddSourceNode(ctx context.Context, nodeID int64) error {
×
26
        _, err := q.db.ExecContext(ctx, addSourceNode, nodeID)
×
27
        return err
×
28
}
×
29

30
const addV1ChannelProof = `-- name: AddV1ChannelProof :execresult
31
UPDATE graph_channels
32
SET node_1_signature = $2,
33
    node_2_signature = $3,
34
    bitcoin_1_signature = $4,
35
    bitcoin_2_signature = $5
36
WHERE scid = $1
37
  AND version = 1
38
`
39

40
type AddV1ChannelProofParams struct {
41
        Scid              []byte
42
        Node1Signature    []byte
43
        Node2Signature    []byte
44
        Bitcoin1Signature []byte
45
        Bitcoin2Signature []byte
46
}
47

48
func (q *Queries) AddV1ChannelProof(ctx context.Context, arg AddV1ChannelProofParams) (sql.Result, error) {
×
49
        return q.db.ExecContext(ctx, addV1ChannelProof,
×
50
                arg.Scid,
×
51
                arg.Node1Signature,
×
52
                arg.Node2Signature,
×
53
                arg.Bitcoin1Signature,
×
54
                arg.Bitcoin2Signature,
×
55
        )
×
56
}
×
57

58
const countZombieChannels = `-- name: CountZombieChannels :one
59
SELECT COUNT(*)
60
FROM graph_zombie_channels
61
WHERE version = $1
62
`
63

64
func (q *Queries) CountZombieChannels(ctx context.Context, version int16) (int64, error) {
×
65
        row := q.db.QueryRowContext(ctx, countZombieChannels, version)
×
66
        var count int64
×
67
        err := row.Scan(&count)
×
68
        return count, err
×
69
}
×
70

71
const createChannel = `-- name: CreateChannel :one
72
/* ─────────────────────────────────────────────
73
   graph_channels table queries
74
   ─────────────────────────────────────────────
75
*/
76

77
INSERT INTO graph_channels (
78
    version, scid, node_id_1, node_id_2,
79
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
80
    node_1_signature, node_2_signature, bitcoin_1_signature,
81
    bitcoin_2_signature
82
) VALUES (
83
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
84
)
85
RETURNING id
86
`
87

88
type CreateChannelParams struct {
89
        Version           int16
90
        Scid              []byte
91
        NodeID1           int64
92
        NodeID2           int64
93
        Outpoint          string
94
        Capacity          sql.NullInt64
95
        BitcoinKey1       []byte
96
        BitcoinKey2       []byte
97
        Node1Signature    []byte
98
        Node2Signature    []byte
99
        Bitcoin1Signature []byte
100
        Bitcoin2Signature []byte
101
}
102

103
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (int64, error) {
×
104
        row := q.db.QueryRowContext(ctx, createChannel,
×
105
                arg.Version,
×
106
                arg.Scid,
×
107
                arg.NodeID1,
×
108
                arg.NodeID2,
×
109
                arg.Outpoint,
×
110
                arg.Capacity,
×
111
                arg.BitcoinKey1,
×
112
                arg.BitcoinKey2,
×
113
                arg.Node1Signature,
×
114
                arg.Node2Signature,
×
115
                arg.Bitcoin1Signature,
×
116
                arg.Bitcoin2Signature,
×
117
        )
×
118
        var id int64
×
119
        err := row.Scan(&id)
×
120
        return id, err
×
121
}
×
122

123
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
124
DELETE FROM graph_channel_policy_extra_types
125
WHERE channel_policy_id = $1
126
`
127

128
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
129
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
130
        return err
×
131
}
×
132

133
const deleteChannels = `-- name: DeleteChannels :exec
134
DELETE FROM graph_channels
135
WHERE id IN (/*SLICE:ids*/?)
136
`
137

138
func (q *Queries) DeleteChannels(ctx context.Context, ids []int64) error {
×
139
        query := deleteChannels
×
140
        var queryParams []interface{}
×
141
        if len(ids) > 0 {
×
142
                for _, v := range ids {
×
143
                        queryParams = append(queryParams, v)
×
144
                }
×
145
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
146
        } else {
×
147
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
148
        }
×
149
        _, err := q.db.ExecContext(ctx, query, queryParams...)
×
150
        return err
×
151
}
152

153
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
154
DELETE FROM graph_node_extra_types
155
WHERE node_id = $1
156
  AND type = $2
157
`
158

159
type DeleteExtraNodeTypeParams struct {
160
        NodeID int64
161
        Type   int64
162
}
163

164
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
165
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
166
        return err
×
167
}
×
168

169
const deleteNode = `-- name: DeleteNode :exec
170
DELETE FROM graph_nodes
171
WHERE id = $1
172
`
173

174
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
175
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
176
        return err
×
177
}
×
178

179
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
180
DELETE FROM graph_node_addresses
181
WHERE node_id = $1
182
`
183

184
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
185
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
186
        return err
×
187
}
×
188

189
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
190
DELETE FROM graph_nodes
191
WHERE pub_key = $1
192
  AND version = $2
193
`
194

195
type DeleteNodeByPubKeyParams struct {
196
        PubKey  []byte
197
        Version int16
198
}
199

200
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
201
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
202
}
×
203

204
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
205
DELETE FROM graph_node_features
206
WHERE node_id = $1
207
  AND feature_bit = $2
208
`
209

210
type DeleteNodeFeatureParams struct {
211
        NodeID     int64
212
        FeatureBit int32
213
}
214

215
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
216
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
217
        return err
×
218
}
×
219

220
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
221
DELETE FROM graph_prune_log
222
WHERE block_height >= $1
223
  AND block_height <= $2
224
`
225

226
type DeletePruneLogEntriesInRangeParams struct {
227
        StartHeight int64
228
        EndHeight   int64
229
}
230

231
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
232
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
233
        return err
×
234
}
×
235

236
const deleteUnconnectedNodes = `-- name: DeleteUnconnectedNodes :many
237
DELETE FROM graph_nodes
238
WHERE
239
    -- Ignore any of our source nodes.
240
    NOT EXISTS (
241
        SELECT 1
242
        FROM graph_source_nodes sn
243
        WHERE sn.node_id = graph_nodes.id
244
    )
245
    -- Select all nodes that do not have any channels.
246
    AND NOT EXISTS (
247
        SELECT 1
248
        FROM graph_channels c
249
        WHERE c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id
250
) RETURNING pub_key
251
`
252

253
func (q *Queries) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) {
×
254
        rows, err := q.db.QueryContext(ctx, deleteUnconnectedNodes)
×
255
        if err != nil {
×
256
                return nil, err
×
257
        }
×
258
        defer rows.Close()
×
259
        var items [][]byte
×
260
        for rows.Next() {
×
261
                var pub_key []byte
×
262
                if err := rows.Scan(&pub_key); err != nil {
×
263
                        return nil, err
×
264
                }
×
265
                items = append(items, pub_key)
×
266
        }
267
        if err := rows.Close(); err != nil {
×
268
                return nil, err
×
269
        }
×
270
        if err := rows.Err(); err != nil {
×
271
                return nil, err
×
272
        }
×
273
        return items, nil
×
274
}
275

276
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
277
DELETE FROM graph_zombie_channels
278
WHERE scid = $1
279
AND version = $2
280
`
281

282
type DeleteZombieChannelParams struct {
283
        Scid    []byte
284
        Version int16
285
}
286

287
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
288
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
289
}
×
290

291
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
292
SELECT
293
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
294
    n1.pub_key AS node1_pub_key,
295
    n2.pub_key AS node2_pub_key
296
FROM graph_channels c
297
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
298
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
299
WHERE c.scid = $1
300
  AND c.version = $2
301
`
302

303
type GetChannelAndNodesBySCIDParams struct {
304
        Scid    []byte
305
        Version int16
306
}
307

308
type GetChannelAndNodesBySCIDRow struct {
309
        ID                int64
310
        Version           int16
311
        Scid              []byte
312
        NodeID1           int64
313
        NodeID2           int64
314
        Outpoint          string
315
        Capacity          sql.NullInt64
316
        BitcoinKey1       []byte
317
        BitcoinKey2       []byte
318
        Node1Signature    []byte
319
        Node2Signature    []byte
320
        Bitcoin1Signature []byte
321
        Bitcoin2Signature []byte
322
        Node1PubKey       []byte
323
        Node2PubKey       []byte
324
}
325

326
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
327
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
328
        var i GetChannelAndNodesBySCIDRow
×
329
        err := row.Scan(
×
330
                &i.ID,
×
331
                &i.Version,
×
332
                &i.Scid,
×
333
                &i.NodeID1,
×
334
                &i.NodeID2,
×
335
                &i.Outpoint,
×
336
                &i.Capacity,
×
337
                &i.BitcoinKey1,
×
338
                &i.BitcoinKey2,
×
339
                &i.Node1Signature,
×
340
                &i.Node2Signature,
×
341
                &i.Bitcoin1Signature,
×
342
                &i.Bitcoin2Signature,
×
343
                &i.Node1PubKey,
×
344
                &i.Node2PubKey,
×
345
        )
×
346
        return i, err
×
347
}
×
348

349
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
350
SELECT
351
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
352

353
    n1.pub_key AS node1_pubkey,
354
    n2.pub_key AS node2_pubkey,
355

356
    -- Node 1 policy
357
    cp1.id AS policy_1_id,
358
    cp1.node_id AS policy_1_node_id,
359
    cp1.version AS policy_1_version,
360
    cp1.timelock AS policy_1_timelock,
361
    cp1.fee_ppm AS policy_1_fee_ppm,
362
    cp1.base_fee_msat AS policy_1_base_fee_msat,
363
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
364
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
365
    cp1.last_update AS policy_1_last_update,
366
    cp1.disabled AS policy_1_disabled,
367
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
368
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
369
    cp1.message_flags AS policy_1_message_flags,
370
    cp1.channel_flags AS policy_1_channel_flags,
371
    cp1.signature AS policy_1_signature,
372

373
    -- Node 2 policy
374
    cp2.id AS policy_2_id,
375
    cp2.node_id AS policy_2_node_id,
376
    cp2.version AS policy_2_version,
377
    cp2.timelock AS policy_2_timelock,
378
    cp2.fee_ppm AS policy_2_fee_ppm,
379
    cp2.base_fee_msat AS policy_2_base_fee_msat,
380
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
381
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
382
    cp2.last_update AS policy_2_last_update,
383
    cp2.disabled AS policy_2_disabled,
384
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
385
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
386
    cp2.message_flags AS policy_2_message_flags,
387
    cp2.channel_flags AS policy_2_channel_flags,
388
    cp2.signature AS policy_2_signature
389
FROM graph_channels c
390
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
391
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
392
    LEFT JOIN graph_channel_policies cp1
393
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
394
    LEFT JOIN graph_channel_policies cp2
395
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
396
WHERE c.outpoint = $1 AND c.version = $2
397
`
398

399
type GetChannelByOutpointWithPoliciesParams struct {
400
        Outpoint string
401
        Version  int16
402
}
403

404
type GetChannelByOutpointWithPoliciesRow struct {
405
        GraphChannel                   GraphChannel
406
        Node1Pubkey                    []byte
407
        Node2Pubkey                    []byte
408
        Policy1ID                      sql.NullInt64
409
        Policy1NodeID                  sql.NullInt64
410
        Policy1Version                 sql.NullInt16
411
        Policy1Timelock                sql.NullInt32
412
        Policy1FeePpm                  sql.NullInt64
413
        Policy1BaseFeeMsat             sql.NullInt64
414
        Policy1MinHtlcMsat             sql.NullInt64
415
        Policy1MaxHtlcMsat             sql.NullInt64
416
        Policy1LastUpdate              sql.NullInt64
417
        Policy1Disabled                sql.NullBool
418
        Policy1InboundBaseFeeMsat      sql.NullInt64
419
        Policy1InboundFeeRateMilliMsat sql.NullInt64
420
        Policy1MessageFlags            sql.NullInt16
421
        Policy1ChannelFlags            sql.NullInt16
422
        Policy1Signature               []byte
423
        Policy2ID                      sql.NullInt64
424
        Policy2NodeID                  sql.NullInt64
425
        Policy2Version                 sql.NullInt16
426
        Policy2Timelock                sql.NullInt32
427
        Policy2FeePpm                  sql.NullInt64
428
        Policy2BaseFeeMsat             sql.NullInt64
429
        Policy2MinHtlcMsat             sql.NullInt64
430
        Policy2MaxHtlcMsat             sql.NullInt64
431
        Policy2LastUpdate              sql.NullInt64
432
        Policy2Disabled                sql.NullBool
433
        Policy2InboundBaseFeeMsat      sql.NullInt64
434
        Policy2InboundFeeRateMilliMsat sql.NullInt64
435
        Policy2MessageFlags            sql.NullInt16
436
        Policy2ChannelFlags            sql.NullInt16
437
        Policy2Signature               []byte
438
}
439

440
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
441
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
442
        var i GetChannelByOutpointWithPoliciesRow
×
443
        err := row.Scan(
×
444
                &i.GraphChannel.ID,
×
445
                &i.GraphChannel.Version,
×
446
                &i.GraphChannel.Scid,
×
447
                &i.GraphChannel.NodeID1,
×
448
                &i.GraphChannel.NodeID2,
×
449
                &i.GraphChannel.Outpoint,
×
450
                &i.GraphChannel.Capacity,
×
451
                &i.GraphChannel.BitcoinKey1,
×
452
                &i.GraphChannel.BitcoinKey2,
×
453
                &i.GraphChannel.Node1Signature,
×
454
                &i.GraphChannel.Node2Signature,
×
455
                &i.GraphChannel.Bitcoin1Signature,
×
456
                &i.GraphChannel.Bitcoin2Signature,
×
457
                &i.Node1Pubkey,
×
458
                &i.Node2Pubkey,
×
459
                &i.Policy1ID,
×
460
                &i.Policy1NodeID,
×
461
                &i.Policy1Version,
×
462
                &i.Policy1Timelock,
×
463
                &i.Policy1FeePpm,
×
464
                &i.Policy1BaseFeeMsat,
×
465
                &i.Policy1MinHtlcMsat,
×
466
                &i.Policy1MaxHtlcMsat,
×
467
                &i.Policy1LastUpdate,
×
468
                &i.Policy1Disabled,
×
469
                &i.Policy1InboundBaseFeeMsat,
×
470
                &i.Policy1InboundFeeRateMilliMsat,
×
471
                &i.Policy1MessageFlags,
×
472
                &i.Policy1ChannelFlags,
×
473
                &i.Policy1Signature,
×
474
                &i.Policy2ID,
×
475
                &i.Policy2NodeID,
×
476
                &i.Policy2Version,
×
477
                &i.Policy2Timelock,
×
478
                &i.Policy2FeePpm,
×
479
                &i.Policy2BaseFeeMsat,
×
480
                &i.Policy2MinHtlcMsat,
×
481
                &i.Policy2MaxHtlcMsat,
×
482
                &i.Policy2LastUpdate,
×
483
                &i.Policy2Disabled,
×
484
                &i.Policy2InboundBaseFeeMsat,
×
485
                &i.Policy2InboundFeeRateMilliMsat,
×
486
                &i.Policy2MessageFlags,
×
487
                &i.Policy2ChannelFlags,
×
488
                &i.Policy2Signature,
×
489
        )
×
490
        return i, err
×
491
}
×
492

493
const getChannelBySCID = `-- name: GetChannelBySCID :one
494
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature FROM graph_channels
495
WHERE scid = $1 AND version = $2
496
`
497

498
type GetChannelBySCIDParams struct {
499
        Scid    []byte
500
        Version int16
501
}
502

503
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error) {
×
504
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
505
        var i GraphChannel
×
506
        err := row.Scan(
×
507
                &i.ID,
×
508
                &i.Version,
×
509
                &i.Scid,
×
510
                &i.NodeID1,
×
511
                &i.NodeID2,
×
512
                &i.Outpoint,
×
513
                &i.Capacity,
×
514
                &i.BitcoinKey1,
×
515
                &i.BitcoinKey2,
×
516
                &i.Node1Signature,
×
517
                &i.Node2Signature,
×
518
                &i.Bitcoin1Signature,
×
519
                &i.Bitcoin2Signature,
×
520
        )
×
521
        return i, err
×
522
}
×
523

524
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
525
SELECT
526
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
527
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
528
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
529

530
    -- Policy 1
531
    cp1.id AS policy1_id,
532
    cp1.node_id AS policy1_node_id,
533
    cp1.version AS policy1_version,
534
    cp1.timelock AS policy1_timelock,
535
    cp1.fee_ppm AS policy1_fee_ppm,
536
    cp1.base_fee_msat AS policy1_base_fee_msat,
537
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
538
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
539
    cp1.last_update AS policy1_last_update,
540
    cp1.disabled AS policy1_disabled,
541
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
542
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
543
    cp1.message_flags AS policy1_message_flags,
544
    cp1.channel_flags AS policy1_channel_flags,
545
    cp1.signature AS policy1_signature,
546

547
    -- Policy 2
548
    cp2.id AS policy2_id,
549
    cp2.node_id AS policy2_node_id,
550
    cp2.version AS policy2_version,
551
    cp2.timelock AS policy2_timelock,
552
    cp2.fee_ppm AS policy2_fee_ppm,
553
    cp2.base_fee_msat AS policy2_base_fee_msat,
554
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
555
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
556
    cp2.last_update AS policy2_last_update,
557
    cp2.disabled AS policy2_disabled,
558
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
559
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
560
    cp2.message_flags AS policy_2_message_flags,
561
    cp2.channel_flags AS policy_2_channel_flags,
562
    cp2.signature AS policy2_signature
563

564
FROM graph_channels c
565
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
566
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
567
    LEFT JOIN graph_channel_policies cp1
568
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
569
    LEFT JOIN graph_channel_policies cp2
570
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
571
WHERE c.scid = $1
572
  AND c.version = $2
573
`
574

575
type GetChannelBySCIDWithPoliciesParams struct {
576
        Scid    []byte
577
        Version int16
578
}
579

580
type GetChannelBySCIDWithPoliciesRow struct {
581
        GraphChannel                   GraphChannel
582
        GraphNode                      GraphNode
583
        GraphNode_2                    GraphNode
584
        Policy1ID                      sql.NullInt64
585
        Policy1NodeID                  sql.NullInt64
586
        Policy1Version                 sql.NullInt16
587
        Policy1Timelock                sql.NullInt32
588
        Policy1FeePpm                  sql.NullInt64
589
        Policy1BaseFeeMsat             sql.NullInt64
590
        Policy1MinHtlcMsat             sql.NullInt64
591
        Policy1MaxHtlcMsat             sql.NullInt64
592
        Policy1LastUpdate              sql.NullInt64
593
        Policy1Disabled                sql.NullBool
594
        Policy1InboundBaseFeeMsat      sql.NullInt64
595
        Policy1InboundFeeRateMilliMsat sql.NullInt64
596
        Policy1MessageFlags            sql.NullInt16
597
        Policy1ChannelFlags            sql.NullInt16
598
        Policy1Signature               []byte
599
        Policy2ID                      sql.NullInt64
600
        Policy2NodeID                  sql.NullInt64
601
        Policy2Version                 sql.NullInt16
602
        Policy2Timelock                sql.NullInt32
603
        Policy2FeePpm                  sql.NullInt64
604
        Policy2BaseFeeMsat             sql.NullInt64
605
        Policy2MinHtlcMsat             sql.NullInt64
606
        Policy2MaxHtlcMsat             sql.NullInt64
607
        Policy2LastUpdate              sql.NullInt64
608
        Policy2Disabled                sql.NullBool
609
        Policy2InboundBaseFeeMsat      sql.NullInt64
610
        Policy2InboundFeeRateMilliMsat sql.NullInt64
611
        Policy2MessageFlags            sql.NullInt16
612
        Policy2ChannelFlags            sql.NullInt16
613
        Policy2Signature               []byte
614
}
615

616
func (q *Queries) GetChannelBySCIDWithPolicies(ctx context.Context, arg GetChannelBySCIDWithPoliciesParams) (GetChannelBySCIDWithPoliciesRow, error) {
×
617
        row := q.db.QueryRowContext(ctx, getChannelBySCIDWithPolicies, arg.Scid, arg.Version)
×
618
        var i GetChannelBySCIDWithPoliciesRow
×
619
        err := row.Scan(
×
620
                &i.GraphChannel.ID,
×
621
                &i.GraphChannel.Version,
×
622
                &i.GraphChannel.Scid,
×
623
                &i.GraphChannel.NodeID1,
×
624
                &i.GraphChannel.NodeID2,
×
625
                &i.GraphChannel.Outpoint,
×
626
                &i.GraphChannel.Capacity,
×
627
                &i.GraphChannel.BitcoinKey1,
×
628
                &i.GraphChannel.BitcoinKey2,
×
629
                &i.GraphChannel.Node1Signature,
×
630
                &i.GraphChannel.Node2Signature,
×
631
                &i.GraphChannel.Bitcoin1Signature,
×
632
                &i.GraphChannel.Bitcoin2Signature,
×
633
                &i.GraphNode.ID,
×
634
                &i.GraphNode.Version,
×
635
                &i.GraphNode.PubKey,
×
636
                &i.GraphNode.Alias,
×
637
                &i.GraphNode.LastUpdate,
×
638
                &i.GraphNode.Color,
×
639
                &i.GraphNode.Signature,
×
640
                &i.GraphNode_2.ID,
×
641
                &i.GraphNode_2.Version,
×
642
                &i.GraphNode_2.PubKey,
×
643
                &i.GraphNode_2.Alias,
×
644
                &i.GraphNode_2.LastUpdate,
×
645
                &i.GraphNode_2.Color,
×
646
                &i.GraphNode_2.Signature,
×
647
                &i.Policy1ID,
×
648
                &i.Policy1NodeID,
×
649
                &i.Policy1Version,
×
650
                &i.Policy1Timelock,
×
651
                &i.Policy1FeePpm,
×
652
                &i.Policy1BaseFeeMsat,
×
653
                &i.Policy1MinHtlcMsat,
×
654
                &i.Policy1MaxHtlcMsat,
×
655
                &i.Policy1LastUpdate,
×
656
                &i.Policy1Disabled,
×
657
                &i.Policy1InboundBaseFeeMsat,
×
658
                &i.Policy1InboundFeeRateMilliMsat,
×
659
                &i.Policy1MessageFlags,
×
660
                &i.Policy1ChannelFlags,
×
661
                &i.Policy1Signature,
×
662
                &i.Policy2ID,
×
663
                &i.Policy2NodeID,
×
664
                &i.Policy2Version,
×
665
                &i.Policy2Timelock,
×
666
                &i.Policy2FeePpm,
×
667
                &i.Policy2BaseFeeMsat,
×
668
                &i.Policy2MinHtlcMsat,
×
669
                &i.Policy2MaxHtlcMsat,
×
670
                &i.Policy2LastUpdate,
×
671
                &i.Policy2Disabled,
×
672
                &i.Policy2InboundBaseFeeMsat,
×
673
                &i.Policy2InboundFeeRateMilliMsat,
×
674
                &i.Policy2MessageFlags,
×
675
                &i.Policy2ChannelFlags,
×
676
                &i.Policy2Signature,
×
677
        )
×
678
        return i, err
×
679
}
×
680

681
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
682
SELECT
683
    channel_id,
684
    type,
685
    value
686
FROM graph_channel_extra_types
687
WHERE channel_id IN (/*SLICE:chan_ids*/?)
688
ORDER BY channel_id, type
689
`
690

691
func (q *Queries) GetChannelExtrasBatch(ctx context.Context, chanIds []int64) ([]GraphChannelExtraType, error) {
×
692
        query := getChannelExtrasBatch
×
693
        var queryParams []interface{}
×
694
        if len(chanIds) > 0 {
×
695
                for _, v := range chanIds {
×
696
                        queryParams = append(queryParams, v)
×
697
                }
×
698
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
699
        } else {
×
700
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
701
        }
×
702
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
703
        if err != nil {
×
704
                return nil, err
×
705
        }
×
706
        defer rows.Close()
×
707
        var items []GraphChannelExtraType
×
708
        for rows.Next() {
×
709
                var i GraphChannelExtraType
×
710
                if err := rows.Scan(&i.ChannelID, &i.Type, &i.Value); err != nil {
×
711
                        return nil, err
×
712
                }
×
713
                items = append(items, i)
×
714
        }
715
        if err := rows.Close(); err != nil {
×
716
                return nil, err
×
717
        }
×
718
        if err := rows.Err(); err != nil {
×
719
                return nil, err
×
720
        }
×
721
        return items, nil
×
722
}
723

724
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
725
SELECT
726
    channel_id,
727
    feature_bit
728
FROM graph_channel_features
729
WHERE channel_id IN (/*SLICE:chan_ids*/?)
730
ORDER BY channel_id, feature_bit
731
`
732

733
func (q *Queries) GetChannelFeaturesBatch(ctx context.Context, chanIds []int64) ([]GraphChannelFeature, error) {
×
734
        query := getChannelFeaturesBatch
×
735
        var queryParams []interface{}
×
736
        if len(chanIds) > 0 {
×
737
                for _, v := range chanIds {
×
738
                        queryParams = append(queryParams, v)
×
739
                }
×
740
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
741
        } else {
×
742
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
743
        }
×
744
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
745
        if err != nil {
×
746
                return nil, err
×
747
        }
×
748
        defer rows.Close()
×
749
        var items []GraphChannelFeature
×
750
        for rows.Next() {
×
751
                var i GraphChannelFeature
×
752
                if err := rows.Scan(&i.ChannelID, &i.FeatureBit); err != nil {
×
753
                        return nil, err
×
754
                }
×
755
                items = append(items, i)
×
756
        }
757
        if err := rows.Close(); err != nil {
×
758
                return nil, err
×
759
        }
×
760
        if err := rows.Err(); err != nil {
×
761
                return nil, err
×
762
        }
×
763
        return items, nil
×
764
}
765

766
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
767
SELECT id, version, channel_id, node_id, timelock, fee_ppm, base_fee_msat, min_htlc_msat, max_htlc_msat, last_update, disabled, inbound_base_fee_msat, inbound_fee_rate_milli_msat, message_flags, channel_flags, signature
768
FROM graph_channel_policies
769
WHERE channel_id = $1
770
  AND node_id = $2
771
  AND version = $3
772
`
773

774
type GetChannelPolicyByChannelAndNodeParams struct {
775
        ChannelID int64
776
        NodeID    int64
777
        Version   int16
778
}
779

780
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (GraphChannelPolicy, error) {
×
781
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
782
        var i GraphChannelPolicy
×
783
        err := row.Scan(
×
784
                &i.ID,
×
785
                &i.Version,
×
786
                &i.ChannelID,
×
787
                &i.NodeID,
×
788
                &i.Timelock,
×
789
                &i.FeePpm,
×
790
                &i.BaseFeeMsat,
×
791
                &i.MinHtlcMsat,
×
792
                &i.MaxHtlcMsat,
×
793
                &i.LastUpdate,
×
794
                &i.Disabled,
×
795
                &i.InboundBaseFeeMsat,
×
796
                &i.InboundFeeRateMilliMsat,
×
797
                &i.MessageFlags,
×
798
                &i.ChannelFlags,
×
799
                &i.Signature,
×
800
        )
×
801
        return i, err
×
802
}
×
803

804
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
805
SELECT
806
    channel_policy_id as policy_id,
807
    type,
808
    value
809
FROM graph_channel_policy_extra_types
810
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
811
ORDER BY channel_policy_id, type
812
`
813

814
type GetChannelPolicyExtraTypesBatchRow struct {
815
        PolicyID int64
816
        Type     int64
817
        Value    []byte
818
}
819

820
func (q *Queries) GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]GetChannelPolicyExtraTypesBatchRow, error) {
×
821
        query := getChannelPolicyExtraTypesBatch
×
822
        var queryParams []interface{}
×
823
        if len(policyIds) > 0 {
×
824
                for _, v := range policyIds {
×
825
                        queryParams = append(queryParams, v)
×
826
                }
×
827
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", makeQueryParams(len(queryParams), len(policyIds)), 1)
×
828
        } else {
×
829
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", "NULL", 1)
×
830
        }
×
831
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
832
        if err != nil {
×
833
                return nil, err
×
834
        }
×
835
        defer rows.Close()
×
836
        var items []GetChannelPolicyExtraTypesBatchRow
×
837
        for rows.Next() {
×
838
                var i GetChannelPolicyExtraTypesBatchRow
×
839
                if err := rows.Scan(&i.PolicyID, &i.Type, &i.Value); err != nil {
×
840
                        return nil, err
×
841
                }
×
842
                items = append(items, i)
×
843
        }
844
        if err := rows.Close(); err != nil {
×
845
                return nil, err
×
846
        }
×
847
        if err := rows.Err(); err != nil {
×
848
                return nil, err
×
849
        }
×
850
        return items, nil
×
851
}
852

853
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
854
SELECT
855
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
856

857
    -- Minimal node data.
858
    n1.id AS node1_id,
859
    n1.pub_key AS node1_pub_key,
860
    n2.id AS node2_id,
861
    n2.pub_key AS node2_pub_key,
862

863
    -- Policy 1
864
    cp1.id AS policy1_id,
865
    cp1.node_id AS policy1_node_id,
866
    cp1.version AS policy1_version,
867
    cp1.timelock AS policy1_timelock,
868
    cp1.fee_ppm AS policy1_fee_ppm,
869
    cp1.base_fee_msat AS policy1_base_fee_msat,
870
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
871
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
872
    cp1.last_update AS policy1_last_update,
873
    cp1.disabled AS policy1_disabled,
874
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
875
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
876
    cp1.message_flags AS policy1_message_flags,
877
    cp1.channel_flags AS policy1_channel_flags,
878
    cp1.signature AS policy1_signature,
879

880
    -- Policy 2
881
    cp2.id AS policy2_id,
882
    cp2.node_id AS policy2_node_id,
883
    cp2.version AS policy2_version,
884
    cp2.timelock AS policy2_timelock,
885
    cp2.fee_ppm AS policy2_fee_ppm,
886
    cp2.base_fee_msat AS policy2_base_fee_msat,
887
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
888
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
889
    cp2.last_update AS policy2_last_update,
890
    cp2.disabled AS policy2_disabled,
891
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
892
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
893
    cp2.message_flags AS policy2_message_flags,
894
    cp2.channel_flags AS policy2_channel_flags,
895
    cp2.signature AS policy2_signature
896

897
FROM graph_channels c
898
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
899
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
900
    LEFT JOIN graph_channel_policies cp1
901
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
902
    LEFT JOIN graph_channel_policies cp2
903
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
904
WHERE c.id IN (/*SLICE:ids*/?)
905
`
906

907
type GetChannelsByIDsRow struct {
908
        GraphChannel                   GraphChannel
909
        Node1ID                        int64
910
        Node1PubKey                    []byte
911
        Node2ID                        int64
912
        Node2PubKey                    []byte
913
        Policy1ID                      sql.NullInt64
914
        Policy1NodeID                  sql.NullInt64
915
        Policy1Version                 sql.NullInt16
916
        Policy1Timelock                sql.NullInt32
917
        Policy1FeePpm                  sql.NullInt64
918
        Policy1BaseFeeMsat             sql.NullInt64
919
        Policy1MinHtlcMsat             sql.NullInt64
920
        Policy1MaxHtlcMsat             sql.NullInt64
921
        Policy1LastUpdate              sql.NullInt64
922
        Policy1Disabled                sql.NullBool
923
        Policy1InboundBaseFeeMsat      sql.NullInt64
924
        Policy1InboundFeeRateMilliMsat sql.NullInt64
925
        Policy1MessageFlags            sql.NullInt16
926
        Policy1ChannelFlags            sql.NullInt16
927
        Policy1Signature               []byte
928
        Policy2ID                      sql.NullInt64
929
        Policy2NodeID                  sql.NullInt64
930
        Policy2Version                 sql.NullInt16
931
        Policy2Timelock                sql.NullInt32
932
        Policy2FeePpm                  sql.NullInt64
933
        Policy2BaseFeeMsat             sql.NullInt64
934
        Policy2MinHtlcMsat             sql.NullInt64
935
        Policy2MaxHtlcMsat             sql.NullInt64
936
        Policy2LastUpdate              sql.NullInt64
937
        Policy2Disabled                sql.NullBool
938
        Policy2InboundBaseFeeMsat      sql.NullInt64
939
        Policy2InboundFeeRateMilliMsat sql.NullInt64
940
        Policy2MessageFlags            sql.NullInt16
941
        Policy2ChannelFlags            sql.NullInt16
942
        Policy2Signature               []byte
943
}
944

945
func (q *Queries) GetChannelsByIDs(ctx context.Context, ids []int64) ([]GetChannelsByIDsRow, error) {
×
946
        query := getChannelsByIDs
×
947
        var queryParams []interface{}
×
948
        if len(ids) > 0 {
×
949
                for _, v := range ids {
×
950
                        queryParams = append(queryParams, v)
×
951
                }
×
952
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
953
        } else {
×
954
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
955
        }
×
956
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
957
        if err != nil {
×
958
                return nil, err
×
959
        }
×
960
        defer rows.Close()
×
961
        var items []GetChannelsByIDsRow
×
962
        for rows.Next() {
×
963
                var i GetChannelsByIDsRow
×
964
                if err := rows.Scan(
×
965
                        &i.GraphChannel.ID,
×
966
                        &i.GraphChannel.Version,
×
967
                        &i.GraphChannel.Scid,
×
968
                        &i.GraphChannel.NodeID1,
×
969
                        &i.GraphChannel.NodeID2,
×
970
                        &i.GraphChannel.Outpoint,
×
971
                        &i.GraphChannel.Capacity,
×
972
                        &i.GraphChannel.BitcoinKey1,
×
973
                        &i.GraphChannel.BitcoinKey2,
×
974
                        &i.GraphChannel.Node1Signature,
×
975
                        &i.GraphChannel.Node2Signature,
×
976
                        &i.GraphChannel.Bitcoin1Signature,
×
977
                        &i.GraphChannel.Bitcoin2Signature,
×
978
                        &i.Node1ID,
×
979
                        &i.Node1PubKey,
×
980
                        &i.Node2ID,
×
981
                        &i.Node2PubKey,
×
982
                        &i.Policy1ID,
×
983
                        &i.Policy1NodeID,
×
984
                        &i.Policy1Version,
×
985
                        &i.Policy1Timelock,
×
986
                        &i.Policy1FeePpm,
×
987
                        &i.Policy1BaseFeeMsat,
×
988
                        &i.Policy1MinHtlcMsat,
×
989
                        &i.Policy1MaxHtlcMsat,
×
990
                        &i.Policy1LastUpdate,
×
991
                        &i.Policy1Disabled,
×
992
                        &i.Policy1InboundBaseFeeMsat,
×
993
                        &i.Policy1InboundFeeRateMilliMsat,
×
994
                        &i.Policy1MessageFlags,
×
995
                        &i.Policy1ChannelFlags,
×
996
                        &i.Policy1Signature,
×
997
                        &i.Policy2ID,
×
998
                        &i.Policy2NodeID,
×
999
                        &i.Policy2Version,
×
1000
                        &i.Policy2Timelock,
×
1001
                        &i.Policy2FeePpm,
×
1002
                        &i.Policy2BaseFeeMsat,
×
1003
                        &i.Policy2MinHtlcMsat,
×
1004
                        &i.Policy2MaxHtlcMsat,
×
1005
                        &i.Policy2LastUpdate,
×
1006
                        &i.Policy2Disabled,
×
1007
                        &i.Policy2InboundBaseFeeMsat,
×
1008
                        &i.Policy2InboundFeeRateMilliMsat,
×
1009
                        &i.Policy2MessageFlags,
×
1010
                        &i.Policy2ChannelFlags,
×
1011
                        &i.Policy2Signature,
×
1012
                ); err != nil {
×
1013
                        return nil, err
×
1014
                }
×
1015
                items = append(items, i)
×
1016
        }
1017
        if err := rows.Close(); err != nil {
×
1018
                return nil, err
×
1019
        }
×
1020
        if err := rows.Err(); err != nil {
×
1021
                return nil, err
×
1022
        }
×
1023
        return items, nil
×
1024
}
1025

1026
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1027
SELECT
1028
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
1029
    n1.pub_key AS node1_pubkey,
1030
    n2.pub_key AS node2_pubkey
1031
FROM graph_channels c
1032
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1033
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1034
WHERE c.outpoint IN
1035
    (/*SLICE:outpoints*/?)
1036
`
1037

1038
type GetChannelsByOutpointsRow struct {
1039
        GraphChannel GraphChannel
1040
        Node1Pubkey  []byte
1041
        Node2Pubkey  []byte
1042
}
1043

1044
func (q *Queries) GetChannelsByOutpoints(ctx context.Context, outpoints []string) ([]GetChannelsByOutpointsRow, error) {
×
1045
        query := getChannelsByOutpoints
×
1046
        var queryParams []interface{}
×
1047
        if len(outpoints) > 0 {
×
1048
                for _, v := range outpoints {
×
1049
                        queryParams = append(queryParams, v)
×
1050
                }
×
1051
                query = strings.Replace(query, "/*SLICE:outpoints*/?", makeQueryParams(len(queryParams), len(outpoints)), 1)
×
1052
        } else {
×
1053
                query = strings.Replace(query, "/*SLICE:outpoints*/?", "NULL", 1)
×
1054
        }
×
1055
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1056
        if err != nil {
×
1057
                return nil, err
×
1058
        }
×
1059
        defer rows.Close()
×
1060
        var items []GetChannelsByOutpointsRow
×
1061
        for rows.Next() {
×
1062
                var i GetChannelsByOutpointsRow
×
1063
                if err := rows.Scan(
×
1064
                        &i.GraphChannel.ID,
×
1065
                        &i.GraphChannel.Version,
×
1066
                        &i.GraphChannel.Scid,
×
1067
                        &i.GraphChannel.NodeID1,
×
1068
                        &i.GraphChannel.NodeID2,
×
1069
                        &i.GraphChannel.Outpoint,
×
1070
                        &i.GraphChannel.Capacity,
×
1071
                        &i.GraphChannel.BitcoinKey1,
×
1072
                        &i.GraphChannel.BitcoinKey2,
×
1073
                        &i.GraphChannel.Node1Signature,
×
1074
                        &i.GraphChannel.Node2Signature,
×
1075
                        &i.GraphChannel.Bitcoin1Signature,
×
1076
                        &i.GraphChannel.Bitcoin2Signature,
×
1077
                        &i.Node1Pubkey,
×
1078
                        &i.Node2Pubkey,
×
1079
                ); err != nil {
×
1080
                        return nil, err
×
1081
                }
×
1082
                items = append(items, i)
×
1083
        }
1084
        if err := rows.Close(); err != nil {
×
1085
                return nil, err
×
1086
        }
×
1087
        if err := rows.Err(); err != nil {
×
1088
                return nil, err
×
1089
        }
×
1090
        return items, nil
×
1091
}
1092

1093
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1094
SELECT
1095
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
1096
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
1097
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
1098

1099
    -- Policy 1 (node_id_1)
1100
    cp1.id AS policy1_id,
1101
    cp1.node_id AS policy1_node_id,
1102
    cp1.version AS policy1_version,
1103
    cp1.timelock AS policy1_timelock,
1104
    cp1.fee_ppm AS policy1_fee_ppm,
1105
    cp1.base_fee_msat AS policy1_base_fee_msat,
1106
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1107
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1108
    cp1.last_update AS policy1_last_update,
1109
    cp1.disabled AS policy1_disabled,
1110
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1111
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1112
    cp1.message_flags AS policy1_message_flags,
1113
    cp1.channel_flags AS policy1_channel_flags,
1114
    cp1.signature AS policy1_signature,
1115

1116
    -- Policy 2 (node_id_2)
1117
    cp2.id AS policy2_id,
1118
    cp2.node_id AS policy2_node_id,
1119
    cp2.version AS policy2_version,
1120
    cp2.timelock AS policy2_timelock,
1121
    cp2.fee_ppm AS policy2_fee_ppm,
1122
    cp2.base_fee_msat AS policy2_base_fee_msat,
1123
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1124
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1125
    cp2.last_update AS policy2_last_update,
1126
    cp2.disabled AS policy2_disabled,
1127
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1128
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1129
    cp2.message_flags AS policy2_message_flags,
1130
    cp2.channel_flags AS policy2_channel_flags,
1131
    cp2.signature AS policy2_signature
1132

1133
FROM graph_channels c
1134
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1135
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1136
    LEFT JOIN graph_channel_policies cp1
1137
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1138
    LEFT JOIN graph_channel_policies cp2
1139
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1140
WHERE c.version = $1
1141
  AND (
1142
       (cp1.last_update >= $2 AND cp1.last_update < $3)
1143
       OR
1144
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1145
    -- TODO(abdulkbk): see the potential of adding a created_at to channel 
1146
    -- table to extend this query to include channels created in a time range.
1147
       OR
1148
       (cp1.last_update IS NULL AND cp2.last_update IS NULL)
1149
  )
1150
  -- Pagination using compound cursor (max_update_time, id).
1151
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1152
  AND (
1153
       (CASE
1154
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1155
               THEN COALESCE(cp1.last_update, 0)
1156
           ELSE COALESCE(cp2.last_update, 0)
1157
       END > COALESCE($4, -1))
1158
       OR 
1159
       (CASE
1160
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1161
               THEN COALESCE(cp1.last_update, 0)
1162
           ELSE COALESCE(cp2.last_update, 0)
1163
       END = COALESCE($4, -1) 
1164
       AND c.id > COALESCE($5, -1))
1165
  )
1166
ORDER BY
1167
    CASE
1168
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1169
            THEN COALESCE(cp1.last_update, 0)
1170
        ELSE COALESCE(cp2.last_update, 0)
1171
    END ASC,
1172
    c.id ASC
1173
LIMIT COALESCE($6, 999999999)
1174
`
1175

1176
type GetChannelsByPolicyLastUpdateRangeParams struct {
1177
        Version        int16
1178
        StartTime      sql.NullInt64
1179
        EndTime        sql.NullInt64
1180
        LastUpdateTime sql.NullInt64
1181
        LastID         sql.NullInt64
1182
        MaxResults     interface{}
1183
}
1184

1185
type GetChannelsByPolicyLastUpdateRangeRow struct {
1186
        GraphChannel                   GraphChannel
1187
        GraphNode                      GraphNode
1188
        GraphNode_2                    GraphNode
1189
        Policy1ID                      sql.NullInt64
1190
        Policy1NodeID                  sql.NullInt64
1191
        Policy1Version                 sql.NullInt16
1192
        Policy1Timelock                sql.NullInt32
1193
        Policy1FeePpm                  sql.NullInt64
1194
        Policy1BaseFeeMsat             sql.NullInt64
1195
        Policy1MinHtlcMsat             sql.NullInt64
1196
        Policy1MaxHtlcMsat             sql.NullInt64
1197
        Policy1LastUpdate              sql.NullInt64
1198
        Policy1Disabled                sql.NullBool
1199
        Policy1InboundBaseFeeMsat      sql.NullInt64
1200
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1201
        Policy1MessageFlags            sql.NullInt16
1202
        Policy1ChannelFlags            sql.NullInt16
1203
        Policy1Signature               []byte
1204
        Policy2ID                      sql.NullInt64
1205
        Policy2NodeID                  sql.NullInt64
1206
        Policy2Version                 sql.NullInt16
1207
        Policy2Timelock                sql.NullInt32
1208
        Policy2FeePpm                  sql.NullInt64
1209
        Policy2BaseFeeMsat             sql.NullInt64
1210
        Policy2MinHtlcMsat             sql.NullInt64
1211
        Policy2MaxHtlcMsat             sql.NullInt64
1212
        Policy2LastUpdate              sql.NullInt64
1213
        Policy2Disabled                sql.NullBool
1214
        Policy2InboundBaseFeeMsat      sql.NullInt64
1215
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1216
        Policy2MessageFlags            sql.NullInt16
UNCOV
1217
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
1218
        Policy2Signature               []byte
×
UNCOV
1219
}
×
UNCOV
1220

×
1221
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
1222
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange,
×
1223
                arg.Version,
×
1224
                arg.StartTime,
×
1225
                arg.EndTime,
×
1226
                arg.LastUpdateTime,
×
1227
                arg.LastID,
×
1228
                arg.MaxResults,
×
1229
        )
×
1230
        if err != nil {
×
1231
                return nil, err
×
1232
        }
×
1233
        defer rows.Close()
×
1234
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
1235
        for rows.Next() {
×
1236
                var i GetChannelsByPolicyLastUpdateRangeRow
×
1237
                if err := rows.Scan(
×
1238
                        &i.GraphChannel.ID,
×
1239
                        &i.GraphChannel.Version,
×
1240
                        &i.GraphChannel.Scid,
×
1241
                        &i.GraphChannel.NodeID1,
×
1242
                        &i.GraphChannel.NodeID2,
×
1243
                        &i.GraphChannel.Outpoint,
×
1244
                        &i.GraphChannel.Capacity,
×
1245
                        &i.GraphChannel.BitcoinKey1,
×
1246
                        &i.GraphChannel.BitcoinKey2,
×
1247
                        &i.GraphChannel.Node1Signature,
×
1248
                        &i.GraphChannel.Node2Signature,
×
1249
                        &i.GraphChannel.Bitcoin1Signature,
×
1250
                        &i.GraphChannel.Bitcoin2Signature,
×
1251
                        &i.GraphNode.ID,
×
1252
                        &i.GraphNode.Version,
×
1253
                        &i.GraphNode.PubKey,
×
1254
                        &i.GraphNode.Alias,
×
1255
                        &i.GraphNode.LastUpdate,
×
1256
                        &i.GraphNode.Color,
×
1257
                        &i.GraphNode.Signature,
×
1258
                        &i.GraphNode_2.ID,
×
1259
                        &i.GraphNode_2.Version,
×
1260
                        &i.GraphNode_2.PubKey,
×
1261
                        &i.GraphNode_2.Alias,
×
1262
                        &i.GraphNode_2.LastUpdate,
×
1263
                        &i.GraphNode_2.Color,
×
1264
                        &i.GraphNode_2.Signature,
×
1265
                        &i.Policy1ID,
×
1266
                        &i.Policy1NodeID,
×
1267
                        &i.Policy1Version,
×
1268
                        &i.Policy1Timelock,
×
1269
                        &i.Policy1FeePpm,
×
1270
                        &i.Policy1BaseFeeMsat,
×
1271
                        &i.Policy1MinHtlcMsat,
×
1272
                        &i.Policy1MaxHtlcMsat,
×
1273
                        &i.Policy1LastUpdate,
×
1274
                        &i.Policy1Disabled,
×
1275
                        &i.Policy1InboundBaseFeeMsat,
×
1276
                        &i.Policy1InboundFeeRateMilliMsat,
×
1277
                        &i.Policy1MessageFlags,
×
1278
                        &i.Policy1ChannelFlags,
×
1279
                        &i.Policy1Signature,
×
1280
                        &i.Policy2ID,
×
1281
                        &i.Policy2NodeID,
×
1282
                        &i.Policy2Version,
×
1283
                        &i.Policy2Timelock,
×
1284
                        &i.Policy2FeePpm,
×
1285
                        &i.Policy2BaseFeeMsat,
×
1286
                        &i.Policy2MinHtlcMsat,
×
1287
                        &i.Policy2MaxHtlcMsat,
×
1288
                        &i.Policy2LastUpdate,
×
1289
                        &i.Policy2Disabled,
×
1290
                        &i.Policy2InboundBaseFeeMsat,
×
1291
                        &i.Policy2InboundFeeRateMilliMsat,
×
1292
                        &i.Policy2MessageFlags,
×
1293
                        &i.Policy2ChannelFlags,
×
1294
                        &i.Policy2Signature,
×
1295
                ); err != nil {
1296
                        return nil, err
×
1297
                }
×
1298
                items = append(items, i)
×
UNCOV
1299
        }
×
1300
        if err := rows.Close(); err != nil {
×
1301
                return nil, err
×
1302
        }
×
1303
        if err := rows.Err(); err != nil {
1304
                return nil, err
1305
        }
1306
        return items, nil
1307
}
1308

1309
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1310
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
1311
    n1.pub_key AS node1_pub_key,
1312
    n2.pub_key AS node2_pub_key
1313
FROM graph_channels c
1314
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1315
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1316
WHERE scid >= $1
1317
  AND scid < $2
1318
`
1319

1320
type GetChannelsBySCIDRangeParams struct {
1321
        StartScid []byte
1322
        EndScid   []byte
1323
}
1324

1325
type GetChannelsBySCIDRangeRow struct {
1326
        GraphChannel GraphChannel
UNCOV
1327
        Node1PubKey  []byte
×
UNCOV
1328
        Node2PubKey  []byte
×
UNCOV
1329
}
×
UNCOV
1330

×
1331
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1332
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1333
        if err != nil {
×
1334
                return nil, err
×
1335
        }
×
1336
        defer rows.Close()
×
1337
        var items []GetChannelsBySCIDRangeRow
×
1338
        for rows.Next() {
×
1339
                var i GetChannelsBySCIDRangeRow
×
1340
                if err := rows.Scan(
×
1341
                        &i.GraphChannel.ID,
×
1342
                        &i.GraphChannel.Version,
×
1343
                        &i.GraphChannel.Scid,
×
1344
                        &i.GraphChannel.NodeID1,
×
1345
                        &i.GraphChannel.NodeID2,
×
1346
                        &i.GraphChannel.Outpoint,
×
1347
                        &i.GraphChannel.Capacity,
×
1348
                        &i.GraphChannel.BitcoinKey1,
×
1349
                        &i.GraphChannel.BitcoinKey2,
×
1350
                        &i.GraphChannel.Node1Signature,
×
1351
                        &i.GraphChannel.Node2Signature,
×
1352
                        &i.GraphChannel.Bitcoin1Signature,
×
1353
                        &i.GraphChannel.Bitcoin2Signature,
×
1354
                        &i.Node1PubKey,
×
1355
                        &i.Node2PubKey,
×
1356
                ); err != nil {
1357
                        return nil, err
×
1358
                }
×
1359
                items = append(items, i)
×
UNCOV
1360
        }
×
1361
        if err := rows.Close(); err != nil {
×
1362
                return nil, err
×
1363
        }
×
1364
        if err := rows.Err(); err != nil {
1365
                return nil, err
1366
        }
1367
        return items, nil
1368
}
1369

1370
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1371
SELECT
1372
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
1373
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
1374
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
1375

1376
    -- Policy 1
1377
    cp1.id AS policy1_id,
1378
    cp1.node_id AS policy1_node_id,
1379
    cp1.version AS policy1_version,
1380
    cp1.timelock AS policy1_timelock,
1381
    cp1.fee_ppm AS policy1_fee_ppm,
1382
    cp1.base_fee_msat AS policy1_base_fee_msat,
1383
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1384
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1385
    cp1.last_update AS policy1_last_update,
1386
    cp1.disabled AS policy1_disabled,
1387
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1388
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1389
    cp1.message_flags AS policy1_message_flags,
1390
    cp1.channel_flags AS policy1_channel_flags,
1391
    cp1.signature AS policy1_signature,
1392

1393
    -- Policy 2
1394
    cp2.id AS policy2_id,
1395
    cp2.node_id AS policy2_node_id,
1396
    cp2.version AS policy2_version,
1397
    cp2.timelock AS policy2_timelock,
1398
    cp2.fee_ppm AS policy2_fee_ppm,
1399
    cp2.base_fee_msat AS policy2_base_fee_msat,
1400
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1401
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1402
    cp2.last_update AS policy2_last_update,
1403
    cp2.disabled AS policy2_disabled,
1404
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1405
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1406
    cp2.message_flags AS policy_2_message_flags,
1407
    cp2.channel_flags AS policy_2_channel_flags,
1408
    cp2.signature AS policy2_signature
1409

1410
FROM graph_channels c
1411
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1412
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1413
    LEFT JOIN graph_channel_policies cp1
1414
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1415
    LEFT JOIN graph_channel_policies cp2
1416
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1417
WHERE
1418
    c.version = $1
1419
  AND c.scid IN (/*SLICE:scids*/?)
1420
`
1421

1422
type GetChannelsBySCIDWithPoliciesParams struct {
1423
        Version int16
1424
        Scids   [][]byte
1425
}
1426

1427
type GetChannelsBySCIDWithPoliciesRow struct {
1428
        GraphChannel                   GraphChannel
1429
        GraphNode                      GraphNode
1430
        GraphNode_2                    GraphNode
1431
        Policy1ID                      sql.NullInt64
1432
        Policy1NodeID                  sql.NullInt64
1433
        Policy1Version                 sql.NullInt16
1434
        Policy1Timelock                sql.NullInt32
1435
        Policy1FeePpm                  sql.NullInt64
1436
        Policy1BaseFeeMsat             sql.NullInt64
1437
        Policy1MinHtlcMsat             sql.NullInt64
1438
        Policy1MaxHtlcMsat             sql.NullInt64
1439
        Policy1LastUpdate              sql.NullInt64
1440
        Policy1Disabled                sql.NullBool
1441
        Policy1InboundBaseFeeMsat      sql.NullInt64
1442
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1443
        Policy1MessageFlags            sql.NullInt16
1444
        Policy1ChannelFlags            sql.NullInt16
1445
        Policy1Signature               []byte
1446
        Policy2ID                      sql.NullInt64
1447
        Policy2NodeID                  sql.NullInt64
1448
        Policy2Version                 sql.NullInt16
1449
        Policy2Timelock                sql.NullInt32
1450
        Policy2FeePpm                  sql.NullInt64
1451
        Policy2BaseFeeMsat             sql.NullInt64
1452
        Policy2MinHtlcMsat             sql.NullInt64
1453
        Policy2MaxHtlcMsat             sql.NullInt64
1454
        Policy2LastUpdate              sql.NullInt64
1455
        Policy2Disabled                sql.NullBool
1456
        Policy2InboundBaseFeeMsat      sql.NullInt64
1457
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1458
        Policy2MessageFlags            sql.NullInt16
UNCOV
1459
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
1460
        Policy2Signature               []byte
×
UNCOV
1461
}
×
UNCOV
1462

×
1463
func (q *Queries) GetChannelsBySCIDWithPolicies(ctx context.Context, arg GetChannelsBySCIDWithPoliciesParams) ([]GetChannelsBySCIDWithPoliciesRow, error) {
×
1464
        query := getChannelsBySCIDWithPolicies
×
1465
        var queryParams []interface{}
×
1466
        queryParams = append(queryParams, arg.Version)
×
1467
        if len(arg.Scids) > 0 {
×
1468
                for _, v := range arg.Scids {
×
1469
                        queryParams = append(queryParams, v)
×
1470
                }
×
1471
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1472
        } else {
×
1473
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1474
        }
×
1475
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1476
        if err != nil {
×
1477
                return nil, err
×
1478
        }
×
1479
        defer rows.Close()
×
1480
        var items []GetChannelsBySCIDWithPoliciesRow
×
1481
        for rows.Next() {
×
1482
                var i GetChannelsBySCIDWithPoliciesRow
×
1483
                if err := rows.Scan(
×
1484
                        &i.GraphChannel.ID,
×
1485
                        &i.GraphChannel.Version,
×
1486
                        &i.GraphChannel.Scid,
×
1487
                        &i.GraphChannel.NodeID1,
×
1488
                        &i.GraphChannel.NodeID2,
×
1489
                        &i.GraphChannel.Outpoint,
×
1490
                        &i.GraphChannel.Capacity,
×
1491
                        &i.GraphChannel.BitcoinKey1,
×
1492
                        &i.GraphChannel.BitcoinKey2,
×
1493
                        &i.GraphChannel.Node1Signature,
×
1494
                        &i.GraphChannel.Node2Signature,
×
1495
                        &i.GraphChannel.Bitcoin1Signature,
×
1496
                        &i.GraphChannel.Bitcoin2Signature,
×
1497
                        &i.GraphNode.ID,
×
1498
                        &i.GraphNode.Version,
×
1499
                        &i.GraphNode.PubKey,
×
1500
                        &i.GraphNode.Alias,
×
1501
                        &i.GraphNode.LastUpdate,
×
1502
                        &i.GraphNode.Color,
×
1503
                        &i.GraphNode.Signature,
×
1504
                        &i.GraphNode_2.ID,
×
1505
                        &i.GraphNode_2.Version,
×
1506
                        &i.GraphNode_2.PubKey,
×
1507
                        &i.GraphNode_2.Alias,
×
1508
                        &i.GraphNode_2.LastUpdate,
×
1509
                        &i.GraphNode_2.Color,
×
1510
                        &i.GraphNode_2.Signature,
×
1511
                        &i.Policy1ID,
×
1512
                        &i.Policy1NodeID,
×
1513
                        &i.Policy1Version,
×
1514
                        &i.Policy1Timelock,
×
1515
                        &i.Policy1FeePpm,
×
1516
                        &i.Policy1BaseFeeMsat,
×
1517
                        &i.Policy1MinHtlcMsat,
×
1518
                        &i.Policy1MaxHtlcMsat,
×
1519
                        &i.Policy1LastUpdate,
×
1520
                        &i.Policy1Disabled,
×
1521
                        &i.Policy1InboundBaseFeeMsat,
×
1522
                        &i.Policy1InboundFeeRateMilliMsat,
×
1523
                        &i.Policy1MessageFlags,
×
1524
                        &i.Policy1ChannelFlags,
×
1525
                        &i.Policy1Signature,
×
1526
                        &i.Policy2ID,
×
1527
                        &i.Policy2NodeID,
×
1528
                        &i.Policy2Version,
×
1529
                        &i.Policy2Timelock,
×
1530
                        &i.Policy2FeePpm,
×
1531
                        &i.Policy2BaseFeeMsat,
×
1532
                        &i.Policy2MinHtlcMsat,
×
1533
                        &i.Policy2MaxHtlcMsat,
×
1534
                        &i.Policy2LastUpdate,
×
1535
                        &i.Policy2Disabled,
×
1536
                        &i.Policy2InboundBaseFeeMsat,
×
1537
                        &i.Policy2InboundFeeRateMilliMsat,
×
1538
                        &i.Policy2MessageFlags,
×
1539
                        &i.Policy2ChannelFlags,
×
1540
                        &i.Policy2Signature,
×
1541
                ); err != nil {
1542
                        return nil, err
×
1543
                }
×
1544
                items = append(items, i)
×
UNCOV
1545
        }
×
1546
        if err := rows.Close(); err != nil {
×
1547
                return nil, err
×
1548
        }
×
1549
        if err := rows.Err(); err != nil {
1550
                return nil, err
1551
        }
1552
        return items, nil
1553
}
1554

1555
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1556
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature FROM graph_channels
1557
WHERE version = $1
1558
  AND scid IN (/*SLICE:scids*/?)
1559
`
1560

1561
type GetChannelsBySCIDsParams struct {
UNCOV
1562
        Version int16
×
UNCOV
1563
        Scids   [][]byte
×
UNCOV
1564
}
×
UNCOV
1565

×
1566
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
1567
        query := getChannelsBySCIDs
×
1568
        var queryParams []interface{}
×
1569
        queryParams = append(queryParams, arg.Version)
×
1570
        if len(arg.Scids) > 0 {
×
1571
                for _, v := range arg.Scids {
×
1572
                        queryParams = append(queryParams, v)
×
1573
                }
×
1574
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1575
        } else {
×
1576
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1577
        }
×
1578
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1579
        if err != nil {
×
1580
                return nil, err
×
1581
        }
×
1582
        defer rows.Close()
×
1583
        var items []GraphChannel
×
1584
        for rows.Next() {
×
1585
                var i GraphChannel
×
1586
                if err := rows.Scan(
×
1587
                        &i.ID,
×
1588
                        &i.Version,
×
1589
                        &i.Scid,
×
1590
                        &i.NodeID1,
×
1591
                        &i.NodeID2,
×
1592
                        &i.Outpoint,
×
1593
                        &i.Capacity,
×
1594
                        &i.BitcoinKey1,
×
1595
                        &i.BitcoinKey2,
×
1596
                        &i.Node1Signature,
×
1597
                        &i.Node2Signature,
×
1598
                        &i.Bitcoin1Signature,
×
1599
                        &i.Bitcoin2Signature,
×
1600
                ); err != nil {
1601
                        return nil, err
×
1602
                }
×
1603
                items = append(items, i)
×
UNCOV
1604
        }
×
1605
        if err := rows.Close(); err != nil {
×
1606
                return nil, err
×
1607
        }
×
1608
        if err := rows.Err(); err != nil {
1609
                return nil, err
1610
        }
1611
        return items, nil
1612
}
1613

1614
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1615
SELECT scid
UNCOV
1616
FROM graph_closed_scids
×
UNCOV
1617
WHERE scid IN (/*SLICE:scids*/?)
×
UNCOV
1618
`
×
UNCOV
1619

×
1620
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
1621
        query := getClosedChannelsSCIDs
×
1622
        var queryParams []interface{}
×
1623
        if len(scids) > 0 {
×
1624
                for _, v := range scids {
×
1625
                        queryParams = append(queryParams, v)
×
1626
                }
×
1627
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
1628
        } else {
×
1629
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1630
        }
×
1631
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1632
        if err != nil {
×
1633
                return nil, err
×
1634
        }
×
1635
        defer rows.Close()
×
1636
        var items [][]byte
×
1637
        for rows.Next() {
×
1638
                var scid []byte
×
1639
                if err := rows.Scan(&scid); err != nil {
1640
                        return nil, err
×
1641
                }
×
1642
                items = append(items, scid)
×
UNCOV
1643
        }
×
1644
        if err := rows.Close(); err != nil {
×
1645
                return nil, err
×
1646
        }
×
1647
        if err := rows.Err(); err != nil {
1648
                return nil, err
1649
        }
1650
        return items, nil
1651
}
1652

1653
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1654
SELECT node_id, type, value
UNCOV
1655
FROM graph_node_extra_types
×
UNCOV
1656
WHERE node_id = $1
×
UNCOV
1657
`
×
UNCOV
1658

×
1659
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
1660
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
1661
        if err != nil {
×
1662
                return nil, err
×
1663
        }
×
1664
        defer rows.Close()
×
1665
        var items []GraphNodeExtraType
×
1666
        for rows.Next() {
×
1667
                var i GraphNodeExtraType
×
1668
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
1669
                        return nil, err
×
1670
                }
×
1671
                items = append(items, i)
×
UNCOV
1672
        }
×
1673
        if err := rows.Close(); err != nil {
×
1674
                return nil, err
×
1675
        }
×
1676
        if err := rows.Err(); err != nil {
1677
                return nil, err
1678
        }
1679
        return items, nil
1680
}
1681

1682
const getNodeAddresses = `-- name: GetNodeAddresses :many
1683
SELECT type, address
1684
FROM graph_node_addresses
1685
WHERE node_id = $1
1686
ORDER BY type ASC, position ASC
1687
`
1688

1689
type GetNodeAddressesRow struct {
UNCOV
1690
        Type    int16
×
UNCOV
1691
        Address string
×
UNCOV
1692
}
×
UNCOV
1693

×
1694
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
1695
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
1696
        if err != nil {
×
1697
                return nil, err
×
1698
        }
×
1699
        defer rows.Close()
×
1700
        var items []GetNodeAddressesRow
×
1701
        for rows.Next() {
×
1702
                var i GetNodeAddressesRow
×
1703
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
1704
                        return nil, err
×
1705
                }
×
1706
                items = append(items, i)
×
UNCOV
1707
        }
×
1708
        if err := rows.Close(); err != nil {
×
1709
                return nil, err
×
1710
        }
×
1711
        if err := rows.Err(); err != nil {
1712
                return nil, err
1713
        }
1714
        return items, nil
1715
}
1716

1717
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
1718
SELECT node_id, type, position, address
1719
FROM graph_node_addresses
UNCOV
1720
WHERE node_id IN (/*SLICE:ids*/?)
×
UNCOV
1721
ORDER BY node_id, type, position
×
UNCOV
1722
`
×
UNCOV
1723

×
1724
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
×
1725
        query := getNodeAddressesBatch
×
1726
        var queryParams []interface{}
×
1727
        if len(ids) > 0 {
×
1728
                for _, v := range ids {
×
1729
                        queryParams = append(queryParams, v)
×
1730
                }
×
1731
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1732
        } else {
×
1733
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1734
        }
×
1735
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1736
        if err != nil {
×
1737
                return nil, err
×
1738
        }
×
1739
        defer rows.Close()
×
1740
        var items []GraphNodeAddress
×
1741
        for rows.Next() {
×
1742
                var i GraphNodeAddress
×
1743
                if err := rows.Scan(
×
1744
                        &i.NodeID,
×
1745
                        &i.Type,
×
1746
                        &i.Position,
×
1747
                        &i.Address,
×
1748
                ); err != nil {
1749
                        return nil, err
×
1750
                }
×
1751
                items = append(items, i)
×
UNCOV
1752
        }
×
1753
        if err := rows.Close(); err != nil {
×
1754
                return nil, err
×
1755
        }
×
1756
        if err := rows.Err(); err != nil {
1757
                return nil, err
1758
        }
1759
        return items, nil
1760
}
1761

1762
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1763
SELECT id, version, pub_key, alias, last_update, color, signature
1764
FROM graph_nodes
1765
WHERE pub_key = $1
1766
  AND version = $2
1767
`
1768

1769
type GetNodeByPubKeyParams struct {
UNCOV
1770
        PubKey  []byte
×
UNCOV
1771
        Version int16
×
UNCOV
1772
}
×
UNCOV
1773

×
1774
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
1775
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
1776
        var i GraphNode
×
1777
        err := row.Scan(
×
1778
                &i.ID,
×
1779
                &i.Version,
×
1780
                &i.PubKey,
×
1781
                &i.Alias,
×
1782
                &i.LastUpdate,
×
1783
                &i.Color,
×
1784
                &i.Signature,
1785
        )
1786
        return i, err
1787
}
1788

1789
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
1790
SELECT node_id, type, value
1791
FROM graph_node_extra_types
UNCOV
1792
WHERE node_id IN (/*SLICE:ids*/?)
×
UNCOV
1793
ORDER BY node_id, type
×
UNCOV
1794
`
×
UNCOV
1795

×
1796
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
1797
        query := getNodeExtraTypesBatch
×
1798
        var queryParams []interface{}
×
1799
        if len(ids) > 0 {
×
1800
                for _, v := range ids {
×
1801
                        queryParams = append(queryParams, v)
×
1802
                }
×
1803
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1804
        } else {
×
1805
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1806
        }
×
1807
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1808
        if err != nil {
×
1809
                return nil, err
×
1810
        }
×
1811
        defer rows.Close()
×
1812
        var items []GraphNodeExtraType
×
1813
        for rows.Next() {
×
1814
                var i GraphNodeExtraType
×
1815
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
1816
                        return nil, err
×
1817
                }
×
1818
                items = append(items, i)
×
UNCOV
1819
        }
×
1820
        if err := rows.Close(); err != nil {
×
1821
                return nil, err
×
1822
        }
×
1823
        if err := rows.Err(); err != nil {
1824
                return nil, err
1825
        }
1826
        return items, nil
1827
}
1828

1829
const getNodeFeatures = `-- name: GetNodeFeatures :many
1830
SELECT node_id, feature_bit
UNCOV
1831
FROM graph_node_features
×
UNCOV
1832
WHERE node_id = $1
×
UNCOV
1833
`
×
UNCOV
1834

×
1835
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
1836
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
1837
        if err != nil {
×
1838
                return nil, err
×
1839
        }
×
1840
        defer rows.Close()
×
1841
        var items []GraphNodeFeature
×
1842
        for rows.Next() {
×
1843
                var i GraphNodeFeature
×
1844
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
1845
                        return nil, err
×
1846
                }
×
1847
                items = append(items, i)
×
UNCOV
1848
        }
×
1849
        if err := rows.Close(); err != nil {
×
1850
                return nil, err
×
1851
        }
×
1852
        if err := rows.Err(); err != nil {
1853
                return nil, err
1854
        }
1855
        return items, nil
1856
}
1857

1858
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
1859
SELECT node_id, feature_bit
1860
FROM graph_node_features
UNCOV
1861
WHERE node_id IN (/*SLICE:ids*/?)
×
UNCOV
1862
ORDER BY node_id, feature_bit
×
UNCOV
1863
`
×
UNCOV
1864

×
1865
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
×
1866
        query := getNodeFeaturesBatch
×
1867
        var queryParams []interface{}
×
1868
        if len(ids) > 0 {
×
1869
                for _, v := range ids {
×
1870
                        queryParams = append(queryParams, v)
×
1871
                }
×
1872
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1873
        } else {
×
1874
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1875
        }
×
1876
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1877
        if err != nil {
×
1878
                return nil, err
×
1879
        }
×
1880
        defer rows.Close()
×
1881
        var items []GraphNodeFeature
×
1882
        for rows.Next() {
×
1883
                var i GraphNodeFeature
×
1884
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
1885
                        return nil, err
×
1886
                }
×
1887
                items = append(items, i)
×
UNCOV
1888
        }
×
1889
        if err := rows.Close(); err != nil {
×
1890
                return nil, err
×
1891
        }
×
1892
        if err := rows.Err(); err != nil {
1893
                return nil, err
1894
        }
1895
        return items, nil
1896
}
1897

1898
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
1899
SELECT f.feature_bit
1900
FROM graph_nodes n
1901
    JOIN graph_node_features f ON f.node_id = n.id
1902
WHERE n.pub_key = $1
1903
  AND n.version = $2
1904
`
1905

1906
type GetNodeFeaturesByPubKeyParams struct {
UNCOV
1907
        PubKey  []byte
×
UNCOV
1908
        Version int16
×
UNCOV
1909
}
×
UNCOV
1910

×
1911
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
1912
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
1913
        if err != nil {
×
1914
                return nil, err
×
1915
        }
×
1916
        defer rows.Close()
×
1917
        var items []int32
×
1918
        for rows.Next() {
×
1919
                var feature_bit int32
×
1920
                if err := rows.Scan(&feature_bit); err != nil {
1921
                        return nil, err
×
1922
                }
×
1923
                items = append(items, feature_bit)
×
UNCOV
1924
        }
×
1925
        if err := rows.Close(); err != nil {
×
1926
                return nil, err
×
1927
        }
×
1928
        if err := rows.Err(); err != nil {
1929
                return nil, err
1930
        }
1931
        return items, nil
1932
}
1933

1934
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
1935
SELECT id
1936
FROM graph_nodes
1937
WHERE pub_key = $1
1938
  AND version = $2
1939
`
1940

1941
type GetNodeIDByPubKeyParams struct {
UNCOV
1942
        PubKey  []byte
×
UNCOV
1943
        Version int16
×
UNCOV
1944
}
×
UNCOV
1945

×
1946
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
1947
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
1948
        var id int64
1949
        err := row.Scan(&id)
1950
        return id, err
1951
}
1952

1953
const getNodesByIDs = `-- name: GetNodesByIDs :many
1954
SELECT id, version, pub_key, alias, last_update, color, signature
UNCOV
1955
FROM graph_nodes
×
UNCOV
1956
WHERE id IN (/*SLICE:ids*/?)
×
UNCOV
1957
`
×
UNCOV
1958

×
1959
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
1960
        query := getNodesByIDs
×
1961
        var queryParams []interface{}
×
1962
        if len(ids) > 0 {
×
1963
                for _, v := range ids {
×
1964
                        queryParams = append(queryParams, v)
×
1965
                }
×
1966
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1967
        } else {
×
1968
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1969
        }
×
1970
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1971
        if err != nil {
×
1972
                return nil, err
×
1973
        }
×
1974
        defer rows.Close()
×
1975
        var items []GraphNode
×
1976
        for rows.Next() {
×
1977
                var i GraphNode
×
1978
                if err := rows.Scan(
×
1979
                        &i.ID,
×
1980
                        &i.Version,
×
1981
                        &i.PubKey,
×
1982
                        &i.Alias,
×
1983
                        &i.LastUpdate,
×
1984
                        &i.Color,
×
1985
                        &i.Signature,
×
1986
                ); err != nil {
1987
                        return nil, err
×
1988
                }
×
1989
                items = append(items, i)
×
UNCOV
1990
        }
×
1991
        if err := rows.Close(); err != nil {
×
1992
                return nil, err
×
1993
        }
×
1994
        if err := rows.Err(); err != nil {
1995
                return nil, err
1996
        }
1997
        return items, nil
1998
}
1999

2000
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
2001
SELECT id, version, pub_key, alias, last_update, color, signature
2002
FROM graph_nodes
2003
WHERE last_update >= $1
2004
  AND last_update <= $2
2005
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2006
  -- This ensures stable ordering and allows us to resume from where we left off.
2007
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2008
  AND (
2009
    -- Include rows with last_update greater than cursor (or all rows if cursor is -1)
2010
    last_update > COALESCE($3, -1)
2011
    OR 
2012
    -- For rows with same last_update, use pub_key as tiebreaker
2013
    (last_update = COALESCE($3, -1) 
2014
     AND pub_key > $4)
2015
  )
2016
  -- Optional filter for public nodes only
2017
  AND (
2018
    -- If only_public is false or not provided, include all nodes
2019
    COALESCE($5, FALSE) IS FALSE
2020
    OR 
2021
    -- For V1 protocol, a node is public if it has at least one public channel.
2022
    -- A public channel has bitcoin_1_signature set (channel announcement received).
2023
    EXISTS (
2024
      SELECT 1
2025
      FROM graph_channels c
2026
      WHERE c.version = 1
2027
        AND c.bitcoin_1_signature IS NOT NULL
2028
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2029
    )
2030
  )
2031
ORDER BY last_update ASC, pub_key ASC
2032
LIMIT COALESCE($6, 999999999)
2033
`
2034

2035
type GetNodesByLastUpdateRangeParams struct {
2036
        StartTime  sql.NullInt64
2037
        EndTime    sql.NullInt64
2038
        LastUpdate sql.NullInt64
2039
        LastPubKey []byte
UNCOV
2040
        OnlyPublic interface{}
×
UNCOV
2041
        MaxResults interface{}
×
UNCOV
2042
}
×
UNCOV
2043

×
2044
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2045
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
2046
                arg.StartTime,
×
2047
                arg.EndTime,
×
2048
                arg.LastUpdate,
×
2049
                arg.LastPubKey,
×
2050
                arg.OnlyPublic,
×
2051
                arg.MaxResults,
×
2052
        )
×
2053
        if err != nil {
×
2054
                return nil, err
×
2055
        }
×
2056
        defer rows.Close()
×
2057
        var items []GraphNode
×
2058
        for rows.Next() {
×
2059
                var i GraphNode
×
2060
                if err := rows.Scan(
×
2061
                        &i.ID,
×
2062
                        &i.Version,
×
2063
                        &i.PubKey,
×
2064
                        &i.Alias,
×
2065
                        &i.LastUpdate,
×
2066
                        &i.Color,
×
2067
                        &i.Signature,
×
2068
                ); err != nil {
2069
                        return nil, err
×
2070
                }
×
2071
                items = append(items, i)
×
UNCOV
2072
        }
×
2073
        if err := rows.Close(); err != nil {
×
2074
                return nil, err
×
2075
        }
×
2076
        if err := rows.Err(); err != nil {
2077
                return nil, err
2078
        }
2079
        return items, nil
2080
}
2081

2082
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2083
SELECT block_height, block_hash
2084
FROM graph_prune_log
UNCOV
2085
WHERE block_height
×
UNCOV
2086
   IN (/*SLICE:heights*/?)
×
UNCOV
2087
`
×
UNCOV
2088

×
2089
func (q *Queries) GetPruneEntriesForHeights(ctx context.Context, heights []int64) ([]GraphPruneLog, error) {
×
2090
        query := getPruneEntriesForHeights
×
2091
        var queryParams []interface{}
×
2092
        if len(heights) > 0 {
×
2093
                for _, v := range heights {
×
2094
                        queryParams = append(queryParams, v)
×
2095
                }
×
2096
                query = strings.Replace(query, "/*SLICE:heights*/?", makeQueryParams(len(queryParams), len(heights)), 1)
×
2097
        } else {
×
2098
                query = strings.Replace(query, "/*SLICE:heights*/?", "NULL", 1)
×
2099
        }
×
2100
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2101
        if err != nil {
×
2102
                return nil, err
×
2103
        }
×
2104
        defer rows.Close()
×
2105
        var items []GraphPruneLog
×
2106
        for rows.Next() {
×
2107
                var i GraphPruneLog
×
2108
                if err := rows.Scan(&i.BlockHeight, &i.BlockHash); err != nil {
2109
                        return nil, err
×
2110
                }
×
2111
                items = append(items, i)
×
UNCOV
2112
        }
×
2113
        if err := rows.Close(); err != nil {
×
2114
                return nil, err
×
2115
        }
×
2116
        if err := rows.Err(); err != nil {
2117
                return nil, err
2118
        }
2119
        return items, nil
2120
}
2121

2122
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2123
SELECT block_hash
UNCOV
2124
FROM graph_prune_log
×
UNCOV
2125
WHERE block_height = $1
×
UNCOV
2126
`
×
UNCOV
2127

×
2128
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2129
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2130
        var block_hash []byte
2131
        err := row.Scan(&block_hash)
2132
        return block_hash, err
2133
}
2134

2135
const getPruneTip = `-- name: GetPruneTip :one
2136
SELECT block_height, block_hash
2137
FROM graph_prune_log
UNCOV
2138
ORDER BY block_height DESC
×
UNCOV
2139
LIMIT 1
×
UNCOV
2140
`
×
UNCOV
2141

×
2142
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2143
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2144
        var i GraphPruneLog
2145
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
2146
        return i, err
2147
}
2148

2149
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2150
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature
2151
FROM graph_channels
2152
WHERE node_1_signature IS NOT NULL
2153
  AND scid >= $1
2154
  AND scid < $2
2155
`
2156

2157
type GetPublicV1ChannelsBySCIDParams struct {
UNCOV
2158
        StartScid []byte
×
UNCOV
2159
        EndScid   []byte
×
UNCOV
2160
}
×
UNCOV
2161

×
2162
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2163
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2164
        if err != nil {
×
2165
                return nil, err
×
2166
        }
×
2167
        defer rows.Close()
×
2168
        var items []GraphChannel
×
2169
        for rows.Next() {
×
2170
                var i GraphChannel
×
2171
                if err := rows.Scan(
×
2172
                        &i.ID,
×
2173
                        &i.Version,
×
2174
                        &i.Scid,
×
2175
                        &i.NodeID1,
×
2176
                        &i.NodeID2,
×
2177
                        &i.Outpoint,
×
2178
                        &i.Capacity,
×
2179
                        &i.BitcoinKey1,
×
2180
                        &i.BitcoinKey2,
×
2181
                        &i.Node1Signature,
×
2182
                        &i.Node2Signature,
×
2183
                        &i.Bitcoin1Signature,
×
2184
                        &i.Bitcoin2Signature,
×
2185
                ); err != nil {
2186
                        return nil, err
×
2187
                }
×
2188
                items = append(items, i)
×
UNCOV
2189
        }
×
2190
        if err := rows.Close(); err != nil {
×
2191
                return nil, err
×
2192
        }
×
2193
        if err := rows.Err(); err != nil {
2194
                return nil, err
2195
        }
2196
        return items, nil
2197
}
2198

2199
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2200
SELECT scid from graph_channels
2201
WHERE outpoint = $1 AND version = $2
2202
`
2203

2204
type GetSCIDByOutpointParams struct {
UNCOV
2205
        Outpoint string
×
UNCOV
2206
        Version  int16
×
UNCOV
2207
}
×
UNCOV
2208

×
2209
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2210
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2211
        var scid []byte
2212
        err := row.Scan(&scid)
2213
        return scid, err
2214
}
2215

2216
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2217
SELECT sn.node_id, n.pub_key
2218
FROM graph_source_nodes sn
2219
    JOIN graph_nodes n ON sn.node_id = n.id
2220
WHERE n.version = $1
2221
`
2222

2223
type GetSourceNodesByVersionRow struct {
UNCOV
2224
        NodeID int64
×
UNCOV
2225
        PubKey []byte
×
UNCOV
2226
}
×
UNCOV
2227

×
2228
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2229
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2230
        if err != nil {
×
2231
                return nil, err
×
2232
        }
×
2233
        defer rows.Close()
×
2234
        var items []GetSourceNodesByVersionRow
×
2235
        for rows.Next() {
×
2236
                var i GetSourceNodesByVersionRow
×
2237
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
2238
                        return nil, err
×
2239
                }
×
2240
                items = append(items, i)
×
UNCOV
2241
        }
×
2242
        if err := rows.Close(); err != nil {
×
2243
                return nil, err
×
2244
        }
×
2245
        if err := rows.Err(); err != nil {
2246
                return nil, err
2247
        }
2248
        return items, nil
2249
}
2250

2251
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2252
SELECT c.scid
2253
FROM graph_channels c
2254
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2255
WHERE cp.disabled = true
2256
AND c.version = 1
2257
GROUP BY c.scid
2258
HAVING COUNT(*) > 1
2259
`
2260

UNCOV
2261
// NOTE: this is V1 specific since for V1, disabled is a
×
UNCOV
2262
// simple, single boolean. The proposed V2 policy
×
UNCOV
2263
// structure will have a more complex disabled bit vector
×
UNCOV
2264
// and so the query for V2 may differ.
×
2265
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2266
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2267
        if err != nil {
×
2268
                return nil, err
×
2269
        }
×
2270
        defer rows.Close()
×
2271
        var items [][]byte
×
2272
        for rows.Next() {
×
2273
                var scid []byte
×
2274
                if err := rows.Scan(&scid); err != nil {
2275
                        return nil, err
×
2276
                }
×
2277
                items = append(items, scid)
×
UNCOV
2278
        }
×
2279
        if err := rows.Close(); err != nil {
×
2280
                return nil, err
×
2281
        }
×
2282
        if err := rows.Err(); err != nil {
2283
                return nil, err
2284
        }
2285
        return items, nil
2286
}
2287

2288
const getZombieChannel = `-- name: GetZombieChannel :one
2289
SELECT scid, version, node_key_1, node_key_2
2290
FROM graph_zombie_channels
2291
WHERE scid = $1
2292
AND version = $2
2293
`
2294

2295
type GetZombieChannelParams struct {
UNCOV
2296
        Scid    []byte
×
UNCOV
2297
        Version int16
×
UNCOV
2298
}
×
UNCOV
2299

×
2300
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2301
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2302
        var i GraphZombieChannel
×
2303
        err := row.Scan(
×
2304
                &i.Scid,
×
2305
                &i.Version,
×
2306
                &i.NodeKey1,
×
2307
                &i.NodeKey2,
2308
        )
2309
        return i, err
2310
}
2311

2312
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2313
SELECT scid, version, node_key_1, node_key_2
2314
FROM graph_zombie_channels
2315
WHERE version = $1
2316
  AND scid IN (/*SLICE:scids*/?)
2317
`
2318

2319
type GetZombieChannelsSCIDsParams struct {
UNCOV
2320
        Version int16
×
UNCOV
2321
        Scids   [][]byte
×
UNCOV
2322
}
×
UNCOV
2323

×
2324
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2325
        query := getZombieChannelsSCIDs
×
2326
        var queryParams []interface{}
×
2327
        queryParams = append(queryParams, arg.Version)
×
2328
        if len(arg.Scids) > 0 {
×
2329
                for _, v := range arg.Scids {
×
2330
                        queryParams = append(queryParams, v)
×
2331
                }
×
2332
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2333
        } else {
×
2334
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2335
        }
×
2336
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2337
        if err != nil {
×
2338
                return nil, err
×
2339
        }
×
2340
        defer rows.Close()
×
2341
        var items []GraphZombieChannel
×
2342
        for rows.Next() {
×
2343
                var i GraphZombieChannel
×
2344
                if err := rows.Scan(
×
2345
                        &i.Scid,
×
2346
                        &i.Version,
×
2347
                        &i.NodeKey1,
×
2348
                        &i.NodeKey2,
×
2349
                ); err != nil {
2350
                        return nil, err
×
2351
                }
×
2352
                items = append(items, i)
×
UNCOV
2353
        }
×
2354
        if err := rows.Close(); err != nil {
×
2355
                return nil, err
×
2356
        }
×
2357
        if err := rows.Err(); err != nil {
2358
                return nil, err
2359
        }
2360
        return items, nil
2361
}
2362

2363
const highestSCID = `-- name: HighestSCID :one
2364
SELECT scid
2365
FROM graph_channels
2366
WHERE version = $1
UNCOV
2367
ORDER BY scid DESC
×
UNCOV
2368
LIMIT 1
×
UNCOV
2369
`
×
UNCOV
2370

×
2371
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2372
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2373
        var scid []byte
2374
        err := row.Scan(&scid)
2375
        return scid, err
2376
}
2377

2378
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2379
/* ─────────────────────────────────────────────
2380
   graph_channel_features table queries
2381
   ─────────────────────────────────────────────
2382
*/
2383

2384
INSERT INTO graph_channel_features (
2385
    channel_id, feature_bit
2386
) VALUES (
2387
    $1, $2
2388
) ON CONFLICT (channel_id, feature_bit)
2389
    -- Do nothing if the channel_id and feature_bit already exist.
2390
    DO NOTHING
2391
`
2392

2393
type InsertChannelFeatureParams struct {
UNCOV
2394
        ChannelID  int64
×
UNCOV
2395
        FeatureBit int32
×
UNCOV
2396
}
×
UNCOV
2397

×
2398
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
2399
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
2400
        return err
2401
}
2402

2403
const insertChannelMig = `-- name: InsertChannelMig :one
2404
INSERT INTO graph_channels (
2405
    version, scid, node_id_1, node_id_2,
2406
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2407
    node_1_signature, node_2_signature, bitcoin_1_signature,
2408
    bitcoin_2_signature
2409
) VALUES (
2410
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2411
) ON CONFLICT (scid, version)
2412
    -- If a conflict occurs, we have already migrated this channel. However, we
2413
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2414
    -- otherwise, the "RETURNING id" part does not work.
2415
    DO UPDATE SET
2416
        node_id_1 = EXCLUDED.node_id_1,
2417
        node_id_2 = EXCLUDED.node_id_2,
2418
        outpoint = EXCLUDED.outpoint,
2419
        capacity = EXCLUDED.capacity,
2420
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2421
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2422
        node_1_signature = EXCLUDED.node_1_signature,
2423
        node_2_signature = EXCLUDED.node_2_signature,
2424
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2425
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2426
RETURNING id
2427
`
2428

2429
type InsertChannelMigParams struct {
2430
        Version           int16
2431
        Scid              []byte
2432
        NodeID1           int64
2433
        NodeID2           int64
2434
        Outpoint          string
2435
        Capacity          sql.NullInt64
2436
        BitcoinKey1       []byte
2437
        BitcoinKey2       []byte
2438
        Node1Signature    []byte
2439
        Node2Signature    []byte
2440
        Bitcoin1Signature []byte
2441
        Bitcoin2Signature []byte
2442
}
2443

2444
// NOTE: This query is only meant to be used by the graph SQL migration since
UNCOV
2445
// for that migration, in order to be retry-safe, we don't want to error out if
×
UNCOV
2446
// we re-insert the same channel again (which would error if the normal
×
UNCOV
2447
// CreateChannel query is used because of the uniqueness constraint on the scid
×
UNCOV
2448
// and version columns).
×
2449
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
2450
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
2451
                arg.Version,
×
2452
                arg.Scid,
×
2453
                arg.NodeID1,
×
2454
                arg.NodeID2,
×
2455
                arg.Outpoint,
×
2456
                arg.Capacity,
×
2457
                arg.BitcoinKey1,
×
2458
                arg.BitcoinKey2,
×
2459
                arg.Node1Signature,
×
2460
                arg.Node2Signature,
×
2461
                arg.Bitcoin1Signature,
×
2462
                arg.Bitcoin2Signature,
×
2463
        )
×
2464
        var id int64
2465
        err := row.Scan(&id)
2466
        return id, err
2467
}
2468

2469
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2470
/* ─────────────────────────────────────────────
2471
   graph_closed_scid table queries
2472
   ────────────────────────────────────────────-
2473
*/
2474

2475
INSERT INTO graph_closed_scids (scid)
UNCOV
2476
VALUES ($1)
×
UNCOV
2477
ON CONFLICT (scid) DO NOTHING
×
UNCOV
2478
`
×
UNCOV
2479

×
2480
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
2481
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
2482
        return err
2483
}
2484

2485
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
2486
INSERT INTO graph_channel_policies (
2487
    version, channel_id, node_id, timelock, fee_ppm,
2488
    base_fee_msat, min_htlc_msat, last_update, disabled,
2489
    max_htlc_msat, inbound_base_fee_msat,
2490
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2491
    signature
2492
) VALUES  (
2493
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2494
)
2495
ON CONFLICT (channel_id, node_id, version)
2496
    -- If a conflict occurs, we have already migrated this policy. However, we
2497
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2498
    -- otherwise, the "RETURNING id" part does not work.
2499
    DO UPDATE SET
2500
        timelock = EXCLUDED.timelock,
2501
        fee_ppm = EXCLUDED.fee_ppm,
2502
        base_fee_msat = EXCLUDED.base_fee_msat,
2503
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2504
        last_update = EXCLUDED.last_update,
2505
        disabled = EXCLUDED.disabled,
2506
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2507
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2508
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2509
        message_flags = EXCLUDED.message_flags,
2510
        channel_flags = EXCLUDED.channel_flags,
2511
        signature = EXCLUDED.signature
2512
RETURNING id
2513
`
2514

2515
type InsertEdgePolicyMigParams struct {
2516
        Version                 int16
2517
        ChannelID               int64
2518
        NodeID                  int64
2519
        Timelock                int32
2520
        FeePpm                  int64
2521
        BaseFeeMsat             int64
2522
        MinHtlcMsat             int64
2523
        LastUpdate              sql.NullInt64
2524
        Disabled                sql.NullBool
2525
        MaxHtlcMsat             sql.NullInt64
2526
        InboundBaseFeeMsat      sql.NullInt64
2527
        InboundFeeRateMilliMsat sql.NullInt64
2528
        MessageFlags            sql.NullInt16
2529
        ChannelFlags            sql.NullInt16
2530
        Signature               []byte
2531
}
2532

2533
// NOTE: This query is only meant to be used by the graph SQL migration since
UNCOV
2534
// for that migration, in order to be retry-safe, we don't want to error out if
×
UNCOV
2535
// we re-insert the same policy (which would error if the normal
×
UNCOV
2536
// UpsertEdgePolicy query is used because of the constraint in that query that
×
UNCOV
2537
// requires a policy update to have a newer last_update than the existing one).
×
2538
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
2539
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
2540
                arg.Version,
×
2541
                arg.ChannelID,
×
2542
                arg.NodeID,
×
2543
                arg.Timelock,
×
2544
                arg.FeePpm,
×
2545
                arg.BaseFeeMsat,
×
2546
                arg.MinHtlcMsat,
×
2547
                arg.LastUpdate,
×
2548
                arg.Disabled,
×
2549
                arg.MaxHtlcMsat,
×
2550
                arg.InboundBaseFeeMsat,
×
2551
                arg.InboundFeeRateMilliMsat,
×
2552
                arg.MessageFlags,
×
2553
                arg.ChannelFlags,
×
2554
                arg.Signature,
×
2555
        )
×
2556
        var id int64
2557
        err := row.Scan(&id)
2558
        return id, err
2559
}
2560

2561
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2562
/* ─────────────────────────────────────────────
2563
   graph_node_features table queries
2564
   ─────────────────────────────────────────────
2565
*/
2566

2567
INSERT INTO graph_node_features (
2568
    node_id, feature_bit
2569
) VALUES (
2570
    $1, $2
2571
) ON CONFLICT (node_id, feature_bit)
2572
    -- Do nothing if the feature already exists for the node.
2573
    DO NOTHING
2574
`
2575

2576
type InsertNodeFeatureParams struct {
UNCOV
2577
        NodeID     int64
×
UNCOV
2578
        FeatureBit int32
×
UNCOV
2579
}
×
UNCOV
2580

×
2581
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
2582
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
2583
        return err
2584
}
2585

2586
const insertNodeMig = `-- name: InsertNodeMig :one
2587
/* ─────────────────────────────────────────────
2588
   Migration specific queries
2589

2590
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2591
   dedicated to the migration that requires it, and so we can then remove
2592
   it from the main set of "live" queries that the code-base has access to.
2593
   ────────────────────────────────────────────-
2594
*/
2595

2596
INSERT INTO graph_nodes (
2597
    version, pub_key, alias, last_update, color, signature
2598
) VALUES (
2599
    $1, $2, $3, $4, $5, $6
2600
)
2601
ON CONFLICT (pub_key, version)
2602
    -- If a conflict occurs, we have already migrated this node. However, we
2603
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2604
    -- otherwise, the "RETURNING id" part does not work.
2605
    DO UPDATE SET
2606
        alias = EXCLUDED.alias,
2607
        last_update = EXCLUDED.last_update,
2608
        color = EXCLUDED.color,
2609
        signature = EXCLUDED.signature
2610
RETURNING id
2611
`
2612

2613
type InsertNodeMigParams struct {
2614
        Version    int16
2615
        PubKey     []byte
2616
        Alias      sql.NullString
2617
        LastUpdate sql.NullInt64
2618
        Color      sql.NullString
2619
        Signature  []byte
2620
}
2621

2622
// NOTE: This query is only meant to be used by the graph SQL migration since
UNCOV
2623
// for that migration, in order to be retry-safe, we don't want to error out if
×
UNCOV
2624
// we re-insert the same node (which would error if the normal UpsertNode query
×
UNCOV
2625
// is used because of the constraint in that query that requires a node update
×
UNCOV
2626
// to have a newer last_update than the existing node).
×
2627
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
2628
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
2629
                arg.Version,
×
2630
                arg.PubKey,
×
2631
                arg.Alias,
×
2632
                arg.LastUpdate,
×
2633
                arg.Color,
×
2634
                arg.Signature,
×
2635
        )
×
2636
        var id int64
2637
        err := row.Scan(&id)
2638
        return id, err
2639
}
2640

2641
const isClosedChannel = `-- name: IsClosedChannel :one
2642
SELECT EXISTS (
2643
    SELECT 1
2644
    FROM graph_closed_scids
UNCOV
2645
    WHERE scid = $1
×
UNCOV
2646
)
×
UNCOV
2647
`
×
UNCOV
2648

×
2649
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
2650
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
2651
        var exists bool
2652
        err := row.Scan(&exists)
2653
        return exists, err
2654
}
2655

2656
const isPublicV1Node = `-- name: IsPublicV1Node :one
2657
SELECT EXISTS (
2658
    SELECT 1
2659
    FROM graph_channels c
2660
    JOIN graph_nodes n ON n.id = c.node_id_1 OR n.id = c.node_id_2
2661
    -- NOTE: we hard-code the version here since the clauses
2662
    -- here that determine if a node is public is specific
2663
    -- to the V1 gossip protocol. In V1, a node is public
2664
    -- if it has a public channel and a public channel is one
2665
    -- where we have the set of signatures of the channel
2666
    -- announcement. It is enough to just check that we have
2667
    -- one of the signatures since we only ever set them
2668
    -- together.
2669
    WHERE c.version = 1
2670
      AND c.bitcoin_1_signature IS NOT NULL
UNCOV
2671
      AND n.pub_key = $1
×
UNCOV
2672
)
×
UNCOV
2673
`
×
UNCOV
2674

×
2675
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2676
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2677
        var exists bool
2678
        err := row.Scan(&exists)
2679
        return exists, err
2680
}
2681

2682
const isZombieChannel = `-- name: IsZombieChannel :one
2683
SELECT EXISTS (
2684
    SELECT 1
2685
    FROM graph_zombie_channels
2686
    WHERE scid = $1
2687
    AND version = $2
2688
) AS is_zombie
2689
`
2690

2691
type IsZombieChannelParams struct {
UNCOV
2692
        Scid    []byte
×
UNCOV
2693
        Version int16
×
UNCOV
2694
}
×
UNCOV
2695

×
2696
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2697
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2698
        var is_zombie bool
2699
        err := row.Scan(&is_zombie)
2700
        return is_zombie, err
2701
}
2702

2703
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2704
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
2705
    n1.pub_key AS node1_pubkey,
2706
    n2.pub_key AS node2_pubkey,
2707

2708
    -- Policy 1
2709
    -- TODO(elle): use sqlc.embed to embed policy structs
2710
    --  once this issue is resolved:
2711
    --  https://github.com/sqlc-dev/sqlc/issues/2997
2712
    cp1.id AS policy1_id,
2713
    cp1.node_id AS policy1_node_id,
2714
    cp1.version AS policy1_version,
2715
    cp1.timelock AS policy1_timelock,
2716
    cp1.fee_ppm AS policy1_fee_ppm,
2717
    cp1.base_fee_msat AS policy1_base_fee_msat,
2718
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
2719
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
2720
    cp1.last_update AS policy1_last_update,
2721
    cp1.disabled AS policy1_disabled,
2722
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2723
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2724
    cp1.message_flags AS policy1_message_flags,
2725
    cp1.channel_flags AS policy1_channel_flags,
2726
    cp1.signature AS policy1_signature,
2727

2728
       -- Policy 2
2729
    cp2.id AS policy2_id,
2730
    cp2.node_id AS policy2_node_id,
2731
    cp2.version AS policy2_version,
2732
    cp2.timelock AS policy2_timelock,
2733
    cp2.fee_ppm AS policy2_fee_ppm,
2734
    cp2.base_fee_msat AS policy2_base_fee_msat,
2735
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
2736
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
2737
    cp2.last_update AS policy2_last_update,
2738
    cp2.disabled AS policy2_disabled,
2739
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2740
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2741
    cp2.message_flags AS policy2_message_flags,
2742
    cp2.channel_flags AS policy2_channel_flags,
2743
    cp2.signature AS policy2_signature
2744

2745
FROM graph_channels c
2746
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2747
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2748
    LEFT JOIN graph_channel_policies cp1
2749
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2750
    LEFT JOIN graph_channel_policies cp2
2751
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2752
WHERE c.version = $1
2753
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2754
`
2755

2756
type ListChannelsByNodeIDParams struct {
2757
        Version int16
2758
        NodeID1 int64
2759
}
2760

2761
type ListChannelsByNodeIDRow struct {
2762
        GraphChannel                   GraphChannel
2763
        Node1Pubkey                    []byte
2764
        Node2Pubkey                    []byte
2765
        Policy1ID                      sql.NullInt64
2766
        Policy1NodeID                  sql.NullInt64
2767
        Policy1Version                 sql.NullInt16
2768
        Policy1Timelock                sql.NullInt32
2769
        Policy1FeePpm                  sql.NullInt64
2770
        Policy1BaseFeeMsat             sql.NullInt64
2771
        Policy1MinHtlcMsat             sql.NullInt64
2772
        Policy1MaxHtlcMsat             sql.NullInt64
2773
        Policy1LastUpdate              sql.NullInt64
2774
        Policy1Disabled                sql.NullBool
2775
        Policy1InboundBaseFeeMsat      sql.NullInt64
2776
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2777
        Policy1MessageFlags            sql.NullInt16
2778
        Policy1ChannelFlags            sql.NullInt16
2779
        Policy1Signature               []byte
2780
        Policy2ID                      sql.NullInt64
2781
        Policy2NodeID                  sql.NullInt64
2782
        Policy2Version                 sql.NullInt16
2783
        Policy2Timelock                sql.NullInt32
2784
        Policy2FeePpm                  sql.NullInt64
2785
        Policy2BaseFeeMsat             sql.NullInt64
2786
        Policy2MinHtlcMsat             sql.NullInt64
2787
        Policy2MaxHtlcMsat             sql.NullInt64
2788
        Policy2LastUpdate              sql.NullInt64
2789
        Policy2Disabled                sql.NullBool
2790
        Policy2InboundBaseFeeMsat      sql.NullInt64
2791
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2792
        Policy2MessageFlags            sql.NullInt16
UNCOV
2793
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
2794
        Policy2Signature               []byte
×
UNCOV
2795
}
×
UNCOV
2796

×
2797
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
2798
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
2799
        if err != nil {
×
2800
                return nil, err
×
2801
        }
×
2802
        defer rows.Close()
×
2803
        var items []ListChannelsByNodeIDRow
×
2804
        for rows.Next() {
×
2805
                var i ListChannelsByNodeIDRow
×
2806
                if err := rows.Scan(
×
2807
                        &i.GraphChannel.ID,
×
2808
                        &i.GraphChannel.Version,
×
2809
                        &i.GraphChannel.Scid,
×
2810
                        &i.GraphChannel.NodeID1,
×
2811
                        &i.GraphChannel.NodeID2,
×
2812
                        &i.GraphChannel.Outpoint,
×
2813
                        &i.GraphChannel.Capacity,
×
2814
                        &i.GraphChannel.BitcoinKey1,
×
2815
                        &i.GraphChannel.BitcoinKey2,
×
2816
                        &i.GraphChannel.Node1Signature,
×
2817
                        &i.GraphChannel.Node2Signature,
×
2818
                        &i.GraphChannel.Bitcoin1Signature,
×
2819
                        &i.GraphChannel.Bitcoin2Signature,
×
2820
                        &i.Node1Pubkey,
×
2821
                        &i.Node2Pubkey,
×
2822
                        &i.Policy1ID,
×
2823
                        &i.Policy1NodeID,
×
2824
                        &i.Policy1Version,
×
2825
                        &i.Policy1Timelock,
×
2826
                        &i.Policy1FeePpm,
×
2827
                        &i.Policy1BaseFeeMsat,
×
2828
                        &i.Policy1MinHtlcMsat,
×
2829
                        &i.Policy1MaxHtlcMsat,
×
2830
                        &i.Policy1LastUpdate,
×
2831
                        &i.Policy1Disabled,
×
2832
                        &i.Policy1InboundBaseFeeMsat,
×
2833
                        &i.Policy1InboundFeeRateMilliMsat,
×
2834
                        &i.Policy1MessageFlags,
×
2835
                        &i.Policy1ChannelFlags,
×
2836
                        &i.Policy1Signature,
×
2837
                        &i.Policy2ID,
×
2838
                        &i.Policy2NodeID,
×
2839
                        &i.Policy2Version,
×
2840
                        &i.Policy2Timelock,
×
2841
                        &i.Policy2FeePpm,
×
2842
                        &i.Policy2BaseFeeMsat,
×
2843
                        &i.Policy2MinHtlcMsat,
×
2844
                        &i.Policy2MaxHtlcMsat,
×
2845
                        &i.Policy2LastUpdate,
×
2846
                        &i.Policy2Disabled,
×
2847
                        &i.Policy2InboundBaseFeeMsat,
×
2848
                        &i.Policy2InboundFeeRateMilliMsat,
×
2849
                        &i.Policy2MessageFlags,
×
2850
                        &i.Policy2ChannelFlags,
×
2851
                        &i.Policy2Signature,
×
2852
                ); err != nil {
2853
                        return nil, err
×
2854
                }
×
2855
                items = append(items, i)
×
UNCOV
2856
        }
×
2857
        if err := rows.Close(); err != nil {
×
2858
                return nil, err
×
2859
        }
×
2860
        if err := rows.Err(); err != nil {
2861
                return nil, err
2862
        }
2863
        return items, nil
2864
}
2865

2866
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
2867
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
2868
       n1.pub_key AS node1_pubkey,
2869
       n2.pub_key AS node2_pubkey,
2870

2871
       -- Policy 1
2872
       -- TODO(elle): use sqlc.embed to embed policy structs
2873
       --  once this issue is resolved:
2874
       --  https://github.com/sqlc-dev/sqlc/issues/2997
2875
       cp1.id AS policy1_id,
2876
       cp1.node_id AS policy1_node_id,
2877
       cp1.version AS policy1_version,
2878
       cp1.timelock AS policy1_timelock,
2879
       cp1.fee_ppm AS policy1_fee_ppm,
2880
       cp1.base_fee_msat AS policy1_base_fee_msat,
2881
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
2882
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
2883
       cp1.last_update AS policy1_last_update,
2884
       cp1.disabled AS policy1_disabled,
2885
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2886
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2887
       cp1.message_flags AS policy1_message_flags,
2888
       cp1.channel_flags AS policy1_channel_flags,
2889
       cp1.signature AS policy1_signature,
2890

2891
       -- Policy 2
2892
       cp2.id AS policy2_id,
2893
       cp2.node_id AS policy2_node_id,
2894
       cp2.version AS policy2_version,
2895
       cp2.timelock AS policy2_timelock,
2896
       cp2.fee_ppm AS policy2_fee_ppm,
2897
       cp2.base_fee_msat AS policy2_base_fee_msat,
2898
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
2899
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
2900
       cp2.last_update AS policy2_last_update,
2901
       cp2.disabled AS policy2_disabled,
2902
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2903
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2904
       cp2.message_flags AS policy2_message_flags,
2905
       cp2.channel_flags AS policy2_channel_flags,
2906
       cp2.signature AS policy2_signature
2907

2908
FROM graph_channels c
2909
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2910
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2911
         LEFT JOIN graph_channel_policies cp1
2912
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2913
         LEFT JOIN graph_channel_policies cp2
2914
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2915
WHERE c.version = $1
2916
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
2917
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
2918
`
2919

2920
type ListChannelsForNodeIDsParams struct {
2921
        Version  int16
2922
        Node1Ids []int64
2923
        Node2Ids []int64
2924
}
2925

2926
type ListChannelsForNodeIDsRow struct {
2927
        GraphChannel                   GraphChannel
2928
        Node1Pubkey                    []byte
2929
        Node2Pubkey                    []byte
2930
        Policy1ID                      sql.NullInt64
2931
        Policy1NodeID                  sql.NullInt64
2932
        Policy1Version                 sql.NullInt16
2933
        Policy1Timelock                sql.NullInt32
2934
        Policy1FeePpm                  sql.NullInt64
2935
        Policy1BaseFeeMsat             sql.NullInt64
2936
        Policy1MinHtlcMsat             sql.NullInt64
2937
        Policy1MaxHtlcMsat             sql.NullInt64
2938
        Policy1LastUpdate              sql.NullInt64
2939
        Policy1Disabled                sql.NullBool
2940
        Policy1InboundBaseFeeMsat      sql.NullInt64
2941
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2942
        Policy1MessageFlags            sql.NullInt16
2943
        Policy1ChannelFlags            sql.NullInt16
2944
        Policy1Signature               []byte
2945
        Policy2ID                      sql.NullInt64
2946
        Policy2NodeID                  sql.NullInt64
2947
        Policy2Version                 sql.NullInt16
2948
        Policy2Timelock                sql.NullInt32
2949
        Policy2FeePpm                  sql.NullInt64
2950
        Policy2BaseFeeMsat             sql.NullInt64
2951
        Policy2MinHtlcMsat             sql.NullInt64
2952
        Policy2MaxHtlcMsat             sql.NullInt64
2953
        Policy2LastUpdate              sql.NullInt64
2954
        Policy2Disabled                sql.NullBool
2955
        Policy2InboundBaseFeeMsat      sql.NullInt64
2956
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2957
        Policy2MessageFlags            sql.NullInt16
UNCOV
2958
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
2959
        Policy2Signature               []byte
×
UNCOV
2960
}
×
UNCOV
2961

×
2962
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
2963
        query := listChannelsForNodeIDs
×
2964
        var queryParams []interface{}
×
2965
        queryParams = append(queryParams, arg.Version)
×
2966
        if len(arg.Node1Ids) > 0 {
×
2967
                for _, v := range arg.Node1Ids {
×
2968
                        queryParams = append(queryParams, v)
×
2969
                }
×
2970
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
2971
        } else {
×
2972
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
2973
        }
×
2974
        if len(arg.Node2Ids) > 0 {
×
2975
                for _, v := range arg.Node2Ids {
×
2976
                        queryParams = append(queryParams, v)
×
2977
                }
×
2978
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
2979
        } else {
×
2980
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
2981
        }
×
2982
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2983
        if err != nil {
×
2984
                return nil, err
×
2985
        }
×
2986
        defer rows.Close()
×
2987
        var items []ListChannelsForNodeIDsRow
×
2988
        for rows.Next() {
×
2989
                var i ListChannelsForNodeIDsRow
×
2990
                if err := rows.Scan(
×
2991
                        &i.GraphChannel.ID,
×
2992
                        &i.GraphChannel.Version,
×
2993
                        &i.GraphChannel.Scid,
×
2994
                        &i.GraphChannel.NodeID1,
×
2995
                        &i.GraphChannel.NodeID2,
×
2996
                        &i.GraphChannel.Outpoint,
×
2997
                        &i.GraphChannel.Capacity,
×
2998
                        &i.GraphChannel.BitcoinKey1,
×
2999
                        &i.GraphChannel.BitcoinKey2,
×
3000
                        &i.GraphChannel.Node1Signature,
×
3001
                        &i.GraphChannel.Node2Signature,
×
3002
                        &i.GraphChannel.Bitcoin1Signature,
×
3003
                        &i.GraphChannel.Bitcoin2Signature,
×
3004
                        &i.Node1Pubkey,
×
3005
                        &i.Node2Pubkey,
×
3006
                        &i.Policy1ID,
×
3007
                        &i.Policy1NodeID,
×
3008
                        &i.Policy1Version,
×
3009
                        &i.Policy1Timelock,
×
3010
                        &i.Policy1FeePpm,
×
3011
                        &i.Policy1BaseFeeMsat,
×
3012
                        &i.Policy1MinHtlcMsat,
×
3013
                        &i.Policy1MaxHtlcMsat,
×
3014
                        &i.Policy1LastUpdate,
×
3015
                        &i.Policy1Disabled,
×
3016
                        &i.Policy1InboundBaseFeeMsat,
×
3017
                        &i.Policy1InboundFeeRateMilliMsat,
×
3018
                        &i.Policy1MessageFlags,
×
3019
                        &i.Policy1ChannelFlags,
×
3020
                        &i.Policy1Signature,
×
3021
                        &i.Policy2ID,
×
3022
                        &i.Policy2NodeID,
×
3023
                        &i.Policy2Version,
×
3024
                        &i.Policy2Timelock,
×
3025
                        &i.Policy2FeePpm,
×
3026
                        &i.Policy2BaseFeeMsat,
×
3027
                        &i.Policy2MinHtlcMsat,
×
3028
                        &i.Policy2MaxHtlcMsat,
×
3029
                        &i.Policy2LastUpdate,
×
3030
                        &i.Policy2Disabled,
×
3031
                        &i.Policy2InboundBaseFeeMsat,
×
3032
                        &i.Policy2InboundFeeRateMilliMsat,
×
3033
                        &i.Policy2MessageFlags,
×
3034
                        &i.Policy2ChannelFlags,
×
3035
                        &i.Policy2Signature,
×
3036
                ); err != nil {
3037
                        return nil, err
×
3038
                }
×
3039
                items = append(items, i)
×
UNCOV
3040
        }
×
3041
        if err := rows.Close(); err != nil {
×
3042
                return nil, err
×
3043
        }
×
3044
        if err := rows.Err(); err != nil {
3045
                return nil, err
3046
        }
3047
        return items, nil
3048
}
3049

3050
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3051
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3052
FROM graph_channels c
3053
WHERE c.version = $1 AND c.id > $2
3054
ORDER BY c.id
3055
LIMIT $3
3056
`
3057

3058
type ListChannelsPaginatedParams struct {
3059
        Version int16
3060
        ID      int64
3061
        Limit   int32
3062
}
3063

3064
type ListChannelsPaginatedRow struct {
3065
        ID          int64
3066
        BitcoinKey1 []byte
UNCOV
3067
        BitcoinKey2 []byte
×
UNCOV
3068
        Outpoint    string
×
UNCOV
3069
}
×
UNCOV
3070

×
3071
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3072
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3073
        if err != nil {
×
3074
                return nil, err
×
3075
        }
×
3076
        defer rows.Close()
×
3077
        var items []ListChannelsPaginatedRow
×
3078
        for rows.Next() {
×
3079
                var i ListChannelsPaginatedRow
×
3080
                if err := rows.Scan(
×
3081
                        &i.ID,
×
3082
                        &i.BitcoinKey1,
×
3083
                        &i.BitcoinKey2,
×
3084
                        &i.Outpoint,
×
3085
                ); err != nil {
3086
                        return nil, err
×
3087
                }
×
3088
                items = append(items, i)
×
UNCOV
3089
        }
×
3090
        if err := rows.Close(); err != nil {
×
3091
                return nil, err
×
3092
        }
×
3093
        if err := rows.Err(); err != nil {
3094
                return nil, err
3095
        }
3096
        return items, nil
3097
}
3098

3099
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3100
SELECT
3101
    c.id as id,
3102
    c.scid as scid,
3103
    c.capacity AS capacity,
3104

3105
    -- Join node pubkeys
3106
    n1.pub_key AS node1_pubkey,
3107
    n2.pub_key AS node2_pubkey,
3108

3109
    -- Node 1 policy
3110
    cp1.timelock AS policy_1_timelock,
3111
    cp1.fee_ppm AS policy_1_fee_ppm,
3112
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3113
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3114
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3115
    cp1.disabled AS policy_1_disabled,
3116
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3117
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3118
    cp1.message_flags AS policy1_message_flags,
3119
    cp1.channel_flags AS policy1_channel_flags,
3120

3121
    -- Node 2 policy
3122
    cp2.timelock AS policy_2_timelock,
3123
    cp2.fee_ppm AS policy_2_fee_ppm,
3124
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3125
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3126
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3127
    cp2.disabled AS policy_2_disabled,
3128
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3129
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3130
    cp2.message_flags AS policy2_message_flags,
3131
    cp2.channel_flags AS policy2_channel_flags
3132

3133
FROM graph_channels c
3134
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3135
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3136
LEFT JOIN graph_channel_policies cp1
3137
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3138
LEFT JOIN graph_channel_policies cp2
3139
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3140
WHERE c.version = $1 AND c.id > $2
3141
ORDER BY c.id
3142
LIMIT $3
3143
`
3144

3145
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3146
        Version int16
3147
        ID      int64
3148
        Limit   int32
3149
}
3150

3151
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3152
        ID                             int64
3153
        Scid                           []byte
3154
        Capacity                       sql.NullInt64
3155
        Node1Pubkey                    []byte
3156
        Node2Pubkey                    []byte
3157
        Policy1Timelock                sql.NullInt32
3158
        Policy1FeePpm                  sql.NullInt64
3159
        Policy1BaseFeeMsat             sql.NullInt64
3160
        Policy1MinHtlcMsat             sql.NullInt64
3161
        Policy1MaxHtlcMsat             sql.NullInt64
3162
        Policy1Disabled                sql.NullBool
3163
        Policy1InboundBaseFeeMsat      sql.NullInt64
3164
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3165
        Policy1MessageFlags            sql.NullInt16
3166
        Policy1ChannelFlags            sql.NullInt16
3167
        Policy2Timelock                sql.NullInt32
3168
        Policy2FeePpm                  sql.NullInt64
3169
        Policy2BaseFeeMsat             sql.NullInt64
3170
        Policy2MinHtlcMsat             sql.NullInt64
3171
        Policy2MaxHtlcMsat             sql.NullInt64
3172
        Policy2Disabled                sql.NullBool
3173
        Policy2InboundBaseFeeMsat      sql.NullInt64
3174
        Policy2InboundFeeRateMilliMsat sql.NullInt64
UNCOV
3175
        Policy2MessageFlags            sql.NullInt16
×
UNCOV
3176
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
3177
}
×
UNCOV
3178

×
3179
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3180
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3181
        if err != nil {
×
3182
                return nil, err
×
3183
        }
×
3184
        defer rows.Close()
×
3185
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3186
        for rows.Next() {
×
3187
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3188
                if err := rows.Scan(
×
3189
                        &i.ID,
×
3190
                        &i.Scid,
×
3191
                        &i.Capacity,
×
3192
                        &i.Node1Pubkey,
×
3193
                        &i.Node2Pubkey,
×
3194
                        &i.Policy1Timelock,
×
3195
                        &i.Policy1FeePpm,
×
3196
                        &i.Policy1BaseFeeMsat,
×
3197
                        &i.Policy1MinHtlcMsat,
×
3198
                        &i.Policy1MaxHtlcMsat,
×
3199
                        &i.Policy1Disabled,
×
3200
                        &i.Policy1InboundBaseFeeMsat,
×
3201
                        &i.Policy1InboundFeeRateMilliMsat,
×
3202
                        &i.Policy1MessageFlags,
×
3203
                        &i.Policy1ChannelFlags,
×
3204
                        &i.Policy2Timelock,
×
3205
                        &i.Policy2FeePpm,
×
3206
                        &i.Policy2BaseFeeMsat,
×
3207
                        &i.Policy2MinHtlcMsat,
×
3208
                        &i.Policy2MaxHtlcMsat,
×
3209
                        &i.Policy2Disabled,
×
3210
                        &i.Policy2InboundBaseFeeMsat,
×
3211
                        &i.Policy2InboundFeeRateMilliMsat,
×
3212
                        &i.Policy2MessageFlags,
×
3213
                        &i.Policy2ChannelFlags,
×
3214
                ); err != nil {
3215
                        return nil, err
×
3216
                }
×
3217
                items = append(items, i)
×
UNCOV
3218
        }
×
3219
        if err := rows.Close(); err != nil {
×
3220
                return nil, err
×
3221
        }
×
3222
        if err := rows.Err(); err != nil {
3223
                return nil, err
3224
        }
3225
        return items, nil
3226
}
3227

3228
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3229
SELECT
3230
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature,
3231

3232
    -- Join node pubkeys
3233
    n1.pub_key AS node1_pubkey,
3234
    n2.pub_key AS node2_pubkey,
3235

3236
    -- Node 1 policy
3237
    cp1.id AS policy_1_id,
3238
    cp1.node_id AS policy_1_node_id,
3239
    cp1.version AS policy_1_version,
3240
    cp1.timelock AS policy_1_timelock,
3241
    cp1.fee_ppm AS policy_1_fee_ppm,
3242
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3243
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3244
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3245
    cp1.last_update AS policy_1_last_update,
3246
    cp1.disabled AS policy_1_disabled,
3247
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3248
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3249
    cp1.message_flags AS policy1_message_flags,
3250
    cp1.channel_flags AS policy1_channel_flags,
3251
    cp1.signature AS policy_1_signature,
3252

3253
    -- Node 2 policy
3254
    cp2.id AS policy_2_id,
3255
    cp2.node_id AS policy_2_node_id,
3256
    cp2.version AS policy_2_version,
3257
    cp2.timelock AS policy_2_timelock,
3258
    cp2.fee_ppm AS policy_2_fee_ppm,
3259
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3260
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3261
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3262
    cp2.last_update AS policy_2_last_update,
3263
    cp2.disabled AS policy_2_disabled,
3264
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3265
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3266
    cp2.message_flags AS policy2_message_flags,
3267
    cp2.channel_flags AS policy2_channel_flags,
3268
    cp2.signature AS policy_2_signature
3269

3270
FROM graph_channels c
3271
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3272
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3273
LEFT JOIN graph_channel_policies cp1
3274
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3275
LEFT JOIN graph_channel_policies cp2
3276
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3277
WHERE c.version = $1 AND c.id > $2
3278
ORDER BY c.id
3279
LIMIT $3
3280
`
3281

3282
type ListChannelsWithPoliciesPaginatedParams struct {
3283
        Version int16
3284
        ID      int64
3285
        Limit   int32
3286
}
3287

3288
type ListChannelsWithPoliciesPaginatedRow struct {
3289
        GraphChannel                   GraphChannel
3290
        Node1Pubkey                    []byte
3291
        Node2Pubkey                    []byte
3292
        Policy1ID                      sql.NullInt64
3293
        Policy1NodeID                  sql.NullInt64
3294
        Policy1Version                 sql.NullInt16
3295
        Policy1Timelock                sql.NullInt32
3296
        Policy1FeePpm                  sql.NullInt64
3297
        Policy1BaseFeeMsat             sql.NullInt64
3298
        Policy1MinHtlcMsat             sql.NullInt64
3299
        Policy1MaxHtlcMsat             sql.NullInt64
3300
        Policy1LastUpdate              sql.NullInt64
3301
        Policy1Disabled                sql.NullBool
3302
        Policy1InboundBaseFeeMsat      sql.NullInt64
3303
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3304
        Policy1MessageFlags            sql.NullInt16
3305
        Policy1ChannelFlags            sql.NullInt16
3306
        Policy1Signature               []byte
3307
        Policy2ID                      sql.NullInt64
3308
        Policy2NodeID                  sql.NullInt64
3309
        Policy2Version                 sql.NullInt16
3310
        Policy2Timelock                sql.NullInt32
3311
        Policy2FeePpm                  sql.NullInt64
3312
        Policy2BaseFeeMsat             sql.NullInt64
3313
        Policy2MinHtlcMsat             sql.NullInt64
3314
        Policy2MaxHtlcMsat             sql.NullInt64
3315
        Policy2LastUpdate              sql.NullInt64
3316
        Policy2Disabled                sql.NullBool
3317
        Policy2InboundBaseFeeMsat      sql.NullInt64
3318
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3319
        Policy2MessageFlags            sql.NullInt16
UNCOV
3320
        Policy2ChannelFlags            sql.NullInt16
×
UNCOV
3321
        Policy2Signature               []byte
×
UNCOV
3322
}
×
UNCOV
3323

×
3324
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
3325
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
3326
        if err != nil {
×
3327
                return nil, err
×
3328
        }
×
3329
        defer rows.Close()
×
3330
        var items []ListChannelsWithPoliciesPaginatedRow
×
3331
        for rows.Next() {
×
3332
                var i ListChannelsWithPoliciesPaginatedRow
×
3333
                if err := rows.Scan(
×
3334
                        &i.GraphChannel.ID,
×
3335
                        &i.GraphChannel.Version,
×
3336
                        &i.GraphChannel.Scid,
×
3337
                        &i.GraphChannel.NodeID1,
×
3338
                        &i.GraphChannel.NodeID2,
×
3339
                        &i.GraphChannel.Outpoint,
×
3340
                        &i.GraphChannel.Capacity,
×
3341
                        &i.GraphChannel.BitcoinKey1,
×
3342
                        &i.GraphChannel.BitcoinKey2,
×
3343
                        &i.GraphChannel.Node1Signature,
×
3344
                        &i.GraphChannel.Node2Signature,
×
3345
                        &i.GraphChannel.Bitcoin1Signature,
×
3346
                        &i.GraphChannel.Bitcoin2Signature,
×
3347
                        &i.Node1Pubkey,
×
3348
                        &i.Node2Pubkey,
×
3349
                        &i.Policy1ID,
×
3350
                        &i.Policy1NodeID,
×
3351
                        &i.Policy1Version,
×
3352
                        &i.Policy1Timelock,
×
3353
                        &i.Policy1FeePpm,
×
3354
                        &i.Policy1BaseFeeMsat,
×
3355
                        &i.Policy1MinHtlcMsat,
×
3356
                        &i.Policy1MaxHtlcMsat,
×
3357
                        &i.Policy1LastUpdate,
×
3358
                        &i.Policy1Disabled,
×
3359
                        &i.Policy1InboundBaseFeeMsat,
×
3360
                        &i.Policy1InboundFeeRateMilliMsat,
×
3361
                        &i.Policy1MessageFlags,
×
3362
                        &i.Policy1ChannelFlags,
×
3363
                        &i.Policy1Signature,
×
3364
                        &i.Policy2ID,
×
3365
                        &i.Policy2NodeID,
×
3366
                        &i.Policy2Version,
×
3367
                        &i.Policy2Timelock,
×
3368
                        &i.Policy2FeePpm,
×
3369
                        &i.Policy2BaseFeeMsat,
×
3370
                        &i.Policy2MinHtlcMsat,
×
3371
                        &i.Policy2MaxHtlcMsat,
×
3372
                        &i.Policy2LastUpdate,
×
3373
                        &i.Policy2Disabled,
×
3374
                        &i.Policy2InboundBaseFeeMsat,
×
3375
                        &i.Policy2InboundFeeRateMilliMsat,
×
3376
                        &i.Policy2MessageFlags,
×
3377
                        &i.Policy2ChannelFlags,
×
3378
                        &i.Policy2Signature,
×
3379
                ); err != nil {
3380
                        return nil, err
×
3381
                }
×
3382
                items = append(items, i)
×
UNCOV
3383
        }
×
3384
        if err := rows.Close(); err != nil {
×
3385
                return nil, err
×
3386
        }
×
3387
        if err := rows.Err(); err != nil {
3388
                return nil, err
3389
        }
3390
        return items, nil
3391
}
3392

3393
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3394
SELECT id, pub_key
3395
FROM graph_nodes
3396
WHERE version = $1  AND id > $2
3397
ORDER BY id
3398
LIMIT $3
3399
`
3400

3401
type ListNodeIDsAndPubKeysParams struct {
3402
        Version int16
3403
        ID      int64
3404
        Limit   int32
3405
}
3406

3407
type ListNodeIDsAndPubKeysRow struct {
UNCOV
3408
        ID     int64
×
UNCOV
3409
        PubKey []byte
×
UNCOV
3410
}
×
UNCOV
3411

×
3412
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
3413
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
3414
        if err != nil {
×
3415
                return nil, err
×
3416
        }
×
3417
        defer rows.Close()
×
3418
        var items []ListNodeIDsAndPubKeysRow
×
3419
        for rows.Next() {
×
3420
                var i ListNodeIDsAndPubKeysRow
×
3421
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
3422
                        return nil, err
×
3423
                }
×
3424
                items = append(items, i)
×
UNCOV
3425
        }
×
3426
        if err := rows.Close(); err != nil {
×
3427
                return nil, err
×
3428
        }
×
3429
        if err := rows.Err(); err != nil {
3430
                return nil, err
3431
        }
3432
        return items, nil
3433
}
3434

3435
const listNodesPaginated = `-- name: ListNodesPaginated :many
3436
SELECT id, version, pub_key, alias, last_update, color, signature
3437
FROM graph_nodes
3438
WHERE version = $1 AND id > $2
3439
ORDER BY id
3440
LIMIT $3
3441
`
3442

3443
type ListNodesPaginatedParams struct {
3444
        Version int16
UNCOV
3445
        ID      int64
×
UNCOV
3446
        Limit   int32
×
UNCOV
3447
}
×
UNCOV
3448

×
3449
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
3450
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
3451
        if err != nil {
×
3452
                return nil, err
×
3453
        }
×
3454
        defer rows.Close()
×
3455
        var items []GraphNode
×
3456
        for rows.Next() {
×
3457
                var i GraphNode
×
3458
                if err := rows.Scan(
×
3459
                        &i.ID,
×
3460
                        &i.Version,
×
3461
                        &i.PubKey,
×
3462
                        &i.Alias,
×
3463
                        &i.LastUpdate,
×
3464
                        &i.Color,
×
3465
                        &i.Signature,
×
3466
                ); err != nil {
3467
                        return nil, err
×
3468
                }
×
3469
                items = append(items, i)
×
UNCOV
3470
        }
×
3471
        if err := rows.Close(); err != nil {
×
3472
                return nil, err
×
3473
        }
×
3474
        if err := rows.Err(); err != nil {
3475
                return nil, err
3476
        }
3477
        return items, nil
3478
}
3479

3480
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3481
/* ─────────────────────────────────────────────
3482
   graph_channel_policy_extra_types table queries
3483
   ─────────────────────────────────────────────
3484
*/
3485

3486
INSERT INTO graph_channel_policy_extra_types (
3487
    channel_policy_id, type, value
3488
)
3489
VALUES ($1, $2, $3)
3490
ON CONFLICT (channel_policy_id, type)
3491
    -- If a conflict occurs on channel_policy_id and type, then we update the
3492
    -- value.
3493
    DO UPDATE SET value = EXCLUDED.value
3494
`
3495

3496
type UpsertChanPolicyExtraTypeParams struct {
3497
        ChannelPolicyID int64
UNCOV
3498
        Type            int64
×
UNCOV
3499
        Value           []byte
×
UNCOV
3500
}
×
UNCOV
3501

×
3502
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
3503
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
3504
        return err
3505
}
3506

3507
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3508
/* ─────────────────────────────────────────────
3509
   graph_channel_extra_types table queries
3510
   ─────────────────────────────────────────────
3511
*/
3512

3513
INSERT INTO graph_channel_extra_types (
3514
    channel_id, type, value
3515
)
3516
VALUES ($1, $2, $3)
3517
    ON CONFLICT (channel_id, type)
3518
    -- Update the value if a conflict occurs on channel_id and type.
3519
    DO UPDATE SET value = EXCLUDED.value
3520
`
3521

3522
type UpsertChannelExtraTypeParams struct {
3523
        ChannelID int64
UNCOV
3524
        Type      int64
×
UNCOV
3525
        Value     []byte
×
UNCOV
3526
}
×
UNCOV
3527

×
3528
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
3529
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
3530
        return err
3531
}
3532

3533
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3534
/* ─────────────────────────────────────────────
3535
   graph_channel_policies table queries
3536
   ─────────────────────────────────────────────
3537
*/
3538

3539
INSERT INTO graph_channel_policies (
3540
    version, channel_id, node_id, timelock, fee_ppm,
3541
    base_fee_msat, min_htlc_msat, last_update, disabled,
3542
    max_htlc_msat, inbound_base_fee_msat,
3543
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3544
    signature
3545
) VALUES  (
3546
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3547
)
3548
ON CONFLICT (channel_id, node_id, version)
3549
    -- Update the following fields if a conflict occurs on channel_id,
3550
    -- node_id, and version.
3551
    DO UPDATE SET
3552
        timelock = EXCLUDED.timelock,
3553
        fee_ppm = EXCLUDED.fee_ppm,
3554
        base_fee_msat = EXCLUDED.base_fee_msat,
3555
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3556
        last_update = EXCLUDED.last_update,
3557
        disabled = EXCLUDED.disabled,
3558
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3559
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3560
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3561
        message_flags = EXCLUDED.message_flags,
3562
        channel_flags = EXCLUDED.channel_flags,
3563
        signature = EXCLUDED.signature
3564
WHERE EXCLUDED.last_update > graph_channel_policies.last_update
3565
RETURNING id
3566
`
3567

3568
type UpsertEdgePolicyParams struct {
3569
        Version                 int16
3570
        ChannelID               int64
3571
        NodeID                  int64
3572
        Timelock                int32
3573
        FeePpm                  int64
3574
        BaseFeeMsat             int64
3575
        MinHtlcMsat             int64
3576
        LastUpdate              sql.NullInt64
3577
        Disabled                sql.NullBool
3578
        MaxHtlcMsat             sql.NullInt64
3579
        InboundBaseFeeMsat      sql.NullInt64
3580
        InboundFeeRateMilliMsat sql.NullInt64
3581
        MessageFlags            sql.NullInt16
UNCOV
3582
        ChannelFlags            sql.NullInt16
×
UNCOV
3583
        Signature               []byte
×
UNCOV
3584
}
×
UNCOV
3585

×
3586
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
3587
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
3588
                arg.Version,
×
3589
                arg.ChannelID,
×
3590
                arg.NodeID,
×
3591
                arg.Timelock,
×
3592
                arg.FeePpm,
×
3593
                arg.BaseFeeMsat,
×
3594
                arg.MinHtlcMsat,
×
3595
                arg.LastUpdate,
×
3596
                arg.Disabled,
×
3597
                arg.MaxHtlcMsat,
×
3598
                arg.InboundBaseFeeMsat,
×
3599
                arg.InboundFeeRateMilliMsat,
×
3600
                arg.MessageFlags,
×
3601
                arg.ChannelFlags,
×
3602
                arg.Signature,
×
3603
        )
×
3604
        var id int64
3605
        err := row.Scan(&id)
3606
        return id, err
3607
}
3608

3609
const upsertNode = `-- name: UpsertNode :one
3610
/* ─────────────────────────────────────────────
3611
   graph_nodes table queries
3612
   ───────────────────────────��─────────────────
3613
*/
3614

3615
INSERT INTO graph_nodes (
3616
    version, pub_key, alias, last_update, color, signature
3617
) VALUES (
3618
    $1, $2, $3, $4, $5, $6
3619
)
3620
ON CONFLICT (pub_key, version)
3621
    -- Update the following fields if a conflict occurs on pub_key
3622
    -- and version.
3623
    DO UPDATE SET
3624
        alias = EXCLUDED.alias,
3625
        last_update = EXCLUDED.last_update,
3626
        color = EXCLUDED.color,
3627
        signature = EXCLUDED.signature
3628
WHERE graph_nodes.last_update IS NULL
3629
    OR EXCLUDED.last_update > graph_nodes.last_update
3630
RETURNING id
3631
`
3632

3633
type UpsertNodeParams struct {
3634
        Version    int16
3635
        PubKey     []byte
3636
        Alias      sql.NullString
3637
        LastUpdate sql.NullInt64
UNCOV
3638
        Color      sql.NullString
×
UNCOV
3639
        Signature  []byte
×
UNCOV
3640
}
×
UNCOV
3641

×
3642
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3643
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3644
                arg.Version,
×
3645
                arg.PubKey,
×
3646
                arg.Alias,
×
3647
                arg.LastUpdate,
×
3648
                arg.Color,
×
3649
                arg.Signature,
×
3650
        )
×
3651
        var id int64
3652
        err := row.Scan(&id)
3653
        return id, err
3654
}
3655

3656
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3657
/* ─────────────────────────────────────────────
3658
   graph_node_addresses table queries
3659
   ───────────────────────────────────��─────────
3660
*/
3661

3662
INSERT INTO graph_node_addresses (
3663
    node_id,
3664
    type,
3665
    address,
3666
    position
3667
) VALUES (
3668
    $1, $2, $3, $4
3669
) ON CONFLICT (node_id, type, position)
3670
    DO UPDATE SET address = EXCLUDED.address
3671
`
3672

3673
type UpsertNodeAddressParams struct {
3674
        NodeID   int64
3675
        Type     int16
UNCOV
3676
        Address  string
×
UNCOV
3677
        Position int32
×
UNCOV
3678
}
×
UNCOV
3679

×
3680
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3681
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3682
                arg.NodeID,
×
3683
                arg.Type,
×
3684
                arg.Address,
×
3685
                arg.Position,
3686
        )
3687
        return err
3688
}
3689

3690
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3691
/* ─────────────────────────────────────────────
3692
   graph_node_extra_types table queries
3693
   ─────────────────────────────────────────────
3694
*/
3695

3696
INSERT INTO graph_node_extra_types (
3697
    node_id, type, value
3698
)
3699
VALUES ($1, $2, $3)
3700
ON CONFLICT (type, node_id)
3701
    -- Update the value if a conflict occurs on type
3702
    -- and node_id.
3703
    DO UPDATE SET value = EXCLUDED.value
3704
`
3705

3706
type UpsertNodeExtraTypeParams struct {
3707
        NodeID int64
UNCOV
3708
        Type   int64
×
UNCOV
3709
        Value  []byte
×
UNCOV
3710
}
×
UNCOV
3711

×
3712
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
3713
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
3714
        return err
3715
}
3716

3717
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3718
/* ───────────────────────────���─────────────────
3719
    graph_prune_log table queries
3720
    ─────────────────────────────────────────────
3721
*/
3722

3723
INSERT INTO graph_prune_log (
3724
    block_height, block_hash
3725
) VALUES (
3726
    $1, $2
3727
)
3728
ON CONFLICT(block_height) DO UPDATE SET
3729
    block_hash = EXCLUDED.block_hash
3730
`
3731

3732
type UpsertPruneLogEntryParams struct {
UNCOV
3733
        BlockHeight int64
×
UNCOV
3734
        BlockHash   []byte
×
UNCOV
3735
}
×
UNCOV
3736

×
3737
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
3738
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
3739
        return err
3740
}
3741

3742
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
3743
/* ─────────────────────────────────────────────
3744
   graph_zombie_channels table queries
3745
   ─────────────────────────────────────────────
3746
*/
3747

3748
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
3749
VALUES ($1, $2, $3, $4)
3750
ON CONFLICT (scid, version)
3751
DO UPDATE SET
3752
    -- If a conflict exists for the SCID and version pair, then we
3753
    -- update the node keys.
3754
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
3755
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
3756
`
3757

3758
type UpsertZombieChannelParams struct {
3759
        Scid     []byte
3760
        Version  int16
UNCOV
3761
        NodeKey1 []byte
×
UNCOV
3762
        NodeKey2 []byte
×
UNCOV
3763
}
×
UNCOV
3764

×
3765
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
3766
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
3767
                arg.Scid,
×
3768
                arg.Version,
×
3769
                arg.NodeKey1,
×
3770
                arg.NodeKey2,
3771
        )
3772
        return err
3773
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc