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

lightningnetwork / lnd / 21994242646

13 Feb 2026 04:22PM UTC coverage: 65.12% (+0.2%) from 64.883%
21994242646

push

github

web-flow
Merge pull request #10414 from lightningnetwork/elle-g175Prep-base

[g175] graph/db: merge g175 types-prep side branch

673 of 1704 new or added lines in 23 files covered. (39.5%)

104 existing lines in 23 files now uncovered.

139183 of 213734 relevant lines covered (65.12%)

20622.62 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

NEW
70
func (q *Queries) AddV2ChannelProof(ctx context.Context, arg AddV2ChannelProofParams) (sql.Result, error) {
×
NEW
71
        return q.db.ExecContext(ctx, addV2ChannelProof, arg.Scid, arg.Signature)
×
NEW
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,
×
NEW
136
                arg.Signature,
×
NEW
137
                arg.FundingPkScript,
×
NEW
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,
×
NEW
368
                &i.Signature,
×
NEW
369
                &i.FundingPkScript,
×
NEW
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,
×
NEW
493
                &i.GraphChannel.Signature,
×
NEW
494
                &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
513
                &i.Policy1BlockHeight,
×
NEW
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,
×
NEW
530
                &i.Policy2BlockHeight,
×
NEW
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,
×
NEW
563
                &i.Signature,
×
NEW
564
                &i.FundingPkScript,
×
NEW
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,
×
NEW
687
                &i.GraphChannel.Signature,
×
NEW
688
                &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
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,
×
NEW
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,
×
NEW
721
                &i.Policy1BlockHeight,
×
NEW
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,
×
NEW
738
                &i.Policy2BlockHeight,
×
NEW
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,
×
NEW
863
                &i.BlockHeight,
×
NEW
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,
×
NEW
1051
                        &i.GraphChannel.Signature,
×
NEW
1052
                        &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
1073
                        &i.Policy1BlockHeight,
×
NEW
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,
×
NEW
1090
                        &i.Policy2BlockHeight,
×
NEW
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,
×
NEW
1157
                        &i.GraphChannel.Signature,
×
NEW
1158
                        &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
1338
                        &i.GraphChannel.Signature,
×
NEW
1339
                        &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
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,
×
NEW
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,
×
NEW
1372
                        &i.Policy1BlockHeight,
×
NEW
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,
×
NEW
1389
                        &i.Policy2BlockHeight,
×
NEW
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,
×
NEW
1450
                        &i.GraphChannel.Signature,
×
NEW
1451
                        &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
1604
                        &i.GraphChannel.Signature,
×
NEW
1605
                        &i.GraphChannel.FundingPkScript,
×
NEW
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,
×
NEW
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,
×
NEW
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,
×
NEW
1638
                        &i.Policy1BlockHeight,
×
NEW
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,
×
NEW
1655
                        &i.Policy2BlockHeight,
×
NEW
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,
×
NEW
1716
                        &i.Signature,
×
NEW
1717
                        &i.FundingPkScript,
×
NEW
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,
×
NEW
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,
×
NEW
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,
×
NEW
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,
×
NEW
2307
                        &i.Signature,
×
NEW
2308
                        &i.FundingPkScript,
×
NEW
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 getZombieChannel = `-- name: GetZombieChannel :one
2414
SELECT scid, version, node_key_1, node_key_2
2415
FROM graph_zombie_channels
2416
WHERE scid = $1
2417
AND version = $2
2418
`
2419

2420
type GetZombieChannelParams struct {
2421
        Scid    []byte
2422
        Version int16
2423
}
2424

2425
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2426
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2427
        var i GraphZombieChannel
×
2428
        err := row.Scan(
×
2429
                &i.Scid,
×
2430
                &i.Version,
×
2431
                &i.NodeKey1,
×
2432
                &i.NodeKey2,
×
2433
        )
×
2434
        return i, err
×
2435
}
×
2436

2437
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2438
SELECT scid, version, node_key_1, node_key_2
2439
FROM graph_zombie_channels
2440
WHERE version = $1
2441
  AND scid IN (/*SLICE:scids*/?)
2442
`
2443

2444
type GetZombieChannelsSCIDsParams struct {
2445
        Version int16
2446
        Scids   [][]byte
2447
}
2448

2449
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2450
        query := getZombieChannelsSCIDs
×
2451
        var queryParams []interface{}
×
2452
        queryParams = append(queryParams, arg.Version)
×
2453
        if len(arg.Scids) > 0 {
×
2454
                for _, v := range arg.Scids {
×
2455
                        queryParams = append(queryParams, v)
×
2456
                }
×
2457
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2458
        } else {
×
2459
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2460
        }
×
2461
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2462
        if err != nil {
×
2463
                return nil, err
×
2464
        }
×
2465
        defer rows.Close()
×
2466
        var items []GraphZombieChannel
×
2467
        for rows.Next() {
×
2468
                var i GraphZombieChannel
×
2469
                if err := rows.Scan(
×
2470
                        &i.Scid,
×
2471
                        &i.Version,
×
2472
                        &i.NodeKey1,
×
2473
                        &i.NodeKey2,
×
2474
                ); err != nil {
×
2475
                        return nil, err
×
2476
                }
×
2477
                items = append(items, i)
×
2478
        }
2479
        if err := rows.Close(); err != nil {
×
2480
                return nil, err
×
2481
        }
×
2482
        if err := rows.Err(); err != nil {
×
2483
                return nil, err
×
2484
        }
×
2485
        return items, nil
×
2486
}
2487

2488
const highestSCID = `-- name: HighestSCID :one
2489
SELECT scid
2490
FROM graph_channels
2491
WHERE version = $1
2492
ORDER BY scid DESC
2493
LIMIT 1
2494
`
2495

2496
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2497
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2498
        var scid []byte
×
2499
        err := row.Scan(&scid)
×
2500
        return scid, err
×
2501
}
×
2502

2503
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2504
/* ─────────────────────────────────────────────
2505
   graph_channel_features table queries
2506
   ─────────────────────────────────────────────
2507
*/
2508

2509
INSERT INTO graph_channel_features (
2510
    channel_id, feature_bit
2511
) VALUES (
2512
    $1, $2
2513
) ON CONFLICT (channel_id, feature_bit)
2514
    -- Do nothing if the channel_id and feature_bit already exist.
2515
    DO NOTHING
2516
`
2517

2518
type InsertChannelFeatureParams struct {
2519
        ChannelID  int64
2520
        FeatureBit int32
2521
}
2522

2523
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2524
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2525
        return err
×
2526
}
×
2527

2528
const insertChannelMig = `-- name: InsertChannelMig :one
2529
INSERT INTO graph_channels (
2530
    version, scid, node_id_1, node_id_2,
2531
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2532
    node_1_signature, node_2_signature, bitcoin_1_signature,
2533
    bitcoin_2_signature
2534
) VALUES (
2535
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2536
) ON CONFLICT (scid, version)
2537
    -- If a conflict occurs, we have already migrated this channel. However, we
2538
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2539
    -- otherwise, the "RETURNING id" part does not work.
2540
    DO UPDATE SET
2541
        node_id_1 = EXCLUDED.node_id_1,
2542
        node_id_2 = EXCLUDED.node_id_2,
2543
        outpoint = EXCLUDED.outpoint,
2544
        capacity = EXCLUDED.capacity,
2545
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2546
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2547
        node_1_signature = EXCLUDED.node_1_signature,
2548
        node_2_signature = EXCLUDED.node_2_signature,
2549
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2550
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2551
RETURNING id
2552
`
2553

2554
type InsertChannelMigParams struct {
2555
        Version           int16
2556
        Scid              []byte
2557
        NodeID1           int64
2558
        NodeID2           int64
2559
        Outpoint          string
2560
        Capacity          sql.NullInt64
2561
        BitcoinKey1       []byte
2562
        BitcoinKey2       []byte
2563
        Node1Signature    []byte
2564
        Node2Signature    []byte
2565
        Bitcoin1Signature []byte
2566
        Bitcoin2Signature []byte
2567
}
2568

2569
// NOTE: This query is only meant to be used by the graph SQL migration since
2570
// for that migration, in order to be retry-safe, we don't want to error out if
2571
// we re-insert the same channel again (which would error if the normal
2572
// CreateChannel query is used because of the uniqueness constraint on the scid
2573
// and version columns).
2574
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
2575
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
2576
                arg.Version,
×
2577
                arg.Scid,
×
2578
                arg.NodeID1,
×
2579
                arg.NodeID2,
×
2580
                arg.Outpoint,
×
2581
                arg.Capacity,
×
2582
                arg.BitcoinKey1,
×
2583
                arg.BitcoinKey2,
×
2584
                arg.Node1Signature,
×
2585
                arg.Node2Signature,
×
2586
                arg.Bitcoin1Signature,
×
2587
                arg.Bitcoin2Signature,
×
2588
        )
×
2589
        var id int64
×
2590
        err := row.Scan(&id)
×
2591
        return id, err
×
2592
}
×
2593

2594
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2595
/* ─────────────────────────────────────────────
2596
   graph_closed_scid table queries
2597
   ────────────────────────────────────────────-
2598
*/
2599

2600
INSERT INTO graph_closed_scids (scid)
2601
VALUES ($1)
2602
ON CONFLICT (scid) DO NOTHING
2603
`
2604

2605
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
2606
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
2607
        return err
×
2608
}
×
2609

2610
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
2611
INSERT INTO graph_channel_policies (
2612
    version, channel_id, node_id, timelock, fee_ppm,
2613
    base_fee_msat, min_htlc_msat, last_update, disabled,
2614
    max_htlc_msat, inbound_base_fee_msat,
2615
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2616
    signature
2617
) VALUES  (
2618
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2619
)
2620
ON CONFLICT (channel_id, node_id, version)
2621
    -- If a conflict occurs, we have already migrated this policy. However, we
2622
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2623
    -- otherwise, the "RETURNING id" part does not work.
2624
    DO UPDATE SET
2625
        timelock = EXCLUDED.timelock,
2626
        fee_ppm = EXCLUDED.fee_ppm,
2627
        base_fee_msat = EXCLUDED.base_fee_msat,
2628
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2629
        last_update = EXCLUDED.last_update,
2630
        disabled = EXCLUDED.disabled,
2631
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2632
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2633
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2634
        message_flags = EXCLUDED.message_flags,
2635
        channel_flags = EXCLUDED.channel_flags,
2636
        signature = EXCLUDED.signature
2637
RETURNING id
2638
`
2639

2640
type InsertEdgePolicyMigParams struct {
2641
        Version                 int16
2642
        ChannelID               int64
2643
        NodeID                  int64
2644
        Timelock                int32
2645
        FeePpm                  int64
2646
        BaseFeeMsat             int64
2647
        MinHtlcMsat             int64
2648
        LastUpdate              sql.NullInt64
2649
        Disabled                sql.NullBool
2650
        MaxHtlcMsat             sql.NullInt64
2651
        InboundBaseFeeMsat      sql.NullInt64
2652
        InboundFeeRateMilliMsat sql.NullInt64
2653
        MessageFlags            sql.NullInt16
2654
        ChannelFlags            sql.NullInt16
2655
        Signature               []byte
2656
}
2657

2658
// NOTE: This query is only meant to be used by the graph SQL migration since
2659
// for that migration, in order to be retry-safe, we don't want to error out if
2660
// we re-insert the same policy (which would error if the normal
2661
// UpsertEdgePolicy query is used because of the constraint in that query that
2662
// requires a policy update to have a newer last_update than the existing one).
2663
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
2664
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
2665
                arg.Version,
×
2666
                arg.ChannelID,
×
2667
                arg.NodeID,
×
2668
                arg.Timelock,
×
2669
                arg.FeePpm,
×
2670
                arg.BaseFeeMsat,
×
2671
                arg.MinHtlcMsat,
×
2672
                arg.LastUpdate,
×
2673
                arg.Disabled,
×
2674
                arg.MaxHtlcMsat,
×
2675
                arg.InboundBaseFeeMsat,
×
2676
                arg.InboundFeeRateMilliMsat,
×
2677
                arg.MessageFlags,
×
2678
                arg.ChannelFlags,
×
2679
                arg.Signature,
×
2680
        )
×
2681
        var id int64
×
2682
        err := row.Scan(&id)
×
2683
        return id, err
×
2684
}
×
2685

2686
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2687
/* ─────────────────────────────────────────────
2688
   graph_node_features table queries
2689
   ─────────────────────────────────────────────
2690
*/
2691

2692
INSERT INTO graph_node_features (
2693
    node_id, feature_bit
2694
) VALUES (
2695
    $1, $2
2696
) ON CONFLICT (node_id, feature_bit)
2697
    -- Do nothing if the feature already exists for the node.
2698
    DO NOTHING
2699
`
2700

2701
type InsertNodeFeatureParams struct {
2702
        NodeID     int64
2703
        FeatureBit int32
2704
}
2705

2706
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
2707
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
2708
        return err
×
2709
}
×
2710

2711
const insertNodeMig = `-- name: InsertNodeMig :one
2712
/* ─────────────────────────────────────────────
2713
   Migration specific queries
2714

2715
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2716
   dedicated to the migration that requires it, and so we can then remove
2717
   it from the main set of "live" queries that the code-base has access to.
2718
   ────────────────────────────────────────────-
2719
*/
2720

2721
INSERT INTO graph_nodes (
2722
    version, pub_key, alias, last_update, color, signature
2723
) VALUES (
2724
    $1, $2, $3, $4, $5, $6
2725
)
2726
ON CONFLICT (pub_key, version)
2727
    -- If a conflict occurs, we have already migrated this node. However, we
2728
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2729
    -- otherwise, the "RETURNING id" part does not work.
2730
    DO UPDATE SET
2731
        alias = EXCLUDED.alias,
2732
        last_update = EXCLUDED.last_update,
2733
        color = EXCLUDED.color,
2734
        signature = EXCLUDED.signature
2735
RETURNING id
2736
`
2737

2738
type InsertNodeMigParams struct {
2739
        Version    int16
2740
        PubKey     []byte
2741
        Alias      sql.NullString
2742
        LastUpdate sql.NullInt64
2743
        Color      sql.NullString
2744
        Signature  []byte
2745
}
2746

2747
// NOTE: This query is only meant to be used by the graph SQL migration since
2748
// for that migration, in order to be retry-safe, we don't want to error out if
2749
// we re-insert the same node (which would error if the normal UpsertNode query
2750
// is used because of the constraint in that query that requires a node update
2751
// to have a newer last_update than the existing node).
2752
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
2753
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
2754
                arg.Version,
×
2755
                arg.PubKey,
×
2756
                arg.Alias,
×
2757
                arg.LastUpdate,
×
2758
                arg.Color,
×
2759
                arg.Signature,
×
2760
        )
×
2761
        var id int64
×
2762
        err := row.Scan(&id)
×
2763
        return id, err
×
2764
}
×
2765

2766
const isClosedChannel = `-- name: IsClosedChannel :one
2767
SELECT EXISTS (
2768
    SELECT 1
2769
    FROM graph_closed_scids
2770
    WHERE scid = $1
2771
)
2772
`
2773

2774
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
2775
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
2776
        var exists bool
×
2777
        err := row.Scan(&exists)
×
2778
        return exists, err
×
2779
}
×
2780

2781
const isPublicV1Node = `-- name: IsPublicV1Node :one
2782
SELECT EXISTS (
2783
    SELECT 1
2784
    FROM graph_channels c
2785
    JOIN graph_nodes n ON n.id = c.node_id_1
2786
    -- NOTE: we hard-code the version here since the clauses
2787
    -- here that determine if a node is public is specific
2788
    -- to the V1 gossip protocol. In V1, a node is public
2789
    -- if it has a public channel and a public channel is one
2790
    -- where we have the set of signatures of the channel
2791
    -- announcement. It is enough to just check that we have
2792
    -- one of the signatures since we only ever set them
2793
    -- together.
2794
    WHERE c.version = 1
2795
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2796
      AND n.pub_key = $1
2797
    UNION ALL
2798
    SELECT 1
2799
    FROM graph_channels c
2800
    JOIN graph_nodes n ON n.id = c.node_id_2
2801
    WHERE c.version = 1
2802
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
2803
      AND n.pub_key = $1
2804
)
2805
`
2806

2807
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2808
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2809
        var exists bool
×
2810
        err := row.Scan(&exists)
×
2811
        return exists, err
×
2812
}
×
2813

2814
const isPublicV2Node = `-- name: IsPublicV2Node :one
2815
SELECT EXISTS (
2816
    SELECT 1
2817
    FROM graph_channels c
2818
    JOIN graph_nodes n ON n.id = c.node_id_1
2819
    -- NOTE: we hard-code the version here since the clauses
2820
    -- here that determine if a node is public is specific
2821
    -- to the V2 gossip protocol.
2822
    WHERE c.version = 2
2823
      AND COALESCE(length(c.signature), 0) > 0
2824
      AND n.pub_key = $1
2825

2826
    UNION ALL
2827

2828
    SELECT 1
2829
    FROM graph_channels c
2830
    JOIN graph_nodes n ON n.id = c.node_id_2
2831
    WHERE c.version = 2
2832
      AND COALESCE(length(c.signature), 0) > 0
2833
      AND n.pub_key = $1
2834
)
2835
`
2836

NEW
2837
func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) {
×
NEW
2838
        row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey)
×
NEW
2839
        var exists bool
×
NEW
2840
        err := row.Scan(&exists)
×
NEW
2841
        return exists, err
×
NEW
2842
}
×
2843

2844
const isZombieChannel = `-- name: IsZombieChannel :one
2845
SELECT EXISTS (
2846
    SELECT 1
2847
    FROM graph_zombie_channels
2848
    WHERE scid = $1
2849
    AND version = $2
2850
) AS is_zombie
2851
`
2852

2853
type IsZombieChannelParams struct {
2854
        Scid    []byte
2855
        Version int16
2856
}
2857

2858
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2859
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2860
        var is_zombie bool
×
2861
        err := row.Scan(&is_zombie)
×
2862
        return is_zombie, err
×
2863
}
×
2864

2865
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2866
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,
2867
    n1.pub_key AS node1_pubkey,
2868
    n2.pub_key AS node2_pubkey,
2869

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

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

2911
FROM graph_channels c
2912
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2913
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2914
    LEFT JOIN graph_channel_policies cp1
2915
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2916
    LEFT JOIN graph_channel_policies cp2
2917
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2918
WHERE c.version = $1
2919
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2920
`
2921

2922
type ListChannelsByNodeIDParams struct {
2923
        Version int16
2924
        NodeID1 int64
2925
}
2926

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

2967
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
2968
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
2969
        if err != nil {
×
2970
                return nil, err
×
2971
        }
×
2972
        defer rows.Close()
×
2973
        var items []ListChannelsByNodeIDRow
×
2974
        for rows.Next() {
×
2975
                var i ListChannelsByNodeIDRow
×
2976
                if err := rows.Scan(
×
2977
                        &i.GraphChannel.ID,
×
2978
                        &i.GraphChannel.Version,
×
2979
                        &i.GraphChannel.Scid,
×
2980
                        &i.GraphChannel.NodeID1,
×
2981
                        &i.GraphChannel.NodeID2,
×
2982
                        &i.GraphChannel.Outpoint,
×
2983
                        &i.GraphChannel.Capacity,
×
2984
                        &i.GraphChannel.BitcoinKey1,
×
2985
                        &i.GraphChannel.BitcoinKey2,
×
2986
                        &i.GraphChannel.Node1Signature,
×
2987
                        &i.GraphChannel.Node2Signature,
×
2988
                        &i.GraphChannel.Bitcoin1Signature,
×
2989
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
2990
                        &i.GraphChannel.Signature,
×
NEW
2991
                        &i.GraphChannel.FundingPkScript,
×
NEW
2992
                        &i.GraphChannel.MerkleRootHash,
×
2993
                        &i.Node1Pubkey,
×
2994
                        &i.Node2Pubkey,
×
2995
                        &i.Policy1ID,
×
2996
                        &i.Policy1NodeID,
×
2997
                        &i.Policy1Version,
×
2998
                        &i.Policy1Timelock,
×
2999
                        &i.Policy1FeePpm,
×
3000
                        &i.Policy1BaseFeeMsat,
×
3001
                        &i.Policy1MinHtlcMsat,
×
3002
                        &i.Policy1MaxHtlcMsat,
×
3003
                        &i.Policy1LastUpdate,
×
3004
                        &i.Policy1Disabled,
×
3005
                        &i.Policy1InboundBaseFeeMsat,
×
3006
                        &i.Policy1InboundFeeRateMilliMsat,
×
3007
                        &i.Policy1MessageFlags,
×
3008
                        &i.Policy1ChannelFlags,
×
3009
                        &i.Policy1Signature,
×
NEW
3010
                        &i.Policy1BlockHeight,
×
NEW
3011
                        &i.Policy1DisableFlags,
×
3012
                        &i.Policy2ID,
×
3013
                        &i.Policy2NodeID,
×
3014
                        &i.Policy2Version,
×
3015
                        &i.Policy2Timelock,
×
3016
                        &i.Policy2FeePpm,
×
3017
                        &i.Policy2BaseFeeMsat,
×
3018
                        &i.Policy2MinHtlcMsat,
×
3019
                        &i.Policy2MaxHtlcMsat,
×
3020
                        &i.Policy2LastUpdate,
×
3021
                        &i.Policy2Disabled,
×
3022
                        &i.Policy2InboundBaseFeeMsat,
×
3023
                        &i.Policy2InboundFeeRateMilliMsat,
×
3024
                        &i.Policy2MessageFlags,
×
3025
                        &i.Policy2ChannelFlags,
×
3026
                        &i.Policy2Signature,
×
NEW
3027
                        &i.Policy2BlockHeight,
×
NEW
3028
                        &i.Policy2DisableFlags,
×
3029
                ); err != nil {
×
3030
                        return nil, err
×
3031
                }
×
3032
                items = append(items, i)
×
3033
        }
3034
        if err := rows.Close(); err != nil {
×
3035
                return nil, err
×
3036
        }
×
3037
        if err := rows.Err(); err != nil {
×
3038
                return nil, err
×
3039
        }
×
3040
        return items, nil
×
3041
}
3042

3043
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3044
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,
3045
       n1.pub_key AS node1_pubkey,
3046
       n2.pub_key AS node2_pubkey,
3047

3048
       -- Policy 1
3049
       -- TODO(elle): use sqlc.embed to embed policy structs
3050
       --  once this issue is resolved:
3051
       --  https://github.com/sqlc-dev/sqlc/issues/2997
3052
       cp1.id AS policy1_id,
3053
       cp1.node_id AS policy1_node_id,
3054
       cp1.version AS policy1_version,
3055
       cp1.timelock AS policy1_timelock,
3056
       cp1.fee_ppm AS policy1_fee_ppm,
3057
       cp1.base_fee_msat AS policy1_base_fee_msat,
3058
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
3059
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
3060
       cp1.last_update AS policy1_last_update,
3061
       cp1.disabled AS policy1_disabled,
3062
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3063
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3064
       cp1.message_flags AS policy1_message_flags,
3065
       cp1.channel_flags AS policy1_channel_flags,
3066
       cp1.signature AS policy1_signature,
3067
    cp1.block_height AS policy1_block_height,
3068
    cp1.disable_flags AS policy1_disable_flags,
3069

3070
       -- Policy 2
3071
       cp2.id AS policy2_id,
3072
       cp2.node_id AS policy2_node_id,
3073
       cp2.version AS policy2_version,
3074
       cp2.timelock AS policy2_timelock,
3075
       cp2.fee_ppm AS policy2_fee_ppm,
3076
       cp2.base_fee_msat AS policy2_base_fee_msat,
3077
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
3078
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
3079
       cp2.last_update AS policy2_last_update,
3080
       cp2.disabled AS policy2_disabled,
3081
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3082
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3083
       cp2.message_flags AS policy2_message_flags,
3084
       cp2.channel_flags AS policy2_channel_flags,
3085
       cp2.signature AS policy2_signature,
3086
    cp2.block_height AS policy2_block_height,
3087
    cp2.disable_flags AS policy2_disable_flags
3088

3089
FROM graph_channels c
3090
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3091
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3092
         LEFT JOIN graph_channel_policies cp1
3093
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3094
         LEFT JOIN graph_channel_policies cp2
3095
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3096
WHERE c.version = $1
3097
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
3098
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
3099
`
3100

3101
type ListChannelsForNodeIDsParams struct {
3102
        Version  int16
3103
        Node1Ids []int64
3104
        Node2Ids []int64
3105
}
3106

3107
type ListChannelsForNodeIDsRow struct {
3108
        GraphChannel                   GraphChannel
3109
        Node1Pubkey                    []byte
3110
        Node2Pubkey                    []byte
3111
        Policy1ID                      sql.NullInt64
3112
        Policy1NodeID                  sql.NullInt64
3113
        Policy1Version                 sql.NullInt16
3114
        Policy1Timelock                sql.NullInt32
3115
        Policy1FeePpm                  sql.NullInt64
3116
        Policy1BaseFeeMsat             sql.NullInt64
3117
        Policy1MinHtlcMsat             sql.NullInt64
3118
        Policy1MaxHtlcMsat             sql.NullInt64
3119
        Policy1LastUpdate              sql.NullInt64
3120
        Policy1Disabled                sql.NullBool
3121
        Policy1InboundBaseFeeMsat      sql.NullInt64
3122
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3123
        Policy1MessageFlags            sql.NullInt16
3124
        Policy1ChannelFlags            sql.NullInt16
3125
        Policy1Signature               []byte
3126
        Policy1BlockHeight             sql.NullInt64
3127
        Policy1DisableFlags            sql.NullInt16
3128
        Policy2ID                      sql.NullInt64
3129
        Policy2NodeID                  sql.NullInt64
3130
        Policy2Version                 sql.NullInt16
3131
        Policy2Timelock                sql.NullInt32
3132
        Policy2FeePpm                  sql.NullInt64
3133
        Policy2BaseFeeMsat             sql.NullInt64
3134
        Policy2MinHtlcMsat             sql.NullInt64
3135
        Policy2MaxHtlcMsat             sql.NullInt64
3136
        Policy2LastUpdate              sql.NullInt64
3137
        Policy2Disabled                sql.NullBool
3138
        Policy2InboundBaseFeeMsat      sql.NullInt64
3139
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3140
        Policy2MessageFlags            sql.NullInt16
3141
        Policy2ChannelFlags            sql.NullInt16
3142
        Policy2Signature               []byte
3143
        Policy2BlockHeight             sql.NullInt64
3144
        Policy2DisableFlags            sql.NullInt16
3145
}
3146

3147
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
3148
        query := listChannelsForNodeIDs
×
3149
        var queryParams []interface{}
×
3150
        queryParams = append(queryParams, arg.Version)
×
3151
        if len(arg.Node1Ids) > 0 {
×
3152
                for _, v := range arg.Node1Ids {
×
3153
                        queryParams = append(queryParams, v)
×
3154
                }
×
3155
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
3156
        } else {
×
3157
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
3158
        }
×
3159
        if len(arg.Node2Ids) > 0 {
×
3160
                for _, v := range arg.Node2Ids {
×
3161
                        queryParams = append(queryParams, v)
×
3162
                }
×
3163
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
3164
        } else {
×
3165
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
3166
        }
×
3167
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
3168
        if err != nil {
×
3169
                return nil, err
×
3170
        }
×
3171
        defer rows.Close()
×
3172
        var items []ListChannelsForNodeIDsRow
×
3173
        for rows.Next() {
×
3174
                var i ListChannelsForNodeIDsRow
×
3175
                if err := rows.Scan(
×
3176
                        &i.GraphChannel.ID,
×
3177
                        &i.GraphChannel.Version,
×
3178
                        &i.GraphChannel.Scid,
×
3179
                        &i.GraphChannel.NodeID1,
×
3180
                        &i.GraphChannel.NodeID2,
×
3181
                        &i.GraphChannel.Outpoint,
×
3182
                        &i.GraphChannel.Capacity,
×
3183
                        &i.GraphChannel.BitcoinKey1,
×
3184
                        &i.GraphChannel.BitcoinKey2,
×
3185
                        &i.GraphChannel.Node1Signature,
×
3186
                        &i.GraphChannel.Node2Signature,
×
3187
                        &i.GraphChannel.Bitcoin1Signature,
×
3188
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
3189
                        &i.GraphChannel.Signature,
×
NEW
3190
                        &i.GraphChannel.FundingPkScript,
×
NEW
3191
                        &i.GraphChannel.MerkleRootHash,
×
3192
                        &i.Node1Pubkey,
×
3193
                        &i.Node2Pubkey,
×
3194
                        &i.Policy1ID,
×
3195
                        &i.Policy1NodeID,
×
3196
                        &i.Policy1Version,
×
3197
                        &i.Policy1Timelock,
×
3198
                        &i.Policy1FeePpm,
×
3199
                        &i.Policy1BaseFeeMsat,
×
3200
                        &i.Policy1MinHtlcMsat,
×
3201
                        &i.Policy1MaxHtlcMsat,
×
3202
                        &i.Policy1LastUpdate,
×
3203
                        &i.Policy1Disabled,
×
3204
                        &i.Policy1InboundBaseFeeMsat,
×
3205
                        &i.Policy1InboundFeeRateMilliMsat,
×
3206
                        &i.Policy1MessageFlags,
×
3207
                        &i.Policy1ChannelFlags,
×
3208
                        &i.Policy1Signature,
×
NEW
3209
                        &i.Policy1BlockHeight,
×
NEW
3210
                        &i.Policy1DisableFlags,
×
3211
                        &i.Policy2ID,
×
3212
                        &i.Policy2NodeID,
×
3213
                        &i.Policy2Version,
×
3214
                        &i.Policy2Timelock,
×
3215
                        &i.Policy2FeePpm,
×
3216
                        &i.Policy2BaseFeeMsat,
×
3217
                        &i.Policy2MinHtlcMsat,
×
3218
                        &i.Policy2MaxHtlcMsat,
×
3219
                        &i.Policy2LastUpdate,
×
3220
                        &i.Policy2Disabled,
×
3221
                        &i.Policy2InboundBaseFeeMsat,
×
3222
                        &i.Policy2InboundFeeRateMilliMsat,
×
3223
                        &i.Policy2MessageFlags,
×
3224
                        &i.Policy2ChannelFlags,
×
3225
                        &i.Policy2Signature,
×
NEW
3226
                        &i.Policy2BlockHeight,
×
NEW
3227
                        &i.Policy2DisableFlags,
×
3228
                ); err != nil {
×
3229
                        return nil, err
×
3230
                }
×
3231
                items = append(items, i)
×
3232
        }
3233
        if err := rows.Close(); err != nil {
×
3234
                return nil, err
×
3235
        }
×
3236
        if err := rows.Err(); err != nil {
×
3237
                return nil, err
×
3238
        }
×
3239
        return items, nil
×
3240
}
3241

3242
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3243
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3244
FROM graph_channels c
3245
WHERE c.version = $1 AND c.id > $2
3246
ORDER BY c.id
3247
LIMIT $3
3248
`
3249

3250
type ListChannelsPaginatedParams struct {
3251
        Version int16
3252
        ID      int64
3253
        Limit   int32
3254
}
3255

3256
type ListChannelsPaginatedRow struct {
3257
        ID          int64
3258
        BitcoinKey1 []byte
3259
        BitcoinKey2 []byte
3260
        Outpoint    string
3261
}
3262

3263
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3264
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3265
        if err != nil {
×
3266
                return nil, err
×
3267
        }
×
3268
        defer rows.Close()
×
3269
        var items []ListChannelsPaginatedRow
×
3270
        for rows.Next() {
×
3271
                var i ListChannelsPaginatedRow
×
3272
                if err := rows.Scan(
×
3273
                        &i.ID,
×
3274
                        &i.BitcoinKey1,
×
3275
                        &i.BitcoinKey2,
×
3276
                        &i.Outpoint,
×
3277
                ); err != nil {
×
3278
                        return nil, err
×
3279
                }
×
3280
                items = append(items, i)
×
3281
        }
3282
        if err := rows.Close(); err != nil {
×
3283
                return nil, err
×
3284
        }
×
3285
        if err := rows.Err(); err != nil {
×
3286
                return nil, err
×
3287
        }
×
3288
        return items, nil
×
3289
}
3290

3291
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3292
SELECT
3293
    c.id as id,
3294
    c.scid as scid,
3295
    c.capacity AS capacity,
3296

3297
    -- Join node pubkeys
3298
    n1.pub_key AS node1_pubkey,
3299
    n2.pub_key AS node2_pubkey,
3300

3301
    -- Node 1 policy
3302
    cp1.timelock AS policy_1_timelock,
3303
    cp1.fee_ppm AS policy_1_fee_ppm,
3304
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3305
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3306
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3307
    cp1.disabled AS policy_1_disabled,
3308
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3309
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3310
    cp1.message_flags AS policy1_message_flags,
3311
    cp1.channel_flags AS policy1_channel_flags,
3312
    cp1.block_height AS policy1_block_height,
3313
    cp1.disable_flags AS policy1_disable_flags,
3314

3315
    -- Node 2 policy
3316
    cp2.timelock AS policy_2_timelock,
3317
    cp2.fee_ppm AS policy_2_fee_ppm,
3318
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3319
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3320
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3321
    cp2.disabled AS policy_2_disabled,
3322
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3323
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3324
    cp2.message_flags AS policy2_message_flags,
3325
    cp2.channel_flags AS policy2_channel_flags,
3326
    cp2.block_height AS policy2_block_height,
3327
    cp2.disable_flags AS policy2_disable_flags
3328

3329
FROM graph_channels c
3330
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3331
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3332
LEFT JOIN graph_channel_policies cp1
3333
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3334
LEFT JOIN graph_channel_policies cp2
3335
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3336
WHERE c.version = $1 AND c.id > $2
3337
ORDER BY c.id
3338
LIMIT $3
3339
`
3340

3341
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3342
        Version int16
3343
        ID      int64
3344
        Limit   int32
3345
}
3346

3347
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3348
        ID                             int64
3349
        Scid                           []byte
3350
        Capacity                       sql.NullInt64
3351
        Node1Pubkey                    []byte
3352
        Node2Pubkey                    []byte
3353
        Policy1Timelock                sql.NullInt32
3354
        Policy1FeePpm                  sql.NullInt64
3355
        Policy1BaseFeeMsat             sql.NullInt64
3356
        Policy1MinHtlcMsat             sql.NullInt64
3357
        Policy1MaxHtlcMsat             sql.NullInt64
3358
        Policy1Disabled                sql.NullBool
3359
        Policy1InboundBaseFeeMsat      sql.NullInt64
3360
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3361
        Policy1MessageFlags            sql.NullInt16
3362
        Policy1ChannelFlags            sql.NullInt16
3363
        Policy1BlockHeight             sql.NullInt64
3364
        Policy1DisableFlags            sql.NullInt16
3365
        Policy2Timelock                sql.NullInt32
3366
        Policy2FeePpm                  sql.NullInt64
3367
        Policy2BaseFeeMsat             sql.NullInt64
3368
        Policy2MinHtlcMsat             sql.NullInt64
3369
        Policy2MaxHtlcMsat             sql.NullInt64
3370
        Policy2Disabled                sql.NullBool
3371
        Policy2InboundBaseFeeMsat      sql.NullInt64
3372
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3373
        Policy2MessageFlags            sql.NullInt16
3374
        Policy2ChannelFlags            sql.NullInt16
3375
        Policy2BlockHeight             sql.NullInt64
3376
        Policy2DisableFlags            sql.NullInt16
3377
}
3378

3379
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3380
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3381
        if err != nil {
×
3382
                return nil, err
×
3383
        }
×
3384
        defer rows.Close()
×
3385
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3386
        for rows.Next() {
×
3387
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3388
                if err := rows.Scan(
×
3389
                        &i.ID,
×
3390
                        &i.Scid,
×
3391
                        &i.Capacity,
×
3392
                        &i.Node1Pubkey,
×
3393
                        &i.Node2Pubkey,
×
3394
                        &i.Policy1Timelock,
×
3395
                        &i.Policy1FeePpm,
×
3396
                        &i.Policy1BaseFeeMsat,
×
3397
                        &i.Policy1MinHtlcMsat,
×
3398
                        &i.Policy1MaxHtlcMsat,
×
3399
                        &i.Policy1Disabled,
×
3400
                        &i.Policy1InboundBaseFeeMsat,
×
3401
                        &i.Policy1InboundFeeRateMilliMsat,
×
3402
                        &i.Policy1MessageFlags,
×
3403
                        &i.Policy1ChannelFlags,
×
NEW
3404
                        &i.Policy1BlockHeight,
×
NEW
3405
                        &i.Policy1DisableFlags,
×
3406
                        &i.Policy2Timelock,
×
3407
                        &i.Policy2FeePpm,
×
3408
                        &i.Policy2BaseFeeMsat,
×
3409
                        &i.Policy2MinHtlcMsat,
×
3410
                        &i.Policy2MaxHtlcMsat,
×
3411
                        &i.Policy2Disabled,
×
3412
                        &i.Policy2InboundBaseFeeMsat,
×
3413
                        &i.Policy2InboundFeeRateMilliMsat,
×
3414
                        &i.Policy2MessageFlags,
×
3415
                        &i.Policy2ChannelFlags,
×
NEW
3416
                        &i.Policy2BlockHeight,
×
NEW
3417
                        &i.Policy2DisableFlags,
×
3418
                ); err != nil {
×
3419
                        return nil, err
×
3420
                }
×
3421
                items = append(items, i)
×
3422
        }
3423
        if err := rows.Close(); err != nil {
×
3424
                return nil, err
×
3425
        }
×
3426
        if err := rows.Err(); err != nil {
×
3427
                return nil, err
×
3428
        }
×
3429
        return items, nil
×
3430
}
3431

3432
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3433
SELECT
3434
    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,
3435

3436
    -- Join node pubkeys
3437
    n1.pub_key AS node1_pubkey,
3438
    n2.pub_key AS node2_pubkey,
3439

3440
    -- Node 1 policy
3441
    cp1.id AS policy_1_id,
3442
    cp1.node_id AS policy_1_node_id,
3443
    cp1.version AS policy_1_version,
3444
    cp1.timelock AS policy_1_timelock,
3445
    cp1.fee_ppm AS policy_1_fee_ppm,
3446
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3447
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3448
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3449
    cp1.last_update AS policy_1_last_update,
3450
    cp1.disabled AS policy_1_disabled,
3451
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3452
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3453
    cp1.message_flags AS policy1_message_flags,
3454
    cp1.channel_flags AS policy1_channel_flags,
3455
    cp1.block_height AS policy1_block_height,
3456
    cp1.disable_flags AS policy1_disable_flags,
3457
    cp1.signature AS policy_1_signature,
3458

3459
    -- Node 2 policy
3460
    cp2.id AS policy_2_id,
3461
    cp2.node_id AS policy_2_node_id,
3462
    cp2.version AS policy_2_version,
3463
    cp2.timelock AS policy_2_timelock,
3464
    cp2.fee_ppm AS policy_2_fee_ppm,
3465
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3466
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3467
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3468
    cp2.last_update AS policy_2_last_update,
3469
    cp2.disabled AS policy_2_disabled,
3470
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3471
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3472
    cp2.message_flags AS policy2_message_flags,
3473
    cp2.channel_flags AS policy2_channel_flags,
3474
    cp2.signature AS policy_2_signature,
3475
    cp2.block_height AS policy_2_block_height,
3476
    cp2.disable_flags AS policy_2_disable_flags
3477

3478
FROM graph_channels c
3479
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3480
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3481
LEFT JOIN graph_channel_policies cp1
3482
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3483
LEFT JOIN graph_channel_policies cp2
3484
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3485
WHERE c.version = $1 AND c.id > $2
3486
ORDER BY c.id
3487
LIMIT $3
3488
`
3489

3490
type ListChannelsWithPoliciesPaginatedParams struct {
3491
        Version int16
3492
        ID      int64
3493
        Limit   int32
3494
}
3495

3496
type ListChannelsWithPoliciesPaginatedRow struct {
3497
        GraphChannel                   GraphChannel
3498
        Node1Pubkey                    []byte
3499
        Node2Pubkey                    []byte
3500
        Policy1ID                      sql.NullInt64
3501
        Policy1NodeID                  sql.NullInt64
3502
        Policy1Version                 sql.NullInt16
3503
        Policy1Timelock                sql.NullInt32
3504
        Policy1FeePpm                  sql.NullInt64
3505
        Policy1BaseFeeMsat             sql.NullInt64
3506
        Policy1MinHtlcMsat             sql.NullInt64
3507
        Policy1MaxHtlcMsat             sql.NullInt64
3508
        Policy1LastUpdate              sql.NullInt64
3509
        Policy1Disabled                sql.NullBool
3510
        Policy1InboundBaseFeeMsat      sql.NullInt64
3511
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3512
        Policy1MessageFlags            sql.NullInt16
3513
        Policy1ChannelFlags            sql.NullInt16
3514
        Policy1BlockHeight             sql.NullInt64
3515
        Policy1DisableFlags            sql.NullInt16
3516
        Policy1Signature               []byte
3517
        Policy2ID                      sql.NullInt64
3518
        Policy2NodeID                  sql.NullInt64
3519
        Policy2Version                 sql.NullInt16
3520
        Policy2Timelock                sql.NullInt32
3521
        Policy2FeePpm                  sql.NullInt64
3522
        Policy2BaseFeeMsat             sql.NullInt64
3523
        Policy2MinHtlcMsat             sql.NullInt64
3524
        Policy2MaxHtlcMsat             sql.NullInt64
3525
        Policy2LastUpdate              sql.NullInt64
3526
        Policy2Disabled                sql.NullBool
3527
        Policy2InboundBaseFeeMsat      sql.NullInt64
3528
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3529
        Policy2MessageFlags            sql.NullInt16
3530
        Policy2ChannelFlags            sql.NullInt16
3531
        Policy2Signature               []byte
3532
        Policy2BlockHeight             sql.NullInt64
3533
        Policy2DisableFlags            sql.NullInt16
3534
}
3535

3536
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
3537
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
3538
        if err != nil {
×
3539
                return nil, err
×
3540
        }
×
3541
        defer rows.Close()
×
3542
        var items []ListChannelsWithPoliciesPaginatedRow
×
3543
        for rows.Next() {
×
3544
                var i ListChannelsWithPoliciesPaginatedRow
×
3545
                if err := rows.Scan(
×
3546
                        &i.GraphChannel.ID,
×
3547
                        &i.GraphChannel.Version,
×
3548
                        &i.GraphChannel.Scid,
×
3549
                        &i.GraphChannel.NodeID1,
×
3550
                        &i.GraphChannel.NodeID2,
×
3551
                        &i.GraphChannel.Outpoint,
×
3552
                        &i.GraphChannel.Capacity,
×
3553
                        &i.GraphChannel.BitcoinKey1,
×
3554
                        &i.GraphChannel.BitcoinKey2,
×
3555
                        &i.GraphChannel.Node1Signature,
×
3556
                        &i.GraphChannel.Node2Signature,
×
3557
                        &i.GraphChannel.Bitcoin1Signature,
×
3558
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
3559
                        &i.GraphChannel.Signature,
×
NEW
3560
                        &i.GraphChannel.FundingPkScript,
×
NEW
3561
                        &i.GraphChannel.MerkleRootHash,
×
3562
                        &i.Node1Pubkey,
×
3563
                        &i.Node2Pubkey,
×
3564
                        &i.Policy1ID,
×
3565
                        &i.Policy1NodeID,
×
3566
                        &i.Policy1Version,
×
3567
                        &i.Policy1Timelock,
×
3568
                        &i.Policy1FeePpm,
×
3569
                        &i.Policy1BaseFeeMsat,
×
3570
                        &i.Policy1MinHtlcMsat,
×
3571
                        &i.Policy1MaxHtlcMsat,
×
3572
                        &i.Policy1LastUpdate,
×
3573
                        &i.Policy1Disabled,
×
3574
                        &i.Policy1InboundBaseFeeMsat,
×
3575
                        &i.Policy1InboundFeeRateMilliMsat,
×
3576
                        &i.Policy1MessageFlags,
×
3577
                        &i.Policy1ChannelFlags,
×
NEW
3578
                        &i.Policy1BlockHeight,
×
NEW
3579
                        &i.Policy1DisableFlags,
×
3580
                        &i.Policy1Signature,
×
3581
                        &i.Policy2ID,
×
3582
                        &i.Policy2NodeID,
×
3583
                        &i.Policy2Version,
×
3584
                        &i.Policy2Timelock,
×
3585
                        &i.Policy2FeePpm,
×
3586
                        &i.Policy2BaseFeeMsat,
×
3587
                        &i.Policy2MinHtlcMsat,
×
3588
                        &i.Policy2MaxHtlcMsat,
×
3589
                        &i.Policy2LastUpdate,
×
3590
                        &i.Policy2Disabled,
×
3591
                        &i.Policy2InboundBaseFeeMsat,
×
3592
                        &i.Policy2InboundFeeRateMilliMsat,
×
3593
                        &i.Policy2MessageFlags,
×
3594
                        &i.Policy2ChannelFlags,
×
3595
                        &i.Policy2Signature,
×
NEW
3596
                        &i.Policy2BlockHeight,
×
NEW
3597
                        &i.Policy2DisableFlags,
×
3598
                ); err != nil {
×
3599
                        return nil, err
×
3600
                }
×
3601
                items = append(items, i)
×
3602
        }
3603
        if err := rows.Close(); err != nil {
×
3604
                return nil, err
×
3605
        }
×
3606
        if err := rows.Err(); err != nil {
×
3607
                return nil, err
×
3608
        }
×
3609
        return items, nil
×
3610
}
3611

3612
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3613
SELECT id, pub_key
3614
FROM graph_nodes
3615
WHERE version = $1  AND id > $2
3616
ORDER BY id
3617
LIMIT $3
3618
`
3619

3620
type ListNodeIDsAndPubKeysParams struct {
3621
        Version int16
3622
        ID      int64
3623
        Limit   int32
3624
}
3625

3626
type ListNodeIDsAndPubKeysRow struct {
3627
        ID     int64
3628
        PubKey []byte
3629
}
3630

3631
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
3632
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
3633
        if err != nil {
×
3634
                return nil, err
×
3635
        }
×
3636
        defer rows.Close()
×
3637
        var items []ListNodeIDsAndPubKeysRow
×
3638
        for rows.Next() {
×
3639
                var i ListNodeIDsAndPubKeysRow
×
3640
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
3641
                        return nil, err
×
3642
                }
×
3643
                items = append(items, i)
×
3644
        }
3645
        if err := rows.Close(); err != nil {
×
3646
                return nil, err
×
3647
        }
×
3648
        if err := rows.Err(); err != nil {
×
3649
                return nil, err
×
3650
        }
×
3651
        return items, nil
×
3652
}
3653

3654
const listNodesPaginated = `-- name: ListNodesPaginated :many
3655
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
3656
FROM graph_nodes
3657
WHERE version = $1 AND id > $2
3658
ORDER BY id
3659
LIMIT $3
3660
`
3661

3662
type ListNodesPaginatedParams struct {
3663
        Version int16
3664
        ID      int64
3665
        Limit   int32
3666
}
3667

3668
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
3669
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
3670
        if err != nil {
×
3671
                return nil, err
×
3672
        }
×
3673
        defer rows.Close()
×
3674
        var items []GraphNode
×
3675
        for rows.Next() {
×
3676
                var i GraphNode
×
3677
                if err := rows.Scan(
×
3678
                        &i.ID,
×
3679
                        &i.Version,
×
3680
                        &i.PubKey,
×
3681
                        &i.Alias,
×
3682
                        &i.LastUpdate,
×
3683
                        &i.Color,
×
3684
                        &i.Signature,
×
NEW
3685
                        &i.BlockHeight,
×
3686
                ); err != nil {
×
3687
                        return nil, err
×
3688
                }
×
3689
                items = append(items, i)
×
3690
        }
3691
        if err := rows.Close(); err != nil {
×
3692
                return nil, err
×
3693
        }
×
3694
        if err := rows.Err(); err != nil {
×
3695
                return nil, err
×
3696
        }
×
3697
        return items, nil
×
3698
}
3699

3700
const nodeExists = `-- name: NodeExists :one
3701
SELECT EXISTS (
3702
    SELECT 1
3703
    FROM graph_nodes
3704
    WHERE pub_key = $1
3705
      AND version = $2
3706
) AS node_exists
3707
`
3708

3709
type NodeExistsParams struct {
3710
        PubKey  []byte
3711
        Version int16
3712
}
3713

NEW
3714
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
NEW
3715
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
NEW
3716
        var node_exists bool
×
NEW
3717
        err := row.Scan(&node_exists)
×
NEW
3718
        return node_exists, err
×
NEW
3719
}
×
3720

3721
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3722
/* ─────────────────────────────────────────────
3723
   graph_channel_policy_extra_types table queries
3724
   ─────────────────────────────────────────────
3725
*/
3726

3727
INSERT INTO graph_channel_policy_extra_types (
3728
    channel_policy_id, type, value
3729
)
3730
VALUES ($1, $2, $3)
3731
ON CONFLICT (channel_policy_id, type)
3732
    -- If a conflict occurs on channel_policy_id and type, then we update the
3733
    -- value.
3734
    DO UPDATE SET value = EXCLUDED.value
3735
`
3736

3737
type UpsertChanPolicyExtraTypeParams struct {
3738
        ChannelPolicyID int64
3739
        Type            int64
3740
        Value           []byte
3741
}
3742

3743
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
3744
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
3745
        return err
×
3746
}
×
3747

3748
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3749
/* ─────────────────────────────────────────────
3750
   graph_channel_extra_types table queries
3751
   ─────────────────────────────────────────────
3752
*/
3753

3754
INSERT INTO graph_channel_extra_types (
3755
    channel_id, type, value
3756
)
3757
VALUES ($1, $2, $3)
3758
    ON CONFLICT (channel_id, type)
3759
    -- Update the value if a conflict occurs on channel_id and type.
3760
    DO UPDATE SET value = EXCLUDED.value
3761
`
3762

3763
type UpsertChannelExtraTypeParams struct {
3764
        ChannelID int64
3765
        Type      int64
3766
        Value     []byte
3767
}
3768

3769
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
3770
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
3771
        return err
×
3772
}
×
3773

3774
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3775
/* ─────────────────────────────────────────────
3776
   graph_channel_policies table queries
3777
   ─────────────────────────────────────────────
3778
*/
3779

3780
INSERT INTO graph_channel_policies (
3781
    version, channel_id, node_id, timelock, fee_ppm,
3782
    base_fee_msat, min_htlc_msat, last_update, disabled,
3783
    max_htlc_msat, inbound_base_fee_msat,
3784
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3785
    signature, block_height, disable_flags
3786
) VALUES  (
3787
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
3788
)
3789
ON CONFLICT (channel_id, node_id, version)
3790
    -- Update the following fields if a conflict occurs on channel_id,
3791
    -- node_id, and version.
3792
    DO UPDATE SET
3793
        timelock = EXCLUDED.timelock,
3794
        fee_ppm = EXCLUDED.fee_ppm,
3795
        base_fee_msat = EXCLUDED.base_fee_msat,
3796
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3797
        last_update = EXCLUDED.last_update,
3798
        disabled = EXCLUDED.disabled,
3799
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3800
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3801
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3802
        message_flags = EXCLUDED.message_flags,
3803
        channel_flags = EXCLUDED.channel_flags,
3804
        signature = EXCLUDED.signature,
3805
        block_height = EXCLUDED.block_height,
3806
        disable_flags = EXCLUDED.disable_flags
3807
WHERE (
3808
    EXCLUDED.version = 1 AND (
3809
        graph_channel_policies.last_update IS NULL
3810
        OR EXCLUDED.last_update > graph_channel_policies.last_update
3811
    )
3812
)
3813
OR (
3814
    EXCLUDED.version = 2 AND (
3815
        graph_channel_policies.block_height IS NULL
3816
        OR EXCLUDED.block_height >= graph_channel_policies.block_height
3817
    )
3818
)
3819
RETURNING id
3820
`
3821

3822
type UpsertEdgePolicyParams struct {
3823
        Version                 int16
3824
        ChannelID               int64
3825
        NodeID                  int64
3826
        Timelock                int32
3827
        FeePpm                  int64
3828
        BaseFeeMsat             int64
3829
        MinHtlcMsat             int64
3830
        LastUpdate              sql.NullInt64
3831
        Disabled                sql.NullBool
3832
        MaxHtlcMsat             sql.NullInt64
3833
        InboundBaseFeeMsat      sql.NullInt64
3834
        InboundFeeRateMilliMsat sql.NullInt64
3835
        MessageFlags            sql.NullInt16
3836
        ChannelFlags            sql.NullInt16
3837
        Signature               []byte
3838
        BlockHeight             sql.NullInt64
3839
        DisableFlags            sql.NullInt16
3840
}
3841

3842
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
3843
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
3844
                arg.Version,
×
3845
                arg.ChannelID,
×
3846
                arg.NodeID,
×
3847
                arg.Timelock,
×
3848
                arg.FeePpm,
×
3849
                arg.BaseFeeMsat,
×
3850
                arg.MinHtlcMsat,
×
3851
                arg.LastUpdate,
×
3852
                arg.Disabled,
×
3853
                arg.MaxHtlcMsat,
×
3854
                arg.InboundBaseFeeMsat,
×
3855
                arg.InboundFeeRateMilliMsat,
×
3856
                arg.MessageFlags,
×
3857
                arg.ChannelFlags,
×
3858
                arg.Signature,
×
NEW
3859
                arg.BlockHeight,
×
NEW
3860
                arg.DisableFlags,
×
3861
        )
×
3862
        var id int64
×
3863
        err := row.Scan(&id)
×
3864
        return id, err
×
3865
}
×
3866

3867
const upsertNode = `-- name: UpsertNode :one
3868
/* ─────────────────────────────────────────────
3869
   graph_nodes table queries
3870
   ───────────────────────────��─────────────────
3871
*/
3872

3873
INSERT INTO graph_nodes (
3874
    version, pub_key, alias, last_update, block_height, color, signature
3875
) VALUES (
3876
    $1, $2, $3, $4, $5, $6, $7
3877
)
3878
ON CONFLICT (pub_key, version)
3879
    -- Update the following fields if a conflict occurs on pub_key
3880
    -- and version.
3881
    DO UPDATE SET
3882
        alias = EXCLUDED.alias,
3883
        last_update = EXCLUDED.last_update,
3884
        block_height = EXCLUDED.block_height,
3885
        color = EXCLUDED.color,
3886
        signature = EXCLUDED.signature
3887
WHERE (graph_nodes.last_update IS NULL
3888
    OR EXCLUDED.last_update > graph_nodes.last_update)
3889
AND (graph_nodes.block_height IS NULL
3890
    OR EXCLUDED.block_height >= graph_nodes.block_height)
3891
RETURNING id
3892
`
3893

3894
type UpsertNodeParams struct {
3895
        Version     int16
3896
        PubKey      []byte
3897
        Alias       sql.NullString
3898
        LastUpdate  sql.NullInt64
3899
        BlockHeight sql.NullInt64
3900
        Color       sql.NullString
3901
        Signature   []byte
3902
}
3903

3904
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3905
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3906
                arg.Version,
×
3907
                arg.PubKey,
×
3908
                arg.Alias,
×
3909
                arg.LastUpdate,
×
NEW
3910
                arg.BlockHeight,
×
3911
                arg.Color,
×
3912
                arg.Signature,
×
3913
        )
×
3914
        var id int64
×
3915
        err := row.Scan(&id)
×
3916
        return id, err
×
3917
}
×
3918

3919
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3920
/* ─────────────────────────────────────────────
3921
   graph_node_addresses table queries
3922
   ───────────────────────────────────��─────────
3923
*/
3924

3925
INSERT INTO graph_node_addresses (
3926
    node_id,
3927
    type,
3928
    address,
3929
    position
3930
) VALUES (
3931
    $1, $2, $3, $4
3932
) ON CONFLICT (node_id, type, position)
3933
    DO UPDATE SET address = EXCLUDED.address
3934
`
3935

3936
type UpsertNodeAddressParams struct {
3937
        NodeID   int64
3938
        Type     int16
3939
        Address  string
3940
        Position int32
3941
}
3942

3943
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3944
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3945
                arg.NodeID,
×
3946
                arg.Type,
×
3947
                arg.Address,
×
3948
                arg.Position,
×
3949
        )
×
3950
        return err
×
3951
}
×
3952

3953
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3954
/* ─────────────────────────────────────────────
3955
   graph_node_extra_types table queries
3956
   ─────────────────────────────────────────────
3957
*/
3958

3959
INSERT INTO graph_node_extra_types (
3960
    node_id, type, value
3961
)
3962
VALUES ($1, $2, $3)
3963
ON CONFLICT (type, node_id)
3964
    -- Update the value if a conflict occurs on type
3965
    -- and node_id.
3966
    DO UPDATE SET value = EXCLUDED.value
3967
`
3968

3969
type UpsertNodeExtraTypeParams struct {
3970
        NodeID int64
3971
        Type   int64
3972
        Value  []byte
3973
}
3974

3975
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
3976
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
3977
        return err
×
3978
}
×
3979

3980
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3981
/* ───────────────────────────���─────────────────
3982
    graph_prune_log table queries
3983
    ─────────────────────────────────────────────
3984
*/
3985

3986
INSERT INTO graph_prune_log (
3987
    block_height, block_hash
3988
) VALUES (
3989
    $1, $2
3990
)
3991
ON CONFLICT(block_height) DO UPDATE SET
3992
    block_hash = EXCLUDED.block_hash
3993
`
3994

3995
type UpsertPruneLogEntryParams struct {
3996
        BlockHeight int64
3997
        BlockHash   []byte
3998
}
3999

4000
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
4001
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
4002
        return err
×
4003
}
×
4004

4005
const upsertSourceNode = `-- name: UpsertSourceNode :one
4006
INSERT INTO graph_nodes (
4007
    version, pub_key, alias, last_update, block_height, color, signature
4008
) VALUES (
4009
    $1, $2, $3, $4, $5, $6, $7
4010
)
4011
ON CONFLICT (pub_key, version)
4012
    -- Update the following fields if a conflict occurs on pub_key
4013
    -- and version.
4014
    DO UPDATE SET
4015
        alias = EXCLUDED.alias,
4016
        last_update = EXCLUDED.last_update,
4017
        block_height = EXCLUDED.block_height,
4018
        color = EXCLUDED.color,
4019
        signature = EXCLUDED.signature
4020
WHERE graph_nodes.last_update IS NULL
4021
    OR EXCLUDED.last_update >= graph_nodes.last_update
4022
AND (graph_nodes.block_height IS NULL
4023
   OR EXCLUDED.block_height >= graph_nodes.block_height)
4024
RETURNING id
4025
`
4026

4027
type UpsertSourceNodeParams struct {
4028
        Version     int16
4029
        PubKey      []byte
4030
        Alias       sql.NullString
4031
        LastUpdate  sql.NullInt64
4032
        BlockHeight sql.NullInt64
4033
        Color       sql.NullString
4034
        Signature   []byte
4035
}
4036

4037
// We use a separate upsert for our own node since we want to be less strict
4038
// about the last_update field. For our own node, we always want to
4039
// update the record even if the last_update is the same as what we have.
4040
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
4041
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
4042
                arg.Version,
×
4043
                arg.PubKey,
×
4044
                arg.Alias,
×
4045
                arg.LastUpdate,
×
NEW
4046
                arg.BlockHeight,
×
4047
                arg.Color,
×
4048
                arg.Signature,
×
4049
        )
×
4050
        var id int64
×
4051
        err := row.Scan(&id)
×
4052
        return id, err
×
4053
}
×
4054

4055
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
4056
/* ─────────────────────────────────────────────
4057
   graph_zombie_channels table queries
4058
   ─────────────────────────────────────────────
4059
*/
4060

4061
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
4062
VALUES ($1, $2, $3, $4)
4063
ON CONFLICT (scid, version)
4064
DO UPDATE SET
4065
    -- If a conflict exists for the SCID and version pair, then we
4066
    -- update the node keys.
4067
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
4068
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
4069
`
4070

4071
type UpsertZombieChannelParams struct {
4072
        Scid     []byte
4073
        Version  int16
4074
        NodeKey1 []byte
4075
        NodeKey2 []byte
4076
}
4077

4078
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
4079
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
4080
                arg.Scid,
×
4081
                arg.Version,
×
4082
                arg.NodeKey1,
×
4083
                arg.NodeKey2,
×
4084
        )
×
4085
        return err
×
4086
}
×
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