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

lightningnetwork / lnd / 24070014837

07 Apr 2026 07:36AM UTC coverage: 61.983% (-0.1%) from 62.104%
24070014837

Pull #10714

github

web-flow
Merge 24c9c8d2e into 167f03bb3
Pull Request #10714: graph/db: cross-version graph Store queries

39 of 674 new or added lines in 4 files covered. (5.79%)

63 existing lines in 15 files now uncovered.

142839 of 230448 relevant lines covered (61.98%)

19101.53 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 addV2ChannelProof = `-- name: AddV2ChannelProof :execresult
59
UPDATE graph_channels
60
SET signature = $2
61
WHERE scid = $1
62
  AND version = 2
63
`
64

65
type AddV2ChannelProofParams struct {
66
        Scid      []byte
67
        Signature []byte
68
}
69

70
func (q *Queries) AddV2ChannelProof(ctx context.Context, arg AddV2ChannelProofParams) (sql.Result, error) {
×
71
        return q.db.ExecContext(ctx, addV2ChannelProof, arg.Scid, arg.Signature)
×
72
}
×
73

74
const countZombieChannels = `-- name: CountZombieChannels :one
75
SELECT COUNT(*)
76
FROM graph_zombie_channels
77
WHERE version = $1
78
`
79

80
func (q *Queries) CountZombieChannels(ctx context.Context, version int16) (int64, error) {
×
81
        row := q.db.QueryRowContext(ctx, countZombieChannels, version)
×
82
        var count int64
×
83
        err := row.Scan(&count)
×
84
        return count, err
×
85
}
×
86

87
const createChannel = `-- name: CreateChannel :one
88
/* ─────────────────────────────────────────────
89
   graph_channels table queries
90
   ─────────────────────────────────────────────
91
*/
92

93
INSERT INTO graph_channels (
94
    version, scid, node_id_1, node_id_2,
95
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
96
    node_1_signature, node_2_signature, bitcoin_1_signature,
97
    bitcoin_2_signature, signature, funding_pk_script, merkle_root_hash
98
) VALUES (
99
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
100
)
101
RETURNING id
102
`
103

104
type CreateChannelParams struct {
105
        Version           int16
106
        Scid              []byte
107
        NodeID1           int64
108
        NodeID2           int64
109
        Outpoint          string
110
        Capacity          sql.NullInt64
111
        BitcoinKey1       []byte
112
        BitcoinKey2       []byte
113
        Node1Signature    []byte
114
        Node2Signature    []byte
115
        Bitcoin1Signature []byte
116
        Bitcoin2Signature []byte
117
        Signature         []byte
118
        FundingPkScript   []byte
119
        MerkleRootHash    []byte
120
}
121

122
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (int64, error) {
×
123
        row := q.db.QueryRowContext(ctx, createChannel,
×
124
                arg.Version,
×
125
                arg.Scid,
×
126
                arg.NodeID1,
×
127
                arg.NodeID2,
×
128
                arg.Outpoint,
×
129
                arg.Capacity,
×
130
                arg.BitcoinKey1,
×
131
                arg.BitcoinKey2,
×
132
                arg.Node1Signature,
×
133
                arg.Node2Signature,
×
134
                arg.Bitcoin1Signature,
×
135
                arg.Bitcoin2Signature,
×
136
                arg.Signature,
×
137
                arg.FundingPkScript,
×
138
                arg.MerkleRootHash,
×
139
        )
×
140
        var id int64
×
141
        err := row.Scan(&id)
×
142
        return id, err
×
143
}
×
144

145
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
146
DELETE FROM graph_channel_policy_extra_types
147
WHERE channel_policy_id = $1
148
`
149

150
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
151
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
152
        return err
×
153
}
×
154

155
const deleteChannels = `-- name: DeleteChannels :exec
156
DELETE FROM graph_channels
157
WHERE id IN (/*SLICE:ids*/?)
158
`
159

160
func (q *Queries) DeleteChannels(ctx context.Context, ids []int64) error {
×
161
        query := deleteChannels
×
162
        var queryParams []interface{}
×
163
        if len(ids) > 0 {
×
164
                for _, v := range ids {
×
165
                        queryParams = append(queryParams, v)
×
166
                }
×
167
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
168
        } else {
×
169
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
170
        }
×
171
        _, err := q.db.ExecContext(ctx, query, queryParams...)
×
172
        return err
×
173
}
174

175
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
176
DELETE FROM graph_node_extra_types
177
WHERE node_id = $1
178
  AND type = $2
179
`
180

181
type DeleteExtraNodeTypeParams struct {
182
        NodeID int64
183
        Type   int64
184
}
185

186
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
187
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
188
        return err
×
189
}
×
190

191
const deleteNode = `-- name: DeleteNode :exec
192
DELETE FROM graph_nodes
193
WHERE id = $1
194
`
195

196
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
197
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
198
        return err
×
199
}
×
200

201
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
202
DELETE FROM graph_node_addresses
203
WHERE node_id = $1
204
`
205

206
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
207
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
208
        return err
×
209
}
×
210

211
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
212
DELETE FROM graph_nodes
213
WHERE pub_key = $1
214
  AND version = $2
215
`
216

217
type DeleteNodeByPubKeyParams struct {
218
        PubKey  []byte
219
        Version int16
220
}
221

222
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
223
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
224
}
×
225

226
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
227
DELETE FROM graph_node_features
228
WHERE node_id = $1
229
  AND feature_bit = $2
230
`
231

232
type DeleteNodeFeatureParams struct {
233
        NodeID     int64
234
        FeatureBit int32
235
}
236

237
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
238
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
239
        return err
×
240
}
×
241

242
const deletePreferredChannel = `-- name: DeletePreferredChannel :exec
243
DELETE FROM graph_preferred_channels WHERE scid = $1
244
`
245

NEW
246
func (q *Queries) DeletePreferredChannel(ctx context.Context, scid []byte) error {
×
NEW
247
        _, err := q.db.ExecContext(ctx, deletePreferredChannel, scid)
×
NEW
248
        return err
×
NEW
249
}
×
250

251
const deletePreferredNode = `-- name: DeletePreferredNode :exec
252
DELETE FROM graph_preferred_nodes WHERE pub_key = $1
253
`
254

NEW
255
func (q *Queries) DeletePreferredNode(ctx context.Context, pubKey []byte) error {
×
NEW
256
        _, err := q.db.ExecContext(ctx, deletePreferredNode, pubKey)
×
NEW
257
        return err
×
NEW
258
}
×
259

260
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
261
DELETE FROM graph_prune_log
262
WHERE block_height >= $1
263
  AND block_height <= $2
264
`
265

266
type DeletePruneLogEntriesInRangeParams struct {
267
        StartHeight int64
268
        EndHeight   int64
269
}
270

271
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
272
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
273
        return err
×
274
}
×
275

276
const deleteUnconnectedNodes = `-- name: DeleteUnconnectedNodes :many
277
DELETE FROM graph_nodes
278
WHERE
279
    -- Ignore any of our source nodes.
280
    NOT EXISTS (
281
        SELECT 1
282
        FROM graph_source_nodes sn
283
        WHERE sn.node_id = graph_nodes.id
284
    )
285
    -- Select all nodes that do not have any channels.
286
    AND NOT EXISTS (
287
        SELECT 1
288
        FROM graph_channels c
289
        WHERE c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id
290
) RETURNING pub_key
291
`
292

293
func (q *Queries) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) {
×
294
        rows, err := q.db.QueryContext(ctx, deleteUnconnectedNodes)
×
295
        if err != nil {
×
296
                return nil, err
×
297
        }
×
298
        defer rows.Close()
×
299
        var items [][]byte
×
300
        for rows.Next() {
×
301
                var pub_key []byte
×
302
                if err := rows.Scan(&pub_key); err != nil {
×
303
                        return nil, err
×
304
                }
×
305
                items = append(items, pub_key)
×
306
        }
307
        if err := rows.Close(); err != nil {
×
308
                return nil, err
×
309
        }
×
310
        if err := rows.Err(); err != nil {
×
311
                return nil, err
×
312
        }
×
313
        return items, nil
×
314
}
315

316
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
317
DELETE FROM graph_zombie_channels
318
WHERE scid = $1
319
AND version = $2
320
`
321

322
type DeleteZombieChannelParams struct {
323
        Scid    []byte
324
        Version int16
325
}
326

327
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
328
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
329
}
×
330

331
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
332
SELECT
333
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
334
    n1.pub_key AS node1_pub_key,
335
    n2.pub_key AS node2_pub_key
336
FROM graph_channels c
337
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
338
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
339
WHERE c.scid = $1
340
  AND c.version = $2
341
`
342

343
type GetChannelAndNodesBySCIDParams struct {
344
        Scid    []byte
345
        Version int16
346
}
347

348
type GetChannelAndNodesBySCIDRow struct {
349
        ID                int64
350
        Version           int16
351
        Scid              []byte
352
        NodeID1           int64
353
        NodeID2           int64
354
        Outpoint          string
355
        Capacity          sql.NullInt64
356
        BitcoinKey1       []byte
357
        BitcoinKey2       []byte
358
        Node1Signature    []byte
359
        Node2Signature    []byte
360
        Bitcoin1Signature []byte
361
        Bitcoin2Signature []byte
362
        Signature         []byte
363
        FundingPkScript   []byte
364
        MerkleRootHash    []byte
365
        Node1PubKey       []byte
366
        Node2PubKey       []byte
367
}
368

369
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
370
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
371
        var i GetChannelAndNodesBySCIDRow
×
372
        err := row.Scan(
×
373
                &i.ID,
×
374
                &i.Version,
×
375
                &i.Scid,
×
376
                &i.NodeID1,
×
377
                &i.NodeID2,
×
378
                &i.Outpoint,
×
379
                &i.Capacity,
×
380
                &i.BitcoinKey1,
×
381
                &i.BitcoinKey2,
×
382
                &i.Node1Signature,
×
383
                &i.Node2Signature,
×
384
                &i.Bitcoin1Signature,
×
385
                &i.Bitcoin2Signature,
×
386
                &i.Signature,
×
387
                &i.FundingPkScript,
×
388
                &i.MerkleRootHash,
×
389
                &i.Node1PubKey,
×
390
                &i.Node2PubKey,
×
391
        )
×
392
        return i, err
×
393
}
×
394

395
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
396
SELECT
397
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
398

399
    n1.pub_key AS node1_pubkey,
400
    n2.pub_key AS node2_pubkey,
401

402
    -- Node 1 policy
403
    cp1.id AS policy_1_id,
404
    cp1.node_id AS policy_1_node_id,
405
    cp1.version AS policy_1_version,
406
    cp1.timelock AS policy_1_timelock,
407
    cp1.fee_ppm AS policy_1_fee_ppm,
408
    cp1.base_fee_msat AS policy_1_base_fee_msat,
409
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
410
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
411
    cp1.last_update AS policy_1_last_update,
412
    cp1.disabled AS policy_1_disabled,
413
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
414
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
415
    cp1.message_flags AS policy_1_message_flags,
416
    cp1.channel_flags AS policy_1_channel_flags,
417
    cp1.signature AS policy_1_signature,
418
    cp1.block_height AS policy_1_block_height,
419
    cp1.disable_flags AS policy_1_disable_flags,
420

421
    -- Node 2 policy
422
    cp2.id AS policy_2_id,
423
    cp2.node_id AS policy_2_node_id,
424
    cp2.version AS policy_2_version,
425
    cp2.timelock AS policy_2_timelock,
426
    cp2.fee_ppm AS policy_2_fee_ppm,
427
    cp2.base_fee_msat AS policy_2_base_fee_msat,
428
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
429
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
430
    cp2.last_update AS policy_2_last_update,
431
    cp2.disabled AS policy_2_disabled,
432
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
433
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
434
    cp2.message_flags AS policy_2_message_flags,
435
    cp2.channel_flags AS policy_2_channel_flags,
436
    cp2.signature AS policy_2_signature,
437
    cp2.block_height AS policy_2_block_height,
438
    cp2.disable_flags AS policy_2_disable_flags
439
FROM graph_channels c
440
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
441
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
442
    LEFT JOIN graph_channel_policies cp1
443
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
444
    LEFT JOIN graph_channel_policies cp2
445
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
446
WHERE c.outpoint = $1 AND c.version = $2
447
`
448

449
type GetChannelByOutpointWithPoliciesParams struct {
450
        Outpoint string
451
        Version  int16
452
}
453

454
type GetChannelByOutpointWithPoliciesRow struct {
455
        GraphChannel                   GraphChannel
456
        Node1Pubkey                    []byte
457
        Node2Pubkey                    []byte
458
        Policy1ID                      sql.NullInt64
459
        Policy1NodeID                  sql.NullInt64
460
        Policy1Version                 sql.NullInt16
461
        Policy1Timelock                sql.NullInt32
462
        Policy1FeePpm                  sql.NullInt64
463
        Policy1BaseFeeMsat             sql.NullInt64
464
        Policy1MinHtlcMsat             sql.NullInt64
465
        Policy1MaxHtlcMsat             sql.NullInt64
466
        Policy1LastUpdate              sql.NullInt64
467
        Policy1Disabled                sql.NullBool
468
        Policy1InboundBaseFeeMsat      sql.NullInt64
469
        Policy1InboundFeeRateMilliMsat sql.NullInt64
470
        Policy1MessageFlags            sql.NullInt16
471
        Policy1ChannelFlags            sql.NullInt16
472
        Policy1Signature               []byte
473
        Policy1BlockHeight             sql.NullInt64
474
        Policy1DisableFlags            sql.NullInt16
475
        Policy2ID                      sql.NullInt64
476
        Policy2NodeID                  sql.NullInt64
477
        Policy2Version                 sql.NullInt16
478
        Policy2Timelock                sql.NullInt32
479
        Policy2FeePpm                  sql.NullInt64
480
        Policy2BaseFeeMsat             sql.NullInt64
481
        Policy2MinHtlcMsat             sql.NullInt64
482
        Policy2MaxHtlcMsat             sql.NullInt64
483
        Policy2LastUpdate              sql.NullInt64
484
        Policy2Disabled                sql.NullBool
485
        Policy2InboundBaseFeeMsat      sql.NullInt64
486
        Policy2InboundFeeRateMilliMsat sql.NullInt64
487
        Policy2MessageFlags            sql.NullInt16
488
        Policy2ChannelFlags            sql.NullInt16
489
        Policy2Signature               []byte
490
        Policy2BlockHeight             sql.NullInt64
491
        Policy2DisableFlags            sql.NullInt16
492
}
493

494
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
495
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
496
        var i GetChannelByOutpointWithPoliciesRow
×
497
        err := row.Scan(
×
498
                &i.GraphChannel.ID,
×
499
                &i.GraphChannel.Version,
×
500
                &i.GraphChannel.Scid,
×
501
                &i.GraphChannel.NodeID1,
×
502
                &i.GraphChannel.NodeID2,
×
503
                &i.GraphChannel.Outpoint,
×
504
                &i.GraphChannel.Capacity,
×
505
                &i.GraphChannel.BitcoinKey1,
×
506
                &i.GraphChannel.BitcoinKey2,
×
507
                &i.GraphChannel.Node1Signature,
×
508
                &i.GraphChannel.Node2Signature,
×
509
                &i.GraphChannel.Bitcoin1Signature,
×
510
                &i.GraphChannel.Bitcoin2Signature,
×
511
                &i.GraphChannel.Signature,
×
512
                &i.GraphChannel.FundingPkScript,
×
513
                &i.GraphChannel.MerkleRootHash,
×
514
                &i.Node1Pubkey,
×
515
                &i.Node2Pubkey,
×
516
                &i.Policy1ID,
×
517
                &i.Policy1NodeID,
×
518
                &i.Policy1Version,
×
519
                &i.Policy1Timelock,
×
520
                &i.Policy1FeePpm,
×
521
                &i.Policy1BaseFeeMsat,
×
522
                &i.Policy1MinHtlcMsat,
×
523
                &i.Policy1MaxHtlcMsat,
×
524
                &i.Policy1LastUpdate,
×
525
                &i.Policy1Disabled,
×
526
                &i.Policy1InboundBaseFeeMsat,
×
527
                &i.Policy1InboundFeeRateMilliMsat,
×
528
                &i.Policy1MessageFlags,
×
529
                &i.Policy1ChannelFlags,
×
530
                &i.Policy1Signature,
×
531
                &i.Policy1BlockHeight,
×
532
                &i.Policy1DisableFlags,
×
533
                &i.Policy2ID,
×
534
                &i.Policy2NodeID,
×
535
                &i.Policy2Version,
×
536
                &i.Policy2Timelock,
×
537
                &i.Policy2FeePpm,
×
538
                &i.Policy2BaseFeeMsat,
×
539
                &i.Policy2MinHtlcMsat,
×
540
                &i.Policy2MaxHtlcMsat,
×
541
                &i.Policy2LastUpdate,
×
542
                &i.Policy2Disabled,
×
543
                &i.Policy2InboundBaseFeeMsat,
×
544
                &i.Policy2InboundFeeRateMilliMsat,
×
545
                &i.Policy2MessageFlags,
×
546
                &i.Policy2ChannelFlags,
×
547
                &i.Policy2Signature,
×
548
                &i.Policy2BlockHeight,
×
549
                &i.Policy2DisableFlags,
×
550
        )
×
551
        return i, err
×
552
}
×
553

554
const getChannelBySCID = `-- name: GetChannelBySCID :one
555
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, signature, funding_pk_script, merkle_root_hash FROM graph_channels
556
WHERE scid = $1 AND version = $2
557
`
558

559
type GetChannelBySCIDParams struct {
560
        Scid    []byte
561
        Version int16
562
}
563

564
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error) {
×
565
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
566
        var i GraphChannel
×
567
        err := row.Scan(
×
568
                &i.ID,
×
569
                &i.Version,
×
570
                &i.Scid,
×
571
                &i.NodeID1,
×
572
                &i.NodeID2,
×
573
                &i.Outpoint,
×
574
                &i.Capacity,
×
575
                &i.BitcoinKey1,
×
576
                &i.BitcoinKey2,
×
577
                &i.Node1Signature,
×
578
                &i.Node2Signature,
×
579
                &i.Bitcoin1Signature,
×
580
                &i.Bitcoin2Signature,
×
581
                &i.Signature,
×
582
                &i.FundingPkScript,
×
583
                &i.MerkleRootHash,
×
584
        )
×
585
        return i, err
×
586
}
×
587

588
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
589
SELECT
590
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
591
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
592
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
593

594
    -- Policy 1
595
    cp1.id AS policy1_id,
596
    cp1.node_id AS policy1_node_id,
597
    cp1.version AS policy1_version,
598
    cp1.timelock AS policy1_timelock,
599
    cp1.fee_ppm AS policy1_fee_ppm,
600
    cp1.base_fee_msat AS policy1_base_fee_msat,
601
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
602
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
603
    cp1.last_update AS policy1_last_update,
604
    cp1.disabled AS policy1_disabled,
605
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
606
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
607
    cp1.message_flags AS policy1_message_flags,
608
    cp1.channel_flags AS policy1_channel_flags,
609
    cp1.signature AS policy1_signature,
610
    cp1.block_height AS policy1_block_height,
611
    cp1.disable_flags AS policy1_disable_flags,
612

613
    -- Policy 2
614
    cp2.id AS policy2_id,
615
    cp2.node_id AS policy2_node_id,
616
    cp2.version AS policy2_version,
617
    cp2.timelock AS policy2_timelock,
618
    cp2.fee_ppm AS policy2_fee_ppm,
619
    cp2.base_fee_msat AS policy2_base_fee_msat,
620
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
621
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
622
    cp2.last_update AS policy2_last_update,
623
    cp2.disabled AS policy2_disabled,
624
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
625
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
626
    cp2.message_flags AS policy_2_message_flags,
627
    cp2.channel_flags AS policy_2_channel_flags,
628
    cp2.signature AS policy2_signature,
629
    cp2.block_height AS policy2_block_height,
630
    cp2.disable_flags AS policy2_disable_flags
631

632
FROM graph_channels c
633
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
634
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
635
    LEFT JOIN graph_channel_policies cp1
636
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
637
    LEFT JOIN graph_channel_policies cp2
638
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
639
WHERE c.scid = $1
640
  AND c.version = $2
641
`
642

643
type GetChannelBySCIDWithPoliciesParams struct {
644
        Scid    []byte
645
        Version int16
646
}
647

648
type GetChannelBySCIDWithPoliciesRow struct {
649
        GraphChannel                   GraphChannel
650
        GraphNode                      GraphNode
651
        GraphNode_2                    GraphNode
652
        Policy1ID                      sql.NullInt64
653
        Policy1NodeID                  sql.NullInt64
654
        Policy1Version                 sql.NullInt16
655
        Policy1Timelock                sql.NullInt32
656
        Policy1FeePpm                  sql.NullInt64
657
        Policy1BaseFeeMsat             sql.NullInt64
658
        Policy1MinHtlcMsat             sql.NullInt64
659
        Policy1MaxHtlcMsat             sql.NullInt64
660
        Policy1LastUpdate              sql.NullInt64
661
        Policy1Disabled                sql.NullBool
662
        Policy1InboundBaseFeeMsat      sql.NullInt64
663
        Policy1InboundFeeRateMilliMsat sql.NullInt64
664
        Policy1MessageFlags            sql.NullInt16
665
        Policy1ChannelFlags            sql.NullInt16
666
        Policy1Signature               []byte
667
        Policy1BlockHeight             sql.NullInt64
668
        Policy1DisableFlags            sql.NullInt16
669
        Policy2ID                      sql.NullInt64
670
        Policy2NodeID                  sql.NullInt64
671
        Policy2Version                 sql.NullInt16
672
        Policy2Timelock                sql.NullInt32
673
        Policy2FeePpm                  sql.NullInt64
674
        Policy2BaseFeeMsat             sql.NullInt64
675
        Policy2MinHtlcMsat             sql.NullInt64
676
        Policy2MaxHtlcMsat             sql.NullInt64
677
        Policy2LastUpdate              sql.NullInt64
678
        Policy2Disabled                sql.NullBool
679
        Policy2InboundBaseFeeMsat      sql.NullInt64
680
        Policy2InboundFeeRateMilliMsat sql.NullInt64
681
        Policy2MessageFlags            sql.NullInt16
682
        Policy2ChannelFlags            sql.NullInt16
683
        Policy2Signature               []byte
684
        Policy2BlockHeight             sql.NullInt64
685
        Policy2DisableFlags            sql.NullInt16
686
}
687

688
func (q *Queries) GetChannelBySCIDWithPolicies(ctx context.Context, arg GetChannelBySCIDWithPoliciesParams) (GetChannelBySCIDWithPoliciesRow, error) {
×
689
        row := q.db.QueryRowContext(ctx, getChannelBySCIDWithPolicies, arg.Scid, arg.Version)
×
690
        var i GetChannelBySCIDWithPoliciesRow
×
691
        err := row.Scan(
×
692
                &i.GraphChannel.ID,
×
693
                &i.GraphChannel.Version,
×
694
                &i.GraphChannel.Scid,
×
695
                &i.GraphChannel.NodeID1,
×
696
                &i.GraphChannel.NodeID2,
×
697
                &i.GraphChannel.Outpoint,
×
698
                &i.GraphChannel.Capacity,
×
699
                &i.GraphChannel.BitcoinKey1,
×
700
                &i.GraphChannel.BitcoinKey2,
×
701
                &i.GraphChannel.Node1Signature,
×
702
                &i.GraphChannel.Node2Signature,
×
703
                &i.GraphChannel.Bitcoin1Signature,
×
704
                &i.GraphChannel.Bitcoin2Signature,
×
705
                &i.GraphChannel.Signature,
×
706
                &i.GraphChannel.FundingPkScript,
×
707
                &i.GraphChannel.MerkleRootHash,
×
708
                &i.GraphNode.ID,
×
709
                &i.GraphNode.Version,
×
710
                &i.GraphNode.PubKey,
×
711
                &i.GraphNode.Alias,
×
712
                &i.GraphNode.LastUpdate,
×
713
                &i.GraphNode.Color,
×
714
                &i.GraphNode.Signature,
×
715
                &i.GraphNode.BlockHeight,
×
716
                &i.GraphNode_2.ID,
×
717
                &i.GraphNode_2.Version,
×
718
                &i.GraphNode_2.PubKey,
×
719
                &i.GraphNode_2.Alias,
×
720
                &i.GraphNode_2.LastUpdate,
×
721
                &i.GraphNode_2.Color,
×
722
                &i.GraphNode_2.Signature,
×
723
                &i.GraphNode_2.BlockHeight,
×
724
                &i.Policy1ID,
×
725
                &i.Policy1NodeID,
×
726
                &i.Policy1Version,
×
727
                &i.Policy1Timelock,
×
728
                &i.Policy1FeePpm,
×
729
                &i.Policy1BaseFeeMsat,
×
730
                &i.Policy1MinHtlcMsat,
×
731
                &i.Policy1MaxHtlcMsat,
×
732
                &i.Policy1LastUpdate,
×
733
                &i.Policy1Disabled,
×
734
                &i.Policy1InboundBaseFeeMsat,
×
735
                &i.Policy1InboundFeeRateMilliMsat,
×
736
                &i.Policy1MessageFlags,
×
737
                &i.Policy1ChannelFlags,
×
738
                &i.Policy1Signature,
×
739
                &i.Policy1BlockHeight,
×
740
                &i.Policy1DisableFlags,
×
741
                &i.Policy2ID,
×
742
                &i.Policy2NodeID,
×
743
                &i.Policy2Version,
×
744
                &i.Policy2Timelock,
×
745
                &i.Policy2FeePpm,
×
746
                &i.Policy2BaseFeeMsat,
×
747
                &i.Policy2MinHtlcMsat,
×
748
                &i.Policy2MaxHtlcMsat,
×
749
                &i.Policy2LastUpdate,
×
750
                &i.Policy2Disabled,
×
751
                &i.Policy2InboundBaseFeeMsat,
×
752
                &i.Policy2InboundFeeRateMilliMsat,
×
753
                &i.Policy2MessageFlags,
×
754
                &i.Policy2ChannelFlags,
×
755
                &i.Policy2Signature,
×
756
                &i.Policy2BlockHeight,
×
757
                &i.Policy2DisableFlags,
×
758
        )
×
759
        return i, err
×
760
}
×
761

762
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
763
SELECT
764
    channel_id,
765
    type,
766
    value
767
FROM graph_channel_extra_types
768
WHERE channel_id IN (/*SLICE:chan_ids*/?)
769
ORDER BY channel_id, type
770
`
771

772
func (q *Queries) GetChannelExtrasBatch(ctx context.Context, chanIds []int64) ([]GraphChannelExtraType, error) {
×
773
        query := getChannelExtrasBatch
×
774
        var queryParams []interface{}
×
775
        if len(chanIds) > 0 {
×
776
                for _, v := range chanIds {
×
777
                        queryParams = append(queryParams, v)
×
778
                }
×
779
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
780
        } else {
×
781
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
782
        }
×
783
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
784
        if err != nil {
×
785
                return nil, err
×
786
        }
×
787
        defer rows.Close()
×
788
        var items []GraphChannelExtraType
×
789
        for rows.Next() {
×
790
                var i GraphChannelExtraType
×
791
                if err := rows.Scan(&i.ChannelID, &i.Type, &i.Value); err != nil {
×
792
                        return nil, err
×
793
                }
×
794
                items = append(items, i)
×
795
        }
796
        if err := rows.Close(); err != nil {
×
797
                return nil, err
×
798
        }
×
799
        if err := rows.Err(); err != nil {
×
800
                return nil, err
×
801
        }
×
802
        return items, nil
×
803
}
804

805
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
806
SELECT
807
    channel_id,
808
    feature_bit
809
FROM graph_channel_features
810
WHERE channel_id IN (/*SLICE:chan_ids*/?)
811
ORDER BY channel_id, feature_bit
812
`
813

814
func (q *Queries) GetChannelFeaturesBatch(ctx context.Context, chanIds []int64) ([]GraphChannelFeature, error) {
×
815
        query := getChannelFeaturesBatch
×
816
        var queryParams []interface{}
×
817
        if len(chanIds) > 0 {
×
818
                for _, v := range chanIds {
×
819
                        queryParams = append(queryParams, v)
×
820
                }
×
821
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
822
        } else {
×
823
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
824
        }
×
825
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
826
        if err != nil {
×
827
                return nil, err
×
828
        }
×
829
        defer rows.Close()
×
830
        var items []GraphChannelFeature
×
831
        for rows.Next() {
×
832
                var i GraphChannelFeature
×
833
                if err := rows.Scan(&i.ChannelID, &i.FeatureBit); err != nil {
×
834
                        return nil, err
×
835
                }
×
836
                items = append(items, i)
×
837
        }
838
        if err := rows.Close(); err != nil {
×
839
                return nil, err
×
840
        }
×
841
        if err := rows.Err(); err != nil {
×
842
                return nil, err
×
843
        }
×
844
        return items, nil
×
845
}
846

847
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
848
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, block_height, disable_flags
849
FROM graph_channel_policies
850
WHERE channel_id = $1
851
  AND node_id = $2
852
  AND version = $3
853
`
854

855
type GetChannelPolicyByChannelAndNodeParams struct {
856
        ChannelID int64
857
        NodeID    int64
858
        Version   int16
859
}
860

861
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (GraphChannelPolicy, error) {
×
862
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
863
        var i GraphChannelPolicy
×
864
        err := row.Scan(
×
865
                &i.ID,
×
866
                &i.Version,
×
867
                &i.ChannelID,
×
868
                &i.NodeID,
×
869
                &i.Timelock,
×
870
                &i.FeePpm,
×
871
                &i.BaseFeeMsat,
×
872
                &i.MinHtlcMsat,
×
873
                &i.MaxHtlcMsat,
×
874
                &i.LastUpdate,
×
875
                &i.Disabled,
×
876
                &i.InboundBaseFeeMsat,
×
877
                &i.InboundFeeRateMilliMsat,
×
878
                &i.MessageFlags,
×
879
                &i.ChannelFlags,
×
880
                &i.Signature,
×
881
                &i.BlockHeight,
×
882
                &i.DisableFlags,
×
883
        )
×
884
        return i, err
×
885
}
×
886

887
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
888
SELECT
889
    channel_policy_id as policy_id,
890
    type,
891
    value
892
FROM graph_channel_policy_extra_types
893
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
894
ORDER BY channel_policy_id, type
895
`
896

897
type GetChannelPolicyExtraTypesBatchRow struct {
898
        PolicyID int64
899
        Type     int64
900
        Value    []byte
901
}
902

903
func (q *Queries) GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]GetChannelPolicyExtraTypesBatchRow, error) {
×
904
        query := getChannelPolicyExtraTypesBatch
×
905
        var queryParams []interface{}
×
906
        if len(policyIds) > 0 {
×
907
                for _, v := range policyIds {
×
908
                        queryParams = append(queryParams, v)
×
909
                }
×
910
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", makeQueryParams(len(queryParams), len(policyIds)), 1)
×
911
        } else {
×
912
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", "NULL", 1)
×
913
        }
×
914
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
915
        if err != nil {
×
916
                return nil, err
×
917
        }
×
918
        defer rows.Close()
×
919
        var items []GetChannelPolicyExtraTypesBatchRow
×
920
        for rows.Next() {
×
921
                var i GetChannelPolicyExtraTypesBatchRow
×
922
                if err := rows.Scan(&i.PolicyID, &i.Type, &i.Value); err != nil {
×
923
                        return nil, err
×
924
                }
×
925
                items = append(items, i)
×
926
        }
927
        if err := rows.Close(); err != nil {
×
928
                return nil, err
×
929
        }
×
930
        if err := rows.Err(); err != nil {
×
931
                return nil, err
×
932
        }
×
933
        return items, nil
×
934
}
935

936
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
937
SELECT
938
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
939

940
    -- Minimal node data.
941
    n1.id AS node1_id,
942
    n1.pub_key AS node1_pub_key,
943
    n2.id AS node2_id,
944
    n2.pub_key AS node2_pub_key,
945

946
    -- Policy 1
947
    cp1.id AS policy1_id,
948
    cp1.node_id AS policy1_node_id,
949
    cp1.version AS policy1_version,
950
    cp1.timelock AS policy1_timelock,
951
    cp1.fee_ppm AS policy1_fee_ppm,
952
    cp1.base_fee_msat AS policy1_base_fee_msat,
953
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
954
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
955
    cp1.last_update AS policy1_last_update,
956
    cp1.disabled AS policy1_disabled,
957
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
958
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
959
    cp1.message_flags AS policy1_message_flags,
960
    cp1.channel_flags AS policy1_channel_flags,
961
    cp1.signature AS policy1_signature,
962
    cp1.block_height AS policy1_block_height,
963
    cp1.disable_flags AS policy1_disable_flags,
964

965
    -- Policy 2
966
    cp2.id AS policy2_id,
967
    cp2.node_id AS policy2_node_id,
968
    cp2.version AS policy2_version,
969
    cp2.timelock AS policy2_timelock,
970
    cp2.fee_ppm AS policy2_fee_ppm,
971
    cp2.base_fee_msat AS policy2_base_fee_msat,
972
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
973
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
974
    cp2.last_update AS policy2_last_update,
975
    cp2.disabled AS policy2_disabled,
976
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
977
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
978
    cp2.message_flags AS policy2_message_flags,
979
    cp2.channel_flags AS policy2_channel_flags,
980
    cp2.signature AS policy2_signature,
981
    cp2.block_height AS policy2_block_height,
982
    cp2.disable_flags AS policy2_disable_flags
983

984
FROM graph_channels c
985
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
986
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
987
    LEFT JOIN graph_channel_policies cp1
988
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
989
    LEFT JOIN graph_channel_policies cp2
990
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
991
WHERE c.id IN (/*SLICE:ids*/?)
992
`
993

994
type GetChannelsByIDsRow struct {
995
        GraphChannel                   GraphChannel
996
        Node1ID                        int64
997
        Node1PubKey                    []byte
998
        Node2ID                        int64
999
        Node2PubKey                    []byte
1000
        Policy1ID                      sql.NullInt64
1001
        Policy1NodeID                  sql.NullInt64
1002
        Policy1Version                 sql.NullInt16
1003
        Policy1Timelock                sql.NullInt32
1004
        Policy1FeePpm                  sql.NullInt64
1005
        Policy1BaseFeeMsat             sql.NullInt64
1006
        Policy1MinHtlcMsat             sql.NullInt64
1007
        Policy1MaxHtlcMsat             sql.NullInt64
1008
        Policy1LastUpdate              sql.NullInt64
1009
        Policy1Disabled                sql.NullBool
1010
        Policy1InboundBaseFeeMsat      sql.NullInt64
1011
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1012
        Policy1MessageFlags            sql.NullInt16
1013
        Policy1ChannelFlags            sql.NullInt16
1014
        Policy1Signature               []byte
1015
        Policy1BlockHeight             sql.NullInt64
1016
        Policy1DisableFlags            sql.NullInt16
1017
        Policy2ID                      sql.NullInt64
1018
        Policy2NodeID                  sql.NullInt64
1019
        Policy2Version                 sql.NullInt16
1020
        Policy2Timelock                sql.NullInt32
1021
        Policy2FeePpm                  sql.NullInt64
1022
        Policy2BaseFeeMsat             sql.NullInt64
1023
        Policy2MinHtlcMsat             sql.NullInt64
1024
        Policy2MaxHtlcMsat             sql.NullInt64
1025
        Policy2LastUpdate              sql.NullInt64
1026
        Policy2Disabled                sql.NullBool
1027
        Policy2InboundBaseFeeMsat      sql.NullInt64
1028
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1029
        Policy2MessageFlags            sql.NullInt16
1030
        Policy2ChannelFlags            sql.NullInt16
1031
        Policy2Signature               []byte
1032
        Policy2BlockHeight             sql.NullInt64
1033
        Policy2DisableFlags            sql.NullInt16
1034
}
1035

1036
func (q *Queries) GetChannelsByIDs(ctx context.Context, ids []int64) ([]GetChannelsByIDsRow, error) {
×
1037
        query := getChannelsByIDs
×
1038
        var queryParams []interface{}
×
1039
        if len(ids) > 0 {
×
1040
                for _, v := range ids {
×
1041
                        queryParams = append(queryParams, v)
×
1042
                }
×
1043
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1044
        } else {
×
1045
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1046
        }
×
1047
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1048
        if err != nil {
×
1049
                return nil, err
×
1050
        }
×
1051
        defer rows.Close()
×
1052
        var items []GetChannelsByIDsRow
×
1053
        for rows.Next() {
×
1054
                var i GetChannelsByIDsRow
×
1055
                if err := rows.Scan(
×
1056
                        &i.GraphChannel.ID,
×
1057
                        &i.GraphChannel.Version,
×
1058
                        &i.GraphChannel.Scid,
×
1059
                        &i.GraphChannel.NodeID1,
×
1060
                        &i.GraphChannel.NodeID2,
×
1061
                        &i.GraphChannel.Outpoint,
×
1062
                        &i.GraphChannel.Capacity,
×
1063
                        &i.GraphChannel.BitcoinKey1,
×
1064
                        &i.GraphChannel.BitcoinKey2,
×
1065
                        &i.GraphChannel.Node1Signature,
×
1066
                        &i.GraphChannel.Node2Signature,
×
1067
                        &i.GraphChannel.Bitcoin1Signature,
×
1068
                        &i.GraphChannel.Bitcoin2Signature,
×
1069
                        &i.GraphChannel.Signature,
×
1070
                        &i.GraphChannel.FundingPkScript,
×
1071
                        &i.GraphChannel.MerkleRootHash,
×
1072
                        &i.Node1ID,
×
1073
                        &i.Node1PubKey,
×
1074
                        &i.Node2ID,
×
1075
                        &i.Node2PubKey,
×
1076
                        &i.Policy1ID,
×
1077
                        &i.Policy1NodeID,
×
1078
                        &i.Policy1Version,
×
1079
                        &i.Policy1Timelock,
×
1080
                        &i.Policy1FeePpm,
×
1081
                        &i.Policy1BaseFeeMsat,
×
1082
                        &i.Policy1MinHtlcMsat,
×
1083
                        &i.Policy1MaxHtlcMsat,
×
1084
                        &i.Policy1LastUpdate,
×
1085
                        &i.Policy1Disabled,
×
1086
                        &i.Policy1InboundBaseFeeMsat,
×
1087
                        &i.Policy1InboundFeeRateMilliMsat,
×
1088
                        &i.Policy1MessageFlags,
×
1089
                        &i.Policy1ChannelFlags,
×
1090
                        &i.Policy1Signature,
×
1091
                        &i.Policy1BlockHeight,
×
1092
                        &i.Policy1DisableFlags,
×
1093
                        &i.Policy2ID,
×
1094
                        &i.Policy2NodeID,
×
1095
                        &i.Policy2Version,
×
1096
                        &i.Policy2Timelock,
×
1097
                        &i.Policy2FeePpm,
×
1098
                        &i.Policy2BaseFeeMsat,
×
1099
                        &i.Policy2MinHtlcMsat,
×
1100
                        &i.Policy2MaxHtlcMsat,
×
1101
                        &i.Policy2LastUpdate,
×
1102
                        &i.Policy2Disabled,
×
1103
                        &i.Policy2InboundBaseFeeMsat,
×
1104
                        &i.Policy2InboundFeeRateMilliMsat,
×
1105
                        &i.Policy2MessageFlags,
×
1106
                        &i.Policy2ChannelFlags,
×
1107
                        &i.Policy2Signature,
×
1108
                        &i.Policy2BlockHeight,
×
1109
                        &i.Policy2DisableFlags,
×
1110
                ); err != nil {
×
1111
                        return nil, err
×
1112
                }
×
1113
                items = append(items, i)
×
1114
        }
1115
        if err := rows.Close(); err != nil {
×
1116
                return nil, err
×
1117
        }
×
1118
        if err := rows.Err(); err != nil {
×
1119
                return nil, err
×
1120
        }
×
1121
        return items, nil
×
1122
}
1123

1124
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1125
SELECT
1126
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
1127
    n1.pub_key AS node1_pubkey,
1128
    n2.pub_key AS node2_pubkey
1129
FROM graph_channels c
1130
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1131
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1132
WHERE c.outpoint IN
1133
    (/*SLICE:outpoints*/?)
1134
`
1135

1136
type GetChannelsByOutpointsRow struct {
1137
        GraphChannel GraphChannel
1138
        Node1Pubkey  []byte
1139
        Node2Pubkey  []byte
1140
}
1141

1142
func (q *Queries) GetChannelsByOutpoints(ctx context.Context, outpoints []string) ([]GetChannelsByOutpointsRow, error) {
×
1143
        query := getChannelsByOutpoints
×
1144
        var queryParams []interface{}
×
1145
        if len(outpoints) > 0 {
×
1146
                for _, v := range outpoints {
×
1147
                        queryParams = append(queryParams, v)
×
1148
                }
×
1149
                query = strings.Replace(query, "/*SLICE:outpoints*/?", makeQueryParams(len(queryParams), len(outpoints)), 1)
×
1150
        } else {
×
1151
                query = strings.Replace(query, "/*SLICE:outpoints*/?", "NULL", 1)
×
1152
        }
×
1153
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1154
        if err != nil {
×
1155
                return nil, err
×
1156
        }
×
1157
        defer rows.Close()
×
1158
        var items []GetChannelsByOutpointsRow
×
1159
        for rows.Next() {
×
1160
                var i GetChannelsByOutpointsRow
×
1161
                if err := rows.Scan(
×
1162
                        &i.GraphChannel.ID,
×
1163
                        &i.GraphChannel.Version,
×
1164
                        &i.GraphChannel.Scid,
×
1165
                        &i.GraphChannel.NodeID1,
×
1166
                        &i.GraphChannel.NodeID2,
×
1167
                        &i.GraphChannel.Outpoint,
×
1168
                        &i.GraphChannel.Capacity,
×
1169
                        &i.GraphChannel.BitcoinKey1,
×
1170
                        &i.GraphChannel.BitcoinKey2,
×
1171
                        &i.GraphChannel.Node1Signature,
×
1172
                        &i.GraphChannel.Node2Signature,
×
1173
                        &i.GraphChannel.Bitcoin1Signature,
×
1174
                        &i.GraphChannel.Bitcoin2Signature,
×
1175
                        &i.GraphChannel.Signature,
×
1176
                        &i.GraphChannel.FundingPkScript,
×
1177
                        &i.GraphChannel.MerkleRootHash,
×
1178
                        &i.Node1Pubkey,
×
1179
                        &i.Node2Pubkey,
×
1180
                ); err != nil {
×
1181
                        return nil, err
×
1182
                }
×
1183
                items = append(items, i)
×
1184
        }
1185
        if err := rows.Close(); err != nil {
×
1186
                return nil, err
×
1187
        }
×
1188
        if err := rows.Err(); err != nil {
×
1189
                return nil, err
×
1190
        }
×
1191
        return items, nil
×
1192
}
1193

1194
const getChannelsByPolicyBlockRange = `-- name: GetChannelsByPolicyBlockRange :many
1195
SELECT
1196
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
1197
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1198
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1199

1200
    -- Policy 1 (node_id_1)
1201
    cp1.id AS policy1_id,
1202
    cp1.node_id AS policy1_node_id,
1203
    cp1.version AS policy1_version,
1204
    cp1.timelock AS policy1_timelock,
1205
    cp1.fee_ppm AS policy1_fee_ppm,
1206
    cp1.base_fee_msat AS policy1_base_fee_msat,
1207
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1208
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1209
    cp1.last_update AS policy1_last_update,
1210
    cp1.disabled AS policy1_disabled,
1211
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1212
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1213
    cp1.message_flags AS policy1_message_flags,
1214
    cp1.channel_flags AS policy1_channel_flags,
1215
    cp1.signature AS policy1_signature,
1216
    cp1.block_height AS policy1_block_height,
1217
    cp1.disable_flags AS policy1_disable_flags,
1218

1219
    -- Policy 2 (node_id_2)
1220
    cp2.id AS policy2_id,
1221
    cp2.node_id AS policy2_node_id,
1222
    cp2.version AS policy2_version,
1223
    cp2.timelock AS policy2_timelock,
1224
    cp2.fee_ppm AS policy2_fee_ppm,
1225
    cp2.base_fee_msat AS policy2_base_fee_msat,
1226
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1227
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1228
    cp2.last_update AS policy2_last_update,
1229
    cp2.disabled AS policy2_disabled,
1230
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1231
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1232
    cp2.message_flags AS policy2_message_flags,
1233
    cp2.channel_flags AS policy2_channel_flags,
1234
    cp2.signature AS policy2_signature,
1235
    cp2.block_height AS policy2_block_height,
1236
    cp2.disable_flags AS policy2_disable_flags
1237

1238
FROM graph_channels c
1239
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1240
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1241
    LEFT JOIN graph_channel_policies cp1
1242
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1243
    LEFT JOIN graph_channel_policies cp2
1244
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1245
WHERE c.version = $1
1246
  AND (
1247
       (cp1.block_height >= $2 AND cp1.block_height < $3)
1248
       OR
1249
       (cp2.block_height >= $2 AND cp2.block_height < $3)
1250
  )
1251
  -- Pagination using compound cursor (max_block_height, id).
1252
  -- We use COALESCE with -1 as sentinel since block heights are always positive.
1253
  AND (
1254
       (CASE
1255
           WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1256
               THEN COALESCE(cp1.block_height, 0)
1257
           ELSE COALESCE(cp2.block_height, 0)
1258
       END > COALESCE($4, -1))
1259
       OR
1260
       (CASE
1261
           WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1262
               THEN COALESCE(cp1.block_height, 0)
1263
           ELSE COALESCE(cp2.block_height, 0)
1264
       END = COALESCE($4, -1)
1265
       AND c.id > COALESCE($5, -1))
1266
  )
1267
ORDER BY
1268
    CASE
1269
        WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1270
            THEN COALESCE(cp1.block_height, 0)
1271
        ELSE COALESCE(cp2.block_height, 0)
1272
    END ASC,
1273
    c.id ASC
1274
LIMIT COALESCE($6, 999999999)
1275
`
1276

1277
type GetChannelsByPolicyBlockRangeParams struct {
1278
        Version         int16
1279
        StartHeight     sql.NullInt64
1280
        EndHeight       sql.NullInt64
1281
        LastBlockHeight sql.NullInt64
1282
        LastID          sql.NullInt64
1283
        MaxResults      interface{}
1284
}
1285

1286
type GetChannelsByPolicyBlockRangeRow struct {
1287
        GraphChannel                   GraphChannel
1288
        GraphNode                      GraphNode
1289
        GraphNode_2                    GraphNode
1290
        Policy1ID                      sql.NullInt64
1291
        Policy1NodeID                  sql.NullInt64
1292
        Policy1Version                 sql.NullInt16
1293
        Policy1Timelock                sql.NullInt32
1294
        Policy1FeePpm                  sql.NullInt64
1295
        Policy1BaseFeeMsat             sql.NullInt64
1296
        Policy1MinHtlcMsat             sql.NullInt64
1297
        Policy1MaxHtlcMsat             sql.NullInt64
1298
        Policy1LastUpdate              sql.NullInt64
1299
        Policy1Disabled                sql.NullBool
1300
        Policy1InboundBaseFeeMsat      sql.NullInt64
1301
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1302
        Policy1MessageFlags            sql.NullInt16
1303
        Policy1ChannelFlags            sql.NullInt16
1304
        Policy1Signature               []byte
1305
        Policy1BlockHeight             sql.NullInt64
1306
        Policy1DisableFlags            sql.NullInt16
1307
        Policy2ID                      sql.NullInt64
1308
        Policy2NodeID                  sql.NullInt64
1309
        Policy2Version                 sql.NullInt16
1310
        Policy2Timelock                sql.NullInt32
1311
        Policy2FeePpm                  sql.NullInt64
1312
        Policy2BaseFeeMsat             sql.NullInt64
1313
        Policy2MinHtlcMsat             sql.NullInt64
1314
        Policy2MaxHtlcMsat             sql.NullInt64
1315
        Policy2LastUpdate              sql.NullInt64
1316
        Policy2Disabled                sql.NullBool
1317
        Policy2InboundBaseFeeMsat      sql.NullInt64
1318
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1319
        Policy2MessageFlags            sql.NullInt16
1320
        Policy2ChannelFlags            sql.NullInt16
1321
        Policy2Signature               []byte
1322
        Policy2BlockHeight             sql.NullInt64
1323
        Policy2DisableFlags            sql.NullInt16
1324
}
1325

1326
func (q *Queries) GetChannelsByPolicyBlockRange(ctx context.Context, arg GetChannelsByPolicyBlockRangeParams) ([]GetChannelsByPolicyBlockRangeRow, error) {
×
1327
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyBlockRange,
×
1328
                arg.Version,
×
1329
                arg.StartHeight,
×
1330
                arg.EndHeight,
×
1331
                arg.LastBlockHeight,
×
1332
                arg.LastID,
×
1333
                arg.MaxResults,
×
1334
        )
×
1335
        if err != nil {
×
1336
                return nil, err
×
1337
        }
×
1338
        defer rows.Close()
×
1339
        var items []GetChannelsByPolicyBlockRangeRow
×
1340
        for rows.Next() {
×
1341
                var i GetChannelsByPolicyBlockRangeRow
×
1342
                if err := rows.Scan(
×
1343
                        &i.GraphChannel.ID,
×
1344
                        &i.GraphChannel.Version,
×
1345
                        &i.GraphChannel.Scid,
×
1346
                        &i.GraphChannel.NodeID1,
×
1347
                        &i.GraphChannel.NodeID2,
×
1348
                        &i.GraphChannel.Outpoint,
×
1349
                        &i.GraphChannel.Capacity,
×
1350
                        &i.GraphChannel.BitcoinKey1,
×
1351
                        &i.GraphChannel.BitcoinKey2,
×
1352
                        &i.GraphChannel.Node1Signature,
×
1353
                        &i.GraphChannel.Node2Signature,
×
1354
                        &i.GraphChannel.Bitcoin1Signature,
×
1355
                        &i.GraphChannel.Bitcoin2Signature,
×
1356
                        &i.GraphChannel.Signature,
×
1357
                        &i.GraphChannel.FundingPkScript,
×
1358
                        &i.GraphChannel.MerkleRootHash,
×
1359
                        &i.GraphNode.ID,
×
1360
                        &i.GraphNode.Version,
×
1361
                        &i.GraphNode.PubKey,
×
1362
                        &i.GraphNode.Alias,
×
1363
                        &i.GraphNode.LastUpdate,
×
1364
                        &i.GraphNode.Color,
×
1365
                        &i.GraphNode.Signature,
×
1366
                        &i.GraphNode.BlockHeight,
×
1367
                        &i.GraphNode_2.ID,
×
1368
                        &i.GraphNode_2.Version,
×
1369
                        &i.GraphNode_2.PubKey,
×
1370
                        &i.GraphNode_2.Alias,
×
1371
                        &i.GraphNode_2.LastUpdate,
×
1372
                        &i.GraphNode_2.Color,
×
1373
                        &i.GraphNode_2.Signature,
×
1374
                        &i.GraphNode_2.BlockHeight,
×
1375
                        &i.Policy1ID,
×
1376
                        &i.Policy1NodeID,
×
1377
                        &i.Policy1Version,
×
1378
                        &i.Policy1Timelock,
×
1379
                        &i.Policy1FeePpm,
×
1380
                        &i.Policy1BaseFeeMsat,
×
1381
                        &i.Policy1MinHtlcMsat,
×
1382
                        &i.Policy1MaxHtlcMsat,
×
1383
                        &i.Policy1LastUpdate,
×
1384
                        &i.Policy1Disabled,
×
1385
                        &i.Policy1InboundBaseFeeMsat,
×
1386
                        &i.Policy1InboundFeeRateMilliMsat,
×
1387
                        &i.Policy1MessageFlags,
×
1388
                        &i.Policy1ChannelFlags,
×
1389
                        &i.Policy1Signature,
×
1390
                        &i.Policy1BlockHeight,
×
1391
                        &i.Policy1DisableFlags,
×
1392
                        &i.Policy2ID,
×
1393
                        &i.Policy2NodeID,
×
1394
                        &i.Policy2Version,
×
1395
                        &i.Policy2Timelock,
×
1396
                        &i.Policy2FeePpm,
×
1397
                        &i.Policy2BaseFeeMsat,
×
1398
                        &i.Policy2MinHtlcMsat,
×
1399
                        &i.Policy2MaxHtlcMsat,
×
1400
                        &i.Policy2LastUpdate,
×
1401
                        &i.Policy2Disabled,
×
1402
                        &i.Policy2InboundBaseFeeMsat,
×
1403
                        &i.Policy2InboundFeeRateMilliMsat,
×
1404
                        &i.Policy2MessageFlags,
×
1405
                        &i.Policy2ChannelFlags,
×
1406
                        &i.Policy2Signature,
×
1407
                        &i.Policy2BlockHeight,
×
1408
                        &i.Policy2DisableFlags,
×
1409
                ); err != nil {
×
1410
                        return nil, err
×
1411
                }
×
1412
                items = append(items, i)
×
1413
        }
1414
        if err := rows.Close(); err != nil {
×
1415
                return nil, err
×
1416
        }
×
1417
        if err := rows.Err(); err != nil {
×
1418
                return nil, err
×
1419
        }
×
1420
        return items, nil
×
1421
}
1422

1423
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1424
SELECT
1425
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
1426
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1427
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1428

1429
    -- Policy 1 (node_id_1)
1430
    cp1.id AS policy1_id,
1431
    cp1.node_id AS policy1_node_id,
1432
    cp1.version AS policy1_version,
1433
    cp1.timelock AS policy1_timelock,
1434
    cp1.fee_ppm AS policy1_fee_ppm,
1435
    cp1.base_fee_msat AS policy1_base_fee_msat,
1436
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1437
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1438
    cp1.last_update AS policy1_last_update,
1439
    cp1.disabled AS policy1_disabled,
1440
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1441
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1442
    cp1.message_flags AS policy1_message_flags,
1443
    cp1.channel_flags AS policy1_channel_flags,
1444
    cp1.signature AS policy1_signature,
1445
    cp1.block_height AS policy1_block_height,
1446
    cp1.disable_flags AS policy1_disable_flags,
1447

1448
    -- Policy 2 (node_id_2)
1449
    cp2.id AS policy2_id,
1450
    cp2.node_id AS policy2_node_id,
1451
    cp2.version AS policy2_version,
1452
    cp2.timelock AS policy2_timelock,
1453
    cp2.fee_ppm AS policy2_fee_ppm,
1454
    cp2.base_fee_msat AS policy2_base_fee_msat,
1455
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1456
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1457
    cp2.last_update AS policy2_last_update,
1458
    cp2.disabled AS policy2_disabled,
1459
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1460
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1461
    cp2.message_flags AS policy2_message_flags,
1462
    cp2.channel_flags AS policy2_channel_flags,
1463
    cp2.signature AS policy2_signature,
1464
    cp2.block_height AS policy2_block_height,
1465
    cp2.disable_flags AS policy2_disable_flags
1466

1467
FROM graph_channels c
1468
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1469
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1470
    LEFT JOIN graph_channel_policies cp1
1471
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1472
    LEFT JOIN graph_channel_policies cp2
1473
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1474
WHERE c.version = $1
1475
  AND (
1476
       (cp1.last_update >= $2 AND cp1.last_update < $3)
1477
       OR
1478
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1479
  )
1480
  -- Pagination using compound cursor (max_update_time, id).
1481
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1482
  AND (
1483
       (CASE
1484
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1485
               THEN COALESCE(cp1.last_update, 0)
1486
           ELSE COALESCE(cp2.last_update, 0)
1487
       END > COALESCE($4, -1))
1488
       OR 
1489
       (CASE
1490
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1491
               THEN COALESCE(cp1.last_update, 0)
1492
           ELSE COALESCE(cp2.last_update, 0)
1493
       END = COALESCE($4, -1) 
1494
       AND c.id > COALESCE($5, -1))
1495
  )
1496
ORDER BY
1497
    CASE
1498
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1499
            THEN COALESCE(cp1.last_update, 0)
1500
        ELSE COALESCE(cp2.last_update, 0)
1501
    END ASC,
1502
    c.id ASC
1503
LIMIT COALESCE($6, 999999999)
1504
`
1505

1506
type GetChannelsByPolicyLastUpdateRangeParams struct {
1507
        Version        int16
1508
        StartTime      sql.NullInt64
1509
        EndTime        sql.NullInt64
1510
        LastUpdateTime sql.NullInt64
1511
        LastID         sql.NullInt64
1512
        MaxResults     interface{}
1513
}
1514

1515
type GetChannelsByPolicyLastUpdateRangeRow struct {
1516
        GraphChannel                   GraphChannel
1517
        GraphNode                      GraphNode
1518
        GraphNode_2                    GraphNode
1519
        Policy1ID                      sql.NullInt64
1520
        Policy1NodeID                  sql.NullInt64
1521
        Policy1Version                 sql.NullInt16
1522
        Policy1Timelock                sql.NullInt32
1523
        Policy1FeePpm                  sql.NullInt64
1524
        Policy1BaseFeeMsat             sql.NullInt64
1525
        Policy1MinHtlcMsat             sql.NullInt64
1526
        Policy1MaxHtlcMsat             sql.NullInt64
1527
        Policy1LastUpdate              sql.NullInt64
1528
        Policy1Disabled                sql.NullBool
1529
        Policy1InboundBaseFeeMsat      sql.NullInt64
1530
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1531
        Policy1MessageFlags            sql.NullInt16
1532
        Policy1ChannelFlags            sql.NullInt16
1533
        Policy1Signature               []byte
1534
        Policy1BlockHeight             sql.NullInt64
1535
        Policy1DisableFlags            sql.NullInt16
1536
        Policy2ID                      sql.NullInt64
1537
        Policy2NodeID                  sql.NullInt64
1538
        Policy2Version                 sql.NullInt16
1539
        Policy2Timelock                sql.NullInt32
1540
        Policy2FeePpm                  sql.NullInt64
1541
        Policy2BaseFeeMsat             sql.NullInt64
1542
        Policy2MinHtlcMsat             sql.NullInt64
1543
        Policy2MaxHtlcMsat             sql.NullInt64
1544
        Policy2LastUpdate              sql.NullInt64
1545
        Policy2Disabled                sql.NullBool
1546
        Policy2InboundBaseFeeMsat      sql.NullInt64
1547
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1548
        Policy2MessageFlags            sql.NullInt16
1549
        Policy2ChannelFlags            sql.NullInt16
1550
        Policy2Signature               []byte
1551
        Policy2BlockHeight             sql.NullInt64
1552
        Policy2DisableFlags            sql.NullInt16
1553
}
1554

1555
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
1556
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange,
×
1557
                arg.Version,
×
1558
                arg.StartTime,
×
1559
                arg.EndTime,
×
1560
                arg.LastUpdateTime,
×
1561
                arg.LastID,
×
1562
                arg.MaxResults,
×
1563
        )
×
1564
        if err != nil {
×
1565
                return nil, err
×
1566
        }
×
1567
        defer rows.Close()
×
1568
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
1569
        for rows.Next() {
×
1570
                var i GetChannelsByPolicyLastUpdateRangeRow
×
1571
                if err := rows.Scan(
×
1572
                        &i.GraphChannel.ID,
×
1573
                        &i.GraphChannel.Version,
×
1574
                        &i.GraphChannel.Scid,
×
1575
                        &i.GraphChannel.NodeID1,
×
1576
                        &i.GraphChannel.NodeID2,
×
1577
                        &i.GraphChannel.Outpoint,
×
1578
                        &i.GraphChannel.Capacity,
×
1579
                        &i.GraphChannel.BitcoinKey1,
×
1580
                        &i.GraphChannel.BitcoinKey2,
×
1581
                        &i.GraphChannel.Node1Signature,
×
1582
                        &i.GraphChannel.Node2Signature,
×
1583
                        &i.GraphChannel.Bitcoin1Signature,
×
1584
                        &i.GraphChannel.Bitcoin2Signature,
×
1585
                        &i.GraphChannel.Signature,
×
1586
                        &i.GraphChannel.FundingPkScript,
×
1587
                        &i.GraphChannel.MerkleRootHash,
×
1588
                        &i.GraphNode.ID,
×
1589
                        &i.GraphNode.Version,
×
1590
                        &i.GraphNode.PubKey,
×
1591
                        &i.GraphNode.Alias,
×
1592
                        &i.GraphNode.LastUpdate,
×
1593
                        &i.GraphNode.Color,
×
1594
                        &i.GraphNode.Signature,
×
1595
                        &i.GraphNode.BlockHeight,
×
1596
                        &i.GraphNode_2.ID,
×
1597
                        &i.GraphNode_2.Version,
×
1598
                        &i.GraphNode_2.PubKey,
×
1599
                        &i.GraphNode_2.Alias,
×
1600
                        &i.GraphNode_2.LastUpdate,
×
1601
                        &i.GraphNode_2.Color,
×
1602
                        &i.GraphNode_2.Signature,
×
1603
                        &i.GraphNode_2.BlockHeight,
×
1604
                        &i.Policy1ID,
×
1605
                        &i.Policy1NodeID,
×
1606
                        &i.Policy1Version,
×
1607
                        &i.Policy1Timelock,
×
1608
                        &i.Policy1FeePpm,
×
1609
                        &i.Policy1BaseFeeMsat,
×
1610
                        &i.Policy1MinHtlcMsat,
×
1611
                        &i.Policy1MaxHtlcMsat,
×
1612
                        &i.Policy1LastUpdate,
×
1613
                        &i.Policy1Disabled,
×
1614
                        &i.Policy1InboundBaseFeeMsat,
×
1615
                        &i.Policy1InboundFeeRateMilliMsat,
×
1616
                        &i.Policy1MessageFlags,
×
1617
                        &i.Policy1ChannelFlags,
×
1618
                        &i.Policy1Signature,
×
1619
                        &i.Policy1BlockHeight,
×
1620
                        &i.Policy1DisableFlags,
×
1621
                        &i.Policy2ID,
×
1622
                        &i.Policy2NodeID,
×
1623
                        &i.Policy2Version,
×
1624
                        &i.Policy2Timelock,
×
1625
                        &i.Policy2FeePpm,
×
1626
                        &i.Policy2BaseFeeMsat,
×
1627
                        &i.Policy2MinHtlcMsat,
×
1628
                        &i.Policy2MaxHtlcMsat,
×
1629
                        &i.Policy2LastUpdate,
×
1630
                        &i.Policy2Disabled,
×
1631
                        &i.Policy2InboundBaseFeeMsat,
×
1632
                        &i.Policy2InboundFeeRateMilliMsat,
×
1633
                        &i.Policy2MessageFlags,
×
1634
                        &i.Policy2ChannelFlags,
×
1635
                        &i.Policy2Signature,
×
1636
                        &i.Policy2BlockHeight,
×
1637
                        &i.Policy2DisableFlags,
×
1638
                ); err != nil {
×
1639
                        return nil, err
×
1640
                }
×
1641
                items = append(items, i)
×
1642
        }
1643
        if err := rows.Close(); err != nil {
×
1644
                return nil, err
×
1645
        }
×
1646
        if err := rows.Err(); err != nil {
×
1647
                return nil, err
×
1648
        }
×
1649
        return items, nil
×
1650
}
1651

1652
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1653
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, c.signature, c.funding_pk_script, c.merkle_root_hash,
1654
    n1.pub_key AS node1_pub_key,
1655
    n2.pub_key AS node2_pub_key
1656
FROM graph_channels c
1657
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1658
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1659
WHERE scid >= $1
1660
  AND scid < $2
1661
`
1662

1663
type GetChannelsBySCIDRangeParams struct {
1664
        StartScid []byte
1665
        EndScid   []byte
1666
}
1667

1668
type GetChannelsBySCIDRangeRow struct {
1669
        GraphChannel GraphChannel
1670
        Node1PubKey  []byte
1671
        Node2PubKey  []byte
1672
}
1673

1674
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1675
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1676
        if err != nil {
×
1677
                return nil, err
×
1678
        }
×
1679
        defer rows.Close()
×
1680
        var items []GetChannelsBySCIDRangeRow
×
1681
        for rows.Next() {
×
1682
                var i GetChannelsBySCIDRangeRow
×
1683
                if err := rows.Scan(
×
1684
                        &i.GraphChannel.ID,
×
1685
                        &i.GraphChannel.Version,
×
1686
                        &i.GraphChannel.Scid,
×
1687
                        &i.GraphChannel.NodeID1,
×
1688
                        &i.GraphChannel.NodeID2,
×
1689
                        &i.GraphChannel.Outpoint,
×
1690
                        &i.GraphChannel.Capacity,
×
1691
                        &i.GraphChannel.BitcoinKey1,
×
1692
                        &i.GraphChannel.BitcoinKey2,
×
1693
                        &i.GraphChannel.Node1Signature,
×
1694
                        &i.GraphChannel.Node2Signature,
×
1695
                        &i.GraphChannel.Bitcoin1Signature,
×
1696
                        &i.GraphChannel.Bitcoin2Signature,
×
1697
                        &i.GraphChannel.Signature,
×
1698
                        &i.GraphChannel.FundingPkScript,
×
1699
                        &i.GraphChannel.MerkleRootHash,
×
1700
                        &i.Node1PubKey,
×
1701
                        &i.Node2PubKey,
×
1702
                ); err != nil {
×
1703
                        return nil, err
×
1704
                }
×
1705
                items = append(items, i)
×
1706
        }
1707
        if err := rows.Close(); err != nil {
×
1708
                return nil, err
×
1709
        }
×
1710
        if err := rows.Err(); err != nil {
×
1711
                return nil, err
×
1712
        }
×
1713
        return items, nil
×
1714
}
1715

1716
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1717
SELECT
1718
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
1719
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1720
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1721

1722
    -- Policy 1
1723
    cp1.id AS policy1_id,
1724
    cp1.node_id AS policy1_node_id,
1725
    cp1.version AS policy1_version,
1726
    cp1.timelock AS policy1_timelock,
1727
    cp1.fee_ppm AS policy1_fee_ppm,
1728
    cp1.base_fee_msat AS policy1_base_fee_msat,
1729
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1730
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1731
    cp1.last_update AS policy1_last_update,
1732
    cp1.disabled AS policy1_disabled,
1733
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1734
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1735
    cp1.message_flags AS policy1_message_flags,
1736
    cp1.channel_flags AS policy1_channel_flags,
1737
    cp1.signature AS policy1_signature,
1738
    cp1.block_height AS policy1_block_height,
1739
    cp1.disable_flags AS policy1_disable_flags,
1740

1741
    -- Policy 2
1742
    cp2.id AS policy2_id,
1743
    cp2.node_id AS policy2_node_id,
1744
    cp2.version AS policy2_version,
1745
    cp2.timelock AS policy2_timelock,
1746
    cp2.fee_ppm AS policy2_fee_ppm,
1747
    cp2.base_fee_msat AS policy2_base_fee_msat,
1748
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1749
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1750
    cp2.last_update AS policy2_last_update,
1751
    cp2.disabled AS policy2_disabled,
1752
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1753
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1754
    cp2.message_flags AS policy_2_message_flags,
1755
    cp2.channel_flags AS policy_2_channel_flags,
1756
    cp2.signature AS policy2_signature,
1757
    cp2.block_height AS policy2_block_height,
1758
    cp2.disable_flags AS policy2_disable_flags
1759

1760
FROM graph_channels c
1761
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1762
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1763
    LEFT JOIN graph_channel_policies cp1
1764
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1765
    LEFT JOIN graph_channel_policies cp2
1766
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1767
WHERE
1768
    c.version = $1
1769
  AND c.scid IN (/*SLICE:scids*/?)
1770
`
1771

1772
type GetChannelsBySCIDWithPoliciesParams struct {
1773
        Version int16
1774
        Scids   [][]byte
1775
}
1776

1777
type GetChannelsBySCIDWithPoliciesRow struct {
1778
        GraphChannel                   GraphChannel
1779
        GraphNode                      GraphNode
1780
        GraphNode_2                    GraphNode
1781
        Policy1ID                      sql.NullInt64
1782
        Policy1NodeID                  sql.NullInt64
1783
        Policy1Version                 sql.NullInt16
1784
        Policy1Timelock                sql.NullInt32
1785
        Policy1FeePpm                  sql.NullInt64
1786
        Policy1BaseFeeMsat             sql.NullInt64
1787
        Policy1MinHtlcMsat             sql.NullInt64
1788
        Policy1MaxHtlcMsat             sql.NullInt64
1789
        Policy1LastUpdate              sql.NullInt64
1790
        Policy1Disabled                sql.NullBool
1791
        Policy1InboundBaseFeeMsat      sql.NullInt64
1792
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1793
        Policy1MessageFlags            sql.NullInt16
1794
        Policy1ChannelFlags            sql.NullInt16
1795
        Policy1Signature               []byte
1796
        Policy1BlockHeight             sql.NullInt64
1797
        Policy1DisableFlags            sql.NullInt16
1798
        Policy2ID                      sql.NullInt64
1799
        Policy2NodeID                  sql.NullInt64
1800
        Policy2Version                 sql.NullInt16
1801
        Policy2Timelock                sql.NullInt32
1802
        Policy2FeePpm                  sql.NullInt64
1803
        Policy2BaseFeeMsat             sql.NullInt64
1804
        Policy2MinHtlcMsat             sql.NullInt64
1805
        Policy2MaxHtlcMsat             sql.NullInt64
1806
        Policy2LastUpdate              sql.NullInt64
1807
        Policy2Disabled                sql.NullBool
1808
        Policy2InboundBaseFeeMsat      sql.NullInt64
1809
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1810
        Policy2MessageFlags            sql.NullInt16
1811
        Policy2ChannelFlags            sql.NullInt16
1812
        Policy2Signature               []byte
1813
        Policy2BlockHeight             sql.NullInt64
1814
        Policy2DisableFlags            sql.NullInt16
1815
}
1816

1817
func (q *Queries) GetChannelsBySCIDWithPolicies(ctx context.Context, arg GetChannelsBySCIDWithPoliciesParams) ([]GetChannelsBySCIDWithPoliciesRow, error) {
×
1818
        query := getChannelsBySCIDWithPolicies
×
1819
        var queryParams []interface{}
×
1820
        queryParams = append(queryParams, arg.Version)
×
1821
        if len(arg.Scids) > 0 {
×
1822
                for _, v := range arg.Scids {
×
1823
                        queryParams = append(queryParams, v)
×
1824
                }
×
1825
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1826
        } else {
×
1827
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1828
        }
×
1829
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1830
        if err != nil {
×
1831
                return nil, err
×
1832
        }
×
1833
        defer rows.Close()
×
1834
        var items []GetChannelsBySCIDWithPoliciesRow
×
1835
        for rows.Next() {
×
1836
                var i GetChannelsBySCIDWithPoliciesRow
×
1837
                if err := rows.Scan(
×
1838
                        &i.GraphChannel.ID,
×
1839
                        &i.GraphChannel.Version,
×
1840
                        &i.GraphChannel.Scid,
×
1841
                        &i.GraphChannel.NodeID1,
×
1842
                        &i.GraphChannel.NodeID2,
×
1843
                        &i.GraphChannel.Outpoint,
×
1844
                        &i.GraphChannel.Capacity,
×
1845
                        &i.GraphChannel.BitcoinKey1,
×
1846
                        &i.GraphChannel.BitcoinKey2,
×
1847
                        &i.GraphChannel.Node1Signature,
×
1848
                        &i.GraphChannel.Node2Signature,
×
1849
                        &i.GraphChannel.Bitcoin1Signature,
×
1850
                        &i.GraphChannel.Bitcoin2Signature,
×
1851
                        &i.GraphChannel.Signature,
×
1852
                        &i.GraphChannel.FundingPkScript,
×
1853
                        &i.GraphChannel.MerkleRootHash,
×
1854
                        &i.GraphNode.ID,
×
1855
                        &i.GraphNode.Version,
×
1856
                        &i.GraphNode.PubKey,
×
1857
                        &i.GraphNode.Alias,
×
1858
                        &i.GraphNode.LastUpdate,
×
1859
                        &i.GraphNode.Color,
×
1860
                        &i.GraphNode.Signature,
×
1861
                        &i.GraphNode.BlockHeight,
×
1862
                        &i.GraphNode_2.ID,
×
1863
                        &i.GraphNode_2.Version,
×
1864
                        &i.GraphNode_2.PubKey,
×
1865
                        &i.GraphNode_2.Alias,
×
1866
                        &i.GraphNode_2.LastUpdate,
×
1867
                        &i.GraphNode_2.Color,
×
1868
                        &i.GraphNode_2.Signature,
×
1869
                        &i.GraphNode_2.BlockHeight,
×
1870
                        &i.Policy1ID,
×
1871
                        &i.Policy1NodeID,
×
1872
                        &i.Policy1Version,
×
1873
                        &i.Policy1Timelock,
×
1874
                        &i.Policy1FeePpm,
×
1875
                        &i.Policy1BaseFeeMsat,
×
1876
                        &i.Policy1MinHtlcMsat,
×
1877
                        &i.Policy1MaxHtlcMsat,
×
1878
                        &i.Policy1LastUpdate,
×
1879
                        &i.Policy1Disabled,
×
1880
                        &i.Policy1InboundBaseFeeMsat,
×
1881
                        &i.Policy1InboundFeeRateMilliMsat,
×
1882
                        &i.Policy1MessageFlags,
×
1883
                        &i.Policy1ChannelFlags,
×
1884
                        &i.Policy1Signature,
×
1885
                        &i.Policy1BlockHeight,
×
1886
                        &i.Policy1DisableFlags,
×
1887
                        &i.Policy2ID,
×
1888
                        &i.Policy2NodeID,
×
1889
                        &i.Policy2Version,
×
1890
                        &i.Policy2Timelock,
×
1891
                        &i.Policy2FeePpm,
×
1892
                        &i.Policy2BaseFeeMsat,
×
1893
                        &i.Policy2MinHtlcMsat,
×
1894
                        &i.Policy2MaxHtlcMsat,
×
1895
                        &i.Policy2LastUpdate,
×
1896
                        &i.Policy2Disabled,
×
1897
                        &i.Policy2InboundBaseFeeMsat,
×
1898
                        &i.Policy2InboundFeeRateMilliMsat,
×
1899
                        &i.Policy2MessageFlags,
×
1900
                        &i.Policy2ChannelFlags,
×
1901
                        &i.Policy2Signature,
×
1902
                        &i.Policy2BlockHeight,
×
1903
                        &i.Policy2DisableFlags,
×
1904
                ); err != nil {
×
1905
                        return nil, err
×
1906
                }
×
1907
                items = append(items, i)
×
1908
        }
1909
        if err := rows.Close(); err != nil {
×
1910
                return nil, err
×
1911
        }
×
1912
        if err := rows.Err(); err != nil {
×
1913
                return nil, err
×
1914
        }
×
1915
        return items, nil
×
1916
}
1917

1918
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1919
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, signature, funding_pk_script, merkle_root_hash FROM graph_channels
1920
WHERE version = $1
1921
  AND scid IN (/*SLICE:scids*/?)
1922
`
1923

1924
type GetChannelsBySCIDsParams struct {
1925
        Version int16
1926
        Scids   [][]byte
1927
}
1928

1929
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
1930
        query := getChannelsBySCIDs
×
1931
        var queryParams []interface{}
×
1932
        queryParams = append(queryParams, arg.Version)
×
1933
        if len(arg.Scids) > 0 {
×
1934
                for _, v := range arg.Scids {
×
1935
                        queryParams = append(queryParams, v)
×
1936
                }
×
1937
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1938
        } else {
×
1939
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1940
        }
×
1941
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1942
        if err != nil {
×
1943
                return nil, err
×
1944
        }
×
1945
        defer rows.Close()
×
1946
        var items []GraphChannel
×
1947
        for rows.Next() {
×
1948
                var i GraphChannel
×
1949
                if err := rows.Scan(
×
1950
                        &i.ID,
×
1951
                        &i.Version,
×
1952
                        &i.Scid,
×
1953
                        &i.NodeID1,
×
1954
                        &i.NodeID2,
×
1955
                        &i.Outpoint,
×
1956
                        &i.Capacity,
×
1957
                        &i.BitcoinKey1,
×
1958
                        &i.BitcoinKey2,
×
1959
                        &i.Node1Signature,
×
1960
                        &i.Node2Signature,
×
1961
                        &i.Bitcoin1Signature,
×
1962
                        &i.Bitcoin2Signature,
×
1963
                        &i.Signature,
×
1964
                        &i.FundingPkScript,
×
1965
                        &i.MerkleRootHash,
×
1966
                ); err != nil {
×
1967
                        return nil, err
×
1968
                }
×
1969
                items = append(items, i)
×
1970
        }
1971
        if err := rows.Close(); err != nil {
×
1972
                return nil, err
×
1973
        }
×
1974
        if err := rows.Err(); err != nil {
×
1975
                return nil, err
×
1976
        }
×
1977
        return items, nil
×
1978
}
1979

1980
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1981
SELECT scid
1982
FROM graph_closed_scids
1983
WHERE scid IN (/*SLICE:scids*/?)
1984
`
1985

1986
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
1987
        query := getClosedChannelsSCIDs
×
1988
        var queryParams []interface{}
×
1989
        if len(scids) > 0 {
×
1990
                for _, v := range scids {
×
1991
                        queryParams = append(queryParams, v)
×
1992
                }
×
1993
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
1994
        } else {
×
1995
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1996
        }
×
1997
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1998
        if err != nil {
×
1999
                return nil, err
×
2000
        }
×
2001
        defer rows.Close()
×
2002
        var items [][]byte
×
2003
        for rows.Next() {
×
2004
                var scid []byte
×
2005
                if err := rows.Scan(&scid); err != nil {
×
2006
                        return nil, err
×
2007
                }
×
2008
                items = append(items, scid)
×
2009
        }
2010
        if err := rows.Close(); err != nil {
×
2011
                return nil, err
×
2012
        }
×
2013
        if err := rows.Err(); err != nil {
×
2014
                return nil, err
×
2015
        }
×
2016
        return items, nil
×
2017
}
2018

2019
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
2020
SELECT node_id, type, value
2021
FROM graph_node_extra_types
2022
WHERE node_id = $1
2023
`
2024

2025
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
2026
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
2027
        if err != nil {
×
2028
                return nil, err
×
2029
        }
×
2030
        defer rows.Close()
×
2031
        var items []GraphNodeExtraType
×
2032
        for rows.Next() {
×
2033
                var i GraphNodeExtraType
×
2034
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
2035
                        return nil, err
×
2036
                }
×
2037
                items = append(items, i)
×
2038
        }
2039
        if err := rows.Close(); err != nil {
×
2040
                return nil, err
×
2041
        }
×
2042
        if err := rows.Err(); err != nil {
×
2043
                return nil, err
×
2044
        }
×
2045
        return items, nil
×
2046
}
2047

2048
const getNodeAddresses = `-- name: GetNodeAddresses :many
2049
SELECT type, address
2050
FROM graph_node_addresses
2051
WHERE node_id = $1
2052
ORDER BY type ASC, position ASC
2053
`
2054

2055
type GetNodeAddressesRow struct {
2056
        Type    int16
2057
        Address string
2058
}
2059

2060
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
2061
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
2062
        if err != nil {
×
2063
                return nil, err
×
2064
        }
×
2065
        defer rows.Close()
×
2066
        var items []GetNodeAddressesRow
×
2067
        for rows.Next() {
×
2068
                var i GetNodeAddressesRow
×
2069
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
2070
                        return nil, err
×
2071
                }
×
2072
                items = append(items, i)
×
2073
        }
2074
        if err := rows.Close(); err != nil {
×
2075
                return nil, err
×
2076
        }
×
2077
        if err := rows.Err(); err != nil {
×
2078
                return nil, err
×
2079
        }
×
2080
        return items, nil
×
2081
}
2082

2083
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
2084
SELECT node_id, type, position, address
2085
FROM graph_node_addresses
2086
WHERE node_id IN (/*SLICE:ids*/?)
2087
ORDER BY node_id, type, position
2088
`
2089

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

2128
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
2129
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2130
FROM graph_nodes
2131
WHERE pub_key = $1
2132
  AND version = $2
2133
`
2134

2135
type GetNodeByPubKeyParams struct {
2136
        PubKey  []byte
2137
        Version int16
2138
}
2139

2140
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
2141
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
2142
        var i GraphNode
×
2143
        err := row.Scan(
×
2144
                &i.ID,
×
2145
                &i.Version,
×
2146
                &i.PubKey,
×
2147
                &i.Alias,
×
2148
                &i.LastUpdate,
×
2149
                &i.Color,
×
2150
                &i.Signature,
×
2151
                &i.BlockHeight,
×
2152
        )
×
2153
        return i, err
×
2154
}
×
2155

2156
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
2157
SELECT node_id, type, value
2158
FROM graph_node_extra_types
2159
WHERE node_id IN (/*SLICE:ids*/?)
2160
ORDER BY node_id, type
2161
`
2162

2163
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
2164
        query := getNodeExtraTypesBatch
×
2165
        var queryParams []interface{}
×
2166
        if len(ids) > 0 {
×
2167
                for _, v := range ids {
×
2168
                        queryParams = append(queryParams, v)
×
2169
                }
×
2170
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2171
        } else {
×
2172
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2173
        }
×
2174
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2175
        if err != nil {
×
2176
                return nil, err
×
2177
        }
×
2178
        defer rows.Close()
×
2179
        var items []GraphNodeExtraType
×
2180
        for rows.Next() {
×
2181
                var i GraphNodeExtraType
×
2182
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
2183
                        return nil, err
×
2184
                }
×
2185
                items = append(items, i)
×
2186
        }
2187
        if err := rows.Close(); err != nil {
×
2188
                return nil, err
×
2189
        }
×
2190
        if err := rows.Err(); err != nil {
×
2191
                return nil, err
×
2192
        }
×
2193
        return items, nil
×
2194
}
2195

2196
const getNodeFeatures = `-- name: GetNodeFeatures :many
2197
SELECT node_id, feature_bit
2198
FROM graph_node_features
2199
WHERE node_id = $1
2200
`
2201

2202
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
2203
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
2204
        if err != nil {
×
2205
                return nil, err
×
2206
        }
×
2207
        defer rows.Close()
×
2208
        var items []GraphNodeFeature
×
2209
        for rows.Next() {
×
2210
                var i GraphNodeFeature
×
2211
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
2212
                        return nil, err
×
2213
                }
×
2214
                items = append(items, i)
×
2215
        }
2216
        if err := rows.Close(); err != nil {
×
2217
                return nil, err
×
2218
        }
×
2219
        if err := rows.Err(); err != nil {
×
2220
                return nil, err
×
2221
        }
×
2222
        return items, nil
×
2223
}
2224

2225
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
2226
SELECT node_id, feature_bit
2227
FROM graph_node_features
2228
WHERE node_id IN (/*SLICE:ids*/?)
2229
ORDER BY node_id, feature_bit
2230
`
2231

2232
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
×
2233
        query := getNodeFeaturesBatch
×
2234
        var queryParams []interface{}
×
2235
        if len(ids) > 0 {
×
2236
                for _, v := range ids {
×
2237
                        queryParams = append(queryParams, v)
×
2238
                }
×
2239
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2240
        } else {
×
2241
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2242
        }
×
2243
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2244
        if err != nil {
×
2245
                return nil, err
×
2246
        }
×
2247
        defer rows.Close()
×
2248
        var items []GraphNodeFeature
×
2249
        for rows.Next() {
×
2250
                var i GraphNodeFeature
×
2251
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
2252
                        return nil, err
×
2253
                }
×
2254
                items = append(items, i)
×
2255
        }
2256
        if err := rows.Close(); err != nil {
×
2257
                return nil, err
×
2258
        }
×
2259
        if err := rows.Err(); err != nil {
×
2260
                return nil, err
×
2261
        }
×
2262
        return items, nil
×
2263
}
2264

2265
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
2266
SELECT f.feature_bit
2267
FROM graph_nodes n
2268
    JOIN graph_node_features f ON f.node_id = n.id
2269
WHERE n.pub_key = $1
2270
  AND n.version = $2
2271
`
2272

2273
type GetNodeFeaturesByPubKeyParams struct {
2274
        PubKey  []byte
2275
        Version int16
2276
}
2277

2278
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
2279
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
2280
        if err != nil {
×
2281
                return nil, err
×
2282
        }
×
2283
        defer rows.Close()
×
2284
        var items []int32
×
2285
        for rows.Next() {
×
2286
                var feature_bit int32
×
2287
                if err := rows.Scan(&feature_bit); err != nil {
×
2288
                        return nil, err
×
2289
                }
×
2290
                items = append(items, feature_bit)
×
2291
        }
2292
        if err := rows.Close(); err != nil {
×
2293
                return nil, err
×
2294
        }
×
2295
        if err := rows.Err(); err != nil {
×
2296
                return nil, err
×
2297
        }
×
2298
        return items, nil
×
2299
}
2300

2301
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
2302
SELECT id
2303
FROM graph_nodes
2304
WHERE pub_key = $1
2305
  AND version = $2
2306
`
2307

2308
type GetNodeIDByPubKeyParams struct {
2309
        PubKey  []byte
2310
        Version int16
2311
}
2312

2313
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
2314
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
2315
        var id int64
×
2316
        err := row.Scan(&id)
×
2317
        return id, err
×
2318
}
×
2319

2320
const getNodesByBlockHeightRange = `-- name: GetNodesByBlockHeightRange :many
2321
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2322
FROM graph_nodes
2323
WHERE graph_nodes.version = $1
2324
  AND block_height >= $2
2325
  AND block_height < $3
2326
  -- Pagination: We use (block_height, pub_key) as a compound cursor.
2327
  -- This ensures stable ordering and allows us to resume from where we left off.
2328
  -- We use COALESCE with -1 as sentinel since block heights are always positive.
2329
  AND (
2330
    block_height > COALESCE($4, -1)
2331
    OR
2332
    (block_height = COALESCE($4, -1)
2333
     AND pub_key > $5)
2334
  )
2335
  -- Optional filter for public nodes only.
2336
  AND (
2337
    COALESCE($6, FALSE) IS FALSE
2338
    OR
2339
    -- For V2 protocol, a node is public if it has at least one announced
2340
    -- v2 channel (indicated by a non-empty channel announcement signature).
2341
    EXISTS (
2342
      SELECT 1
2343
      FROM graph_channels c
2344
      WHERE c.version = graph_nodes.version
2345
        AND COALESCE(length(c.signature), 0) > 0
2346
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2347
    )
2348
  )
2349
ORDER BY block_height ASC, pub_key ASC
2350
LIMIT COALESCE($7, 999999999)
2351
`
2352

2353
type GetNodesByBlockHeightRangeParams struct {
2354
        Version         int16
2355
        StartHeight     sql.NullInt64
2356
        EndHeight       sql.NullInt64
2357
        LastBlockHeight sql.NullInt64
2358
        LastPubKey      []byte
2359
        OnlyPublic      interface{}
2360
        MaxResults      interface{}
2361
}
2362

2363
func (q *Queries) GetNodesByBlockHeightRange(ctx context.Context, arg GetNodesByBlockHeightRangeParams) ([]GraphNode, error) {
×
2364
        rows, err := q.db.QueryContext(ctx, getNodesByBlockHeightRange,
×
2365
                arg.Version,
×
2366
                arg.StartHeight,
×
2367
                arg.EndHeight,
×
2368
                arg.LastBlockHeight,
×
2369
                arg.LastPubKey,
×
2370
                arg.OnlyPublic,
×
2371
                arg.MaxResults,
×
2372
        )
×
2373
        if err != nil {
×
2374
                return nil, err
×
2375
        }
×
2376
        defer rows.Close()
×
2377
        var items []GraphNode
×
2378
        for rows.Next() {
×
2379
                var i GraphNode
×
2380
                if err := rows.Scan(
×
2381
                        &i.ID,
×
2382
                        &i.Version,
×
2383
                        &i.PubKey,
×
2384
                        &i.Alias,
×
2385
                        &i.LastUpdate,
×
2386
                        &i.Color,
×
2387
                        &i.Signature,
×
2388
                        &i.BlockHeight,
×
2389
                ); err != nil {
×
2390
                        return nil, err
×
2391
                }
×
2392
                items = append(items, i)
×
2393
        }
2394
        if err := rows.Close(); err != nil {
×
2395
                return nil, err
×
2396
        }
×
2397
        if err := rows.Err(); err != nil {
×
2398
                return nil, err
×
2399
        }
×
2400
        return items, nil
×
2401
}
2402

2403
const getNodesByIDs = `-- name: GetNodesByIDs :many
2404
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2405
FROM graph_nodes
2406
WHERE id IN (/*SLICE:ids*/?)
2407
`
2408

2409
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
2410
        query := getNodesByIDs
×
2411
        var queryParams []interface{}
×
2412
        if len(ids) > 0 {
×
2413
                for _, v := range ids {
×
2414
                        queryParams = append(queryParams, v)
×
2415
                }
×
2416
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2417
        } else {
×
2418
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2419
        }
×
2420
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2421
        if err != nil {
×
2422
                return nil, err
×
2423
        }
×
2424
        defer rows.Close()
×
2425
        var items []GraphNode
×
2426
        for rows.Next() {
×
2427
                var i GraphNode
×
2428
                if err := rows.Scan(
×
2429
                        &i.ID,
×
2430
                        &i.Version,
×
2431
                        &i.PubKey,
×
2432
                        &i.Alias,
×
2433
                        &i.LastUpdate,
×
2434
                        &i.Color,
×
2435
                        &i.Signature,
×
2436
                        &i.BlockHeight,
×
2437
                ); err != nil {
×
2438
                        return nil, err
×
2439
                }
×
2440
                items = append(items, i)
×
2441
        }
2442
        if err := rows.Close(); err != nil {
×
2443
                return nil, err
×
2444
        }
×
2445
        if err := rows.Err(); err != nil {
×
2446
                return nil, err
×
2447
        }
×
2448
        return items, nil
×
2449
}
2450

2451
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
2452
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2453
FROM graph_nodes
2454
WHERE version = 1
2455
  AND last_update >= $1
2456
  AND last_update < $2
2457
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2458
  -- This ensures stable ordering and allows us to resume from where we left off.
2459
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2460
  AND (
2461
    last_update > COALESCE($3, -1)
2462
    OR
2463
    (last_update = COALESCE($3, -1)
2464
     AND pub_key > $4)
2465
  )
2466
ORDER BY last_update ASC, pub_key ASC
2467
LIMIT COALESCE($5, 999999999)
2468
`
2469

2470
type GetNodesByLastUpdateRangeParams struct {
2471
        StartTime  sql.NullInt64
2472
        EndTime    sql.NullInt64
2473
        LastUpdate sql.NullInt64
2474
        LastPubKey []byte
2475
        MaxResults interface{}
2476
}
2477

2478
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2479
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
2480
                arg.StartTime,
×
2481
                arg.EndTime,
×
2482
                arg.LastUpdate,
×
2483
                arg.LastPubKey,
×
2484
                arg.MaxResults,
×
2485
        )
×
2486
        if err != nil {
×
2487
                return nil, err
×
2488
        }
×
2489
        defer rows.Close()
×
2490
        var items []GraphNode
×
2491
        for rows.Next() {
×
2492
                var i GraphNode
×
2493
                if err := rows.Scan(
×
2494
                        &i.ID,
×
2495
                        &i.Version,
×
2496
                        &i.PubKey,
×
2497
                        &i.Alias,
×
2498
                        &i.LastUpdate,
×
2499
                        &i.Color,
×
2500
                        &i.Signature,
×
2501
                        &i.BlockHeight,
×
2502
                ); err != nil {
×
2503
                        return nil, err
×
2504
                }
×
2505
                items = append(items, i)
×
2506
        }
2507
        if err := rows.Close(); err != nil {
×
2508
                return nil, err
×
2509
        }
×
2510
        if err := rows.Err(); err != nil {
×
2511
                return nil, err
×
2512
        }
×
2513
        return items, nil
×
2514
}
2515

2516
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2517
SELECT block_height, block_hash
2518
FROM graph_prune_log
2519
WHERE block_height
2520
   IN (/*SLICE:heights*/?)
2521
`
2522

2523
func (q *Queries) GetPruneEntriesForHeights(ctx context.Context, heights []int64) ([]GraphPruneLog, error) {
×
2524
        query := getPruneEntriesForHeights
×
2525
        var queryParams []interface{}
×
2526
        if len(heights) > 0 {
×
2527
                for _, v := range heights {
×
2528
                        queryParams = append(queryParams, v)
×
2529
                }
×
2530
                query = strings.Replace(query, "/*SLICE:heights*/?", makeQueryParams(len(queryParams), len(heights)), 1)
×
2531
        } else {
×
2532
                query = strings.Replace(query, "/*SLICE:heights*/?", "NULL", 1)
×
2533
        }
×
2534
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2535
        if err != nil {
×
2536
                return nil, err
×
2537
        }
×
2538
        defer rows.Close()
×
2539
        var items []GraphPruneLog
×
2540
        for rows.Next() {
×
2541
                var i GraphPruneLog
×
2542
                if err := rows.Scan(&i.BlockHeight, &i.BlockHash); err != nil {
×
2543
                        return nil, err
×
2544
                }
×
2545
                items = append(items, i)
×
2546
        }
2547
        if err := rows.Close(); err != nil {
×
2548
                return nil, err
×
2549
        }
×
2550
        if err := rows.Err(); err != nil {
×
2551
                return nil, err
×
2552
        }
×
2553
        return items, nil
×
2554
}
2555

2556
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2557
SELECT block_hash
2558
FROM graph_prune_log
2559
WHERE block_height = $1
2560
`
2561

2562
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2563
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2564
        var block_hash []byte
×
2565
        err := row.Scan(&block_hash)
×
2566
        return block_hash, err
×
2567
}
×
2568

2569
const getPruneTip = `-- name: GetPruneTip :one
2570
SELECT block_height, block_hash
2571
FROM graph_prune_log
2572
ORDER BY block_height DESC
2573
LIMIT 1
2574
`
2575

2576
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2577
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2578
        var i GraphPruneLog
×
2579
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2580
        return i, err
×
2581
}
×
2582

2583
const getPublicNodesByLastUpdateRange = `-- name: GetPublicNodesByLastUpdateRange :many
2584
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2585
FROM graph_nodes
2586
WHERE version = 1
2587
  AND last_update >= $1
2588
  AND last_update <= $2
2589
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2590
  -- This ensures stable ordering and allows us to resume from where we left off.
2591
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2592
  AND (
2593
    last_update > COALESCE($3, -1)
2594
    OR
2595
    (last_update = COALESCE($3, -1)
2596
     AND pub_key > $4)
2597
  )
2598
  AND (
2599
    EXISTS (
2600
        SELECT 1
2601
        FROM graph_channels c
2602
        WHERE c.version = 1
2603
          AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2604
          AND c.node_id_1 = graph_nodes.id
2605
    )
2606
    OR EXISTS (
2607
        SELECT 1
2608
        FROM graph_channels c
2609
        WHERE c.version = 1
2610
          AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2611
          AND c.node_id_2 = graph_nodes.id
2612
    )
2613
  )
2614
ORDER BY last_update ASC, pub_key ASC
2615
LIMIT COALESCE($5, 999999999)
2616
`
2617

2618
type GetPublicNodesByLastUpdateRangeParams struct {
2619
        StartTime  sql.NullInt64
2620
        EndTime    sql.NullInt64
2621
        LastUpdate sql.NullInt64
2622
        LastPubKey []byte
2623
        MaxResults interface{}
2624
}
2625

2626
// Returns only public V1 nodes within the given last_update range. A V1 node
2627
// is public if it has at least one channel with a bitcoin_1_signature set. The
2628
// public check uses two separate EXISTS probes (one per node_id column)
2629
// instead of a single OR on node_id_1/node_id_2 so the planner can use the
2630
// channel node-id indexes directly.
2631
func (q *Queries) GetPublicNodesByLastUpdateRange(ctx context.Context, arg GetPublicNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2632
        rows, err := q.db.QueryContext(ctx, getPublicNodesByLastUpdateRange,
×
2633
                arg.StartTime,
×
2634
                arg.EndTime,
×
2635
                arg.LastUpdate,
×
2636
                arg.LastPubKey,
×
2637
                arg.MaxResults,
×
2638
        )
×
2639
        if err != nil {
×
2640
                return nil, err
×
2641
        }
×
2642
        defer rows.Close()
×
2643
        var items []GraphNode
×
2644
        for rows.Next() {
×
2645
                var i GraphNode
×
2646
                if err := rows.Scan(
×
2647
                        &i.ID,
×
2648
                        &i.Version,
×
2649
                        &i.PubKey,
×
2650
                        &i.Alias,
×
2651
                        &i.LastUpdate,
×
2652
                        &i.Color,
×
2653
                        &i.Signature,
×
2654
                        &i.BlockHeight,
×
2655
                ); err != nil {
×
2656
                        return nil, err
×
2657
                }
×
2658
                items = append(items, i)
×
2659
        }
2660
        if err := rows.Close(); err != nil {
×
2661
                return nil, err
×
2662
        }
×
2663
        if err := rows.Err(); err != nil {
×
2664
                return nil, err
×
2665
        }
×
2666
        return items, nil
×
2667
}
2668

2669
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2670
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, signature, funding_pk_script, merkle_root_hash
2671
FROM graph_channels
2672
WHERE version = 1
2673
  AND COALESCE(length(node_1_signature), 0) > 0
2674
  AND scid >= $1
2675
  AND scid < $2
2676
ORDER BY scid ASC
2677
`
2678

2679
type GetPublicV1ChannelsBySCIDParams struct {
2680
        StartScid []byte
2681
        EndScid   []byte
2682
}
2683

2684
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2685
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2686
        if err != nil {
×
2687
                return nil, err
×
2688
        }
×
2689
        defer rows.Close()
×
2690
        var items []GraphChannel
×
2691
        for rows.Next() {
×
2692
                var i GraphChannel
×
2693
                if err := rows.Scan(
×
2694
                        &i.ID,
×
2695
                        &i.Version,
×
2696
                        &i.Scid,
×
2697
                        &i.NodeID1,
×
2698
                        &i.NodeID2,
×
2699
                        &i.Outpoint,
×
2700
                        &i.Capacity,
×
2701
                        &i.BitcoinKey1,
×
2702
                        &i.BitcoinKey2,
×
2703
                        &i.Node1Signature,
×
2704
                        &i.Node2Signature,
×
2705
                        &i.Bitcoin1Signature,
×
2706
                        &i.Bitcoin2Signature,
×
2707
                        &i.Signature,
×
2708
                        &i.FundingPkScript,
×
2709
                        &i.MerkleRootHash,
×
2710
                ); err != nil {
×
2711
                        return nil, err
×
2712
                }
×
2713
                items = append(items, i)
×
2714
        }
2715
        if err := rows.Close(); err != nil {
×
2716
                return nil, err
×
2717
        }
×
2718
        if err := rows.Err(); err != nil {
×
2719
                return nil, err
×
2720
        }
×
2721
        return items, nil
×
2722
}
2723

2724
const getPublicV2ChannelsBySCID = `-- name: GetPublicV2ChannelsBySCID :many
2725
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, signature, funding_pk_script, merkle_root_hash
2726
FROM graph_channels
2727
WHERE version = 2
2728
  AND COALESCE(length(signature), 0) > 0
2729
  AND scid >= $1
2730
  AND scid < $2
2731
ORDER BY scid ASC
2732
`
2733

2734
type GetPublicV2ChannelsBySCIDParams struct {
2735
        StartScid []byte
2736
        EndScid   []byte
2737
}
2738

2739
func (q *Queries) GetPublicV2ChannelsBySCID(ctx context.Context, arg GetPublicV2ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2740
        rows, err := q.db.QueryContext(ctx, getPublicV2ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2741
        if err != nil {
×
2742
                return nil, err
×
2743
        }
×
2744
        defer rows.Close()
×
2745
        var items []GraphChannel
×
2746
        for rows.Next() {
×
2747
                var i GraphChannel
×
2748
                if err := rows.Scan(
×
2749
                        &i.ID,
×
2750
                        &i.Version,
×
2751
                        &i.Scid,
×
2752
                        &i.NodeID1,
×
2753
                        &i.NodeID2,
×
2754
                        &i.Outpoint,
×
2755
                        &i.Capacity,
×
2756
                        &i.BitcoinKey1,
×
2757
                        &i.BitcoinKey2,
×
2758
                        &i.Node1Signature,
×
2759
                        &i.Node2Signature,
×
2760
                        &i.Bitcoin1Signature,
×
2761
                        &i.Bitcoin2Signature,
×
2762
                        &i.Signature,
×
2763
                        &i.FundingPkScript,
×
2764
                        &i.MerkleRootHash,
×
2765
                ); err != nil {
×
2766
                        return nil, err
×
2767
                }
×
2768
                items = append(items, i)
×
2769
        }
2770
        if err := rows.Close(); err != nil {
×
2771
                return nil, err
×
2772
        }
×
2773
        if err := rows.Err(); err != nil {
×
2774
                return nil, err
×
2775
        }
×
2776
        return items, nil
×
2777
}
2778

2779
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2780
SELECT scid from graph_channels
2781
WHERE outpoint = $1 AND version = $2
2782
`
2783

2784
type GetSCIDByOutpointParams struct {
2785
        Outpoint string
2786
        Version  int16
2787
}
2788

2789
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2790
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2791
        var scid []byte
×
2792
        err := row.Scan(&scid)
×
2793
        return scid, err
×
2794
}
×
2795

2796
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2797
SELECT sn.node_id, n.pub_key
2798
FROM graph_source_nodes sn
2799
    JOIN graph_nodes n ON sn.node_id = n.id
2800
WHERE n.version = $1
2801
`
2802

2803
type GetSourceNodesByVersionRow struct {
2804
        NodeID int64
2805
        PubKey []byte
2806
}
2807

2808
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2809
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2810
        if err != nil {
×
2811
                return nil, err
×
2812
        }
×
2813
        defer rows.Close()
×
2814
        var items []GetSourceNodesByVersionRow
×
2815
        for rows.Next() {
×
2816
                var i GetSourceNodesByVersionRow
×
2817
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
2818
                        return nil, err
×
2819
                }
×
2820
                items = append(items, i)
×
2821
        }
2822
        if err := rows.Close(); err != nil {
×
2823
                return nil, err
×
2824
        }
×
2825
        if err := rows.Err(); err != nil {
×
2826
                return nil, err
×
2827
        }
×
2828
        return items, nil
×
2829
}
2830

2831
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2832
SELECT c.scid
2833
FROM graph_channels c
2834
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2835
WHERE cp.disabled = true
2836
AND c.version = 1
2837
GROUP BY c.scid
2838
HAVING COUNT(*) > 1
2839
`
2840

2841
// NOTE: this is V1 specific since for V1, disabled is a
2842
// simple, single boolean. The proposed V2 policy
2843
// structure will have a more complex disabled bit vector
2844
// and so the query for V2 may differ.
2845
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2846
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2847
        if err != nil {
×
2848
                return nil, err
×
2849
        }
×
2850
        defer rows.Close()
×
2851
        var items [][]byte
×
2852
        for rows.Next() {
×
2853
                var scid []byte
×
2854
                if err := rows.Scan(&scid); err != nil {
×
2855
                        return nil, err
×
2856
                }
×
2857
                items = append(items, scid)
×
2858
        }
2859
        if err := rows.Close(); err != nil {
×
2860
                return nil, err
×
2861
        }
×
2862
        if err := rows.Err(); err != nil {
×
2863
                return nil, err
×
2864
        }
×
2865
        return items, nil
×
2866
}
2867

2868
const getV2DisabledSCIDs = `-- name: GetV2DisabledSCIDs :many
2869
SELECT c.scid
2870
FROM graph_channels c
2871
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2872
WHERE COALESCE(cp.disable_flags, 0) != 0
2873
AND c.version = 2
2874
GROUP BY c.scid
2875
HAVING COUNT(*) > 1
2876
`
2877

2878
// NOTE: this is V2 specific since V2 uses a disable flag
2879
// bit vector instead of a single boolean.
2880
func (q *Queries) GetV2DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2881
        rows, err := q.db.QueryContext(ctx, getV2DisabledSCIDs)
×
2882
        if err != nil {
×
2883
                return nil, err
×
2884
        }
×
2885
        defer rows.Close()
×
2886
        var items [][]byte
×
2887
        for rows.Next() {
×
2888
                var scid []byte
×
2889
                if err := rows.Scan(&scid); err != nil {
×
2890
                        return nil, err
×
2891
                }
×
2892
                items = append(items, scid)
×
2893
        }
2894
        if err := rows.Close(); err != nil {
×
2895
                return nil, err
×
2896
        }
×
2897
        if err := rows.Err(); err != nil {
×
2898
                return nil, err
×
2899
        }
×
2900
        return items, nil
×
2901
}
2902

2903
const getZombieChannel = `-- name: GetZombieChannel :one
2904
SELECT scid, version, node_key_1, node_key_2
2905
FROM graph_zombie_channels
2906
WHERE scid = $1
2907
AND version = $2
2908
`
2909

2910
type GetZombieChannelParams struct {
2911
        Scid    []byte
2912
        Version int16
2913
}
2914

2915
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2916
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2917
        var i GraphZombieChannel
×
2918
        err := row.Scan(
×
2919
                &i.Scid,
×
2920
                &i.Version,
×
2921
                &i.NodeKey1,
×
2922
                &i.NodeKey2,
×
2923
        )
×
2924
        return i, err
×
2925
}
×
2926

2927
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2928
SELECT scid, version, node_key_1, node_key_2
2929
FROM graph_zombie_channels
2930
WHERE version = $1
2931
  AND scid IN (/*SLICE:scids*/?)
2932
`
2933

2934
type GetZombieChannelsSCIDsParams struct {
2935
        Version int16
2936
        Scids   [][]byte
2937
}
2938

2939
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2940
        query := getZombieChannelsSCIDs
×
2941
        var queryParams []interface{}
×
2942
        queryParams = append(queryParams, arg.Version)
×
2943
        if len(arg.Scids) > 0 {
×
2944
                for _, v := range arg.Scids {
×
2945
                        queryParams = append(queryParams, v)
×
2946
                }
×
2947
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2948
        } else {
×
2949
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2950
        }
×
2951
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2952
        if err != nil {
×
2953
                return nil, err
×
2954
        }
×
2955
        defer rows.Close()
×
2956
        var items []GraphZombieChannel
×
2957
        for rows.Next() {
×
2958
                var i GraphZombieChannel
×
2959
                if err := rows.Scan(
×
2960
                        &i.Scid,
×
2961
                        &i.Version,
×
2962
                        &i.NodeKey1,
×
2963
                        &i.NodeKey2,
×
2964
                ); err != nil {
×
2965
                        return nil, err
×
2966
                }
×
2967
                items = append(items, i)
×
2968
        }
2969
        if err := rows.Close(); err != nil {
×
2970
                return nil, err
×
2971
        }
×
2972
        if err := rows.Err(); err != nil {
×
2973
                return nil, err
×
2974
        }
×
2975
        return items, nil
×
2976
}
2977

2978
const highestSCID = `-- name: HighestSCID :one
2979
SELECT scid
2980
FROM graph_channels
2981
WHERE version = $1
2982
ORDER BY scid DESC
2983
LIMIT 1
2984
`
2985

2986
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2987
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2988
        var scid []byte
×
2989
        err := row.Scan(&scid)
×
2990
        return scid, err
×
2991
}
×
2992

2993
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2994
/* ─────────────────────────────────────────────
2995
   graph_channel_features table queries
2996
   ─────────────────────────────────────────────
2997
*/
2998

2999
INSERT INTO graph_channel_features (
3000
    channel_id, feature_bit
3001
) VALUES (
3002
    $1, $2
3003
) ON CONFLICT (channel_id, feature_bit)
3004
    -- Do nothing if the channel_id and feature_bit already exist.
3005
    DO NOTHING
3006
`
3007

3008
type InsertChannelFeatureParams struct {
3009
        ChannelID  int64
3010
        FeatureBit int32
3011
}
3012

3013
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
3014
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
3015
        return err
×
3016
}
×
3017

3018
const insertChannelMig = `-- name: InsertChannelMig :one
3019
INSERT INTO graph_channels (
3020
    version, scid, node_id_1, node_id_2,
3021
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
3022
    node_1_signature, node_2_signature, bitcoin_1_signature,
3023
    bitcoin_2_signature
3024
) VALUES (
3025
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
3026
) ON CONFLICT (scid, version)
3027
    -- If a conflict occurs, we have already migrated this channel. However, we
3028
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3029
    -- otherwise, the "RETURNING id" part does not work.
3030
    DO UPDATE SET
3031
        node_id_1 = EXCLUDED.node_id_1,
3032
        node_id_2 = EXCLUDED.node_id_2,
3033
        outpoint = EXCLUDED.outpoint,
3034
        capacity = EXCLUDED.capacity,
3035
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
3036
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
3037
        node_1_signature = EXCLUDED.node_1_signature,
3038
        node_2_signature = EXCLUDED.node_2_signature,
3039
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
3040
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
3041
RETURNING id
3042
`
3043

3044
type InsertChannelMigParams struct {
3045
        Version           int16
3046
        Scid              []byte
3047
        NodeID1           int64
3048
        NodeID2           int64
3049
        Outpoint          string
3050
        Capacity          sql.NullInt64
3051
        BitcoinKey1       []byte
3052
        BitcoinKey2       []byte
3053
        Node1Signature    []byte
3054
        Node2Signature    []byte
3055
        Bitcoin1Signature []byte
3056
        Bitcoin2Signature []byte
3057
}
3058

3059
// NOTE: This query is only meant to be used by the graph SQL migration since
3060
// for that migration, in order to be retry-safe, we don't want to error out if
3061
// we re-insert the same channel again (which would error if the normal
3062
// CreateChannel query is used because of the uniqueness constraint on the scid
3063
// and version columns).
3064
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
3065
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
3066
                arg.Version,
×
3067
                arg.Scid,
×
3068
                arg.NodeID1,
×
3069
                arg.NodeID2,
×
3070
                arg.Outpoint,
×
3071
                arg.Capacity,
×
3072
                arg.BitcoinKey1,
×
3073
                arg.BitcoinKey2,
×
3074
                arg.Node1Signature,
×
3075
                arg.Node2Signature,
×
3076
                arg.Bitcoin1Signature,
×
3077
                arg.Bitcoin2Signature,
×
3078
        )
×
3079
        var id int64
×
3080
        err := row.Scan(&id)
×
3081
        return id, err
×
3082
}
×
3083

3084
const insertClosedChannel = `-- name: InsertClosedChannel :exec
3085
/* ─────────────────────────────────────────────
3086
   graph_closed_scid table queries
3087
   ────────────────────────────────────────────-
3088
*/
3089

3090
INSERT INTO graph_closed_scids (scid)
3091
VALUES ($1)
3092
ON CONFLICT (scid) DO NOTHING
3093
`
3094

3095
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
3096
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
3097
        return err
×
3098
}
×
3099

3100
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
3101
INSERT INTO graph_channel_policies (
3102
    version, channel_id, node_id, timelock, fee_ppm,
3103
    base_fee_msat, min_htlc_msat, last_update, disabled,
3104
    max_htlc_msat, inbound_base_fee_msat,
3105
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3106
    signature
3107
) VALUES  (
3108
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3109
)
3110
ON CONFLICT (channel_id, node_id, version)
3111
    -- If a conflict occurs, we have already migrated this policy. However, we
3112
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3113
    -- otherwise, the "RETURNING id" part does not work.
3114
    DO UPDATE SET
3115
        timelock = EXCLUDED.timelock,
3116
        fee_ppm = EXCLUDED.fee_ppm,
3117
        base_fee_msat = EXCLUDED.base_fee_msat,
3118
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3119
        last_update = EXCLUDED.last_update,
3120
        disabled = EXCLUDED.disabled,
3121
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3122
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3123
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3124
        message_flags = EXCLUDED.message_flags,
3125
        channel_flags = EXCLUDED.channel_flags,
3126
        signature = EXCLUDED.signature
3127
RETURNING id
3128
`
3129

3130
type InsertEdgePolicyMigParams struct {
3131
        Version                 int16
3132
        ChannelID               int64
3133
        NodeID                  int64
3134
        Timelock                int32
3135
        FeePpm                  int64
3136
        BaseFeeMsat             int64
3137
        MinHtlcMsat             int64
3138
        LastUpdate              sql.NullInt64
3139
        Disabled                sql.NullBool
3140
        MaxHtlcMsat             sql.NullInt64
3141
        InboundBaseFeeMsat      sql.NullInt64
3142
        InboundFeeRateMilliMsat sql.NullInt64
3143
        MessageFlags            sql.NullInt16
3144
        ChannelFlags            sql.NullInt16
3145
        Signature               []byte
3146
}
3147

3148
// NOTE: This query is only meant to be used by the graph SQL migration since
3149
// for that migration, in order to be retry-safe, we don't want to error out if
3150
// we re-insert the same policy (which would error if the normal
3151
// UpsertEdgePolicy query is used because of the constraint in that query that
3152
// requires a policy update to have a newer last_update than the existing one).
3153
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
3154
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
3155
                arg.Version,
×
3156
                arg.ChannelID,
×
3157
                arg.NodeID,
×
3158
                arg.Timelock,
×
3159
                arg.FeePpm,
×
3160
                arg.BaseFeeMsat,
×
3161
                arg.MinHtlcMsat,
×
3162
                arg.LastUpdate,
×
3163
                arg.Disabled,
×
3164
                arg.MaxHtlcMsat,
×
3165
                arg.InboundBaseFeeMsat,
×
3166
                arg.InboundFeeRateMilliMsat,
×
3167
                arg.MessageFlags,
×
3168
                arg.ChannelFlags,
×
3169
                arg.Signature,
×
3170
        )
×
3171
        var id int64
×
3172
        err := row.Scan(&id)
×
3173
        return id, err
×
3174
}
×
3175

3176
const insertNodeFeature = `-- name: InsertNodeFeature :exec
3177
/* ─────────────────────────────────────────────
3178
   graph_node_features table queries
3179
   ─────────────────────────────────────────────
3180
*/
3181

3182
INSERT INTO graph_node_features (
3183
    node_id, feature_bit
3184
) VALUES (
3185
    $1, $2
3186
) ON CONFLICT (node_id, feature_bit)
3187
    -- Do nothing if the feature already exists for the node.
3188
    DO NOTHING
3189
`
3190

3191
type InsertNodeFeatureParams struct {
3192
        NodeID     int64
3193
        FeatureBit int32
3194
}
3195

3196
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
3197
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
3198
        return err
×
3199
}
×
3200

3201
const insertNodeMig = `-- name: InsertNodeMig :one
3202
/* ─────────────────────────────────────────────
3203
   Migration specific queries
3204

3205
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
3206
   dedicated to the migration that requires it, and so we can then remove
3207
   it from the main set of "live" queries that the code-base has access to.
3208
   ────────────────────────────────────────────-
3209
*/
3210

3211
INSERT INTO graph_nodes (
3212
    version, pub_key, alias, last_update, color, signature
3213
) VALUES (
3214
    $1, $2, $3, $4, $5, $6
3215
)
3216
ON CONFLICT (pub_key, version)
3217
    -- If a conflict occurs, we have already migrated this node. However, we
3218
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3219
    -- otherwise, the "RETURNING id" part does not work.
3220
    DO UPDATE SET
3221
        alias = EXCLUDED.alias,
3222
        last_update = EXCLUDED.last_update,
3223
        color = EXCLUDED.color,
3224
        signature = EXCLUDED.signature
3225
RETURNING id
3226
`
3227

3228
type InsertNodeMigParams struct {
3229
        Version    int16
3230
        PubKey     []byte
3231
        Alias      sql.NullString
3232
        LastUpdate sql.NullInt64
3233
        Color      sql.NullString
3234
        Signature  []byte
3235
}
3236

3237
// NOTE: This query is only meant to be used by the graph SQL migration since
3238
// for that migration, in order to be retry-safe, we don't want to error out if
3239
// we re-insert the same node (which would error if the normal UpsertNode query
3240
// is used because of the constraint in that query that requires a node update
3241
// to have a newer last_update than the existing node).
3242
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
3243
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
3244
                arg.Version,
×
3245
                arg.PubKey,
×
3246
                arg.Alias,
×
3247
                arg.LastUpdate,
×
3248
                arg.Color,
×
3249
                arg.Signature,
×
3250
        )
×
3251
        var id int64
×
3252
        err := row.Scan(&id)
×
3253
        return id, err
×
3254
}
×
3255

3256
const isClosedChannel = `-- name: IsClosedChannel :one
3257
SELECT EXISTS (
3258
    SELECT 1
3259
    FROM graph_closed_scids
3260
    WHERE scid = $1
3261
)
3262
`
3263

3264
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
3265
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
3266
        var exists bool
×
3267
        err := row.Scan(&exists)
×
3268
        return exists, err
×
3269
}
×
3270

3271
const isPublicV1Node = `-- name: IsPublicV1Node :one
3272
SELECT EXISTS (
3273
    SELECT 1
3274
    FROM graph_channels c
3275
    JOIN graph_nodes n ON n.id = c.node_id_1
3276
    -- NOTE: we hard-code the version here since the clauses
3277
    -- here that determine if a node is public is specific
3278
    -- to the V1 gossip protocol. In V1, a node is public
3279
    -- if it has a public channel and a public channel is one
3280
    -- where we have the set of signatures of the channel
3281
    -- announcement. It is enough to just check that we have
3282
    -- one of the signatures since we only ever set them
3283
    -- together.
3284
    WHERE c.version = 1
3285
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3286
      AND n.pub_key = $1
3287
    UNION ALL
3288
    SELECT 1
3289
    FROM graph_channels c
3290
    JOIN graph_nodes n ON n.id = c.node_id_2
3291
    WHERE c.version = 1
3292
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3293
      AND n.pub_key = $1
3294
)
3295
`
3296

3297
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3298
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
3299
        var exists bool
×
3300
        err := row.Scan(&exists)
×
3301
        return exists, err
×
3302
}
×
3303

3304
const isPublicV2Node = `-- name: IsPublicV2Node :one
3305
SELECT EXISTS (
3306
    SELECT 1
3307
    FROM graph_channels c
3308
    JOIN graph_nodes n ON n.id = c.node_id_1
3309
    -- NOTE: we hard-code the version here since the clauses
3310
    -- here that determine if a node is public is specific
3311
    -- to the V2 gossip protocol.
3312
    WHERE c.version = 2
3313
      AND COALESCE(length(c.signature), 0) > 0
3314
      AND n.pub_key = $1
3315

3316
    UNION ALL
3317

3318
    SELECT 1
3319
    FROM graph_channels c
3320
    JOIN graph_nodes n ON n.id = c.node_id_2
3321
    WHERE c.version = 2
3322
      AND COALESCE(length(c.signature), 0) > 0
3323
      AND n.pub_key = $1
3324
)
3325
`
3326

3327
func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3328
        row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey)
×
3329
        var exists bool
×
3330
        err := row.Scan(&exists)
×
3331
        return exists, err
×
3332
}
×
3333

3334
const isZombieChannel = `-- name: IsZombieChannel :one
3335
SELECT EXISTS (
3336
    SELECT 1
3337
    FROM graph_zombie_channels
3338
    WHERE scid = $1
3339
    AND version = $2
3340
) AS is_zombie
3341
`
3342

3343
type IsZombieChannelParams struct {
3344
        Scid    []byte
3345
        Version int16
3346
}
3347

3348
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
3349
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
3350
        var is_zombie bool
×
3351
        err := row.Scan(&is_zombie)
×
3352
        return is_zombie, err
×
3353
}
×
3354

3355
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
3356
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, c.signature, c.funding_pk_script, c.merkle_root_hash,
3357
    n1.pub_key AS node1_pubkey,
3358
    n2.pub_key AS node2_pubkey,
3359

3360
    -- Policy 1
3361
    -- TODO(elle): use sqlc.embed to embed policy structs
3362
    --  once this issue is resolved:
3363
    --  https://github.com/sqlc-dev/sqlc/issues/2997
3364
    cp1.id AS policy1_id,
3365
    cp1.node_id AS policy1_node_id,
3366
    cp1.version AS policy1_version,
3367
    cp1.timelock AS policy1_timelock,
3368
    cp1.fee_ppm AS policy1_fee_ppm,
3369
    cp1.base_fee_msat AS policy1_base_fee_msat,
3370
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
3371
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
3372
    cp1.last_update AS policy1_last_update,
3373
    cp1.disabled AS policy1_disabled,
3374
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3375
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3376
    cp1.message_flags AS policy1_message_flags,
3377
    cp1.channel_flags AS policy1_channel_flags,
3378
    cp1.signature AS policy1_signature,
3379
    cp1.block_height AS policy1_block_height,
3380
    cp1.disable_flags AS policy1_disable_flags,
3381

3382
       -- Policy 2
3383
    cp2.id AS policy2_id,
3384
    cp2.node_id AS policy2_node_id,
3385
    cp2.version AS policy2_version,
3386
    cp2.timelock AS policy2_timelock,
3387
    cp2.fee_ppm AS policy2_fee_ppm,
3388
    cp2.base_fee_msat AS policy2_base_fee_msat,
3389
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
3390
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
3391
    cp2.last_update AS policy2_last_update,
3392
    cp2.disabled AS policy2_disabled,
3393
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3394
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3395
    cp2.message_flags AS policy2_message_flags,
3396
    cp2.channel_flags AS policy2_channel_flags,
3397
    cp2.signature AS policy2_signature,
3398
    cp2.block_height AS policy2_block_height,
3399
    cp2.disable_flags AS policy2_disable_flags
3400

3401
FROM graph_channels c
3402
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3403
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3404
    LEFT JOIN graph_channel_policies cp1
3405
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3406
    LEFT JOIN graph_channel_policies cp2
3407
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3408
WHERE c.version = $1
3409
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
3410
`
3411

3412
type ListChannelsByNodeIDParams struct {
3413
        Version int16
3414
        NodeID1 int64
3415
}
3416

3417
type ListChannelsByNodeIDRow struct {
3418
        GraphChannel                   GraphChannel
3419
        Node1Pubkey                    []byte
3420
        Node2Pubkey                    []byte
3421
        Policy1ID                      sql.NullInt64
3422
        Policy1NodeID                  sql.NullInt64
3423
        Policy1Version                 sql.NullInt16
3424
        Policy1Timelock                sql.NullInt32
3425
        Policy1FeePpm                  sql.NullInt64
3426
        Policy1BaseFeeMsat             sql.NullInt64
3427
        Policy1MinHtlcMsat             sql.NullInt64
3428
        Policy1MaxHtlcMsat             sql.NullInt64
3429
        Policy1LastUpdate              sql.NullInt64
3430
        Policy1Disabled                sql.NullBool
3431
        Policy1InboundBaseFeeMsat      sql.NullInt64
3432
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3433
        Policy1MessageFlags            sql.NullInt16
3434
        Policy1ChannelFlags            sql.NullInt16
3435
        Policy1Signature               []byte
3436
        Policy1BlockHeight             sql.NullInt64
3437
        Policy1DisableFlags            sql.NullInt16
3438
        Policy2ID                      sql.NullInt64
3439
        Policy2NodeID                  sql.NullInt64
3440
        Policy2Version                 sql.NullInt16
3441
        Policy2Timelock                sql.NullInt32
3442
        Policy2FeePpm                  sql.NullInt64
3443
        Policy2BaseFeeMsat             sql.NullInt64
3444
        Policy2MinHtlcMsat             sql.NullInt64
3445
        Policy2MaxHtlcMsat             sql.NullInt64
3446
        Policy2LastUpdate              sql.NullInt64
3447
        Policy2Disabled                sql.NullBool
3448
        Policy2InboundBaseFeeMsat      sql.NullInt64
3449
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3450
        Policy2MessageFlags            sql.NullInt16
3451
        Policy2ChannelFlags            sql.NullInt16
3452
        Policy2Signature               []byte
3453
        Policy2BlockHeight             sql.NullInt64
3454
        Policy2DisableFlags            sql.NullInt16
3455
}
3456

3457
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
3458
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
3459
        if err != nil {
×
3460
                return nil, err
×
3461
        }
×
3462
        defer rows.Close()
×
3463
        var items []ListChannelsByNodeIDRow
×
3464
        for rows.Next() {
×
3465
                var i ListChannelsByNodeIDRow
×
3466
                if err := rows.Scan(
×
3467
                        &i.GraphChannel.ID,
×
3468
                        &i.GraphChannel.Version,
×
3469
                        &i.GraphChannel.Scid,
×
3470
                        &i.GraphChannel.NodeID1,
×
3471
                        &i.GraphChannel.NodeID2,
×
3472
                        &i.GraphChannel.Outpoint,
×
3473
                        &i.GraphChannel.Capacity,
×
3474
                        &i.GraphChannel.BitcoinKey1,
×
3475
                        &i.GraphChannel.BitcoinKey2,
×
3476
                        &i.GraphChannel.Node1Signature,
×
3477
                        &i.GraphChannel.Node2Signature,
×
3478
                        &i.GraphChannel.Bitcoin1Signature,
×
3479
                        &i.GraphChannel.Bitcoin2Signature,
×
3480
                        &i.GraphChannel.Signature,
×
3481
                        &i.GraphChannel.FundingPkScript,
×
3482
                        &i.GraphChannel.MerkleRootHash,
×
3483
                        &i.Node1Pubkey,
×
3484
                        &i.Node2Pubkey,
×
3485
                        &i.Policy1ID,
×
3486
                        &i.Policy1NodeID,
×
3487
                        &i.Policy1Version,
×
3488
                        &i.Policy1Timelock,
×
3489
                        &i.Policy1FeePpm,
×
3490
                        &i.Policy1BaseFeeMsat,
×
3491
                        &i.Policy1MinHtlcMsat,
×
3492
                        &i.Policy1MaxHtlcMsat,
×
3493
                        &i.Policy1LastUpdate,
×
3494
                        &i.Policy1Disabled,
×
3495
                        &i.Policy1InboundBaseFeeMsat,
×
3496
                        &i.Policy1InboundFeeRateMilliMsat,
×
3497
                        &i.Policy1MessageFlags,
×
3498
                        &i.Policy1ChannelFlags,
×
3499
                        &i.Policy1Signature,
×
3500
                        &i.Policy1BlockHeight,
×
3501
                        &i.Policy1DisableFlags,
×
3502
                        &i.Policy2ID,
×
3503
                        &i.Policy2NodeID,
×
3504
                        &i.Policy2Version,
×
3505
                        &i.Policy2Timelock,
×
3506
                        &i.Policy2FeePpm,
×
3507
                        &i.Policy2BaseFeeMsat,
×
3508
                        &i.Policy2MinHtlcMsat,
×
3509
                        &i.Policy2MaxHtlcMsat,
×
3510
                        &i.Policy2LastUpdate,
×
3511
                        &i.Policy2Disabled,
×
3512
                        &i.Policy2InboundBaseFeeMsat,
×
3513
                        &i.Policy2InboundFeeRateMilliMsat,
×
3514
                        &i.Policy2MessageFlags,
×
3515
                        &i.Policy2ChannelFlags,
×
3516
                        &i.Policy2Signature,
×
3517
                        &i.Policy2BlockHeight,
×
3518
                        &i.Policy2DisableFlags,
×
3519
                ); err != nil {
×
3520
                        return nil, err
×
3521
                }
×
3522
                items = append(items, i)
×
3523
        }
3524
        if err := rows.Close(); err != nil {
×
3525
                return nil, err
×
3526
        }
×
3527
        if err := rows.Err(); err != nil {
×
3528
                return nil, err
×
3529
        }
×
3530
        return items, nil
×
3531
}
3532

3533
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3534
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, c.signature, c.funding_pk_script, c.merkle_root_hash,
3535
       n1.pub_key AS node1_pubkey,
3536
       n2.pub_key AS node2_pubkey,
3537

3538
       -- Policy 1
3539
       -- TODO(elle): use sqlc.embed to embed policy structs
3540
       --  once this issue is resolved:
3541
       --  https://github.com/sqlc-dev/sqlc/issues/2997
3542
       cp1.id AS policy1_id,
3543
       cp1.node_id AS policy1_node_id,
3544
       cp1.version AS policy1_version,
3545
       cp1.timelock AS policy1_timelock,
3546
       cp1.fee_ppm AS policy1_fee_ppm,
3547
       cp1.base_fee_msat AS policy1_base_fee_msat,
3548
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
3549
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
3550
       cp1.last_update AS policy1_last_update,
3551
       cp1.disabled AS policy1_disabled,
3552
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3553
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3554
       cp1.message_flags AS policy1_message_flags,
3555
       cp1.channel_flags AS policy1_channel_flags,
3556
       cp1.signature AS policy1_signature,
3557
    cp1.block_height AS policy1_block_height,
3558
    cp1.disable_flags AS policy1_disable_flags,
3559

3560
       -- Policy 2
3561
       cp2.id AS policy2_id,
3562
       cp2.node_id AS policy2_node_id,
3563
       cp2.version AS policy2_version,
3564
       cp2.timelock AS policy2_timelock,
3565
       cp2.fee_ppm AS policy2_fee_ppm,
3566
       cp2.base_fee_msat AS policy2_base_fee_msat,
3567
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
3568
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
3569
       cp2.last_update AS policy2_last_update,
3570
       cp2.disabled AS policy2_disabled,
3571
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3572
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3573
       cp2.message_flags AS policy2_message_flags,
3574
       cp2.channel_flags AS policy2_channel_flags,
3575
       cp2.signature AS policy2_signature,
3576
    cp2.block_height AS policy2_block_height,
3577
    cp2.disable_flags AS policy2_disable_flags
3578

3579
FROM graph_channels c
3580
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3581
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3582
         LEFT JOIN graph_channel_policies cp1
3583
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3584
         LEFT JOIN graph_channel_policies cp2
3585
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3586
WHERE c.version = $1
3587
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
3588
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
3589
`
3590

3591
type ListChannelsForNodeIDsParams struct {
3592
        Version  int16
3593
        Node1Ids []int64
3594
        Node2Ids []int64
3595
}
3596

3597
type ListChannelsForNodeIDsRow struct {
3598
        GraphChannel                   GraphChannel
3599
        Node1Pubkey                    []byte
3600
        Node2Pubkey                    []byte
3601
        Policy1ID                      sql.NullInt64
3602
        Policy1NodeID                  sql.NullInt64
3603
        Policy1Version                 sql.NullInt16
3604
        Policy1Timelock                sql.NullInt32
3605
        Policy1FeePpm                  sql.NullInt64
3606
        Policy1BaseFeeMsat             sql.NullInt64
3607
        Policy1MinHtlcMsat             sql.NullInt64
3608
        Policy1MaxHtlcMsat             sql.NullInt64
3609
        Policy1LastUpdate              sql.NullInt64
3610
        Policy1Disabled                sql.NullBool
3611
        Policy1InboundBaseFeeMsat      sql.NullInt64
3612
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3613
        Policy1MessageFlags            sql.NullInt16
3614
        Policy1ChannelFlags            sql.NullInt16
3615
        Policy1Signature               []byte
3616
        Policy1BlockHeight             sql.NullInt64
3617
        Policy1DisableFlags            sql.NullInt16
3618
        Policy2ID                      sql.NullInt64
3619
        Policy2NodeID                  sql.NullInt64
3620
        Policy2Version                 sql.NullInt16
3621
        Policy2Timelock                sql.NullInt32
3622
        Policy2FeePpm                  sql.NullInt64
3623
        Policy2BaseFeeMsat             sql.NullInt64
3624
        Policy2MinHtlcMsat             sql.NullInt64
3625
        Policy2MaxHtlcMsat             sql.NullInt64
3626
        Policy2LastUpdate              sql.NullInt64
3627
        Policy2Disabled                sql.NullBool
3628
        Policy2InboundBaseFeeMsat      sql.NullInt64
3629
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3630
        Policy2MessageFlags            sql.NullInt16
3631
        Policy2ChannelFlags            sql.NullInt16
3632
        Policy2Signature               []byte
3633
        Policy2BlockHeight             sql.NullInt64
3634
        Policy2DisableFlags            sql.NullInt16
3635
}
3636

3637
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
3638
        query := listChannelsForNodeIDs
×
3639
        var queryParams []interface{}
×
3640
        queryParams = append(queryParams, arg.Version)
×
3641
        if len(arg.Node1Ids) > 0 {
×
3642
                for _, v := range arg.Node1Ids {
×
3643
                        queryParams = append(queryParams, v)
×
3644
                }
×
3645
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
3646
        } else {
×
3647
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
3648
        }
×
3649
        if len(arg.Node2Ids) > 0 {
×
3650
                for _, v := range arg.Node2Ids {
×
3651
                        queryParams = append(queryParams, v)
×
3652
                }
×
3653
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
3654
        } else {
×
3655
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
3656
        }
×
3657
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
3658
        if err != nil {
×
3659
                return nil, err
×
3660
        }
×
3661
        defer rows.Close()
×
3662
        var items []ListChannelsForNodeIDsRow
×
3663
        for rows.Next() {
×
3664
                var i ListChannelsForNodeIDsRow
×
3665
                if err := rows.Scan(
×
3666
                        &i.GraphChannel.ID,
×
3667
                        &i.GraphChannel.Version,
×
3668
                        &i.GraphChannel.Scid,
×
3669
                        &i.GraphChannel.NodeID1,
×
3670
                        &i.GraphChannel.NodeID2,
×
3671
                        &i.GraphChannel.Outpoint,
×
3672
                        &i.GraphChannel.Capacity,
×
3673
                        &i.GraphChannel.BitcoinKey1,
×
3674
                        &i.GraphChannel.BitcoinKey2,
×
3675
                        &i.GraphChannel.Node1Signature,
×
3676
                        &i.GraphChannel.Node2Signature,
×
3677
                        &i.GraphChannel.Bitcoin1Signature,
×
3678
                        &i.GraphChannel.Bitcoin2Signature,
×
3679
                        &i.GraphChannel.Signature,
×
3680
                        &i.GraphChannel.FundingPkScript,
×
3681
                        &i.GraphChannel.MerkleRootHash,
×
3682
                        &i.Node1Pubkey,
×
3683
                        &i.Node2Pubkey,
×
3684
                        &i.Policy1ID,
×
3685
                        &i.Policy1NodeID,
×
3686
                        &i.Policy1Version,
×
3687
                        &i.Policy1Timelock,
×
3688
                        &i.Policy1FeePpm,
×
3689
                        &i.Policy1BaseFeeMsat,
×
3690
                        &i.Policy1MinHtlcMsat,
×
3691
                        &i.Policy1MaxHtlcMsat,
×
3692
                        &i.Policy1LastUpdate,
×
3693
                        &i.Policy1Disabled,
×
3694
                        &i.Policy1InboundBaseFeeMsat,
×
3695
                        &i.Policy1InboundFeeRateMilliMsat,
×
3696
                        &i.Policy1MessageFlags,
×
3697
                        &i.Policy1ChannelFlags,
×
3698
                        &i.Policy1Signature,
×
3699
                        &i.Policy1BlockHeight,
×
3700
                        &i.Policy1DisableFlags,
×
3701
                        &i.Policy2ID,
×
3702
                        &i.Policy2NodeID,
×
3703
                        &i.Policy2Version,
×
3704
                        &i.Policy2Timelock,
×
3705
                        &i.Policy2FeePpm,
×
3706
                        &i.Policy2BaseFeeMsat,
×
3707
                        &i.Policy2MinHtlcMsat,
×
3708
                        &i.Policy2MaxHtlcMsat,
×
3709
                        &i.Policy2LastUpdate,
×
3710
                        &i.Policy2Disabled,
×
3711
                        &i.Policy2InboundBaseFeeMsat,
×
3712
                        &i.Policy2InboundFeeRateMilliMsat,
×
3713
                        &i.Policy2MessageFlags,
×
3714
                        &i.Policy2ChannelFlags,
×
3715
                        &i.Policy2Signature,
×
3716
                        &i.Policy2BlockHeight,
×
3717
                        &i.Policy2DisableFlags,
×
3718
                ); err != nil {
×
3719
                        return nil, err
×
3720
                }
×
3721
                items = append(items, i)
×
3722
        }
3723
        if err := rows.Close(); err != nil {
×
3724
                return nil, err
×
3725
        }
×
3726
        if err := rows.Err(); err != nil {
×
3727
                return nil, err
×
3728
        }
×
3729
        return items, nil
×
3730
}
3731

3732
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3733
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3734
FROM graph_channels c
3735
WHERE c.version = $1 AND c.id > $2
3736
ORDER BY c.id
3737
LIMIT $3
3738
`
3739

3740
type ListChannelsPaginatedParams struct {
3741
        Version int16
3742
        ID      int64
3743
        Limit   int32
3744
}
3745

3746
type ListChannelsPaginatedRow struct {
3747
        ID          int64
3748
        BitcoinKey1 []byte
3749
        BitcoinKey2 []byte
3750
        Outpoint    string
3751
}
3752

3753
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3754
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3755
        if err != nil {
×
3756
                return nil, err
×
3757
        }
×
3758
        defer rows.Close()
×
3759
        var items []ListChannelsPaginatedRow
×
3760
        for rows.Next() {
×
3761
                var i ListChannelsPaginatedRow
×
3762
                if err := rows.Scan(
×
3763
                        &i.ID,
×
3764
                        &i.BitcoinKey1,
×
3765
                        &i.BitcoinKey2,
×
3766
                        &i.Outpoint,
×
3767
                ); err != nil {
×
3768
                        return nil, err
×
3769
                }
×
3770
                items = append(items, i)
×
3771
        }
3772
        if err := rows.Close(); err != nil {
×
3773
                return nil, err
×
3774
        }
×
3775
        if err := rows.Err(); err != nil {
×
3776
                return nil, err
×
3777
        }
×
3778
        return items, nil
×
3779
}
3780

3781
const listChannelsPaginatedV2 = `-- name: ListChannelsPaginatedV2 :many
3782
SELECT id, outpoint, funding_pk_script
3783
FROM graph_channels c
3784
WHERE c.version = 2 AND c.id > $1
3785
ORDER BY c.id
3786
LIMIT $2
3787
`
3788

3789
type ListChannelsPaginatedV2Params struct {
3790
        ID    int64
3791
        Limit int32
3792
}
3793

3794
type ListChannelsPaginatedV2Row struct {
3795
        ID              int64
3796
        Outpoint        string
3797
        FundingPkScript []byte
3798
}
3799

3800
func (q *Queries) ListChannelsPaginatedV2(ctx context.Context, arg ListChannelsPaginatedV2Params) ([]ListChannelsPaginatedV2Row, error) {
×
3801
        rows, err := q.db.QueryContext(ctx, listChannelsPaginatedV2, arg.ID, arg.Limit)
×
3802
        if err != nil {
×
3803
                return nil, err
×
3804
        }
×
3805
        defer rows.Close()
×
3806
        var items []ListChannelsPaginatedV2Row
×
3807
        for rows.Next() {
×
3808
                var i ListChannelsPaginatedV2Row
×
3809
                if err := rows.Scan(&i.ID, &i.Outpoint, &i.FundingPkScript); err != nil {
×
3810
                        return nil, err
×
3811
                }
×
3812
                items = append(items, i)
×
3813
        }
3814
        if err := rows.Close(); err != nil {
×
3815
                return nil, err
×
3816
        }
×
3817
        if err := rows.Err(); err != nil {
×
3818
                return nil, err
×
3819
        }
×
3820
        return items, nil
×
3821
}
3822

3823
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3824
SELECT
3825
    c.id as id,
3826
    c.scid as scid,
3827
    c.capacity AS capacity,
3828

3829
    -- Join node pubkeys
3830
    n1.pub_key AS node1_pubkey,
3831
    n2.pub_key AS node2_pubkey,
3832

3833
    -- Node 1 policy
3834
    cp1.version AS policy1_version,
3835
    cp1.timelock AS policy_1_timelock,
3836
    cp1.fee_ppm AS policy_1_fee_ppm,
3837
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3838
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3839
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3840
    cp1.disabled AS policy_1_disabled,
3841
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3842
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3843
    cp1.message_flags AS policy1_message_flags,
3844
    cp1.channel_flags AS policy1_channel_flags,
3845
    cp1.block_height AS policy1_block_height,
3846
    cp1.disable_flags AS policy1_disable_flags,
3847

3848
    -- Node 2 policy
3849
    cp2.version AS policy2_version,
3850
    cp2.timelock AS policy_2_timelock,
3851
    cp2.fee_ppm AS policy_2_fee_ppm,
3852
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3853
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3854
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3855
    cp2.disabled AS policy_2_disabled,
3856
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3857
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3858
    cp2.message_flags AS policy2_message_flags,
3859
    cp2.channel_flags AS policy2_channel_flags,
3860
    cp2.block_height AS policy2_block_height,
3861
    cp2.disable_flags AS policy2_disable_flags
3862

3863
FROM graph_channels c
3864
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3865
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3866
LEFT JOIN graph_channel_policies cp1
3867
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3868
LEFT JOIN graph_channel_policies cp2
3869
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3870
WHERE c.version = $1 AND c.id > $2
3871
ORDER BY c.id
3872
LIMIT $3
3873
`
3874

3875
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3876
        Version int16
3877
        ID      int64
3878
        Limit   int32
3879
}
3880

3881
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3882
        ID                             int64
3883
        Scid                           []byte
3884
        Capacity                       sql.NullInt64
3885
        Node1Pubkey                    []byte
3886
        Node2Pubkey                    []byte
3887
        Policy1Version                 sql.NullInt16
3888
        Policy1Timelock                sql.NullInt32
3889
        Policy1FeePpm                  sql.NullInt64
3890
        Policy1BaseFeeMsat             sql.NullInt64
3891
        Policy1MinHtlcMsat             sql.NullInt64
3892
        Policy1MaxHtlcMsat             sql.NullInt64
3893
        Policy1Disabled                sql.NullBool
3894
        Policy1InboundBaseFeeMsat      sql.NullInt64
3895
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3896
        Policy1MessageFlags            sql.NullInt16
3897
        Policy1ChannelFlags            sql.NullInt16
3898
        Policy1BlockHeight             sql.NullInt64
3899
        Policy1DisableFlags            sql.NullInt16
3900
        Policy2Version                 sql.NullInt16
3901
        Policy2Timelock                sql.NullInt32
3902
        Policy2FeePpm                  sql.NullInt64
3903
        Policy2BaseFeeMsat             sql.NullInt64
3904
        Policy2MinHtlcMsat             sql.NullInt64
3905
        Policy2MaxHtlcMsat             sql.NullInt64
3906
        Policy2Disabled                sql.NullBool
3907
        Policy2InboundBaseFeeMsat      sql.NullInt64
3908
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3909
        Policy2MessageFlags            sql.NullInt16
3910
        Policy2ChannelFlags            sql.NullInt16
3911
        Policy2BlockHeight             sql.NullInt64
3912
        Policy2DisableFlags            sql.NullInt16
3913
}
3914

3915
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3916
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3917
        if err != nil {
×
3918
                return nil, err
×
3919
        }
×
3920
        defer rows.Close()
×
3921
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3922
        for rows.Next() {
×
3923
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3924
                if err := rows.Scan(
×
3925
                        &i.ID,
×
3926
                        &i.Scid,
×
3927
                        &i.Capacity,
×
3928
                        &i.Node1Pubkey,
×
3929
                        &i.Node2Pubkey,
×
3930
                        &i.Policy1Version,
×
3931
                        &i.Policy1Timelock,
×
3932
                        &i.Policy1FeePpm,
×
3933
                        &i.Policy1BaseFeeMsat,
×
3934
                        &i.Policy1MinHtlcMsat,
×
3935
                        &i.Policy1MaxHtlcMsat,
×
3936
                        &i.Policy1Disabled,
×
3937
                        &i.Policy1InboundBaseFeeMsat,
×
3938
                        &i.Policy1InboundFeeRateMilliMsat,
×
3939
                        &i.Policy1MessageFlags,
×
3940
                        &i.Policy1ChannelFlags,
×
3941
                        &i.Policy1BlockHeight,
×
3942
                        &i.Policy1DisableFlags,
×
3943
                        &i.Policy2Version,
×
3944
                        &i.Policy2Timelock,
×
3945
                        &i.Policy2FeePpm,
×
3946
                        &i.Policy2BaseFeeMsat,
×
3947
                        &i.Policy2MinHtlcMsat,
×
3948
                        &i.Policy2MaxHtlcMsat,
×
3949
                        &i.Policy2Disabled,
×
3950
                        &i.Policy2InboundBaseFeeMsat,
×
3951
                        &i.Policy2InboundFeeRateMilliMsat,
×
3952
                        &i.Policy2MessageFlags,
×
3953
                        &i.Policy2ChannelFlags,
×
3954
                        &i.Policy2BlockHeight,
×
3955
                        &i.Policy2DisableFlags,
×
3956
                ); err != nil {
×
3957
                        return nil, err
×
3958
                }
×
3959
                items = append(items, i)
×
3960
        }
3961
        if err := rows.Close(); err != nil {
×
3962
                return nil, err
×
3963
        }
×
3964
        if err := rows.Err(); err != nil {
×
3965
                return nil, err
×
3966
        }
×
3967
        return items, nil
×
3968
}
3969

3970
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3971
SELECT
3972
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
3973

3974
    -- Join node pubkeys
3975
    n1.pub_key AS node1_pubkey,
3976
    n2.pub_key AS node2_pubkey,
3977

3978
    -- Node 1 policy
3979
    cp1.id AS policy_1_id,
3980
    cp1.node_id AS policy_1_node_id,
3981
    cp1.version AS policy_1_version,
3982
    cp1.timelock AS policy_1_timelock,
3983
    cp1.fee_ppm AS policy_1_fee_ppm,
3984
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3985
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3986
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3987
    cp1.last_update AS policy_1_last_update,
3988
    cp1.disabled AS policy_1_disabled,
3989
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3990
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3991
    cp1.message_flags AS policy1_message_flags,
3992
    cp1.channel_flags AS policy1_channel_flags,
3993
    cp1.block_height AS policy1_block_height,
3994
    cp1.disable_flags AS policy1_disable_flags,
3995
    cp1.signature AS policy_1_signature,
3996

3997
    -- Node 2 policy
3998
    cp2.id AS policy_2_id,
3999
    cp2.node_id AS policy_2_node_id,
4000
    cp2.version AS policy_2_version,
4001
    cp2.timelock AS policy_2_timelock,
4002
    cp2.fee_ppm AS policy_2_fee_ppm,
4003
    cp2.base_fee_msat AS policy_2_base_fee_msat,
4004
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
4005
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
4006
    cp2.last_update AS policy_2_last_update,
4007
    cp2.disabled AS policy_2_disabled,
4008
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
4009
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
4010
    cp2.message_flags AS policy2_message_flags,
4011
    cp2.channel_flags AS policy2_channel_flags,
4012
    cp2.signature AS policy_2_signature,
4013
    cp2.block_height AS policy_2_block_height,
4014
    cp2.disable_flags AS policy_2_disable_flags
4015

4016
FROM graph_channels c
4017
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
4018
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
4019
LEFT JOIN graph_channel_policies cp1
4020
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
4021
LEFT JOIN graph_channel_policies cp2
4022
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
4023
WHERE c.version = $1 AND c.id > $2
4024
ORDER BY c.id
4025
LIMIT $3
4026
`
4027

4028
type ListChannelsWithPoliciesPaginatedParams struct {
4029
        Version int16
4030
        ID      int64
4031
        Limit   int32
4032
}
4033

4034
type ListChannelsWithPoliciesPaginatedRow struct {
4035
        GraphChannel                   GraphChannel
4036
        Node1Pubkey                    []byte
4037
        Node2Pubkey                    []byte
4038
        Policy1ID                      sql.NullInt64
4039
        Policy1NodeID                  sql.NullInt64
4040
        Policy1Version                 sql.NullInt16
4041
        Policy1Timelock                sql.NullInt32
4042
        Policy1FeePpm                  sql.NullInt64
4043
        Policy1BaseFeeMsat             sql.NullInt64
4044
        Policy1MinHtlcMsat             sql.NullInt64
4045
        Policy1MaxHtlcMsat             sql.NullInt64
4046
        Policy1LastUpdate              sql.NullInt64
4047
        Policy1Disabled                sql.NullBool
4048
        Policy1InboundBaseFeeMsat      sql.NullInt64
4049
        Policy1InboundFeeRateMilliMsat sql.NullInt64
4050
        Policy1MessageFlags            sql.NullInt16
4051
        Policy1ChannelFlags            sql.NullInt16
4052
        Policy1BlockHeight             sql.NullInt64
4053
        Policy1DisableFlags            sql.NullInt16
4054
        Policy1Signature               []byte
4055
        Policy2ID                      sql.NullInt64
4056
        Policy2NodeID                  sql.NullInt64
4057
        Policy2Version                 sql.NullInt16
4058
        Policy2Timelock                sql.NullInt32
4059
        Policy2FeePpm                  sql.NullInt64
4060
        Policy2BaseFeeMsat             sql.NullInt64
4061
        Policy2MinHtlcMsat             sql.NullInt64
4062
        Policy2MaxHtlcMsat             sql.NullInt64
4063
        Policy2LastUpdate              sql.NullInt64
4064
        Policy2Disabled                sql.NullBool
4065
        Policy2InboundBaseFeeMsat      sql.NullInt64
4066
        Policy2InboundFeeRateMilliMsat sql.NullInt64
4067
        Policy2MessageFlags            sql.NullInt16
4068
        Policy2ChannelFlags            sql.NullInt16
4069
        Policy2Signature               []byte
4070
        Policy2BlockHeight             sql.NullInt64
4071
        Policy2DisableFlags            sql.NullInt16
4072
}
4073

4074
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
4075
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
4076
        if err != nil {
×
4077
                return nil, err
×
4078
        }
×
4079
        defer rows.Close()
×
4080
        var items []ListChannelsWithPoliciesPaginatedRow
×
4081
        for rows.Next() {
×
4082
                var i ListChannelsWithPoliciesPaginatedRow
×
4083
                if err := rows.Scan(
×
4084
                        &i.GraphChannel.ID,
×
4085
                        &i.GraphChannel.Version,
×
4086
                        &i.GraphChannel.Scid,
×
4087
                        &i.GraphChannel.NodeID1,
×
4088
                        &i.GraphChannel.NodeID2,
×
4089
                        &i.GraphChannel.Outpoint,
×
4090
                        &i.GraphChannel.Capacity,
×
4091
                        &i.GraphChannel.BitcoinKey1,
×
4092
                        &i.GraphChannel.BitcoinKey2,
×
4093
                        &i.GraphChannel.Node1Signature,
×
4094
                        &i.GraphChannel.Node2Signature,
×
4095
                        &i.GraphChannel.Bitcoin1Signature,
×
4096
                        &i.GraphChannel.Bitcoin2Signature,
×
4097
                        &i.GraphChannel.Signature,
×
4098
                        &i.GraphChannel.FundingPkScript,
×
4099
                        &i.GraphChannel.MerkleRootHash,
×
4100
                        &i.Node1Pubkey,
×
4101
                        &i.Node2Pubkey,
×
4102
                        &i.Policy1ID,
×
4103
                        &i.Policy1NodeID,
×
4104
                        &i.Policy1Version,
×
4105
                        &i.Policy1Timelock,
×
4106
                        &i.Policy1FeePpm,
×
4107
                        &i.Policy1BaseFeeMsat,
×
4108
                        &i.Policy1MinHtlcMsat,
×
4109
                        &i.Policy1MaxHtlcMsat,
×
4110
                        &i.Policy1LastUpdate,
×
4111
                        &i.Policy1Disabled,
×
4112
                        &i.Policy1InboundBaseFeeMsat,
×
4113
                        &i.Policy1InboundFeeRateMilliMsat,
×
4114
                        &i.Policy1MessageFlags,
×
4115
                        &i.Policy1ChannelFlags,
×
4116
                        &i.Policy1BlockHeight,
×
4117
                        &i.Policy1DisableFlags,
×
4118
                        &i.Policy1Signature,
×
4119
                        &i.Policy2ID,
×
4120
                        &i.Policy2NodeID,
×
4121
                        &i.Policy2Version,
×
4122
                        &i.Policy2Timelock,
×
4123
                        &i.Policy2FeePpm,
×
4124
                        &i.Policy2BaseFeeMsat,
×
4125
                        &i.Policy2MinHtlcMsat,
×
4126
                        &i.Policy2MaxHtlcMsat,
×
4127
                        &i.Policy2LastUpdate,
×
4128
                        &i.Policy2Disabled,
×
4129
                        &i.Policy2InboundBaseFeeMsat,
×
4130
                        &i.Policy2InboundFeeRateMilliMsat,
×
4131
                        &i.Policy2MessageFlags,
×
4132
                        &i.Policy2ChannelFlags,
×
4133
                        &i.Policy2Signature,
×
4134
                        &i.Policy2BlockHeight,
×
4135
                        &i.Policy2DisableFlags,
×
4136
                ); err != nil {
×
4137
                        return nil, err
×
4138
                }
×
4139
                items = append(items, i)
×
4140
        }
4141
        if err := rows.Close(); err != nil {
×
4142
                return nil, err
×
4143
        }
×
4144
        if err := rows.Err(); err != nil {
×
4145
                return nil, err
×
4146
        }
×
4147
        return items, nil
×
4148
}
4149

4150
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
4151
SELECT id, pub_key
4152
FROM graph_nodes
4153
WHERE version = $1  AND id > $2
4154
ORDER BY id
4155
LIMIT $3
4156
`
4157

4158
type ListNodeIDsAndPubKeysParams struct {
4159
        Version int16
4160
        ID      int64
4161
        Limit   int32
4162
}
4163

4164
type ListNodeIDsAndPubKeysRow struct {
4165
        ID     int64
4166
        PubKey []byte
4167
}
4168

4169
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
4170
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
4171
        if err != nil {
×
4172
                return nil, err
×
4173
        }
×
4174
        defer rows.Close()
×
4175
        var items []ListNodeIDsAndPubKeysRow
×
4176
        for rows.Next() {
×
4177
                var i ListNodeIDsAndPubKeysRow
×
4178
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
4179
                        return nil, err
×
4180
                }
×
4181
                items = append(items, i)
×
4182
        }
4183
        if err := rows.Close(); err != nil {
×
4184
                return nil, err
×
4185
        }
×
4186
        if err := rows.Err(); err != nil {
×
4187
                return nil, err
×
4188
        }
×
4189
        return items, nil
×
4190
}
4191

4192
const listNodesPaginated = `-- name: ListNodesPaginated :many
4193
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
4194
FROM graph_nodes
4195
WHERE version = $1 AND id > $2
4196
ORDER BY id
4197
LIMIT $3
4198
`
4199

4200
type ListNodesPaginatedParams struct {
4201
        Version int16
4202
        ID      int64
4203
        Limit   int32
4204
}
4205

4206
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
4207
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
4208
        if err != nil {
×
4209
                return nil, err
×
4210
        }
×
4211
        defer rows.Close()
×
4212
        var items []GraphNode
×
4213
        for rows.Next() {
×
4214
                var i GraphNode
×
4215
                if err := rows.Scan(
×
4216
                        &i.ID,
×
4217
                        &i.Version,
×
4218
                        &i.PubKey,
×
4219
                        &i.Alias,
×
4220
                        &i.LastUpdate,
×
4221
                        &i.Color,
×
4222
                        &i.Signature,
×
4223
                        &i.BlockHeight,
×
4224
                ); err != nil {
×
4225
                        return nil, err
×
4226
                }
×
4227
                items = append(items, i)
×
4228
        }
4229
        if err := rows.Close(); err != nil {
×
4230
                return nil, err
×
4231
        }
×
4232
        if err := rows.Err(); err != nil {
×
4233
                return nil, err
×
4234
        }
×
4235
        return items, nil
×
4236
}
4237

4238
const listPreferredChannelsWithPoliciesPaginated = `-- name: ListPreferredChannelsWithPoliciesPaginated :many
4239
SELECT
4240
    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, c.signature, c.funding_pk_script, c.merkle_root_hash,
4241

4242
    -- Join node pubkeys
4243
    n1.pub_key AS node1_pubkey,
4244
    n2.pub_key AS node2_pubkey,
4245

4246
    -- Node 1 policy
4247
    cp1.id AS policy_1_id,
4248
    cp1.node_id AS policy_1_node_id,
4249
    cp1.version AS policy_1_version,
4250
    cp1.timelock AS policy_1_timelock,
4251
    cp1.fee_ppm AS policy_1_fee_ppm,
4252
    cp1.base_fee_msat AS policy_1_base_fee_msat,
4253
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
4254
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
4255
    cp1.last_update AS policy_1_last_update,
4256
    cp1.disabled AS policy_1_disabled,
4257
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
4258
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
4259
    cp1.message_flags AS policy1_message_flags,
4260
    cp1.channel_flags AS policy1_channel_flags,
4261
    cp1.block_height AS policy1_block_height,
4262
    cp1.disable_flags AS policy1_disable_flags,
4263
    cp1.signature AS policy_1_signature,
4264

4265
    -- Node 2 policy
4266
    cp2.id AS policy_2_id,
4267
    cp2.node_id AS policy_2_node_id,
4268
    cp2.version AS policy_2_version,
4269
    cp2.timelock AS policy_2_timelock,
4270
    cp2.fee_ppm AS policy_2_fee_ppm,
4271
    cp2.base_fee_msat AS policy_2_base_fee_msat,
4272
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
4273
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
4274
    cp2.last_update AS policy_2_last_update,
4275
    cp2.disabled AS policy_2_disabled,
4276
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
4277
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
4278
    cp2.message_flags AS policy2_message_flags,
4279
    cp2.channel_flags AS policy2_channel_flags,
4280
    cp2.signature AS policy_2_signature,
4281
    cp2.block_height AS policy_2_block_height,
4282
    cp2.disable_flags AS policy_2_disable_flags
4283

4284
FROM graph_preferred_channels pc
4285
JOIN graph_channels c ON c.id = pc.channel_id
4286
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
4287
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
4288
LEFT JOIN graph_channel_policies cp1
4289
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
4290
LEFT JOIN graph_channel_policies cp2
4291
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
4292
WHERE pc.scid > $1
4293
ORDER BY pc.scid
4294
LIMIT $2
4295
`
4296

4297
type ListPreferredChannelsWithPoliciesPaginatedParams struct {
4298
        Scid  []byte
4299
        Limit int32
4300
}
4301

4302
type ListPreferredChannelsWithPoliciesPaginatedRow struct {
4303
        GraphChannel                   GraphChannel
4304
        Node1Pubkey                    []byte
4305
        Node2Pubkey                    []byte
4306
        Policy1ID                      sql.NullInt64
4307
        Policy1NodeID                  sql.NullInt64
4308
        Policy1Version                 sql.NullInt16
4309
        Policy1Timelock                sql.NullInt32
4310
        Policy1FeePpm                  sql.NullInt64
4311
        Policy1BaseFeeMsat             sql.NullInt64
4312
        Policy1MinHtlcMsat             sql.NullInt64
4313
        Policy1MaxHtlcMsat             sql.NullInt64
4314
        Policy1LastUpdate              sql.NullInt64
4315
        Policy1Disabled                sql.NullBool
4316
        Policy1InboundBaseFeeMsat      sql.NullInt64
4317
        Policy1InboundFeeRateMilliMsat sql.NullInt64
4318
        Policy1MessageFlags            sql.NullInt16
4319
        Policy1ChannelFlags            sql.NullInt16
4320
        Policy1BlockHeight             sql.NullInt64
4321
        Policy1DisableFlags            sql.NullInt16
4322
        Policy1Signature               []byte
4323
        Policy2ID                      sql.NullInt64
4324
        Policy2NodeID                  sql.NullInt64
4325
        Policy2Version                 sql.NullInt16
4326
        Policy2Timelock                sql.NullInt32
4327
        Policy2FeePpm                  sql.NullInt64
4328
        Policy2BaseFeeMsat             sql.NullInt64
4329
        Policy2MinHtlcMsat             sql.NullInt64
4330
        Policy2MaxHtlcMsat             sql.NullInt64
4331
        Policy2LastUpdate              sql.NullInt64
4332
        Policy2Disabled                sql.NullBool
4333
        Policy2InboundBaseFeeMsat      sql.NullInt64
4334
        Policy2InboundFeeRateMilliMsat sql.NullInt64
4335
        Policy2MessageFlags            sql.NullInt16
4336
        Policy2ChannelFlags            sql.NullInt16
4337
        Policy2Signature               []byte
4338
        Policy2BlockHeight             sql.NullInt64
4339
        Policy2DisableFlags            sql.NullInt16
4340
}
4341

NEW
4342
func (q *Queries) ListPreferredChannelsWithPoliciesPaginated(ctx context.Context, arg ListPreferredChannelsWithPoliciesPaginatedParams) ([]ListPreferredChannelsWithPoliciesPaginatedRow, error) {
×
NEW
4343
        rows, err := q.db.QueryContext(ctx, listPreferredChannelsWithPoliciesPaginated, arg.Scid, arg.Limit)
×
NEW
4344
        if err != nil {
×
NEW
4345
                return nil, err
×
NEW
4346
        }
×
NEW
4347
        defer rows.Close()
×
NEW
4348
        var items []ListPreferredChannelsWithPoliciesPaginatedRow
×
NEW
4349
        for rows.Next() {
×
NEW
4350
                var i ListPreferredChannelsWithPoliciesPaginatedRow
×
NEW
4351
                if err := rows.Scan(
×
NEW
4352
                        &i.GraphChannel.ID,
×
NEW
4353
                        &i.GraphChannel.Version,
×
NEW
4354
                        &i.GraphChannel.Scid,
×
NEW
4355
                        &i.GraphChannel.NodeID1,
×
NEW
4356
                        &i.GraphChannel.NodeID2,
×
NEW
4357
                        &i.GraphChannel.Outpoint,
×
NEW
4358
                        &i.GraphChannel.Capacity,
×
NEW
4359
                        &i.GraphChannel.BitcoinKey1,
×
NEW
4360
                        &i.GraphChannel.BitcoinKey2,
×
NEW
4361
                        &i.GraphChannel.Node1Signature,
×
NEW
4362
                        &i.GraphChannel.Node2Signature,
×
NEW
4363
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
4364
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
4365
                        &i.GraphChannel.Signature,
×
NEW
4366
                        &i.GraphChannel.FundingPkScript,
×
NEW
4367
                        &i.GraphChannel.MerkleRootHash,
×
NEW
4368
                        &i.Node1Pubkey,
×
NEW
4369
                        &i.Node2Pubkey,
×
NEW
4370
                        &i.Policy1ID,
×
NEW
4371
                        &i.Policy1NodeID,
×
NEW
4372
                        &i.Policy1Version,
×
NEW
4373
                        &i.Policy1Timelock,
×
NEW
4374
                        &i.Policy1FeePpm,
×
NEW
4375
                        &i.Policy1BaseFeeMsat,
×
NEW
4376
                        &i.Policy1MinHtlcMsat,
×
NEW
4377
                        &i.Policy1MaxHtlcMsat,
×
NEW
4378
                        &i.Policy1LastUpdate,
×
NEW
4379
                        &i.Policy1Disabled,
×
NEW
4380
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
4381
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
4382
                        &i.Policy1MessageFlags,
×
NEW
4383
                        &i.Policy1ChannelFlags,
×
NEW
4384
                        &i.Policy1BlockHeight,
×
NEW
4385
                        &i.Policy1DisableFlags,
×
NEW
4386
                        &i.Policy1Signature,
×
NEW
4387
                        &i.Policy2ID,
×
NEW
4388
                        &i.Policy2NodeID,
×
NEW
4389
                        &i.Policy2Version,
×
NEW
4390
                        &i.Policy2Timelock,
×
NEW
4391
                        &i.Policy2FeePpm,
×
NEW
4392
                        &i.Policy2BaseFeeMsat,
×
NEW
4393
                        &i.Policy2MinHtlcMsat,
×
NEW
4394
                        &i.Policy2MaxHtlcMsat,
×
NEW
4395
                        &i.Policy2LastUpdate,
×
NEW
4396
                        &i.Policy2Disabled,
×
NEW
4397
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
4398
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
4399
                        &i.Policy2MessageFlags,
×
NEW
4400
                        &i.Policy2ChannelFlags,
×
NEW
4401
                        &i.Policy2Signature,
×
NEW
4402
                        &i.Policy2BlockHeight,
×
NEW
4403
                        &i.Policy2DisableFlags,
×
NEW
4404
                ); err != nil {
×
NEW
4405
                        return nil, err
×
NEW
4406
                }
×
NEW
4407
                items = append(items, i)
×
4408
        }
NEW
4409
        if err := rows.Close(); err != nil {
×
NEW
4410
                return nil, err
×
NEW
4411
        }
×
NEW
4412
        if err := rows.Err(); err != nil {
×
NEW
4413
                return nil, err
×
NEW
4414
        }
×
NEW
4415
        return items, nil
×
4416
}
4417

4418
const listPreferredDirectedChannelsPaginated = `-- name: ListPreferredDirectedChannelsPaginated :many
4419
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, c.signature, c.funding_pk_script, c.merkle_root_hash,
4420
    n1.pub_key AS node1_pubkey,
4421
    n2.pub_key AS node2_pubkey,
4422

4423
    -- Policy 1
4424
    cp1.id AS policy1_id,
4425
    cp1.node_id AS policy1_node_id,
4426
    cp1.version AS policy1_version,
4427
    cp1.timelock AS policy1_timelock,
4428
    cp1.fee_ppm AS policy1_fee_ppm,
4429
    cp1.base_fee_msat AS policy1_base_fee_msat,
4430
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
4431
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
4432
    cp1.last_update AS policy1_last_update,
4433
    cp1.disabled AS policy1_disabled,
4434
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
4435
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
4436
    cp1.message_flags AS policy1_message_flags,
4437
    cp1.channel_flags AS policy1_channel_flags,
4438
    cp1.signature AS policy1_signature,
4439
    cp1.block_height AS policy1_block_height,
4440
    cp1.disable_flags AS policy1_disable_flags,
4441

4442
    -- Policy 2
4443
    cp2.id AS policy2_id,
4444
    cp2.node_id AS policy2_node_id,
4445
    cp2.version AS policy2_version,
4446
    cp2.timelock AS policy2_timelock,
4447
    cp2.fee_ppm AS policy2_fee_ppm,
4448
    cp2.base_fee_msat AS policy2_base_fee_msat,
4449
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
4450
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
4451
    cp2.last_update AS policy2_last_update,
4452
    cp2.disabled AS policy2_disabled,
4453
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
4454
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
4455
    cp2.message_flags AS policy2_message_flags,
4456
    cp2.channel_flags AS policy2_channel_flags,
4457
    cp2.signature AS policy2_signature,
4458
    cp2.block_height AS policy2_block_height,
4459
    cp2.disable_flags AS policy2_disable_flags
4460

4461
FROM graph_preferred_channels pc
4462
JOIN graph_channels c ON c.id = pc.channel_id
4463
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
4464
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
4465
LEFT JOIN graph_channel_policies cp1
4466
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
4467
LEFT JOIN graph_channel_policies cp2
4468
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
4469
WHERE (n1.pub_key = $1 OR n2.pub_key = $1)
4470
  AND pc.scid > $2
4471
ORDER BY pc.scid
4472
LIMIT $3
4473
`
4474

4475
type ListPreferredDirectedChannelsPaginatedParams struct {
4476
        PubKey    []byte
4477
        Scid      []byte
4478
        PageLimit int32
4479
}
4480

4481
type ListPreferredDirectedChannelsPaginatedRow struct {
4482
        GraphChannel                   GraphChannel
4483
        Node1Pubkey                    []byte
4484
        Node2Pubkey                    []byte
4485
        Policy1ID                      sql.NullInt64
4486
        Policy1NodeID                  sql.NullInt64
4487
        Policy1Version                 sql.NullInt16
4488
        Policy1Timelock                sql.NullInt32
4489
        Policy1FeePpm                  sql.NullInt64
4490
        Policy1BaseFeeMsat             sql.NullInt64
4491
        Policy1MinHtlcMsat             sql.NullInt64
4492
        Policy1MaxHtlcMsat             sql.NullInt64
4493
        Policy1LastUpdate              sql.NullInt64
4494
        Policy1Disabled                sql.NullBool
4495
        Policy1InboundBaseFeeMsat      sql.NullInt64
4496
        Policy1InboundFeeRateMilliMsat sql.NullInt64
4497
        Policy1MessageFlags            sql.NullInt16
4498
        Policy1ChannelFlags            sql.NullInt16
4499
        Policy1Signature               []byte
4500
        Policy1BlockHeight             sql.NullInt64
4501
        Policy1DisableFlags            sql.NullInt16
4502
        Policy2ID                      sql.NullInt64
4503
        Policy2NodeID                  sql.NullInt64
4504
        Policy2Version                 sql.NullInt16
4505
        Policy2Timelock                sql.NullInt32
4506
        Policy2FeePpm                  sql.NullInt64
4507
        Policy2BaseFeeMsat             sql.NullInt64
4508
        Policy2MinHtlcMsat             sql.NullInt64
4509
        Policy2MaxHtlcMsat             sql.NullInt64
4510
        Policy2LastUpdate              sql.NullInt64
4511
        Policy2Disabled                sql.NullBool
4512
        Policy2InboundBaseFeeMsat      sql.NullInt64
4513
        Policy2InboundFeeRateMilliMsat sql.NullInt64
4514
        Policy2MessageFlags            sql.NullInt16
4515
        Policy2ChannelFlags            sql.NullInt16
4516
        Policy2Signature               []byte
4517
        Policy2BlockHeight             sql.NullInt64
4518
        Policy2DisableFlags            sql.NullInt16
4519
}
4520

NEW
4521
func (q *Queries) ListPreferredDirectedChannelsPaginated(ctx context.Context, arg ListPreferredDirectedChannelsPaginatedParams) ([]ListPreferredDirectedChannelsPaginatedRow, error) {
×
NEW
4522
        rows, err := q.db.QueryContext(ctx, listPreferredDirectedChannelsPaginated, arg.PubKey, arg.Scid, arg.PageLimit)
×
NEW
4523
        if err != nil {
×
NEW
4524
                return nil, err
×
NEW
4525
        }
×
NEW
4526
        defer rows.Close()
×
NEW
4527
        var items []ListPreferredDirectedChannelsPaginatedRow
×
NEW
4528
        for rows.Next() {
×
NEW
4529
                var i ListPreferredDirectedChannelsPaginatedRow
×
NEW
4530
                if err := rows.Scan(
×
NEW
4531
                        &i.GraphChannel.ID,
×
NEW
4532
                        &i.GraphChannel.Version,
×
NEW
4533
                        &i.GraphChannel.Scid,
×
NEW
4534
                        &i.GraphChannel.NodeID1,
×
NEW
4535
                        &i.GraphChannel.NodeID2,
×
NEW
4536
                        &i.GraphChannel.Outpoint,
×
NEW
4537
                        &i.GraphChannel.Capacity,
×
NEW
4538
                        &i.GraphChannel.BitcoinKey1,
×
NEW
4539
                        &i.GraphChannel.BitcoinKey2,
×
NEW
4540
                        &i.GraphChannel.Node1Signature,
×
NEW
4541
                        &i.GraphChannel.Node2Signature,
×
NEW
4542
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
4543
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
4544
                        &i.GraphChannel.Signature,
×
NEW
4545
                        &i.GraphChannel.FundingPkScript,
×
NEW
4546
                        &i.GraphChannel.MerkleRootHash,
×
NEW
4547
                        &i.Node1Pubkey,
×
NEW
4548
                        &i.Node2Pubkey,
×
NEW
4549
                        &i.Policy1ID,
×
NEW
4550
                        &i.Policy1NodeID,
×
NEW
4551
                        &i.Policy1Version,
×
NEW
4552
                        &i.Policy1Timelock,
×
NEW
4553
                        &i.Policy1FeePpm,
×
NEW
4554
                        &i.Policy1BaseFeeMsat,
×
NEW
4555
                        &i.Policy1MinHtlcMsat,
×
NEW
4556
                        &i.Policy1MaxHtlcMsat,
×
NEW
4557
                        &i.Policy1LastUpdate,
×
NEW
4558
                        &i.Policy1Disabled,
×
NEW
4559
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
4560
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
4561
                        &i.Policy1MessageFlags,
×
NEW
4562
                        &i.Policy1ChannelFlags,
×
NEW
4563
                        &i.Policy1Signature,
×
NEW
4564
                        &i.Policy1BlockHeight,
×
NEW
4565
                        &i.Policy1DisableFlags,
×
NEW
4566
                        &i.Policy2ID,
×
NEW
4567
                        &i.Policy2NodeID,
×
NEW
4568
                        &i.Policy2Version,
×
NEW
4569
                        &i.Policy2Timelock,
×
NEW
4570
                        &i.Policy2FeePpm,
×
NEW
4571
                        &i.Policy2BaseFeeMsat,
×
NEW
4572
                        &i.Policy2MinHtlcMsat,
×
NEW
4573
                        &i.Policy2MaxHtlcMsat,
×
NEW
4574
                        &i.Policy2LastUpdate,
×
NEW
4575
                        &i.Policy2Disabled,
×
NEW
4576
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
4577
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
4578
                        &i.Policy2MessageFlags,
×
NEW
4579
                        &i.Policy2ChannelFlags,
×
NEW
4580
                        &i.Policy2Signature,
×
NEW
4581
                        &i.Policy2BlockHeight,
×
NEW
4582
                        &i.Policy2DisableFlags,
×
NEW
4583
                ); err != nil {
×
NEW
4584
                        return nil, err
×
NEW
4585
                }
×
NEW
4586
                items = append(items, i)
×
4587
        }
NEW
4588
        if err := rows.Close(); err != nil {
×
NEW
4589
                return nil, err
×
NEW
4590
        }
×
NEW
4591
        if err := rows.Err(); err != nil {
×
NEW
4592
                return nil, err
×
NEW
4593
        }
×
NEW
4594
        return items, nil
×
4595
}
4596

4597
const listPreferredNodesPaginated = `-- name: ListPreferredNodesPaginated :many
4598
SELECT n.id, n.version, n.pub_key, n.alias, n.last_update, n.color, n.signature, n.block_height
4599
FROM graph_preferred_nodes pn
4600
JOIN graph_nodes n ON n.id = pn.node_id
4601
WHERE pn.pub_key > $1
4602
ORDER BY pn.pub_key
4603
LIMIT $2
4604
`
4605

4606
type ListPreferredNodesPaginatedParams struct {
4607
        PubKey []byte
4608
        Limit  int32
4609
}
4610

4611
type ListPreferredNodesPaginatedRow struct {
4612
        GraphNode GraphNode
4613
}
4614

NEW
4615
func (q *Queries) ListPreferredNodesPaginated(ctx context.Context, arg ListPreferredNodesPaginatedParams) ([]ListPreferredNodesPaginatedRow, error) {
×
NEW
4616
        rows, err := q.db.QueryContext(ctx, listPreferredNodesPaginated, arg.PubKey, arg.Limit)
×
NEW
4617
        if err != nil {
×
NEW
4618
                return nil, err
×
NEW
4619
        }
×
NEW
4620
        defer rows.Close()
×
NEW
4621
        var items []ListPreferredNodesPaginatedRow
×
NEW
4622
        for rows.Next() {
×
NEW
4623
                var i ListPreferredNodesPaginatedRow
×
NEW
4624
                if err := rows.Scan(
×
NEW
4625
                        &i.GraphNode.ID,
×
NEW
4626
                        &i.GraphNode.Version,
×
NEW
4627
                        &i.GraphNode.PubKey,
×
NEW
4628
                        &i.GraphNode.Alias,
×
NEW
4629
                        &i.GraphNode.LastUpdate,
×
NEW
4630
                        &i.GraphNode.Color,
×
NEW
4631
                        &i.GraphNode.Signature,
×
NEW
4632
                        &i.GraphNode.BlockHeight,
×
NEW
4633
                ); err != nil {
×
NEW
4634
                        return nil, err
×
NEW
4635
                }
×
NEW
4636
                items = append(items, i)
×
4637
        }
NEW
4638
        if err := rows.Close(); err != nil {
×
NEW
4639
                return nil, err
×
NEW
4640
        }
×
NEW
4641
        if err := rows.Err(); err != nil {
×
NEW
4642
                return nil, err
×
NEW
4643
        }
×
NEW
4644
        return items, nil
×
4645
}
4646

4647
const nodeExists = `-- name: NodeExists :one
4648
SELECT EXISTS (
4649
    SELECT 1
4650
    FROM graph_nodes
4651
    WHERE pub_key = $1
4652
      AND version = $2
4653
) AS node_exists
4654
`
4655

4656
type NodeExistsParams struct {
4657
        PubKey  []byte
4658
        Version int16
4659
}
4660

4661
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
4662
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
4663
        var node_exists bool
×
4664
        err := row.Scan(&node_exists)
×
4665
        return node_exists, err
×
4666
}
×
4667

4668
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
4669
/* ─────────────────────────────────────────────
4670
   graph_channel_policy_extra_types table queries
4671
   ─────────────────────────────────────────────
4672
*/
4673

4674
INSERT INTO graph_channel_policy_extra_types (
4675
    channel_policy_id, type, value
4676
)
4677
VALUES ($1, $2, $3)
4678
ON CONFLICT (channel_policy_id, type)
4679
    -- If a conflict occurs on channel_policy_id and type, then we update the
4680
    -- value.
4681
    DO UPDATE SET value = EXCLUDED.value
4682
`
4683

4684
type UpsertChanPolicyExtraTypeParams struct {
4685
        ChannelPolicyID int64
4686
        Type            int64
4687
        Value           []byte
4688
}
4689

4690
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
4691
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
4692
        return err
×
4693
}
×
4694

4695
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
4696
/* ─────────────────────────────────────────────
4697
   graph_channel_extra_types table queries
4698
   ─────────────────────────────────────────────
4699
*/
4700

4701
INSERT INTO graph_channel_extra_types (
4702
    channel_id, type, value
4703
)
4704
VALUES ($1, $2, $3)
4705
    ON CONFLICT (channel_id, type)
4706
    -- Update the value if a conflict occurs on channel_id and type.
4707
    DO UPDATE SET value = EXCLUDED.value
4708
`
4709

4710
type UpsertChannelExtraTypeParams struct {
4711
        ChannelID int64
4712
        Type      int64
4713
        Value     []byte
4714
}
4715

4716
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
4717
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
4718
        return err
×
4719
}
×
4720

4721
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
4722
/* ─────────────────────────────────────────────
4723
   graph_channel_policies table queries
4724
   ─────────────────────────────────────────────
4725
*/
4726

4727
INSERT INTO graph_channel_policies (
4728
    version, channel_id, node_id, timelock, fee_ppm,
4729
    base_fee_msat, min_htlc_msat, last_update, disabled,
4730
    max_htlc_msat, inbound_base_fee_msat,
4731
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
4732
    signature, block_height, disable_flags
4733
) VALUES  (
4734
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
4735
)
4736
ON CONFLICT (channel_id, node_id, version)
4737
    -- Update the following fields if a conflict occurs on channel_id,
4738
    -- node_id, and version.
4739
    DO UPDATE SET
4740
        timelock = EXCLUDED.timelock,
4741
        fee_ppm = EXCLUDED.fee_ppm,
4742
        base_fee_msat = EXCLUDED.base_fee_msat,
4743
        min_htlc_msat = EXCLUDED.min_htlc_msat,
4744
        last_update = EXCLUDED.last_update,
4745
        disabled = EXCLUDED.disabled,
4746
        max_htlc_msat = EXCLUDED.max_htlc_msat,
4747
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
4748
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
4749
        message_flags = EXCLUDED.message_flags,
4750
        channel_flags = EXCLUDED.channel_flags,
4751
        signature = EXCLUDED.signature,
4752
        block_height = EXCLUDED.block_height,
4753
        disable_flags = EXCLUDED.disable_flags
4754
WHERE (
4755
    EXCLUDED.version = 1 AND (
4756
        graph_channel_policies.last_update IS NULL
4757
        OR EXCLUDED.last_update > graph_channel_policies.last_update
4758
    )
4759
)
4760
OR (
4761
    EXCLUDED.version = 2 AND (
4762
        graph_channel_policies.block_height IS NULL
4763
        OR EXCLUDED.block_height >= graph_channel_policies.block_height
4764
    )
4765
)
4766
RETURNING id
4767
`
4768

4769
type UpsertEdgePolicyParams struct {
4770
        Version                 int16
4771
        ChannelID               int64
4772
        NodeID                  int64
4773
        Timelock                int32
4774
        FeePpm                  int64
4775
        BaseFeeMsat             int64
4776
        MinHtlcMsat             int64
4777
        LastUpdate              sql.NullInt64
4778
        Disabled                sql.NullBool
4779
        MaxHtlcMsat             sql.NullInt64
4780
        InboundBaseFeeMsat      sql.NullInt64
4781
        InboundFeeRateMilliMsat sql.NullInt64
4782
        MessageFlags            sql.NullInt16
4783
        ChannelFlags            sql.NullInt16
4784
        Signature               []byte
4785
        BlockHeight             sql.NullInt64
4786
        DisableFlags            sql.NullInt16
4787
}
4788

4789
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
4790
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
4791
                arg.Version,
×
4792
                arg.ChannelID,
×
4793
                arg.NodeID,
×
4794
                arg.Timelock,
×
4795
                arg.FeePpm,
×
4796
                arg.BaseFeeMsat,
×
4797
                arg.MinHtlcMsat,
×
4798
                arg.LastUpdate,
×
4799
                arg.Disabled,
×
4800
                arg.MaxHtlcMsat,
×
4801
                arg.InboundBaseFeeMsat,
×
4802
                arg.InboundFeeRateMilliMsat,
×
4803
                arg.MessageFlags,
×
4804
                arg.ChannelFlags,
×
4805
                arg.Signature,
×
4806
                arg.BlockHeight,
×
4807
                arg.DisableFlags,
×
4808
        )
×
4809
        var id int64
×
4810
        err := row.Scan(&id)
×
4811
        return id, err
×
4812
}
×
4813

4814
const upsertNode = `-- name: UpsertNode :one
4815
/* ─────────────────────────────────────────────
4816
   graph_nodes table queries
4817
   ───────────────────────────��─────────────────
4818
*/
4819

4820
INSERT INTO graph_nodes (
4821
    version, pub_key, alias, last_update, block_height, color, signature
4822
) VALUES (
4823
    $1, $2, $3, $4, $5, $6, $7
4824
)
4825
ON CONFLICT (pub_key, version)
4826
    -- Update the following fields if a conflict occurs on pub_key
4827
    -- and version.
4828
    DO UPDATE SET
4829
        alias = EXCLUDED.alias,
4830
        last_update = EXCLUDED.last_update,
4831
        block_height = EXCLUDED.block_height,
4832
        color = EXCLUDED.color,
4833
        signature = EXCLUDED.signature
4834
WHERE (graph_nodes.last_update IS NULL
4835
    OR EXCLUDED.last_update > graph_nodes.last_update)
4836
AND (graph_nodes.block_height IS NULL
4837
    OR EXCLUDED.block_height >= graph_nodes.block_height)
4838
RETURNING id
4839
`
4840

4841
type UpsertNodeParams struct {
4842
        Version     int16
4843
        PubKey      []byte
4844
        Alias       sql.NullString
4845
        LastUpdate  sql.NullInt64
4846
        BlockHeight sql.NullInt64
4847
        Color       sql.NullString
4848
        Signature   []byte
4849
}
4850

4851
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
4852
        row := q.db.QueryRowContext(ctx, upsertNode,
×
4853
                arg.Version,
×
4854
                arg.PubKey,
×
4855
                arg.Alias,
×
4856
                arg.LastUpdate,
×
4857
                arg.BlockHeight,
×
4858
                arg.Color,
×
4859
                arg.Signature,
×
4860
        )
×
4861
        var id int64
×
4862
        err := row.Scan(&id)
×
4863
        return id, err
×
4864
}
×
4865

4866
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
4867
/* ─────────────────────────────────────────────
4868
   graph_node_addresses table queries
4869
   ───────────────────────────────────��─────────
4870
*/
4871

4872
INSERT INTO graph_node_addresses (
4873
    node_id,
4874
    type,
4875
    address,
4876
    position
4877
) VALUES (
4878
    $1, $2, $3, $4
4879
) ON CONFLICT (node_id, type, position)
4880
    DO UPDATE SET address = EXCLUDED.address
4881
`
4882

4883
type UpsertNodeAddressParams struct {
4884
        NodeID   int64
4885
        Type     int16
4886
        Address  string
4887
        Position int32
4888
}
4889

4890
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
4891
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
4892
                arg.NodeID,
×
4893
                arg.Type,
×
4894
                arg.Address,
×
4895
                arg.Position,
×
4896
        )
×
4897
        return err
×
4898
}
×
4899

4900
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
4901
/* ─────────────────────────────────────────────
4902
   graph_node_extra_types table queries
4903
   ─────────────────────────────────────────────
4904
*/
4905

4906
INSERT INTO graph_node_extra_types (
4907
    node_id, type, value
4908
)
4909
VALUES ($1, $2, $3)
4910
ON CONFLICT (type, node_id)
4911
    -- Update the value if a conflict occurs on type
4912
    -- and node_id.
4913
    DO UPDATE SET value = EXCLUDED.value
4914
`
4915

4916
type UpsertNodeExtraTypeParams struct {
4917
        NodeID int64
4918
        Type   int64
4919
        Value  []byte
4920
}
4921

4922
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
4923
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
4924
        return err
×
4925
}
×
4926

4927
const upsertPreferredChannel = `-- name: UpsertPreferredChannel :exec
4928
/* ─────────────────────────────────────────────
4929
   graph_preferred_channels table queries
4930
   ─────────────────────────────────────────────
4931
*/
4932

4933
INSERT INTO graph_preferred_channels (scid, channel_id)
4934
SELECT c.scid, c.id
4935
FROM graph_channels c
4936
WHERE c.scid = $1
4937
ORDER BY
4938
    CASE WHEN EXISTS (
4939
        SELECT 1 FROM graph_channel_policies p
4940
        WHERE p.channel_id = c.id AND p.version = c.version
4941
    ) THEN 0 ELSE 1 END,
4942
    c.version DESC
4943
LIMIT 1
4944
ON CONFLICT (scid) DO UPDATE SET channel_id = EXCLUDED.channel_id
4945
`
4946

4947
// Recompute the preferred channel for a given SCID and upsert the result.
4948
// Priority: v2 with policies > v1 with policies > v2 bare > v1 bare.
NEW
4949
func (q *Queries) UpsertPreferredChannel(ctx context.Context, scid []byte) error {
×
NEW
4950
        _, err := q.db.ExecContext(ctx, upsertPreferredChannel, scid)
×
NEW
4951
        return err
×
NEW
4952
}
×
4953

4954
const upsertPreferredNode = `-- name: UpsertPreferredNode :exec
4955
/* ─────────────────────────────────────────────
4956
   graph_preferred_nodes table queries
4957
   ─────────────────────────────────────────────
4958
*/
4959

4960
INSERT INTO graph_preferred_nodes (pub_key, node_id)
4961
SELECT n.pub_key, n.id
4962
FROM graph_nodes n
4963
WHERE n.pub_key = $1
4964
ORDER BY
4965
    CASE WHEN COALESCE(length(n.signature), 0) > 0
4966
         THEN 0 ELSE 1 END,
4967
    n.version DESC
4968
LIMIT 1
4969
ON CONFLICT (pub_key) DO UPDATE SET node_id = EXCLUDED.node_id
4970
`
4971

4972
// Recompute the preferred node for a given pub_key and upsert the result.
4973
// Priority: v2 announced > v1 announced > v2 shell > v1 shell.
NEW
4974
func (q *Queries) UpsertPreferredNode(ctx context.Context, pubKey []byte) error {
×
NEW
4975
        _, err := q.db.ExecContext(ctx, upsertPreferredNode, pubKey)
×
NEW
4976
        return err
×
NEW
4977
}
×
4978

4979
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
4980
/* ───────────────────────────���─────────────────
4981
    graph_prune_log table queries
4982
    ─────────────────────────────────────────────
4983
*/
4984

4985
INSERT INTO graph_prune_log (
4986
    block_height, block_hash
4987
) VALUES (
4988
    $1, $2
4989
)
4990
ON CONFLICT(block_height) DO UPDATE SET
4991
    block_hash = EXCLUDED.block_hash
4992
`
4993

4994
type UpsertPruneLogEntryParams struct {
4995
        BlockHeight int64
4996
        BlockHash   []byte
4997
}
4998

4999
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
5000
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
5001
        return err
×
5002
}
×
5003

5004
const upsertSourceNode = `-- name: UpsertSourceNode :one
5005
INSERT INTO graph_nodes (
5006
    version, pub_key, alias, last_update, block_height, color, signature
5007
) VALUES (
5008
    $1, $2, $3, $4, $5, $6, $7
5009
)
5010
ON CONFLICT (pub_key, version)
5011
    -- Update the following fields if a conflict occurs on pub_key
5012
    -- and version.
5013
    DO UPDATE SET
5014
        alias = EXCLUDED.alias,
5015
        last_update = EXCLUDED.last_update,
5016
        block_height = EXCLUDED.block_height,
5017
        color = EXCLUDED.color,
5018
        signature = EXCLUDED.signature
5019
WHERE graph_nodes.last_update IS NULL
5020
    OR EXCLUDED.last_update >= graph_nodes.last_update
5021
AND (graph_nodes.block_height IS NULL
5022
   OR EXCLUDED.block_height >= graph_nodes.block_height)
5023
RETURNING id
5024
`
5025

5026
type UpsertSourceNodeParams struct {
5027
        Version     int16
5028
        PubKey      []byte
5029
        Alias       sql.NullString
5030
        LastUpdate  sql.NullInt64
5031
        BlockHeight sql.NullInt64
5032
        Color       sql.NullString
5033
        Signature   []byte
5034
}
5035

5036
// We use a separate upsert for our own node since we want to be less strict
5037
// about the last_update field. For our own node, we always want to
5038
// update the record even if the last_update is the same as what we have.
5039
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
5040
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
5041
                arg.Version,
×
5042
                arg.PubKey,
×
5043
                arg.Alias,
×
5044
                arg.LastUpdate,
×
5045
                arg.BlockHeight,
×
5046
                arg.Color,
×
5047
                arg.Signature,
×
5048
        )
×
5049
        var id int64
×
5050
        err := row.Scan(&id)
×
5051
        return id, err
×
5052
}
×
5053

5054
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
5055
/* ─────────────────────────────────────────────
5056
   graph_zombie_channels table queries
5057
   ─────────────────────────────────────────────
5058
*/
5059

5060
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
5061
VALUES ($1, $2, $3, $4)
5062
ON CONFLICT (scid, version)
5063
DO UPDATE SET
5064
    -- If a conflict exists for the SCID and version pair, then we
5065
    -- update the node keys.
5066
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
5067
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
5068
`
5069

5070
type UpsertZombieChannelParams struct {
5071
        Scid     []byte
5072
        Version  int16
5073
        NodeKey1 []byte
5074
        NodeKey2 []byte
5075
}
5076

5077
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
5078
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
5079
                arg.Scid,
×
5080
                arg.Version,
×
5081
                arg.NodeKey1,
×
5082
                arg.NodeKey2,
×
5083
        )
×
5084
        return err
×
5085
}
×
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

© 2026 Coveralls, Inc