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

lightningnetwork / lnd / 22618499859

03 Mar 2026 10:18AM UTC coverage: 62.018% (-0.2%) from 62.24%
22618499859

Pull #10582

github

web-flow
Merge 7bfabfdf3 into 2aec8f338
Pull Request #10582: [g175] graph/db: add versioned range queries and complete v2 graph query migration

667 of 1806 new or added lines in 24 files covered. (36.93%)

172 existing lines in 28 files now uncovered.

139913 of 225601 relevant lines covered (62.02%)

19551.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 deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
243
DELETE FROM graph_prune_log
244
WHERE block_height >= $1
245
  AND block_height <= $2
246
`
247

248
type DeletePruneLogEntriesInRangeParams struct {
249
        StartHeight int64
250
        EndHeight   int64
251
}
252

253
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
254
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
255
        return err
×
256
}
×
257

258
const deleteUnconnectedNodes = `-- name: DeleteUnconnectedNodes :many
259
DELETE FROM graph_nodes
260
WHERE
261
    -- Ignore any of our source nodes.
262
    NOT EXISTS (
263
        SELECT 1
264
        FROM graph_source_nodes sn
265
        WHERE sn.node_id = graph_nodes.id
266
    )
267
    -- Select all nodes that do not have any channels.
268
    AND NOT EXISTS (
269
        SELECT 1
270
        FROM graph_channels c
271
        WHERE c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id
272
) RETURNING pub_key
273
`
274

275
func (q *Queries) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) {
×
276
        rows, err := q.db.QueryContext(ctx, deleteUnconnectedNodes)
×
277
        if err != nil {
×
278
                return nil, err
×
279
        }
×
280
        defer rows.Close()
×
281
        var items [][]byte
×
282
        for rows.Next() {
×
283
                var pub_key []byte
×
284
                if err := rows.Scan(&pub_key); err != nil {
×
285
                        return nil, err
×
286
                }
×
287
                items = append(items, pub_key)
×
288
        }
289
        if err := rows.Close(); err != nil {
×
290
                return nil, err
×
291
        }
×
292
        if err := rows.Err(); err != nil {
×
293
                return nil, err
×
294
        }
×
295
        return items, nil
×
296
}
297

298
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
299
DELETE FROM graph_zombie_channels
300
WHERE scid = $1
301
AND version = $2
302
`
303

304
type DeleteZombieChannelParams struct {
305
        Scid    []byte
306
        Version int16
307
}
308

309
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
310
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
311
}
×
312

313
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
314
SELECT
315
    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,
316
    n1.pub_key AS node1_pub_key,
317
    n2.pub_key AS node2_pub_key
318
FROM graph_channels c
319
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
320
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
321
WHERE c.scid = $1
322
  AND c.version = $2
323
`
324

325
type GetChannelAndNodesBySCIDParams struct {
326
        Scid    []byte
327
        Version int16
328
}
329

330
type GetChannelAndNodesBySCIDRow struct {
331
        ID                int64
332
        Version           int16
333
        Scid              []byte
334
        NodeID1           int64
335
        NodeID2           int64
336
        Outpoint          string
337
        Capacity          sql.NullInt64
338
        BitcoinKey1       []byte
339
        BitcoinKey2       []byte
340
        Node1Signature    []byte
341
        Node2Signature    []byte
342
        Bitcoin1Signature []byte
343
        Bitcoin2Signature []byte
344
        Signature         []byte
345
        FundingPkScript   []byte
346
        MerkleRootHash    []byte
347
        Node1PubKey       []byte
348
        Node2PubKey       []byte
349
}
350

351
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
352
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
353
        var i GetChannelAndNodesBySCIDRow
×
354
        err := row.Scan(
×
355
                &i.ID,
×
356
                &i.Version,
×
357
                &i.Scid,
×
358
                &i.NodeID1,
×
359
                &i.NodeID2,
×
360
                &i.Outpoint,
×
361
                &i.Capacity,
×
362
                &i.BitcoinKey1,
×
363
                &i.BitcoinKey2,
×
364
                &i.Node1Signature,
×
365
                &i.Node2Signature,
×
366
                &i.Bitcoin1Signature,
×
367
                &i.Bitcoin2Signature,
×
368
                &i.Signature,
×
369
                &i.FundingPkScript,
×
370
                &i.MerkleRootHash,
×
371
                &i.Node1PubKey,
×
372
                &i.Node2PubKey,
×
373
        )
×
374
        return i, err
×
375
}
×
376

377
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
378
SELECT
379
    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,
380

381
    n1.pub_key AS node1_pubkey,
382
    n2.pub_key AS node2_pubkey,
383

384
    -- Node 1 policy
385
    cp1.id AS policy_1_id,
386
    cp1.node_id AS policy_1_node_id,
387
    cp1.version AS policy_1_version,
388
    cp1.timelock AS policy_1_timelock,
389
    cp1.fee_ppm AS policy_1_fee_ppm,
390
    cp1.base_fee_msat AS policy_1_base_fee_msat,
391
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
392
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
393
    cp1.last_update AS policy_1_last_update,
394
    cp1.disabled AS policy_1_disabled,
395
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
396
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
397
    cp1.message_flags AS policy_1_message_flags,
398
    cp1.channel_flags AS policy_1_channel_flags,
399
    cp1.signature AS policy_1_signature,
400
    cp1.block_height AS policy_1_block_height,
401
    cp1.disable_flags AS policy_1_disable_flags,
402

403
    -- Node 2 policy
404
    cp2.id AS policy_2_id,
405
    cp2.node_id AS policy_2_node_id,
406
    cp2.version AS policy_2_version,
407
    cp2.timelock AS policy_2_timelock,
408
    cp2.fee_ppm AS policy_2_fee_ppm,
409
    cp2.base_fee_msat AS policy_2_base_fee_msat,
410
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
411
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
412
    cp2.last_update AS policy_2_last_update,
413
    cp2.disabled AS policy_2_disabled,
414
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
415
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
416
    cp2.message_flags AS policy_2_message_flags,
417
    cp2.channel_flags AS policy_2_channel_flags,
418
    cp2.signature AS policy_2_signature,
419
    cp2.block_height AS policy_2_block_height,
420
    cp2.disable_flags AS policy_2_disable_flags
421
FROM graph_channels c
422
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
423
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
424
    LEFT JOIN graph_channel_policies cp1
425
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
426
    LEFT JOIN graph_channel_policies cp2
427
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
428
WHERE c.outpoint = $1 AND c.version = $2
429
`
430

431
type GetChannelByOutpointWithPoliciesParams struct {
432
        Outpoint string
433
        Version  int16
434
}
435

436
type GetChannelByOutpointWithPoliciesRow struct {
437
        GraphChannel                   GraphChannel
438
        Node1Pubkey                    []byte
439
        Node2Pubkey                    []byte
440
        Policy1ID                      sql.NullInt64
441
        Policy1NodeID                  sql.NullInt64
442
        Policy1Version                 sql.NullInt16
443
        Policy1Timelock                sql.NullInt32
444
        Policy1FeePpm                  sql.NullInt64
445
        Policy1BaseFeeMsat             sql.NullInt64
446
        Policy1MinHtlcMsat             sql.NullInt64
447
        Policy1MaxHtlcMsat             sql.NullInt64
448
        Policy1LastUpdate              sql.NullInt64
449
        Policy1Disabled                sql.NullBool
450
        Policy1InboundBaseFeeMsat      sql.NullInt64
451
        Policy1InboundFeeRateMilliMsat sql.NullInt64
452
        Policy1MessageFlags            sql.NullInt16
453
        Policy1ChannelFlags            sql.NullInt16
454
        Policy1Signature               []byte
455
        Policy1BlockHeight             sql.NullInt64
456
        Policy1DisableFlags            sql.NullInt16
457
        Policy2ID                      sql.NullInt64
458
        Policy2NodeID                  sql.NullInt64
459
        Policy2Version                 sql.NullInt16
460
        Policy2Timelock                sql.NullInt32
461
        Policy2FeePpm                  sql.NullInt64
462
        Policy2BaseFeeMsat             sql.NullInt64
463
        Policy2MinHtlcMsat             sql.NullInt64
464
        Policy2MaxHtlcMsat             sql.NullInt64
465
        Policy2LastUpdate              sql.NullInt64
466
        Policy2Disabled                sql.NullBool
467
        Policy2InboundBaseFeeMsat      sql.NullInt64
468
        Policy2InboundFeeRateMilliMsat sql.NullInt64
469
        Policy2MessageFlags            sql.NullInt16
470
        Policy2ChannelFlags            sql.NullInt16
471
        Policy2Signature               []byte
472
        Policy2BlockHeight             sql.NullInt64
473
        Policy2DisableFlags            sql.NullInt16
474
}
475

476
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
477
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
478
        var i GetChannelByOutpointWithPoliciesRow
×
479
        err := row.Scan(
×
480
                &i.GraphChannel.ID,
×
481
                &i.GraphChannel.Version,
×
482
                &i.GraphChannel.Scid,
×
483
                &i.GraphChannel.NodeID1,
×
484
                &i.GraphChannel.NodeID2,
×
485
                &i.GraphChannel.Outpoint,
×
486
                &i.GraphChannel.Capacity,
×
487
                &i.GraphChannel.BitcoinKey1,
×
488
                &i.GraphChannel.BitcoinKey2,
×
489
                &i.GraphChannel.Node1Signature,
×
490
                &i.GraphChannel.Node2Signature,
×
491
                &i.GraphChannel.Bitcoin1Signature,
×
492
                &i.GraphChannel.Bitcoin2Signature,
×
493
                &i.GraphChannel.Signature,
×
494
                &i.GraphChannel.FundingPkScript,
×
495
                &i.GraphChannel.MerkleRootHash,
×
496
                &i.Node1Pubkey,
×
497
                &i.Node2Pubkey,
×
498
                &i.Policy1ID,
×
499
                &i.Policy1NodeID,
×
500
                &i.Policy1Version,
×
501
                &i.Policy1Timelock,
×
502
                &i.Policy1FeePpm,
×
503
                &i.Policy1BaseFeeMsat,
×
504
                &i.Policy1MinHtlcMsat,
×
505
                &i.Policy1MaxHtlcMsat,
×
506
                &i.Policy1LastUpdate,
×
507
                &i.Policy1Disabled,
×
508
                &i.Policy1InboundBaseFeeMsat,
×
509
                &i.Policy1InboundFeeRateMilliMsat,
×
510
                &i.Policy1MessageFlags,
×
511
                &i.Policy1ChannelFlags,
×
512
                &i.Policy1Signature,
×
513
                &i.Policy1BlockHeight,
×
514
                &i.Policy1DisableFlags,
×
515
                &i.Policy2ID,
×
516
                &i.Policy2NodeID,
×
517
                &i.Policy2Version,
×
518
                &i.Policy2Timelock,
×
519
                &i.Policy2FeePpm,
×
520
                &i.Policy2BaseFeeMsat,
×
521
                &i.Policy2MinHtlcMsat,
×
522
                &i.Policy2MaxHtlcMsat,
×
523
                &i.Policy2LastUpdate,
×
524
                &i.Policy2Disabled,
×
525
                &i.Policy2InboundBaseFeeMsat,
×
526
                &i.Policy2InboundFeeRateMilliMsat,
×
527
                &i.Policy2MessageFlags,
×
528
                &i.Policy2ChannelFlags,
×
529
                &i.Policy2Signature,
×
530
                &i.Policy2BlockHeight,
×
531
                &i.Policy2DisableFlags,
×
532
        )
×
533
        return i, err
×
534
}
×
535

536
const getChannelBySCID = `-- name: GetChannelBySCID :one
537
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
538
WHERE scid = $1 AND version = $2
539
`
540

541
type GetChannelBySCIDParams struct {
542
        Scid    []byte
543
        Version int16
544
}
545

546
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error) {
×
547
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
548
        var i GraphChannel
×
549
        err := row.Scan(
×
550
                &i.ID,
×
551
                &i.Version,
×
552
                &i.Scid,
×
553
                &i.NodeID1,
×
554
                &i.NodeID2,
×
555
                &i.Outpoint,
×
556
                &i.Capacity,
×
557
                &i.BitcoinKey1,
×
558
                &i.BitcoinKey2,
×
559
                &i.Node1Signature,
×
560
                &i.Node2Signature,
×
561
                &i.Bitcoin1Signature,
×
562
                &i.Bitcoin2Signature,
×
563
                &i.Signature,
×
564
                &i.FundingPkScript,
×
565
                &i.MerkleRootHash,
×
566
        )
×
567
        return i, err
×
568
}
×
569

570
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
571
SELECT
572
    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,
573
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
574
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
575

576
    -- Policy 1
577
    cp1.id AS policy1_id,
578
    cp1.node_id AS policy1_node_id,
579
    cp1.version AS policy1_version,
580
    cp1.timelock AS policy1_timelock,
581
    cp1.fee_ppm AS policy1_fee_ppm,
582
    cp1.base_fee_msat AS policy1_base_fee_msat,
583
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
584
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
585
    cp1.last_update AS policy1_last_update,
586
    cp1.disabled AS policy1_disabled,
587
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
588
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
589
    cp1.message_flags AS policy1_message_flags,
590
    cp1.channel_flags AS policy1_channel_flags,
591
    cp1.signature AS policy1_signature,
592
    cp1.block_height AS policy1_block_height,
593
    cp1.disable_flags AS policy1_disable_flags,
594

595
    -- Policy 2
596
    cp2.id AS policy2_id,
597
    cp2.node_id AS policy2_node_id,
598
    cp2.version AS policy2_version,
599
    cp2.timelock AS policy2_timelock,
600
    cp2.fee_ppm AS policy2_fee_ppm,
601
    cp2.base_fee_msat AS policy2_base_fee_msat,
602
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
603
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
604
    cp2.last_update AS policy2_last_update,
605
    cp2.disabled AS policy2_disabled,
606
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
607
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
608
    cp2.message_flags AS policy_2_message_flags,
609
    cp2.channel_flags AS policy_2_channel_flags,
610
    cp2.signature AS policy2_signature,
611
    cp2.block_height AS policy2_block_height,
612
    cp2.disable_flags AS policy2_disable_flags
613

614
FROM graph_channels c
615
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
616
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
617
    LEFT JOIN graph_channel_policies cp1
618
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
619
    LEFT JOIN graph_channel_policies cp2
620
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
621
WHERE c.scid = $1
622
  AND c.version = $2
623
`
624

625
type GetChannelBySCIDWithPoliciesParams struct {
626
        Scid    []byte
627
        Version int16
628
}
629

630
type GetChannelBySCIDWithPoliciesRow struct {
631
        GraphChannel                   GraphChannel
632
        GraphNode                      GraphNode
633
        GraphNode_2                    GraphNode
634
        Policy1ID                      sql.NullInt64
635
        Policy1NodeID                  sql.NullInt64
636
        Policy1Version                 sql.NullInt16
637
        Policy1Timelock                sql.NullInt32
638
        Policy1FeePpm                  sql.NullInt64
639
        Policy1BaseFeeMsat             sql.NullInt64
640
        Policy1MinHtlcMsat             sql.NullInt64
641
        Policy1MaxHtlcMsat             sql.NullInt64
642
        Policy1LastUpdate              sql.NullInt64
643
        Policy1Disabled                sql.NullBool
644
        Policy1InboundBaseFeeMsat      sql.NullInt64
645
        Policy1InboundFeeRateMilliMsat sql.NullInt64
646
        Policy1MessageFlags            sql.NullInt16
647
        Policy1ChannelFlags            sql.NullInt16
648
        Policy1Signature               []byte
649
        Policy1BlockHeight             sql.NullInt64
650
        Policy1DisableFlags            sql.NullInt16
651
        Policy2ID                      sql.NullInt64
652
        Policy2NodeID                  sql.NullInt64
653
        Policy2Version                 sql.NullInt16
654
        Policy2Timelock                sql.NullInt32
655
        Policy2FeePpm                  sql.NullInt64
656
        Policy2BaseFeeMsat             sql.NullInt64
657
        Policy2MinHtlcMsat             sql.NullInt64
658
        Policy2MaxHtlcMsat             sql.NullInt64
659
        Policy2LastUpdate              sql.NullInt64
660
        Policy2Disabled                sql.NullBool
661
        Policy2InboundBaseFeeMsat      sql.NullInt64
662
        Policy2InboundFeeRateMilliMsat sql.NullInt64
663
        Policy2MessageFlags            sql.NullInt16
664
        Policy2ChannelFlags            sql.NullInt16
665
        Policy2Signature               []byte
666
        Policy2BlockHeight             sql.NullInt64
667
        Policy2DisableFlags            sql.NullInt16
668
}
669

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

744
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
745
SELECT
746
    channel_id,
747
    type,
748
    value
749
FROM graph_channel_extra_types
750
WHERE channel_id IN (/*SLICE:chan_ids*/?)
751
ORDER BY channel_id, type
752
`
753

754
func (q *Queries) GetChannelExtrasBatch(ctx context.Context, chanIds []int64) ([]GraphChannelExtraType, error) {
×
755
        query := getChannelExtrasBatch
×
756
        var queryParams []interface{}
×
757
        if len(chanIds) > 0 {
×
758
                for _, v := range chanIds {
×
759
                        queryParams = append(queryParams, v)
×
760
                }
×
761
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
762
        } else {
×
763
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
764
        }
×
765
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
766
        if err != nil {
×
767
                return nil, err
×
768
        }
×
769
        defer rows.Close()
×
770
        var items []GraphChannelExtraType
×
771
        for rows.Next() {
×
772
                var i GraphChannelExtraType
×
773
                if err := rows.Scan(&i.ChannelID, &i.Type, &i.Value); err != nil {
×
774
                        return nil, err
×
775
                }
×
776
                items = append(items, i)
×
777
        }
778
        if err := rows.Close(); err != nil {
×
779
                return nil, err
×
780
        }
×
781
        if err := rows.Err(); err != nil {
×
782
                return nil, err
×
783
        }
×
784
        return items, nil
×
785
}
786

787
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
788
SELECT
789
    channel_id,
790
    feature_bit
791
FROM graph_channel_features
792
WHERE channel_id IN (/*SLICE:chan_ids*/?)
793
ORDER BY channel_id, feature_bit
794
`
795

796
func (q *Queries) GetChannelFeaturesBatch(ctx context.Context, chanIds []int64) ([]GraphChannelFeature, error) {
×
797
        query := getChannelFeaturesBatch
×
798
        var queryParams []interface{}
×
799
        if len(chanIds) > 0 {
×
800
                for _, v := range chanIds {
×
801
                        queryParams = append(queryParams, v)
×
802
                }
×
803
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
804
        } else {
×
805
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
806
        }
×
807
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
808
        if err != nil {
×
809
                return nil, err
×
810
        }
×
811
        defer rows.Close()
×
812
        var items []GraphChannelFeature
×
813
        for rows.Next() {
×
814
                var i GraphChannelFeature
×
815
                if err := rows.Scan(&i.ChannelID, &i.FeatureBit); err != nil {
×
816
                        return nil, err
×
817
                }
×
818
                items = append(items, i)
×
819
        }
820
        if err := rows.Close(); err != nil {
×
821
                return nil, err
×
822
        }
×
823
        if err := rows.Err(); err != nil {
×
824
                return nil, err
×
825
        }
×
826
        return items, nil
×
827
}
828

829
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
830
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
831
FROM graph_channel_policies
832
WHERE channel_id = $1
833
  AND node_id = $2
834
  AND version = $3
835
`
836

837
type GetChannelPolicyByChannelAndNodeParams struct {
838
        ChannelID int64
839
        NodeID    int64
840
        Version   int16
841
}
842

843
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (GraphChannelPolicy, error) {
×
844
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
845
        var i GraphChannelPolicy
×
846
        err := row.Scan(
×
847
                &i.ID,
×
848
                &i.Version,
×
849
                &i.ChannelID,
×
850
                &i.NodeID,
×
851
                &i.Timelock,
×
852
                &i.FeePpm,
×
853
                &i.BaseFeeMsat,
×
854
                &i.MinHtlcMsat,
×
855
                &i.MaxHtlcMsat,
×
856
                &i.LastUpdate,
×
857
                &i.Disabled,
×
858
                &i.InboundBaseFeeMsat,
×
859
                &i.InboundFeeRateMilliMsat,
×
860
                &i.MessageFlags,
×
861
                &i.ChannelFlags,
×
862
                &i.Signature,
×
863
                &i.BlockHeight,
×
864
                &i.DisableFlags,
×
865
        )
×
866
        return i, err
×
867
}
×
868

869
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
870
SELECT
871
    channel_policy_id as policy_id,
872
    type,
873
    value
874
FROM graph_channel_policy_extra_types
875
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
876
ORDER BY channel_policy_id, type
877
`
878

879
type GetChannelPolicyExtraTypesBatchRow struct {
880
        PolicyID int64
881
        Type     int64
882
        Value    []byte
883
}
884

885
func (q *Queries) GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]GetChannelPolicyExtraTypesBatchRow, error) {
×
886
        query := getChannelPolicyExtraTypesBatch
×
887
        var queryParams []interface{}
×
888
        if len(policyIds) > 0 {
×
889
                for _, v := range policyIds {
×
890
                        queryParams = append(queryParams, v)
×
891
                }
×
892
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", makeQueryParams(len(queryParams), len(policyIds)), 1)
×
893
        } else {
×
894
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", "NULL", 1)
×
895
        }
×
896
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
897
        if err != nil {
×
898
                return nil, err
×
899
        }
×
900
        defer rows.Close()
×
901
        var items []GetChannelPolicyExtraTypesBatchRow
×
902
        for rows.Next() {
×
903
                var i GetChannelPolicyExtraTypesBatchRow
×
904
                if err := rows.Scan(&i.PolicyID, &i.Type, &i.Value); err != nil {
×
905
                        return nil, err
×
906
                }
×
907
                items = append(items, i)
×
908
        }
909
        if err := rows.Close(); err != nil {
×
910
                return nil, err
×
911
        }
×
912
        if err := rows.Err(); err != nil {
×
913
                return nil, err
×
914
        }
×
915
        return items, nil
×
916
}
917

918
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
919
SELECT
920
    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,
921

922
    -- Minimal node data.
923
    n1.id AS node1_id,
924
    n1.pub_key AS node1_pub_key,
925
    n2.id AS node2_id,
926
    n2.pub_key AS node2_pub_key,
927

928
    -- Policy 1
929
    cp1.id AS policy1_id,
930
    cp1.node_id AS policy1_node_id,
931
    cp1.version AS policy1_version,
932
    cp1.timelock AS policy1_timelock,
933
    cp1.fee_ppm AS policy1_fee_ppm,
934
    cp1.base_fee_msat AS policy1_base_fee_msat,
935
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
936
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
937
    cp1.last_update AS policy1_last_update,
938
    cp1.disabled AS policy1_disabled,
939
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
940
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
941
    cp1.message_flags AS policy1_message_flags,
942
    cp1.channel_flags AS policy1_channel_flags,
943
    cp1.signature AS policy1_signature,
944
    cp1.block_height AS policy1_block_height,
945
    cp1.disable_flags AS policy1_disable_flags,
946

947
    -- Policy 2
948
    cp2.id AS policy2_id,
949
    cp2.node_id AS policy2_node_id,
950
    cp2.version AS policy2_version,
951
    cp2.timelock AS policy2_timelock,
952
    cp2.fee_ppm AS policy2_fee_ppm,
953
    cp2.base_fee_msat AS policy2_base_fee_msat,
954
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
955
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
956
    cp2.last_update AS policy2_last_update,
957
    cp2.disabled AS policy2_disabled,
958
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
959
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
960
    cp2.message_flags AS policy2_message_flags,
961
    cp2.channel_flags AS policy2_channel_flags,
962
    cp2.signature AS policy2_signature,
963
    cp2.block_height AS policy2_block_height,
964
    cp2.disable_flags AS policy2_disable_flags
965

966
FROM graph_channels c
967
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
968
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
969
    LEFT JOIN graph_channel_policies cp1
970
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
971
    LEFT JOIN graph_channel_policies cp2
972
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
973
WHERE c.id IN (/*SLICE:ids*/?)
974
`
975

976
type GetChannelsByIDsRow struct {
977
        GraphChannel                   GraphChannel
978
        Node1ID                        int64
979
        Node1PubKey                    []byte
980
        Node2ID                        int64
981
        Node2PubKey                    []byte
982
        Policy1ID                      sql.NullInt64
983
        Policy1NodeID                  sql.NullInt64
984
        Policy1Version                 sql.NullInt16
985
        Policy1Timelock                sql.NullInt32
986
        Policy1FeePpm                  sql.NullInt64
987
        Policy1BaseFeeMsat             sql.NullInt64
988
        Policy1MinHtlcMsat             sql.NullInt64
989
        Policy1MaxHtlcMsat             sql.NullInt64
990
        Policy1LastUpdate              sql.NullInt64
991
        Policy1Disabled                sql.NullBool
992
        Policy1InboundBaseFeeMsat      sql.NullInt64
993
        Policy1InboundFeeRateMilliMsat sql.NullInt64
994
        Policy1MessageFlags            sql.NullInt16
995
        Policy1ChannelFlags            sql.NullInt16
996
        Policy1Signature               []byte
997
        Policy1BlockHeight             sql.NullInt64
998
        Policy1DisableFlags            sql.NullInt16
999
        Policy2ID                      sql.NullInt64
1000
        Policy2NodeID                  sql.NullInt64
1001
        Policy2Version                 sql.NullInt16
1002
        Policy2Timelock                sql.NullInt32
1003
        Policy2FeePpm                  sql.NullInt64
1004
        Policy2BaseFeeMsat             sql.NullInt64
1005
        Policy2MinHtlcMsat             sql.NullInt64
1006
        Policy2MaxHtlcMsat             sql.NullInt64
1007
        Policy2LastUpdate              sql.NullInt64
1008
        Policy2Disabled                sql.NullBool
1009
        Policy2InboundBaseFeeMsat      sql.NullInt64
1010
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1011
        Policy2MessageFlags            sql.NullInt16
1012
        Policy2ChannelFlags            sql.NullInt16
1013
        Policy2Signature               []byte
1014
        Policy2BlockHeight             sql.NullInt64
1015
        Policy2DisableFlags            sql.NullInt16
1016
}
1017

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

1106
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1107
SELECT
1108
    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,
1109
    n1.pub_key AS node1_pubkey,
1110
    n2.pub_key AS node2_pubkey
1111
FROM graph_channels c
1112
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1113
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1114
WHERE c.outpoint IN
1115
    (/*SLICE:outpoints*/?)
1116
`
1117

1118
type GetChannelsByOutpointsRow struct {
1119
        GraphChannel GraphChannel
1120
        Node1Pubkey  []byte
1121
        Node2Pubkey  []byte
1122
}
1123

1124
func (q *Queries) GetChannelsByOutpoints(ctx context.Context, outpoints []string) ([]GetChannelsByOutpointsRow, error) {
×
1125
        query := getChannelsByOutpoints
×
1126
        var queryParams []interface{}
×
1127
        if len(outpoints) > 0 {
×
1128
                for _, v := range outpoints {
×
1129
                        queryParams = append(queryParams, v)
×
1130
                }
×
1131
                query = strings.Replace(query, "/*SLICE:outpoints*/?", makeQueryParams(len(queryParams), len(outpoints)), 1)
×
1132
        } else {
×
1133
                query = strings.Replace(query, "/*SLICE:outpoints*/?", "NULL", 1)
×
1134
        }
×
1135
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1136
        if err != nil {
×
1137
                return nil, err
×
1138
        }
×
1139
        defer rows.Close()
×
1140
        var items []GetChannelsByOutpointsRow
×
1141
        for rows.Next() {
×
1142
                var i GetChannelsByOutpointsRow
×
1143
                if err := rows.Scan(
×
1144
                        &i.GraphChannel.ID,
×
1145
                        &i.GraphChannel.Version,
×
1146
                        &i.GraphChannel.Scid,
×
1147
                        &i.GraphChannel.NodeID1,
×
1148
                        &i.GraphChannel.NodeID2,
×
1149
                        &i.GraphChannel.Outpoint,
×
1150
                        &i.GraphChannel.Capacity,
×
1151
                        &i.GraphChannel.BitcoinKey1,
×
1152
                        &i.GraphChannel.BitcoinKey2,
×
1153
                        &i.GraphChannel.Node1Signature,
×
1154
                        &i.GraphChannel.Node2Signature,
×
1155
                        &i.GraphChannel.Bitcoin1Signature,
×
1156
                        &i.GraphChannel.Bitcoin2Signature,
×
1157
                        &i.GraphChannel.Signature,
×
1158
                        &i.GraphChannel.FundingPkScript,
×
1159
                        &i.GraphChannel.MerkleRootHash,
×
1160
                        &i.Node1Pubkey,
×
1161
                        &i.Node2Pubkey,
×
1162
                ); err != nil {
×
1163
                        return nil, err
×
1164
                }
×
1165
                items = append(items, i)
×
1166
        }
1167
        if err := rows.Close(); err != nil {
×
1168
                return nil, err
×
1169
        }
×
1170
        if err := rows.Err(); err != nil {
×
1171
                return nil, err
×
1172
        }
×
1173
        return items, nil
×
1174
}
1175

1176
const getChannelsByPolicyBlockRange = `-- name: GetChannelsByPolicyBlockRange :many
1177
SELECT
1178
    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,
1179
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1180
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1181

1182
    -- Policy 1 (node_id_1)
1183
    cp1.id AS policy1_id,
1184
    cp1.node_id AS policy1_node_id,
1185
    cp1.version AS policy1_version,
1186
    cp1.timelock AS policy1_timelock,
1187
    cp1.fee_ppm AS policy1_fee_ppm,
1188
    cp1.base_fee_msat AS policy1_base_fee_msat,
1189
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1190
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1191
    cp1.last_update AS policy1_last_update,
1192
    cp1.disabled AS policy1_disabled,
1193
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1194
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1195
    cp1.message_flags AS policy1_message_flags,
1196
    cp1.channel_flags AS policy1_channel_flags,
1197
    cp1.signature AS policy1_signature,
1198
    cp1.block_height AS policy1_block_height,
1199
    cp1.disable_flags AS policy1_disable_flags,
1200

1201
    -- Policy 2 (node_id_2)
1202
    cp2.id AS policy2_id,
1203
    cp2.node_id AS policy2_node_id,
1204
    cp2.version AS policy2_version,
1205
    cp2.timelock AS policy2_timelock,
1206
    cp2.fee_ppm AS policy2_fee_ppm,
1207
    cp2.base_fee_msat AS policy2_base_fee_msat,
1208
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1209
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1210
    cp2.last_update AS policy2_last_update,
1211
    cp2.disabled AS policy2_disabled,
1212
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1213
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1214
    cp2.message_flags AS policy2_message_flags,
1215
    cp2.channel_flags AS policy2_channel_flags,
1216
    cp2.signature AS policy2_signature,
1217
    cp2.block_height AS policy2_block_height,
1218
    cp2.disable_flags AS policy2_disable_flags
1219

1220
FROM graph_channels c
1221
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1222
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1223
    LEFT JOIN graph_channel_policies cp1
1224
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1225
    LEFT JOIN graph_channel_policies cp2
1226
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1227
WHERE c.version = $1
1228
  AND (
1229
       (cp1.block_height >= $2 AND cp1.block_height < $3)
1230
       OR
1231
       (cp2.block_height >= $2 AND cp2.block_height < $3)
1232
  )
1233
  -- Pagination using compound cursor (max_block_height, id).
1234
  -- We use COALESCE with -1 as sentinel since heights are always positive.
1235
  AND (
1236
       (CASE
1237
           WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1238
               THEN COALESCE(cp1.block_height, 0)
1239
           ELSE COALESCE(cp2.block_height, 0)
1240
       END > COALESCE($4, -1))
1241
       OR
1242
       (CASE
1243
           WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1244
               THEN COALESCE(cp1.block_height, 0)
1245
           ELSE COALESCE(cp2.block_height, 0)
1246
       END = COALESCE($4, -1)
1247
       AND c.id > COALESCE($5, -1))
1248
  )
1249
ORDER BY
1250
    CASE
1251
        WHEN COALESCE(cp1.block_height, 0) >= COALESCE(cp2.block_height, 0)
1252
            THEN COALESCE(cp1.block_height, 0)
1253
        ELSE COALESCE(cp2.block_height, 0)
1254
    END ASC,
1255
    c.id ASC
1256
LIMIT COALESCE($6, 999999999)
1257
`
1258

1259
type GetChannelsByPolicyBlockRangeParams struct {
1260
        Version         int16
1261
        StartHeight     sql.NullInt64
1262
        EndHeight       sql.NullInt64
1263
        LastBlockHeight sql.NullInt64
1264
        LastID          sql.NullInt64
1265
        MaxResults      interface{}
1266
}
1267

1268
type GetChannelsByPolicyBlockRangeRow struct {
1269
        GraphChannel                   GraphChannel
1270
        GraphNode                      GraphNode
1271
        GraphNode_2                    GraphNode
1272
        Policy1ID                      sql.NullInt64
1273
        Policy1NodeID                  sql.NullInt64
1274
        Policy1Version                 sql.NullInt16
1275
        Policy1Timelock                sql.NullInt32
1276
        Policy1FeePpm                  sql.NullInt64
1277
        Policy1BaseFeeMsat             sql.NullInt64
1278
        Policy1MinHtlcMsat             sql.NullInt64
1279
        Policy1MaxHtlcMsat             sql.NullInt64
1280
        Policy1LastUpdate              sql.NullInt64
1281
        Policy1Disabled                sql.NullBool
1282
        Policy1InboundBaseFeeMsat      sql.NullInt64
1283
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1284
        Policy1MessageFlags            sql.NullInt16
1285
        Policy1ChannelFlags            sql.NullInt16
1286
        Policy1Signature               []byte
1287
        Policy1BlockHeight             sql.NullInt64
1288
        Policy1DisableFlags            sql.NullInt16
1289
        Policy2ID                      sql.NullInt64
1290
        Policy2NodeID                  sql.NullInt64
1291
        Policy2Version                 sql.NullInt16
1292
        Policy2Timelock                sql.NullInt32
1293
        Policy2FeePpm                  sql.NullInt64
1294
        Policy2BaseFeeMsat             sql.NullInt64
1295
        Policy2MinHtlcMsat             sql.NullInt64
1296
        Policy2MaxHtlcMsat             sql.NullInt64
1297
        Policy2LastUpdate              sql.NullInt64
1298
        Policy2Disabled                sql.NullBool
1299
        Policy2InboundBaseFeeMsat      sql.NullInt64
1300
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1301
        Policy2MessageFlags            sql.NullInt16
1302
        Policy2ChannelFlags            sql.NullInt16
1303
        Policy2Signature               []byte
1304
        Policy2BlockHeight             sql.NullInt64
1305
        Policy2DisableFlags            sql.NullInt16
1306
}
1307

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

1405
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1406
SELECT
1407
    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,
1408
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1409
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1410

1411
    -- Policy 1 (node_id_1)
1412
    cp1.id AS policy1_id,
1413
    cp1.node_id AS policy1_node_id,
1414
    cp1.version AS policy1_version,
1415
    cp1.timelock AS policy1_timelock,
1416
    cp1.fee_ppm AS policy1_fee_ppm,
1417
    cp1.base_fee_msat AS policy1_base_fee_msat,
1418
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1419
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1420
    cp1.last_update AS policy1_last_update,
1421
    cp1.disabled AS policy1_disabled,
1422
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1423
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1424
    cp1.message_flags AS policy1_message_flags,
1425
    cp1.channel_flags AS policy1_channel_flags,
1426
    cp1.signature AS policy1_signature,
1427
    cp1.block_height AS policy1_block_height,
1428
    cp1.disable_flags AS policy1_disable_flags,
1429

1430
    -- Policy 2 (node_id_2)
1431
    cp2.id AS policy2_id,
1432
    cp2.node_id AS policy2_node_id,
1433
    cp2.version AS policy2_version,
1434
    cp2.timelock AS policy2_timelock,
1435
    cp2.fee_ppm AS policy2_fee_ppm,
1436
    cp2.base_fee_msat AS policy2_base_fee_msat,
1437
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1438
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1439
    cp2.last_update AS policy2_last_update,
1440
    cp2.disabled AS policy2_disabled,
1441
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1442
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1443
    cp2.message_flags AS policy2_message_flags,
1444
    cp2.channel_flags AS policy2_channel_flags,
1445
    cp2.signature AS policy2_signature,
1446
    cp2.block_height AS policy2_block_height,
1447
    cp2.disable_flags AS policy2_disable_flags
1448

1449
FROM graph_channels c
1450
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1451
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1452
    LEFT JOIN graph_channel_policies cp1
1453
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1454
    LEFT JOIN graph_channel_policies cp2
1455
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1456
WHERE c.version = $1
1457
  AND (
1458
       (cp1.last_update >= $2 AND cp1.last_update < $3)
1459
       OR
1460
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1461
  )
1462
  -- Pagination using compound cursor (max_update_time, id).
1463
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1464
  AND (
1465
       (CASE
1466
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1467
               THEN COALESCE(cp1.last_update, 0)
1468
           ELSE COALESCE(cp2.last_update, 0)
1469
       END > COALESCE($4, -1))
1470
       OR 
1471
       (CASE
1472
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1473
               THEN COALESCE(cp1.last_update, 0)
1474
           ELSE COALESCE(cp2.last_update, 0)
1475
       END = COALESCE($4, -1) 
1476
       AND c.id > COALESCE($5, -1))
1477
  )
1478
ORDER BY
1479
    CASE
1480
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1481
            THEN COALESCE(cp1.last_update, 0)
1482
        ELSE COALESCE(cp2.last_update, 0)
1483
    END ASC,
1484
    c.id ASC
1485
LIMIT COALESCE($6, 999999999)
1486
`
1487

1488
type GetChannelsByPolicyLastUpdateRangeParams struct {
1489
        Version        int16
1490
        StartTime      sql.NullInt64
1491
        EndTime        sql.NullInt64
1492
        LastUpdateTime sql.NullInt64
1493
        LastID         sql.NullInt64
1494
        MaxResults     interface{}
1495
}
1496

1497
type GetChannelsByPolicyLastUpdateRangeRow struct {
1498
        GraphChannel                   GraphChannel
1499
        GraphNode                      GraphNode
1500
        GraphNode_2                    GraphNode
1501
        Policy1ID                      sql.NullInt64
1502
        Policy1NodeID                  sql.NullInt64
1503
        Policy1Version                 sql.NullInt16
1504
        Policy1Timelock                sql.NullInt32
1505
        Policy1FeePpm                  sql.NullInt64
1506
        Policy1BaseFeeMsat             sql.NullInt64
1507
        Policy1MinHtlcMsat             sql.NullInt64
1508
        Policy1MaxHtlcMsat             sql.NullInt64
1509
        Policy1LastUpdate              sql.NullInt64
1510
        Policy1Disabled                sql.NullBool
1511
        Policy1InboundBaseFeeMsat      sql.NullInt64
1512
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1513
        Policy1MessageFlags            sql.NullInt16
1514
        Policy1ChannelFlags            sql.NullInt16
1515
        Policy1Signature               []byte
1516
        Policy1BlockHeight             sql.NullInt64
1517
        Policy1DisableFlags            sql.NullInt16
1518
        Policy2ID                      sql.NullInt64
1519
        Policy2NodeID                  sql.NullInt64
1520
        Policy2Version                 sql.NullInt16
1521
        Policy2Timelock                sql.NullInt32
1522
        Policy2FeePpm                  sql.NullInt64
1523
        Policy2BaseFeeMsat             sql.NullInt64
1524
        Policy2MinHtlcMsat             sql.NullInt64
1525
        Policy2MaxHtlcMsat             sql.NullInt64
1526
        Policy2LastUpdate              sql.NullInt64
1527
        Policy2Disabled                sql.NullBool
1528
        Policy2InboundBaseFeeMsat      sql.NullInt64
1529
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1530
        Policy2MessageFlags            sql.NullInt16
1531
        Policy2ChannelFlags            sql.NullInt16
1532
        Policy2Signature               []byte
1533
        Policy2BlockHeight             sql.NullInt64
1534
        Policy2DisableFlags            sql.NullInt16
1535
}
1536

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

1634
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1635
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,
1636
    n1.pub_key AS node1_pub_key,
1637
    n2.pub_key AS node2_pub_key
1638
FROM graph_channels c
1639
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1640
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1641
WHERE scid >= $1
1642
  AND scid < $2
1643
`
1644

1645
type GetChannelsBySCIDRangeParams struct {
1646
        StartScid []byte
1647
        EndScid   []byte
1648
}
1649

1650
type GetChannelsBySCIDRangeRow struct {
1651
        GraphChannel GraphChannel
1652
        Node1PubKey  []byte
1653
        Node2PubKey  []byte
1654
}
1655

1656
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1657
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1658
        if err != nil {
×
1659
                return nil, err
×
1660
        }
×
1661
        defer rows.Close()
×
1662
        var items []GetChannelsBySCIDRangeRow
×
1663
        for rows.Next() {
×
1664
                var i GetChannelsBySCIDRangeRow
×
1665
                if err := rows.Scan(
×
1666
                        &i.GraphChannel.ID,
×
1667
                        &i.GraphChannel.Version,
×
1668
                        &i.GraphChannel.Scid,
×
1669
                        &i.GraphChannel.NodeID1,
×
1670
                        &i.GraphChannel.NodeID2,
×
1671
                        &i.GraphChannel.Outpoint,
×
1672
                        &i.GraphChannel.Capacity,
×
1673
                        &i.GraphChannel.BitcoinKey1,
×
1674
                        &i.GraphChannel.BitcoinKey2,
×
1675
                        &i.GraphChannel.Node1Signature,
×
1676
                        &i.GraphChannel.Node2Signature,
×
1677
                        &i.GraphChannel.Bitcoin1Signature,
×
1678
                        &i.GraphChannel.Bitcoin2Signature,
×
1679
                        &i.GraphChannel.Signature,
×
1680
                        &i.GraphChannel.FundingPkScript,
×
1681
                        &i.GraphChannel.MerkleRootHash,
×
1682
                        &i.Node1PubKey,
×
1683
                        &i.Node2PubKey,
×
1684
                ); err != nil {
×
1685
                        return nil, err
×
1686
                }
×
1687
                items = append(items, i)
×
1688
        }
1689
        if err := rows.Close(); err != nil {
×
1690
                return nil, err
×
1691
        }
×
1692
        if err := rows.Err(); err != nil {
×
1693
                return nil, err
×
1694
        }
×
1695
        return items, nil
×
1696
}
1697

1698
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1699
SELECT
1700
    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,
1701
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1702
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1703

1704
    -- Policy 1
1705
    cp1.id AS policy1_id,
1706
    cp1.node_id AS policy1_node_id,
1707
    cp1.version AS policy1_version,
1708
    cp1.timelock AS policy1_timelock,
1709
    cp1.fee_ppm AS policy1_fee_ppm,
1710
    cp1.base_fee_msat AS policy1_base_fee_msat,
1711
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1712
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1713
    cp1.last_update AS policy1_last_update,
1714
    cp1.disabled AS policy1_disabled,
1715
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1716
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1717
    cp1.message_flags AS policy1_message_flags,
1718
    cp1.channel_flags AS policy1_channel_flags,
1719
    cp1.signature AS policy1_signature,
1720
    cp1.block_height AS policy1_block_height,
1721
    cp1.disable_flags AS policy1_disable_flags,
1722

1723
    -- Policy 2
1724
    cp2.id AS policy2_id,
1725
    cp2.node_id AS policy2_node_id,
1726
    cp2.version AS policy2_version,
1727
    cp2.timelock AS policy2_timelock,
1728
    cp2.fee_ppm AS policy2_fee_ppm,
1729
    cp2.base_fee_msat AS policy2_base_fee_msat,
1730
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1731
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1732
    cp2.last_update AS policy2_last_update,
1733
    cp2.disabled AS policy2_disabled,
1734
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1735
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1736
    cp2.message_flags AS policy_2_message_flags,
1737
    cp2.channel_flags AS policy_2_channel_flags,
1738
    cp2.signature AS policy2_signature,
1739
    cp2.block_height AS policy2_block_height,
1740
    cp2.disable_flags AS policy2_disable_flags
1741

1742
FROM graph_channels c
1743
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1744
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1745
    LEFT JOIN graph_channel_policies cp1
1746
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1747
    LEFT JOIN graph_channel_policies cp2
1748
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1749
WHERE
1750
    c.version = $1
1751
  AND c.scid IN (/*SLICE:scids*/?)
1752
`
1753

1754
type GetChannelsBySCIDWithPoliciesParams struct {
1755
        Version int16
1756
        Scids   [][]byte
1757
}
1758

1759
type GetChannelsBySCIDWithPoliciesRow struct {
1760
        GraphChannel                   GraphChannel
1761
        GraphNode                      GraphNode
1762
        GraphNode_2                    GraphNode
1763
        Policy1ID                      sql.NullInt64
1764
        Policy1NodeID                  sql.NullInt64
1765
        Policy1Version                 sql.NullInt16
1766
        Policy1Timelock                sql.NullInt32
1767
        Policy1FeePpm                  sql.NullInt64
1768
        Policy1BaseFeeMsat             sql.NullInt64
1769
        Policy1MinHtlcMsat             sql.NullInt64
1770
        Policy1MaxHtlcMsat             sql.NullInt64
1771
        Policy1LastUpdate              sql.NullInt64
1772
        Policy1Disabled                sql.NullBool
1773
        Policy1InboundBaseFeeMsat      sql.NullInt64
1774
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1775
        Policy1MessageFlags            sql.NullInt16
1776
        Policy1ChannelFlags            sql.NullInt16
1777
        Policy1Signature               []byte
1778
        Policy1BlockHeight             sql.NullInt64
1779
        Policy1DisableFlags            sql.NullInt16
1780
        Policy2ID                      sql.NullInt64
1781
        Policy2NodeID                  sql.NullInt64
1782
        Policy2Version                 sql.NullInt16
1783
        Policy2Timelock                sql.NullInt32
1784
        Policy2FeePpm                  sql.NullInt64
1785
        Policy2BaseFeeMsat             sql.NullInt64
1786
        Policy2MinHtlcMsat             sql.NullInt64
1787
        Policy2MaxHtlcMsat             sql.NullInt64
1788
        Policy2LastUpdate              sql.NullInt64
1789
        Policy2Disabled                sql.NullBool
1790
        Policy2InboundBaseFeeMsat      sql.NullInt64
1791
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1792
        Policy2MessageFlags            sql.NullInt16
1793
        Policy2ChannelFlags            sql.NullInt16
1794
        Policy2Signature               []byte
1795
        Policy2BlockHeight             sql.NullInt64
1796
        Policy2DisableFlags            sql.NullInt16
1797
}
1798

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

1900
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1901
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
1902
WHERE version = $1
1903
  AND scid IN (/*SLICE:scids*/?)
1904
`
1905

1906
type GetChannelsBySCIDsParams struct {
1907
        Version int16
1908
        Scids   [][]byte
1909
}
1910

1911
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
1912
        query := getChannelsBySCIDs
×
1913
        var queryParams []interface{}
×
1914
        queryParams = append(queryParams, arg.Version)
×
1915
        if len(arg.Scids) > 0 {
×
1916
                for _, v := range arg.Scids {
×
1917
                        queryParams = append(queryParams, v)
×
1918
                }
×
1919
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1920
        } else {
×
1921
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1922
        }
×
1923
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1924
        if err != nil {
×
1925
                return nil, err
×
1926
        }
×
1927
        defer rows.Close()
×
1928
        var items []GraphChannel
×
1929
        for rows.Next() {
×
1930
                var i GraphChannel
×
1931
                if err := rows.Scan(
×
1932
                        &i.ID,
×
1933
                        &i.Version,
×
1934
                        &i.Scid,
×
1935
                        &i.NodeID1,
×
1936
                        &i.NodeID2,
×
1937
                        &i.Outpoint,
×
1938
                        &i.Capacity,
×
1939
                        &i.BitcoinKey1,
×
1940
                        &i.BitcoinKey2,
×
1941
                        &i.Node1Signature,
×
1942
                        &i.Node2Signature,
×
1943
                        &i.Bitcoin1Signature,
×
1944
                        &i.Bitcoin2Signature,
×
1945
                        &i.Signature,
×
1946
                        &i.FundingPkScript,
×
1947
                        &i.MerkleRootHash,
×
1948
                ); err != nil {
×
1949
                        return nil, err
×
1950
                }
×
1951
                items = append(items, i)
×
1952
        }
1953
        if err := rows.Close(); err != nil {
×
1954
                return nil, err
×
1955
        }
×
1956
        if err := rows.Err(); err != nil {
×
1957
                return nil, err
×
1958
        }
×
1959
        return items, nil
×
1960
}
1961

1962
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1963
SELECT scid
1964
FROM graph_closed_scids
1965
WHERE scid IN (/*SLICE:scids*/?)
1966
`
1967

1968
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
1969
        query := getClosedChannelsSCIDs
×
1970
        var queryParams []interface{}
×
1971
        if len(scids) > 0 {
×
1972
                for _, v := range scids {
×
1973
                        queryParams = append(queryParams, v)
×
1974
                }
×
1975
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
1976
        } else {
×
1977
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1978
        }
×
1979
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1980
        if err != nil {
×
1981
                return nil, err
×
1982
        }
×
1983
        defer rows.Close()
×
1984
        var items [][]byte
×
1985
        for rows.Next() {
×
1986
                var scid []byte
×
1987
                if err := rows.Scan(&scid); err != nil {
×
1988
                        return nil, err
×
1989
                }
×
1990
                items = append(items, scid)
×
1991
        }
1992
        if err := rows.Close(); err != nil {
×
1993
                return nil, err
×
1994
        }
×
1995
        if err := rows.Err(); err != nil {
×
1996
                return nil, err
×
1997
        }
×
1998
        return items, nil
×
1999
}
2000

2001
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
2002
SELECT node_id, type, value
2003
FROM graph_node_extra_types
2004
WHERE node_id = $1
2005
`
2006

2007
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
2008
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
2009
        if err != nil {
×
2010
                return nil, err
×
2011
        }
×
2012
        defer rows.Close()
×
2013
        var items []GraphNodeExtraType
×
2014
        for rows.Next() {
×
2015
                var i GraphNodeExtraType
×
2016
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
2017
                        return nil, err
×
2018
                }
×
2019
                items = append(items, i)
×
2020
        }
2021
        if err := rows.Close(); err != nil {
×
2022
                return nil, err
×
2023
        }
×
2024
        if err := rows.Err(); err != nil {
×
2025
                return nil, err
×
2026
        }
×
2027
        return items, nil
×
2028
}
2029

2030
const getNodeAddresses = `-- name: GetNodeAddresses :many
2031
SELECT type, address
2032
FROM graph_node_addresses
2033
WHERE node_id = $1
2034
ORDER BY type ASC, position ASC
2035
`
2036

2037
type GetNodeAddressesRow struct {
2038
        Type    int16
2039
        Address string
2040
}
2041

2042
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
2043
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
2044
        if err != nil {
×
2045
                return nil, err
×
2046
        }
×
2047
        defer rows.Close()
×
2048
        var items []GetNodeAddressesRow
×
2049
        for rows.Next() {
×
2050
                var i GetNodeAddressesRow
×
2051
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
2052
                        return nil, err
×
2053
                }
×
2054
                items = append(items, i)
×
2055
        }
2056
        if err := rows.Close(); err != nil {
×
2057
                return nil, err
×
2058
        }
×
2059
        if err := rows.Err(); err != nil {
×
2060
                return nil, err
×
2061
        }
×
2062
        return items, nil
×
2063
}
2064

2065
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
2066
SELECT node_id, type, position, address
2067
FROM graph_node_addresses
2068
WHERE node_id IN (/*SLICE:ids*/?)
2069
ORDER BY node_id, type, position
2070
`
2071

2072
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
×
2073
        query := getNodeAddressesBatch
×
2074
        var queryParams []interface{}
×
2075
        if len(ids) > 0 {
×
2076
                for _, v := range ids {
×
2077
                        queryParams = append(queryParams, v)
×
2078
                }
×
2079
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2080
        } else {
×
2081
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2082
        }
×
2083
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2084
        if err != nil {
×
2085
                return nil, err
×
2086
        }
×
2087
        defer rows.Close()
×
2088
        var items []GraphNodeAddress
×
2089
        for rows.Next() {
×
2090
                var i GraphNodeAddress
×
2091
                if err := rows.Scan(
×
2092
                        &i.NodeID,
×
2093
                        &i.Type,
×
2094
                        &i.Position,
×
2095
                        &i.Address,
×
2096
                ); err != nil {
×
2097
                        return nil, err
×
2098
                }
×
2099
                items = append(items, i)
×
2100
        }
2101
        if err := rows.Close(); err != nil {
×
2102
                return nil, err
×
2103
        }
×
2104
        if err := rows.Err(); err != nil {
×
2105
                return nil, err
×
2106
        }
×
2107
        return items, nil
×
2108
}
2109

2110
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
2111
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2112
FROM graph_nodes
2113
WHERE pub_key = $1
2114
  AND version = $2
2115
`
2116

2117
type GetNodeByPubKeyParams struct {
2118
        PubKey  []byte
2119
        Version int16
2120
}
2121

2122
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
2123
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
2124
        var i GraphNode
×
2125
        err := row.Scan(
×
2126
                &i.ID,
×
2127
                &i.Version,
×
2128
                &i.PubKey,
×
2129
                &i.Alias,
×
2130
                &i.LastUpdate,
×
2131
                &i.Color,
×
2132
                &i.Signature,
×
2133
                &i.BlockHeight,
×
2134
        )
×
2135
        return i, err
×
2136
}
×
2137

2138
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
2139
SELECT node_id, type, value
2140
FROM graph_node_extra_types
2141
WHERE node_id IN (/*SLICE:ids*/?)
2142
ORDER BY node_id, type
2143
`
2144

2145
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
2146
        query := getNodeExtraTypesBatch
×
2147
        var queryParams []interface{}
×
2148
        if len(ids) > 0 {
×
2149
                for _, v := range ids {
×
2150
                        queryParams = append(queryParams, v)
×
2151
                }
×
2152
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2153
        } else {
×
2154
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2155
        }
×
2156
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2157
        if err != nil {
×
2158
                return nil, err
×
2159
        }
×
2160
        defer rows.Close()
×
2161
        var items []GraphNodeExtraType
×
2162
        for rows.Next() {
×
2163
                var i GraphNodeExtraType
×
2164
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
2165
                        return nil, err
×
2166
                }
×
2167
                items = append(items, i)
×
2168
        }
2169
        if err := rows.Close(); err != nil {
×
2170
                return nil, err
×
2171
        }
×
2172
        if err := rows.Err(); err != nil {
×
2173
                return nil, err
×
2174
        }
×
2175
        return items, nil
×
2176
}
2177

2178
const getNodeFeatures = `-- name: GetNodeFeatures :many
2179
SELECT node_id, feature_bit
2180
FROM graph_node_features
2181
WHERE node_id = $1
2182
`
2183

2184
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
2185
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
2186
        if err != nil {
×
2187
                return nil, err
×
2188
        }
×
2189
        defer rows.Close()
×
2190
        var items []GraphNodeFeature
×
2191
        for rows.Next() {
×
2192
                var i GraphNodeFeature
×
2193
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
2194
                        return nil, err
×
2195
                }
×
2196
                items = append(items, i)
×
2197
        }
2198
        if err := rows.Close(); err != nil {
×
2199
                return nil, err
×
2200
        }
×
2201
        if err := rows.Err(); err != nil {
×
2202
                return nil, err
×
2203
        }
×
2204
        return items, nil
×
2205
}
2206

2207
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
2208
SELECT node_id, feature_bit
2209
FROM graph_node_features
2210
WHERE node_id IN (/*SLICE:ids*/?)
2211
ORDER BY node_id, feature_bit
2212
`
2213

2214
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
×
2215
        query := getNodeFeaturesBatch
×
2216
        var queryParams []interface{}
×
2217
        if len(ids) > 0 {
×
2218
                for _, v := range ids {
×
2219
                        queryParams = append(queryParams, v)
×
2220
                }
×
2221
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2222
        } else {
×
2223
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2224
        }
×
2225
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2226
        if err != nil {
×
2227
                return nil, err
×
2228
        }
×
2229
        defer rows.Close()
×
2230
        var items []GraphNodeFeature
×
2231
        for rows.Next() {
×
2232
                var i GraphNodeFeature
×
2233
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
2234
                        return nil, err
×
2235
                }
×
2236
                items = append(items, i)
×
2237
        }
2238
        if err := rows.Close(); err != nil {
×
2239
                return nil, err
×
2240
        }
×
2241
        if err := rows.Err(); err != nil {
×
2242
                return nil, err
×
2243
        }
×
2244
        return items, nil
×
2245
}
2246

2247
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
2248
SELECT f.feature_bit
2249
FROM graph_nodes n
2250
    JOIN graph_node_features f ON f.node_id = n.id
2251
WHERE n.pub_key = $1
2252
  AND n.version = $2
2253
`
2254

2255
type GetNodeFeaturesByPubKeyParams struct {
2256
        PubKey  []byte
2257
        Version int16
2258
}
2259

2260
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
2261
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
2262
        if err != nil {
×
2263
                return nil, err
×
2264
        }
×
2265
        defer rows.Close()
×
2266
        var items []int32
×
2267
        for rows.Next() {
×
2268
                var feature_bit int32
×
2269
                if err := rows.Scan(&feature_bit); err != nil {
×
2270
                        return nil, err
×
2271
                }
×
2272
                items = append(items, feature_bit)
×
2273
        }
2274
        if err := rows.Close(); err != nil {
×
2275
                return nil, err
×
2276
        }
×
2277
        if err := rows.Err(); err != nil {
×
2278
                return nil, err
×
2279
        }
×
2280
        return items, nil
×
2281
}
2282

2283
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
2284
SELECT id
2285
FROM graph_nodes
2286
WHERE pub_key = $1
2287
  AND version = $2
2288
`
2289

2290
type GetNodeIDByPubKeyParams struct {
2291
        PubKey  []byte
2292
        Version int16
2293
}
2294

2295
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
2296
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
2297
        var id int64
×
2298
        err := row.Scan(&id)
×
2299
        return id, err
×
2300
}
×
2301

2302
const getNodesByBlockHeightRange = `-- name: GetNodesByBlockHeightRange :many
2303
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2304
FROM graph_nodes
2305
WHERE graph_nodes.version = $1
2306
  AND block_height >= $2
2307
  AND block_height <= $3
2308
  -- Pagination: We use (block_height, pub_key) as a compound cursor.
2309
  -- This ensures stable ordering and allows us to resume from where we left off.
2310
  -- We use COALESCE with -1 as sentinel since heights are always positive.
2311
  AND (
2312
    -- Include rows with block_height greater than cursor (or all rows if cursor is -1)
2313
    block_height > COALESCE($4, -1)
2314
    OR
2315
    -- For rows with same block_height, use pub_key as tiebreaker
2316
    (block_height = COALESCE($4, -1)
2317
     AND pub_key > $5)
2318
  )
2319
  -- Optional filter for public nodes only
2320
  AND (
2321
    -- If only_public is false or not provided, include all nodes
2322
    COALESCE($6, FALSE) IS FALSE
2323
    OR
2324
    -- For V2 protocol, a node is public if it has at least one public channel.
2325
    -- A public channel has signature set (channel announcement received).
2326
    EXISTS (
2327
      SELECT 1
2328
      FROM graph_channels c
2329
      WHERE c.version = 2
2330
        AND COALESCE(length(c.signature), 0) > 0
2331
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2332
    )
2333
  )
2334
ORDER BY block_height ASC, pub_key ASC
2335
LIMIT COALESCE($7, 999999999)
2336
`
2337

2338
type GetNodesByBlockHeightRangeParams struct {
2339
        Version         int16
2340
        StartHeight     sql.NullInt64
2341
        EndHeight       sql.NullInt64
2342
        LastBlockHeight sql.NullInt64
2343
        LastPubKey      []byte
2344
        OnlyPublic      interface{}
2345
        MaxResults      interface{}
2346
}
2347

NEW
2348
func (q *Queries) GetNodesByBlockHeightRange(ctx context.Context, arg GetNodesByBlockHeightRangeParams) ([]GraphNode, error) {
×
NEW
2349
        rows, err := q.db.QueryContext(ctx, getNodesByBlockHeightRange,
×
NEW
2350
                arg.Version,
×
NEW
2351
                arg.StartHeight,
×
NEW
2352
                arg.EndHeight,
×
NEW
2353
                arg.LastBlockHeight,
×
NEW
2354
                arg.LastPubKey,
×
NEW
2355
                arg.OnlyPublic,
×
NEW
2356
                arg.MaxResults,
×
NEW
2357
        )
×
NEW
2358
        if err != nil {
×
NEW
2359
                return nil, err
×
NEW
2360
        }
×
NEW
2361
        defer rows.Close()
×
NEW
2362
        var items []GraphNode
×
NEW
2363
        for rows.Next() {
×
NEW
2364
                var i GraphNode
×
NEW
2365
                if err := rows.Scan(
×
NEW
2366
                        &i.ID,
×
NEW
2367
                        &i.Version,
×
NEW
2368
                        &i.PubKey,
×
NEW
2369
                        &i.Alias,
×
NEW
2370
                        &i.LastUpdate,
×
NEW
2371
                        &i.Color,
×
NEW
2372
                        &i.Signature,
×
NEW
2373
                        &i.BlockHeight,
×
NEW
2374
                ); err != nil {
×
NEW
2375
                        return nil, err
×
NEW
2376
                }
×
NEW
2377
                items = append(items, i)
×
2378
        }
NEW
2379
        if err := rows.Close(); err != nil {
×
NEW
2380
                return nil, err
×
NEW
2381
        }
×
NEW
2382
        if err := rows.Err(); err != nil {
×
NEW
2383
                return nil, err
×
NEW
2384
        }
×
NEW
2385
        return items, nil
×
2386
}
2387

2388
const getNodesByIDs = `-- name: GetNodesByIDs :many
2389
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2390
FROM graph_nodes
2391
WHERE id IN (/*SLICE:ids*/?)
2392
`
2393

2394
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
2395
        query := getNodesByIDs
×
2396
        var queryParams []interface{}
×
2397
        if len(ids) > 0 {
×
2398
                for _, v := range ids {
×
2399
                        queryParams = append(queryParams, v)
×
2400
                }
×
2401
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2402
        } else {
×
2403
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2404
        }
×
2405
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2406
        if err != nil {
×
2407
                return nil, err
×
2408
        }
×
2409
        defer rows.Close()
×
2410
        var items []GraphNode
×
2411
        for rows.Next() {
×
2412
                var i GraphNode
×
2413
                if err := rows.Scan(
×
2414
                        &i.ID,
×
2415
                        &i.Version,
×
2416
                        &i.PubKey,
×
2417
                        &i.Alias,
×
2418
                        &i.LastUpdate,
×
2419
                        &i.Color,
×
2420
                        &i.Signature,
×
2421
                        &i.BlockHeight,
×
2422
                ); err != nil {
×
2423
                        return nil, err
×
2424
                }
×
2425
                items = append(items, i)
×
2426
        }
2427
        if err := rows.Close(); err != nil {
×
2428
                return nil, err
×
2429
        }
×
2430
        if err := rows.Err(); err != nil {
×
2431
                return nil, err
×
2432
        }
×
2433
        return items, nil
×
2434
}
2435

2436
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
2437
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2438
FROM graph_nodes
2439
WHERE graph_nodes.version = $1
2440
  AND last_update >= $2
2441
  AND last_update <= $3
2442
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2443
  -- This ensures stable ordering and allows us to resume from where we left off.
2444
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2445
  AND (
2446
    -- Include rows with last_update greater than cursor (or all rows if cursor is -1)
2447
    last_update > COALESCE($4, -1)
2448
    OR 
2449
    -- For rows with same last_update, use pub_key as tiebreaker
2450
    (last_update = COALESCE($4, -1) 
2451
     AND pub_key > $5)
2452
  )
2453
  -- Optional filter for public nodes only
2454
  AND (
2455
    -- If only_public is false or not provided, include all nodes
2456
    COALESCE($6, FALSE) IS FALSE
2457
    OR 
2458
    -- For V1 protocol, a node is public if it has at least one public channel.
2459
    -- A public channel has bitcoin_1_signature set (channel announcement received).
2460
    EXISTS (
2461
      SELECT 1
2462
      FROM graph_channels c
2463
      WHERE c.version = 1
2464
        AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2465
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2466
    )
2467
  )
2468
ORDER BY last_update ASC, pub_key ASC
2469
LIMIT COALESCE($7, 999999999)
2470
`
2471

2472
type GetNodesByLastUpdateRangeParams struct {
2473
        Version    int16
2474
        StartTime  sql.NullInt64
2475
        EndTime    sql.NullInt64
2476
        LastUpdate sql.NullInt64
2477
        LastPubKey []byte
2478
        OnlyPublic interface{}
2479
        MaxResults interface{}
2480
}
2481

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

2522
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2523
SELECT block_height, block_hash
2524
FROM graph_prune_log
2525
WHERE block_height
2526
   IN (/*SLICE:heights*/?)
2527
`
2528

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

2562
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2563
SELECT block_hash
2564
FROM graph_prune_log
2565
WHERE block_height = $1
2566
`
2567

2568
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2569
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2570
        var block_hash []byte
×
2571
        err := row.Scan(&block_hash)
×
2572
        return block_hash, err
×
2573
}
×
2574

2575
const getPruneTip = `-- name: GetPruneTip :one
2576
SELECT block_height, block_hash
2577
FROM graph_prune_log
2578
ORDER BY block_height DESC
2579
LIMIT 1
2580
`
2581

2582
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2583
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2584
        var i GraphPruneLog
×
2585
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2586
        return i, err
×
2587
}
×
2588

2589
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2590
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
2591
FROM graph_channels
2592
WHERE version = 1
2593
  AND COALESCE(length(node_1_signature), 0) > 0
2594
  AND scid >= $1
2595
  AND scid < $2
2596
ORDER BY scid ASC
2597
`
2598

2599
type GetPublicV1ChannelsBySCIDParams struct {
2600
        StartScid []byte
2601
        EndScid   []byte
2602
}
2603

2604
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2605
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2606
        if err != nil {
×
2607
                return nil, err
×
2608
        }
×
2609
        defer rows.Close()
×
2610
        var items []GraphChannel
×
2611
        for rows.Next() {
×
2612
                var i GraphChannel
×
2613
                if err := rows.Scan(
×
2614
                        &i.ID,
×
2615
                        &i.Version,
×
2616
                        &i.Scid,
×
2617
                        &i.NodeID1,
×
2618
                        &i.NodeID2,
×
2619
                        &i.Outpoint,
×
2620
                        &i.Capacity,
×
2621
                        &i.BitcoinKey1,
×
2622
                        &i.BitcoinKey2,
×
2623
                        &i.Node1Signature,
×
2624
                        &i.Node2Signature,
×
2625
                        &i.Bitcoin1Signature,
×
2626
                        &i.Bitcoin2Signature,
×
2627
                        &i.Signature,
×
2628
                        &i.FundingPkScript,
×
2629
                        &i.MerkleRootHash,
×
2630
                ); err != nil {
×
2631
                        return nil, err
×
2632
                }
×
2633
                items = append(items, i)
×
2634
        }
2635
        if err := rows.Close(); err != nil {
×
2636
                return nil, err
×
2637
        }
×
2638
        if err := rows.Err(); err != nil {
×
2639
                return nil, err
×
2640
        }
×
2641
        return items, nil
×
2642
}
2643

2644
const getPublicV2ChannelsBySCID = `-- name: GetPublicV2ChannelsBySCID :many
2645
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
2646
FROM graph_channels
2647
WHERE version = 2
2648
  AND COALESCE(length(signature), 0) > 0
2649
  AND scid >= $1
2650
  AND scid < $2
2651
ORDER BY scid ASC
2652
`
2653

2654
type GetPublicV2ChannelsBySCIDParams struct {
2655
        StartScid []byte
2656
        EndScid   []byte
2657
}
2658

NEW
2659
func (q *Queries) GetPublicV2ChannelsBySCID(ctx context.Context, arg GetPublicV2ChannelsBySCIDParams) ([]GraphChannel, error) {
×
NEW
2660
        rows, err := q.db.QueryContext(ctx, getPublicV2ChannelsBySCID, arg.StartScid, arg.EndScid)
×
NEW
2661
        if err != nil {
×
NEW
2662
                return nil, err
×
NEW
2663
        }
×
NEW
2664
        defer rows.Close()
×
NEW
2665
        var items []GraphChannel
×
NEW
2666
        for rows.Next() {
×
NEW
2667
                var i GraphChannel
×
NEW
2668
                if err := rows.Scan(
×
NEW
2669
                        &i.ID,
×
NEW
2670
                        &i.Version,
×
NEW
2671
                        &i.Scid,
×
NEW
2672
                        &i.NodeID1,
×
NEW
2673
                        &i.NodeID2,
×
NEW
2674
                        &i.Outpoint,
×
NEW
2675
                        &i.Capacity,
×
NEW
2676
                        &i.BitcoinKey1,
×
NEW
2677
                        &i.BitcoinKey2,
×
NEW
2678
                        &i.Node1Signature,
×
NEW
2679
                        &i.Node2Signature,
×
NEW
2680
                        &i.Bitcoin1Signature,
×
NEW
2681
                        &i.Bitcoin2Signature,
×
NEW
2682
                        &i.Signature,
×
NEW
2683
                        &i.FundingPkScript,
×
NEW
2684
                        &i.MerkleRootHash,
×
NEW
2685
                ); err != nil {
×
NEW
2686
                        return nil, err
×
NEW
2687
                }
×
NEW
2688
                items = append(items, i)
×
2689
        }
NEW
2690
        if err := rows.Close(); err != nil {
×
NEW
2691
                return nil, err
×
NEW
2692
        }
×
NEW
2693
        if err := rows.Err(); err != nil {
×
NEW
2694
                return nil, err
×
NEW
2695
        }
×
NEW
2696
        return items, nil
×
2697
}
2698

2699
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2700
SELECT scid from graph_channels
2701
WHERE outpoint = $1 AND version = $2
2702
`
2703

2704
type GetSCIDByOutpointParams struct {
2705
        Outpoint string
2706
        Version  int16
2707
}
2708

2709
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2710
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2711
        var scid []byte
×
2712
        err := row.Scan(&scid)
×
2713
        return scid, err
×
2714
}
×
2715

2716
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2717
SELECT sn.node_id, n.pub_key
2718
FROM graph_source_nodes sn
2719
    JOIN graph_nodes n ON sn.node_id = n.id
2720
WHERE n.version = $1
2721
`
2722

2723
type GetSourceNodesByVersionRow struct {
2724
        NodeID int64
2725
        PubKey []byte
2726
}
2727

2728
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2729
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2730
        if err != nil {
×
2731
                return nil, err
×
2732
        }
×
2733
        defer rows.Close()
×
2734
        var items []GetSourceNodesByVersionRow
×
2735
        for rows.Next() {
×
2736
                var i GetSourceNodesByVersionRow
×
2737
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
2738
                        return nil, err
×
2739
                }
×
2740
                items = append(items, i)
×
2741
        }
2742
        if err := rows.Close(); err != nil {
×
2743
                return nil, err
×
2744
        }
×
2745
        if err := rows.Err(); err != nil {
×
2746
                return nil, err
×
2747
        }
×
2748
        return items, nil
×
2749
}
2750

2751
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2752
SELECT c.scid
2753
FROM graph_channels c
2754
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2755
WHERE cp.disabled = true
2756
AND c.version = 1
2757
GROUP BY c.scid
2758
HAVING COUNT(*) > 1
2759
`
2760

2761
// NOTE: this is V1 specific since for V1, disabled is a
2762
// simple, single boolean. The proposed V2 policy
2763
// structure will have a more complex disabled bit vector
2764
// and so the query for V2 may differ.
2765
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2766
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2767
        if err != nil {
×
2768
                return nil, err
×
2769
        }
×
2770
        defer rows.Close()
×
2771
        var items [][]byte
×
2772
        for rows.Next() {
×
2773
                var scid []byte
×
2774
                if err := rows.Scan(&scid); err != nil {
×
2775
                        return nil, err
×
2776
                }
×
2777
                items = append(items, scid)
×
2778
        }
2779
        if err := rows.Close(); err != nil {
×
2780
                return nil, err
×
2781
        }
×
2782
        if err := rows.Err(); err != nil {
×
2783
                return nil, err
×
2784
        }
×
2785
        return items, nil
×
2786
}
2787

2788
const getV2DisabledSCIDs = `-- name: GetV2DisabledSCIDs :many
2789
SELECT c.scid
2790
FROM graph_channels c
2791
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2792
WHERE COALESCE(cp.disable_flags, 0) != 0
2793
AND c.version = 2
2794
GROUP BY c.scid
2795
HAVING COUNT(*) > 1
2796
`
2797

2798
// NOTE: this is V2 specific since V2 uses a disable flag
2799
// bit vector instead of a single boolean.
2800
func (q *Queries) GetV2DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2801
        rows, err := q.db.QueryContext(ctx, getV2DisabledSCIDs)
×
2802
        if err != nil {
×
2803
                return nil, err
×
2804
        }
×
2805
        defer rows.Close()
×
2806
        var items [][]byte
×
2807
        for rows.Next() {
×
2808
                var scid []byte
×
2809
                if err := rows.Scan(&scid); err != nil {
×
2810
                        return nil, err
×
2811
                }
×
2812
                items = append(items, scid)
×
2813
        }
2814
        if err := rows.Close(); err != nil {
×
2815
                return nil, err
×
2816
        }
×
2817
        if err := rows.Err(); err != nil {
×
2818
                return nil, err
×
2819
        }
×
2820
        return items, nil
×
2821
}
2822

2823
const getZombieChannel = `-- name: GetZombieChannel :one
2824
SELECT scid, version, node_key_1, node_key_2
2825
FROM graph_zombie_channels
2826
WHERE scid = $1
2827
AND version = $2
2828
`
2829

2830
type GetZombieChannelParams struct {
2831
        Scid    []byte
2832
        Version int16
2833
}
2834

2835
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2836
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2837
        var i GraphZombieChannel
×
2838
        err := row.Scan(
×
2839
                &i.Scid,
×
2840
                &i.Version,
×
2841
                &i.NodeKey1,
×
2842
                &i.NodeKey2,
×
2843
        )
×
2844
        return i, err
×
2845
}
×
2846

2847
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2848
SELECT scid, version, node_key_1, node_key_2
2849
FROM graph_zombie_channels
2850
WHERE version = $1
2851
  AND scid IN (/*SLICE:scids*/?)
2852
`
2853

2854
type GetZombieChannelsSCIDsParams struct {
2855
        Version int16
2856
        Scids   [][]byte
2857
}
2858

2859
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2860
        query := getZombieChannelsSCIDs
×
2861
        var queryParams []interface{}
×
2862
        queryParams = append(queryParams, arg.Version)
×
2863
        if len(arg.Scids) > 0 {
×
2864
                for _, v := range arg.Scids {
×
2865
                        queryParams = append(queryParams, v)
×
2866
                }
×
2867
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2868
        } else {
×
2869
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2870
        }
×
2871
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2872
        if err != nil {
×
2873
                return nil, err
×
2874
        }
×
2875
        defer rows.Close()
×
2876
        var items []GraphZombieChannel
×
2877
        for rows.Next() {
×
2878
                var i GraphZombieChannel
×
2879
                if err := rows.Scan(
×
2880
                        &i.Scid,
×
2881
                        &i.Version,
×
2882
                        &i.NodeKey1,
×
2883
                        &i.NodeKey2,
×
2884
                ); err != nil {
×
2885
                        return nil, err
×
2886
                }
×
2887
                items = append(items, i)
×
2888
        }
2889
        if err := rows.Close(); err != nil {
×
2890
                return nil, err
×
2891
        }
×
2892
        if err := rows.Err(); err != nil {
×
2893
                return nil, err
×
2894
        }
×
2895
        return items, nil
×
2896
}
2897

2898
const highestSCID = `-- name: HighestSCID :one
2899
SELECT scid
2900
FROM graph_channels
2901
WHERE version = $1
2902
ORDER BY scid DESC
2903
LIMIT 1
2904
`
2905

2906
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2907
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2908
        var scid []byte
×
2909
        err := row.Scan(&scid)
×
2910
        return scid, err
×
2911
}
×
2912

2913
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2914
/* ─────────────────────────────────────────────
2915
   graph_channel_features table queries
2916
   ─────────────────────────────────────────────
2917
*/
2918

2919
INSERT INTO graph_channel_features (
2920
    channel_id, feature_bit
2921
) VALUES (
2922
    $1, $2
2923
) ON CONFLICT (channel_id, feature_bit)
2924
    -- Do nothing if the channel_id and feature_bit already exist.
2925
    DO NOTHING
2926
`
2927

2928
type InsertChannelFeatureParams struct {
2929
        ChannelID  int64
2930
        FeatureBit int32
2931
}
2932

2933
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2934
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2935
        return err
×
2936
}
×
2937

2938
const insertChannelMig = `-- name: InsertChannelMig :one
2939
INSERT INTO graph_channels (
2940
    version, scid, node_id_1, node_id_2,
2941
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2942
    node_1_signature, node_2_signature, bitcoin_1_signature,
2943
    bitcoin_2_signature
2944
) VALUES (
2945
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2946
) ON CONFLICT (scid, version)
2947
    -- If a conflict occurs, we have already migrated this channel. However, we
2948
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2949
    -- otherwise, the "RETURNING id" part does not work.
2950
    DO UPDATE SET
2951
        node_id_1 = EXCLUDED.node_id_1,
2952
        node_id_2 = EXCLUDED.node_id_2,
2953
        outpoint = EXCLUDED.outpoint,
2954
        capacity = EXCLUDED.capacity,
2955
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2956
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2957
        node_1_signature = EXCLUDED.node_1_signature,
2958
        node_2_signature = EXCLUDED.node_2_signature,
2959
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2960
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2961
RETURNING id
2962
`
2963

2964
type InsertChannelMigParams struct {
2965
        Version           int16
2966
        Scid              []byte
2967
        NodeID1           int64
2968
        NodeID2           int64
2969
        Outpoint          string
2970
        Capacity          sql.NullInt64
2971
        BitcoinKey1       []byte
2972
        BitcoinKey2       []byte
2973
        Node1Signature    []byte
2974
        Node2Signature    []byte
2975
        Bitcoin1Signature []byte
2976
        Bitcoin2Signature []byte
2977
}
2978

2979
// NOTE: This query is only meant to be used by the graph SQL migration since
2980
// for that migration, in order to be retry-safe, we don't want to error out if
2981
// we re-insert the same channel again (which would error if the normal
2982
// CreateChannel query is used because of the uniqueness constraint on the scid
2983
// and version columns).
2984
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
2985
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
2986
                arg.Version,
×
2987
                arg.Scid,
×
2988
                arg.NodeID1,
×
2989
                arg.NodeID2,
×
2990
                arg.Outpoint,
×
2991
                arg.Capacity,
×
2992
                arg.BitcoinKey1,
×
2993
                arg.BitcoinKey2,
×
2994
                arg.Node1Signature,
×
2995
                arg.Node2Signature,
×
2996
                arg.Bitcoin1Signature,
×
2997
                arg.Bitcoin2Signature,
×
2998
        )
×
2999
        var id int64
×
3000
        err := row.Scan(&id)
×
3001
        return id, err
×
3002
}
×
3003

3004
const insertClosedChannel = `-- name: InsertClosedChannel :exec
3005
/* ─────────────────────────────────────────────
3006
   graph_closed_scid table queries
3007
   ────────────────────────────────────────────-
3008
*/
3009

3010
INSERT INTO graph_closed_scids (scid)
3011
VALUES ($1)
3012
ON CONFLICT (scid) DO NOTHING
3013
`
3014

3015
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
3016
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
3017
        return err
×
3018
}
×
3019

3020
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
3021
INSERT INTO graph_channel_policies (
3022
    version, channel_id, node_id, timelock, fee_ppm,
3023
    base_fee_msat, min_htlc_msat, last_update, disabled,
3024
    max_htlc_msat, inbound_base_fee_msat,
3025
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3026
    signature
3027
) VALUES  (
3028
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3029
)
3030
ON CONFLICT (channel_id, node_id, version)
3031
    -- If a conflict occurs, we have already migrated this policy. However, we
3032
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3033
    -- otherwise, the "RETURNING id" part does not work.
3034
    DO UPDATE SET
3035
        timelock = EXCLUDED.timelock,
3036
        fee_ppm = EXCLUDED.fee_ppm,
3037
        base_fee_msat = EXCLUDED.base_fee_msat,
3038
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3039
        last_update = EXCLUDED.last_update,
3040
        disabled = EXCLUDED.disabled,
3041
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3042
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3043
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3044
        message_flags = EXCLUDED.message_flags,
3045
        channel_flags = EXCLUDED.channel_flags,
3046
        signature = EXCLUDED.signature
3047
RETURNING id
3048
`
3049

3050
type InsertEdgePolicyMigParams struct {
3051
        Version                 int16
3052
        ChannelID               int64
3053
        NodeID                  int64
3054
        Timelock                int32
3055
        FeePpm                  int64
3056
        BaseFeeMsat             int64
3057
        MinHtlcMsat             int64
3058
        LastUpdate              sql.NullInt64
3059
        Disabled                sql.NullBool
3060
        MaxHtlcMsat             sql.NullInt64
3061
        InboundBaseFeeMsat      sql.NullInt64
3062
        InboundFeeRateMilliMsat sql.NullInt64
3063
        MessageFlags            sql.NullInt16
3064
        ChannelFlags            sql.NullInt16
3065
        Signature               []byte
3066
}
3067

3068
// NOTE: This query is only meant to be used by the graph SQL migration since
3069
// for that migration, in order to be retry-safe, we don't want to error out if
3070
// we re-insert the same policy (which would error if the normal
3071
// UpsertEdgePolicy query is used because of the constraint in that query that
3072
// requires a policy update to have a newer last_update than the existing one).
3073
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
3074
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
3075
                arg.Version,
×
3076
                arg.ChannelID,
×
3077
                arg.NodeID,
×
3078
                arg.Timelock,
×
3079
                arg.FeePpm,
×
3080
                arg.BaseFeeMsat,
×
3081
                arg.MinHtlcMsat,
×
3082
                arg.LastUpdate,
×
3083
                arg.Disabled,
×
3084
                arg.MaxHtlcMsat,
×
3085
                arg.InboundBaseFeeMsat,
×
3086
                arg.InboundFeeRateMilliMsat,
×
3087
                arg.MessageFlags,
×
3088
                arg.ChannelFlags,
×
3089
                arg.Signature,
×
3090
        )
×
3091
        var id int64
×
3092
        err := row.Scan(&id)
×
3093
        return id, err
×
3094
}
×
3095

3096
const insertNodeFeature = `-- name: InsertNodeFeature :exec
3097
/* ─────────────────────────────────────────────
3098
   graph_node_features table queries
3099
   ─────────────────────────────────────────────
3100
*/
3101

3102
INSERT INTO graph_node_features (
3103
    node_id, feature_bit
3104
) VALUES (
3105
    $1, $2
3106
) ON CONFLICT (node_id, feature_bit)
3107
    -- Do nothing if the feature already exists for the node.
3108
    DO NOTHING
3109
`
3110

3111
type InsertNodeFeatureParams struct {
3112
        NodeID     int64
3113
        FeatureBit int32
3114
}
3115

3116
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
3117
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
3118
        return err
×
3119
}
×
3120

3121
const insertNodeMig = `-- name: InsertNodeMig :one
3122
/* ─────────────────────────────────────────────
3123
   Migration specific queries
3124

3125
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
3126
   dedicated to the migration that requires it, and so we can then remove
3127
   it from the main set of "live" queries that the code-base has access to.
3128
   ────────────────────────────────────────────-
3129
*/
3130

3131
INSERT INTO graph_nodes (
3132
    version, pub_key, alias, last_update, color, signature
3133
) VALUES (
3134
    $1, $2, $3, $4, $5, $6
3135
)
3136
ON CONFLICT (pub_key, version)
3137
    -- If a conflict occurs, we have already migrated this node. However, we
3138
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3139
    -- otherwise, the "RETURNING id" part does not work.
3140
    DO UPDATE SET
3141
        alias = EXCLUDED.alias,
3142
        last_update = EXCLUDED.last_update,
3143
        color = EXCLUDED.color,
3144
        signature = EXCLUDED.signature
3145
RETURNING id
3146
`
3147

3148
type InsertNodeMigParams struct {
3149
        Version    int16
3150
        PubKey     []byte
3151
        Alias      sql.NullString
3152
        LastUpdate sql.NullInt64
3153
        Color      sql.NullString
3154
        Signature  []byte
3155
}
3156

3157
// NOTE: This query is only meant to be used by the graph SQL migration since
3158
// for that migration, in order to be retry-safe, we don't want to error out if
3159
// we re-insert the same node (which would error if the normal UpsertNode query
3160
// is used because of the constraint in that query that requires a node update
3161
// to have a newer last_update than the existing node).
3162
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
3163
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
3164
                arg.Version,
×
3165
                arg.PubKey,
×
3166
                arg.Alias,
×
3167
                arg.LastUpdate,
×
3168
                arg.Color,
×
3169
                arg.Signature,
×
3170
        )
×
3171
        var id int64
×
3172
        err := row.Scan(&id)
×
3173
        return id, err
×
3174
}
×
3175

3176
const isClosedChannel = `-- name: IsClosedChannel :one
3177
SELECT EXISTS (
3178
    SELECT 1
3179
    FROM graph_closed_scids
3180
    WHERE scid = $1
3181
)
3182
`
3183

3184
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
3185
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
3186
        var exists bool
×
3187
        err := row.Scan(&exists)
×
3188
        return exists, err
×
3189
}
×
3190

3191
const isPublicV1Node = `-- name: IsPublicV1Node :one
3192
SELECT EXISTS (
3193
    SELECT 1
3194
    FROM graph_channels c
3195
    JOIN graph_nodes n ON n.id = c.node_id_1
3196
    -- NOTE: we hard-code the version here since the clauses
3197
    -- here that determine if a node is public is specific
3198
    -- to the V1 gossip protocol. In V1, a node is public
3199
    -- if it has a public channel and a public channel is one
3200
    -- where we have the set of signatures of the channel
3201
    -- announcement. It is enough to just check that we have
3202
    -- one of the signatures since we only ever set them
3203
    -- together.
3204
    WHERE c.version = 1
3205
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3206
      AND n.pub_key = $1
3207
    UNION ALL
3208
    SELECT 1
3209
    FROM graph_channels c
3210
    JOIN graph_nodes n ON n.id = c.node_id_2
3211
    WHERE c.version = 1
3212
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3213
      AND n.pub_key = $1
3214
)
3215
`
3216

3217
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3218
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
3219
        var exists bool
×
3220
        err := row.Scan(&exists)
×
3221
        return exists, err
×
3222
}
×
3223

3224
const isPublicV2Node = `-- name: IsPublicV2Node :one
3225
SELECT EXISTS (
3226
    SELECT 1
3227
    FROM graph_channels c
3228
    JOIN graph_nodes n ON n.id = c.node_id_1
3229
    -- NOTE: we hard-code the version here since the clauses
3230
    -- here that determine if a node is public is specific
3231
    -- to the V2 gossip protocol.
3232
    WHERE c.version = 2
3233
      AND COALESCE(length(c.signature), 0) > 0
3234
      AND n.pub_key = $1
3235

3236
    UNION ALL
3237

3238
    SELECT 1
3239
    FROM graph_channels c
3240
    JOIN graph_nodes n ON n.id = c.node_id_2
3241
    WHERE c.version = 2
3242
      AND COALESCE(length(c.signature), 0) > 0
3243
      AND n.pub_key = $1
3244
)
3245
`
3246

3247
func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3248
        row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey)
×
3249
        var exists bool
×
3250
        err := row.Scan(&exists)
×
3251
        return exists, err
×
3252
}
×
3253

3254
const isZombieChannel = `-- name: IsZombieChannel :one
3255
SELECT EXISTS (
3256
    SELECT 1
3257
    FROM graph_zombie_channels
3258
    WHERE scid = $1
3259
    AND version = $2
3260
) AS is_zombie
3261
`
3262

3263
type IsZombieChannelParams struct {
3264
        Scid    []byte
3265
        Version int16
3266
}
3267

3268
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
3269
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
3270
        var is_zombie bool
×
3271
        err := row.Scan(&is_zombie)
×
3272
        return is_zombie, err
×
3273
}
×
3274

3275
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
3276
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,
3277
    n1.pub_key AS node1_pubkey,
3278
    n2.pub_key AS node2_pubkey,
3279

3280
    -- Policy 1
3281
    -- TODO(elle): use sqlc.embed to embed policy structs
3282
    --  once this issue is resolved:
3283
    --  https://github.com/sqlc-dev/sqlc/issues/2997
3284
    cp1.id AS policy1_id,
3285
    cp1.node_id AS policy1_node_id,
3286
    cp1.version AS policy1_version,
3287
    cp1.timelock AS policy1_timelock,
3288
    cp1.fee_ppm AS policy1_fee_ppm,
3289
    cp1.base_fee_msat AS policy1_base_fee_msat,
3290
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
3291
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
3292
    cp1.last_update AS policy1_last_update,
3293
    cp1.disabled AS policy1_disabled,
3294
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3295
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3296
    cp1.message_flags AS policy1_message_flags,
3297
    cp1.channel_flags AS policy1_channel_flags,
3298
    cp1.signature AS policy1_signature,
3299
    cp1.block_height AS policy1_block_height,
3300
    cp1.disable_flags AS policy1_disable_flags,
3301

3302
       -- Policy 2
3303
    cp2.id AS policy2_id,
3304
    cp2.node_id AS policy2_node_id,
3305
    cp2.version AS policy2_version,
3306
    cp2.timelock AS policy2_timelock,
3307
    cp2.fee_ppm AS policy2_fee_ppm,
3308
    cp2.base_fee_msat AS policy2_base_fee_msat,
3309
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
3310
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
3311
    cp2.last_update AS policy2_last_update,
3312
    cp2.disabled AS policy2_disabled,
3313
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3314
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3315
    cp2.message_flags AS policy2_message_flags,
3316
    cp2.channel_flags AS policy2_channel_flags,
3317
    cp2.signature AS policy2_signature,
3318
    cp2.block_height AS policy2_block_height,
3319
    cp2.disable_flags AS policy2_disable_flags
3320

3321
FROM graph_channels c
3322
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3323
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3324
    LEFT JOIN graph_channel_policies cp1
3325
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3326
    LEFT JOIN graph_channel_policies cp2
3327
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3328
WHERE c.version = $1
3329
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
3330
`
3331

3332
type ListChannelsByNodeIDParams struct {
3333
        Version int16
3334
        NodeID1 int64
3335
}
3336

3337
type ListChannelsByNodeIDRow struct {
3338
        GraphChannel                   GraphChannel
3339
        Node1Pubkey                    []byte
3340
        Node2Pubkey                    []byte
3341
        Policy1ID                      sql.NullInt64
3342
        Policy1NodeID                  sql.NullInt64
3343
        Policy1Version                 sql.NullInt16
3344
        Policy1Timelock                sql.NullInt32
3345
        Policy1FeePpm                  sql.NullInt64
3346
        Policy1BaseFeeMsat             sql.NullInt64
3347
        Policy1MinHtlcMsat             sql.NullInt64
3348
        Policy1MaxHtlcMsat             sql.NullInt64
3349
        Policy1LastUpdate              sql.NullInt64
3350
        Policy1Disabled                sql.NullBool
3351
        Policy1InboundBaseFeeMsat      sql.NullInt64
3352
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3353
        Policy1MessageFlags            sql.NullInt16
3354
        Policy1ChannelFlags            sql.NullInt16
3355
        Policy1Signature               []byte
3356
        Policy1BlockHeight             sql.NullInt64
3357
        Policy1DisableFlags            sql.NullInt16
3358
        Policy2ID                      sql.NullInt64
3359
        Policy2NodeID                  sql.NullInt64
3360
        Policy2Version                 sql.NullInt16
3361
        Policy2Timelock                sql.NullInt32
3362
        Policy2FeePpm                  sql.NullInt64
3363
        Policy2BaseFeeMsat             sql.NullInt64
3364
        Policy2MinHtlcMsat             sql.NullInt64
3365
        Policy2MaxHtlcMsat             sql.NullInt64
3366
        Policy2LastUpdate              sql.NullInt64
3367
        Policy2Disabled                sql.NullBool
3368
        Policy2InboundBaseFeeMsat      sql.NullInt64
3369
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3370
        Policy2MessageFlags            sql.NullInt16
3371
        Policy2ChannelFlags            sql.NullInt16
3372
        Policy2Signature               []byte
3373
        Policy2BlockHeight             sql.NullInt64
3374
        Policy2DisableFlags            sql.NullInt16
3375
}
3376

3377
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
3378
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
3379
        if err != nil {
×
3380
                return nil, err
×
3381
        }
×
3382
        defer rows.Close()
×
3383
        var items []ListChannelsByNodeIDRow
×
3384
        for rows.Next() {
×
3385
                var i ListChannelsByNodeIDRow
×
3386
                if err := rows.Scan(
×
3387
                        &i.GraphChannel.ID,
×
3388
                        &i.GraphChannel.Version,
×
3389
                        &i.GraphChannel.Scid,
×
3390
                        &i.GraphChannel.NodeID1,
×
3391
                        &i.GraphChannel.NodeID2,
×
3392
                        &i.GraphChannel.Outpoint,
×
3393
                        &i.GraphChannel.Capacity,
×
3394
                        &i.GraphChannel.BitcoinKey1,
×
3395
                        &i.GraphChannel.BitcoinKey2,
×
3396
                        &i.GraphChannel.Node1Signature,
×
3397
                        &i.GraphChannel.Node2Signature,
×
3398
                        &i.GraphChannel.Bitcoin1Signature,
×
3399
                        &i.GraphChannel.Bitcoin2Signature,
×
3400
                        &i.GraphChannel.Signature,
×
3401
                        &i.GraphChannel.FundingPkScript,
×
3402
                        &i.GraphChannel.MerkleRootHash,
×
3403
                        &i.Node1Pubkey,
×
3404
                        &i.Node2Pubkey,
×
3405
                        &i.Policy1ID,
×
3406
                        &i.Policy1NodeID,
×
3407
                        &i.Policy1Version,
×
3408
                        &i.Policy1Timelock,
×
3409
                        &i.Policy1FeePpm,
×
3410
                        &i.Policy1BaseFeeMsat,
×
3411
                        &i.Policy1MinHtlcMsat,
×
3412
                        &i.Policy1MaxHtlcMsat,
×
3413
                        &i.Policy1LastUpdate,
×
3414
                        &i.Policy1Disabled,
×
3415
                        &i.Policy1InboundBaseFeeMsat,
×
3416
                        &i.Policy1InboundFeeRateMilliMsat,
×
3417
                        &i.Policy1MessageFlags,
×
3418
                        &i.Policy1ChannelFlags,
×
3419
                        &i.Policy1Signature,
×
3420
                        &i.Policy1BlockHeight,
×
3421
                        &i.Policy1DisableFlags,
×
3422
                        &i.Policy2ID,
×
3423
                        &i.Policy2NodeID,
×
3424
                        &i.Policy2Version,
×
3425
                        &i.Policy2Timelock,
×
3426
                        &i.Policy2FeePpm,
×
3427
                        &i.Policy2BaseFeeMsat,
×
3428
                        &i.Policy2MinHtlcMsat,
×
3429
                        &i.Policy2MaxHtlcMsat,
×
3430
                        &i.Policy2LastUpdate,
×
3431
                        &i.Policy2Disabled,
×
3432
                        &i.Policy2InboundBaseFeeMsat,
×
3433
                        &i.Policy2InboundFeeRateMilliMsat,
×
3434
                        &i.Policy2MessageFlags,
×
3435
                        &i.Policy2ChannelFlags,
×
3436
                        &i.Policy2Signature,
×
3437
                        &i.Policy2BlockHeight,
×
3438
                        &i.Policy2DisableFlags,
×
3439
                ); err != nil {
×
3440
                        return nil, err
×
3441
                }
×
3442
                items = append(items, i)
×
3443
        }
3444
        if err := rows.Close(); err != nil {
×
3445
                return nil, err
×
3446
        }
×
3447
        if err := rows.Err(); err != nil {
×
3448
                return nil, err
×
3449
        }
×
3450
        return items, nil
×
3451
}
3452

3453
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3454
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,
3455
       n1.pub_key AS node1_pubkey,
3456
       n2.pub_key AS node2_pubkey,
3457

3458
       -- Policy 1
3459
       -- TODO(elle): use sqlc.embed to embed policy structs
3460
       --  once this issue is resolved:
3461
       --  https://github.com/sqlc-dev/sqlc/issues/2997
3462
       cp1.id AS policy1_id,
3463
       cp1.node_id AS policy1_node_id,
3464
       cp1.version AS policy1_version,
3465
       cp1.timelock AS policy1_timelock,
3466
       cp1.fee_ppm AS policy1_fee_ppm,
3467
       cp1.base_fee_msat AS policy1_base_fee_msat,
3468
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
3469
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
3470
       cp1.last_update AS policy1_last_update,
3471
       cp1.disabled AS policy1_disabled,
3472
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3473
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3474
       cp1.message_flags AS policy1_message_flags,
3475
       cp1.channel_flags AS policy1_channel_flags,
3476
       cp1.signature AS policy1_signature,
3477
    cp1.block_height AS policy1_block_height,
3478
    cp1.disable_flags AS policy1_disable_flags,
3479

3480
       -- Policy 2
3481
       cp2.id AS policy2_id,
3482
       cp2.node_id AS policy2_node_id,
3483
       cp2.version AS policy2_version,
3484
       cp2.timelock AS policy2_timelock,
3485
       cp2.fee_ppm AS policy2_fee_ppm,
3486
       cp2.base_fee_msat AS policy2_base_fee_msat,
3487
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
3488
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
3489
       cp2.last_update AS policy2_last_update,
3490
       cp2.disabled AS policy2_disabled,
3491
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3492
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3493
       cp2.message_flags AS policy2_message_flags,
3494
       cp2.channel_flags AS policy2_channel_flags,
3495
       cp2.signature AS policy2_signature,
3496
    cp2.block_height AS policy2_block_height,
3497
    cp2.disable_flags AS policy2_disable_flags
3498

3499
FROM graph_channels c
3500
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3501
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3502
         LEFT JOIN graph_channel_policies cp1
3503
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3504
         LEFT JOIN graph_channel_policies cp2
3505
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3506
WHERE c.version = $1
3507
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
3508
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
3509
`
3510

3511
type ListChannelsForNodeIDsParams struct {
3512
        Version  int16
3513
        Node1Ids []int64
3514
        Node2Ids []int64
3515
}
3516

3517
type ListChannelsForNodeIDsRow struct {
3518
        GraphChannel                   GraphChannel
3519
        Node1Pubkey                    []byte
3520
        Node2Pubkey                    []byte
3521
        Policy1ID                      sql.NullInt64
3522
        Policy1NodeID                  sql.NullInt64
3523
        Policy1Version                 sql.NullInt16
3524
        Policy1Timelock                sql.NullInt32
3525
        Policy1FeePpm                  sql.NullInt64
3526
        Policy1BaseFeeMsat             sql.NullInt64
3527
        Policy1MinHtlcMsat             sql.NullInt64
3528
        Policy1MaxHtlcMsat             sql.NullInt64
3529
        Policy1LastUpdate              sql.NullInt64
3530
        Policy1Disabled                sql.NullBool
3531
        Policy1InboundBaseFeeMsat      sql.NullInt64
3532
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3533
        Policy1MessageFlags            sql.NullInt16
3534
        Policy1ChannelFlags            sql.NullInt16
3535
        Policy1Signature               []byte
3536
        Policy1BlockHeight             sql.NullInt64
3537
        Policy1DisableFlags            sql.NullInt16
3538
        Policy2ID                      sql.NullInt64
3539
        Policy2NodeID                  sql.NullInt64
3540
        Policy2Version                 sql.NullInt16
3541
        Policy2Timelock                sql.NullInt32
3542
        Policy2FeePpm                  sql.NullInt64
3543
        Policy2BaseFeeMsat             sql.NullInt64
3544
        Policy2MinHtlcMsat             sql.NullInt64
3545
        Policy2MaxHtlcMsat             sql.NullInt64
3546
        Policy2LastUpdate              sql.NullInt64
3547
        Policy2Disabled                sql.NullBool
3548
        Policy2InboundBaseFeeMsat      sql.NullInt64
3549
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3550
        Policy2MessageFlags            sql.NullInt16
3551
        Policy2ChannelFlags            sql.NullInt16
3552
        Policy2Signature               []byte
3553
        Policy2BlockHeight             sql.NullInt64
3554
        Policy2DisableFlags            sql.NullInt16
3555
}
3556

3557
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
3558
        query := listChannelsForNodeIDs
×
3559
        var queryParams []interface{}
×
3560
        queryParams = append(queryParams, arg.Version)
×
3561
        if len(arg.Node1Ids) > 0 {
×
3562
                for _, v := range arg.Node1Ids {
×
3563
                        queryParams = append(queryParams, v)
×
3564
                }
×
3565
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
3566
        } else {
×
3567
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
3568
        }
×
3569
        if len(arg.Node2Ids) > 0 {
×
3570
                for _, v := range arg.Node2Ids {
×
3571
                        queryParams = append(queryParams, v)
×
3572
                }
×
3573
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
3574
        } else {
×
3575
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
3576
        }
×
3577
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
3578
        if err != nil {
×
3579
                return nil, err
×
3580
        }
×
3581
        defer rows.Close()
×
3582
        var items []ListChannelsForNodeIDsRow
×
3583
        for rows.Next() {
×
3584
                var i ListChannelsForNodeIDsRow
×
3585
                if err := rows.Scan(
×
3586
                        &i.GraphChannel.ID,
×
3587
                        &i.GraphChannel.Version,
×
3588
                        &i.GraphChannel.Scid,
×
3589
                        &i.GraphChannel.NodeID1,
×
3590
                        &i.GraphChannel.NodeID2,
×
3591
                        &i.GraphChannel.Outpoint,
×
3592
                        &i.GraphChannel.Capacity,
×
3593
                        &i.GraphChannel.BitcoinKey1,
×
3594
                        &i.GraphChannel.BitcoinKey2,
×
3595
                        &i.GraphChannel.Node1Signature,
×
3596
                        &i.GraphChannel.Node2Signature,
×
3597
                        &i.GraphChannel.Bitcoin1Signature,
×
3598
                        &i.GraphChannel.Bitcoin2Signature,
×
3599
                        &i.GraphChannel.Signature,
×
3600
                        &i.GraphChannel.FundingPkScript,
×
3601
                        &i.GraphChannel.MerkleRootHash,
×
3602
                        &i.Node1Pubkey,
×
3603
                        &i.Node2Pubkey,
×
3604
                        &i.Policy1ID,
×
3605
                        &i.Policy1NodeID,
×
3606
                        &i.Policy1Version,
×
3607
                        &i.Policy1Timelock,
×
3608
                        &i.Policy1FeePpm,
×
3609
                        &i.Policy1BaseFeeMsat,
×
3610
                        &i.Policy1MinHtlcMsat,
×
3611
                        &i.Policy1MaxHtlcMsat,
×
3612
                        &i.Policy1LastUpdate,
×
3613
                        &i.Policy1Disabled,
×
3614
                        &i.Policy1InboundBaseFeeMsat,
×
3615
                        &i.Policy1InboundFeeRateMilliMsat,
×
3616
                        &i.Policy1MessageFlags,
×
3617
                        &i.Policy1ChannelFlags,
×
3618
                        &i.Policy1Signature,
×
3619
                        &i.Policy1BlockHeight,
×
3620
                        &i.Policy1DisableFlags,
×
3621
                        &i.Policy2ID,
×
3622
                        &i.Policy2NodeID,
×
3623
                        &i.Policy2Version,
×
3624
                        &i.Policy2Timelock,
×
3625
                        &i.Policy2FeePpm,
×
3626
                        &i.Policy2BaseFeeMsat,
×
3627
                        &i.Policy2MinHtlcMsat,
×
3628
                        &i.Policy2MaxHtlcMsat,
×
3629
                        &i.Policy2LastUpdate,
×
3630
                        &i.Policy2Disabled,
×
3631
                        &i.Policy2InboundBaseFeeMsat,
×
3632
                        &i.Policy2InboundFeeRateMilliMsat,
×
3633
                        &i.Policy2MessageFlags,
×
3634
                        &i.Policy2ChannelFlags,
×
3635
                        &i.Policy2Signature,
×
3636
                        &i.Policy2BlockHeight,
×
3637
                        &i.Policy2DisableFlags,
×
3638
                ); err != nil {
×
3639
                        return nil, err
×
3640
                }
×
3641
                items = append(items, i)
×
3642
        }
3643
        if err := rows.Close(); err != nil {
×
3644
                return nil, err
×
3645
        }
×
3646
        if err := rows.Err(); err != nil {
×
3647
                return nil, err
×
3648
        }
×
3649
        return items, nil
×
3650
}
3651

3652
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3653
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3654
FROM graph_channels c
3655
WHERE c.version = $1 AND c.id > $2
3656
ORDER BY c.id
3657
LIMIT $3
3658
`
3659

3660
type ListChannelsPaginatedParams struct {
3661
        Version int16
3662
        ID      int64
3663
        Limit   int32
3664
}
3665

3666
type ListChannelsPaginatedRow struct {
3667
        ID          int64
3668
        BitcoinKey1 []byte
3669
        BitcoinKey2 []byte
3670
        Outpoint    string
3671
}
3672

3673
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3674
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3675
        if err != nil {
×
3676
                return nil, err
×
3677
        }
×
3678
        defer rows.Close()
×
3679
        var items []ListChannelsPaginatedRow
×
3680
        for rows.Next() {
×
3681
                var i ListChannelsPaginatedRow
×
3682
                if err := rows.Scan(
×
3683
                        &i.ID,
×
3684
                        &i.BitcoinKey1,
×
3685
                        &i.BitcoinKey2,
×
3686
                        &i.Outpoint,
×
3687
                ); err != nil {
×
3688
                        return nil, err
×
3689
                }
×
3690
                items = append(items, i)
×
3691
        }
3692
        if err := rows.Close(); err != nil {
×
3693
                return nil, err
×
3694
        }
×
3695
        if err := rows.Err(); err != nil {
×
3696
                return nil, err
×
3697
        }
×
3698
        return items, nil
×
3699
}
3700

3701
const listChannelsPaginatedV2 = `-- name: ListChannelsPaginatedV2 :many
3702
SELECT id, outpoint, funding_pk_script
3703
FROM graph_channels c
3704
WHERE c.version = 2 AND c.id > $1
3705
ORDER BY c.id
3706
LIMIT $2
3707
`
3708

3709
type ListChannelsPaginatedV2Params struct {
3710
        ID    int64
3711
        Limit int32
3712
}
3713

3714
type ListChannelsPaginatedV2Row struct {
3715
        ID              int64
3716
        Outpoint        string
3717
        FundingPkScript []byte
3718
}
3719

NEW
3720
func (q *Queries) ListChannelsPaginatedV2(ctx context.Context, arg ListChannelsPaginatedV2Params) ([]ListChannelsPaginatedV2Row, error) {
×
NEW
3721
        rows, err := q.db.QueryContext(ctx, listChannelsPaginatedV2, arg.ID, arg.Limit)
×
NEW
3722
        if err != nil {
×
NEW
3723
                return nil, err
×
NEW
3724
        }
×
NEW
3725
        defer rows.Close()
×
NEW
3726
        var items []ListChannelsPaginatedV2Row
×
NEW
3727
        for rows.Next() {
×
NEW
3728
                var i ListChannelsPaginatedV2Row
×
NEW
3729
                if err := rows.Scan(&i.ID, &i.Outpoint, &i.FundingPkScript); err != nil {
×
NEW
3730
                        return nil, err
×
NEW
3731
                }
×
NEW
3732
                items = append(items, i)
×
3733
        }
NEW
3734
        if err := rows.Close(); err != nil {
×
NEW
3735
                return nil, err
×
NEW
3736
        }
×
NEW
3737
        if err := rows.Err(); err != nil {
×
NEW
3738
                return nil, err
×
NEW
3739
        }
×
NEW
3740
        return items, nil
×
3741
}
3742

3743
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3744
SELECT
3745
    c.id as id,
3746
    c.scid as scid,
3747
    c.capacity AS capacity,
3748

3749
    -- Join node pubkeys
3750
    n1.pub_key AS node1_pubkey,
3751
    n2.pub_key AS node2_pubkey,
3752

3753
    -- Node 1 policy
3754
    cp1.version AS policy1_version,
3755
    cp1.timelock AS policy_1_timelock,
3756
    cp1.fee_ppm AS policy_1_fee_ppm,
3757
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3758
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3759
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3760
    cp1.disabled AS policy_1_disabled,
3761
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3762
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3763
    cp1.message_flags AS policy1_message_flags,
3764
    cp1.channel_flags AS policy1_channel_flags,
3765
    cp1.block_height AS policy1_block_height,
3766
    cp1.disable_flags AS policy1_disable_flags,
3767

3768
    -- Node 2 policy
3769
    cp2.version AS policy2_version,
3770
    cp2.timelock AS policy_2_timelock,
3771
    cp2.fee_ppm AS policy_2_fee_ppm,
3772
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3773
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3774
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3775
    cp2.disabled AS policy_2_disabled,
3776
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3777
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3778
    cp2.message_flags AS policy2_message_flags,
3779
    cp2.channel_flags AS policy2_channel_flags,
3780
    cp2.block_height AS policy2_block_height,
3781
    cp2.disable_flags AS policy2_disable_flags
3782

3783
FROM graph_channels c
3784
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3785
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3786
LEFT JOIN graph_channel_policies cp1
3787
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3788
LEFT JOIN graph_channel_policies cp2
3789
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3790
WHERE c.version = $1 AND c.id > $2
3791
ORDER BY c.id
3792
LIMIT $3
3793
`
3794

3795
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3796
        Version int16
3797
        ID      int64
3798
        Limit   int32
3799
}
3800

3801
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3802
        ID                             int64
3803
        Scid                           []byte
3804
        Capacity                       sql.NullInt64
3805
        Node1Pubkey                    []byte
3806
        Node2Pubkey                    []byte
3807
        Policy1Version                 sql.NullInt16
3808
        Policy1Timelock                sql.NullInt32
3809
        Policy1FeePpm                  sql.NullInt64
3810
        Policy1BaseFeeMsat             sql.NullInt64
3811
        Policy1MinHtlcMsat             sql.NullInt64
3812
        Policy1MaxHtlcMsat             sql.NullInt64
3813
        Policy1Disabled                sql.NullBool
3814
        Policy1InboundBaseFeeMsat      sql.NullInt64
3815
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3816
        Policy1MessageFlags            sql.NullInt16
3817
        Policy1ChannelFlags            sql.NullInt16
3818
        Policy1BlockHeight             sql.NullInt64
3819
        Policy1DisableFlags            sql.NullInt16
3820
        Policy2Version                 sql.NullInt16
3821
        Policy2Timelock                sql.NullInt32
3822
        Policy2FeePpm                  sql.NullInt64
3823
        Policy2BaseFeeMsat             sql.NullInt64
3824
        Policy2MinHtlcMsat             sql.NullInt64
3825
        Policy2MaxHtlcMsat             sql.NullInt64
3826
        Policy2Disabled                sql.NullBool
3827
        Policy2InboundBaseFeeMsat      sql.NullInt64
3828
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3829
        Policy2MessageFlags            sql.NullInt16
3830
        Policy2ChannelFlags            sql.NullInt16
3831
        Policy2BlockHeight             sql.NullInt64
3832
        Policy2DisableFlags            sql.NullInt16
3833
}
3834

3835
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3836
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3837
        if err != nil {
×
3838
                return nil, err
×
3839
        }
×
3840
        defer rows.Close()
×
3841
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3842
        for rows.Next() {
×
3843
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3844
                if err := rows.Scan(
×
3845
                        &i.ID,
×
3846
                        &i.Scid,
×
3847
                        &i.Capacity,
×
3848
                        &i.Node1Pubkey,
×
3849
                        &i.Node2Pubkey,
×
3850
                        &i.Policy1Version,
×
3851
                        &i.Policy1Timelock,
×
3852
                        &i.Policy1FeePpm,
×
3853
                        &i.Policy1BaseFeeMsat,
×
3854
                        &i.Policy1MinHtlcMsat,
×
3855
                        &i.Policy1MaxHtlcMsat,
×
3856
                        &i.Policy1Disabled,
×
3857
                        &i.Policy1InboundBaseFeeMsat,
×
3858
                        &i.Policy1InboundFeeRateMilliMsat,
×
3859
                        &i.Policy1MessageFlags,
×
3860
                        &i.Policy1ChannelFlags,
×
3861
                        &i.Policy1BlockHeight,
×
3862
                        &i.Policy1DisableFlags,
×
3863
                        &i.Policy2Version,
×
3864
                        &i.Policy2Timelock,
×
3865
                        &i.Policy2FeePpm,
×
3866
                        &i.Policy2BaseFeeMsat,
×
3867
                        &i.Policy2MinHtlcMsat,
×
3868
                        &i.Policy2MaxHtlcMsat,
×
3869
                        &i.Policy2Disabled,
×
3870
                        &i.Policy2InboundBaseFeeMsat,
×
3871
                        &i.Policy2InboundFeeRateMilliMsat,
×
3872
                        &i.Policy2MessageFlags,
×
3873
                        &i.Policy2ChannelFlags,
×
3874
                        &i.Policy2BlockHeight,
×
3875
                        &i.Policy2DisableFlags,
×
3876
                ); err != nil {
×
3877
                        return nil, err
×
3878
                }
×
3879
                items = append(items, i)
×
3880
        }
3881
        if err := rows.Close(); err != nil {
×
3882
                return nil, err
×
3883
        }
×
3884
        if err := rows.Err(); err != nil {
×
3885
                return nil, err
×
3886
        }
×
3887
        return items, nil
×
3888
}
3889

3890
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3891
SELECT
3892
    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,
3893

3894
    -- Join node pubkeys
3895
    n1.pub_key AS node1_pubkey,
3896
    n2.pub_key AS node2_pubkey,
3897

3898
    -- Node 1 policy
3899
    cp1.id AS policy_1_id,
3900
    cp1.node_id AS policy_1_node_id,
3901
    cp1.version AS policy_1_version,
3902
    cp1.timelock AS policy_1_timelock,
3903
    cp1.fee_ppm AS policy_1_fee_ppm,
3904
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3905
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3906
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3907
    cp1.last_update AS policy_1_last_update,
3908
    cp1.disabled AS policy_1_disabled,
3909
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3910
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3911
    cp1.message_flags AS policy1_message_flags,
3912
    cp1.channel_flags AS policy1_channel_flags,
3913
    cp1.block_height AS policy1_block_height,
3914
    cp1.disable_flags AS policy1_disable_flags,
3915
    cp1.signature AS policy_1_signature,
3916

3917
    -- Node 2 policy
3918
    cp2.id AS policy_2_id,
3919
    cp2.node_id AS policy_2_node_id,
3920
    cp2.version AS policy_2_version,
3921
    cp2.timelock AS policy_2_timelock,
3922
    cp2.fee_ppm AS policy_2_fee_ppm,
3923
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3924
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3925
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3926
    cp2.last_update AS policy_2_last_update,
3927
    cp2.disabled AS policy_2_disabled,
3928
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3929
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3930
    cp2.message_flags AS policy2_message_flags,
3931
    cp2.channel_flags AS policy2_channel_flags,
3932
    cp2.signature AS policy_2_signature,
3933
    cp2.block_height AS policy_2_block_height,
3934
    cp2.disable_flags AS policy_2_disable_flags
3935

3936
FROM graph_channels c
3937
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3938
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3939
LEFT JOIN graph_channel_policies cp1
3940
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3941
LEFT JOIN graph_channel_policies cp2
3942
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3943
WHERE c.version = $1 AND c.id > $2
3944
ORDER BY c.id
3945
LIMIT $3
3946
`
3947

3948
type ListChannelsWithPoliciesPaginatedParams struct {
3949
        Version int16
3950
        ID      int64
3951
        Limit   int32
3952
}
3953

3954
type ListChannelsWithPoliciesPaginatedRow struct {
3955
        GraphChannel                   GraphChannel
3956
        Node1Pubkey                    []byte
3957
        Node2Pubkey                    []byte
3958
        Policy1ID                      sql.NullInt64
3959
        Policy1NodeID                  sql.NullInt64
3960
        Policy1Version                 sql.NullInt16
3961
        Policy1Timelock                sql.NullInt32
3962
        Policy1FeePpm                  sql.NullInt64
3963
        Policy1BaseFeeMsat             sql.NullInt64
3964
        Policy1MinHtlcMsat             sql.NullInt64
3965
        Policy1MaxHtlcMsat             sql.NullInt64
3966
        Policy1LastUpdate              sql.NullInt64
3967
        Policy1Disabled                sql.NullBool
3968
        Policy1InboundBaseFeeMsat      sql.NullInt64
3969
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3970
        Policy1MessageFlags            sql.NullInt16
3971
        Policy1ChannelFlags            sql.NullInt16
3972
        Policy1BlockHeight             sql.NullInt64
3973
        Policy1DisableFlags            sql.NullInt16
3974
        Policy1Signature               []byte
3975
        Policy2ID                      sql.NullInt64
3976
        Policy2NodeID                  sql.NullInt64
3977
        Policy2Version                 sql.NullInt16
3978
        Policy2Timelock                sql.NullInt32
3979
        Policy2FeePpm                  sql.NullInt64
3980
        Policy2BaseFeeMsat             sql.NullInt64
3981
        Policy2MinHtlcMsat             sql.NullInt64
3982
        Policy2MaxHtlcMsat             sql.NullInt64
3983
        Policy2LastUpdate              sql.NullInt64
3984
        Policy2Disabled                sql.NullBool
3985
        Policy2InboundBaseFeeMsat      sql.NullInt64
3986
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3987
        Policy2MessageFlags            sql.NullInt16
3988
        Policy2ChannelFlags            sql.NullInt16
3989
        Policy2Signature               []byte
3990
        Policy2BlockHeight             sql.NullInt64
3991
        Policy2DisableFlags            sql.NullInt16
3992
}
3993

3994
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
3995
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
3996
        if err != nil {
×
3997
                return nil, err
×
3998
        }
×
3999
        defer rows.Close()
×
4000
        var items []ListChannelsWithPoliciesPaginatedRow
×
4001
        for rows.Next() {
×
4002
                var i ListChannelsWithPoliciesPaginatedRow
×
4003
                if err := rows.Scan(
×
4004
                        &i.GraphChannel.ID,
×
4005
                        &i.GraphChannel.Version,
×
4006
                        &i.GraphChannel.Scid,
×
4007
                        &i.GraphChannel.NodeID1,
×
4008
                        &i.GraphChannel.NodeID2,
×
4009
                        &i.GraphChannel.Outpoint,
×
4010
                        &i.GraphChannel.Capacity,
×
4011
                        &i.GraphChannel.BitcoinKey1,
×
4012
                        &i.GraphChannel.BitcoinKey2,
×
4013
                        &i.GraphChannel.Node1Signature,
×
4014
                        &i.GraphChannel.Node2Signature,
×
4015
                        &i.GraphChannel.Bitcoin1Signature,
×
4016
                        &i.GraphChannel.Bitcoin2Signature,
×
4017
                        &i.GraphChannel.Signature,
×
4018
                        &i.GraphChannel.FundingPkScript,
×
4019
                        &i.GraphChannel.MerkleRootHash,
×
4020
                        &i.Node1Pubkey,
×
4021
                        &i.Node2Pubkey,
×
4022
                        &i.Policy1ID,
×
4023
                        &i.Policy1NodeID,
×
4024
                        &i.Policy1Version,
×
4025
                        &i.Policy1Timelock,
×
4026
                        &i.Policy1FeePpm,
×
4027
                        &i.Policy1BaseFeeMsat,
×
4028
                        &i.Policy1MinHtlcMsat,
×
4029
                        &i.Policy1MaxHtlcMsat,
×
4030
                        &i.Policy1LastUpdate,
×
4031
                        &i.Policy1Disabled,
×
4032
                        &i.Policy1InboundBaseFeeMsat,
×
4033
                        &i.Policy1InboundFeeRateMilliMsat,
×
4034
                        &i.Policy1MessageFlags,
×
4035
                        &i.Policy1ChannelFlags,
×
4036
                        &i.Policy1BlockHeight,
×
4037
                        &i.Policy1DisableFlags,
×
4038
                        &i.Policy1Signature,
×
4039
                        &i.Policy2ID,
×
4040
                        &i.Policy2NodeID,
×
4041
                        &i.Policy2Version,
×
4042
                        &i.Policy2Timelock,
×
4043
                        &i.Policy2FeePpm,
×
4044
                        &i.Policy2BaseFeeMsat,
×
4045
                        &i.Policy2MinHtlcMsat,
×
4046
                        &i.Policy2MaxHtlcMsat,
×
4047
                        &i.Policy2LastUpdate,
×
4048
                        &i.Policy2Disabled,
×
4049
                        &i.Policy2InboundBaseFeeMsat,
×
4050
                        &i.Policy2InboundFeeRateMilliMsat,
×
4051
                        &i.Policy2MessageFlags,
×
4052
                        &i.Policy2ChannelFlags,
×
4053
                        &i.Policy2Signature,
×
4054
                        &i.Policy2BlockHeight,
×
4055
                        &i.Policy2DisableFlags,
×
4056
                ); err != nil {
×
4057
                        return nil, err
×
4058
                }
×
4059
                items = append(items, i)
×
4060
        }
4061
        if err := rows.Close(); err != nil {
×
4062
                return nil, err
×
4063
        }
×
4064
        if err := rows.Err(); err != nil {
×
4065
                return nil, err
×
4066
        }
×
4067
        return items, nil
×
4068
}
4069

4070
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
4071
SELECT id, pub_key
4072
FROM graph_nodes
4073
WHERE version = $1  AND id > $2
4074
ORDER BY id
4075
LIMIT $3
4076
`
4077

4078
type ListNodeIDsAndPubKeysParams struct {
4079
        Version int16
4080
        ID      int64
4081
        Limit   int32
4082
}
4083

4084
type ListNodeIDsAndPubKeysRow struct {
4085
        ID     int64
4086
        PubKey []byte
4087
}
4088

4089
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
4090
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
4091
        if err != nil {
×
4092
                return nil, err
×
4093
        }
×
4094
        defer rows.Close()
×
4095
        var items []ListNodeIDsAndPubKeysRow
×
4096
        for rows.Next() {
×
4097
                var i ListNodeIDsAndPubKeysRow
×
4098
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
4099
                        return nil, err
×
4100
                }
×
4101
                items = append(items, i)
×
4102
        }
4103
        if err := rows.Close(); err != nil {
×
4104
                return nil, err
×
4105
        }
×
4106
        if err := rows.Err(); err != nil {
×
4107
                return nil, err
×
4108
        }
×
4109
        return items, nil
×
4110
}
4111

4112
const listNodesPaginated = `-- name: ListNodesPaginated :many
4113
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
4114
FROM graph_nodes
4115
WHERE version = $1 AND id > $2
4116
ORDER BY id
4117
LIMIT $3
4118
`
4119

4120
type ListNodesPaginatedParams struct {
4121
        Version int16
4122
        ID      int64
4123
        Limit   int32
4124
}
4125

4126
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
4127
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
4128
        if err != nil {
×
4129
                return nil, err
×
4130
        }
×
4131
        defer rows.Close()
×
4132
        var items []GraphNode
×
4133
        for rows.Next() {
×
4134
                var i GraphNode
×
4135
                if err := rows.Scan(
×
4136
                        &i.ID,
×
4137
                        &i.Version,
×
4138
                        &i.PubKey,
×
4139
                        &i.Alias,
×
4140
                        &i.LastUpdate,
×
4141
                        &i.Color,
×
4142
                        &i.Signature,
×
4143
                        &i.BlockHeight,
×
4144
                ); err != nil {
×
4145
                        return nil, err
×
4146
                }
×
4147
                items = append(items, i)
×
4148
        }
4149
        if err := rows.Close(); err != nil {
×
4150
                return nil, err
×
4151
        }
×
4152
        if err := rows.Err(); err != nil {
×
4153
                return nil, err
×
4154
        }
×
4155
        return items, nil
×
4156
}
4157

4158
const nodeExists = `-- name: NodeExists :one
4159
SELECT EXISTS (
4160
    SELECT 1
4161
    FROM graph_nodes
4162
    WHERE pub_key = $1
4163
      AND version = $2
4164
) AS node_exists
4165
`
4166

4167
type NodeExistsParams struct {
4168
        PubKey  []byte
4169
        Version int16
4170
}
4171

4172
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
4173
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
4174
        var node_exists bool
×
4175
        err := row.Scan(&node_exists)
×
4176
        return node_exists, err
×
4177
}
×
4178

4179
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
4180
/* ─────────────────────────────────────────────
4181
   graph_channel_policy_extra_types table queries
4182
   ─────────────────────────────────────────────
4183
*/
4184

4185
INSERT INTO graph_channel_policy_extra_types (
4186
    channel_policy_id, type, value
4187
)
4188
VALUES ($1, $2, $3)
4189
ON CONFLICT (channel_policy_id, type)
4190
    -- If a conflict occurs on channel_policy_id and type, then we update the
4191
    -- value.
4192
    DO UPDATE SET value = EXCLUDED.value
4193
`
4194

4195
type UpsertChanPolicyExtraTypeParams struct {
4196
        ChannelPolicyID int64
4197
        Type            int64
4198
        Value           []byte
4199
}
4200

4201
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
4202
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
4203
        return err
×
4204
}
×
4205

4206
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
4207
/* ─────────────────────────────────────────────
4208
   graph_channel_extra_types table queries
4209
   ─────────────────────────────────────────────
4210
*/
4211

4212
INSERT INTO graph_channel_extra_types (
4213
    channel_id, type, value
4214
)
4215
VALUES ($1, $2, $3)
4216
    ON CONFLICT (channel_id, type)
4217
    -- Update the value if a conflict occurs on channel_id and type.
4218
    DO UPDATE SET value = EXCLUDED.value
4219
`
4220

4221
type UpsertChannelExtraTypeParams struct {
4222
        ChannelID int64
4223
        Type      int64
4224
        Value     []byte
4225
}
4226

4227
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
4228
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
4229
        return err
×
4230
}
×
4231

4232
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
4233
/* ─────────────────────────────────────────────
4234
   graph_channel_policies table queries
4235
   ─────────────────────────────────────────────
4236
*/
4237

4238
INSERT INTO graph_channel_policies (
4239
    version, channel_id, node_id, timelock, fee_ppm,
4240
    base_fee_msat, min_htlc_msat, last_update, disabled,
4241
    max_htlc_msat, inbound_base_fee_msat,
4242
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
4243
    signature, block_height, disable_flags
4244
) VALUES  (
4245
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
4246
)
4247
ON CONFLICT (channel_id, node_id, version)
4248
    -- Update the following fields if a conflict occurs on channel_id,
4249
    -- node_id, and version.
4250
    DO UPDATE SET
4251
        timelock = EXCLUDED.timelock,
4252
        fee_ppm = EXCLUDED.fee_ppm,
4253
        base_fee_msat = EXCLUDED.base_fee_msat,
4254
        min_htlc_msat = EXCLUDED.min_htlc_msat,
4255
        last_update = EXCLUDED.last_update,
4256
        disabled = EXCLUDED.disabled,
4257
        max_htlc_msat = EXCLUDED.max_htlc_msat,
4258
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
4259
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
4260
        message_flags = EXCLUDED.message_flags,
4261
        channel_flags = EXCLUDED.channel_flags,
4262
        signature = EXCLUDED.signature,
4263
        block_height = EXCLUDED.block_height,
4264
        disable_flags = EXCLUDED.disable_flags
4265
WHERE (
4266
    EXCLUDED.version = 1 AND (
4267
        graph_channel_policies.last_update IS NULL
4268
        OR EXCLUDED.last_update > graph_channel_policies.last_update
4269
    )
4270
)
4271
OR (
4272
    EXCLUDED.version = 2 AND (
4273
        graph_channel_policies.block_height IS NULL
4274
        OR EXCLUDED.block_height >= graph_channel_policies.block_height
4275
    )
4276
)
4277
RETURNING id
4278
`
4279

4280
type UpsertEdgePolicyParams struct {
4281
        Version                 int16
4282
        ChannelID               int64
4283
        NodeID                  int64
4284
        Timelock                int32
4285
        FeePpm                  int64
4286
        BaseFeeMsat             int64
4287
        MinHtlcMsat             int64
4288
        LastUpdate              sql.NullInt64
4289
        Disabled                sql.NullBool
4290
        MaxHtlcMsat             sql.NullInt64
4291
        InboundBaseFeeMsat      sql.NullInt64
4292
        InboundFeeRateMilliMsat sql.NullInt64
4293
        MessageFlags            sql.NullInt16
4294
        ChannelFlags            sql.NullInt16
4295
        Signature               []byte
4296
        BlockHeight             sql.NullInt64
4297
        DisableFlags            sql.NullInt16
4298
}
4299

4300
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
4301
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
4302
                arg.Version,
×
4303
                arg.ChannelID,
×
4304
                arg.NodeID,
×
4305
                arg.Timelock,
×
4306
                arg.FeePpm,
×
4307
                arg.BaseFeeMsat,
×
4308
                arg.MinHtlcMsat,
×
4309
                arg.LastUpdate,
×
4310
                arg.Disabled,
×
4311
                arg.MaxHtlcMsat,
×
4312
                arg.InboundBaseFeeMsat,
×
4313
                arg.InboundFeeRateMilliMsat,
×
4314
                arg.MessageFlags,
×
4315
                arg.ChannelFlags,
×
4316
                arg.Signature,
×
4317
                arg.BlockHeight,
×
4318
                arg.DisableFlags,
×
4319
        )
×
4320
        var id int64
×
4321
        err := row.Scan(&id)
×
4322
        return id, err
×
4323
}
×
4324

4325
const upsertNode = `-- name: UpsertNode :one
4326
/* ─────────────────────────────────────────────
4327
   graph_nodes table queries
4328
   ───────────────────────────��─────────────────
4329
*/
4330

4331
INSERT INTO graph_nodes (
4332
    version, pub_key, alias, last_update, block_height, color, signature
4333
) VALUES (
4334
    $1, $2, $3, $4, $5, $6, $7
4335
)
4336
ON CONFLICT (pub_key, version)
4337
    -- Update the following fields if a conflict occurs on pub_key
4338
    -- and version.
4339
    DO UPDATE SET
4340
        alias = EXCLUDED.alias,
4341
        last_update = EXCLUDED.last_update,
4342
        block_height = EXCLUDED.block_height,
4343
        color = EXCLUDED.color,
4344
        signature = EXCLUDED.signature
4345
WHERE (graph_nodes.last_update IS NULL
4346
    OR EXCLUDED.last_update > graph_nodes.last_update)
4347
AND (graph_nodes.block_height IS NULL
4348
    OR EXCLUDED.block_height >= graph_nodes.block_height)
4349
RETURNING id
4350
`
4351

4352
type UpsertNodeParams struct {
4353
        Version     int16
4354
        PubKey      []byte
4355
        Alias       sql.NullString
4356
        LastUpdate  sql.NullInt64
4357
        BlockHeight sql.NullInt64
4358
        Color       sql.NullString
4359
        Signature   []byte
4360
}
4361

4362
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
4363
        row := q.db.QueryRowContext(ctx, upsertNode,
×
4364
                arg.Version,
×
4365
                arg.PubKey,
×
4366
                arg.Alias,
×
4367
                arg.LastUpdate,
×
4368
                arg.BlockHeight,
×
4369
                arg.Color,
×
4370
                arg.Signature,
×
4371
        )
×
4372
        var id int64
×
4373
        err := row.Scan(&id)
×
4374
        return id, err
×
4375
}
×
4376

4377
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
4378
/* ─────────────────────────────────────────────
4379
   graph_node_addresses table queries
4380
   ───────────────────────────────────��─────────
4381
*/
4382

4383
INSERT INTO graph_node_addresses (
4384
    node_id,
4385
    type,
4386
    address,
4387
    position
4388
) VALUES (
4389
    $1, $2, $3, $4
4390
) ON CONFLICT (node_id, type, position)
4391
    DO UPDATE SET address = EXCLUDED.address
4392
`
4393

4394
type UpsertNodeAddressParams struct {
4395
        NodeID   int64
4396
        Type     int16
4397
        Address  string
4398
        Position int32
4399
}
4400

4401
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
4402
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
4403
                arg.NodeID,
×
4404
                arg.Type,
×
4405
                arg.Address,
×
4406
                arg.Position,
×
4407
        )
×
4408
        return err
×
4409
}
×
4410

4411
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
4412
/* ─────────────────────────────────────────────
4413
   graph_node_extra_types table queries
4414
   ─────────────────────────────────────────────
4415
*/
4416

4417
INSERT INTO graph_node_extra_types (
4418
    node_id, type, value
4419
)
4420
VALUES ($1, $2, $3)
4421
ON CONFLICT (type, node_id)
4422
    -- Update the value if a conflict occurs on type
4423
    -- and node_id.
4424
    DO UPDATE SET value = EXCLUDED.value
4425
`
4426

4427
type UpsertNodeExtraTypeParams struct {
4428
        NodeID int64
4429
        Type   int64
4430
        Value  []byte
4431
}
4432

4433
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
4434
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
4435
        return err
×
4436
}
×
4437

4438
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
4439
/* ───────────────────────────���─────────────────
4440
    graph_prune_log table queries
4441
    ─────────────────────────────────────────────
4442
*/
4443

4444
INSERT INTO graph_prune_log (
4445
    block_height, block_hash
4446
) VALUES (
4447
    $1, $2
4448
)
4449
ON CONFLICT(block_height) DO UPDATE SET
4450
    block_hash = EXCLUDED.block_hash
4451
`
4452

4453
type UpsertPruneLogEntryParams struct {
4454
        BlockHeight int64
4455
        BlockHash   []byte
4456
}
4457

4458
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
4459
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
4460
        return err
×
4461
}
×
4462

4463
const upsertSourceNode = `-- name: UpsertSourceNode :one
4464
INSERT INTO graph_nodes (
4465
    version, pub_key, alias, last_update, block_height, color, signature
4466
) VALUES (
4467
    $1, $2, $3, $4, $5, $6, $7
4468
)
4469
ON CONFLICT (pub_key, version)
4470
    -- Update the following fields if a conflict occurs on pub_key
4471
    -- and version.
4472
    DO UPDATE SET
4473
        alias = EXCLUDED.alias,
4474
        last_update = EXCLUDED.last_update,
4475
        block_height = EXCLUDED.block_height,
4476
        color = EXCLUDED.color,
4477
        signature = EXCLUDED.signature
4478
WHERE graph_nodes.last_update IS NULL
4479
    OR EXCLUDED.last_update >= graph_nodes.last_update
4480
AND (graph_nodes.block_height IS NULL
4481
   OR EXCLUDED.block_height >= graph_nodes.block_height)
4482
RETURNING id
4483
`
4484

4485
type UpsertSourceNodeParams struct {
4486
        Version     int16
4487
        PubKey      []byte
4488
        Alias       sql.NullString
4489
        LastUpdate  sql.NullInt64
4490
        BlockHeight sql.NullInt64
4491
        Color       sql.NullString
4492
        Signature   []byte
4493
}
4494

4495
// We use a separate upsert for our own node since we want to be less strict
4496
// about the last_update field. For our own node, we always want to
4497
// update the record even if the last_update is the same as what we have.
4498
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
4499
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
4500
                arg.Version,
×
4501
                arg.PubKey,
×
4502
                arg.Alias,
×
4503
                arg.LastUpdate,
×
4504
                arg.BlockHeight,
×
4505
                arg.Color,
×
4506
                arg.Signature,
×
4507
        )
×
4508
        var id int64
×
4509
        err := row.Scan(&id)
×
4510
        return id, err
×
4511
}
×
4512

4513
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
4514
/* ─────────────────────────────────────────────
4515
   graph_zombie_channels table queries
4516
   ─────────────────────────────────────────────
4517
*/
4518

4519
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
4520
VALUES ($1, $2, $3, $4)
4521
ON CONFLICT (scid, version)
4522
DO UPDATE SET
4523
    -- If a conflict exists for the SCID and version pair, then we
4524
    -- update the node keys.
4525
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
4526
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
4527
`
4528

4529
type UpsertZombieChannelParams struct {
4530
        Scid     []byte
4531
        Version  int16
4532
        NodeKey1 []byte
4533
        NodeKey2 []byte
4534
}
4535

4536
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
4537
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
4538
                arg.Scid,
×
4539
                arg.Version,
×
4540
                arg.NodeKey1,
×
4541
                arg.NodeKey2,
×
4542
        )
×
4543
        return err
×
4544
}
×
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