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

lightningnetwork / lnd / 23444781664

23 Mar 2026 03:16PM UTC coverage: 62.213% (-0.1%) from 62.31%
23444781664

Pull #10656

github

web-flow
Merge fe7a97b3f into 02206abe6
Pull Request #10656: graph/db: cross-version graph Store

178 of 805 new or added lines in 7 files covered. (22.11%)

94 existing lines in 17 files now uncovered.

141276 of 227084 relevant lines covered (62.21%)

19340.05 hits per line

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

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

6
package sqlc
7

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1176
const getChannelsByPolicyBlockRange = `-- name: GetChannelsByPolicyBlockRange :many
1177
SELECT
1178
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1179
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1180
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1181

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

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

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

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

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

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

1405
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1406
SELECT
1407
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1408
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1409
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1410

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

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

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

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

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

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

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

1643
type GetChannelsBySCIDRangeParams struct {
1644
        StartScid []byte
1645
        EndScid   []byte
1646
}
1647

1648
type GetChannelsBySCIDRangeRow struct {
1649
        GraphChannel GraphChannel
1650
        Node1PubKey  []byte
1651
        Node2PubKey  []byte
1652
}
1653

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

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

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

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

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

1752
type GetChannelsBySCIDWithPoliciesParams struct {
1753
        Version int16
1754
        Scids   [][]byte
1755
}
1756

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

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

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

1904
type GetChannelsBySCIDsParams struct {
1905
        Version int16
1906
        Scids   [][]byte
1907
}
1908

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

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

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

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

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

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

2035
type GetNodeAddressesRow struct {
2036
        Type    int16
2037
        Address string
2038
}
2039

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

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

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

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

2115
type GetNodeByPubKeyParams struct {
2116
        PubKey  []byte
2117
        Version int16
2118
}
2119

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

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

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

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

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

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

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

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

2253
type GetNodeFeaturesByPubKeyParams struct {
2254
        PubKey  []byte
2255
        Version int16
2256
}
2257

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

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

2288
type GetNodeIDByPubKeyParams struct {
2289
        PubKey  []byte
2290
        Version int16
2291
}
2292

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2595
type GetPublicV1ChannelsBySCIDParams struct {
2596
        StartScid []byte
2597
        EndScid   []byte
2598
}
2599

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

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

2650
type GetPublicV2ChannelsBySCIDParams struct {
2651
        StartScid []byte
2652
        EndScid   []byte
2653
}
2654

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

2695
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2696
SELECT scid from graph_channels
2697
WHERE outpoint = $1 AND version = $2
2698
`
2699

2700
type GetSCIDByOutpointParams struct {
2701
        Outpoint string
2702
        Version  int16
2703
}
2704

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

2712
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2713
SELECT sn.node_id, n.pub_key
2714
FROM graph_source_nodes sn
2715
    JOIN graph_nodes n ON sn.node_id = n.id
2716
WHERE n.version = $1
2717
`
2718

2719
type GetSourceNodesByVersionRow struct {
2720
        NodeID int64
2721
        PubKey []byte
2722
}
2723

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

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

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

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

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

2819
const getZombieChannel = `-- name: GetZombieChannel :one
2820
SELECT scid, version, node_key_1, node_key_2
2821
FROM graph_zombie_channels
2822
WHERE scid = $1
2823
AND version = $2
2824
`
2825

2826
type GetZombieChannelParams struct {
2827
        Scid    []byte
2828
        Version int16
2829
}
2830

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

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

2850
type GetZombieChannelsSCIDsParams struct {
2851
        Version int16
2852
        Scids   [][]byte
2853
}
2854

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

2894
const highestSCID = `-- name: HighestSCID :one
2895
SELECT scid
2896
FROM graph_channels
2897
WHERE version = $1
2898
ORDER BY scid DESC
2899
LIMIT 1
2900
`
2901

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

2909
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2910
/* ─────────────────────────────────────────────
2911
   graph_channel_features table queries
2912
   ─────────────────────────────────────────────
2913
*/
2914

2915
INSERT INTO graph_channel_features (
2916
    channel_id, feature_bit
2917
) VALUES (
2918
    $1, $2
2919
) ON CONFLICT (channel_id, feature_bit)
2920
    -- Do nothing if the channel_id and feature_bit already exist.
2921
    DO NOTHING
2922
`
2923

2924
type InsertChannelFeatureParams struct {
2925
        ChannelID  int64
2926
        FeatureBit int32
2927
}
2928

2929
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2930
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2931
        return err
×
2932
}
×
2933

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

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

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

3000
const insertClosedChannel = `-- name: InsertClosedChannel :exec
3001
/* ─────────────────────────────────────────────
3002
   graph_closed_scid table queries
3003
   ────────────────────────────────────────────-
3004
*/
3005

3006
INSERT INTO graph_closed_scids (scid)
3007
VALUES ($1)
3008
ON CONFLICT (scid) DO NOTHING
3009
`
3010

3011
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
3012
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
3013
        return err
×
3014
}
×
3015

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

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

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

3092
const insertNodeFeature = `-- name: InsertNodeFeature :exec
3093
/* ─────────────────────────────────────────────
3094
   graph_node_features table queries
3095
   ─────────────────────────────────────────────
3096
*/
3097

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

3107
type InsertNodeFeatureParams struct {
3108
        NodeID     int64
3109
        FeatureBit int32
3110
}
3111

3112
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
3113
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
3114
        return err
×
3115
}
×
3116

3117
const insertNodeMig = `-- name: InsertNodeMig :one
3118
/* ─────────────────────────────────────────────
3119
   Migration specific queries
3120

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

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

3144
type InsertNodeMigParams struct {
3145
        Version    int16
3146
        PubKey     []byte
3147
        Alias      sql.NullString
3148
        LastUpdate sql.NullInt64
3149
        Color      sql.NullString
3150
        Signature  []byte
3151
}
3152

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

3172
const isClosedChannel = `-- name: IsClosedChannel :one
3173
SELECT EXISTS (
3174
    SELECT 1
3175
    FROM graph_closed_scids
3176
    WHERE scid = $1
3177
)
3178
`
3179

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

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

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

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

3232
    UNION ALL
3233

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

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

3250
const isZombieChannel = `-- name: IsZombieChannel :one
3251
SELECT EXISTS (
3252
    SELECT 1
3253
    FROM graph_zombie_channels
3254
    WHERE scid = $1
3255
    AND version = $2
3256
) AS is_zombie
3257
`
3258

3259
type IsZombieChannelParams struct {
3260
        Scid    []byte
3261
        Version int16
3262
}
3263

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

3271
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
3272
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,
3273
    n1.pub_key AS node1_pubkey,
3274
    n2.pub_key AS node2_pubkey,
3275

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

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

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

3328
type ListChannelsByNodeIDParams struct {
3329
        Version int16
3330
        NodeID1 int64
3331
}
3332

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

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

3449
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
3450
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,
3451
       n1.pub_key AS node1_pubkey,
3452
       n2.pub_key AS node2_pubkey,
3453

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

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

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

3507
type ListChannelsForNodeIDsParams struct {
3508
        Version  int16
3509
        Node1Ids []int64
3510
        Node2Ids []int64
3511
}
3512

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

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

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

3656
type ListChannelsPaginatedParams struct {
3657
        Version int16
3658
        ID      int64
3659
        Limit   int32
3660
}
3661

3662
type ListChannelsPaginatedRow struct {
3663
        ID          int64
3664
        BitcoinKey1 []byte
3665
        BitcoinKey2 []byte
3666
        Outpoint    string
3667
}
3668

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

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

3705
type ListChannelsPaginatedV2Params struct {
3706
        ID    int64
3707
        Limit int32
3708
}
3709

3710
type ListChannelsPaginatedV2Row struct {
3711
        ID              int64
3712
        Outpoint        string
3713
        FundingPkScript []byte
3714
}
3715

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

3739
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3740
SELECT
3741
    c.id as id,
3742
    c.scid as scid,
3743
    c.capacity AS capacity,
3744

3745
    -- Join node pubkeys
3746
    n1.pub_key AS node1_pubkey,
3747
    n2.pub_key AS node2_pubkey,
3748

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

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

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

3791
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3792
        Version int16
3793
        ID      int64
3794
        Limit   int32
3795
}
3796

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

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

3886
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3887
SELECT
3888
    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,
3889

3890
    -- Join node pubkeys
3891
    n1.pub_key AS node1_pubkey,
3892
    n2.pub_key AS node2_pubkey,
3893

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

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

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

3944
type ListChannelsWithPoliciesPaginatedParams struct {
3945
        Version int16
3946
        ID      int64
3947
        Limit   int32
3948
}
3949

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

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

4066
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
4067
SELECT id, pub_key
4068
FROM graph_nodes
4069
WHERE version = $1  AND id > $2
4070
ORDER BY id
4071
LIMIT $3
4072
`
4073

4074
type ListNodeIDsAndPubKeysParams struct {
4075
        Version int16
4076
        ID      int64
4077
        Limit   int32
4078
}
4079

4080
type ListNodeIDsAndPubKeysRow struct {
4081
        ID     int64
4082
        PubKey []byte
4083
}
4084

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

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

4116
type ListNodesPaginatedParams struct {
4117
        Version int16
4118
        ID      int64
4119
        Limit   int32
4120
}
4121

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

4154
const nodeExists = `-- name: NodeExists :one
4155
SELECT EXISTS (
4156
    SELECT 1
4157
    FROM graph_nodes
4158
    WHERE pub_key = $1
4159
      AND version = $2
4160
) AS node_exists
4161
`
4162

4163
type NodeExistsParams struct {
4164
        PubKey  []byte
4165
        Version int16
4166
}
4167

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

4175
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
4176
/* ─────────────────────────────────────────────
4177
   graph_channel_policy_extra_types table queries
4178
   ─────────────────────────────────────────────
4179
*/
4180

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

4191
type UpsertChanPolicyExtraTypeParams struct {
4192
        ChannelPolicyID int64
4193
        Type            int64
4194
        Value           []byte
4195
}
4196

4197
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
4198
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
4199
        return err
×
4200
}
×
4201

4202
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
4203
/* ─────────────────────────────────────────────
4204
   graph_channel_extra_types table queries
4205
   ─────────────────────────────────────────────
4206
*/
4207

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

4217
type UpsertChannelExtraTypeParams struct {
4218
        ChannelID int64
4219
        Type      int64
4220
        Value     []byte
4221
}
4222

4223
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
4224
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
4225
        return err
×
4226
}
×
4227

4228
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
4229
/* ─────────────────────────────────────────────
4230
   graph_channel_policies table queries
4231
   ─────────────────────────────────────────────
4232
*/
4233

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

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

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

4321
const upsertNode = `-- name: UpsertNode :one
4322
/* ─────────────────────────────────────────────
4323
   graph_nodes table queries
4324
   ───────────────────────────��─────────────────
4325
*/
4326

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

4348
type UpsertNodeParams struct {
4349
        Version     int16
4350
        PubKey      []byte
4351
        Alias       sql.NullString
4352
        LastUpdate  sql.NullInt64
4353
        BlockHeight sql.NullInt64
4354
        Color       sql.NullString
4355
        Signature   []byte
4356
}
4357

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

4373
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
4374
/* ─────────────────────────────────────────────
4375
   graph_node_addresses table queries
4376
   ───────────────────────────────────��─────────
4377
*/
4378

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

4390
type UpsertNodeAddressParams struct {
4391
        NodeID   int64
4392
        Type     int16
4393
        Address  string
4394
        Position int32
4395
}
4396

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

4407
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
4408
/* ─────────────────────────────────────────────
4409
   graph_node_extra_types table queries
4410
   ─────────────────────────────────────────────
4411
*/
4412

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

4423
type UpsertNodeExtraTypeParams struct {
4424
        NodeID int64
4425
        Type   int64
4426
        Value  []byte
4427
}
4428

4429
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
4430
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
4431
        return err
×
4432
}
×
4433

4434
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
4435
/* ───────────────────────────���─────────────────
4436
    graph_prune_log table queries
4437
    ─────────────────────────────────────────────
4438
*/
4439

4440
INSERT INTO graph_prune_log (
4441
    block_height, block_hash
4442
) VALUES (
4443
    $1, $2
4444
)
4445
ON CONFLICT(block_height) DO UPDATE SET
4446
    block_hash = EXCLUDED.block_hash
4447
`
4448

4449
type UpsertPruneLogEntryParams struct {
4450
        BlockHeight int64
4451
        BlockHash   []byte
4452
}
4453

4454
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
4455
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
4456
        return err
×
4457
}
×
4458

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

4481
type UpsertSourceNodeParams struct {
4482
        Version     int16
4483
        PubKey      []byte
4484
        Alias       sql.NullString
4485
        LastUpdate  sql.NullInt64
4486
        BlockHeight sql.NullInt64
4487
        Color       sql.NullString
4488
        Signature   []byte
4489
}
4490

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

4509
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
4510
/* ─────────────────────────────────────────────
4511
   graph_zombie_channels table queries
4512
   ─────────────────────────────────────────────
4513
*/
4514

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

4525
type UpsertZombieChannelParams struct {
4526
        Scid     []byte
4527
        Version  int16
4528
        NodeKey1 []byte
4529
        NodeKey2 []byte
4530
}
4531

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