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

lightningnetwork / lnd / 23646040644

27 Mar 2026 12:24PM UTC coverage: 61.851% (-0.3%) from 62.138%
23646040644

Pull #10656

github

web-flow
Merge 1ef529b0f into fb1a7e6a4
Pull Request #10656: graph/db: cross-version graph Store

204 of 1232 new or added lines in 7 files covered. (16.56%)

306 existing lines in 36 files now uncovered.

141429 of 228659 relevant lines covered (61.85%)

19328.63 hits per line

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

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

6
package sqlc
7

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

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

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

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

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

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

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

58
const addV2ChannelProof = `-- name: AddV2ChannelProof :execresult
59
UPDATE graph_channels
60
SET signature = $2
61
WHERE scid = $1
62
  AND version = 2
63
`
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

242
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
243
DELETE FROM graph_prune_log
244
WHERE block_height >= $1
245
  AND block_height <= $2
246
`
247

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

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

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

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

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

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

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

313
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
314
SELECT
315
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
316
    n1.pub_key AS node1_pub_key,
317
    n2.pub_key AS node2_pub_key
318
FROM graph_channels c
319
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
320
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
321
WHERE c.scid = $1
322
  AND c.version = $2
323
`
324

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

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

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

377
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
378
SELECT
379
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
380

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

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

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

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

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

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

536
const getChannelBySCID = `-- name: GetChannelBySCID :one
537
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature, signature, funding_pk_script, merkle_root_hash FROM graph_channels
538
WHERE scid = $1 AND version = $2
539
`
540

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

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

570
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
571
SELECT
572
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
573
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
574
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
575

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

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

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

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

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

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

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

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

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

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

829
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
830
SELECT id, version, channel_id, node_id, timelock, fee_ppm, base_fee_msat, min_htlc_msat, max_htlc_msat, last_update, disabled, inbound_base_fee_msat, inbound_fee_rate_milli_msat, message_flags, channel_flags, signature, block_height, disable_flags
831
FROM graph_channel_policies
832
WHERE channel_id = $1
833
  AND node_id = $2
834
  AND version = $3
835
`
836

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

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

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

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

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

918
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
919
SELECT
920
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
921

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

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

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

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

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

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

1106
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1107
SELECT
1108
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1109
    n1.pub_key AS node1_pubkey,
1110
    n2.pub_key AS node2_pubkey
1111
FROM graph_channels c
1112
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1113
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1114
WHERE c.outpoint IN
1115
    (/*SLICE:outpoints*/?)
1116
`
1117

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

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

1176
const getChannelsByPolicyBlockRange = `-- name: GetChannelsByPolicyBlockRange :many
1177
WITH candidate_channels AS (
1178
    SELECT DISTINCT channel_id
1179
    FROM graph_channel_policies
1180
    WHERE version = $1
1181
      AND block_height >= $2
1182
      AND block_height < $3
1183
)
1184
SELECT
1185
    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,
1186
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1187
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1188

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

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

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

1267
type GetChannelsByPolicyBlockRangeParams struct {
1268
        Version         int16
1269
        StartHeight     sql.NullInt64
1270
        EndHeight       sql.NullInt64
1271
        LastBlockHeight sql.NullInt64
1272
        LastID          sql.NullInt64
1273
        MaxResults      interface{}
1274
}
1275

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

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

1413
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1414
WITH candidate_channels AS (
1415
    SELECT DISTINCT channel_id
1416
    FROM graph_channel_policies
1417
    WHERE version = 1
1418
      AND last_update >= $1
1419
      AND last_update < $2
1420
)
1421
SELECT
1422
    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,
1423
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1424
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1425

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

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

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

1504
type GetChannelsByPolicyLastUpdateRangeParams struct {
1505
        StartTime      sql.NullInt64
1506
        EndTime        sql.NullInt64
1507
        LastUpdateTime sql.NullInt64
1508
        LastID         sql.NullInt64
1509
        MaxResults     interface{}
1510
}
1511

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

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

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

1659
type GetChannelsBySCIDRangeParams struct {
1660
        StartScid []byte
1661
        EndScid   []byte
1662
}
1663

1664
type GetChannelsBySCIDRangeRow struct {
1665
        GraphChannel GraphChannel
1666
        Node1PubKey  []byte
1667
        Node2PubKey  []byte
1668
}
1669

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

1712
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1713
SELECT
1714
    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,
1715
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1716
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1717

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

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

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

1768
type GetChannelsBySCIDWithPoliciesParams struct {
1769
        Version int16
1770
        Scids   [][]byte
1771
}
1772

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

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

1914
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1915
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
1916
WHERE version = $1
1917
  AND scid IN (/*SLICE:scids*/?)
1918
`
1919

1920
type GetChannelsBySCIDsParams struct {
1921
        Version int16
1922
        Scids   [][]byte
1923
}
1924

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

1976
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1977
SELECT scid
1978
FROM graph_closed_scids
1979
WHERE scid IN (/*SLICE:scids*/?)
1980
`
1981

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

2015
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
2016
SELECT node_id, type, value
2017
FROM graph_node_extra_types
2018
WHERE node_id = $1
2019
`
2020

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

2044
const getNodeAddresses = `-- name: GetNodeAddresses :many
2045
SELECT type, address
2046
FROM graph_node_addresses
2047
WHERE node_id = $1
2048
ORDER BY type ASC, position ASC
2049
`
2050

2051
type GetNodeAddressesRow struct {
2052
        Type    int16
2053
        Address string
2054
}
2055

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

2079
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
2080
SELECT node_id, type, position, address
2081
FROM graph_node_addresses
2082
WHERE node_id IN (/*SLICE:ids*/?)
2083
ORDER BY node_id, type, position
2084
`
2085

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

2124
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
2125
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2126
FROM graph_nodes
2127
WHERE pub_key = $1
2128
  AND version = $2
2129
`
2130

2131
type GetNodeByPubKeyParams struct {
2132
        PubKey  []byte
2133
        Version int16
2134
}
2135

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

2152
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
2153
SELECT node_id, type, value
2154
FROM graph_node_extra_types
2155
WHERE node_id IN (/*SLICE:ids*/?)
2156
ORDER BY node_id, type
2157
`
2158

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

2192
const getNodeFeatures = `-- name: GetNodeFeatures :many
2193
SELECT node_id, feature_bit
2194
FROM graph_node_features
2195
WHERE node_id = $1
2196
`
2197

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

2221
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
2222
SELECT node_id, feature_bit
2223
FROM graph_node_features
2224
WHERE node_id IN (/*SLICE:ids*/?)
2225
ORDER BY node_id, feature_bit
2226
`
2227

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

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

2269
type GetNodeFeaturesByPubKeyParams struct {
2270
        PubKey  []byte
2271
        Version int16
2272
}
2273

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

2297
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
2298
SELECT id
2299
FROM graph_nodes
2300
WHERE pub_key = $1
2301
  AND version = $2
2302
`
2303

2304
type GetNodeIDByPubKeyParams struct {
2305
        PubKey  []byte
2306
        Version int16
2307
}
2308

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

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

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

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

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

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

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

2486
type GetNodesByLastUpdateRangeParams struct {
2487
        StartTime  sql.NullInt64
2488
        EndTime    sql.NullInt64
2489
        LastUpdate sql.NullInt64
2490
        LastPubKey []byte
2491
        OnlyPublic interface{}
2492
        MaxResults interface{}
2493
}
2494

2495
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2496
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
2497
                arg.StartTime,
×
2498
                arg.EndTime,
×
2499
                arg.LastUpdate,
×
2500
                arg.LastPubKey,
×
2501
                arg.OnlyPublic,
×
2502
                arg.MaxResults,
×
2503
        )
×
2504
        if err != nil {
×
2505
                return nil, err
×
2506
        }
×
2507
        defer rows.Close()
×
2508
        var items []GraphNode
×
2509
        for rows.Next() {
×
2510
                var i GraphNode
×
2511
                if err := rows.Scan(
×
2512
                        &i.ID,
×
2513
                        &i.Version,
×
2514
                        &i.PubKey,
×
2515
                        &i.Alias,
×
2516
                        &i.LastUpdate,
×
2517
                        &i.Color,
×
2518
                        &i.Signature,
×
2519
                        &i.BlockHeight,
×
2520
                ); err != nil {
×
2521
                        return nil, err
×
2522
                }
×
2523
                items = append(items, i)
×
2524
        }
2525
        if err := rows.Close(); err != nil {
×
2526
                return nil, err
×
2527
        }
×
2528
        if err := rows.Err(); err != nil {
×
2529
                return nil, err
×
2530
        }
×
2531
        return items, nil
×
2532
}
2533

2534
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2535
SELECT block_height, block_hash
2536
FROM graph_prune_log
2537
WHERE block_height
2538
   IN (/*SLICE:heights*/?)
2539
`
2540

2541
func (q *Queries) GetPruneEntriesForHeights(ctx context.Context, heights []int64) ([]GraphPruneLog, error) {
×
2542
        query := getPruneEntriesForHeights
×
2543
        var queryParams []interface{}
×
2544
        if len(heights) > 0 {
×
2545
                for _, v := range heights {
×
2546
                        queryParams = append(queryParams, v)
×
2547
                }
×
2548
                query = strings.Replace(query, "/*SLICE:heights*/?", makeQueryParams(len(queryParams), len(heights)), 1)
×
2549
        } else {
×
2550
                query = strings.Replace(query, "/*SLICE:heights*/?", "NULL", 1)
×
2551
        }
×
2552
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2553
        if err != nil {
×
2554
                return nil, err
×
2555
        }
×
2556
        defer rows.Close()
×
2557
        var items []GraphPruneLog
×
2558
        for rows.Next() {
×
2559
                var i GraphPruneLog
×
2560
                if err := rows.Scan(&i.BlockHeight, &i.BlockHash); err != nil {
×
2561
                        return nil, err
×
2562
                }
×
2563
                items = append(items, i)
×
2564
        }
2565
        if err := rows.Close(); err != nil {
×
2566
                return nil, err
×
2567
        }
×
2568
        if err := rows.Err(); err != nil {
×
2569
                return nil, err
×
2570
        }
×
2571
        return items, nil
×
2572
}
2573

2574
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2575
SELECT block_hash
2576
FROM graph_prune_log
2577
WHERE block_height = $1
2578
`
2579

2580
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2581
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2582
        var block_hash []byte
×
2583
        err := row.Scan(&block_hash)
×
2584
        return block_hash, err
×
2585
}
×
2586

2587
const getPruneTip = `-- name: GetPruneTip :one
2588
SELECT block_height, block_hash
2589
FROM graph_prune_log
2590
ORDER BY block_height DESC
2591
LIMIT 1
2592
`
2593

2594
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2595
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2596
        var i GraphPruneLog
×
2597
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2598
        return i, err
×
2599
}
×
2600

2601
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2602
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
2603
FROM graph_channels
2604
WHERE version = 1
2605
  AND COALESCE(length(node_1_signature), 0) > 0
2606
  AND scid >= $1
2607
  AND scid < $2
2608
ORDER BY scid ASC
2609
`
2610

2611
type GetPublicV1ChannelsBySCIDParams struct {
2612
        StartScid []byte
2613
        EndScid   []byte
2614
}
2615

2616
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2617
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2618
        if err != nil {
×
2619
                return nil, err
×
2620
        }
×
2621
        defer rows.Close()
×
2622
        var items []GraphChannel
×
2623
        for rows.Next() {
×
2624
                var i GraphChannel
×
2625
                if err := rows.Scan(
×
2626
                        &i.ID,
×
2627
                        &i.Version,
×
2628
                        &i.Scid,
×
2629
                        &i.NodeID1,
×
2630
                        &i.NodeID2,
×
2631
                        &i.Outpoint,
×
2632
                        &i.Capacity,
×
2633
                        &i.BitcoinKey1,
×
2634
                        &i.BitcoinKey2,
×
2635
                        &i.Node1Signature,
×
2636
                        &i.Node2Signature,
×
2637
                        &i.Bitcoin1Signature,
×
2638
                        &i.Bitcoin2Signature,
×
2639
                        &i.Signature,
×
2640
                        &i.FundingPkScript,
×
2641
                        &i.MerkleRootHash,
×
2642
                ); err != nil {
×
2643
                        return nil, err
×
2644
                }
×
2645
                items = append(items, i)
×
2646
        }
2647
        if err := rows.Close(); err != nil {
×
2648
                return nil, err
×
2649
        }
×
2650
        if err := rows.Err(); err != nil {
×
2651
                return nil, err
×
2652
        }
×
2653
        return items, nil
×
2654
}
2655

2656
const getPublicV2ChannelsBySCID = `-- name: GetPublicV2ChannelsBySCID :many
2657
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
2658
FROM graph_channels
2659
WHERE version = 2
2660
  AND COALESCE(length(signature), 0) > 0
2661
  AND scid >= $1
2662
  AND scid < $2
2663
ORDER BY scid ASC
2664
`
2665

2666
type GetPublicV2ChannelsBySCIDParams struct {
2667
        StartScid []byte
2668
        EndScid   []byte
2669
}
2670

2671
func (q *Queries) GetPublicV2ChannelsBySCID(ctx context.Context, arg GetPublicV2ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2672
        rows, err := q.db.QueryContext(ctx, getPublicV2ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2673
        if err != nil {
×
2674
                return nil, err
×
2675
        }
×
2676
        defer rows.Close()
×
2677
        var items []GraphChannel
×
2678
        for rows.Next() {
×
2679
                var i GraphChannel
×
2680
                if err := rows.Scan(
×
2681
                        &i.ID,
×
2682
                        &i.Version,
×
2683
                        &i.Scid,
×
2684
                        &i.NodeID1,
×
2685
                        &i.NodeID2,
×
2686
                        &i.Outpoint,
×
2687
                        &i.Capacity,
×
2688
                        &i.BitcoinKey1,
×
2689
                        &i.BitcoinKey2,
×
2690
                        &i.Node1Signature,
×
2691
                        &i.Node2Signature,
×
2692
                        &i.Bitcoin1Signature,
×
2693
                        &i.Bitcoin2Signature,
×
2694
                        &i.Signature,
×
2695
                        &i.FundingPkScript,
×
2696
                        &i.MerkleRootHash,
×
2697
                ); err != nil {
×
2698
                        return nil, err
×
2699
                }
×
2700
                items = append(items, i)
×
2701
        }
2702
        if err := rows.Close(); err != nil {
×
2703
                return nil, err
×
2704
        }
×
2705
        if err := rows.Err(); err != nil {
×
2706
                return nil, err
×
2707
        }
×
2708
        return items, nil
×
2709
}
2710

2711
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2712
SELECT scid from graph_channels
2713
WHERE outpoint = $1 AND version = $2
2714
`
2715

2716
type GetSCIDByOutpointParams struct {
2717
        Outpoint string
2718
        Version  int16
2719
}
2720

2721
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2722
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2723
        var scid []byte
×
2724
        err := row.Scan(&scid)
×
2725
        return scid, err
×
2726
}
×
2727

2728
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2729
SELECT sn.node_id, n.pub_key
2730
FROM graph_source_nodes sn
2731
    JOIN graph_nodes n ON sn.node_id = n.id
2732
WHERE n.version = $1
2733
`
2734

2735
type GetSourceNodesByVersionRow struct {
2736
        NodeID int64
2737
        PubKey []byte
2738
}
2739

2740
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2741
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2742
        if err != nil {
×
2743
                return nil, err
×
2744
        }
×
2745
        defer rows.Close()
×
2746
        var items []GetSourceNodesByVersionRow
×
2747
        for rows.Next() {
×
2748
                var i GetSourceNodesByVersionRow
×
2749
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
2750
                        return nil, err
×
2751
                }
×
2752
                items = append(items, i)
×
2753
        }
2754
        if err := rows.Close(); err != nil {
×
2755
                return nil, err
×
2756
        }
×
2757
        if err := rows.Err(); err != nil {
×
2758
                return nil, err
×
2759
        }
×
2760
        return items, nil
×
2761
}
2762

2763
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2764
SELECT c.scid
2765
FROM graph_channels c
2766
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2767
WHERE cp.disabled = true
2768
AND c.version = 1
2769
GROUP BY c.scid
2770
HAVING COUNT(*) > 1
2771
`
2772

2773
// NOTE: this is V1 specific since for V1, disabled is a
2774
// simple, single boolean. The proposed V2 policy
2775
// structure will have a more complex disabled bit vector
2776
// and so the query for V2 may differ.
2777
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2778
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2779
        if err != nil {
×
2780
                return nil, err
×
2781
        }
×
2782
        defer rows.Close()
×
2783
        var items [][]byte
×
2784
        for rows.Next() {
×
2785
                var scid []byte
×
2786
                if err := rows.Scan(&scid); err != nil {
×
2787
                        return nil, err
×
2788
                }
×
2789
                items = append(items, scid)
×
2790
        }
2791
        if err := rows.Close(); err != nil {
×
2792
                return nil, err
×
2793
        }
×
2794
        if err := rows.Err(); err != nil {
×
2795
                return nil, err
×
2796
        }
×
2797
        return items, nil
×
2798
}
2799

2800
const getV2DisabledSCIDs = `-- name: GetV2DisabledSCIDs :many
2801
SELECT c.scid
2802
FROM graph_channels c
2803
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2804
WHERE COALESCE(cp.disable_flags, 0) != 0
2805
AND c.version = 2
2806
GROUP BY c.scid
2807
HAVING COUNT(*) > 1
2808
`
2809

2810
// NOTE: this is V2 specific since V2 uses a disable flag
2811
// bit vector instead of a single boolean.
2812
func (q *Queries) GetV2DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2813
        rows, err := q.db.QueryContext(ctx, getV2DisabledSCIDs)
×
2814
        if err != nil {
×
2815
                return nil, err
×
2816
        }
×
2817
        defer rows.Close()
×
2818
        var items [][]byte
×
2819
        for rows.Next() {
×
2820
                var scid []byte
×
2821
                if err := rows.Scan(&scid); err != nil {
×
2822
                        return nil, err
×
2823
                }
×
2824
                items = append(items, scid)
×
2825
        }
2826
        if err := rows.Close(); err != nil {
×
2827
                return nil, err
×
2828
        }
×
2829
        if err := rows.Err(); err != nil {
×
2830
                return nil, err
×
2831
        }
×
2832
        return items, nil
×
2833
}
2834

2835
const getZombieChannel = `-- name: GetZombieChannel :one
2836
SELECT scid, version, node_key_1, node_key_2
2837
FROM graph_zombie_channels
2838
WHERE scid = $1
2839
AND version = $2
2840
`
2841

2842
type GetZombieChannelParams struct {
2843
        Scid    []byte
2844
        Version int16
2845
}
2846

2847
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2848
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2849
        var i GraphZombieChannel
×
2850
        err := row.Scan(
×
2851
                &i.Scid,
×
2852
                &i.Version,
×
2853
                &i.NodeKey1,
×
2854
                &i.NodeKey2,
×
2855
        )
×
2856
        return i, err
×
2857
}
×
2858

2859
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2860
SELECT scid, version, node_key_1, node_key_2
2861
FROM graph_zombie_channels
2862
WHERE version = $1
2863
  AND scid IN (/*SLICE:scids*/?)
2864
`
2865

2866
type GetZombieChannelsSCIDsParams struct {
2867
        Version int16
2868
        Scids   [][]byte
2869
}
2870

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

2910
const highestSCID = `-- name: HighestSCID :one
2911
SELECT scid
2912
FROM graph_channels
2913
WHERE version = $1
2914
ORDER BY scid DESC
2915
LIMIT 1
2916
`
2917

2918
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2919
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2920
        var scid []byte
×
2921
        err := row.Scan(&scid)
×
2922
        return scid, err
×
2923
}
×
2924

2925
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2926
/* ─────────────────────────────────────────────
2927
   graph_channel_features table queries
2928
   ─────────────────────────────────────────────
2929
*/
2930

2931
INSERT INTO graph_channel_features (
2932
    channel_id, feature_bit
2933
) VALUES (
2934
    $1, $2
2935
) ON CONFLICT (channel_id, feature_bit)
2936
    -- Do nothing if the channel_id and feature_bit already exist.
2937
    DO NOTHING
2938
`
2939

2940
type InsertChannelFeatureParams struct {
2941
        ChannelID  int64
2942
        FeatureBit int32
2943
}
2944

2945
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2946
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2947
        return err
×
2948
}
×
2949

2950
const insertChannelMig = `-- name: InsertChannelMig :one
2951
INSERT INTO graph_channels (
2952
    version, scid, node_id_1, node_id_2,
2953
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2954
    node_1_signature, node_2_signature, bitcoin_1_signature,
2955
    bitcoin_2_signature
2956
) VALUES (
2957
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2958
) ON CONFLICT (scid, version)
2959
    -- If a conflict occurs, we have already migrated this channel. However, we
2960
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2961
    -- otherwise, the "RETURNING id" part does not work.
2962
    DO UPDATE SET
2963
        node_id_1 = EXCLUDED.node_id_1,
2964
        node_id_2 = EXCLUDED.node_id_2,
2965
        outpoint = EXCLUDED.outpoint,
2966
        capacity = EXCLUDED.capacity,
2967
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2968
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2969
        node_1_signature = EXCLUDED.node_1_signature,
2970
        node_2_signature = EXCLUDED.node_2_signature,
2971
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2972
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2973
RETURNING id
2974
`
2975

2976
type InsertChannelMigParams struct {
2977
        Version           int16
2978
        Scid              []byte
2979
        NodeID1           int64
2980
        NodeID2           int64
2981
        Outpoint          string
2982
        Capacity          sql.NullInt64
2983
        BitcoinKey1       []byte
2984
        BitcoinKey2       []byte
2985
        Node1Signature    []byte
2986
        Node2Signature    []byte
2987
        Bitcoin1Signature []byte
2988
        Bitcoin2Signature []byte
2989
}
2990

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

3016
const insertClosedChannel = `-- name: InsertClosedChannel :exec
3017
/* ─────────────────────────────────────────────
3018
   graph_closed_scid table queries
3019
   ────────────────────────────────────────────-
3020
*/
3021

3022
INSERT INTO graph_closed_scids (scid)
3023
VALUES ($1)
3024
ON CONFLICT (scid) DO NOTHING
3025
`
3026

3027
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
3028
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
3029
        return err
×
3030
}
×
3031

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

3062
type InsertEdgePolicyMigParams struct {
3063
        Version                 int16
3064
        ChannelID               int64
3065
        NodeID                  int64
3066
        Timelock                int32
3067
        FeePpm                  int64
3068
        BaseFeeMsat             int64
3069
        MinHtlcMsat             int64
3070
        LastUpdate              sql.NullInt64
3071
        Disabled                sql.NullBool
3072
        MaxHtlcMsat             sql.NullInt64
3073
        InboundBaseFeeMsat      sql.NullInt64
3074
        InboundFeeRateMilliMsat sql.NullInt64
3075
        MessageFlags            sql.NullInt16
3076
        ChannelFlags            sql.NullInt16
3077
        Signature               []byte
3078
}
3079

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

3108
const insertNodeFeature = `-- name: InsertNodeFeature :exec
3109
/* ─────────────────────────────────────────────
3110
   graph_node_features table queries
3111
   ─────────────────────────────────────────────
3112
*/
3113

3114
INSERT INTO graph_node_features (
3115
    node_id, feature_bit
3116
) VALUES (
3117
    $1, $2
3118
) ON CONFLICT (node_id, feature_bit)
3119
    -- Do nothing if the feature already exists for the node.
3120
    DO NOTHING
3121
`
3122

3123
type InsertNodeFeatureParams struct {
3124
        NodeID     int64
3125
        FeatureBit int32
3126
}
3127

3128
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
3129
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
3130
        return err
×
3131
}
×
3132

3133
const insertNodeMig = `-- name: InsertNodeMig :one
3134
/* ─────────────────────────────────────────────
3135
   Migration specific queries
3136

3137
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
3138
   dedicated to the migration that requires it, and so we can then remove
3139
   it from the main set of "live" queries that the code-base has access to.
3140
   ────────────────────────────────────────────-
3141
*/
3142

3143
INSERT INTO graph_nodes (
3144
    version, pub_key, alias, last_update, color, signature
3145
) VALUES (
3146
    $1, $2, $3, $4, $5, $6
3147
)
3148
ON CONFLICT (pub_key, version)
3149
    -- If a conflict occurs, we have already migrated this node. However, we
3150
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
3151
    -- otherwise, the "RETURNING id" part does not work.
3152
    DO UPDATE SET
3153
        alias = EXCLUDED.alias,
3154
        last_update = EXCLUDED.last_update,
3155
        color = EXCLUDED.color,
3156
        signature = EXCLUDED.signature
3157
RETURNING id
3158
`
3159

3160
type InsertNodeMigParams struct {
3161
        Version    int16
3162
        PubKey     []byte
3163
        Alias      sql.NullString
3164
        LastUpdate sql.NullInt64
3165
        Color      sql.NullString
3166
        Signature  []byte
3167
}
3168

3169
// NOTE: This query is only meant to be used by the graph SQL migration since
3170
// for that migration, in order to be retry-safe, we don't want to error out if
3171
// we re-insert the same node (which would error if the normal UpsertNode query
3172
// is used because of the constraint in that query that requires a node update
3173
// to have a newer last_update than the existing node).
3174
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
3175
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
3176
                arg.Version,
×
3177
                arg.PubKey,
×
3178
                arg.Alias,
×
3179
                arg.LastUpdate,
×
3180
                arg.Color,
×
3181
                arg.Signature,
×
3182
        )
×
3183
        var id int64
×
3184
        err := row.Scan(&id)
×
3185
        return id, err
×
3186
}
×
3187

3188
const isClosedChannel = `-- name: IsClosedChannel :one
3189
SELECT EXISTS (
3190
    SELECT 1
3191
    FROM graph_closed_scids
3192
    WHERE scid = $1
3193
)
3194
`
3195

3196
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
3197
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
3198
        var exists bool
×
3199
        err := row.Scan(&exists)
×
3200
        return exists, err
×
3201
}
×
3202

3203
const isPublicV1Node = `-- name: IsPublicV1Node :one
3204
SELECT EXISTS (
3205
    SELECT 1
3206
    FROM graph_channels c
3207
    JOIN graph_nodes n ON n.id = c.node_id_1
3208
    -- NOTE: we hard-code the version here since the clauses
3209
    -- here that determine if a node is public is specific
3210
    -- to the V1 gossip protocol. In V1, a node is public
3211
    -- if it has a public channel and a public channel is one
3212
    -- where we have the set of signatures of the channel
3213
    -- announcement. It is enough to just check that we have
3214
    -- one of the signatures since we only ever set them
3215
    -- together.
3216
    WHERE c.version = 1
3217
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3218
      AND n.pub_key = $1
3219
    UNION ALL
3220
    SELECT 1
3221
    FROM graph_channels c
3222
    JOIN graph_nodes n ON n.id = c.node_id_2
3223
    WHERE c.version = 1
3224
      AND COALESCE(length(c.bitcoin_1_signature), 0) > 0
3225
      AND n.pub_key = $1
3226
)
3227
`
3228

3229
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3230
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
3231
        var exists bool
×
3232
        err := row.Scan(&exists)
×
3233
        return exists, err
×
3234
}
×
3235

3236
const isPublicV2Node = `-- name: IsPublicV2Node :one
3237
SELECT EXISTS (
3238
    SELECT 1
3239
    FROM graph_channels c
3240
    JOIN graph_nodes n ON n.id = c.node_id_1
3241
    -- NOTE: we hard-code the version here since the clauses
3242
    -- here that determine if a node is public is specific
3243
    -- to the V2 gossip protocol.
3244
    WHERE c.version = 2
3245
      AND COALESCE(length(c.signature), 0) > 0
3246
      AND n.pub_key = $1
3247

3248
    UNION ALL
3249

3250
    SELECT 1
3251
    FROM graph_channels c
3252
    JOIN graph_nodes n ON n.id = c.node_id_2
3253
    WHERE c.version = 2
3254
      AND COALESCE(length(c.signature), 0) > 0
3255
      AND n.pub_key = $1
3256
)
3257
`
3258

3259
func (q *Queries) IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error) {
×
3260
        row := q.db.QueryRowContext(ctx, isPublicV2Node, pubKey)
×
3261
        var exists bool
×
3262
        err := row.Scan(&exists)
×
3263
        return exists, err
×
3264
}
×
3265

3266
const isZombieChannel = `-- name: IsZombieChannel :one
3267
SELECT EXISTS (
3268
    SELECT 1
3269
    FROM graph_zombie_channels
3270
    WHERE scid = $1
3271
    AND version = $2
3272
) AS is_zombie
3273
`
3274

3275
type IsZombieChannelParams struct {
3276
        Scid    []byte
3277
        Version int16
3278
}
3279

3280
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
3281
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
3282
        var is_zombie bool
×
3283
        err := row.Scan(&is_zombie)
×
3284
        return is_zombie, err
×
3285
}
×
3286

3287
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
3288
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,
3289
    n1.pub_key AS node1_pubkey,
3290
    n2.pub_key AS node2_pubkey,
3291

3292
    -- Policy 1
3293
    -- TODO(elle): use sqlc.embed to embed policy structs
3294
    --  once this issue is resolved:
3295
    --  https://github.com/sqlc-dev/sqlc/issues/2997
3296
    cp1.id AS policy1_id,
3297
    cp1.node_id AS policy1_node_id,
3298
    cp1.version AS policy1_version,
3299
    cp1.timelock AS policy1_timelock,
3300
    cp1.fee_ppm AS policy1_fee_ppm,
3301
    cp1.base_fee_msat AS policy1_base_fee_msat,
3302
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
3303
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
3304
    cp1.last_update AS policy1_last_update,
3305
    cp1.disabled AS policy1_disabled,
3306
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3307
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3308
    cp1.message_flags AS policy1_message_flags,
3309
    cp1.channel_flags AS policy1_channel_flags,
3310
    cp1.signature AS policy1_signature,
3311
    cp1.block_height AS policy1_block_height,
3312
    cp1.disable_flags AS policy1_disable_flags,
3313

3314
       -- Policy 2
3315
    cp2.id AS policy2_id,
3316
    cp2.node_id AS policy2_node_id,
3317
    cp2.version AS policy2_version,
3318
    cp2.timelock AS policy2_timelock,
3319
    cp2.fee_ppm AS policy2_fee_ppm,
3320
    cp2.base_fee_msat AS policy2_base_fee_msat,
3321
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
3322
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
3323
    cp2.last_update AS policy2_last_update,
3324
    cp2.disabled AS policy2_disabled,
3325
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3326
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3327
    cp2.message_flags AS policy2_message_flags,
3328
    cp2.channel_flags AS policy2_channel_flags,
3329
    cp2.signature AS policy2_signature,
3330
    cp2.block_height AS policy2_block_height,
3331
    cp2.disable_flags AS policy2_disable_flags
3332

3333
FROM graph_channels c
3334
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3335
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3336
    LEFT JOIN graph_channel_policies cp1
3337
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3338
    LEFT JOIN graph_channel_policies cp2
3339
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3340
WHERE c.version = $1
3341
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
3342
`
3343

3344
type ListChannelsByNodeIDParams struct {
3345
        Version int16
3346
        NodeID1 int64
3347
}
3348

3349
type ListChannelsByNodeIDRow struct {
3350
        GraphChannel                   GraphChannel
3351
        Node1Pubkey                    []byte
3352
        Node2Pubkey                    []byte
3353
        Policy1ID                      sql.NullInt64
3354
        Policy1NodeID                  sql.NullInt64
3355
        Policy1Version                 sql.NullInt16
3356
        Policy1Timelock                sql.NullInt32
3357
        Policy1FeePpm                  sql.NullInt64
3358
        Policy1BaseFeeMsat             sql.NullInt64
3359
        Policy1MinHtlcMsat             sql.NullInt64
3360
        Policy1MaxHtlcMsat             sql.NullInt64
3361
        Policy1LastUpdate              sql.NullInt64
3362
        Policy1Disabled                sql.NullBool
3363
        Policy1InboundBaseFeeMsat      sql.NullInt64
3364
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3365
        Policy1MessageFlags            sql.NullInt16
3366
        Policy1ChannelFlags            sql.NullInt16
3367
        Policy1Signature               []byte
3368
        Policy1BlockHeight             sql.NullInt64
3369
        Policy1DisableFlags            sql.NullInt16
3370
        Policy2ID                      sql.NullInt64
3371
        Policy2NodeID                  sql.NullInt64
3372
        Policy2Version                 sql.NullInt16
3373
        Policy2Timelock                sql.NullInt32
3374
        Policy2FeePpm                  sql.NullInt64
3375
        Policy2BaseFeeMsat             sql.NullInt64
3376
        Policy2MinHtlcMsat             sql.NullInt64
3377
        Policy2MaxHtlcMsat             sql.NullInt64
3378
        Policy2LastUpdate              sql.NullInt64
3379
        Policy2Disabled                sql.NullBool
3380
        Policy2InboundBaseFeeMsat      sql.NullInt64
3381
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3382
        Policy2MessageFlags            sql.NullInt16
3383
        Policy2ChannelFlags            sql.NullInt16
3384
        Policy2Signature               []byte
3385
        Policy2BlockHeight             sql.NullInt64
3386
        Policy2DisableFlags            sql.NullInt16
3387
}
3388

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

3465
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3466
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,
3467
       n1.pub_key AS node1_pubkey,
3468
       n2.pub_key AS node2_pubkey,
3469

3470
       -- Policy 1
3471
       -- TODO(elle): use sqlc.embed to embed policy structs
3472
       --  once this issue is resolved:
3473
       --  https://github.com/sqlc-dev/sqlc/issues/2997
3474
       cp1.id AS policy1_id,
3475
       cp1.node_id AS policy1_node_id,
3476
       cp1.version AS policy1_version,
3477
       cp1.timelock AS policy1_timelock,
3478
       cp1.fee_ppm AS policy1_fee_ppm,
3479
       cp1.base_fee_msat AS policy1_base_fee_msat,
3480
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
3481
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
3482
       cp1.last_update AS policy1_last_update,
3483
       cp1.disabled AS policy1_disabled,
3484
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3485
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3486
       cp1.message_flags AS policy1_message_flags,
3487
       cp1.channel_flags AS policy1_channel_flags,
3488
       cp1.signature AS policy1_signature,
3489
    cp1.block_height AS policy1_block_height,
3490
    cp1.disable_flags AS policy1_disable_flags,
3491

3492
       -- Policy 2
3493
       cp2.id AS policy2_id,
3494
       cp2.node_id AS policy2_node_id,
3495
       cp2.version AS policy2_version,
3496
       cp2.timelock AS policy2_timelock,
3497
       cp2.fee_ppm AS policy2_fee_ppm,
3498
       cp2.base_fee_msat AS policy2_base_fee_msat,
3499
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
3500
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
3501
       cp2.last_update AS policy2_last_update,
3502
       cp2.disabled AS policy2_disabled,
3503
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3504
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3505
       cp2.message_flags AS policy2_message_flags,
3506
       cp2.channel_flags AS policy2_channel_flags,
3507
       cp2.signature AS policy2_signature,
3508
    cp2.block_height AS policy2_block_height,
3509
    cp2.disable_flags AS policy2_disable_flags
3510

3511
FROM graph_channels c
3512
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3513
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3514
         LEFT JOIN graph_channel_policies cp1
3515
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3516
         LEFT JOIN graph_channel_policies cp2
3517
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3518
WHERE c.version = $1
3519
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
3520
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
3521
`
3522

3523
type ListChannelsForNodeIDsParams struct {
3524
        Version  int16
3525
        Node1Ids []int64
3526
        Node2Ids []int64
3527
}
3528

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

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

3664
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3665
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3666
FROM graph_channels c
3667
WHERE c.version = $1 AND c.id > $2
3668
ORDER BY c.id
3669
LIMIT $3
3670
`
3671

3672
type ListChannelsPaginatedParams struct {
3673
        Version int16
3674
        ID      int64
3675
        Limit   int32
3676
}
3677

3678
type ListChannelsPaginatedRow struct {
3679
        ID          int64
3680
        BitcoinKey1 []byte
3681
        BitcoinKey2 []byte
3682
        Outpoint    string
3683
}
3684

3685
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3686
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3687
        if err != nil {
×
3688
                return nil, err
×
3689
        }
×
3690
        defer rows.Close()
×
3691
        var items []ListChannelsPaginatedRow
×
3692
        for rows.Next() {
×
3693
                var i ListChannelsPaginatedRow
×
3694
                if err := rows.Scan(
×
3695
                        &i.ID,
×
3696
                        &i.BitcoinKey1,
×
3697
                        &i.BitcoinKey2,
×
3698
                        &i.Outpoint,
×
3699
                ); err != nil {
×
3700
                        return nil, err
×
3701
                }
×
3702
                items = append(items, i)
×
3703
        }
3704
        if err := rows.Close(); err != nil {
×
3705
                return nil, err
×
3706
        }
×
3707
        if err := rows.Err(); err != nil {
×
3708
                return nil, err
×
3709
        }
×
3710
        return items, nil
×
3711
}
3712

3713
const listChannelsPaginatedV2 = `-- name: ListChannelsPaginatedV2 :many
3714
SELECT id, outpoint, funding_pk_script
3715
FROM graph_channels c
3716
WHERE c.version = 2 AND c.id > $1
3717
ORDER BY c.id
3718
LIMIT $2
3719
`
3720

3721
type ListChannelsPaginatedV2Params struct {
3722
        ID    int64
3723
        Limit int32
3724
}
3725

3726
type ListChannelsPaginatedV2Row struct {
3727
        ID              int64
3728
        Outpoint        string
3729
        FundingPkScript []byte
3730
}
3731

3732
func (q *Queries) ListChannelsPaginatedV2(ctx context.Context, arg ListChannelsPaginatedV2Params) ([]ListChannelsPaginatedV2Row, error) {
×
3733
        rows, err := q.db.QueryContext(ctx, listChannelsPaginatedV2, arg.ID, arg.Limit)
×
3734
        if err != nil {
×
3735
                return nil, err
×
3736
        }
×
3737
        defer rows.Close()
×
3738
        var items []ListChannelsPaginatedV2Row
×
3739
        for rows.Next() {
×
3740
                var i ListChannelsPaginatedV2Row
×
3741
                if err := rows.Scan(&i.ID, &i.Outpoint, &i.FundingPkScript); err != nil {
×
3742
                        return nil, err
×
3743
                }
×
3744
                items = append(items, i)
×
3745
        }
3746
        if err := rows.Close(); err != nil {
×
3747
                return nil, err
×
3748
        }
×
3749
        if err := rows.Err(); err != nil {
×
3750
                return nil, err
×
3751
        }
×
3752
        return items, nil
×
3753
}
3754

3755
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3756
SELECT
3757
    c.id as id,
3758
    c.scid as scid,
3759
    c.capacity AS capacity,
3760

3761
    -- Join node pubkeys
3762
    n1.pub_key AS node1_pubkey,
3763
    n2.pub_key AS node2_pubkey,
3764

3765
    -- Node 1 policy
3766
    cp1.version AS policy1_version,
3767
    cp1.timelock AS policy_1_timelock,
3768
    cp1.fee_ppm AS policy_1_fee_ppm,
3769
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3770
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3771
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3772
    cp1.disabled AS policy_1_disabled,
3773
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3774
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3775
    cp1.message_flags AS policy1_message_flags,
3776
    cp1.channel_flags AS policy1_channel_flags,
3777
    cp1.block_height AS policy1_block_height,
3778
    cp1.disable_flags AS policy1_disable_flags,
3779

3780
    -- Node 2 policy
3781
    cp2.version AS policy2_version,
3782
    cp2.timelock AS policy_2_timelock,
3783
    cp2.fee_ppm AS policy_2_fee_ppm,
3784
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3785
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3786
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3787
    cp2.disabled AS policy_2_disabled,
3788
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3789
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3790
    cp2.message_flags AS policy2_message_flags,
3791
    cp2.channel_flags AS policy2_channel_flags,
3792
    cp2.block_height AS policy2_block_height,
3793
    cp2.disable_flags AS policy2_disable_flags
3794

3795
FROM graph_channels c
3796
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3797
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3798
LEFT JOIN graph_channel_policies cp1
3799
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3800
LEFT JOIN graph_channel_policies cp2
3801
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3802
WHERE c.version = $1 AND c.id > $2
3803
ORDER BY c.id
3804
LIMIT $3
3805
`
3806

3807
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3808
        Version int16
3809
        ID      int64
3810
        Limit   int32
3811
}
3812

3813
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3814
        ID                             int64
3815
        Scid                           []byte
3816
        Capacity                       sql.NullInt64
3817
        Node1Pubkey                    []byte
3818
        Node2Pubkey                    []byte
3819
        Policy1Version                 sql.NullInt16
3820
        Policy1Timelock                sql.NullInt32
3821
        Policy1FeePpm                  sql.NullInt64
3822
        Policy1BaseFeeMsat             sql.NullInt64
3823
        Policy1MinHtlcMsat             sql.NullInt64
3824
        Policy1MaxHtlcMsat             sql.NullInt64
3825
        Policy1Disabled                sql.NullBool
3826
        Policy1InboundBaseFeeMsat      sql.NullInt64
3827
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3828
        Policy1MessageFlags            sql.NullInt16
3829
        Policy1ChannelFlags            sql.NullInt16
3830
        Policy1BlockHeight             sql.NullInt64
3831
        Policy1DisableFlags            sql.NullInt16
3832
        Policy2Version                 sql.NullInt16
3833
        Policy2Timelock                sql.NullInt32
3834
        Policy2FeePpm                  sql.NullInt64
3835
        Policy2BaseFeeMsat             sql.NullInt64
3836
        Policy2MinHtlcMsat             sql.NullInt64
3837
        Policy2MaxHtlcMsat             sql.NullInt64
3838
        Policy2Disabled                sql.NullBool
3839
        Policy2InboundBaseFeeMsat      sql.NullInt64
3840
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3841
        Policy2MessageFlags            sql.NullInt16
3842
        Policy2ChannelFlags            sql.NullInt16
3843
        Policy2BlockHeight             sql.NullInt64
3844
        Policy2DisableFlags            sql.NullInt16
3845
}
3846

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

3902
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3903
SELECT
3904
    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,
3905

3906
    -- Join node pubkeys
3907
    n1.pub_key AS node1_pubkey,
3908
    n2.pub_key AS node2_pubkey,
3909

3910
    -- Node 1 policy
3911
    cp1.id AS policy_1_id,
3912
    cp1.node_id AS policy_1_node_id,
3913
    cp1.version AS policy_1_version,
3914
    cp1.timelock AS policy_1_timelock,
3915
    cp1.fee_ppm AS policy_1_fee_ppm,
3916
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3917
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3918
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3919
    cp1.last_update AS policy_1_last_update,
3920
    cp1.disabled AS policy_1_disabled,
3921
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3922
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3923
    cp1.message_flags AS policy1_message_flags,
3924
    cp1.channel_flags AS policy1_channel_flags,
3925
    cp1.block_height AS policy1_block_height,
3926
    cp1.disable_flags AS policy1_disable_flags,
3927
    cp1.signature AS policy_1_signature,
3928

3929
    -- Node 2 policy
3930
    cp2.id AS policy_2_id,
3931
    cp2.node_id AS policy_2_node_id,
3932
    cp2.version AS policy_2_version,
3933
    cp2.timelock AS policy_2_timelock,
3934
    cp2.fee_ppm AS policy_2_fee_ppm,
3935
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3936
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3937
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3938
    cp2.last_update AS policy_2_last_update,
3939
    cp2.disabled AS policy_2_disabled,
3940
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3941
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3942
    cp2.message_flags AS policy2_message_flags,
3943
    cp2.channel_flags AS policy2_channel_flags,
3944
    cp2.signature AS policy_2_signature,
3945
    cp2.block_height AS policy_2_block_height,
3946
    cp2.disable_flags AS policy_2_disable_flags
3947

3948
FROM graph_channels c
3949
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3950
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3951
LEFT JOIN graph_channel_policies cp1
3952
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3953
LEFT JOIN graph_channel_policies cp2
3954
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3955
WHERE c.version = $1 AND c.id > $2
3956
ORDER BY c.id
3957
LIMIT $3
3958
`
3959

3960
type ListChannelsWithPoliciesPaginatedParams struct {
3961
        Version int16
3962
        ID      int64
3963
        Limit   int32
3964
}
3965

3966
type ListChannelsWithPoliciesPaginatedRow struct {
3967
        GraphChannel                   GraphChannel
3968
        Node1Pubkey                    []byte
3969
        Node2Pubkey                    []byte
3970
        Policy1ID                      sql.NullInt64
3971
        Policy1NodeID                  sql.NullInt64
3972
        Policy1Version                 sql.NullInt16
3973
        Policy1Timelock                sql.NullInt32
3974
        Policy1FeePpm                  sql.NullInt64
3975
        Policy1BaseFeeMsat             sql.NullInt64
3976
        Policy1MinHtlcMsat             sql.NullInt64
3977
        Policy1MaxHtlcMsat             sql.NullInt64
3978
        Policy1LastUpdate              sql.NullInt64
3979
        Policy1Disabled                sql.NullBool
3980
        Policy1InboundBaseFeeMsat      sql.NullInt64
3981
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3982
        Policy1MessageFlags            sql.NullInt16
3983
        Policy1ChannelFlags            sql.NullInt16
3984
        Policy1BlockHeight             sql.NullInt64
3985
        Policy1DisableFlags            sql.NullInt16
3986
        Policy1Signature               []byte
3987
        Policy2ID                      sql.NullInt64
3988
        Policy2NodeID                  sql.NullInt64
3989
        Policy2Version                 sql.NullInt16
3990
        Policy2Timelock                sql.NullInt32
3991
        Policy2FeePpm                  sql.NullInt64
3992
        Policy2BaseFeeMsat             sql.NullInt64
3993
        Policy2MinHtlcMsat             sql.NullInt64
3994
        Policy2MaxHtlcMsat             sql.NullInt64
3995
        Policy2LastUpdate              sql.NullInt64
3996
        Policy2Disabled                sql.NullBool
3997
        Policy2InboundBaseFeeMsat      sql.NullInt64
3998
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3999
        Policy2MessageFlags            sql.NullInt16
4000
        Policy2ChannelFlags            sql.NullInt16
4001
        Policy2Signature               []byte
4002
        Policy2BlockHeight             sql.NullInt64
4003
        Policy2DisableFlags            sql.NullInt16
4004
}
4005

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

4082
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
4083
SELECT id, pub_key
4084
FROM graph_nodes
4085
WHERE version = $1  AND id > $2
4086
ORDER BY id
4087
LIMIT $3
4088
`
4089

4090
type ListNodeIDsAndPubKeysParams struct {
4091
        Version int16
4092
        ID      int64
4093
        Limit   int32
4094
}
4095

4096
type ListNodeIDsAndPubKeysRow struct {
4097
        ID     int64
4098
        PubKey []byte
4099
}
4100

4101
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
4102
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
4103
        if err != nil {
×
4104
                return nil, err
×
4105
        }
×
4106
        defer rows.Close()
×
4107
        var items []ListNodeIDsAndPubKeysRow
×
4108
        for rows.Next() {
×
4109
                var i ListNodeIDsAndPubKeysRow
×
4110
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
4111
                        return nil, err
×
4112
                }
×
4113
                items = append(items, i)
×
4114
        }
4115
        if err := rows.Close(); err != nil {
×
4116
                return nil, err
×
4117
        }
×
4118
        if err := rows.Err(); err != nil {
×
4119
                return nil, err
×
4120
        }
×
4121
        return items, nil
×
4122
}
4123

4124
const listNodesPaginated = `-- name: ListNodesPaginated :many
4125
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
4126
FROM graph_nodes
4127
WHERE version = $1 AND id > $2
4128
ORDER BY id
4129
LIMIT $3
4130
`
4131

4132
type ListNodesPaginatedParams struct {
4133
        Version int16
4134
        ID      int64
4135
        Limit   int32
4136
}
4137

4138
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
4139
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
4140
        if err != nil {
×
4141
                return nil, err
×
4142
        }
×
4143
        defer rows.Close()
×
4144
        var items []GraphNode
×
4145
        for rows.Next() {
×
4146
                var i GraphNode
×
4147
                if err := rows.Scan(
×
4148
                        &i.ID,
×
4149
                        &i.Version,
×
4150
                        &i.PubKey,
×
4151
                        &i.Alias,
×
4152
                        &i.LastUpdate,
×
4153
                        &i.Color,
×
4154
                        &i.Signature,
×
4155
                        &i.BlockHeight,
×
4156
                ); err != nil {
×
4157
                        return nil, err
×
4158
                }
×
4159
                items = append(items, i)
×
4160
        }
4161
        if err := rows.Close(); err != nil {
×
4162
                return nil, err
×
4163
        }
×
4164
        if err := rows.Err(); err != nil {
×
4165
                return nil, err
×
4166
        }
×
4167
        return items, nil
×
4168
}
4169

4170
const listPreferredChannelsWithPoliciesPaginated = `-- name: ListPreferredChannelsWithPoliciesPaginated :many
4171
WITH page_scids(cursor_scid) AS (
4172
    SELECT page.scid
4173
    FROM (
4174
        SELECT c2.scid AS scid
4175
        FROM graph_channels c2
4176
        WHERE c2.version = 2
4177
          AND c2.scid > $1
4178
        UNION
4179
        SELECT c1.scid AS scid
4180
        FROM graph_channels c1
4181
        WHERE c1.version = 1
4182
          AND c1.scid > $1
4183
    ) AS page
4184
    ORDER BY page.scid
4185
    LIMIT $2
4186
),
4187
selected_channels AS (
4188
    SELECT
4189
        s.cursor_scid AS selected_scid,
4190
        COALESCE(
4191
            (
4192
                SELECT c.id
4193
                FROM graph_channels c
4194
                WHERE c.scid = s.cursor_scid
4195
                  AND c.version = 2
4196
                  AND EXISTS (
4197
                      SELECT 1
4198
                      FROM graph_channel_policies p
4199
                      WHERE p.channel_id = c.id
4200
                        AND p.version = 2
4201
                  )
4202
            ),
4203
            (
4204
                SELECT c.id
4205
                FROM graph_channels c
4206
                WHERE c.scid = s.cursor_scid
4207
                  AND c.version = 1
4208
                  AND EXISTS (
4209
                      SELECT 1
4210
                      FROM graph_channel_policies p
4211
                      WHERE p.channel_id = c.id
4212
                        AND p.version = 1
4213
                  )
4214
            ),
4215
            (
4216
                SELECT c.id
4217
                FROM graph_channels c
4218
                WHERE c.scid = s.cursor_scid
4219
                  AND c.version = 2
4220
            ),
4221
            (
4222
                SELECT c.id
4223
                FROM graph_channels c
4224
                WHERE c.scid = s.cursor_scid
4225
                  AND c.version = 1
4226
            )
4227
        ) AS channel_db_id
4228
    FROM page_scids s
4229
)
4230
SELECT
4231
    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,
4232

4233
    -- Join node pubkeys
4234
    n1.pub_key AS node1_pubkey,
4235
    n2.pub_key AS node2_pubkey,
4236

4237
    -- Node 1 policy
4238
    cp1.id AS policy_1_id,
4239
    cp1.node_id AS policy_1_node_id,
4240
    cp1.version AS policy_1_version,
4241
    cp1.timelock AS policy_1_timelock,
4242
    cp1.fee_ppm AS policy_1_fee_ppm,
4243
    cp1.base_fee_msat AS policy_1_base_fee_msat,
4244
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
4245
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
4246
    cp1.last_update AS policy_1_last_update,
4247
    cp1.disabled AS policy_1_disabled,
4248
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
4249
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
4250
    cp1.message_flags AS policy1_message_flags,
4251
    cp1.channel_flags AS policy1_channel_flags,
4252
    cp1.block_height AS policy1_block_height,
4253
    cp1.disable_flags AS policy1_disable_flags,
4254
    cp1.signature AS policy_1_signature,
4255

4256
    -- Node 2 policy
4257
    cp2.id AS policy_2_id,
4258
    cp2.node_id AS policy_2_node_id,
4259
    cp2.version AS policy_2_version,
4260
    cp2.timelock AS policy_2_timelock,
4261
    cp2.fee_ppm AS policy_2_fee_ppm,
4262
    cp2.base_fee_msat AS policy_2_base_fee_msat,
4263
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
4264
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
4265
    cp2.last_update AS policy_2_last_update,
4266
    cp2.disabled AS policy_2_disabled,
4267
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
4268
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
4269
    cp2.message_flags AS policy2_message_flags,
4270
    cp2.channel_flags AS policy2_channel_flags,
4271
    cp2.signature AS policy_2_signature,
4272
    cp2.block_height AS policy_2_block_height,
4273
    cp2.disable_flags AS policy_2_disable_flags
4274

4275
FROM selected_channels s
4276
JOIN graph_channels c ON c.id = s.channel_db_id
4277
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
4278
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
4279
LEFT JOIN graph_channel_policies cp1
4280
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
4281
LEFT JOIN graph_channel_policies cp2
4282
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
4283
ORDER BY s.selected_scid
4284
`
4285

4286
type ListPreferredChannelsWithPoliciesPaginatedParams struct {
4287
        Scid  []byte
4288
        Limit int32
4289
}
4290

4291
type ListPreferredChannelsWithPoliciesPaginatedRow struct {
4292
        GraphChannel                   GraphChannel
4293
        Node1Pubkey                    []byte
4294
        Node2Pubkey                    []byte
4295
        Policy1ID                      sql.NullInt64
4296
        Policy1NodeID                  sql.NullInt64
4297
        Policy1Version                 sql.NullInt16
4298
        Policy1Timelock                sql.NullInt32
4299
        Policy1FeePpm                  sql.NullInt64
4300
        Policy1BaseFeeMsat             sql.NullInt64
4301
        Policy1MinHtlcMsat             sql.NullInt64
4302
        Policy1MaxHtlcMsat             sql.NullInt64
4303
        Policy1LastUpdate              sql.NullInt64
4304
        Policy1Disabled                sql.NullBool
4305
        Policy1InboundBaseFeeMsat      sql.NullInt64
4306
        Policy1InboundFeeRateMilliMsat sql.NullInt64
4307
        Policy1MessageFlags            sql.NullInt16
4308
        Policy1ChannelFlags            sql.NullInt16
4309
        Policy1BlockHeight             sql.NullInt64
4310
        Policy1DisableFlags            sql.NullInt16
4311
        Policy1Signature               []byte
4312
        Policy2ID                      sql.NullInt64
4313
        Policy2NodeID                  sql.NullInt64
4314
        Policy2Version                 sql.NullInt16
4315
        Policy2Timelock                sql.NullInt32
4316
        Policy2FeePpm                  sql.NullInt64
4317
        Policy2BaseFeeMsat             sql.NullInt64
4318
        Policy2MinHtlcMsat             sql.NullInt64
4319
        Policy2MaxHtlcMsat             sql.NullInt64
4320
        Policy2LastUpdate              sql.NullInt64
4321
        Policy2Disabled                sql.NullBool
4322
        Policy2InboundBaseFeeMsat      sql.NullInt64
4323
        Policy2InboundFeeRateMilliMsat sql.NullInt64
4324
        Policy2MessageFlags            sql.NullInt16
4325
        Policy2ChannelFlags            sql.NullInt16
4326
        Policy2Signature               []byte
4327
        Policy2BlockHeight             sql.NullInt64
4328
        Policy2DisableFlags            sql.NullInt16
4329
}
4330

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

4407
const listPreferredDirectedChannelsPaginated = `-- name: ListPreferredDirectedChannelsPaginated :many
4408
WITH page_scids(cursor_scid) AS (
4409
    SELECT page.scid
4410
    FROM (
4411
        SELECT c2.scid AS scid
4412
        FROM graph_channels c2
4413
        WHERE c2.version = 2
4414
          AND (c2.node_id_1 = $1 OR c2.node_id_2 = $1)
4415
          AND c2.scid > $2
4416
        UNION
4417
        SELECT c1.scid AS scid
4418
        FROM graph_channels c1
4419
        WHERE c1.version = 1
4420
          AND (c1.node_id_1 = $3 OR c1.node_id_2 = $3)
4421
          AND c1.scid > $2
4422
    ) AS page
4423
    ORDER BY page.scid
4424
    LIMIT $4
4425
),
4426
selected_channels AS (
4427
    SELECT
4428
        s.cursor_scid AS selected_scid,
4429
        COALESCE(
4430
            (
4431
                SELECT c.id
4432
                FROM graph_channels c
4433
                WHERE c.scid = s.cursor_scid
4434
                  AND c.version = 2
4435
                  AND (c.node_id_1 = $1 OR c.node_id_2 = $1)
4436
            ),
4437
            (
4438
                SELECT c.id
4439
                FROM graph_channels c
4440
                WHERE c.scid = s.cursor_scid
4441
                  AND c.version = 1
4442
                  AND (c.node_id_1 = $3 OR c.node_id_2 = $3)
4443
            )
4444
        ) AS channel_db_id
4445
    FROM page_scids s
4446
)
4447
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,
4448
    n1.pub_key AS node1_pubkey,
4449
    n2.pub_key AS node2_pubkey,
4450

4451
    -- Policy 1
4452
    cp1.id AS policy1_id,
4453
    cp1.node_id AS policy1_node_id,
4454
    cp1.version AS policy1_version,
4455
    cp1.timelock AS policy1_timelock,
4456
    cp1.fee_ppm AS policy1_fee_ppm,
4457
    cp1.base_fee_msat AS policy1_base_fee_msat,
4458
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
4459
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
4460
    cp1.last_update AS policy1_last_update,
4461
    cp1.disabled AS policy1_disabled,
4462
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
4463
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
4464
    cp1.message_flags AS policy1_message_flags,
4465
    cp1.channel_flags AS policy1_channel_flags,
4466
    cp1.signature AS policy1_signature,
4467
    cp1.block_height AS policy1_block_height,
4468
    cp1.disable_flags AS policy1_disable_flags,
4469

4470
    -- Policy 2
4471
    cp2.id AS policy2_id,
4472
    cp2.node_id AS policy2_node_id,
4473
    cp2.version AS policy2_version,
4474
    cp2.timelock AS policy2_timelock,
4475
    cp2.fee_ppm AS policy2_fee_ppm,
4476
    cp2.base_fee_msat AS policy2_base_fee_msat,
4477
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
4478
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
4479
    cp2.last_update AS policy2_last_update,
4480
    cp2.disabled AS policy2_disabled,
4481
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
4482
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
4483
    cp2.message_flags AS policy2_message_flags,
4484
    cp2.channel_flags AS policy2_channel_flags,
4485
    cp2.signature AS policy2_signature,
4486
    cp2.block_height AS policy2_block_height,
4487
    cp2.disable_flags AS policy2_disable_flags
4488

4489
FROM selected_channels s
4490
JOIN graph_channels c ON c.id = s.channel_db_id
4491
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
4492
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
4493
LEFT JOIN graph_channel_policies cp1
4494
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
4495
LEFT JOIN graph_channel_policies cp2
4496
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
4497
ORDER BY s.selected_scid
4498
`
4499

4500
type ListPreferredDirectedChannelsPaginatedParams struct {
4501
        NodeIDV2  int64
4502
        Scid      []byte
4503
        NodeIDV1  int64
4504
        PageLimit int32
4505
}
4506

4507
type ListPreferredDirectedChannelsPaginatedRow struct {
4508
        GraphChannel                   GraphChannel
4509
        Node1Pubkey                    []byte
4510
        Node2Pubkey                    []byte
4511
        Policy1ID                      sql.NullInt64
4512
        Policy1NodeID                  sql.NullInt64
4513
        Policy1Version                 sql.NullInt16
4514
        Policy1Timelock                sql.NullInt32
4515
        Policy1FeePpm                  sql.NullInt64
4516
        Policy1BaseFeeMsat             sql.NullInt64
4517
        Policy1MinHtlcMsat             sql.NullInt64
4518
        Policy1MaxHtlcMsat             sql.NullInt64
4519
        Policy1LastUpdate              sql.NullInt64
4520
        Policy1Disabled                sql.NullBool
4521
        Policy1InboundBaseFeeMsat      sql.NullInt64
4522
        Policy1InboundFeeRateMilliMsat sql.NullInt64
4523
        Policy1MessageFlags            sql.NullInt16
4524
        Policy1ChannelFlags            sql.NullInt16
4525
        Policy1Signature               []byte
4526
        Policy1BlockHeight             sql.NullInt64
4527
        Policy1DisableFlags            sql.NullInt16
4528
        Policy2ID                      sql.NullInt64
4529
        Policy2NodeID                  sql.NullInt64
4530
        Policy2Version                 sql.NullInt16
4531
        Policy2Timelock                sql.NullInt32
4532
        Policy2FeePpm                  sql.NullInt64
4533
        Policy2BaseFeeMsat             sql.NullInt64
4534
        Policy2MinHtlcMsat             sql.NullInt64
4535
        Policy2MaxHtlcMsat             sql.NullInt64
4536
        Policy2LastUpdate              sql.NullInt64
4537
        Policy2Disabled                sql.NullBool
4538
        Policy2InboundBaseFeeMsat      sql.NullInt64
4539
        Policy2InboundFeeRateMilliMsat sql.NullInt64
4540
        Policy2MessageFlags            sql.NullInt16
4541
        Policy2ChannelFlags            sql.NullInt16
4542
        Policy2Signature               []byte
4543
        Policy2BlockHeight             sql.NullInt64
4544
        Policy2DisableFlags            sql.NullInt16
4545
}
4546

NEW
4547
func (q *Queries) ListPreferredDirectedChannelsPaginated(ctx context.Context, arg ListPreferredDirectedChannelsPaginatedParams) ([]ListPreferredDirectedChannelsPaginatedRow, error) {
×
NEW
4548
        rows, err := q.db.QueryContext(ctx, listPreferredDirectedChannelsPaginated,
×
NEW
4549
                arg.NodeIDV2,
×
NEW
4550
                arg.Scid,
×
NEW
4551
                arg.NodeIDV1,
×
NEW
4552
                arg.PageLimit,
×
NEW
4553
        )
×
NEW
4554
        if err != nil {
×
NEW
4555
                return nil, err
×
NEW
4556
        }
×
NEW
4557
        defer rows.Close()
×
NEW
4558
        var items []ListPreferredDirectedChannelsPaginatedRow
×
NEW
4559
        for rows.Next() {
×
NEW
4560
                var i ListPreferredDirectedChannelsPaginatedRow
×
NEW
4561
                if err := rows.Scan(
×
NEW
4562
                        &i.GraphChannel.ID,
×
NEW
4563
                        &i.GraphChannel.Version,
×
NEW
4564
                        &i.GraphChannel.Scid,
×
NEW
4565
                        &i.GraphChannel.NodeID1,
×
NEW
4566
                        &i.GraphChannel.NodeID2,
×
NEW
4567
                        &i.GraphChannel.Outpoint,
×
NEW
4568
                        &i.GraphChannel.Capacity,
×
NEW
4569
                        &i.GraphChannel.BitcoinKey1,
×
NEW
4570
                        &i.GraphChannel.BitcoinKey2,
×
NEW
4571
                        &i.GraphChannel.Node1Signature,
×
NEW
4572
                        &i.GraphChannel.Node2Signature,
×
NEW
4573
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
4574
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
4575
                        &i.GraphChannel.Signature,
×
NEW
4576
                        &i.GraphChannel.FundingPkScript,
×
NEW
4577
                        &i.GraphChannel.MerkleRootHash,
×
NEW
4578
                        &i.Node1Pubkey,
×
NEW
4579
                        &i.Node2Pubkey,
×
NEW
4580
                        &i.Policy1ID,
×
NEW
4581
                        &i.Policy1NodeID,
×
NEW
4582
                        &i.Policy1Version,
×
NEW
4583
                        &i.Policy1Timelock,
×
NEW
4584
                        &i.Policy1FeePpm,
×
NEW
4585
                        &i.Policy1BaseFeeMsat,
×
NEW
4586
                        &i.Policy1MinHtlcMsat,
×
NEW
4587
                        &i.Policy1MaxHtlcMsat,
×
NEW
4588
                        &i.Policy1LastUpdate,
×
NEW
4589
                        &i.Policy1Disabled,
×
NEW
4590
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
4591
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
4592
                        &i.Policy1MessageFlags,
×
NEW
4593
                        &i.Policy1ChannelFlags,
×
NEW
4594
                        &i.Policy1Signature,
×
NEW
4595
                        &i.Policy1BlockHeight,
×
NEW
4596
                        &i.Policy1DisableFlags,
×
NEW
4597
                        &i.Policy2ID,
×
NEW
4598
                        &i.Policy2NodeID,
×
NEW
4599
                        &i.Policy2Version,
×
NEW
4600
                        &i.Policy2Timelock,
×
NEW
4601
                        &i.Policy2FeePpm,
×
NEW
4602
                        &i.Policy2BaseFeeMsat,
×
NEW
4603
                        &i.Policy2MinHtlcMsat,
×
NEW
4604
                        &i.Policy2MaxHtlcMsat,
×
NEW
4605
                        &i.Policy2LastUpdate,
×
NEW
4606
                        &i.Policy2Disabled,
×
NEW
4607
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
4608
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
4609
                        &i.Policy2MessageFlags,
×
NEW
4610
                        &i.Policy2ChannelFlags,
×
NEW
4611
                        &i.Policy2Signature,
×
NEW
4612
                        &i.Policy2BlockHeight,
×
NEW
4613
                        &i.Policy2DisableFlags,
×
NEW
4614
                ); err != nil {
×
NEW
4615
                        return nil, err
×
NEW
4616
                }
×
NEW
4617
                items = append(items, i)
×
4618
        }
NEW
4619
        if err := rows.Close(); err != nil {
×
NEW
4620
                return nil, err
×
NEW
4621
        }
×
NEW
4622
        if err := rows.Err(); err != nil {
×
NEW
4623
                return nil, err
×
NEW
4624
        }
×
NEW
4625
        return items, nil
×
4626
}
4627

4628
const listPreferredNodesPaginated = `-- name: ListPreferredNodesPaginated :many
4629
WITH page_pub_keys(cursor_pub_key) AS (
4630
    SELECT page.pub_key
4631
    FROM (
4632
        SELECT n2.pub_key AS pub_key
4633
        FROM graph_nodes n2
4634
        WHERE n2.version = 2
4635
          AND n2.pub_key > $1
4636
        UNION
4637
        SELECT n1.pub_key AS pub_key
4638
        FROM graph_nodes n1
4639
        WHERE n1.version = 1
4640
          AND n1.pub_key > $1
4641
    ) AS page
4642
    ORDER BY page.pub_key
4643
    LIMIT $2
4644
),
4645
selected_nodes AS (
4646
    SELECT
4647
        p.cursor_pub_key AS selected_pub_key,
4648
        COALESCE(
4649
            (
4650
                SELECT n.id
4651
                FROM graph_nodes n
4652
                WHERE n.pub_key = p.cursor_pub_key
4653
                  AND n.version = 2
4654
                  AND COALESCE(length(n.signature), 0) > 0
4655
            ),
4656
            (
4657
                SELECT n.id
4658
                FROM graph_nodes n
4659
                WHERE n.pub_key = p.cursor_pub_key
4660
                  AND n.version = 1
4661
                  AND COALESCE(length(n.signature), 0) > 0
4662
            ),
4663
            (
4664
                SELECT n.id
4665
                FROM graph_nodes n
4666
                WHERE n.pub_key = p.cursor_pub_key
4667
                  AND n.version = 2
4668
            ),
4669
            (
4670
                SELECT n.id
4671
                FROM graph_nodes n
4672
                WHERE n.pub_key = p.cursor_pub_key
4673
                  AND n.version = 1
4674
            )
4675
        ) AS node_id
4676
    FROM page_pub_keys p
4677
)
4678
SELECT n.id, n.version, n.pub_key, n.alias, n.last_update, n.color, n.signature, n.block_height
4679
FROM selected_nodes s
4680
JOIN graph_nodes n ON n.id = s.node_id
4681
ORDER BY s.selected_pub_key
4682
`
4683

4684
type ListPreferredNodesPaginatedParams struct {
4685
        PubKey []byte
4686
        Limit  int32
4687
}
4688

4689
type ListPreferredNodesPaginatedRow struct {
4690
        GraphNode GraphNode
4691
}
4692

NEW
4693
func (q *Queries) ListPreferredNodesPaginated(ctx context.Context, arg ListPreferredNodesPaginatedParams) ([]ListPreferredNodesPaginatedRow, error) {
×
NEW
4694
        rows, err := q.db.QueryContext(ctx, listPreferredNodesPaginated, arg.PubKey, arg.Limit)
×
NEW
4695
        if err != nil {
×
NEW
4696
                return nil, err
×
NEW
4697
        }
×
NEW
4698
        defer rows.Close()
×
NEW
4699
        var items []ListPreferredNodesPaginatedRow
×
NEW
4700
        for rows.Next() {
×
NEW
4701
                var i ListPreferredNodesPaginatedRow
×
NEW
4702
                if err := rows.Scan(
×
NEW
4703
                        &i.GraphNode.ID,
×
NEW
4704
                        &i.GraphNode.Version,
×
NEW
4705
                        &i.GraphNode.PubKey,
×
NEW
4706
                        &i.GraphNode.Alias,
×
NEW
4707
                        &i.GraphNode.LastUpdate,
×
NEW
4708
                        &i.GraphNode.Color,
×
NEW
4709
                        &i.GraphNode.Signature,
×
NEW
4710
                        &i.GraphNode.BlockHeight,
×
NEW
4711
                ); err != nil {
×
NEW
4712
                        return nil, err
×
NEW
4713
                }
×
NEW
4714
                items = append(items, i)
×
4715
        }
NEW
4716
        if err := rows.Close(); err != nil {
×
NEW
4717
                return nil, err
×
NEW
4718
        }
×
NEW
4719
        if err := rows.Err(); err != nil {
×
NEW
4720
                return nil, err
×
NEW
4721
        }
×
NEW
4722
        return items, nil
×
4723
}
4724

4725
const nodeExists = `-- name: NodeExists :one
4726
SELECT EXISTS (
4727
    SELECT 1
4728
    FROM graph_nodes
4729
    WHERE pub_key = $1
4730
      AND version = $2
4731
) AS node_exists
4732
`
4733

4734
type NodeExistsParams struct {
4735
        PubKey  []byte
4736
        Version int16
4737
}
4738

4739
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
4740
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
4741
        var node_exists bool
×
4742
        err := row.Scan(&node_exists)
×
4743
        return node_exists, err
×
4744
}
×
4745

4746
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
4747
/* ─────────────────────────────────────────────
4748
   graph_channel_policy_extra_types table queries
4749
   ─────────────────────────────────────────────
4750
*/
4751

4752
INSERT INTO graph_channel_policy_extra_types (
4753
    channel_policy_id, type, value
4754
)
4755
VALUES ($1, $2, $3)
4756
ON CONFLICT (channel_policy_id, type)
4757
    -- If a conflict occurs on channel_policy_id and type, then we update the
4758
    -- value.
4759
    DO UPDATE SET value = EXCLUDED.value
4760
`
4761

4762
type UpsertChanPolicyExtraTypeParams struct {
4763
        ChannelPolicyID int64
4764
        Type            int64
4765
        Value           []byte
4766
}
4767

4768
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
4769
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
4770
        return err
×
4771
}
×
4772

4773
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
4774
/* ─────────────────────────────────────────────
4775
   graph_channel_extra_types table queries
4776
   ─────────────────────────────────────────────
4777
*/
4778

4779
INSERT INTO graph_channel_extra_types (
4780
    channel_id, type, value
4781
)
4782
VALUES ($1, $2, $3)
4783
    ON CONFLICT (channel_id, type)
4784
    -- Update the value if a conflict occurs on channel_id and type.
4785
    DO UPDATE SET value = EXCLUDED.value
4786
`
4787

4788
type UpsertChannelExtraTypeParams struct {
4789
        ChannelID int64
4790
        Type      int64
4791
        Value     []byte
4792
}
4793

4794
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
4795
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
4796
        return err
×
4797
}
×
4798

4799
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
4800
/* ─────────────────────────────────────────────
4801
   graph_channel_policies table queries
4802
   ─────────────────────────────────────────────
4803
*/
4804

4805
INSERT INTO graph_channel_policies (
4806
    version, channel_id, node_id, timelock, fee_ppm,
4807
    base_fee_msat, min_htlc_msat, last_update, disabled,
4808
    max_htlc_msat, inbound_base_fee_msat,
4809
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
4810
    signature, block_height, disable_flags
4811
) VALUES  (
4812
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
4813
)
4814
ON CONFLICT (channel_id, node_id, version)
4815
    -- Update the following fields if a conflict occurs on channel_id,
4816
    -- node_id, and version.
4817
    DO UPDATE SET
4818
        timelock = EXCLUDED.timelock,
4819
        fee_ppm = EXCLUDED.fee_ppm,
4820
        base_fee_msat = EXCLUDED.base_fee_msat,
4821
        min_htlc_msat = EXCLUDED.min_htlc_msat,
4822
        last_update = EXCLUDED.last_update,
4823
        disabled = EXCLUDED.disabled,
4824
        max_htlc_msat = EXCLUDED.max_htlc_msat,
4825
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
4826
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
4827
        message_flags = EXCLUDED.message_flags,
4828
        channel_flags = EXCLUDED.channel_flags,
4829
        signature = EXCLUDED.signature,
4830
        block_height = EXCLUDED.block_height,
4831
        disable_flags = EXCLUDED.disable_flags
4832
WHERE (
4833
    EXCLUDED.version = 1 AND (
4834
        graph_channel_policies.last_update IS NULL
4835
        OR EXCLUDED.last_update > graph_channel_policies.last_update
4836
    )
4837
)
4838
OR (
4839
    EXCLUDED.version = 2 AND (
4840
        graph_channel_policies.block_height IS NULL
4841
        OR EXCLUDED.block_height >= graph_channel_policies.block_height
4842
    )
4843
)
4844
RETURNING id
4845
`
4846

4847
type UpsertEdgePolicyParams struct {
4848
        Version                 int16
4849
        ChannelID               int64
4850
        NodeID                  int64
4851
        Timelock                int32
4852
        FeePpm                  int64
4853
        BaseFeeMsat             int64
4854
        MinHtlcMsat             int64
4855
        LastUpdate              sql.NullInt64
4856
        Disabled                sql.NullBool
4857
        MaxHtlcMsat             sql.NullInt64
4858
        InboundBaseFeeMsat      sql.NullInt64
4859
        InboundFeeRateMilliMsat sql.NullInt64
4860
        MessageFlags            sql.NullInt16
4861
        ChannelFlags            sql.NullInt16
4862
        Signature               []byte
4863
        BlockHeight             sql.NullInt64
4864
        DisableFlags            sql.NullInt16
4865
}
4866

4867
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
4868
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
4869
                arg.Version,
×
4870
                arg.ChannelID,
×
4871
                arg.NodeID,
×
4872
                arg.Timelock,
×
4873
                arg.FeePpm,
×
4874
                arg.BaseFeeMsat,
×
4875
                arg.MinHtlcMsat,
×
4876
                arg.LastUpdate,
×
4877
                arg.Disabled,
×
4878
                arg.MaxHtlcMsat,
×
4879
                arg.InboundBaseFeeMsat,
×
4880
                arg.InboundFeeRateMilliMsat,
×
4881
                arg.MessageFlags,
×
4882
                arg.ChannelFlags,
×
4883
                arg.Signature,
×
4884
                arg.BlockHeight,
×
4885
                arg.DisableFlags,
×
4886
        )
×
4887
        var id int64
×
4888
        err := row.Scan(&id)
×
4889
        return id, err
×
4890
}
×
4891

4892
const upsertNode = `-- name: UpsertNode :one
4893
/* ─────────────────────────────────────────────
4894
   graph_nodes table queries
4895
   ───────────────────────────��─────────────────
4896
*/
4897

4898
INSERT INTO graph_nodes (
4899
    version, pub_key, alias, last_update, block_height, color, signature
4900
) VALUES (
4901
    $1, $2, $3, $4, $5, $6, $7
4902
)
4903
ON CONFLICT (pub_key, version)
4904
    -- Update the following fields if a conflict occurs on pub_key
4905
    -- and version.
4906
    DO UPDATE SET
4907
        alias = EXCLUDED.alias,
4908
        last_update = EXCLUDED.last_update,
4909
        block_height = EXCLUDED.block_height,
4910
        color = EXCLUDED.color,
4911
        signature = EXCLUDED.signature
4912
WHERE (graph_nodes.last_update IS NULL
4913
    OR EXCLUDED.last_update > graph_nodes.last_update)
4914
AND (graph_nodes.block_height IS NULL
4915
    OR EXCLUDED.block_height >= graph_nodes.block_height)
4916
RETURNING id
4917
`
4918

4919
type UpsertNodeParams struct {
4920
        Version     int16
4921
        PubKey      []byte
4922
        Alias       sql.NullString
4923
        LastUpdate  sql.NullInt64
4924
        BlockHeight sql.NullInt64
4925
        Color       sql.NullString
4926
        Signature   []byte
4927
}
4928

4929
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
4930
        row := q.db.QueryRowContext(ctx, upsertNode,
×
4931
                arg.Version,
×
4932
                arg.PubKey,
×
4933
                arg.Alias,
×
4934
                arg.LastUpdate,
×
4935
                arg.BlockHeight,
×
4936
                arg.Color,
×
4937
                arg.Signature,
×
4938
        )
×
4939
        var id int64
×
4940
        err := row.Scan(&id)
×
4941
        return id, err
×
4942
}
×
4943

4944
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
4945
/* ─────────────────────────────────────────────
4946
   graph_node_addresses table queries
4947
   ───────────────────────────────────��─────────
4948
*/
4949

4950
INSERT INTO graph_node_addresses (
4951
    node_id,
4952
    type,
4953
    address,
4954
    position
4955
) VALUES (
4956
    $1, $2, $3, $4
4957
) ON CONFLICT (node_id, type, position)
4958
    DO UPDATE SET address = EXCLUDED.address
4959
`
4960

4961
type UpsertNodeAddressParams struct {
4962
        NodeID   int64
4963
        Type     int16
4964
        Address  string
4965
        Position int32
4966
}
4967

4968
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
4969
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
4970
                arg.NodeID,
×
4971
                arg.Type,
×
4972
                arg.Address,
×
4973
                arg.Position,
×
4974
        )
×
4975
        return err
×
4976
}
×
4977

4978
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
4979
/* ─────────────────────────────────────────────
4980
   graph_node_extra_types table queries
4981
   ─────────────────────────────────────────────
4982
*/
4983

4984
INSERT INTO graph_node_extra_types (
4985
    node_id, type, value
4986
)
4987
VALUES ($1, $2, $3)
4988
ON CONFLICT (type, node_id)
4989
    -- Update the value if a conflict occurs on type
4990
    -- and node_id.
4991
    DO UPDATE SET value = EXCLUDED.value
4992
`
4993

4994
type UpsertNodeExtraTypeParams struct {
4995
        NodeID int64
4996
        Type   int64
4997
        Value  []byte
4998
}
4999

5000
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
5001
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
5002
        return err
×
5003
}
×
5004

5005
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
5006
/* ───────────────────────────���─────────────────
5007
    graph_prune_log table queries
5008
    ─────────────────────────────────────────────
5009
*/
5010

5011
INSERT INTO graph_prune_log (
5012
    block_height, block_hash
5013
) VALUES (
5014
    $1, $2
5015
)
5016
ON CONFLICT(block_height) DO UPDATE SET
5017
    block_hash = EXCLUDED.block_hash
5018
`
5019

5020
type UpsertPruneLogEntryParams struct {
5021
        BlockHeight int64
5022
        BlockHash   []byte
5023
}
5024

5025
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
5026
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
5027
        return err
×
5028
}
×
5029

5030
const upsertSourceNode = `-- name: UpsertSourceNode :one
5031
INSERT INTO graph_nodes (
5032
    version, pub_key, alias, last_update, block_height, color, signature
5033
) VALUES (
5034
    $1, $2, $3, $4, $5, $6, $7
5035
)
5036
ON CONFLICT (pub_key, version)
5037
    -- Update the following fields if a conflict occurs on pub_key
5038
    -- and version.
5039
    DO UPDATE SET
5040
        alias = EXCLUDED.alias,
5041
        last_update = EXCLUDED.last_update,
5042
        block_height = EXCLUDED.block_height,
5043
        color = EXCLUDED.color,
5044
        signature = EXCLUDED.signature
5045
WHERE graph_nodes.last_update IS NULL
5046
    OR EXCLUDED.last_update >= graph_nodes.last_update
5047
AND (graph_nodes.block_height IS NULL
5048
   OR EXCLUDED.block_height >= graph_nodes.block_height)
5049
RETURNING id
5050
`
5051

5052
type UpsertSourceNodeParams struct {
5053
        Version     int16
5054
        PubKey      []byte
5055
        Alias       sql.NullString
5056
        LastUpdate  sql.NullInt64
5057
        BlockHeight sql.NullInt64
5058
        Color       sql.NullString
5059
        Signature   []byte
5060
}
5061

5062
// We use a separate upsert for our own node since we want to be less strict
5063
// about the last_update field. For our own node, we always want to
5064
// update the record even if the last_update is the same as what we have.
5065
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
5066
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
5067
                arg.Version,
×
5068
                arg.PubKey,
×
5069
                arg.Alias,
×
5070
                arg.LastUpdate,
×
5071
                arg.BlockHeight,
×
5072
                arg.Color,
×
5073
                arg.Signature,
×
5074
        )
×
5075
        var id int64
×
5076
        err := row.Scan(&id)
×
5077
        return id, err
×
5078
}
×
5079

5080
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
5081
/* ─────────────────────────────────────────────
5082
   graph_zombie_channels table queries
5083
   ─────────────────────────────────────────────
5084
*/
5085

5086
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
5087
VALUES ($1, $2, $3, $4)
5088
ON CONFLICT (scid, version)
5089
DO UPDATE SET
5090
    -- If a conflict exists for the SCID and version pair, then we
5091
    -- update the node keys.
5092
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
5093
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
5094
`
5095

5096
type UpsertZombieChannelParams struct {
5097
        Scid     []byte
5098
        Version  int16
5099
        NodeKey1 []byte
5100
        NodeKey2 []byte
5101
}
5102

5103
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
5104
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
5105
                arg.Scid,
×
5106
                arg.Version,
×
5107
                arg.NodeKey1,
×
5108
                arg.NodeKey2,
×
5109
        )
×
5110
        return err
×
5111
}
×
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