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

lightningnetwork / lnd / 22063194374

16 Feb 2026 12:43PM UTC coverage: 65.088% (-0.03%) from 65.12%
22063194374

Pull #10572

github

web-flow
Merge 3c0af85d1 into f9e382560
Pull Request #10572: [g175] graph/db: add gossip-version plumbing for core graph APIs

131 of 281 new or added lines in 8 files covered. (46.62%)

121 existing lines in 26 files now uncovered.

139199 of 213863 relevant lines covered (65.09%)

20538.64 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 getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :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.last_update >= $2 AND cp1.last_update < $3)
1230
       OR
1231
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1232
  )
1233
  -- Pagination using compound cursor (max_update_time, id).
1234
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1235
  AND (
1236
       (CASE
1237
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1238
               THEN COALESCE(cp1.last_update, 0)
1239
           ELSE COALESCE(cp2.last_update, 0)
1240
       END > COALESCE($4, -1))
1241
       OR 
1242
       (CASE
1243
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1244
               THEN COALESCE(cp1.last_update, 0)
1245
           ELSE COALESCE(cp2.last_update, 0)
1246
       END = COALESCE($4, -1) 
1247
       AND c.id > COALESCE($5, -1))
1248
  )
1249
ORDER BY
1250
    CASE
1251
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1252
            THEN COALESCE(cp1.last_update, 0)
1253
        ELSE COALESCE(cp2.last_update, 0)
1254
    END ASC,
1255
    c.id ASC
1256
LIMIT COALESCE($6, 999999999)
1257
`
1258

1259
type GetChannelsByPolicyLastUpdateRangeParams struct {
1260
        Version        int16
1261
        StartTime      sql.NullInt64
1262
        EndTime        sql.NullInt64
1263
        LastUpdateTime sql.NullInt64
1264
        LastID         sql.NullInt64
1265
        MaxResults     interface{}
1266
}
1267

1268
type GetChannelsByPolicyLastUpdateRangeRow 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

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

1405
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1406
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,
1407
    n1.pub_key AS node1_pub_key,
1408
    n2.pub_key AS node2_pub_key
1409
FROM graph_channels c
1410
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1411
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1412
WHERE scid >= $1
1413
  AND scid < $2
1414
`
1415

1416
type GetChannelsBySCIDRangeParams struct {
1417
        StartScid []byte
1418
        EndScid   []byte
1419
}
1420

1421
type GetChannelsBySCIDRangeRow struct {
1422
        GraphChannel GraphChannel
1423
        Node1PubKey  []byte
1424
        Node2PubKey  []byte
1425
}
1426

1427
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1428
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1429
        if err != nil {
×
1430
                return nil, err
×
1431
        }
×
1432
        defer rows.Close()
×
1433
        var items []GetChannelsBySCIDRangeRow
×
1434
        for rows.Next() {
×
1435
                var i GetChannelsBySCIDRangeRow
×
1436
                if err := rows.Scan(
×
1437
                        &i.GraphChannel.ID,
×
1438
                        &i.GraphChannel.Version,
×
1439
                        &i.GraphChannel.Scid,
×
1440
                        &i.GraphChannel.NodeID1,
×
1441
                        &i.GraphChannel.NodeID2,
×
1442
                        &i.GraphChannel.Outpoint,
×
1443
                        &i.GraphChannel.Capacity,
×
1444
                        &i.GraphChannel.BitcoinKey1,
×
1445
                        &i.GraphChannel.BitcoinKey2,
×
1446
                        &i.GraphChannel.Node1Signature,
×
1447
                        &i.GraphChannel.Node2Signature,
×
1448
                        &i.GraphChannel.Bitcoin1Signature,
×
1449
                        &i.GraphChannel.Bitcoin2Signature,
×
1450
                        &i.GraphChannel.Signature,
×
1451
                        &i.GraphChannel.FundingPkScript,
×
1452
                        &i.GraphChannel.MerkleRootHash,
×
1453
                        &i.Node1PubKey,
×
1454
                        &i.Node2PubKey,
×
1455
                ); err != nil {
×
1456
                        return nil, err
×
1457
                }
×
1458
                items = append(items, i)
×
1459
        }
1460
        if err := rows.Close(); err != nil {
×
1461
                return nil, err
×
1462
        }
×
1463
        if err := rows.Err(); err != nil {
×
1464
                return nil, err
×
1465
        }
×
1466
        return items, nil
×
1467
}
1468

1469
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1470
SELECT
1471
    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,
1472
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1473
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1474

1475
    -- Policy 1
1476
    cp1.id AS policy1_id,
1477
    cp1.node_id AS policy1_node_id,
1478
    cp1.version AS policy1_version,
1479
    cp1.timelock AS policy1_timelock,
1480
    cp1.fee_ppm AS policy1_fee_ppm,
1481
    cp1.base_fee_msat AS policy1_base_fee_msat,
1482
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1483
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1484
    cp1.last_update AS policy1_last_update,
1485
    cp1.disabled AS policy1_disabled,
1486
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1487
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1488
    cp1.message_flags AS policy1_message_flags,
1489
    cp1.channel_flags AS policy1_channel_flags,
1490
    cp1.signature AS policy1_signature,
1491
    cp1.block_height AS policy1_block_height,
1492
    cp1.disable_flags AS policy1_disable_flags,
1493

1494
    -- Policy 2
1495
    cp2.id AS policy2_id,
1496
    cp2.node_id AS policy2_node_id,
1497
    cp2.version AS policy2_version,
1498
    cp2.timelock AS policy2_timelock,
1499
    cp2.fee_ppm AS policy2_fee_ppm,
1500
    cp2.base_fee_msat AS policy2_base_fee_msat,
1501
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1502
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1503
    cp2.last_update AS policy2_last_update,
1504
    cp2.disabled AS policy2_disabled,
1505
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1506
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1507
    cp2.message_flags AS policy_2_message_flags,
1508
    cp2.channel_flags AS policy_2_channel_flags,
1509
    cp2.signature AS policy2_signature,
1510
    cp2.block_height AS policy2_block_height,
1511
    cp2.disable_flags AS policy2_disable_flags
1512

1513
FROM graph_channels c
1514
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1515
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1516
    LEFT JOIN graph_channel_policies cp1
1517
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1518
    LEFT JOIN graph_channel_policies cp2
1519
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1520
WHERE
1521
    c.version = $1
1522
  AND c.scid IN (/*SLICE:scids*/?)
1523
`
1524

1525
type GetChannelsBySCIDWithPoliciesParams struct {
1526
        Version int16
1527
        Scids   [][]byte
1528
}
1529

1530
type GetChannelsBySCIDWithPoliciesRow struct {
1531
        GraphChannel                   GraphChannel
1532
        GraphNode                      GraphNode
1533
        GraphNode_2                    GraphNode
1534
        Policy1ID                      sql.NullInt64
1535
        Policy1NodeID                  sql.NullInt64
1536
        Policy1Version                 sql.NullInt16
1537
        Policy1Timelock                sql.NullInt32
1538
        Policy1FeePpm                  sql.NullInt64
1539
        Policy1BaseFeeMsat             sql.NullInt64
1540
        Policy1MinHtlcMsat             sql.NullInt64
1541
        Policy1MaxHtlcMsat             sql.NullInt64
1542
        Policy1LastUpdate              sql.NullInt64
1543
        Policy1Disabled                sql.NullBool
1544
        Policy1InboundBaseFeeMsat      sql.NullInt64
1545
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1546
        Policy1MessageFlags            sql.NullInt16
1547
        Policy1ChannelFlags            sql.NullInt16
1548
        Policy1Signature               []byte
1549
        Policy1BlockHeight             sql.NullInt64
1550
        Policy1DisableFlags            sql.NullInt16
1551
        Policy2ID                      sql.NullInt64
1552
        Policy2NodeID                  sql.NullInt64
1553
        Policy2Version                 sql.NullInt16
1554
        Policy2Timelock                sql.NullInt32
1555
        Policy2FeePpm                  sql.NullInt64
1556
        Policy2BaseFeeMsat             sql.NullInt64
1557
        Policy2MinHtlcMsat             sql.NullInt64
1558
        Policy2MaxHtlcMsat             sql.NullInt64
1559
        Policy2LastUpdate              sql.NullInt64
1560
        Policy2Disabled                sql.NullBool
1561
        Policy2InboundBaseFeeMsat      sql.NullInt64
1562
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1563
        Policy2MessageFlags            sql.NullInt16
1564
        Policy2ChannelFlags            sql.NullInt16
1565
        Policy2Signature               []byte
1566
        Policy2BlockHeight             sql.NullInt64
1567
        Policy2DisableFlags            sql.NullInt16
1568
}
1569

1570
func (q *Queries) GetChannelsBySCIDWithPolicies(ctx context.Context, arg GetChannelsBySCIDWithPoliciesParams) ([]GetChannelsBySCIDWithPoliciesRow, error) {
×
1571
        query := getChannelsBySCIDWithPolicies
×
1572
        var queryParams []interface{}
×
1573
        queryParams = append(queryParams, arg.Version)
×
1574
        if len(arg.Scids) > 0 {
×
1575
                for _, v := range arg.Scids {
×
1576
                        queryParams = append(queryParams, v)
×
1577
                }
×
1578
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1579
        } else {
×
1580
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1581
        }
×
1582
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1583
        if err != nil {
×
1584
                return nil, err
×
1585
        }
×
1586
        defer rows.Close()
×
1587
        var items []GetChannelsBySCIDWithPoliciesRow
×
1588
        for rows.Next() {
×
1589
                var i GetChannelsBySCIDWithPoliciesRow
×
1590
                if err := rows.Scan(
×
1591
                        &i.GraphChannel.ID,
×
1592
                        &i.GraphChannel.Version,
×
1593
                        &i.GraphChannel.Scid,
×
1594
                        &i.GraphChannel.NodeID1,
×
1595
                        &i.GraphChannel.NodeID2,
×
1596
                        &i.GraphChannel.Outpoint,
×
1597
                        &i.GraphChannel.Capacity,
×
1598
                        &i.GraphChannel.BitcoinKey1,
×
1599
                        &i.GraphChannel.BitcoinKey2,
×
1600
                        &i.GraphChannel.Node1Signature,
×
1601
                        &i.GraphChannel.Node2Signature,
×
1602
                        &i.GraphChannel.Bitcoin1Signature,
×
1603
                        &i.GraphChannel.Bitcoin2Signature,
×
1604
                        &i.GraphChannel.Signature,
×
1605
                        &i.GraphChannel.FundingPkScript,
×
1606
                        &i.GraphChannel.MerkleRootHash,
×
1607
                        &i.GraphNode.ID,
×
1608
                        &i.GraphNode.Version,
×
1609
                        &i.GraphNode.PubKey,
×
1610
                        &i.GraphNode.Alias,
×
1611
                        &i.GraphNode.LastUpdate,
×
1612
                        &i.GraphNode.Color,
×
1613
                        &i.GraphNode.Signature,
×
1614
                        &i.GraphNode.BlockHeight,
×
1615
                        &i.GraphNode_2.ID,
×
1616
                        &i.GraphNode_2.Version,
×
1617
                        &i.GraphNode_2.PubKey,
×
1618
                        &i.GraphNode_2.Alias,
×
1619
                        &i.GraphNode_2.LastUpdate,
×
1620
                        &i.GraphNode_2.Color,
×
1621
                        &i.GraphNode_2.Signature,
×
1622
                        &i.GraphNode_2.BlockHeight,
×
1623
                        &i.Policy1ID,
×
1624
                        &i.Policy1NodeID,
×
1625
                        &i.Policy1Version,
×
1626
                        &i.Policy1Timelock,
×
1627
                        &i.Policy1FeePpm,
×
1628
                        &i.Policy1BaseFeeMsat,
×
1629
                        &i.Policy1MinHtlcMsat,
×
1630
                        &i.Policy1MaxHtlcMsat,
×
1631
                        &i.Policy1LastUpdate,
×
1632
                        &i.Policy1Disabled,
×
1633
                        &i.Policy1InboundBaseFeeMsat,
×
1634
                        &i.Policy1InboundFeeRateMilliMsat,
×
1635
                        &i.Policy1MessageFlags,
×
1636
                        &i.Policy1ChannelFlags,
×
1637
                        &i.Policy1Signature,
×
1638
                        &i.Policy1BlockHeight,
×
1639
                        &i.Policy1DisableFlags,
×
1640
                        &i.Policy2ID,
×
1641
                        &i.Policy2NodeID,
×
1642
                        &i.Policy2Version,
×
1643
                        &i.Policy2Timelock,
×
1644
                        &i.Policy2FeePpm,
×
1645
                        &i.Policy2BaseFeeMsat,
×
1646
                        &i.Policy2MinHtlcMsat,
×
1647
                        &i.Policy2MaxHtlcMsat,
×
1648
                        &i.Policy2LastUpdate,
×
1649
                        &i.Policy2Disabled,
×
1650
                        &i.Policy2InboundBaseFeeMsat,
×
1651
                        &i.Policy2InboundFeeRateMilliMsat,
×
1652
                        &i.Policy2MessageFlags,
×
1653
                        &i.Policy2ChannelFlags,
×
1654
                        &i.Policy2Signature,
×
1655
                        &i.Policy2BlockHeight,
×
1656
                        &i.Policy2DisableFlags,
×
1657
                ); err != nil {
×
1658
                        return nil, err
×
1659
                }
×
1660
                items = append(items, i)
×
1661
        }
1662
        if err := rows.Close(); err != nil {
×
1663
                return nil, err
×
1664
        }
×
1665
        if err := rows.Err(); err != nil {
×
1666
                return nil, err
×
1667
        }
×
1668
        return items, nil
×
1669
}
1670

1671
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1672
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
1673
WHERE version = $1
1674
  AND scid IN (/*SLICE:scids*/?)
1675
`
1676

1677
type GetChannelsBySCIDsParams struct {
1678
        Version int16
1679
        Scids   [][]byte
1680
}
1681

1682
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
1683
        query := getChannelsBySCIDs
×
1684
        var queryParams []interface{}
×
1685
        queryParams = append(queryParams, arg.Version)
×
1686
        if len(arg.Scids) > 0 {
×
1687
                for _, v := range arg.Scids {
×
1688
                        queryParams = append(queryParams, v)
×
1689
                }
×
1690
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1691
        } else {
×
1692
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1693
        }
×
1694
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1695
        if err != nil {
×
1696
                return nil, err
×
1697
        }
×
1698
        defer rows.Close()
×
1699
        var items []GraphChannel
×
1700
        for rows.Next() {
×
1701
                var i GraphChannel
×
1702
                if err := rows.Scan(
×
1703
                        &i.ID,
×
1704
                        &i.Version,
×
1705
                        &i.Scid,
×
1706
                        &i.NodeID1,
×
1707
                        &i.NodeID2,
×
1708
                        &i.Outpoint,
×
1709
                        &i.Capacity,
×
1710
                        &i.BitcoinKey1,
×
1711
                        &i.BitcoinKey2,
×
1712
                        &i.Node1Signature,
×
1713
                        &i.Node2Signature,
×
1714
                        &i.Bitcoin1Signature,
×
1715
                        &i.Bitcoin2Signature,
×
1716
                        &i.Signature,
×
1717
                        &i.FundingPkScript,
×
1718
                        &i.MerkleRootHash,
×
1719
                ); err != nil {
×
1720
                        return nil, err
×
1721
                }
×
1722
                items = append(items, i)
×
1723
        }
1724
        if err := rows.Close(); err != nil {
×
1725
                return nil, err
×
1726
        }
×
1727
        if err := rows.Err(); err != nil {
×
1728
                return nil, err
×
1729
        }
×
1730
        return items, nil
×
1731
}
1732

1733
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1734
SELECT scid
1735
FROM graph_closed_scids
1736
WHERE scid IN (/*SLICE:scids*/?)
1737
`
1738

1739
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
1740
        query := getClosedChannelsSCIDs
×
1741
        var queryParams []interface{}
×
1742
        if len(scids) > 0 {
×
1743
                for _, v := range scids {
×
1744
                        queryParams = append(queryParams, v)
×
1745
                }
×
1746
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
1747
        } else {
×
1748
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1749
        }
×
1750
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1751
        if err != nil {
×
1752
                return nil, err
×
1753
        }
×
1754
        defer rows.Close()
×
1755
        var items [][]byte
×
1756
        for rows.Next() {
×
1757
                var scid []byte
×
1758
                if err := rows.Scan(&scid); err != nil {
×
1759
                        return nil, err
×
1760
                }
×
1761
                items = append(items, scid)
×
1762
        }
1763
        if err := rows.Close(); err != nil {
×
1764
                return nil, err
×
1765
        }
×
1766
        if err := rows.Err(); err != nil {
×
1767
                return nil, err
×
1768
        }
×
1769
        return items, nil
×
1770
}
1771

1772
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1773
SELECT node_id, type, value
1774
FROM graph_node_extra_types
1775
WHERE node_id = $1
1776
`
1777

1778
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
1779
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
1780
        if err != nil {
×
1781
                return nil, err
×
1782
        }
×
1783
        defer rows.Close()
×
1784
        var items []GraphNodeExtraType
×
1785
        for rows.Next() {
×
1786
                var i GraphNodeExtraType
×
1787
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
1788
                        return nil, err
×
1789
                }
×
1790
                items = append(items, i)
×
1791
        }
1792
        if err := rows.Close(); err != nil {
×
1793
                return nil, err
×
1794
        }
×
1795
        if err := rows.Err(); err != nil {
×
1796
                return nil, err
×
1797
        }
×
1798
        return items, nil
×
1799
}
1800

1801
const getNodeAddresses = `-- name: GetNodeAddresses :many
1802
SELECT type, address
1803
FROM graph_node_addresses
1804
WHERE node_id = $1
1805
ORDER BY type ASC, position ASC
1806
`
1807

1808
type GetNodeAddressesRow struct {
1809
        Type    int16
1810
        Address string
1811
}
1812

1813
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
1814
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
1815
        if err != nil {
×
1816
                return nil, err
×
1817
        }
×
1818
        defer rows.Close()
×
1819
        var items []GetNodeAddressesRow
×
1820
        for rows.Next() {
×
1821
                var i GetNodeAddressesRow
×
1822
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
1823
                        return nil, err
×
1824
                }
×
1825
                items = append(items, i)
×
1826
        }
1827
        if err := rows.Close(); err != nil {
×
1828
                return nil, err
×
1829
        }
×
1830
        if err := rows.Err(); err != nil {
×
1831
                return nil, err
×
1832
        }
×
1833
        return items, nil
×
1834
}
1835

1836
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
1837
SELECT node_id, type, position, address
1838
FROM graph_node_addresses
1839
WHERE node_id IN (/*SLICE:ids*/?)
1840
ORDER BY node_id, type, position
1841
`
1842

1843
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
×
1844
        query := getNodeAddressesBatch
×
1845
        var queryParams []interface{}
×
1846
        if len(ids) > 0 {
×
1847
                for _, v := range ids {
×
1848
                        queryParams = append(queryParams, v)
×
1849
                }
×
1850
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1851
        } else {
×
1852
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1853
        }
×
1854
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1855
        if err != nil {
×
1856
                return nil, err
×
1857
        }
×
1858
        defer rows.Close()
×
1859
        var items []GraphNodeAddress
×
1860
        for rows.Next() {
×
1861
                var i GraphNodeAddress
×
1862
                if err := rows.Scan(
×
1863
                        &i.NodeID,
×
1864
                        &i.Type,
×
1865
                        &i.Position,
×
1866
                        &i.Address,
×
1867
                ); err != nil {
×
1868
                        return nil, err
×
1869
                }
×
1870
                items = append(items, i)
×
1871
        }
1872
        if err := rows.Close(); err != nil {
×
1873
                return nil, err
×
1874
        }
×
1875
        if err := rows.Err(); err != nil {
×
1876
                return nil, err
×
1877
        }
×
1878
        return items, nil
×
1879
}
1880

1881
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1882
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
1883
FROM graph_nodes
1884
WHERE pub_key = $1
1885
  AND version = $2
1886
`
1887

1888
type GetNodeByPubKeyParams struct {
1889
        PubKey  []byte
1890
        Version int16
1891
}
1892

1893
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
1894
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
1895
        var i GraphNode
×
1896
        err := row.Scan(
×
1897
                &i.ID,
×
1898
                &i.Version,
×
1899
                &i.PubKey,
×
1900
                &i.Alias,
×
1901
                &i.LastUpdate,
×
1902
                &i.Color,
×
1903
                &i.Signature,
×
1904
                &i.BlockHeight,
×
1905
        )
×
1906
        return i, err
×
1907
}
×
1908

1909
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
1910
SELECT node_id, type, value
1911
FROM graph_node_extra_types
1912
WHERE node_id IN (/*SLICE:ids*/?)
1913
ORDER BY node_id, type
1914
`
1915

1916
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
1917
        query := getNodeExtraTypesBatch
×
1918
        var queryParams []interface{}
×
1919
        if len(ids) > 0 {
×
1920
                for _, v := range ids {
×
1921
                        queryParams = append(queryParams, v)
×
1922
                }
×
1923
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1924
        } else {
×
1925
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1926
        }
×
1927
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1928
        if err != nil {
×
1929
                return nil, err
×
1930
        }
×
1931
        defer rows.Close()
×
1932
        var items []GraphNodeExtraType
×
1933
        for rows.Next() {
×
1934
                var i GraphNodeExtraType
×
1935
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
1936
                        return nil, err
×
1937
                }
×
1938
                items = append(items, i)
×
1939
        }
1940
        if err := rows.Close(); err != nil {
×
1941
                return nil, err
×
1942
        }
×
1943
        if err := rows.Err(); err != nil {
×
1944
                return nil, err
×
1945
        }
×
1946
        return items, nil
×
1947
}
1948

1949
const getNodeFeatures = `-- name: GetNodeFeatures :many
1950
SELECT node_id, feature_bit
1951
FROM graph_node_features
1952
WHERE node_id = $1
1953
`
1954

1955
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
1956
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
1957
        if err != nil {
×
1958
                return nil, err
×
1959
        }
×
1960
        defer rows.Close()
×
1961
        var items []GraphNodeFeature
×
1962
        for rows.Next() {
×
1963
                var i GraphNodeFeature
×
1964
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
1965
                        return nil, err
×
1966
                }
×
1967
                items = append(items, i)
×
1968
        }
1969
        if err := rows.Close(); err != nil {
×
1970
                return nil, err
×
1971
        }
×
1972
        if err := rows.Err(); err != nil {
×
1973
                return nil, err
×
1974
        }
×
1975
        return items, nil
×
1976
}
1977

1978
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
1979
SELECT node_id, feature_bit
1980
FROM graph_node_features
1981
WHERE node_id IN (/*SLICE:ids*/?)
1982
ORDER BY node_id, feature_bit
1983
`
1984

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

2018
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
2019
SELECT f.feature_bit
2020
FROM graph_nodes n
2021
    JOIN graph_node_features f ON f.node_id = n.id
2022
WHERE n.pub_key = $1
2023
  AND n.version = $2
2024
`
2025

2026
type GetNodeFeaturesByPubKeyParams struct {
2027
        PubKey  []byte
2028
        Version int16
2029
}
2030

2031
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
2032
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
2033
        if err != nil {
×
2034
                return nil, err
×
2035
        }
×
2036
        defer rows.Close()
×
2037
        var items []int32
×
2038
        for rows.Next() {
×
2039
                var feature_bit int32
×
2040
                if err := rows.Scan(&feature_bit); err != nil {
×
2041
                        return nil, err
×
2042
                }
×
2043
                items = append(items, feature_bit)
×
2044
        }
2045
        if err := rows.Close(); err != nil {
×
2046
                return nil, err
×
2047
        }
×
2048
        if err := rows.Err(); err != nil {
×
2049
                return nil, err
×
2050
        }
×
2051
        return items, nil
×
2052
}
2053

2054
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
2055
SELECT id
2056
FROM graph_nodes
2057
WHERE pub_key = $1
2058
  AND version = $2
2059
`
2060

2061
type GetNodeIDByPubKeyParams struct {
2062
        PubKey  []byte
2063
        Version int16
2064
}
2065

2066
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
2067
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
2068
        var id int64
×
2069
        err := row.Scan(&id)
×
2070
        return id, err
×
2071
}
×
2072

2073
const getNodesByIDs = `-- name: GetNodesByIDs :many
2074
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2075
FROM graph_nodes
2076
WHERE id IN (/*SLICE:ids*/?)
2077
`
2078

2079
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
2080
        query := getNodesByIDs
×
2081
        var queryParams []interface{}
×
2082
        if len(ids) > 0 {
×
2083
                for _, v := range ids {
×
2084
                        queryParams = append(queryParams, v)
×
2085
                }
×
2086
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2087
        } else {
×
2088
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2089
        }
×
2090
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2091
        if err != nil {
×
2092
                return nil, err
×
2093
        }
×
2094
        defer rows.Close()
×
2095
        var items []GraphNode
×
2096
        for rows.Next() {
×
2097
                var i GraphNode
×
2098
                if err := rows.Scan(
×
2099
                        &i.ID,
×
2100
                        &i.Version,
×
2101
                        &i.PubKey,
×
2102
                        &i.Alias,
×
2103
                        &i.LastUpdate,
×
2104
                        &i.Color,
×
2105
                        &i.Signature,
×
2106
                        &i.BlockHeight,
×
2107
                ); err != nil {
×
2108
                        return nil, err
×
2109
                }
×
2110
                items = append(items, i)
×
2111
        }
2112
        if err := rows.Close(); err != nil {
×
2113
                return nil, err
×
2114
        }
×
2115
        if err := rows.Err(); err != nil {
×
2116
                return nil, err
×
2117
        }
×
2118
        return items, nil
×
2119
}
2120

2121
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
2122
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2123
FROM graph_nodes
2124
WHERE last_update >= $1
2125
  AND last_update <= $2
2126
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2127
  -- This ensures stable ordering and allows us to resume from where we left off.
2128
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2129
  AND (
2130
    -- Include rows with last_update greater than cursor (or all rows if cursor is -1)
2131
    last_update > COALESCE($3, -1)
2132
    OR 
2133
    -- For rows with same last_update, use pub_key as tiebreaker
2134
    (last_update = COALESCE($3, -1) 
2135
     AND pub_key > $4)
2136
  )
2137
  -- Optional filter for public nodes only
2138
  AND (
2139
    -- If only_public is false or not provided, include all nodes
2140
    COALESCE($5, FALSE) IS FALSE
2141
    OR 
2142
    -- For V1 protocol, a node is public if it has at least one public channel.
2143
    -- A public channel has bitcoin_1_signature set (channel announcement received).
2144
    EXISTS (
2145
      SELECT 1
2146
      FROM graph_channels c
2147
      WHERE c.version = 1
2148
        AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2149
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2150
    )
2151
  )
2152
ORDER BY last_update ASC, pub_key ASC
2153
LIMIT COALESCE($6, 999999999)
2154
`
2155

2156
type GetNodesByLastUpdateRangeParams struct {
2157
        StartTime  sql.NullInt64
2158
        EndTime    sql.NullInt64
2159
        LastUpdate sql.NullInt64
2160
        LastPubKey []byte
2161
        OnlyPublic interface{}
2162
        MaxResults interface{}
2163
}
2164

2165
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2166
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
2167
                arg.StartTime,
×
2168
                arg.EndTime,
×
2169
                arg.LastUpdate,
×
2170
                arg.LastPubKey,
×
2171
                arg.OnlyPublic,
×
2172
                arg.MaxResults,
×
2173
        )
×
2174
        if err != nil {
×
2175
                return nil, err
×
2176
        }
×
2177
        defer rows.Close()
×
2178
        var items []GraphNode
×
2179
        for rows.Next() {
×
2180
                var i GraphNode
×
2181
                if err := rows.Scan(
×
2182
                        &i.ID,
×
2183
                        &i.Version,
×
2184
                        &i.PubKey,
×
2185
                        &i.Alias,
×
2186
                        &i.LastUpdate,
×
2187
                        &i.Color,
×
2188
                        &i.Signature,
×
2189
                        &i.BlockHeight,
×
2190
                ); err != nil {
×
2191
                        return nil, err
×
2192
                }
×
2193
                items = append(items, i)
×
2194
        }
2195
        if err := rows.Close(); err != nil {
×
2196
                return nil, err
×
2197
        }
×
2198
        if err := rows.Err(); err != nil {
×
2199
                return nil, err
×
2200
        }
×
2201
        return items, nil
×
2202
}
2203

2204
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2205
SELECT block_height, block_hash
2206
FROM graph_prune_log
2207
WHERE block_height
2208
   IN (/*SLICE:heights*/?)
2209
`
2210

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

2244
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2245
SELECT block_hash
2246
FROM graph_prune_log
2247
WHERE block_height = $1
2248
`
2249

2250
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2251
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2252
        var block_hash []byte
×
2253
        err := row.Scan(&block_hash)
×
2254
        return block_hash, err
×
2255
}
×
2256

2257
const getPruneTip = `-- name: GetPruneTip :one
2258
SELECT block_height, block_hash
2259
FROM graph_prune_log
2260
ORDER BY block_height DESC
2261
LIMIT 1
2262
`
2263

2264
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2265
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2266
        var i GraphPruneLog
×
2267
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2268
        return i, err
×
2269
}
×
2270

2271
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2272
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
2273
FROM graph_channels
2274
WHERE COALESCE(length(node_1_signature), 0) > 0
2275
  AND scid >= $1
2276
  AND scid < $2
2277
`
2278

2279
type GetPublicV1ChannelsBySCIDParams struct {
2280
        StartScid []byte
2281
        EndScid   []byte
2282
}
2283

2284
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2285
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2286
        if err != nil {
×
2287
                return nil, err
×
2288
        }
×
2289
        defer rows.Close()
×
2290
        var items []GraphChannel
×
2291
        for rows.Next() {
×
2292
                var i GraphChannel
×
2293
                if err := rows.Scan(
×
2294
                        &i.ID,
×
2295
                        &i.Version,
×
2296
                        &i.Scid,
×
2297
                        &i.NodeID1,
×
2298
                        &i.NodeID2,
×
2299
                        &i.Outpoint,
×
2300
                        &i.Capacity,
×
2301
                        &i.BitcoinKey1,
×
2302
                        &i.BitcoinKey2,
×
2303
                        &i.Node1Signature,
×
2304
                        &i.Node2Signature,
×
2305
                        &i.Bitcoin1Signature,
×
2306
                        &i.Bitcoin2Signature,
×
2307
                        &i.Signature,
×
2308
                        &i.FundingPkScript,
×
2309
                        &i.MerkleRootHash,
×
2310
                ); err != nil {
×
2311
                        return nil, err
×
2312
                }
×
2313
                items = append(items, i)
×
2314
        }
2315
        if err := rows.Close(); err != nil {
×
2316
                return nil, err
×
2317
        }
×
2318
        if err := rows.Err(); err != nil {
×
2319
                return nil, err
×
2320
        }
×
2321
        return items, nil
×
2322
}
2323

2324
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2325
SELECT scid from graph_channels
2326
WHERE outpoint = $1 AND version = $2
2327
`
2328

2329
type GetSCIDByOutpointParams struct {
2330
        Outpoint string
2331
        Version  int16
2332
}
2333

2334
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2335
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2336
        var scid []byte
×
2337
        err := row.Scan(&scid)
×
2338
        return scid, err
×
2339
}
×
2340

2341
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2342
SELECT sn.node_id, n.pub_key
2343
FROM graph_source_nodes sn
2344
    JOIN graph_nodes n ON sn.node_id = n.id
2345
WHERE n.version = $1
2346
`
2347

2348
type GetSourceNodesByVersionRow struct {
2349
        NodeID int64
2350
        PubKey []byte
2351
}
2352

2353
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2354
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2355
        if err != nil {
×
2356
                return nil, err
×
2357
        }
×
2358
        defer rows.Close()
×
2359
        var items []GetSourceNodesByVersionRow
×
2360
        for rows.Next() {
×
2361
                var i GetSourceNodesByVersionRow
×
2362
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
2363
                        return nil, err
×
2364
                }
×
2365
                items = append(items, i)
×
2366
        }
2367
        if err := rows.Close(); err != nil {
×
2368
                return nil, err
×
2369
        }
×
2370
        if err := rows.Err(); err != nil {
×
2371
                return nil, err
×
2372
        }
×
2373
        return items, nil
×
2374
}
2375

2376
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2377
SELECT c.scid
2378
FROM graph_channels c
2379
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2380
WHERE cp.disabled = true
2381
AND c.version = 1
2382
GROUP BY c.scid
2383
HAVING COUNT(*) > 1
2384
`
2385

2386
// NOTE: this is V1 specific since for V1, disabled is a
2387
// simple, single boolean. The proposed V2 policy
2388
// structure will have a more complex disabled bit vector
2389
// and so the query for V2 may differ.
2390
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2391
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2392
        if err != nil {
×
2393
                return nil, err
×
2394
        }
×
2395
        defer rows.Close()
×
2396
        var items [][]byte
×
2397
        for rows.Next() {
×
2398
                var scid []byte
×
2399
                if err := rows.Scan(&scid); err != nil {
×
2400
                        return nil, err
×
2401
                }
×
2402
                items = append(items, scid)
×
2403
        }
2404
        if err := rows.Close(); err != nil {
×
2405
                return nil, err
×
2406
        }
×
2407
        if err := rows.Err(); err != nil {
×
2408
                return nil, err
×
2409
        }
×
2410
        return items, nil
×
2411
}
2412

2413
const getV2DisabledSCIDs = `-- name: GetV2DisabledSCIDs :many
2414
SELECT c.scid
2415
FROM graph_channels c
2416
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2417
WHERE COALESCE(cp.disable_flags, 0) != 0
2418
AND c.version = 2
2419
GROUP BY c.scid
2420
HAVING COUNT(*) > 1
2421
`
2422

2423
// NOTE: this is V2 specific since V2 uses a disable flag
2424
// bit vector instead of a single boolean.
NEW
2425
func (q *Queries) GetV2DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
NEW
2426
        rows, err := q.db.QueryContext(ctx, getV2DisabledSCIDs)
×
NEW
2427
        if err != nil {
×
NEW
2428
                return nil, err
×
NEW
2429
        }
×
NEW
2430
        defer rows.Close()
×
NEW
2431
        var items [][]byte
×
NEW
2432
        for rows.Next() {
×
NEW
2433
                var scid []byte
×
NEW
2434
                if err := rows.Scan(&scid); err != nil {
×
NEW
2435
                        return nil, err
×
NEW
2436
                }
×
NEW
2437
                items = append(items, scid)
×
2438
        }
NEW
2439
        if err := rows.Close(); err != nil {
×
NEW
2440
                return nil, err
×
NEW
2441
        }
×
NEW
2442
        if err := rows.Err(); err != nil {
×
NEW
2443
                return nil, err
×
NEW
2444
        }
×
NEW
2445
        return items, nil
×
2446
}
2447

2448
const getZombieChannel = `-- name: GetZombieChannel :one
2449
SELECT scid, version, node_key_1, node_key_2
2450
FROM graph_zombie_channels
2451
WHERE scid = $1
2452
AND version = $2
2453
`
2454

2455
type GetZombieChannelParams struct {
2456
        Scid    []byte
2457
        Version int16
2458
}
2459

2460
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2461
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2462
        var i GraphZombieChannel
×
2463
        err := row.Scan(
×
2464
                &i.Scid,
×
2465
                &i.Version,
×
2466
                &i.NodeKey1,
×
2467
                &i.NodeKey2,
×
2468
        )
×
2469
        return i, err
×
2470
}
×
2471

2472
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2473
SELECT scid, version, node_key_1, node_key_2
2474
FROM graph_zombie_channels
2475
WHERE version = $1
2476
  AND scid IN (/*SLICE:scids*/?)
2477
`
2478

2479
type GetZombieChannelsSCIDsParams struct {
2480
        Version int16
2481
        Scids   [][]byte
2482
}
2483

2484
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2485
        query := getZombieChannelsSCIDs
×
2486
        var queryParams []interface{}
×
2487
        queryParams = append(queryParams, arg.Version)
×
2488
        if len(arg.Scids) > 0 {
×
2489
                for _, v := range arg.Scids {
×
2490
                        queryParams = append(queryParams, v)
×
2491
                }
×
2492
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2493
        } else {
×
2494
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2495
        }
×
2496
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2497
        if err != nil {
×
2498
                return nil, err
×
2499
        }
×
2500
        defer rows.Close()
×
2501
        var items []GraphZombieChannel
×
2502
        for rows.Next() {
×
2503
                var i GraphZombieChannel
×
2504
                if err := rows.Scan(
×
2505
                        &i.Scid,
×
2506
                        &i.Version,
×
2507
                        &i.NodeKey1,
×
2508
                        &i.NodeKey2,
×
2509
                ); err != nil {
×
2510
                        return nil, err
×
2511
                }
×
2512
                items = append(items, i)
×
2513
        }
2514
        if err := rows.Close(); err != nil {
×
2515
                return nil, err
×
2516
        }
×
2517
        if err := rows.Err(); err != nil {
×
2518
                return nil, err
×
2519
        }
×
2520
        return items, nil
×
2521
}
2522

2523
const highestSCID = `-- name: HighestSCID :one
2524
SELECT scid
2525
FROM graph_channels
2526
WHERE version = $1
2527
ORDER BY scid DESC
2528
LIMIT 1
2529
`
2530

2531
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2532
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2533
        var scid []byte
×
2534
        err := row.Scan(&scid)
×
2535
        return scid, err
×
2536
}
×
2537

2538
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2539
/* ─────────────────────────────────────────────
2540
   graph_channel_features table queries
2541
   ─────────────────────────────────────────────
2542
*/
2543

2544
INSERT INTO graph_channel_features (
2545
    channel_id, feature_bit
2546
) VALUES (
2547
    $1, $2
2548
) ON CONFLICT (channel_id, feature_bit)
2549
    -- Do nothing if the channel_id and feature_bit already exist.
2550
    DO NOTHING
2551
`
2552

2553
type InsertChannelFeatureParams struct {
2554
        ChannelID  int64
2555
        FeatureBit int32
2556
}
2557

2558
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2559
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2560
        return err
×
2561
}
×
2562

2563
const insertChannelMig = `-- name: InsertChannelMig :one
2564
INSERT INTO graph_channels (
2565
    version, scid, node_id_1, node_id_2,
2566
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2567
    node_1_signature, node_2_signature, bitcoin_1_signature,
2568
    bitcoin_2_signature
2569
) VALUES (
2570
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2571
) ON CONFLICT (scid, version)
2572
    -- If a conflict occurs, we have already migrated this channel. However, we
2573
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2574
    -- otherwise, the "RETURNING id" part does not work.
2575
    DO UPDATE SET
2576
        node_id_1 = EXCLUDED.node_id_1,
2577
        node_id_2 = EXCLUDED.node_id_2,
2578
        outpoint = EXCLUDED.outpoint,
2579
        capacity = EXCLUDED.capacity,
2580
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2581
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2582
        node_1_signature = EXCLUDED.node_1_signature,
2583
        node_2_signature = EXCLUDED.node_2_signature,
2584
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2585
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2586
RETURNING id
2587
`
2588

2589
type InsertChannelMigParams struct {
2590
        Version           int16
2591
        Scid              []byte
2592
        NodeID1           int64
2593
        NodeID2           int64
2594
        Outpoint          string
2595
        Capacity          sql.NullInt64
2596
        BitcoinKey1       []byte
2597
        BitcoinKey2       []byte
2598
        Node1Signature    []byte
2599
        Node2Signature    []byte
2600
        Bitcoin1Signature []byte
2601
        Bitcoin2Signature []byte
2602
}
2603

2604
// NOTE: This query is only meant to be used by the graph SQL migration since
2605
// for that migration, in order to be retry-safe, we don't want to error out if
2606
// we re-insert the same channel again (which would error if the normal
2607
// CreateChannel query is used because of the uniqueness constraint on the scid
2608
// and version columns).
2609
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
2610
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
2611
                arg.Version,
×
2612
                arg.Scid,
×
2613
                arg.NodeID1,
×
2614
                arg.NodeID2,
×
2615
                arg.Outpoint,
×
2616
                arg.Capacity,
×
2617
                arg.BitcoinKey1,
×
2618
                arg.BitcoinKey2,
×
2619
                arg.Node1Signature,
×
2620
                arg.Node2Signature,
×
2621
                arg.Bitcoin1Signature,
×
2622
                arg.Bitcoin2Signature,
×
2623
        )
×
2624
        var id int64
×
2625
        err := row.Scan(&id)
×
2626
        return id, err
×
2627
}
×
2628

2629
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2630
/* ─────────────────────────────────────────────
2631
   graph_closed_scid table queries
2632
   ────────────────────────────────────────────-
2633
*/
2634

2635
INSERT INTO graph_closed_scids (scid)
2636
VALUES ($1)
2637
ON CONFLICT (scid) DO NOTHING
2638
`
2639

2640
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
2641
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
2642
        return err
×
2643
}
×
2644

2645
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
2646
INSERT INTO graph_channel_policies (
2647
    version, channel_id, node_id, timelock, fee_ppm,
2648
    base_fee_msat, min_htlc_msat, last_update, disabled,
2649
    max_htlc_msat, inbound_base_fee_msat,
2650
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2651
    signature
2652
) VALUES  (
2653
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2654
)
2655
ON CONFLICT (channel_id, node_id, version)
2656
    -- If a conflict occurs, we have already migrated this policy. However, we
2657
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2658
    -- otherwise, the "RETURNING id" part does not work.
2659
    DO UPDATE SET
2660
        timelock = EXCLUDED.timelock,
2661
        fee_ppm = EXCLUDED.fee_ppm,
2662
        base_fee_msat = EXCLUDED.base_fee_msat,
2663
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2664
        last_update = EXCLUDED.last_update,
2665
        disabled = EXCLUDED.disabled,
2666
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2667
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2668
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2669
        message_flags = EXCLUDED.message_flags,
2670
        channel_flags = EXCLUDED.channel_flags,
2671
        signature = EXCLUDED.signature
2672
RETURNING id
2673
`
2674

2675
type InsertEdgePolicyMigParams struct {
2676
        Version                 int16
2677
        ChannelID               int64
2678
        NodeID                  int64
2679
        Timelock                int32
2680
        FeePpm                  int64
2681
        BaseFeeMsat             int64
2682
        MinHtlcMsat             int64
2683
        LastUpdate              sql.NullInt64
2684
        Disabled                sql.NullBool
2685
        MaxHtlcMsat             sql.NullInt64
2686
        InboundBaseFeeMsat      sql.NullInt64
2687
        InboundFeeRateMilliMsat sql.NullInt64
2688
        MessageFlags            sql.NullInt16
2689
        ChannelFlags            sql.NullInt16
2690
        Signature               []byte
2691
}
2692

2693
// NOTE: This query is only meant to be used by the graph SQL migration since
2694
// for that migration, in order to be retry-safe, we don't want to error out if
2695
// we re-insert the same policy (which would error if the normal
2696
// UpsertEdgePolicy query is used because of the constraint in that query that
2697
// requires a policy update to have a newer last_update than the existing one).
2698
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
2699
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
2700
                arg.Version,
×
2701
                arg.ChannelID,
×
2702
                arg.NodeID,
×
2703
                arg.Timelock,
×
2704
                arg.FeePpm,
×
2705
                arg.BaseFeeMsat,
×
2706
                arg.MinHtlcMsat,
×
2707
                arg.LastUpdate,
×
2708
                arg.Disabled,
×
2709
                arg.MaxHtlcMsat,
×
2710
                arg.InboundBaseFeeMsat,
×
2711
                arg.InboundFeeRateMilliMsat,
×
2712
                arg.MessageFlags,
×
2713
                arg.ChannelFlags,
×
2714
                arg.Signature,
×
2715
        )
×
2716
        var id int64
×
2717
        err := row.Scan(&id)
×
2718
        return id, err
×
2719
}
×
2720

2721
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2722
/* ─────────────────────────────────────────────
2723
   graph_node_features table queries
2724
   ─────────────────────────────────────────────
2725
*/
2726

2727
INSERT INTO graph_node_features (
2728
    node_id, feature_bit
2729
) VALUES (
2730
    $1, $2
2731
) ON CONFLICT (node_id, feature_bit)
2732
    -- Do nothing if the feature already exists for the node.
2733
    DO NOTHING
2734
`
2735

2736
type InsertNodeFeatureParams struct {
2737
        NodeID     int64
2738
        FeatureBit int32
2739
}
2740

2741
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
2742
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
2743
        return err
×
2744
}
×
2745

2746
const insertNodeMig = `-- name: InsertNodeMig :one
2747
/* ─────────────────────────────────────────────
2748
   Migration specific queries
2749

2750
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2751
   dedicated to the migration that requires it, and so we can then remove
2752
   it from the main set of "live" queries that the code-base has access to.
2753
   ────────────────────────────────────────────-
2754
*/
2755

2756
INSERT INTO graph_nodes (
2757
    version, pub_key, alias, last_update, color, signature
2758
) VALUES (
2759
    $1, $2, $3, $4, $5, $6
2760
)
2761
ON CONFLICT (pub_key, version)
2762
    -- If a conflict occurs, we have already migrated this node. However, we
2763
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2764
    -- otherwise, the "RETURNING id" part does not work.
2765
    DO UPDATE SET
2766
        alias = EXCLUDED.alias,
2767
        last_update = EXCLUDED.last_update,
2768
        color = EXCLUDED.color,
2769
        signature = EXCLUDED.signature
2770
RETURNING id
2771
`
2772

2773
type InsertNodeMigParams struct {
2774
        Version    int16
2775
        PubKey     []byte
2776
        Alias      sql.NullString
2777
        LastUpdate sql.NullInt64
2778
        Color      sql.NullString
2779
        Signature  []byte
2780
}
2781

2782
// NOTE: This query is only meant to be used by the graph SQL migration since
2783
// for that migration, in order to be retry-safe, we don't want to error out if
2784
// we re-insert the same node (which would error if the normal UpsertNode query
2785
// is used because of the constraint in that query that requires a node update
2786
// to have a newer last_update than the existing node).
2787
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
2788
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
2789
                arg.Version,
×
2790
                arg.PubKey,
×
2791
                arg.Alias,
×
2792
                arg.LastUpdate,
×
2793
                arg.Color,
×
2794
                arg.Signature,
×
2795
        )
×
2796
        var id int64
×
2797
        err := row.Scan(&id)
×
2798
        return id, err
×
2799
}
×
2800

2801
const isClosedChannel = `-- name: IsClosedChannel :one
2802
SELECT EXISTS (
2803
    SELECT 1
2804
    FROM graph_closed_scids
2805
    WHERE scid = $1
2806
)
2807
`
2808

2809
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
2810
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
2811
        var exists bool
×
2812
        err := row.Scan(&exists)
×
2813
        return exists, err
×
2814
}
×
2815

2816
const isPublicV1Node = `-- name: IsPublicV1Node :one
2817
SELECT EXISTS (
2818
    SELECT 1
2819
    FROM graph_channels c
2820
    JOIN graph_nodes n ON n.id = c.node_id_1
2821
    -- NOTE: we hard-code the version here since the clauses
2822
    -- here that determine if a node is public is specific
2823
    -- to the V1 gossip protocol. In V1, a node is public
2824
    -- if it has a public channel and a public channel is one
2825
    -- where we have the set of signatures of the channel
2826
    -- announcement. It is enough to just check that we have
2827
    -- one of the signatures since we only ever set them
2828
    -- together.
2829
    WHERE c.version = 1
2830
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2831
      AND n.pub_key = $1
2832
    UNION ALL
2833
    SELECT 1
2834
    FROM graph_channels c
2835
    JOIN graph_nodes n ON n.id = c.node_id_2
2836
    WHERE c.version = 1
2837
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2838
      AND n.pub_key = $1
2839
)
2840
`
2841

2842
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2843
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2844
        var exists bool
×
2845
        err := row.Scan(&exists)
×
2846
        return exists, err
×
2847
}
×
2848

2849
const isPublicV2Node = `-- name: IsPublicV2Node :one
2850
SELECT EXISTS (
2851
    SELECT 1
2852
    FROM graph_channels c
2853
    JOIN graph_nodes n ON n.id = c.node_id_1
2854
    -- NOTE: we hard-code the version here since the clauses
2855
    -- here that determine if a node is public is specific
2856
    -- to the V2 gossip protocol.
2857
    WHERE c.version = 2
2858
      AND COALESCE(length(c.signature), 0) > 0
2859
      AND n.pub_key = $1
2860

2861
    UNION ALL
2862

2863
    SELECT 1
2864
    FROM graph_channels c
2865
    JOIN graph_nodes n ON n.id = c.node_id_2
2866
    WHERE c.version = 2
2867
      AND COALESCE(length(c.signature), 0) > 0
2868
      AND n.pub_key = $1
2869
)
2870
`
2871

2872
func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2873
        row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey)
×
2874
        var exists bool
×
2875
        err := row.Scan(&exists)
×
2876
        return exists, err
×
2877
}
×
2878

2879
const isZombieChannel = `-- name: IsZombieChannel :one
2880
SELECT EXISTS (
2881
    SELECT 1
2882
    FROM graph_zombie_channels
2883
    WHERE scid = $1
2884
    AND version = $2
2885
) AS is_zombie
2886
`
2887

2888
type IsZombieChannelParams struct {
2889
        Scid    []byte
2890
        Version int16
2891
}
2892

2893
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2894
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2895
        var is_zombie bool
×
2896
        err := row.Scan(&is_zombie)
×
2897
        return is_zombie, err
×
2898
}
×
2899

2900
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2901
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,
2902
    n1.pub_key AS node1_pubkey,
2903
    n2.pub_key AS node2_pubkey,
2904

2905
    -- Policy 1
2906
    -- TODO(elle): use sqlc.embed to embed policy structs
2907
    --  once this issue is resolved:
2908
    --  https://github.com/sqlc-dev/sqlc/issues/2997
2909
    cp1.id AS policy1_id,
2910
    cp1.node_id AS policy1_node_id,
2911
    cp1.version AS policy1_version,
2912
    cp1.timelock AS policy1_timelock,
2913
    cp1.fee_ppm AS policy1_fee_ppm,
2914
    cp1.base_fee_msat AS policy1_base_fee_msat,
2915
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
2916
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
2917
    cp1.last_update AS policy1_last_update,
2918
    cp1.disabled AS policy1_disabled,
2919
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2920
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2921
    cp1.message_flags AS policy1_message_flags,
2922
    cp1.channel_flags AS policy1_channel_flags,
2923
    cp1.signature AS policy1_signature,
2924
    cp1.block_height AS policy1_block_height,
2925
    cp1.disable_flags AS policy1_disable_flags,
2926

2927
       -- Policy 2
2928
    cp2.id AS policy2_id,
2929
    cp2.node_id AS policy2_node_id,
2930
    cp2.version AS policy2_version,
2931
    cp2.timelock AS policy2_timelock,
2932
    cp2.fee_ppm AS policy2_fee_ppm,
2933
    cp2.base_fee_msat AS policy2_base_fee_msat,
2934
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
2935
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
2936
    cp2.last_update AS policy2_last_update,
2937
    cp2.disabled AS policy2_disabled,
2938
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2939
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2940
    cp2.message_flags AS policy2_message_flags,
2941
    cp2.channel_flags AS policy2_channel_flags,
2942
    cp2.signature AS policy2_signature,
2943
    cp2.block_height AS policy2_block_height,
2944
    cp2.disable_flags AS policy2_disable_flags
2945

2946
FROM graph_channels c
2947
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2948
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2949
    LEFT JOIN graph_channel_policies cp1
2950
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2951
    LEFT JOIN graph_channel_policies cp2
2952
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2953
WHERE c.version = $1
2954
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2955
`
2956

2957
type ListChannelsByNodeIDParams struct {
2958
        Version int16
2959
        NodeID1 int64
2960
}
2961

2962
type ListChannelsByNodeIDRow struct {
2963
        GraphChannel                   GraphChannel
2964
        Node1Pubkey                    []byte
2965
        Node2Pubkey                    []byte
2966
        Policy1ID                      sql.NullInt64
2967
        Policy1NodeID                  sql.NullInt64
2968
        Policy1Version                 sql.NullInt16
2969
        Policy1Timelock                sql.NullInt32
2970
        Policy1FeePpm                  sql.NullInt64
2971
        Policy1BaseFeeMsat             sql.NullInt64
2972
        Policy1MinHtlcMsat             sql.NullInt64
2973
        Policy1MaxHtlcMsat             sql.NullInt64
2974
        Policy1LastUpdate              sql.NullInt64
2975
        Policy1Disabled                sql.NullBool
2976
        Policy1InboundBaseFeeMsat      sql.NullInt64
2977
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2978
        Policy1MessageFlags            sql.NullInt16
2979
        Policy1ChannelFlags            sql.NullInt16
2980
        Policy1Signature               []byte
2981
        Policy1BlockHeight             sql.NullInt64
2982
        Policy1DisableFlags            sql.NullInt16
2983
        Policy2ID                      sql.NullInt64
2984
        Policy2NodeID                  sql.NullInt64
2985
        Policy2Version                 sql.NullInt16
2986
        Policy2Timelock                sql.NullInt32
2987
        Policy2FeePpm                  sql.NullInt64
2988
        Policy2BaseFeeMsat             sql.NullInt64
2989
        Policy2MinHtlcMsat             sql.NullInt64
2990
        Policy2MaxHtlcMsat             sql.NullInt64
2991
        Policy2LastUpdate              sql.NullInt64
2992
        Policy2Disabled                sql.NullBool
2993
        Policy2InboundBaseFeeMsat      sql.NullInt64
2994
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2995
        Policy2MessageFlags            sql.NullInt16
2996
        Policy2ChannelFlags            sql.NullInt16
2997
        Policy2Signature               []byte
2998
        Policy2BlockHeight             sql.NullInt64
2999
        Policy2DisableFlags            sql.NullInt16
3000
}
3001

3002
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
3003
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
3004
        if err != nil {
×
3005
                return nil, err
×
3006
        }
×
3007
        defer rows.Close()
×
3008
        var items []ListChannelsByNodeIDRow
×
3009
        for rows.Next() {
×
3010
                var i ListChannelsByNodeIDRow
×
3011
                if err := rows.Scan(
×
3012
                        &i.GraphChannel.ID,
×
3013
                        &i.GraphChannel.Version,
×
3014
                        &i.GraphChannel.Scid,
×
3015
                        &i.GraphChannel.NodeID1,
×
3016
                        &i.GraphChannel.NodeID2,
×
3017
                        &i.GraphChannel.Outpoint,
×
3018
                        &i.GraphChannel.Capacity,
×
3019
                        &i.GraphChannel.BitcoinKey1,
×
3020
                        &i.GraphChannel.BitcoinKey2,
×
3021
                        &i.GraphChannel.Node1Signature,
×
3022
                        &i.GraphChannel.Node2Signature,
×
3023
                        &i.GraphChannel.Bitcoin1Signature,
×
3024
                        &i.GraphChannel.Bitcoin2Signature,
×
3025
                        &i.GraphChannel.Signature,
×
3026
                        &i.GraphChannel.FundingPkScript,
×
3027
                        &i.GraphChannel.MerkleRootHash,
×
3028
                        &i.Node1Pubkey,
×
3029
                        &i.Node2Pubkey,
×
3030
                        &i.Policy1ID,
×
3031
                        &i.Policy1NodeID,
×
3032
                        &i.Policy1Version,
×
3033
                        &i.Policy1Timelock,
×
3034
                        &i.Policy1FeePpm,
×
3035
                        &i.Policy1BaseFeeMsat,
×
3036
                        &i.Policy1MinHtlcMsat,
×
3037
                        &i.Policy1MaxHtlcMsat,
×
3038
                        &i.Policy1LastUpdate,
×
3039
                        &i.Policy1Disabled,
×
3040
                        &i.Policy1InboundBaseFeeMsat,
×
3041
                        &i.Policy1InboundFeeRateMilliMsat,
×
3042
                        &i.Policy1MessageFlags,
×
3043
                        &i.Policy1ChannelFlags,
×
3044
                        &i.Policy1Signature,
×
3045
                        &i.Policy1BlockHeight,
×
3046
                        &i.Policy1DisableFlags,
×
3047
                        &i.Policy2ID,
×
3048
                        &i.Policy2NodeID,
×
3049
                        &i.Policy2Version,
×
3050
                        &i.Policy2Timelock,
×
3051
                        &i.Policy2FeePpm,
×
3052
                        &i.Policy2BaseFeeMsat,
×
3053
                        &i.Policy2MinHtlcMsat,
×
3054
                        &i.Policy2MaxHtlcMsat,
×
3055
                        &i.Policy2LastUpdate,
×
3056
                        &i.Policy2Disabled,
×
3057
                        &i.Policy2InboundBaseFeeMsat,
×
3058
                        &i.Policy2InboundFeeRateMilliMsat,
×
3059
                        &i.Policy2MessageFlags,
×
3060
                        &i.Policy2ChannelFlags,
×
3061
                        &i.Policy2Signature,
×
3062
                        &i.Policy2BlockHeight,
×
3063
                        &i.Policy2DisableFlags,
×
3064
                ); err != nil {
×
3065
                        return nil, err
×
3066
                }
×
3067
                items = append(items, i)
×
3068
        }
3069
        if err := rows.Close(); err != nil {
×
3070
                return nil, err
×
3071
        }
×
3072
        if err := rows.Err(); err != nil {
×
3073
                return nil, err
×
3074
        }
×
3075
        return items, nil
×
3076
}
3077

3078
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3079
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,
3080
       n1.pub_key AS node1_pubkey,
3081
       n2.pub_key AS node2_pubkey,
3082

3083
       -- Policy 1
3084
       -- TODO(elle): use sqlc.embed to embed policy structs
3085
       --  once this issue is resolved:
3086
       --  https://github.com/sqlc-dev/sqlc/issues/2997
3087
       cp1.id AS policy1_id,
3088
       cp1.node_id AS policy1_node_id,
3089
       cp1.version AS policy1_version,
3090
       cp1.timelock AS policy1_timelock,
3091
       cp1.fee_ppm AS policy1_fee_ppm,
3092
       cp1.base_fee_msat AS policy1_base_fee_msat,
3093
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
3094
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
3095
       cp1.last_update AS policy1_last_update,
3096
       cp1.disabled AS policy1_disabled,
3097
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3098
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3099
       cp1.message_flags AS policy1_message_flags,
3100
       cp1.channel_flags AS policy1_channel_flags,
3101
       cp1.signature AS policy1_signature,
3102
    cp1.block_height AS policy1_block_height,
3103
    cp1.disable_flags AS policy1_disable_flags,
3104

3105
       -- Policy 2
3106
       cp2.id AS policy2_id,
3107
       cp2.node_id AS policy2_node_id,
3108
       cp2.version AS policy2_version,
3109
       cp2.timelock AS policy2_timelock,
3110
       cp2.fee_ppm AS policy2_fee_ppm,
3111
       cp2.base_fee_msat AS policy2_base_fee_msat,
3112
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
3113
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
3114
       cp2.last_update AS policy2_last_update,
3115
       cp2.disabled AS policy2_disabled,
3116
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3117
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3118
       cp2.message_flags AS policy2_message_flags,
3119
       cp2.channel_flags AS policy2_channel_flags,
3120
       cp2.signature AS policy2_signature,
3121
    cp2.block_height AS policy2_block_height,
3122
    cp2.disable_flags AS policy2_disable_flags
3123

3124
FROM graph_channels c
3125
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3126
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3127
         LEFT JOIN graph_channel_policies cp1
3128
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3129
         LEFT JOIN graph_channel_policies cp2
3130
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3131
WHERE c.version = $1
3132
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
3133
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
3134
`
3135

3136
type ListChannelsForNodeIDsParams struct {
3137
        Version  int16
3138
        Node1Ids []int64
3139
        Node2Ids []int64
3140
}
3141

3142
type ListChannelsForNodeIDsRow struct {
3143
        GraphChannel                   GraphChannel
3144
        Node1Pubkey                    []byte
3145
        Node2Pubkey                    []byte
3146
        Policy1ID                      sql.NullInt64
3147
        Policy1NodeID                  sql.NullInt64
3148
        Policy1Version                 sql.NullInt16
3149
        Policy1Timelock                sql.NullInt32
3150
        Policy1FeePpm                  sql.NullInt64
3151
        Policy1BaseFeeMsat             sql.NullInt64
3152
        Policy1MinHtlcMsat             sql.NullInt64
3153
        Policy1MaxHtlcMsat             sql.NullInt64
3154
        Policy1LastUpdate              sql.NullInt64
3155
        Policy1Disabled                sql.NullBool
3156
        Policy1InboundBaseFeeMsat      sql.NullInt64
3157
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3158
        Policy1MessageFlags            sql.NullInt16
3159
        Policy1ChannelFlags            sql.NullInt16
3160
        Policy1Signature               []byte
3161
        Policy1BlockHeight             sql.NullInt64
3162
        Policy1DisableFlags            sql.NullInt16
3163
        Policy2ID                      sql.NullInt64
3164
        Policy2NodeID                  sql.NullInt64
3165
        Policy2Version                 sql.NullInt16
3166
        Policy2Timelock                sql.NullInt32
3167
        Policy2FeePpm                  sql.NullInt64
3168
        Policy2BaseFeeMsat             sql.NullInt64
3169
        Policy2MinHtlcMsat             sql.NullInt64
3170
        Policy2MaxHtlcMsat             sql.NullInt64
3171
        Policy2LastUpdate              sql.NullInt64
3172
        Policy2Disabled                sql.NullBool
3173
        Policy2InboundBaseFeeMsat      sql.NullInt64
3174
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3175
        Policy2MessageFlags            sql.NullInt16
3176
        Policy2ChannelFlags            sql.NullInt16
3177
        Policy2Signature               []byte
3178
        Policy2BlockHeight             sql.NullInt64
3179
        Policy2DisableFlags            sql.NullInt16
3180
}
3181

3182
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
3183
        query := listChannelsForNodeIDs
×
3184
        var queryParams []interface{}
×
3185
        queryParams = append(queryParams, arg.Version)
×
3186
        if len(arg.Node1Ids) > 0 {
×
3187
                for _, v := range arg.Node1Ids {
×
3188
                        queryParams = append(queryParams, v)
×
3189
                }
×
3190
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
3191
        } else {
×
3192
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
3193
        }
×
3194
        if len(arg.Node2Ids) > 0 {
×
3195
                for _, v := range arg.Node2Ids {
×
3196
                        queryParams = append(queryParams, v)
×
3197
                }
×
3198
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
3199
        } else {
×
3200
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
3201
        }
×
3202
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
3203
        if err != nil {
×
3204
                return nil, err
×
3205
        }
×
3206
        defer rows.Close()
×
3207
        var items []ListChannelsForNodeIDsRow
×
3208
        for rows.Next() {
×
3209
                var i ListChannelsForNodeIDsRow
×
3210
                if err := rows.Scan(
×
3211
                        &i.GraphChannel.ID,
×
3212
                        &i.GraphChannel.Version,
×
3213
                        &i.GraphChannel.Scid,
×
3214
                        &i.GraphChannel.NodeID1,
×
3215
                        &i.GraphChannel.NodeID2,
×
3216
                        &i.GraphChannel.Outpoint,
×
3217
                        &i.GraphChannel.Capacity,
×
3218
                        &i.GraphChannel.BitcoinKey1,
×
3219
                        &i.GraphChannel.BitcoinKey2,
×
3220
                        &i.GraphChannel.Node1Signature,
×
3221
                        &i.GraphChannel.Node2Signature,
×
3222
                        &i.GraphChannel.Bitcoin1Signature,
×
3223
                        &i.GraphChannel.Bitcoin2Signature,
×
3224
                        &i.GraphChannel.Signature,
×
3225
                        &i.GraphChannel.FundingPkScript,
×
3226
                        &i.GraphChannel.MerkleRootHash,
×
3227
                        &i.Node1Pubkey,
×
3228
                        &i.Node2Pubkey,
×
3229
                        &i.Policy1ID,
×
3230
                        &i.Policy1NodeID,
×
3231
                        &i.Policy1Version,
×
3232
                        &i.Policy1Timelock,
×
3233
                        &i.Policy1FeePpm,
×
3234
                        &i.Policy1BaseFeeMsat,
×
3235
                        &i.Policy1MinHtlcMsat,
×
3236
                        &i.Policy1MaxHtlcMsat,
×
3237
                        &i.Policy1LastUpdate,
×
3238
                        &i.Policy1Disabled,
×
3239
                        &i.Policy1InboundBaseFeeMsat,
×
3240
                        &i.Policy1InboundFeeRateMilliMsat,
×
3241
                        &i.Policy1MessageFlags,
×
3242
                        &i.Policy1ChannelFlags,
×
3243
                        &i.Policy1Signature,
×
3244
                        &i.Policy1BlockHeight,
×
3245
                        &i.Policy1DisableFlags,
×
3246
                        &i.Policy2ID,
×
3247
                        &i.Policy2NodeID,
×
3248
                        &i.Policy2Version,
×
3249
                        &i.Policy2Timelock,
×
3250
                        &i.Policy2FeePpm,
×
3251
                        &i.Policy2BaseFeeMsat,
×
3252
                        &i.Policy2MinHtlcMsat,
×
3253
                        &i.Policy2MaxHtlcMsat,
×
3254
                        &i.Policy2LastUpdate,
×
3255
                        &i.Policy2Disabled,
×
3256
                        &i.Policy2InboundBaseFeeMsat,
×
3257
                        &i.Policy2InboundFeeRateMilliMsat,
×
3258
                        &i.Policy2MessageFlags,
×
3259
                        &i.Policy2ChannelFlags,
×
3260
                        &i.Policy2Signature,
×
3261
                        &i.Policy2BlockHeight,
×
3262
                        &i.Policy2DisableFlags,
×
3263
                ); err != nil {
×
3264
                        return nil, err
×
3265
                }
×
3266
                items = append(items, i)
×
3267
        }
3268
        if err := rows.Close(); err != nil {
×
3269
                return nil, err
×
3270
        }
×
3271
        if err := rows.Err(); err != nil {
×
3272
                return nil, err
×
3273
        }
×
3274
        return items, nil
×
3275
}
3276

3277
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3278
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3279
FROM graph_channels c
3280
WHERE c.version = $1 AND c.id > $2
3281
ORDER BY c.id
3282
LIMIT $3
3283
`
3284

3285
type ListChannelsPaginatedParams struct {
3286
        Version int16
3287
        ID      int64
3288
        Limit   int32
3289
}
3290

3291
type ListChannelsPaginatedRow struct {
3292
        ID          int64
3293
        BitcoinKey1 []byte
3294
        BitcoinKey2 []byte
3295
        Outpoint    string
3296
}
3297

3298
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3299
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3300
        if err != nil {
×
3301
                return nil, err
×
3302
        }
×
3303
        defer rows.Close()
×
3304
        var items []ListChannelsPaginatedRow
×
3305
        for rows.Next() {
×
3306
                var i ListChannelsPaginatedRow
×
3307
                if err := rows.Scan(
×
3308
                        &i.ID,
×
3309
                        &i.BitcoinKey1,
×
3310
                        &i.BitcoinKey2,
×
3311
                        &i.Outpoint,
×
3312
                ); err != nil {
×
3313
                        return nil, err
×
3314
                }
×
3315
                items = append(items, i)
×
3316
        }
3317
        if err := rows.Close(); err != nil {
×
3318
                return nil, err
×
3319
        }
×
3320
        if err := rows.Err(); err != nil {
×
3321
                return nil, err
×
3322
        }
×
3323
        return items, nil
×
3324
}
3325

3326
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3327
SELECT
3328
    c.id as id,
3329
    c.scid as scid,
3330
    c.capacity AS capacity,
3331

3332
    -- Join node pubkeys
3333
    n1.pub_key AS node1_pubkey,
3334
    n2.pub_key AS node2_pubkey,
3335

3336
    -- Node 1 policy
3337
    cp1.timelock AS policy_1_timelock,
3338
    cp1.fee_ppm AS policy_1_fee_ppm,
3339
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3340
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3341
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3342
    cp1.disabled AS policy_1_disabled,
3343
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3344
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3345
    cp1.message_flags AS policy1_message_flags,
3346
    cp1.channel_flags AS policy1_channel_flags,
3347
    cp1.block_height AS policy1_block_height,
3348
    cp1.disable_flags AS policy1_disable_flags,
3349

3350
    -- Node 2 policy
3351
    cp2.timelock AS policy_2_timelock,
3352
    cp2.fee_ppm AS policy_2_fee_ppm,
3353
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3354
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3355
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3356
    cp2.disabled AS policy_2_disabled,
3357
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3358
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3359
    cp2.message_flags AS policy2_message_flags,
3360
    cp2.channel_flags AS policy2_channel_flags,
3361
    cp2.block_height AS policy2_block_height,
3362
    cp2.disable_flags AS policy2_disable_flags
3363

3364
FROM graph_channels c
3365
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3366
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3367
LEFT JOIN graph_channel_policies cp1
3368
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3369
LEFT JOIN graph_channel_policies cp2
3370
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3371
WHERE c.version = $1 AND c.id > $2
3372
ORDER BY c.id
3373
LIMIT $3
3374
`
3375

3376
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3377
        Version int16
3378
        ID      int64
3379
        Limit   int32
3380
}
3381

3382
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3383
        ID                             int64
3384
        Scid                           []byte
3385
        Capacity                       sql.NullInt64
3386
        Node1Pubkey                    []byte
3387
        Node2Pubkey                    []byte
3388
        Policy1Timelock                sql.NullInt32
3389
        Policy1FeePpm                  sql.NullInt64
3390
        Policy1BaseFeeMsat             sql.NullInt64
3391
        Policy1MinHtlcMsat             sql.NullInt64
3392
        Policy1MaxHtlcMsat             sql.NullInt64
3393
        Policy1Disabled                sql.NullBool
3394
        Policy1InboundBaseFeeMsat      sql.NullInt64
3395
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3396
        Policy1MessageFlags            sql.NullInt16
3397
        Policy1ChannelFlags            sql.NullInt16
3398
        Policy1BlockHeight             sql.NullInt64
3399
        Policy1DisableFlags            sql.NullInt16
3400
        Policy2Timelock                sql.NullInt32
3401
        Policy2FeePpm                  sql.NullInt64
3402
        Policy2BaseFeeMsat             sql.NullInt64
3403
        Policy2MinHtlcMsat             sql.NullInt64
3404
        Policy2MaxHtlcMsat             sql.NullInt64
3405
        Policy2Disabled                sql.NullBool
3406
        Policy2InboundBaseFeeMsat      sql.NullInt64
3407
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3408
        Policy2MessageFlags            sql.NullInt16
3409
        Policy2ChannelFlags            sql.NullInt16
3410
        Policy2BlockHeight             sql.NullInt64
3411
        Policy2DisableFlags            sql.NullInt16
3412
}
3413

3414
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3415
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3416
        if err != nil {
×
3417
                return nil, err
×
3418
        }
×
3419
        defer rows.Close()
×
3420
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3421
        for rows.Next() {
×
3422
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3423
                if err := rows.Scan(
×
3424
                        &i.ID,
×
3425
                        &i.Scid,
×
3426
                        &i.Capacity,
×
3427
                        &i.Node1Pubkey,
×
3428
                        &i.Node2Pubkey,
×
3429
                        &i.Policy1Timelock,
×
3430
                        &i.Policy1FeePpm,
×
3431
                        &i.Policy1BaseFeeMsat,
×
3432
                        &i.Policy1MinHtlcMsat,
×
3433
                        &i.Policy1MaxHtlcMsat,
×
3434
                        &i.Policy1Disabled,
×
3435
                        &i.Policy1InboundBaseFeeMsat,
×
3436
                        &i.Policy1InboundFeeRateMilliMsat,
×
3437
                        &i.Policy1MessageFlags,
×
3438
                        &i.Policy1ChannelFlags,
×
3439
                        &i.Policy1BlockHeight,
×
3440
                        &i.Policy1DisableFlags,
×
3441
                        &i.Policy2Timelock,
×
3442
                        &i.Policy2FeePpm,
×
3443
                        &i.Policy2BaseFeeMsat,
×
3444
                        &i.Policy2MinHtlcMsat,
×
3445
                        &i.Policy2MaxHtlcMsat,
×
3446
                        &i.Policy2Disabled,
×
3447
                        &i.Policy2InboundBaseFeeMsat,
×
3448
                        &i.Policy2InboundFeeRateMilliMsat,
×
3449
                        &i.Policy2MessageFlags,
×
3450
                        &i.Policy2ChannelFlags,
×
3451
                        &i.Policy2BlockHeight,
×
3452
                        &i.Policy2DisableFlags,
×
3453
                ); err != nil {
×
3454
                        return nil, err
×
3455
                }
×
3456
                items = append(items, i)
×
3457
        }
3458
        if err := rows.Close(); err != nil {
×
3459
                return nil, err
×
3460
        }
×
3461
        if err := rows.Err(); err != nil {
×
3462
                return nil, err
×
3463
        }
×
3464
        return items, nil
×
3465
}
3466

3467
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3468
SELECT
3469
    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,
3470

3471
    -- Join node pubkeys
3472
    n1.pub_key AS node1_pubkey,
3473
    n2.pub_key AS node2_pubkey,
3474

3475
    -- Node 1 policy
3476
    cp1.id AS policy_1_id,
3477
    cp1.node_id AS policy_1_node_id,
3478
    cp1.version AS policy_1_version,
3479
    cp1.timelock AS policy_1_timelock,
3480
    cp1.fee_ppm AS policy_1_fee_ppm,
3481
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3482
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3483
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3484
    cp1.last_update AS policy_1_last_update,
3485
    cp1.disabled AS policy_1_disabled,
3486
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3487
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3488
    cp1.message_flags AS policy1_message_flags,
3489
    cp1.channel_flags AS policy1_channel_flags,
3490
    cp1.block_height AS policy1_block_height,
3491
    cp1.disable_flags AS policy1_disable_flags,
3492
    cp1.signature AS policy_1_signature,
3493

3494
    -- Node 2 policy
3495
    cp2.id AS policy_2_id,
3496
    cp2.node_id AS policy_2_node_id,
3497
    cp2.version AS policy_2_version,
3498
    cp2.timelock AS policy_2_timelock,
3499
    cp2.fee_ppm AS policy_2_fee_ppm,
3500
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3501
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3502
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3503
    cp2.last_update AS policy_2_last_update,
3504
    cp2.disabled AS policy_2_disabled,
3505
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3506
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3507
    cp2.message_flags AS policy2_message_flags,
3508
    cp2.channel_flags AS policy2_channel_flags,
3509
    cp2.signature AS policy_2_signature,
3510
    cp2.block_height AS policy_2_block_height,
3511
    cp2.disable_flags AS policy_2_disable_flags
3512

3513
FROM graph_channels c
3514
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3515
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3516
LEFT JOIN graph_channel_policies cp1
3517
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3518
LEFT JOIN graph_channel_policies cp2
3519
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3520
WHERE c.version = $1 AND c.id > $2
3521
ORDER BY c.id
3522
LIMIT $3
3523
`
3524

3525
type ListChannelsWithPoliciesPaginatedParams struct {
3526
        Version int16
3527
        ID      int64
3528
        Limit   int32
3529
}
3530

3531
type ListChannelsWithPoliciesPaginatedRow struct {
3532
        GraphChannel                   GraphChannel
3533
        Node1Pubkey                    []byte
3534
        Node2Pubkey                    []byte
3535
        Policy1ID                      sql.NullInt64
3536
        Policy1NodeID                  sql.NullInt64
3537
        Policy1Version                 sql.NullInt16
3538
        Policy1Timelock                sql.NullInt32
3539
        Policy1FeePpm                  sql.NullInt64
3540
        Policy1BaseFeeMsat             sql.NullInt64
3541
        Policy1MinHtlcMsat             sql.NullInt64
3542
        Policy1MaxHtlcMsat             sql.NullInt64
3543
        Policy1LastUpdate              sql.NullInt64
3544
        Policy1Disabled                sql.NullBool
3545
        Policy1InboundBaseFeeMsat      sql.NullInt64
3546
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3547
        Policy1MessageFlags            sql.NullInt16
3548
        Policy1ChannelFlags            sql.NullInt16
3549
        Policy1BlockHeight             sql.NullInt64
3550
        Policy1DisableFlags            sql.NullInt16
3551
        Policy1Signature               []byte
3552
        Policy2ID                      sql.NullInt64
3553
        Policy2NodeID                  sql.NullInt64
3554
        Policy2Version                 sql.NullInt16
3555
        Policy2Timelock                sql.NullInt32
3556
        Policy2FeePpm                  sql.NullInt64
3557
        Policy2BaseFeeMsat             sql.NullInt64
3558
        Policy2MinHtlcMsat             sql.NullInt64
3559
        Policy2MaxHtlcMsat             sql.NullInt64
3560
        Policy2LastUpdate              sql.NullInt64
3561
        Policy2Disabled                sql.NullBool
3562
        Policy2InboundBaseFeeMsat      sql.NullInt64
3563
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3564
        Policy2MessageFlags            sql.NullInt16
3565
        Policy2ChannelFlags            sql.NullInt16
3566
        Policy2Signature               []byte
3567
        Policy2BlockHeight             sql.NullInt64
3568
        Policy2DisableFlags            sql.NullInt16
3569
}
3570

3571
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
3572
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
3573
        if err != nil {
×
3574
                return nil, err
×
3575
        }
×
3576
        defer rows.Close()
×
3577
        var items []ListChannelsWithPoliciesPaginatedRow
×
3578
        for rows.Next() {
×
3579
                var i ListChannelsWithPoliciesPaginatedRow
×
3580
                if err := rows.Scan(
×
3581
                        &i.GraphChannel.ID,
×
3582
                        &i.GraphChannel.Version,
×
3583
                        &i.GraphChannel.Scid,
×
3584
                        &i.GraphChannel.NodeID1,
×
3585
                        &i.GraphChannel.NodeID2,
×
3586
                        &i.GraphChannel.Outpoint,
×
3587
                        &i.GraphChannel.Capacity,
×
3588
                        &i.GraphChannel.BitcoinKey1,
×
3589
                        &i.GraphChannel.BitcoinKey2,
×
3590
                        &i.GraphChannel.Node1Signature,
×
3591
                        &i.GraphChannel.Node2Signature,
×
3592
                        &i.GraphChannel.Bitcoin1Signature,
×
3593
                        &i.GraphChannel.Bitcoin2Signature,
×
3594
                        &i.GraphChannel.Signature,
×
3595
                        &i.GraphChannel.FundingPkScript,
×
3596
                        &i.GraphChannel.MerkleRootHash,
×
3597
                        &i.Node1Pubkey,
×
3598
                        &i.Node2Pubkey,
×
3599
                        &i.Policy1ID,
×
3600
                        &i.Policy1NodeID,
×
3601
                        &i.Policy1Version,
×
3602
                        &i.Policy1Timelock,
×
3603
                        &i.Policy1FeePpm,
×
3604
                        &i.Policy1BaseFeeMsat,
×
3605
                        &i.Policy1MinHtlcMsat,
×
3606
                        &i.Policy1MaxHtlcMsat,
×
3607
                        &i.Policy1LastUpdate,
×
3608
                        &i.Policy1Disabled,
×
3609
                        &i.Policy1InboundBaseFeeMsat,
×
3610
                        &i.Policy1InboundFeeRateMilliMsat,
×
3611
                        &i.Policy1MessageFlags,
×
3612
                        &i.Policy1ChannelFlags,
×
3613
                        &i.Policy1BlockHeight,
×
3614
                        &i.Policy1DisableFlags,
×
3615
                        &i.Policy1Signature,
×
3616
                        &i.Policy2ID,
×
3617
                        &i.Policy2NodeID,
×
3618
                        &i.Policy2Version,
×
3619
                        &i.Policy2Timelock,
×
3620
                        &i.Policy2FeePpm,
×
3621
                        &i.Policy2BaseFeeMsat,
×
3622
                        &i.Policy2MinHtlcMsat,
×
3623
                        &i.Policy2MaxHtlcMsat,
×
3624
                        &i.Policy2LastUpdate,
×
3625
                        &i.Policy2Disabled,
×
3626
                        &i.Policy2InboundBaseFeeMsat,
×
3627
                        &i.Policy2InboundFeeRateMilliMsat,
×
3628
                        &i.Policy2MessageFlags,
×
3629
                        &i.Policy2ChannelFlags,
×
3630
                        &i.Policy2Signature,
×
3631
                        &i.Policy2BlockHeight,
×
3632
                        &i.Policy2DisableFlags,
×
3633
                ); err != nil {
×
3634
                        return nil, err
×
3635
                }
×
3636
                items = append(items, i)
×
3637
        }
3638
        if err := rows.Close(); err != nil {
×
3639
                return nil, err
×
3640
        }
×
3641
        if err := rows.Err(); err != nil {
×
3642
                return nil, err
×
3643
        }
×
3644
        return items, nil
×
3645
}
3646

3647
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3648
SELECT id, pub_key
3649
FROM graph_nodes
3650
WHERE version = $1  AND id > $2
3651
ORDER BY id
3652
LIMIT $3
3653
`
3654

3655
type ListNodeIDsAndPubKeysParams struct {
3656
        Version int16
3657
        ID      int64
3658
        Limit   int32
3659
}
3660

3661
type ListNodeIDsAndPubKeysRow struct {
3662
        ID     int64
3663
        PubKey []byte
3664
}
3665

3666
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
3667
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
3668
        if err != nil {
×
3669
                return nil, err
×
3670
        }
×
3671
        defer rows.Close()
×
3672
        var items []ListNodeIDsAndPubKeysRow
×
3673
        for rows.Next() {
×
3674
                var i ListNodeIDsAndPubKeysRow
×
3675
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
3676
                        return nil, err
×
3677
                }
×
3678
                items = append(items, i)
×
3679
        }
3680
        if err := rows.Close(); err != nil {
×
3681
                return nil, err
×
3682
        }
×
3683
        if err := rows.Err(); err != nil {
×
3684
                return nil, err
×
3685
        }
×
3686
        return items, nil
×
3687
}
3688

3689
const listNodesPaginated = `-- name: ListNodesPaginated :many
3690
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
3691
FROM graph_nodes
3692
WHERE version = $1 AND id > $2
3693
ORDER BY id
3694
LIMIT $3
3695
`
3696

3697
type ListNodesPaginatedParams struct {
3698
        Version int16
3699
        ID      int64
3700
        Limit   int32
3701
}
3702

3703
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
3704
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
3705
        if err != nil {
×
3706
                return nil, err
×
3707
        }
×
3708
        defer rows.Close()
×
3709
        var items []GraphNode
×
3710
        for rows.Next() {
×
3711
                var i GraphNode
×
3712
                if err := rows.Scan(
×
3713
                        &i.ID,
×
3714
                        &i.Version,
×
3715
                        &i.PubKey,
×
3716
                        &i.Alias,
×
3717
                        &i.LastUpdate,
×
3718
                        &i.Color,
×
3719
                        &i.Signature,
×
3720
                        &i.BlockHeight,
×
3721
                ); err != nil {
×
3722
                        return nil, err
×
3723
                }
×
3724
                items = append(items, i)
×
3725
        }
3726
        if err := rows.Close(); err != nil {
×
3727
                return nil, err
×
3728
        }
×
3729
        if err := rows.Err(); err != nil {
×
3730
                return nil, err
×
3731
        }
×
3732
        return items, nil
×
3733
}
3734

3735
const nodeExists = `-- name: NodeExists :one
3736
SELECT EXISTS (
3737
    SELECT 1
3738
    FROM graph_nodes
3739
    WHERE pub_key = $1
3740
      AND version = $2
3741
) AS node_exists
3742
`
3743

3744
type NodeExistsParams struct {
3745
        PubKey  []byte
3746
        Version int16
3747
}
3748

3749
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
3750
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
3751
        var node_exists bool
×
3752
        err := row.Scan(&node_exists)
×
3753
        return node_exists, err
×
3754
}
×
3755

3756
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3757
/* ─────────────────────────────────────────────
3758
   graph_channel_policy_extra_types table queries
3759
   ─────────────────────────────────────────────
3760
*/
3761

3762
INSERT INTO graph_channel_policy_extra_types (
3763
    channel_policy_id, type, value
3764
)
3765
VALUES ($1, $2, $3)
3766
ON CONFLICT (channel_policy_id, type)
3767
    -- If a conflict occurs on channel_policy_id and type, then we update the
3768
    -- value.
3769
    DO UPDATE SET value = EXCLUDED.value
3770
`
3771

3772
type UpsertChanPolicyExtraTypeParams struct {
3773
        ChannelPolicyID int64
3774
        Type            int64
3775
        Value           []byte
3776
}
3777

3778
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
3779
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
3780
        return err
×
3781
}
×
3782

3783
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3784
/* ─────────────────────────────────────────────
3785
   graph_channel_extra_types table queries
3786
   ─────────────────────────────────────────────
3787
*/
3788

3789
INSERT INTO graph_channel_extra_types (
3790
    channel_id, type, value
3791
)
3792
VALUES ($1, $2, $3)
3793
    ON CONFLICT (channel_id, type)
3794
    -- Update the value if a conflict occurs on channel_id and type.
3795
    DO UPDATE SET value = EXCLUDED.value
3796
`
3797

3798
type UpsertChannelExtraTypeParams struct {
3799
        ChannelID int64
3800
        Type      int64
3801
        Value     []byte
3802
}
3803

3804
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
3805
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
3806
        return err
×
3807
}
×
3808

3809
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3810
/* ─────────────────────────────────────────────
3811
   graph_channel_policies table queries
3812
   ─────────────────────────────────────────────
3813
*/
3814

3815
INSERT INTO graph_channel_policies (
3816
    version, channel_id, node_id, timelock, fee_ppm,
3817
    base_fee_msat, min_htlc_msat, last_update, disabled,
3818
    max_htlc_msat, inbound_base_fee_msat,
3819
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3820
    signature, block_height, disable_flags
3821
) VALUES  (
3822
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
3823
)
3824
ON CONFLICT (channel_id, node_id, version)
3825
    -- Update the following fields if a conflict occurs on channel_id,
3826
    -- node_id, and version.
3827
    DO UPDATE SET
3828
        timelock = EXCLUDED.timelock,
3829
        fee_ppm = EXCLUDED.fee_ppm,
3830
        base_fee_msat = EXCLUDED.base_fee_msat,
3831
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3832
        last_update = EXCLUDED.last_update,
3833
        disabled = EXCLUDED.disabled,
3834
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3835
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3836
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3837
        message_flags = EXCLUDED.message_flags,
3838
        channel_flags = EXCLUDED.channel_flags,
3839
        signature = EXCLUDED.signature,
3840
        block_height = EXCLUDED.block_height,
3841
        disable_flags = EXCLUDED.disable_flags
3842
WHERE (
3843
    EXCLUDED.version = 1 AND (
3844
        graph_channel_policies.last_update IS NULL
3845
        OR EXCLUDED.last_update > graph_channel_policies.last_update
3846
    )
3847
)
3848
OR (
3849
    EXCLUDED.version = 2 AND (
3850
        graph_channel_policies.block_height IS NULL
3851
        OR EXCLUDED.block_height >= graph_channel_policies.block_height
3852
    )
3853
)
3854
RETURNING id
3855
`
3856

3857
type UpsertEdgePolicyParams struct {
3858
        Version                 int16
3859
        ChannelID               int64
3860
        NodeID                  int64
3861
        Timelock                int32
3862
        FeePpm                  int64
3863
        BaseFeeMsat             int64
3864
        MinHtlcMsat             int64
3865
        LastUpdate              sql.NullInt64
3866
        Disabled                sql.NullBool
3867
        MaxHtlcMsat             sql.NullInt64
3868
        InboundBaseFeeMsat      sql.NullInt64
3869
        InboundFeeRateMilliMsat sql.NullInt64
3870
        MessageFlags            sql.NullInt16
3871
        ChannelFlags            sql.NullInt16
3872
        Signature               []byte
3873
        BlockHeight             sql.NullInt64
3874
        DisableFlags            sql.NullInt16
3875
}
3876

3877
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
3878
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
3879
                arg.Version,
×
3880
                arg.ChannelID,
×
3881
                arg.NodeID,
×
3882
                arg.Timelock,
×
3883
                arg.FeePpm,
×
3884
                arg.BaseFeeMsat,
×
3885
                arg.MinHtlcMsat,
×
3886
                arg.LastUpdate,
×
3887
                arg.Disabled,
×
3888
                arg.MaxHtlcMsat,
×
3889
                arg.InboundBaseFeeMsat,
×
3890
                arg.InboundFeeRateMilliMsat,
×
3891
                arg.MessageFlags,
×
3892
                arg.ChannelFlags,
×
3893
                arg.Signature,
×
3894
                arg.BlockHeight,
×
3895
                arg.DisableFlags,
×
3896
        )
×
3897
        var id int64
×
3898
        err := row.Scan(&id)
×
3899
        return id, err
×
3900
}
×
3901

3902
const upsertNode = `-- name: UpsertNode :one
3903
/* ─────────────────────────────────────────────
3904
   graph_nodes table queries
3905
   ───────────────────────────��─────────────────
3906
*/
3907

3908
INSERT INTO graph_nodes (
3909
    version, pub_key, alias, last_update, block_height, color, signature
3910
) VALUES (
3911
    $1, $2, $3, $4, $5, $6, $7
3912
)
3913
ON CONFLICT (pub_key, version)
3914
    -- Update the following fields if a conflict occurs on pub_key
3915
    -- and version.
3916
    DO UPDATE SET
3917
        alias = EXCLUDED.alias,
3918
        last_update = EXCLUDED.last_update,
3919
        block_height = EXCLUDED.block_height,
3920
        color = EXCLUDED.color,
3921
        signature = EXCLUDED.signature
3922
WHERE (graph_nodes.last_update IS NULL
3923
    OR EXCLUDED.last_update > graph_nodes.last_update)
3924
AND (graph_nodes.block_height IS NULL
3925
    OR EXCLUDED.block_height >= graph_nodes.block_height)
3926
RETURNING id
3927
`
3928

3929
type UpsertNodeParams struct {
3930
        Version     int16
3931
        PubKey      []byte
3932
        Alias       sql.NullString
3933
        LastUpdate  sql.NullInt64
3934
        BlockHeight sql.NullInt64
3935
        Color       sql.NullString
3936
        Signature   []byte
3937
}
3938

3939
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3940
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3941
                arg.Version,
×
3942
                arg.PubKey,
×
3943
                arg.Alias,
×
3944
                arg.LastUpdate,
×
3945
                arg.BlockHeight,
×
3946
                arg.Color,
×
3947
                arg.Signature,
×
3948
        )
×
3949
        var id int64
×
3950
        err := row.Scan(&id)
×
3951
        return id, err
×
3952
}
×
3953

3954
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3955
/* ─────────────────────────────────────────────
3956
   graph_node_addresses table queries
3957
   ───────────────────────────────────��─────────
3958
*/
3959

3960
INSERT INTO graph_node_addresses (
3961
    node_id,
3962
    type,
3963
    address,
3964
    position
3965
) VALUES (
3966
    $1, $2, $3, $4
3967
) ON CONFLICT (node_id, type, position)
3968
    DO UPDATE SET address = EXCLUDED.address
3969
`
3970

3971
type UpsertNodeAddressParams struct {
3972
        NodeID   int64
3973
        Type     int16
3974
        Address  string
3975
        Position int32
3976
}
3977

3978
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3979
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3980
                arg.NodeID,
×
3981
                arg.Type,
×
3982
                arg.Address,
×
3983
                arg.Position,
×
3984
        )
×
3985
        return err
×
3986
}
×
3987

3988
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3989
/* ─────────────────────────────────────────────
3990
   graph_node_extra_types table queries
3991
   ─────────────────────────────────────────────
3992
*/
3993

3994
INSERT INTO graph_node_extra_types (
3995
    node_id, type, value
3996
)
3997
VALUES ($1, $2, $3)
3998
ON CONFLICT (type, node_id)
3999
    -- Update the value if a conflict occurs on type
4000
    -- and node_id.
4001
    DO UPDATE SET value = EXCLUDED.value
4002
`
4003

4004
type UpsertNodeExtraTypeParams struct {
4005
        NodeID int64
4006
        Type   int64
4007
        Value  []byte
4008
}
4009

4010
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
4011
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
4012
        return err
×
4013
}
×
4014

4015
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
4016
/* ───────────────────────────���─────────────────
4017
    graph_prune_log table queries
4018
    ─────────────────────────────────────────────
4019
*/
4020

4021
INSERT INTO graph_prune_log (
4022
    block_height, block_hash
4023
) VALUES (
4024
    $1, $2
4025
)
4026
ON CONFLICT(block_height) DO UPDATE SET
4027
    block_hash = EXCLUDED.block_hash
4028
`
4029

4030
type UpsertPruneLogEntryParams struct {
4031
        BlockHeight int64
4032
        BlockHash   []byte
4033
}
4034

4035
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
4036
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
4037
        return err
×
4038
}
×
4039

4040
const upsertSourceNode = `-- name: UpsertSourceNode :one
4041
INSERT INTO graph_nodes (
4042
    version, pub_key, alias, last_update, block_height, color, signature
4043
) VALUES (
4044
    $1, $2, $3, $4, $5, $6, $7
4045
)
4046
ON CONFLICT (pub_key, version)
4047
    -- Update the following fields if a conflict occurs on pub_key
4048
    -- and version.
4049
    DO UPDATE SET
4050
        alias = EXCLUDED.alias,
4051
        last_update = EXCLUDED.last_update,
4052
        block_height = EXCLUDED.block_height,
4053
        color = EXCLUDED.color,
4054
        signature = EXCLUDED.signature
4055
WHERE graph_nodes.last_update IS NULL
4056
    OR EXCLUDED.last_update >= graph_nodes.last_update
4057
AND (graph_nodes.block_height IS NULL
4058
   OR EXCLUDED.block_height >= graph_nodes.block_height)
4059
RETURNING id
4060
`
4061

4062
type UpsertSourceNodeParams struct {
4063
        Version     int16
4064
        PubKey      []byte
4065
        Alias       sql.NullString
4066
        LastUpdate  sql.NullInt64
4067
        BlockHeight sql.NullInt64
4068
        Color       sql.NullString
4069
        Signature   []byte
4070
}
4071

4072
// We use a separate upsert for our own node since we want to be less strict
4073
// about the last_update field. For our own node, we always want to
4074
// update the record even if the last_update is the same as what we have.
4075
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
4076
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
4077
                arg.Version,
×
4078
                arg.PubKey,
×
4079
                arg.Alias,
×
4080
                arg.LastUpdate,
×
4081
                arg.BlockHeight,
×
4082
                arg.Color,
×
4083
                arg.Signature,
×
4084
        )
×
4085
        var id int64
×
4086
        err := row.Scan(&id)
×
4087
        return id, err
×
4088
}
×
4089

4090
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
4091
/* ─────────────────────────────────────────────
4092
   graph_zombie_channels table queries
4093
   ─────────────────────────────────────────────
4094
*/
4095

4096
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
4097
VALUES ($1, $2, $3, $4)
4098
ON CONFLICT (scid, version)
4099
DO UPDATE SET
4100
    -- If a conflict exists for the SCID and version pair, then we
4101
    -- update the node keys.
4102
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
4103
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
4104
`
4105

4106
type UpsertZombieChannelParams struct {
4107
        Scid     []byte
4108
        Version  int16
4109
        NodeKey1 []byte
4110
        NodeKey2 []byte
4111
}
4112

4113
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
4114
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
4115
                arg.Scid,
×
4116
                arg.Version,
×
4117
                arg.NodeKey1,
×
4118
                arg.NodeKey2,
×
4119
        )
×
4120
        return err
×
4121
}
×
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