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

lightningnetwork / lnd / 19316446305

13 Nov 2025 12:34AM UTC coverage: 65.219% (+8.3%) from 56.89%
19316446305

push

github

web-flow
Merge pull request #10343 from lightningnetwork/0-21-0-staging

Merge branch `0-21-staging`

361 of 5339 new or added lines in 47 files covered. (6.76%)

34 existing lines in 8 files now uncovered.

137571 of 210938 relevant lines covered (65.22%)

20832.75 hits per line

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

0.0
/graph/db/migration1/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

NEW
25
func (q *Queries) AddSourceNode(ctx context.Context, nodeID int64) error {
×
NEW
26
        _, err := q.db.ExecContext(ctx, addSourceNode, nodeID)
×
NEW
27
        return err
×
NEW
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

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

58
const countZombieChannels = `-- name: CountZombieChannels :one
59
SELECT COUNT(*)
60
FROM graph_zombie_channels
61
WHERE version = $1
62
`
63

NEW
64
func (q *Queries) CountZombieChannels(ctx context.Context, version int16) (int64, error) {
×
NEW
65
        row := q.db.QueryRowContext(ctx, countZombieChannels, version)
×
NEW
66
        var count int64
×
NEW
67
        err := row.Scan(&count)
×
NEW
68
        return count, err
×
NEW
69
}
×
70

71
const createChannel = `-- name: CreateChannel :one
72
/* ─────────────────────────────────────────────
73
   graph_channels table queries
74
   ─────────────────────────────────────────────
75
*/
76

77
INSERT INTO graph_channels (
78
    version, scid, node_id_1, node_id_2,
79
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
80
    node_1_signature, node_2_signature, bitcoin_1_signature,
81
    bitcoin_2_signature
82
) VALUES (
83
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
84
)
85
RETURNING id
86
`
87

88
type CreateChannelParams struct {
89
        Version           int16
90
        Scid              []byte
91
        NodeID1           int64
92
        NodeID2           int64
93
        Outpoint          string
94
        Capacity          sql.NullInt64
95
        BitcoinKey1       []byte
96
        BitcoinKey2       []byte
97
        Node1Signature    []byte
98
        Node2Signature    []byte
99
        Bitcoin1Signature []byte
100
        Bitcoin2Signature []byte
101
}
102

NEW
103
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (int64, error) {
×
NEW
104
        row := q.db.QueryRowContext(ctx, createChannel,
×
NEW
105
                arg.Version,
×
NEW
106
                arg.Scid,
×
NEW
107
                arg.NodeID1,
×
NEW
108
                arg.NodeID2,
×
NEW
109
                arg.Outpoint,
×
NEW
110
                arg.Capacity,
×
NEW
111
                arg.BitcoinKey1,
×
NEW
112
                arg.BitcoinKey2,
×
NEW
113
                arg.Node1Signature,
×
NEW
114
                arg.Node2Signature,
×
NEW
115
                arg.Bitcoin1Signature,
×
NEW
116
                arg.Bitcoin2Signature,
×
NEW
117
        )
×
NEW
118
        var id int64
×
NEW
119
        err := row.Scan(&id)
×
NEW
120
        return id, err
×
NEW
121
}
×
122

123
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
124
DELETE FROM graph_channel_policy_extra_types
125
WHERE channel_policy_id = $1
126
`
127

NEW
128
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
NEW
129
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
NEW
130
        return err
×
NEW
131
}
×
132

133
const deleteChannels = `-- name: DeleteChannels :exec
134
DELETE FROM graph_channels
135
WHERE id IN (/*SLICE:ids*/?)
136
`
137

NEW
138
func (q *Queries) DeleteChannels(ctx context.Context, ids []int64) error {
×
NEW
139
        query := deleteChannels
×
NEW
140
        var queryParams []interface{}
×
NEW
141
        if len(ids) > 0 {
×
NEW
142
                for _, v := range ids {
×
NEW
143
                        queryParams = append(queryParams, v)
×
NEW
144
                }
×
NEW
145
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
146
        } else {
×
NEW
147
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
148
        }
×
NEW
149
        _, err := q.db.ExecContext(ctx, query, queryParams...)
×
NEW
150
        return err
×
151
}
152

153
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
154
DELETE FROM graph_node_extra_types
155
WHERE node_id = $1
156
  AND type = $2
157
`
158

159
type DeleteExtraNodeTypeParams struct {
160
        NodeID int64
161
        Type   int64
162
}
163

NEW
164
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
NEW
165
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
NEW
166
        return err
×
NEW
167
}
×
168

169
const deleteNode = `-- name: DeleteNode :exec
170
DELETE FROM graph_nodes
171
WHERE id = $1
172
`
173

NEW
174
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
NEW
175
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
NEW
176
        return err
×
NEW
177
}
×
178

179
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
180
DELETE FROM graph_node_addresses
181
WHERE node_id = $1
182
`
183

NEW
184
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
NEW
185
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
NEW
186
        return err
×
NEW
187
}
×
188

189
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
190
DELETE FROM graph_nodes
191
WHERE pub_key = $1
192
  AND version = $2
193
`
194

195
type DeleteNodeByPubKeyParams struct {
196
        PubKey  []byte
197
        Version int16
198
}
199

NEW
200
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
NEW
201
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
NEW
202
}
×
203

204
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
205
DELETE FROM graph_node_features
206
WHERE node_id = $1
207
  AND feature_bit = $2
208
`
209

210
type DeleteNodeFeatureParams struct {
211
        NodeID     int64
212
        FeatureBit int32
213
}
214

NEW
215
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
NEW
216
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
NEW
217
        return err
×
NEW
218
}
×
219

220
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
221
DELETE FROM graph_prune_log
222
WHERE block_height >= $1
223
  AND block_height <= $2
224
`
225

226
type DeletePruneLogEntriesInRangeParams struct {
227
        StartHeight int64
228
        EndHeight   int64
229
}
230

NEW
231
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
NEW
232
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
NEW
233
        return err
×
NEW
234
}
×
235

236
const deleteUnconnectedNodes = `-- name: DeleteUnconnectedNodes :many
237
DELETE FROM graph_nodes
238
WHERE
239
    -- Ignore any of our source nodes.
240
    NOT EXISTS (
241
        SELECT 1
242
        FROM graph_source_nodes sn
243
        WHERE sn.node_id = graph_nodes.id
244
    )
245
    -- Select all nodes that do not have any channels.
246
    AND NOT EXISTS (
247
        SELECT 1
248
        FROM graph_channels c
249
        WHERE c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id
250
) RETURNING pub_key
251
`
252

NEW
253
func (q *Queries) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) {
×
NEW
254
        rows, err := q.db.QueryContext(ctx, deleteUnconnectedNodes)
×
NEW
255
        if err != nil {
×
NEW
256
                return nil, err
×
NEW
257
        }
×
NEW
258
        defer rows.Close()
×
NEW
259
        var items [][]byte
×
NEW
260
        for rows.Next() {
×
NEW
261
                var pub_key []byte
×
NEW
262
                if err := rows.Scan(&pub_key); err != nil {
×
NEW
263
                        return nil, err
×
NEW
264
                }
×
NEW
265
                items = append(items, pub_key)
×
266
        }
NEW
267
        if err := rows.Close(); err != nil {
×
NEW
268
                return nil, err
×
NEW
269
        }
×
NEW
270
        if err := rows.Err(); err != nil {
×
NEW
271
                return nil, err
×
NEW
272
        }
×
NEW
273
        return items, nil
×
274
}
275

276
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
277
DELETE FROM graph_zombie_channels
278
WHERE scid = $1
279
AND version = $2
280
`
281

282
type DeleteZombieChannelParams struct {
283
        Scid    []byte
284
        Version int16
285
}
286

NEW
287
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
NEW
288
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
NEW
289
}
×
290

291
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
292
SELECT
293
    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,
294
    n1.pub_key AS node1_pub_key,
295
    n2.pub_key AS node2_pub_key
296
FROM graph_channels c
297
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
298
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
299
WHERE c.scid = $1
300
  AND c.version = $2
301
`
302

303
type GetChannelAndNodesBySCIDParams struct {
304
        Scid    []byte
305
        Version int16
306
}
307

308
type GetChannelAndNodesBySCIDRow struct {
309
        ID                int64
310
        Version           int16
311
        Scid              []byte
312
        NodeID1           int64
313
        NodeID2           int64
314
        Outpoint          string
315
        Capacity          sql.NullInt64
316
        BitcoinKey1       []byte
317
        BitcoinKey2       []byte
318
        Node1Signature    []byte
319
        Node2Signature    []byte
320
        Bitcoin1Signature []byte
321
        Bitcoin2Signature []byte
322
        Node1PubKey       []byte
323
        Node2PubKey       []byte
324
}
325

NEW
326
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
NEW
327
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
NEW
328
        var i GetChannelAndNodesBySCIDRow
×
NEW
329
        err := row.Scan(
×
NEW
330
                &i.ID,
×
NEW
331
                &i.Version,
×
NEW
332
                &i.Scid,
×
NEW
333
                &i.NodeID1,
×
NEW
334
                &i.NodeID2,
×
NEW
335
                &i.Outpoint,
×
NEW
336
                &i.Capacity,
×
NEW
337
                &i.BitcoinKey1,
×
NEW
338
                &i.BitcoinKey2,
×
NEW
339
                &i.Node1Signature,
×
NEW
340
                &i.Node2Signature,
×
NEW
341
                &i.Bitcoin1Signature,
×
NEW
342
                &i.Bitcoin2Signature,
×
NEW
343
                &i.Node1PubKey,
×
NEW
344
                &i.Node2PubKey,
×
NEW
345
        )
×
NEW
346
        return i, err
×
NEW
347
}
×
348

349
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
350
SELECT
351
    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,
352

353
    n1.pub_key AS node1_pubkey,
354
    n2.pub_key AS node2_pubkey,
355

356
    -- Node 1 policy
357
    cp1.id AS policy_1_id,
358
    cp1.node_id AS policy_1_node_id,
359
    cp1.version AS policy_1_version,
360
    cp1.timelock AS policy_1_timelock,
361
    cp1.fee_ppm AS policy_1_fee_ppm,
362
    cp1.base_fee_msat AS policy_1_base_fee_msat,
363
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
364
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
365
    cp1.last_update AS policy_1_last_update,
366
    cp1.disabled AS policy_1_disabled,
367
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
368
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
369
    cp1.message_flags AS policy_1_message_flags,
370
    cp1.channel_flags AS policy_1_channel_flags,
371
    cp1.signature AS policy_1_signature,
372

373
    -- Node 2 policy
374
    cp2.id AS policy_2_id,
375
    cp2.node_id AS policy_2_node_id,
376
    cp2.version AS policy_2_version,
377
    cp2.timelock AS policy_2_timelock,
378
    cp2.fee_ppm AS policy_2_fee_ppm,
379
    cp2.base_fee_msat AS policy_2_base_fee_msat,
380
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
381
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
382
    cp2.last_update AS policy_2_last_update,
383
    cp2.disabled AS policy_2_disabled,
384
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
385
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
386
    cp2.message_flags AS policy_2_message_flags,
387
    cp2.channel_flags AS policy_2_channel_flags,
388
    cp2.signature AS policy_2_signature
389
FROM graph_channels c
390
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
391
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
392
    LEFT JOIN graph_channel_policies cp1
393
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
394
    LEFT JOIN graph_channel_policies cp2
395
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
396
WHERE c.outpoint = $1 AND c.version = $2
397
`
398

399
type GetChannelByOutpointWithPoliciesParams struct {
400
        Outpoint string
401
        Version  int16
402
}
403

404
type GetChannelByOutpointWithPoliciesRow struct {
405
        GraphChannel                   GraphChannel
406
        Node1Pubkey                    []byte
407
        Node2Pubkey                    []byte
408
        Policy1ID                      sql.NullInt64
409
        Policy1NodeID                  sql.NullInt64
410
        Policy1Version                 sql.NullInt16
411
        Policy1Timelock                sql.NullInt32
412
        Policy1FeePpm                  sql.NullInt64
413
        Policy1BaseFeeMsat             sql.NullInt64
414
        Policy1MinHtlcMsat             sql.NullInt64
415
        Policy1MaxHtlcMsat             sql.NullInt64
416
        Policy1LastUpdate              sql.NullInt64
417
        Policy1Disabled                sql.NullBool
418
        Policy1InboundBaseFeeMsat      sql.NullInt64
419
        Policy1InboundFeeRateMilliMsat sql.NullInt64
420
        Policy1MessageFlags            sql.NullInt16
421
        Policy1ChannelFlags            sql.NullInt16
422
        Policy1Signature               []byte
423
        Policy2ID                      sql.NullInt64
424
        Policy2NodeID                  sql.NullInt64
425
        Policy2Version                 sql.NullInt16
426
        Policy2Timelock                sql.NullInt32
427
        Policy2FeePpm                  sql.NullInt64
428
        Policy2BaseFeeMsat             sql.NullInt64
429
        Policy2MinHtlcMsat             sql.NullInt64
430
        Policy2MaxHtlcMsat             sql.NullInt64
431
        Policy2LastUpdate              sql.NullInt64
432
        Policy2Disabled                sql.NullBool
433
        Policy2InboundBaseFeeMsat      sql.NullInt64
434
        Policy2InboundFeeRateMilliMsat sql.NullInt64
435
        Policy2MessageFlags            sql.NullInt16
436
        Policy2ChannelFlags            sql.NullInt16
437
        Policy2Signature               []byte
438
}
439

NEW
440
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
NEW
441
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
NEW
442
        var i GetChannelByOutpointWithPoliciesRow
×
NEW
443
        err := row.Scan(
×
NEW
444
                &i.GraphChannel.ID,
×
NEW
445
                &i.GraphChannel.Version,
×
NEW
446
                &i.GraphChannel.Scid,
×
NEW
447
                &i.GraphChannel.NodeID1,
×
NEW
448
                &i.GraphChannel.NodeID2,
×
NEW
449
                &i.GraphChannel.Outpoint,
×
NEW
450
                &i.GraphChannel.Capacity,
×
NEW
451
                &i.GraphChannel.BitcoinKey1,
×
NEW
452
                &i.GraphChannel.BitcoinKey2,
×
NEW
453
                &i.GraphChannel.Node1Signature,
×
NEW
454
                &i.GraphChannel.Node2Signature,
×
NEW
455
                &i.GraphChannel.Bitcoin1Signature,
×
NEW
456
                &i.GraphChannel.Bitcoin2Signature,
×
NEW
457
                &i.Node1Pubkey,
×
NEW
458
                &i.Node2Pubkey,
×
NEW
459
                &i.Policy1ID,
×
NEW
460
                &i.Policy1NodeID,
×
NEW
461
                &i.Policy1Version,
×
NEW
462
                &i.Policy1Timelock,
×
NEW
463
                &i.Policy1FeePpm,
×
NEW
464
                &i.Policy1BaseFeeMsat,
×
NEW
465
                &i.Policy1MinHtlcMsat,
×
NEW
466
                &i.Policy1MaxHtlcMsat,
×
NEW
467
                &i.Policy1LastUpdate,
×
NEW
468
                &i.Policy1Disabled,
×
NEW
469
                &i.Policy1InboundBaseFeeMsat,
×
NEW
470
                &i.Policy1InboundFeeRateMilliMsat,
×
NEW
471
                &i.Policy1MessageFlags,
×
NEW
472
                &i.Policy1ChannelFlags,
×
NEW
473
                &i.Policy1Signature,
×
NEW
474
                &i.Policy2ID,
×
NEW
475
                &i.Policy2NodeID,
×
NEW
476
                &i.Policy2Version,
×
NEW
477
                &i.Policy2Timelock,
×
NEW
478
                &i.Policy2FeePpm,
×
NEW
479
                &i.Policy2BaseFeeMsat,
×
NEW
480
                &i.Policy2MinHtlcMsat,
×
NEW
481
                &i.Policy2MaxHtlcMsat,
×
NEW
482
                &i.Policy2LastUpdate,
×
NEW
483
                &i.Policy2Disabled,
×
NEW
484
                &i.Policy2InboundBaseFeeMsat,
×
NEW
485
                &i.Policy2InboundFeeRateMilliMsat,
×
NEW
486
                &i.Policy2MessageFlags,
×
NEW
487
                &i.Policy2ChannelFlags,
×
NEW
488
                &i.Policy2Signature,
×
NEW
489
        )
×
NEW
490
        return i, err
×
NEW
491
}
×
492

493
const getChannelBySCID = `-- name: GetChannelBySCID :one
494
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 FROM graph_channels
495
WHERE scid = $1 AND version = $2
496
`
497

498
type GetChannelBySCIDParams struct {
499
        Scid    []byte
500
        Version int16
501
}
502

NEW
503
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error) {
×
NEW
504
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
NEW
505
        var i GraphChannel
×
NEW
506
        err := row.Scan(
×
NEW
507
                &i.ID,
×
NEW
508
                &i.Version,
×
NEW
509
                &i.Scid,
×
NEW
510
                &i.NodeID1,
×
NEW
511
                &i.NodeID2,
×
NEW
512
                &i.Outpoint,
×
NEW
513
                &i.Capacity,
×
NEW
514
                &i.BitcoinKey1,
×
NEW
515
                &i.BitcoinKey2,
×
NEW
516
                &i.Node1Signature,
×
NEW
517
                &i.Node2Signature,
×
NEW
518
                &i.Bitcoin1Signature,
×
NEW
519
                &i.Bitcoin2Signature,
×
NEW
520
        )
×
NEW
521
        return i, err
×
NEW
522
}
×
523

524
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
525
SELECT
526
    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,
527
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
528
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
529

530
    -- Policy 1
531
    cp1.id AS policy1_id,
532
    cp1.node_id AS policy1_node_id,
533
    cp1.version AS policy1_version,
534
    cp1.timelock AS policy1_timelock,
535
    cp1.fee_ppm AS policy1_fee_ppm,
536
    cp1.base_fee_msat AS policy1_base_fee_msat,
537
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
538
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
539
    cp1.last_update AS policy1_last_update,
540
    cp1.disabled AS policy1_disabled,
541
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
542
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
543
    cp1.message_flags AS policy1_message_flags,
544
    cp1.channel_flags AS policy1_channel_flags,
545
    cp1.signature AS policy1_signature,
546

547
    -- Policy 2
548
    cp2.id AS policy2_id,
549
    cp2.node_id AS policy2_node_id,
550
    cp2.version AS policy2_version,
551
    cp2.timelock AS policy2_timelock,
552
    cp2.fee_ppm AS policy2_fee_ppm,
553
    cp2.base_fee_msat AS policy2_base_fee_msat,
554
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
555
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
556
    cp2.last_update AS policy2_last_update,
557
    cp2.disabled AS policy2_disabled,
558
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
559
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
560
    cp2.message_flags AS policy_2_message_flags,
561
    cp2.channel_flags AS policy_2_channel_flags,
562
    cp2.signature AS policy2_signature
563

564
FROM graph_channels c
565
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
566
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
567
    LEFT JOIN graph_channel_policies cp1
568
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
569
    LEFT JOIN graph_channel_policies cp2
570
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
571
WHERE c.scid = $1
572
  AND c.version = $2
573
`
574

575
type GetChannelBySCIDWithPoliciesParams struct {
576
        Scid    []byte
577
        Version int16
578
}
579

580
type GetChannelBySCIDWithPoliciesRow struct {
581
        GraphChannel                   GraphChannel
582
        GraphNode                      GraphNode
583
        GraphNode_2                    GraphNode
584
        Policy1ID                      sql.NullInt64
585
        Policy1NodeID                  sql.NullInt64
586
        Policy1Version                 sql.NullInt16
587
        Policy1Timelock                sql.NullInt32
588
        Policy1FeePpm                  sql.NullInt64
589
        Policy1BaseFeeMsat             sql.NullInt64
590
        Policy1MinHtlcMsat             sql.NullInt64
591
        Policy1MaxHtlcMsat             sql.NullInt64
592
        Policy1LastUpdate              sql.NullInt64
593
        Policy1Disabled                sql.NullBool
594
        Policy1InboundBaseFeeMsat      sql.NullInt64
595
        Policy1InboundFeeRateMilliMsat sql.NullInt64
596
        Policy1MessageFlags            sql.NullInt16
597
        Policy1ChannelFlags            sql.NullInt16
598
        Policy1Signature               []byte
599
        Policy2ID                      sql.NullInt64
600
        Policy2NodeID                  sql.NullInt64
601
        Policy2Version                 sql.NullInt16
602
        Policy2Timelock                sql.NullInt32
603
        Policy2FeePpm                  sql.NullInt64
604
        Policy2BaseFeeMsat             sql.NullInt64
605
        Policy2MinHtlcMsat             sql.NullInt64
606
        Policy2MaxHtlcMsat             sql.NullInt64
607
        Policy2LastUpdate              sql.NullInt64
608
        Policy2Disabled                sql.NullBool
609
        Policy2InboundBaseFeeMsat      sql.NullInt64
610
        Policy2InboundFeeRateMilliMsat sql.NullInt64
611
        Policy2MessageFlags            sql.NullInt16
612
        Policy2ChannelFlags            sql.NullInt16
613
        Policy2Signature               []byte
614
}
615

NEW
616
func (q *Queries) GetChannelBySCIDWithPolicies(ctx context.Context, arg GetChannelBySCIDWithPoliciesParams) (GetChannelBySCIDWithPoliciesRow, error) {
×
NEW
617
        row := q.db.QueryRowContext(ctx, getChannelBySCIDWithPolicies, arg.Scid, arg.Version)
×
NEW
618
        var i GetChannelBySCIDWithPoliciesRow
×
NEW
619
        err := row.Scan(
×
NEW
620
                &i.GraphChannel.ID,
×
NEW
621
                &i.GraphChannel.Version,
×
NEW
622
                &i.GraphChannel.Scid,
×
NEW
623
                &i.GraphChannel.NodeID1,
×
NEW
624
                &i.GraphChannel.NodeID2,
×
NEW
625
                &i.GraphChannel.Outpoint,
×
NEW
626
                &i.GraphChannel.Capacity,
×
NEW
627
                &i.GraphChannel.BitcoinKey1,
×
NEW
628
                &i.GraphChannel.BitcoinKey2,
×
NEW
629
                &i.GraphChannel.Node1Signature,
×
NEW
630
                &i.GraphChannel.Node2Signature,
×
NEW
631
                &i.GraphChannel.Bitcoin1Signature,
×
NEW
632
                &i.GraphChannel.Bitcoin2Signature,
×
NEW
633
                &i.GraphNode.ID,
×
NEW
634
                &i.GraphNode.Version,
×
NEW
635
                &i.GraphNode.PubKey,
×
NEW
636
                &i.GraphNode.Alias,
×
NEW
637
                &i.GraphNode.LastUpdate,
×
NEW
638
                &i.GraphNode.Color,
×
NEW
639
                &i.GraphNode.Signature,
×
NEW
640
                &i.GraphNode_2.ID,
×
NEW
641
                &i.GraphNode_2.Version,
×
NEW
642
                &i.GraphNode_2.PubKey,
×
NEW
643
                &i.GraphNode_2.Alias,
×
NEW
644
                &i.GraphNode_2.LastUpdate,
×
NEW
645
                &i.GraphNode_2.Color,
×
NEW
646
                &i.GraphNode_2.Signature,
×
NEW
647
                &i.Policy1ID,
×
NEW
648
                &i.Policy1NodeID,
×
NEW
649
                &i.Policy1Version,
×
NEW
650
                &i.Policy1Timelock,
×
NEW
651
                &i.Policy1FeePpm,
×
NEW
652
                &i.Policy1BaseFeeMsat,
×
NEW
653
                &i.Policy1MinHtlcMsat,
×
NEW
654
                &i.Policy1MaxHtlcMsat,
×
NEW
655
                &i.Policy1LastUpdate,
×
NEW
656
                &i.Policy1Disabled,
×
NEW
657
                &i.Policy1InboundBaseFeeMsat,
×
NEW
658
                &i.Policy1InboundFeeRateMilliMsat,
×
NEW
659
                &i.Policy1MessageFlags,
×
NEW
660
                &i.Policy1ChannelFlags,
×
NEW
661
                &i.Policy1Signature,
×
NEW
662
                &i.Policy2ID,
×
NEW
663
                &i.Policy2NodeID,
×
NEW
664
                &i.Policy2Version,
×
NEW
665
                &i.Policy2Timelock,
×
NEW
666
                &i.Policy2FeePpm,
×
NEW
667
                &i.Policy2BaseFeeMsat,
×
NEW
668
                &i.Policy2MinHtlcMsat,
×
NEW
669
                &i.Policy2MaxHtlcMsat,
×
NEW
670
                &i.Policy2LastUpdate,
×
NEW
671
                &i.Policy2Disabled,
×
NEW
672
                &i.Policy2InboundBaseFeeMsat,
×
NEW
673
                &i.Policy2InboundFeeRateMilliMsat,
×
NEW
674
                &i.Policy2MessageFlags,
×
NEW
675
                &i.Policy2ChannelFlags,
×
NEW
676
                &i.Policy2Signature,
×
NEW
677
        )
×
NEW
678
        return i, err
×
NEW
679
}
×
680

681
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
682
SELECT
683
    channel_id,
684
    type,
685
    value
686
FROM graph_channel_extra_types
687
WHERE channel_id IN (/*SLICE:chan_ids*/?)
688
ORDER BY channel_id, type
689
`
690

NEW
691
func (q *Queries) GetChannelExtrasBatch(ctx context.Context, chanIds []int64) ([]GraphChannelExtraType, error) {
×
NEW
692
        query := getChannelExtrasBatch
×
NEW
693
        var queryParams []interface{}
×
NEW
694
        if len(chanIds) > 0 {
×
NEW
695
                for _, v := range chanIds {
×
NEW
696
                        queryParams = append(queryParams, v)
×
NEW
697
                }
×
NEW
698
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
NEW
699
        } else {
×
NEW
700
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
NEW
701
        }
×
NEW
702
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
703
        if err != nil {
×
NEW
704
                return nil, err
×
NEW
705
        }
×
NEW
706
        defer rows.Close()
×
NEW
707
        var items []GraphChannelExtraType
×
NEW
708
        for rows.Next() {
×
NEW
709
                var i GraphChannelExtraType
×
NEW
710
                if err := rows.Scan(&i.ChannelID, &i.Type, &i.Value); err != nil {
×
NEW
711
                        return nil, err
×
NEW
712
                }
×
NEW
713
                items = append(items, i)
×
714
        }
NEW
715
        if err := rows.Close(); err != nil {
×
NEW
716
                return nil, err
×
NEW
717
        }
×
NEW
718
        if err := rows.Err(); err != nil {
×
NEW
719
                return nil, err
×
NEW
720
        }
×
NEW
721
        return items, nil
×
722
}
723

724
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
725
SELECT
726
    channel_id,
727
    feature_bit
728
FROM graph_channel_features
729
WHERE channel_id IN (/*SLICE:chan_ids*/?)
730
ORDER BY channel_id, feature_bit
731
`
732

NEW
733
func (q *Queries) GetChannelFeaturesBatch(ctx context.Context, chanIds []int64) ([]GraphChannelFeature, error) {
×
NEW
734
        query := getChannelFeaturesBatch
×
NEW
735
        var queryParams []interface{}
×
NEW
736
        if len(chanIds) > 0 {
×
NEW
737
                for _, v := range chanIds {
×
NEW
738
                        queryParams = append(queryParams, v)
×
NEW
739
                }
×
NEW
740
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
NEW
741
        } else {
×
NEW
742
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
NEW
743
        }
×
NEW
744
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
745
        if err != nil {
×
NEW
746
                return nil, err
×
NEW
747
        }
×
NEW
748
        defer rows.Close()
×
NEW
749
        var items []GraphChannelFeature
×
NEW
750
        for rows.Next() {
×
NEW
751
                var i GraphChannelFeature
×
NEW
752
                if err := rows.Scan(&i.ChannelID, &i.FeatureBit); err != nil {
×
NEW
753
                        return nil, err
×
NEW
754
                }
×
NEW
755
                items = append(items, i)
×
756
        }
NEW
757
        if err := rows.Close(); err != nil {
×
NEW
758
                return nil, err
×
NEW
759
        }
×
NEW
760
        if err := rows.Err(); err != nil {
×
NEW
761
                return nil, err
×
NEW
762
        }
×
NEW
763
        return items, nil
×
764
}
765

766
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
767
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
768
FROM graph_channel_policies
769
WHERE channel_id = $1
770
  AND node_id = $2
771
  AND version = $3
772
`
773

774
type GetChannelPolicyByChannelAndNodeParams struct {
775
        ChannelID int64
776
        NodeID    int64
777
        Version   int16
778
}
779

NEW
780
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (GraphChannelPolicy, error) {
×
NEW
781
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
NEW
782
        var i GraphChannelPolicy
×
NEW
783
        err := row.Scan(
×
NEW
784
                &i.ID,
×
NEW
785
                &i.Version,
×
NEW
786
                &i.ChannelID,
×
NEW
787
                &i.NodeID,
×
NEW
788
                &i.Timelock,
×
NEW
789
                &i.FeePpm,
×
NEW
790
                &i.BaseFeeMsat,
×
NEW
791
                &i.MinHtlcMsat,
×
NEW
792
                &i.MaxHtlcMsat,
×
NEW
793
                &i.LastUpdate,
×
NEW
794
                &i.Disabled,
×
NEW
795
                &i.InboundBaseFeeMsat,
×
NEW
796
                &i.InboundFeeRateMilliMsat,
×
NEW
797
                &i.MessageFlags,
×
NEW
798
                &i.ChannelFlags,
×
NEW
799
                &i.Signature,
×
NEW
800
        )
×
NEW
801
        return i, err
×
NEW
802
}
×
803

804
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
805
SELECT
806
    channel_policy_id as policy_id,
807
    type,
808
    value
809
FROM graph_channel_policy_extra_types
810
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
811
ORDER BY channel_policy_id, type
812
`
813

814
type GetChannelPolicyExtraTypesBatchRow struct {
815
        PolicyID int64
816
        Type     int64
817
        Value    []byte
818
}
819

NEW
820
func (q *Queries) GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]GetChannelPolicyExtraTypesBatchRow, error) {
×
NEW
821
        query := getChannelPolicyExtraTypesBatch
×
NEW
822
        var queryParams []interface{}
×
NEW
823
        if len(policyIds) > 0 {
×
NEW
824
                for _, v := range policyIds {
×
NEW
825
                        queryParams = append(queryParams, v)
×
NEW
826
                }
×
NEW
827
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", makeQueryParams(len(queryParams), len(policyIds)), 1)
×
NEW
828
        } else {
×
NEW
829
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", "NULL", 1)
×
NEW
830
        }
×
NEW
831
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
832
        if err != nil {
×
NEW
833
                return nil, err
×
NEW
834
        }
×
NEW
835
        defer rows.Close()
×
NEW
836
        var items []GetChannelPolicyExtraTypesBatchRow
×
NEW
837
        for rows.Next() {
×
NEW
838
                var i GetChannelPolicyExtraTypesBatchRow
×
NEW
839
                if err := rows.Scan(&i.PolicyID, &i.Type, &i.Value); err != nil {
×
NEW
840
                        return nil, err
×
NEW
841
                }
×
NEW
842
                items = append(items, i)
×
843
        }
NEW
844
        if err := rows.Close(); err != nil {
×
NEW
845
                return nil, err
×
NEW
846
        }
×
NEW
847
        if err := rows.Err(); err != nil {
×
NEW
848
                return nil, err
×
NEW
849
        }
×
NEW
850
        return items, nil
×
851
}
852

853
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
854
SELECT
855
    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,
856

857
    -- Minimal node data.
858
    n1.id AS node1_id,
859
    n1.pub_key AS node1_pub_key,
860
    n2.id AS node2_id,
861
    n2.pub_key AS node2_pub_key,
862

863
    -- Policy 1
864
    cp1.id AS policy1_id,
865
    cp1.node_id AS policy1_node_id,
866
    cp1.version AS policy1_version,
867
    cp1.timelock AS policy1_timelock,
868
    cp1.fee_ppm AS policy1_fee_ppm,
869
    cp1.base_fee_msat AS policy1_base_fee_msat,
870
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
871
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
872
    cp1.last_update AS policy1_last_update,
873
    cp1.disabled AS policy1_disabled,
874
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
875
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
876
    cp1.message_flags AS policy1_message_flags,
877
    cp1.channel_flags AS policy1_channel_flags,
878
    cp1.signature AS policy1_signature,
879

880
    -- Policy 2
881
    cp2.id AS policy2_id,
882
    cp2.node_id AS policy2_node_id,
883
    cp2.version AS policy2_version,
884
    cp2.timelock AS policy2_timelock,
885
    cp2.fee_ppm AS policy2_fee_ppm,
886
    cp2.base_fee_msat AS policy2_base_fee_msat,
887
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
888
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
889
    cp2.last_update AS policy2_last_update,
890
    cp2.disabled AS policy2_disabled,
891
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
892
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
893
    cp2.message_flags AS policy2_message_flags,
894
    cp2.channel_flags AS policy2_channel_flags,
895
    cp2.signature AS policy2_signature
896

897
FROM graph_channels c
898
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
899
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
900
    LEFT JOIN graph_channel_policies cp1
901
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
902
    LEFT JOIN graph_channel_policies cp2
903
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
904
WHERE c.id IN (/*SLICE:ids*/?)
905
`
906

907
type GetChannelsByIDsRow struct {
908
        GraphChannel                   GraphChannel
909
        Node1ID                        int64
910
        Node1PubKey                    []byte
911
        Node2ID                        int64
912
        Node2PubKey                    []byte
913
        Policy1ID                      sql.NullInt64
914
        Policy1NodeID                  sql.NullInt64
915
        Policy1Version                 sql.NullInt16
916
        Policy1Timelock                sql.NullInt32
917
        Policy1FeePpm                  sql.NullInt64
918
        Policy1BaseFeeMsat             sql.NullInt64
919
        Policy1MinHtlcMsat             sql.NullInt64
920
        Policy1MaxHtlcMsat             sql.NullInt64
921
        Policy1LastUpdate              sql.NullInt64
922
        Policy1Disabled                sql.NullBool
923
        Policy1InboundBaseFeeMsat      sql.NullInt64
924
        Policy1InboundFeeRateMilliMsat sql.NullInt64
925
        Policy1MessageFlags            sql.NullInt16
926
        Policy1ChannelFlags            sql.NullInt16
927
        Policy1Signature               []byte
928
        Policy2ID                      sql.NullInt64
929
        Policy2NodeID                  sql.NullInt64
930
        Policy2Version                 sql.NullInt16
931
        Policy2Timelock                sql.NullInt32
932
        Policy2FeePpm                  sql.NullInt64
933
        Policy2BaseFeeMsat             sql.NullInt64
934
        Policy2MinHtlcMsat             sql.NullInt64
935
        Policy2MaxHtlcMsat             sql.NullInt64
936
        Policy2LastUpdate              sql.NullInt64
937
        Policy2Disabled                sql.NullBool
938
        Policy2InboundBaseFeeMsat      sql.NullInt64
939
        Policy2InboundFeeRateMilliMsat sql.NullInt64
940
        Policy2MessageFlags            sql.NullInt16
941
        Policy2ChannelFlags            sql.NullInt16
942
        Policy2Signature               []byte
943
}
944

NEW
945
func (q *Queries) GetChannelsByIDs(ctx context.Context, ids []int64) ([]GetChannelsByIDsRow, error) {
×
NEW
946
        query := getChannelsByIDs
×
NEW
947
        var queryParams []interface{}
×
NEW
948
        if len(ids) > 0 {
×
NEW
949
                for _, v := range ids {
×
NEW
950
                        queryParams = append(queryParams, v)
×
NEW
951
                }
×
NEW
952
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
953
        } else {
×
NEW
954
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
955
        }
×
NEW
956
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
957
        if err != nil {
×
NEW
958
                return nil, err
×
NEW
959
        }
×
NEW
960
        defer rows.Close()
×
NEW
961
        var items []GetChannelsByIDsRow
×
NEW
962
        for rows.Next() {
×
NEW
963
                var i GetChannelsByIDsRow
×
NEW
964
                if err := rows.Scan(
×
NEW
965
                        &i.GraphChannel.ID,
×
NEW
966
                        &i.GraphChannel.Version,
×
NEW
967
                        &i.GraphChannel.Scid,
×
NEW
968
                        &i.GraphChannel.NodeID1,
×
NEW
969
                        &i.GraphChannel.NodeID2,
×
NEW
970
                        &i.GraphChannel.Outpoint,
×
NEW
971
                        &i.GraphChannel.Capacity,
×
NEW
972
                        &i.GraphChannel.BitcoinKey1,
×
NEW
973
                        &i.GraphChannel.BitcoinKey2,
×
NEW
974
                        &i.GraphChannel.Node1Signature,
×
NEW
975
                        &i.GraphChannel.Node2Signature,
×
NEW
976
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
977
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
978
                        &i.Node1ID,
×
NEW
979
                        &i.Node1PubKey,
×
NEW
980
                        &i.Node2ID,
×
NEW
981
                        &i.Node2PubKey,
×
NEW
982
                        &i.Policy1ID,
×
NEW
983
                        &i.Policy1NodeID,
×
NEW
984
                        &i.Policy1Version,
×
NEW
985
                        &i.Policy1Timelock,
×
NEW
986
                        &i.Policy1FeePpm,
×
NEW
987
                        &i.Policy1BaseFeeMsat,
×
NEW
988
                        &i.Policy1MinHtlcMsat,
×
NEW
989
                        &i.Policy1MaxHtlcMsat,
×
NEW
990
                        &i.Policy1LastUpdate,
×
NEW
991
                        &i.Policy1Disabled,
×
NEW
992
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
993
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
994
                        &i.Policy1MessageFlags,
×
NEW
995
                        &i.Policy1ChannelFlags,
×
NEW
996
                        &i.Policy1Signature,
×
NEW
997
                        &i.Policy2ID,
×
NEW
998
                        &i.Policy2NodeID,
×
NEW
999
                        &i.Policy2Version,
×
NEW
1000
                        &i.Policy2Timelock,
×
NEW
1001
                        &i.Policy2FeePpm,
×
NEW
1002
                        &i.Policy2BaseFeeMsat,
×
NEW
1003
                        &i.Policy2MinHtlcMsat,
×
NEW
1004
                        &i.Policy2MaxHtlcMsat,
×
NEW
1005
                        &i.Policy2LastUpdate,
×
NEW
1006
                        &i.Policy2Disabled,
×
NEW
1007
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
1008
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
1009
                        &i.Policy2MessageFlags,
×
NEW
1010
                        &i.Policy2ChannelFlags,
×
NEW
1011
                        &i.Policy2Signature,
×
NEW
1012
                ); err != nil {
×
NEW
1013
                        return nil, err
×
NEW
1014
                }
×
NEW
1015
                items = append(items, i)
×
1016
        }
NEW
1017
        if err := rows.Close(); err != nil {
×
NEW
1018
                return nil, err
×
NEW
1019
        }
×
NEW
1020
        if err := rows.Err(); err != nil {
×
NEW
1021
                return nil, err
×
NEW
1022
        }
×
NEW
1023
        return items, nil
×
1024
}
1025

1026
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1027
SELECT
1028
    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,
1029
    n1.pub_key AS node1_pubkey,
1030
    n2.pub_key AS node2_pubkey
1031
FROM graph_channels c
1032
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1033
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1034
WHERE c.outpoint IN
1035
    (/*SLICE:outpoints*/?)
1036
`
1037

1038
type GetChannelsByOutpointsRow struct {
1039
        GraphChannel GraphChannel
1040
        Node1Pubkey  []byte
1041
        Node2Pubkey  []byte
1042
}
1043

NEW
1044
func (q *Queries) GetChannelsByOutpoints(ctx context.Context, outpoints []string) ([]GetChannelsByOutpointsRow, error) {
×
NEW
1045
        query := getChannelsByOutpoints
×
NEW
1046
        var queryParams []interface{}
×
NEW
1047
        if len(outpoints) > 0 {
×
NEW
1048
                for _, v := range outpoints {
×
NEW
1049
                        queryParams = append(queryParams, v)
×
NEW
1050
                }
×
NEW
1051
                query = strings.Replace(query, "/*SLICE:outpoints*/?", makeQueryParams(len(queryParams), len(outpoints)), 1)
×
NEW
1052
        } else {
×
NEW
1053
                query = strings.Replace(query, "/*SLICE:outpoints*/?", "NULL", 1)
×
NEW
1054
        }
×
NEW
1055
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1056
        if err != nil {
×
NEW
1057
                return nil, err
×
NEW
1058
        }
×
NEW
1059
        defer rows.Close()
×
NEW
1060
        var items []GetChannelsByOutpointsRow
×
NEW
1061
        for rows.Next() {
×
NEW
1062
                var i GetChannelsByOutpointsRow
×
NEW
1063
                if err := rows.Scan(
×
NEW
1064
                        &i.GraphChannel.ID,
×
NEW
1065
                        &i.GraphChannel.Version,
×
NEW
1066
                        &i.GraphChannel.Scid,
×
NEW
1067
                        &i.GraphChannel.NodeID1,
×
NEW
1068
                        &i.GraphChannel.NodeID2,
×
NEW
1069
                        &i.GraphChannel.Outpoint,
×
NEW
1070
                        &i.GraphChannel.Capacity,
×
NEW
1071
                        &i.GraphChannel.BitcoinKey1,
×
NEW
1072
                        &i.GraphChannel.BitcoinKey2,
×
NEW
1073
                        &i.GraphChannel.Node1Signature,
×
NEW
1074
                        &i.GraphChannel.Node2Signature,
×
NEW
1075
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
1076
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1077
                        &i.Node1Pubkey,
×
NEW
1078
                        &i.Node2Pubkey,
×
NEW
1079
                ); err != nil {
×
NEW
1080
                        return nil, err
×
NEW
1081
                }
×
NEW
1082
                items = append(items, i)
×
1083
        }
NEW
1084
        if err := rows.Close(); err != nil {
×
NEW
1085
                return nil, err
×
NEW
1086
        }
×
NEW
1087
        if err := rows.Err(); err != nil {
×
NEW
1088
                return nil, err
×
NEW
1089
        }
×
NEW
1090
        return items, nil
×
1091
}
1092

1093
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1094
SELECT
1095
    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,
1096
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
1097
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
1098

1099
    -- Policy 1 (node_id_1)
1100
    cp1.id AS policy1_id,
1101
    cp1.node_id AS policy1_node_id,
1102
    cp1.version AS policy1_version,
1103
    cp1.timelock AS policy1_timelock,
1104
    cp1.fee_ppm AS policy1_fee_ppm,
1105
    cp1.base_fee_msat AS policy1_base_fee_msat,
1106
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1107
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1108
    cp1.last_update AS policy1_last_update,
1109
    cp1.disabled AS policy1_disabled,
1110
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1111
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1112
    cp1.message_flags AS policy1_message_flags,
1113
    cp1.channel_flags AS policy1_channel_flags,
1114
    cp1.signature AS policy1_signature,
1115

1116
    -- Policy 2 (node_id_2)
1117
    cp2.id AS policy2_id,
1118
    cp2.node_id AS policy2_node_id,
1119
    cp2.version AS policy2_version,
1120
    cp2.timelock AS policy2_timelock,
1121
    cp2.fee_ppm AS policy2_fee_ppm,
1122
    cp2.base_fee_msat AS policy2_base_fee_msat,
1123
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1124
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1125
    cp2.last_update AS policy2_last_update,
1126
    cp2.disabled AS policy2_disabled,
1127
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1128
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1129
    cp2.message_flags AS policy2_message_flags,
1130
    cp2.channel_flags AS policy2_channel_flags,
1131
    cp2.signature AS policy2_signature
1132

1133
FROM graph_channels c
1134
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1135
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1136
    LEFT JOIN graph_channel_policies cp1
1137
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1138
    LEFT JOIN graph_channel_policies cp2
1139
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1140
WHERE c.version = $1
1141
  AND (
1142
       (cp1.last_update >= $2 AND cp1.last_update < $3)
1143
       OR
1144
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1145
  )
1146
  -- Pagination using compound cursor (max_update_time, id).
1147
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1148
  AND (
1149
       (CASE
1150
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1151
               THEN COALESCE(cp1.last_update, 0)
1152
           ELSE COALESCE(cp2.last_update, 0)
1153
       END > COALESCE($4, -1))
1154
       OR 
1155
       (CASE
1156
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1157
               THEN COALESCE(cp1.last_update, 0)
1158
           ELSE COALESCE(cp2.last_update, 0)
1159
       END = COALESCE($4, -1) 
1160
       AND c.id > COALESCE($5, -1))
1161
  )
1162
ORDER BY
1163
    CASE
1164
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1165
            THEN COALESCE(cp1.last_update, 0)
1166
        ELSE COALESCE(cp2.last_update, 0)
1167
    END ASC,
1168
    c.id ASC
1169
LIMIT COALESCE($6, 999999999)
1170
`
1171

1172
type GetChannelsByPolicyLastUpdateRangeParams struct {
1173
        Version        int16
1174
        StartTime      sql.NullInt64
1175
        EndTime        sql.NullInt64
1176
        LastUpdateTime sql.NullInt64
1177
        LastID         sql.NullInt64
1178
        MaxResults     interface{}
1179
}
1180

1181
type GetChannelsByPolicyLastUpdateRangeRow struct {
1182
        GraphChannel                   GraphChannel
1183
        GraphNode                      GraphNode
1184
        GraphNode_2                    GraphNode
1185
        Policy1ID                      sql.NullInt64
1186
        Policy1NodeID                  sql.NullInt64
1187
        Policy1Version                 sql.NullInt16
1188
        Policy1Timelock                sql.NullInt32
1189
        Policy1FeePpm                  sql.NullInt64
1190
        Policy1BaseFeeMsat             sql.NullInt64
1191
        Policy1MinHtlcMsat             sql.NullInt64
1192
        Policy1MaxHtlcMsat             sql.NullInt64
1193
        Policy1LastUpdate              sql.NullInt64
1194
        Policy1Disabled                sql.NullBool
1195
        Policy1InboundBaseFeeMsat      sql.NullInt64
1196
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1197
        Policy1MessageFlags            sql.NullInt16
1198
        Policy1ChannelFlags            sql.NullInt16
1199
        Policy1Signature               []byte
1200
        Policy2ID                      sql.NullInt64
1201
        Policy2NodeID                  sql.NullInt64
1202
        Policy2Version                 sql.NullInt16
1203
        Policy2Timelock                sql.NullInt32
1204
        Policy2FeePpm                  sql.NullInt64
1205
        Policy2BaseFeeMsat             sql.NullInt64
1206
        Policy2MinHtlcMsat             sql.NullInt64
1207
        Policy2MaxHtlcMsat             sql.NullInt64
1208
        Policy2LastUpdate              sql.NullInt64
1209
        Policy2Disabled                sql.NullBool
1210
        Policy2InboundBaseFeeMsat      sql.NullInt64
1211
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1212
        Policy2MessageFlags            sql.NullInt16
1213
        Policy2ChannelFlags            sql.NullInt16
1214
        Policy2Signature               []byte
1215
}
1216

NEW
1217
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
NEW
1218
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange,
×
NEW
1219
                arg.Version,
×
NEW
1220
                arg.StartTime,
×
NEW
1221
                arg.EndTime,
×
NEW
1222
                arg.LastUpdateTime,
×
NEW
1223
                arg.LastID,
×
NEW
1224
                arg.MaxResults,
×
NEW
1225
        )
×
NEW
1226
        if err != nil {
×
NEW
1227
                return nil, err
×
NEW
1228
        }
×
NEW
1229
        defer rows.Close()
×
NEW
1230
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
NEW
1231
        for rows.Next() {
×
NEW
1232
                var i GetChannelsByPolicyLastUpdateRangeRow
×
NEW
1233
                if err := rows.Scan(
×
NEW
1234
                        &i.GraphChannel.ID,
×
NEW
1235
                        &i.GraphChannel.Version,
×
NEW
1236
                        &i.GraphChannel.Scid,
×
NEW
1237
                        &i.GraphChannel.NodeID1,
×
NEW
1238
                        &i.GraphChannel.NodeID2,
×
NEW
1239
                        &i.GraphChannel.Outpoint,
×
NEW
1240
                        &i.GraphChannel.Capacity,
×
NEW
1241
                        &i.GraphChannel.BitcoinKey1,
×
NEW
1242
                        &i.GraphChannel.BitcoinKey2,
×
NEW
1243
                        &i.GraphChannel.Node1Signature,
×
NEW
1244
                        &i.GraphChannel.Node2Signature,
×
NEW
1245
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
1246
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1247
                        &i.GraphNode.ID,
×
NEW
1248
                        &i.GraphNode.Version,
×
NEW
1249
                        &i.GraphNode.PubKey,
×
NEW
1250
                        &i.GraphNode.Alias,
×
NEW
1251
                        &i.GraphNode.LastUpdate,
×
NEW
1252
                        &i.GraphNode.Color,
×
NEW
1253
                        &i.GraphNode.Signature,
×
NEW
1254
                        &i.GraphNode_2.ID,
×
NEW
1255
                        &i.GraphNode_2.Version,
×
NEW
1256
                        &i.GraphNode_2.PubKey,
×
NEW
1257
                        &i.GraphNode_2.Alias,
×
NEW
1258
                        &i.GraphNode_2.LastUpdate,
×
NEW
1259
                        &i.GraphNode_2.Color,
×
NEW
1260
                        &i.GraphNode_2.Signature,
×
NEW
1261
                        &i.Policy1ID,
×
NEW
1262
                        &i.Policy1NodeID,
×
NEW
1263
                        &i.Policy1Version,
×
NEW
1264
                        &i.Policy1Timelock,
×
NEW
1265
                        &i.Policy1FeePpm,
×
NEW
1266
                        &i.Policy1BaseFeeMsat,
×
NEW
1267
                        &i.Policy1MinHtlcMsat,
×
NEW
1268
                        &i.Policy1MaxHtlcMsat,
×
NEW
1269
                        &i.Policy1LastUpdate,
×
NEW
1270
                        &i.Policy1Disabled,
×
NEW
1271
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
1272
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
1273
                        &i.Policy1MessageFlags,
×
NEW
1274
                        &i.Policy1ChannelFlags,
×
NEW
1275
                        &i.Policy1Signature,
×
NEW
1276
                        &i.Policy2ID,
×
NEW
1277
                        &i.Policy2NodeID,
×
NEW
1278
                        &i.Policy2Version,
×
NEW
1279
                        &i.Policy2Timelock,
×
NEW
1280
                        &i.Policy2FeePpm,
×
NEW
1281
                        &i.Policy2BaseFeeMsat,
×
NEW
1282
                        &i.Policy2MinHtlcMsat,
×
NEW
1283
                        &i.Policy2MaxHtlcMsat,
×
NEW
1284
                        &i.Policy2LastUpdate,
×
NEW
1285
                        &i.Policy2Disabled,
×
NEW
1286
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
1287
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
1288
                        &i.Policy2MessageFlags,
×
NEW
1289
                        &i.Policy2ChannelFlags,
×
NEW
1290
                        &i.Policy2Signature,
×
NEW
1291
                ); err != nil {
×
NEW
1292
                        return nil, err
×
NEW
1293
                }
×
NEW
1294
                items = append(items, i)
×
1295
        }
NEW
1296
        if err := rows.Close(); err != nil {
×
NEW
1297
                return nil, err
×
NEW
1298
        }
×
NEW
1299
        if err := rows.Err(); err != nil {
×
NEW
1300
                return nil, err
×
NEW
1301
        }
×
NEW
1302
        return items, nil
×
1303
}
1304

1305
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1306
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,
1307
    n1.pub_key AS node1_pub_key,
1308
    n2.pub_key AS node2_pub_key
1309
FROM graph_channels c
1310
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1311
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1312
WHERE scid >= $1
1313
  AND scid < $2
1314
`
1315

1316
type GetChannelsBySCIDRangeParams struct {
1317
        StartScid []byte
1318
        EndScid   []byte
1319
}
1320

1321
type GetChannelsBySCIDRangeRow struct {
1322
        GraphChannel GraphChannel
1323
        Node1PubKey  []byte
1324
        Node2PubKey  []byte
1325
}
1326

NEW
1327
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
NEW
1328
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
NEW
1329
        if err != nil {
×
NEW
1330
                return nil, err
×
NEW
1331
        }
×
NEW
1332
        defer rows.Close()
×
NEW
1333
        var items []GetChannelsBySCIDRangeRow
×
NEW
1334
        for rows.Next() {
×
NEW
1335
                var i GetChannelsBySCIDRangeRow
×
NEW
1336
                if err := rows.Scan(
×
NEW
1337
                        &i.GraphChannel.ID,
×
NEW
1338
                        &i.GraphChannel.Version,
×
NEW
1339
                        &i.GraphChannel.Scid,
×
NEW
1340
                        &i.GraphChannel.NodeID1,
×
NEW
1341
                        &i.GraphChannel.NodeID2,
×
NEW
1342
                        &i.GraphChannel.Outpoint,
×
NEW
1343
                        &i.GraphChannel.Capacity,
×
NEW
1344
                        &i.GraphChannel.BitcoinKey1,
×
NEW
1345
                        &i.GraphChannel.BitcoinKey2,
×
NEW
1346
                        &i.GraphChannel.Node1Signature,
×
NEW
1347
                        &i.GraphChannel.Node2Signature,
×
NEW
1348
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
1349
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1350
                        &i.Node1PubKey,
×
NEW
1351
                        &i.Node2PubKey,
×
NEW
1352
                ); err != nil {
×
NEW
1353
                        return nil, err
×
NEW
1354
                }
×
NEW
1355
                items = append(items, i)
×
1356
        }
NEW
1357
        if err := rows.Close(); err != nil {
×
NEW
1358
                return nil, err
×
NEW
1359
        }
×
NEW
1360
        if err := rows.Err(); err != nil {
×
NEW
1361
                return nil, err
×
NEW
1362
        }
×
NEW
1363
        return items, nil
×
1364
}
1365

1366
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1367
SELECT
1368
    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,
1369
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
1370
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
1371

1372
    -- Policy 1
1373
    cp1.id AS policy1_id,
1374
    cp1.node_id AS policy1_node_id,
1375
    cp1.version AS policy1_version,
1376
    cp1.timelock AS policy1_timelock,
1377
    cp1.fee_ppm AS policy1_fee_ppm,
1378
    cp1.base_fee_msat AS policy1_base_fee_msat,
1379
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1380
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1381
    cp1.last_update AS policy1_last_update,
1382
    cp1.disabled AS policy1_disabled,
1383
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1384
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1385
    cp1.message_flags AS policy1_message_flags,
1386
    cp1.channel_flags AS policy1_channel_flags,
1387
    cp1.signature AS policy1_signature,
1388

1389
    -- Policy 2
1390
    cp2.id AS policy2_id,
1391
    cp2.node_id AS policy2_node_id,
1392
    cp2.version AS policy2_version,
1393
    cp2.timelock AS policy2_timelock,
1394
    cp2.fee_ppm AS policy2_fee_ppm,
1395
    cp2.base_fee_msat AS policy2_base_fee_msat,
1396
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1397
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1398
    cp2.last_update AS policy2_last_update,
1399
    cp2.disabled AS policy2_disabled,
1400
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1401
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1402
    cp2.message_flags AS policy_2_message_flags,
1403
    cp2.channel_flags AS policy_2_channel_flags,
1404
    cp2.signature AS policy2_signature
1405

1406
FROM graph_channels c
1407
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1408
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1409
    LEFT JOIN graph_channel_policies cp1
1410
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1411
    LEFT JOIN graph_channel_policies cp2
1412
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1413
WHERE
1414
    c.version = $1
1415
  AND c.scid IN (/*SLICE:scids*/?)
1416
`
1417

1418
type GetChannelsBySCIDWithPoliciesParams struct {
1419
        Version int16
1420
        Scids   [][]byte
1421
}
1422

1423
type GetChannelsBySCIDWithPoliciesRow struct {
1424
        GraphChannel                   GraphChannel
1425
        GraphNode                      GraphNode
1426
        GraphNode_2                    GraphNode
1427
        Policy1ID                      sql.NullInt64
1428
        Policy1NodeID                  sql.NullInt64
1429
        Policy1Version                 sql.NullInt16
1430
        Policy1Timelock                sql.NullInt32
1431
        Policy1FeePpm                  sql.NullInt64
1432
        Policy1BaseFeeMsat             sql.NullInt64
1433
        Policy1MinHtlcMsat             sql.NullInt64
1434
        Policy1MaxHtlcMsat             sql.NullInt64
1435
        Policy1LastUpdate              sql.NullInt64
1436
        Policy1Disabled                sql.NullBool
1437
        Policy1InboundBaseFeeMsat      sql.NullInt64
1438
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1439
        Policy1MessageFlags            sql.NullInt16
1440
        Policy1ChannelFlags            sql.NullInt16
1441
        Policy1Signature               []byte
1442
        Policy2ID                      sql.NullInt64
1443
        Policy2NodeID                  sql.NullInt64
1444
        Policy2Version                 sql.NullInt16
1445
        Policy2Timelock                sql.NullInt32
1446
        Policy2FeePpm                  sql.NullInt64
1447
        Policy2BaseFeeMsat             sql.NullInt64
1448
        Policy2MinHtlcMsat             sql.NullInt64
1449
        Policy2MaxHtlcMsat             sql.NullInt64
1450
        Policy2LastUpdate              sql.NullInt64
1451
        Policy2Disabled                sql.NullBool
1452
        Policy2InboundBaseFeeMsat      sql.NullInt64
1453
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1454
        Policy2MessageFlags            sql.NullInt16
1455
        Policy2ChannelFlags            sql.NullInt16
1456
        Policy2Signature               []byte
1457
}
1458

NEW
1459
func (q *Queries) GetChannelsBySCIDWithPolicies(ctx context.Context, arg GetChannelsBySCIDWithPoliciesParams) ([]GetChannelsBySCIDWithPoliciesRow, error) {
×
NEW
1460
        query := getChannelsBySCIDWithPolicies
×
NEW
1461
        var queryParams []interface{}
×
NEW
1462
        queryParams = append(queryParams, arg.Version)
×
NEW
1463
        if len(arg.Scids) > 0 {
×
NEW
1464
                for _, v := range arg.Scids {
×
NEW
1465
                        queryParams = append(queryParams, v)
×
NEW
1466
                }
×
NEW
1467
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
NEW
1468
        } else {
×
NEW
1469
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
NEW
1470
        }
×
NEW
1471
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1472
        if err != nil {
×
NEW
1473
                return nil, err
×
NEW
1474
        }
×
NEW
1475
        defer rows.Close()
×
NEW
1476
        var items []GetChannelsBySCIDWithPoliciesRow
×
NEW
1477
        for rows.Next() {
×
NEW
1478
                var i GetChannelsBySCIDWithPoliciesRow
×
NEW
1479
                if err := rows.Scan(
×
NEW
1480
                        &i.GraphChannel.ID,
×
NEW
1481
                        &i.GraphChannel.Version,
×
NEW
1482
                        &i.GraphChannel.Scid,
×
NEW
1483
                        &i.GraphChannel.NodeID1,
×
NEW
1484
                        &i.GraphChannel.NodeID2,
×
NEW
1485
                        &i.GraphChannel.Outpoint,
×
NEW
1486
                        &i.GraphChannel.Capacity,
×
NEW
1487
                        &i.GraphChannel.BitcoinKey1,
×
NEW
1488
                        &i.GraphChannel.BitcoinKey2,
×
NEW
1489
                        &i.GraphChannel.Node1Signature,
×
NEW
1490
                        &i.GraphChannel.Node2Signature,
×
NEW
1491
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
1492
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1493
                        &i.GraphNode.ID,
×
NEW
1494
                        &i.GraphNode.Version,
×
NEW
1495
                        &i.GraphNode.PubKey,
×
NEW
1496
                        &i.GraphNode.Alias,
×
NEW
1497
                        &i.GraphNode.LastUpdate,
×
NEW
1498
                        &i.GraphNode.Color,
×
NEW
1499
                        &i.GraphNode.Signature,
×
NEW
1500
                        &i.GraphNode_2.ID,
×
NEW
1501
                        &i.GraphNode_2.Version,
×
NEW
1502
                        &i.GraphNode_2.PubKey,
×
NEW
1503
                        &i.GraphNode_2.Alias,
×
NEW
1504
                        &i.GraphNode_2.LastUpdate,
×
NEW
1505
                        &i.GraphNode_2.Color,
×
NEW
1506
                        &i.GraphNode_2.Signature,
×
NEW
1507
                        &i.Policy1ID,
×
NEW
1508
                        &i.Policy1NodeID,
×
NEW
1509
                        &i.Policy1Version,
×
NEW
1510
                        &i.Policy1Timelock,
×
NEW
1511
                        &i.Policy1FeePpm,
×
NEW
1512
                        &i.Policy1BaseFeeMsat,
×
NEW
1513
                        &i.Policy1MinHtlcMsat,
×
NEW
1514
                        &i.Policy1MaxHtlcMsat,
×
NEW
1515
                        &i.Policy1LastUpdate,
×
NEW
1516
                        &i.Policy1Disabled,
×
NEW
1517
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
1518
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
1519
                        &i.Policy1MessageFlags,
×
NEW
1520
                        &i.Policy1ChannelFlags,
×
NEW
1521
                        &i.Policy1Signature,
×
NEW
1522
                        &i.Policy2ID,
×
NEW
1523
                        &i.Policy2NodeID,
×
NEW
1524
                        &i.Policy2Version,
×
NEW
1525
                        &i.Policy2Timelock,
×
NEW
1526
                        &i.Policy2FeePpm,
×
NEW
1527
                        &i.Policy2BaseFeeMsat,
×
NEW
1528
                        &i.Policy2MinHtlcMsat,
×
NEW
1529
                        &i.Policy2MaxHtlcMsat,
×
NEW
1530
                        &i.Policy2LastUpdate,
×
NEW
1531
                        &i.Policy2Disabled,
×
NEW
1532
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
1533
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
1534
                        &i.Policy2MessageFlags,
×
NEW
1535
                        &i.Policy2ChannelFlags,
×
NEW
1536
                        &i.Policy2Signature,
×
NEW
1537
                ); err != nil {
×
NEW
1538
                        return nil, err
×
NEW
1539
                }
×
NEW
1540
                items = append(items, i)
×
1541
        }
NEW
1542
        if err := rows.Close(); err != nil {
×
NEW
1543
                return nil, err
×
NEW
1544
        }
×
NEW
1545
        if err := rows.Err(); err != nil {
×
NEW
1546
                return nil, err
×
NEW
1547
        }
×
NEW
1548
        return items, nil
×
1549
}
1550

1551
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1552
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 FROM graph_channels
1553
WHERE version = $1
1554
  AND scid IN (/*SLICE:scids*/?)
1555
`
1556

1557
type GetChannelsBySCIDsParams struct {
1558
        Version int16
1559
        Scids   [][]byte
1560
}
1561

NEW
1562
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
NEW
1563
        query := getChannelsBySCIDs
×
NEW
1564
        var queryParams []interface{}
×
NEW
1565
        queryParams = append(queryParams, arg.Version)
×
NEW
1566
        if len(arg.Scids) > 0 {
×
NEW
1567
                for _, v := range arg.Scids {
×
NEW
1568
                        queryParams = append(queryParams, v)
×
NEW
1569
                }
×
NEW
1570
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
NEW
1571
        } else {
×
NEW
1572
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
NEW
1573
        }
×
NEW
1574
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1575
        if err != nil {
×
NEW
1576
                return nil, err
×
NEW
1577
        }
×
NEW
1578
        defer rows.Close()
×
NEW
1579
        var items []GraphChannel
×
NEW
1580
        for rows.Next() {
×
NEW
1581
                var i GraphChannel
×
NEW
1582
                if err := rows.Scan(
×
NEW
1583
                        &i.ID,
×
NEW
1584
                        &i.Version,
×
NEW
1585
                        &i.Scid,
×
NEW
1586
                        &i.NodeID1,
×
NEW
1587
                        &i.NodeID2,
×
NEW
1588
                        &i.Outpoint,
×
NEW
1589
                        &i.Capacity,
×
NEW
1590
                        &i.BitcoinKey1,
×
NEW
1591
                        &i.BitcoinKey2,
×
NEW
1592
                        &i.Node1Signature,
×
NEW
1593
                        &i.Node2Signature,
×
NEW
1594
                        &i.Bitcoin1Signature,
×
NEW
1595
                        &i.Bitcoin2Signature,
×
NEW
1596
                ); err != nil {
×
NEW
1597
                        return nil, err
×
NEW
1598
                }
×
NEW
1599
                items = append(items, i)
×
1600
        }
NEW
1601
        if err := rows.Close(); err != nil {
×
NEW
1602
                return nil, err
×
NEW
1603
        }
×
NEW
1604
        if err := rows.Err(); err != nil {
×
NEW
1605
                return nil, err
×
NEW
1606
        }
×
NEW
1607
        return items, nil
×
1608
}
1609

1610
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1611
SELECT scid
1612
FROM graph_closed_scids
1613
WHERE scid IN (/*SLICE:scids*/?)
1614
`
1615

NEW
1616
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
NEW
1617
        query := getClosedChannelsSCIDs
×
NEW
1618
        var queryParams []interface{}
×
NEW
1619
        if len(scids) > 0 {
×
NEW
1620
                for _, v := range scids {
×
NEW
1621
                        queryParams = append(queryParams, v)
×
NEW
1622
                }
×
NEW
1623
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
NEW
1624
        } else {
×
NEW
1625
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
NEW
1626
        }
×
NEW
1627
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1628
        if err != nil {
×
NEW
1629
                return nil, err
×
NEW
1630
        }
×
NEW
1631
        defer rows.Close()
×
NEW
1632
        var items [][]byte
×
NEW
1633
        for rows.Next() {
×
NEW
1634
                var scid []byte
×
NEW
1635
                if err := rows.Scan(&scid); err != nil {
×
NEW
1636
                        return nil, err
×
NEW
1637
                }
×
NEW
1638
                items = append(items, scid)
×
1639
        }
NEW
1640
        if err := rows.Close(); err != nil {
×
NEW
1641
                return nil, err
×
NEW
1642
        }
×
NEW
1643
        if err := rows.Err(); err != nil {
×
NEW
1644
                return nil, err
×
NEW
1645
        }
×
NEW
1646
        return items, nil
×
1647
}
1648

1649
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1650
SELECT node_id, type, value
1651
FROM graph_node_extra_types
1652
WHERE node_id = $1
1653
`
1654

NEW
1655
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
NEW
1656
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
NEW
1657
        if err != nil {
×
NEW
1658
                return nil, err
×
NEW
1659
        }
×
NEW
1660
        defer rows.Close()
×
NEW
1661
        var items []GraphNodeExtraType
×
NEW
1662
        for rows.Next() {
×
NEW
1663
                var i GraphNodeExtraType
×
NEW
1664
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
NEW
1665
                        return nil, err
×
NEW
1666
                }
×
NEW
1667
                items = append(items, i)
×
1668
        }
NEW
1669
        if err := rows.Close(); err != nil {
×
NEW
1670
                return nil, err
×
NEW
1671
        }
×
NEW
1672
        if err := rows.Err(); err != nil {
×
NEW
1673
                return nil, err
×
NEW
1674
        }
×
NEW
1675
        return items, nil
×
1676
}
1677

1678
const getNodeAddresses = `-- name: GetNodeAddresses :many
1679
SELECT type, address
1680
FROM graph_node_addresses
1681
WHERE node_id = $1
1682
ORDER BY type ASC, position ASC
1683
`
1684

1685
type GetNodeAddressesRow struct {
1686
        Type    int16
1687
        Address string
1688
}
1689

NEW
1690
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
NEW
1691
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
NEW
1692
        if err != nil {
×
NEW
1693
                return nil, err
×
NEW
1694
        }
×
NEW
1695
        defer rows.Close()
×
NEW
1696
        var items []GetNodeAddressesRow
×
NEW
1697
        for rows.Next() {
×
NEW
1698
                var i GetNodeAddressesRow
×
NEW
1699
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
NEW
1700
                        return nil, err
×
NEW
1701
                }
×
NEW
1702
                items = append(items, i)
×
1703
        }
NEW
1704
        if err := rows.Close(); err != nil {
×
NEW
1705
                return nil, err
×
NEW
1706
        }
×
NEW
1707
        if err := rows.Err(); err != nil {
×
NEW
1708
                return nil, err
×
NEW
1709
        }
×
NEW
1710
        return items, nil
×
1711
}
1712

1713
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
1714
SELECT node_id, type, position, address
1715
FROM graph_node_addresses
1716
WHERE node_id IN (/*SLICE:ids*/?)
1717
ORDER BY node_id, type, position
1718
`
1719

NEW
1720
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
×
NEW
1721
        query := getNodeAddressesBatch
×
NEW
1722
        var queryParams []interface{}
×
NEW
1723
        if len(ids) > 0 {
×
NEW
1724
                for _, v := range ids {
×
NEW
1725
                        queryParams = append(queryParams, v)
×
NEW
1726
                }
×
NEW
1727
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
1728
        } else {
×
NEW
1729
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
1730
        }
×
NEW
1731
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1732
        if err != nil {
×
NEW
1733
                return nil, err
×
NEW
1734
        }
×
NEW
1735
        defer rows.Close()
×
NEW
1736
        var items []GraphNodeAddress
×
NEW
1737
        for rows.Next() {
×
NEW
1738
                var i GraphNodeAddress
×
NEW
1739
                if err := rows.Scan(
×
NEW
1740
                        &i.NodeID,
×
NEW
1741
                        &i.Type,
×
NEW
1742
                        &i.Position,
×
NEW
1743
                        &i.Address,
×
NEW
1744
                ); err != nil {
×
NEW
1745
                        return nil, err
×
NEW
1746
                }
×
NEW
1747
                items = append(items, i)
×
1748
        }
NEW
1749
        if err := rows.Close(); err != nil {
×
NEW
1750
                return nil, err
×
NEW
1751
        }
×
NEW
1752
        if err := rows.Err(); err != nil {
×
NEW
1753
                return nil, err
×
NEW
1754
        }
×
NEW
1755
        return items, nil
×
1756
}
1757

1758
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1759
SELECT id, version, pub_key, alias, last_update, color, signature
1760
FROM graph_nodes
1761
WHERE pub_key = $1
1762
  AND version = $2
1763
`
1764

1765
type GetNodeByPubKeyParams struct {
1766
        PubKey  []byte
1767
        Version int16
1768
}
1769

NEW
1770
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
NEW
1771
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
NEW
1772
        var i GraphNode
×
NEW
1773
        err := row.Scan(
×
NEW
1774
                &i.ID,
×
NEW
1775
                &i.Version,
×
NEW
1776
                &i.PubKey,
×
NEW
1777
                &i.Alias,
×
NEW
1778
                &i.LastUpdate,
×
NEW
1779
                &i.Color,
×
NEW
1780
                &i.Signature,
×
NEW
1781
        )
×
NEW
1782
        return i, err
×
NEW
1783
}
×
1784

1785
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
1786
SELECT node_id, type, value
1787
FROM graph_node_extra_types
1788
WHERE node_id IN (/*SLICE:ids*/?)
1789
ORDER BY node_id, type
1790
`
1791

NEW
1792
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
NEW
1793
        query := getNodeExtraTypesBatch
×
NEW
1794
        var queryParams []interface{}
×
NEW
1795
        if len(ids) > 0 {
×
NEW
1796
                for _, v := range ids {
×
NEW
1797
                        queryParams = append(queryParams, v)
×
NEW
1798
                }
×
NEW
1799
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
1800
        } else {
×
NEW
1801
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
1802
        }
×
NEW
1803
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1804
        if err != nil {
×
NEW
1805
                return nil, err
×
NEW
1806
        }
×
NEW
1807
        defer rows.Close()
×
NEW
1808
        var items []GraphNodeExtraType
×
NEW
1809
        for rows.Next() {
×
NEW
1810
                var i GraphNodeExtraType
×
NEW
1811
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
NEW
1812
                        return nil, err
×
NEW
1813
                }
×
NEW
1814
                items = append(items, i)
×
1815
        }
NEW
1816
        if err := rows.Close(); err != nil {
×
NEW
1817
                return nil, err
×
NEW
1818
        }
×
NEW
1819
        if err := rows.Err(); err != nil {
×
NEW
1820
                return nil, err
×
NEW
1821
        }
×
NEW
1822
        return items, nil
×
1823
}
1824

1825
const getNodeFeatures = `-- name: GetNodeFeatures :many
1826
SELECT node_id, feature_bit
1827
FROM graph_node_features
1828
WHERE node_id = $1
1829
`
1830

NEW
1831
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
NEW
1832
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
NEW
1833
        if err != nil {
×
NEW
1834
                return nil, err
×
NEW
1835
        }
×
NEW
1836
        defer rows.Close()
×
NEW
1837
        var items []GraphNodeFeature
×
NEW
1838
        for rows.Next() {
×
NEW
1839
                var i GraphNodeFeature
×
NEW
1840
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
NEW
1841
                        return nil, err
×
NEW
1842
                }
×
NEW
1843
                items = append(items, i)
×
1844
        }
NEW
1845
        if err := rows.Close(); err != nil {
×
NEW
1846
                return nil, err
×
NEW
1847
        }
×
NEW
1848
        if err := rows.Err(); err != nil {
×
NEW
1849
                return nil, err
×
NEW
1850
        }
×
NEW
1851
        return items, nil
×
1852
}
1853

1854
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
1855
SELECT node_id, feature_bit
1856
FROM graph_node_features
1857
WHERE node_id IN (/*SLICE:ids*/?)
1858
ORDER BY node_id, feature_bit
1859
`
1860

NEW
1861
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
×
NEW
1862
        query := getNodeFeaturesBatch
×
NEW
1863
        var queryParams []interface{}
×
NEW
1864
        if len(ids) > 0 {
×
NEW
1865
                for _, v := range ids {
×
NEW
1866
                        queryParams = append(queryParams, v)
×
NEW
1867
                }
×
NEW
1868
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
1869
        } else {
×
NEW
1870
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
1871
        }
×
NEW
1872
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1873
        if err != nil {
×
NEW
1874
                return nil, err
×
NEW
1875
        }
×
NEW
1876
        defer rows.Close()
×
NEW
1877
        var items []GraphNodeFeature
×
NEW
1878
        for rows.Next() {
×
NEW
1879
                var i GraphNodeFeature
×
NEW
1880
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
NEW
1881
                        return nil, err
×
NEW
1882
                }
×
NEW
1883
                items = append(items, i)
×
1884
        }
NEW
1885
        if err := rows.Close(); err != nil {
×
NEW
1886
                return nil, err
×
NEW
1887
        }
×
NEW
1888
        if err := rows.Err(); err != nil {
×
NEW
1889
                return nil, err
×
NEW
1890
        }
×
NEW
1891
        return items, nil
×
1892
}
1893

1894
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
1895
SELECT f.feature_bit
1896
FROM graph_nodes n
1897
    JOIN graph_node_features f ON f.node_id = n.id
1898
WHERE n.pub_key = $1
1899
  AND n.version = $2
1900
`
1901

1902
type GetNodeFeaturesByPubKeyParams struct {
1903
        PubKey  []byte
1904
        Version int16
1905
}
1906

NEW
1907
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
NEW
1908
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
NEW
1909
        if err != nil {
×
NEW
1910
                return nil, err
×
NEW
1911
        }
×
NEW
1912
        defer rows.Close()
×
NEW
1913
        var items []int32
×
NEW
1914
        for rows.Next() {
×
NEW
1915
                var feature_bit int32
×
NEW
1916
                if err := rows.Scan(&feature_bit); err != nil {
×
NEW
1917
                        return nil, err
×
NEW
1918
                }
×
NEW
1919
                items = append(items, feature_bit)
×
1920
        }
NEW
1921
        if err := rows.Close(); err != nil {
×
NEW
1922
                return nil, err
×
NEW
1923
        }
×
NEW
1924
        if err := rows.Err(); err != nil {
×
NEW
1925
                return nil, err
×
NEW
1926
        }
×
NEW
1927
        return items, nil
×
1928
}
1929

1930
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
1931
SELECT id
1932
FROM graph_nodes
1933
WHERE pub_key = $1
1934
  AND version = $2
1935
`
1936

1937
type GetNodeIDByPubKeyParams struct {
1938
        PubKey  []byte
1939
        Version int16
1940
}
1941

NEW
1942
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
NEW
1943
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
NEW
1944
        var id int64
×
NEW
1945
        err := row.Scan(&id)
×
NEW
1946
        return id, err
×
NEW
1947
}
×
1948

1949
const getNodesByIDs = `-- name: GetNodesByIDs :many
1950
SELECT id, version, pub_key, alias, last_update, color, signature
1951
FROM graph_nodes
1952
WHERE id IN (/*SLICE:ids*/?)
1953
`
1954

NEW
1955
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
NEW
1956
        query := getNodesByIDs
×
NEW
1957
        var queryParams []interface{}
×
NEW
1958
        if len(ids) > 0 {
×
NEW
1959
                for _, v := range ids {
×
NEW
1960
                        queryParams = append(queryParams, v)
×
NEW
1961
                }
×
NEW
1962
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
NEW
1963
        } else {
×
NEW
1964
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
NEW
1965
        }
×
NEW
1966
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
1967
        if err != nil {
×
NEW
1968
                return nil, err
×
NEW
1969
        }
×
NEW
1970
        defer rows.Close()
×
NEW
1971
        var items []GraphNode
×
NEW
1972
        for rows.Next() {
×
NEW
1973
                var i GraphNode
×
NEW
1974
                if err := rows.Scan(
×
NEW
1975
                        &i.ID,
×
NEW
1976
                        &i.Version,
×
NEW
1977
                        &i.PubKey,
×
NEW
1978
                        &i.Alias,
×
NEW
1979
                        &i.LastUpdate,
×
NEW
1980
                        &i.Color,
×
NEW
1981
                        &i.Signature,
×
NEW
1982
                ); err != nil {
×
NEW
1983
                        return nil, err
×
NEW
1984
                }
×
NEW
1985
                items = append(items, i)
×
1986
        }
NEW
1987
        if err := rows.Close(); err != nil {
×
NEW
1988
                return nil, err
×
NEW
1989
        }
×
NEW
1990
        if err := rows.Err(); err != nil {
×
NEW
1991
                return nil, err
×
NEW
1992
        }
×
NEW
1993
        return items, nil
×
1994
}
1995

1996
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
1997
SELECT id, version, pub_key, alias, last_update, color, signature
1998
FROM graph_nodes
1999
WHERE last_update >= $1
2000
  AND last_update <= $2
2001
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2002
  -- This ensures stable ordering and allows us to resume from where we left off.
2003
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2004
  AND (
2005
    -- Include rows with last_update greater than cursor (or all rows if cursor is -1)
2006
    last_update > COALESCE($3, -1)
2007
    OR 
2008
    -- For rows with same last_update, use pub_key as tiebreaker
2009
    (last_update = COALESCE($3, -1) 
2010
     AND pub_key > $4)
2011
  )
2012
  -- Optional filter for public nodes only
2013
  AND (
2014
    -- If only_public is false or not provided, include all nodes
2015
    COALESCE($5, FALSE) IS FALSE
2016
    OR 
2017
    -- For V1 protocol, a node is public if it has at least one public channel.
2018
    -- A public channel has bitcoin_1_signature set (channel announcement received).
2019
    EXISTS (
2020
      SELECT 1
2021
      FROM graph_channels c
2022
      WHERE c.version = 1
2023
        AND c.bitcoin_1_signature IS NOT NULL
2024
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2025
    )
2026
  )
2027
ORDER BY last_update ASC, pub_key ASC
2028
LIMIT COALESCE($6, 999999999)
2029
`
2030

2031
type GetNodesByLastUpdateRangeParams struct {
2032
        StartTime  sql.NullInt64
2033
        EndTime    sql.NullInt64
2034
        LastUpdate sql.NullInt64
2035
        LastPubKey []byte
2036
        OnlyPublic interface{}
2037
        MaxResults interface{}
2038
}
2039

NEW
2040
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
NEW
2041
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
NEW
2042
                arg.StartTime,
×
NEW
2043
                arg.EndTime,
×
NEW
2044
                arg.LastUpdate,
×
NEW
2045
                arg.LastPubKey,
×
NEW
2046
                arg.OnlyPublic,
×
NEW
2047
                arg.MaxResults,
×
NEW
2048
        )
×
NEW
2049
        if err != nil {
×
NEW
2050
                return nil, err
×
NEW
2051
        }
×
NEW
2052
        defer rows.Close()
×
NEW
2053
        var items []GraphNode
×
NEW
2054
        for rows.Next() {
×
NEW
2055
                var i GraphNode
×
NEW
2056
                if err := rows.Scan(
×
NEW
2057
                        &i.ID,
×
NEW
2058
                        &i.Version,
×
NEW
2059
                        &i.PubKey,
×
NEW
2060
                        &i.Alias,
×
NEW
2061
                        &i.LastUpdate,
×
NEW
2062
                        &i.Color,
×
NEW
2063
                        &i.Signature,
×
NEW
2064
                ); err != nil {
×
NEW
2065
                        return nil, err
×
NEW
2066
                }
×
NEW
2067
                items = append(items, i)
×
2068
        }
NEW
2069
        if err := rows.Close(); err != nil {
×
NEW
2070
                return nil, err
×
NEW
2071
        }
×
NEW
2072
        if err := rows.Err(); err != nil {
×
NEW
2073
                return nil, err
×
NEW
2074
        }
×
NEW
2075
        return items, nil
×
2076
}
2077

2078
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2079
SELECT block_height, block_hash
2080
FROM graph_prune_log
2081
WHERE block_height
2082
   IN (/*SLICE:heights*/?)
2083
`
2084

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

2118
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2119
SELECT block_hash
2120
FROM graph_prune_log
2121
WHERE block_height = $1
2122
`
2123

NEW
2124
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
NEW
2125
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
NEW
2126
        var block_hash []byte
×
NEW
2127
        err := row.Scan(&block_hash)
×
NEW
2128
        return block_hash, err
×
NEW
2129
}
×
2130

2131
const getPruneTip = `-- name: GetPruneTip :one
2132
SELECT block_height, block_hash
2133
FROM graph_prune_log
2134
ORDER BY block_height DESC
2135
LIMIT 1
2136
`
2137

NEW
2138
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
NEW
2139
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
NEW
2140
        var i GraphPruneLog
×
NEW
2141
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
NEW
2142
        return i, err
×
NEW
2143
}
×
2144

2145
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2146
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
2147
FROM graph_channels
2148
WHERE node_1_signature IS NOT NULL
2149
  AND scid >= $1
2150
  AND scid < $2
2151
`
2152

2153
type GetPublicV1ChannelsBySCIDParams struct {
2154
        StartScid []byte
2155
        EndScid   []byte
2156
}
2157

NEW
2158
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
NEW
2159
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
NEW
2160
        if err != nil {
×
NEW
2161
                return nil, err
×
NEW
2162
        }
×
NEW
2163
        defer rows.Close()
×
NEW
2164
        var items []GraphChannel
×
NEW
2165
        for rows.Next() {
×
NEW
2166
                var i GraphChannel
×
NEW
2167
                if err := rows.Scan(
×
NEW
2168
                        &i.ID,
×
NEW
2169
                        &i.Version,
×
NEW
2170
                        &i.Scid,
×
NEW
2171
                        &i.NodeID1,
×
NEW
2172
                        &i.NodeID2,
×
NEW
2173
                        &i.Outpoint,
×
NEW
2174
                        &i.Capacity,
×
NEW
2175
                        &i.BitcoinKey1,
×
NEW
2176
                        &i.BitcoinKey2,
×
NEW
2177
                        &i.Node1Signature,
×
NEW
2178
                        &i.Node2Signature,
×
NEW
2179
                        &i.Bitcoin1Signature,
×
NEW
2180
                        &i.Bitcoin2Signature,
×
NEW
2181
                ); err != nil {
×
NEW
2182
                        return nil, err
×
NEW
2183
                }
×
NEW
2184
                items = append(items, i)
×
2185
        }
NEW
2186
        if err := rows.Close(); err != nil {
×
NEW
2187
                return nil, err
×
NEW
2188
        }
×
NEW
2189
        if err := rows.Err(); err != nil {
×
NEW
2190
                return nil, err
×
NEW
2191
        }
×
NEW
2192
        return items, nil
×
2193
}
2194

2195
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2196
SELECT scid from graph_channels
2197
WHERE outpoint = $1 AND version = $2
2198
`
2199

2200
type GetSCIDByOutpointParams struct {
2201
        Outpoint string
2202
        Version  int16
2203
}
2204

NEW
2205
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
NEW
2206
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
NEW
2207
        var scid []byte
×
NEW
2208
        err := row.Scan(&scid)
×
NEW
2209
        return scid, err
×
NEW
2210
}
×
2211

2212
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2213
SELECT sn.node_id, n.pub_key
2214
FROM graph_source_nodes sn
2215
    JOIN graph_nodes n ON sn.node_id = n.id
2216
WHERE n.version = $1
2217
`
2218

2219
type GetSourceNodesByVersionRow struct {
2220
        NodeID int64
2221
        PubKey []byte
2222
}
2223

NEW
2224
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
NEW
2225
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
NEW
2226
        if err != nil {
×
NEW
2227
                return nil, err
×
NEW
2228
        }
×
NEW
2229
        defer rows.Close()
×
NEW
2230
        var items []GetSourceNodesByVersionRow
×
NEW
2231
        for rows.Next() {
×
NEW
2232
                var i GetSourceNodesByVersionRow
×
NEW
2233
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
NEW
2234
                        return nil, err
×
NEW
2235
                }
×
NEW
2236
                items = append(items, i)
×
2237
        }
NEW
2238
        if err := rows.Close(); err != nil {
×
NEW
2239
                return nil, err
×
NEW
2240
        }
×
NEW
2241
        if err := rows.Err(); err != nil {
×
NEW
2242
                return nil, err
×
NEW
2243
        }
×
NEW
2244
        return items, nil
×
2245
}
2246

2247
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2248
SELECT c.scid
2249
FROM graph_channels c
2250
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2251
WHERE cp.disabled = true
2252
AND c.version = 1
2253
GROUP BY c.scid
2254
HAVING COUNT(*) > 1
2255
`
2256

2257
// NOTE: this is V1 specific since for V1, disabled is a
2258
// simple, single boolean. The proposed V2 policy
2259
// structure will have a more complex disabled bit vector
2260
// and so the query for V2 may differ.
NEW
2261
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
NEW
2262
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
NEW
2263
        if err != nil {
×
NEW
2264
                return nil, err
×
NEW
2265
        }
×
NEW
2266
        defer rows.Close()
×
NEW
2267
        var items [][]byte
×
NEW
2268
        for rows.Next() {
×
NEW
2269
                var scid []byte
×
NEW
2270
                if err := rows.Scan(&scid); err != nil {
×
NEW
2271
                        return nil, err
×
NEW
2272
                }
×
NEW
2273
                items = append(items, scid)
×
2274
        }
NEW
2275
        if err := rows.Close(); err != nil {
×
NEW
2276
                return nil, err
×
NEW
2277
        }
×
NEW
2278
        if err := rows.Err(); err != nil {
×
NEW
2279
                return nil, err
×
NEW
2280
        }
×
NEW
2281
        return items, nil
×
2282
}
2283

2284
const getZombieChannel = `-- name: GetZombieChannel :one
2285
SELECT scid, version, node_key_1, node_key_2
2286
FROM graph_zombie_channels
2287
WHERE scid = $1
2288
AND version = $2
2289
`
2290

2291
type GetZombieChannelParams struct {
2292
        Scid    []byte
2293
        Version int16
2294
}
2295

NEW
2296
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
NEW
2297
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
NEW
2298
        var i GraphZombieChannel
×
NEW
2299
        err := row.Scan(
×
NEW
2300
                &i.Scid,
×
NEW
2301
                &i.Version,
×
NEW
2302
                &i.NodeKey1,
×
NEW
2303
                &i.NodeKey2,
×
NEW
2304
        )
×
NEW
2305
        return i, err
×
NEW
2306
}
×
2307

2308
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2309
SELECT scid, version, node_key_1, node_key_2
2310
FROM graph_zombie_channels
2311
WHERE version = $1
2312
  AND scid IN (/*SLICE:scids*/?)
2313
`
2314

2315
type GetZombieChannelsSCIDsParams struct {
2316
        Version int16
2317
        Scids   [][]byte
2318
}
2319

NEW
2320
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
NEW
2321
        query := getZombieChannelsSCIDs
×
NEW
2322
        var queryParams []interface{}
×
NEW
2323
        queryParams = append(queryParams, arg.Version)
×
NEW
2324
        if len(arg.Scids) > 0 {
×
NEW
2325
                for _, v := range arg.Scids {
×
NEW
2326
                        queryParams = append(queryParams, v)
×
NEW
2327
                }
×
NEW
2328
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
NEW
2329
        } else {
×
NEW
2330
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
NEW
2331
        }
×
NEW
2332
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
2333
        if err != nil {
×
NEW
2334
                return nil, err
×
NEW
2335
        }
×
NEW
2336
        defer rows.Close()
×
NEW
2337
        var items []GraphZombieChannel
×
NEW
2338
        for rows.Next() {
×
NEW
2339
                var i GraphZombieChannel
×
NEW
2340
                if err := rows.Scan(
×
NEW
2341
                        &i.Scid,
×
NEW
2342
                        &i.Version,
×
NEW
2343
                        &i.NodeKey1,
×
NEW
2344
                        &i.NodeKey2,
×
NEW
2345
                ); err != nil {
×
NEW
2346
                        return nil, err
×
NEW
2347
                }
×
NEW
2348
                items = append(items, i)
×
2349
        }
NEW
2350
        if err := rows.Close(); err != nil {
×
NEW
2351
                return nil, err
×
NEW
2352
        }
×
NEW
2353
        if err := rows.Err(); err != nil {
×
NEW
2354
                return nil, err
×
NEW
2355
        }
×
NEW
2356
        return items, nil
×
2357
}
2358

2359
const highestSCID = `-- name: HighestSCID :one
2360
SELECT scid
2361
FROM graph_channels
2362
WHERE version = $1
2363
ORDER BY scid DESC
2364
LIMIT 1
2365
`
2366

NEW
2367
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
NEW
2368
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
NEW
2369
        var scid []byte
×
NEW
2370
        err := row.Scan(&scid)
×
NEW
2371
        return scid, err
×
NEW
2372
}
×
2373

2374
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2375
/* ─────────────────────────────────────────────
2376
   graph_channel_features table queries
2377
   ─────────────────────────────────────────────
2378
*/
2379

2380
INSERT INTO graph_channel_features (
2381
    channel_id, feature_bit
2382
) VALUES (
2383
    $1, $2
2384
) ON CONFLICT (channel_id, feature_bit)
2385
    -- Do nothing if the channel_id and feature_bit already exist.
2386
    DO NOTHING
2387
`
2388

2389
type InsertChannelFeatureParams struct {
2390
        ChannelID  int64
2391
        FeatureBit int32
2392
}
2393

NEW
2394
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
NEW
2395
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
NEW
2396
        return err
×
NEW
2397
}
×
2398

2399
const insertChannelMig = `-- name: InsertChannelMig :one
2400
INSERT INTO graph_channels (
2401
    version, scid, node_id_1, node_id_2,
2402
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2403
    node_1_signature, node_2_signature, bitcoin_1_signature,
2404
    bitcoin_2_signature
2405
) VALUES (
2406
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2407
) ON CONFLICT (scid, version)
2408
    -- If a conflict occurs, we have already migrated this channel. However, we
2409
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2410
    -- otherwise, the "RETURNING id" part does not work.
2411
    DO UPDATE SET
2412
        node_id_1 = EXCLUDED.node_id_1,
2413
        node_id_2 = EXCLUDED.node_id_2,
2414
        outpoint = EXCLUDED.outpoint,
2415
        capacity = EXCLUDED.capacity,
2416
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2417
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2418
        node_1_signature = EXCLUDED.node_1_signature,
2419
        node_2_signature = EXCLUDED.node_2_signature,
2420
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2421
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2422
RETURNING id
2423
`
2424

2425
type InsertChannelMigParams struct {
2426
        Version           int16
2427
        Scid              []byte
2428
        NodeID1           int64
2429
        NodeID2           int64
2430
        Outpoint          string
2431
        Capacity          sql.NullInt64
2432
        BitcoinKey1       []byte
2433
        BitcoinKey2       []byte
2434
        Node1Signature    []byte
2435
        Node2Signature    []byte
2436
        Bitcoin1Signature []byte
2437
        Bitcoin2Signature []byte
2438
}
2439

2440
// NOTE: This query is only meant to be used by the graph SQL migration since
2441
// for that migration, in order to be retry-safe, we don't want to error out if
2442
// we re-insert the same channel again (which would error if the normal
2443
// CreateChannel query is used because of the uniqueness constraint on the scid
2444
// and version columns).
NEW
2445
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
NEW
2446
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
NEW
2447
                arg.Version,
×
NEW
2448
                arg.Scid,
×
NEW
2449
                arg.NodeID1,
×
NEW
2450
                arg.NodeID2,
×
NEW
2451
                arg.Outpoint,
×
NEW
2452
                arg.Capacity,
×
NEW
2453
                arg.BitcoinKey1,
×
NEW
2454
                arg.BitcoinKey2,
×
NEW
2455
                arg.Node1Signature,
×
NEW
2456
                arg.Node2Signature,
×
NEW
2457
                arg.Bitcoin1Signature,
×
NEW
2458
                arg.Bitcoin2Signature,
×
NEW
2459
        )
×
NEW
2460
        var id int64
×
NEW
2461
        err := row.Scan(&id)
×
NEW
2462
        return id, err
×
NEW
2463
}
×
2464

2465
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2466
/* ─────────────────────────────────────────────
2467
   graph_closed_scid table queries
2468
   ────────────────────────────────────────────-
2469
*/
2470

2471
INSERT INTO graph_closed_scids (scid)
2472
VALUES ($1)
2473
ON CONFLICT (scid) DO NOTHING
2474
`
2475

NEW
2476
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
NEW
2477
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
NEW
2478
        return err
×
NEW
2479
}
×
2480

2481
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
2482
INSERT INTO graph_channel_policies (
2483
    version, channel_id, node_id, timelock, fee_ppm,
2484
    base_fee_msat, min_htlc_msat, last_update, disabled,
2485
    max_htlc_msat, inbound_base_fee_msat,
2486
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2487
    signature
2488
) VALUES  (
2489
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2490
)
2491
ON CONFLICT (channel_id, node_id, version)
2492
    -- If a conflict occurs, we have already migrated this policy. However, we
2493
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2494
    -- otherwise, the "RETURNING id" part does not work.
2495
    DO UPDATE SET
2496
        timelock = EXCLUDED.timelock,
2497
        fee_ppm = EXCLUDED.fee_ppm,
2498
        base_fee_msat = EXCLUDED.base_fee_msat,
2499
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2500
        last_update = EXCLUDED.last_update,
2501
        disabled = EXCLUDED.disabled,
2502
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2503
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2504
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2505
        message_flags = EXCLUDED.message_flags,
2506
        channel_flags = EXCLUDED.channel_flags,
2507
        signature = EXCLUDED.signature
2508
RETURNING id
2509
`
2510

2511
type InsertEdgePolicyMigParams struct {
2512
        Version                 int16
2513
        ChannelID               int64
2514
        NodeID                  int64
2515
        Timelock                int32
2516
        FeePpm                  int64
2517
        BaseFeeMsat             int64
2518
        MinHtlcMsat             int64
2519
        LastUpdate              sql.NullInt64
2520
        Disabled                sql.NullBool
2521
        MaxHtlcMsat             sql.NullInt64
2522
        InboundBaseFeeMsat      sql.NullInt64
2523
        InboundFeeRateMilliMsat sql.NullInt64
2524
        MessageFlags            sql.NullInt16
2525
        ChannelFlags            sql.NullInt16
2526
        Signature               []byte
2527
}
2528

2529
// NOTE: This query is only meant to be used by the graph SQL migration since
2530
// for that migration, in order to be retry-safe, we don't want to error out if
2531
// we re-insert the same policy (which would error if the normal
2532
// UpsertEdgePolicy query is used because of the constraint in that query that
2533
// requires a policy update to have a newer last_update than the existing one).
NEW
2534
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
NEW
2535
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
NEW
2536
                arg.Version,
×
NEW
2537
                arg.ChannelID,
×
NEW
2538
                arg.NodeID,
×
NEW
2539
                arg.Timelock,
×
NEW
2540
                arg.FeePpm,
×
NEW
2541
                arg.BaseFeeMsat,
×
NEW
2542
                arg.MinHtlcMsat,
×
NEW
2543
                arg.LastUpdate,
×
NEW
2544
                arg.Disabled,
×
NEW
2545
                arg.MaxHtlcMsat,
×
NEW
2546
                arg.InboundBaseFeeMsat,
×
NEW
2547
                arg.InboundFeeRateMilliMsat,
×
NEW
2548
                arg.MessageFlags,
×
NEW
2549
                arg.ChannelFlags,
×
NEW
2550
                arg.Signature,
×
NEW
2551
        )
×
NEW
2552
        var id int64
×
NEW
2553
        err := row.Scan(&id)
×
NEW
2554
        return id, err
×
NEW
2555
}
×
2556

2557
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2558
/* ─────────────────────────────────────────────
2559
   graph_node_features table queries
2560
   ─────────────────────────────────────────────
2561
*/
2562

2563
INSERT INTO graph_node_features (
2564
    node_id, feature_bit
2565
) VALUES (
2566
    $1, $2
2567
) ON CONFLICT (node_id, feature_bit)
2568
    -- Do nothing if the feature already exists for the node.
2569
    DO NOTHING
2570
`
2571

2572
type InsertNodeFeatureParams struct {
2573
        NodeID     int64
2574
        FeatureBit int32
2575
}
2576

NEW
2577
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
NEW
2578
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
NEW
2579
        return err
×
NEW
2580
}
×
2581

2582
const insertNodeMig = `-- name: InsertNodeMig :one
2583
/* ─────────────────────────────────────────────
2584
   Migration specific queries
2585

2586
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2587
   dedicated to the migration that requires it, and so we can then remove
2588
   it from the main set of "live" queries that the code-base has access to.
2589
   ────────────────────────────────────────────-
2590
*/
2591

2592
INSERT INTO graph_nodes (
2593
    version, pub_key, alias, last_update, color, signature
2594
) VALUES (
2595
    $1, $2, $3, $4, $5, $6
2596
)
2597
ON CONFLICT (pub_key, version)
2598
    -- If a conflict occurs, we have already migrated this node. However, we
2599
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2600
    -- otherwise, the "RETURNING id" part does not work.
2601
    DO UPDATE SET
2602
        alias = EXCLUDED.alias,
2603
        last_update = EXCLUDED.last_update,
2604
        color = EXCLUDED.color,
2605
        signature = EXCLUDED.signature
2606
RETURNING id
2607
`
2608

2609
type InsertNodeMigParams struct {
2610
        Version    int16
2611
        PubKey     []byte
2612
        Alias      sql.NullString
2613
        LastUpdate sql.NullInt64
2614
        Color      sql.NullString
2615
        Signature  []byte
2616
}
2617

2618
// NOTE: This query is only meant to be used by the graph SQL migration since
2619
// for that migration, in order to be retry-safe, we don't want to error out if
2620
// we re-insert the same node (which would error if the normal UpsertNode query
2621
// is used because of the constraint in that query that requires a node update
2622
// to have a newer last_update than the existing node).
NEW
2623
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
NEW
2624
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
NEW
2625
                arg.Version,
×
NEW
2626
                arg.PubKey,
×
NEW
2627
                arg.Alias,
×
NEW
2628
                arg.LastUpdate,
×
NEW
2629
                arg.Color,
×
NEW
2630
                arg.Signature,
×
NEW
2631
        )
×
NEW
2632
        var id int64
×
NEW
2633
        err := row.Scan(&id)
×
NEW
2634
        return id, err
×
NEW
2635
}
×
2636

2637
const isClosedChannel = `-- name: IsClosedChannel :one
2638
SELECT EXISTS (
2639
    SELECT 1
2640
    FROM graph_closed_scids
2641
    WHERE scid = $1
2642
)
2643
`
2644

NEW
2645
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
NEW
2646
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
NEW
2647
        var exists bool
×
NEW
2648
        err := row.Scan(&exists)
×
NEW
2649
        return exists, err
×
NEW
2650
}
×
2651

2652
const isPublicV1Node = `-- name: IsPublicV1Node :one
2653
SELECT EXISTS (
2654
    SELECT 1
2655
    FROM graph_channels c
2656
    JOIN graph_nodes n ON n.id = c.node_id_1 OR n.id = c.node_id_2
2657
    -- NOTE: we hard-code the version here since the clauses
2658
    -- here that determine if a node is public is specific
2659
    -- to the V1 gossip protocol. In V1, a node is public
2660
    -- if it has a public channel and a public channel is one
2661
    -- where we have the set of signatures of the channel
2662
    -- announcement. It is enough to just check that we have
2663
    -- one of the signatures since we only ever set them
2664
    -- together.
2665
    WHERE c.version = 1
2666
      AND c.bitcoin_1_signature IS NOT NULL
2667
      AND n.pub_key = $1
2668
)
2669
`
2670

NEW
2671
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
NEW
2672
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
NEW
2673
        var exists bool
×
NEW
2674
        err := row.Scan(&exists)
×
NEW
2675
        return exists, err
×
NEW
2676
}
×
2677

2678
const isZombieChannel = `-- name: IsZombieChannel :one
2679
SELECT EXISTS (
2680
    SELECT 1
2681
    FROM graph_zombie_channels
2682
    WHERE scid = $1
2683
    AND version = $2
2684
) AS is_zombie
2685
`
2686

2687
type IsZombieChannelParams struct {
2688
        Scid    []byte
2689
        Version int16
2690
}
2691

NEW
2692
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
NEW
2693
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
NEW
2694
        var is_zombie bool
×
NEW
2695
        err := row.Scan(&is_zombie)
×
NEW
2696
        return is_zombie, err
×
NEW
2697
}
×
2698

2699
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2700
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,
2701
    n1.pub_key AS node1_pubkey,
2702
    n2.pub_key AS node2_pubkey,
2703

2704
    -- Policy 1
2705
    -- TODO(elle): use sqlc.embed to embed policy structs
2706
    --  once this issue is resolved:
2707
    --  https://github.com/sqlc-dev/sqlc/issues/2997
2708
    cp1.id AS policy1_id,
2709
    cp1.node_id AS policy1_node_id,
2710
    cp1.version AS policy1_version,
2711
    cp1.timelock AS policy1_timelock,
2712
    cp1.fee_ppm AS policy1_fee_ppm,
2713
    cp1.base_fee_msat AS policy1_base_fee_msat,
2714
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
2715
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
2716
    cp1.last_update AS policy1_last_update,
2717
    cp1.disabled AS policy1_disabled,
2718
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2719
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2720
    cp1.message_flags AS policy1_message_flags,
2721
    cp1.channel_flags AS policy1_channel_flags,
2722
    cp1.signature AS policy1_signature,
2723

2724
       -- Policy 2
2725
    cp2.id AS policy2_id,
2726
    cp2.node_id AS policy2_node_id,
2727
    cp2.version AS policy2_version,
2728
    cp2.timelock AS policy2_timelock,
2729
    cp2.fee_ppm AS policy2_fee_ppm,
2730
    cp2.base_fee_msat AS policy2_base_fee_msat,
2731
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
2732
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
2733
    cp2.last_update AS policy2_last_update,
2734
    cp2.disabled AS policy2_disabled,
2735
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2736
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2737
    cp2.message_flags AS policy2_message_flags,
2738
    cp2.channel_flags AS policy2_channel_flags,
2739
    cp2.signature AS policy2_signature
2740

2741
FROM graph_channels c
2742
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2743
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2744
    LEFT JOIN graph_channel_policies cp1
2745
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2746
    LEFT JOIN graph_channel_policies cp2
2747
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2748
WHERE c.version = $1
2749
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2750
`
2751

2752
type ListChannelsByNodeIDParams struct {
2753
        Version int16
2754
        NodeID1 int64
2755
}
2756

2757
type ListChannelsByNodeIDRow struct {
2758
        GraphChannel                   GraphChannel
2759
        Node1Pubkey                    []byte
2760
        Node2Pubkey                    []byte
2761
        Policy1ID                      sql.NullInt64
2762
        Policy1NodeID                  sql.NullInt64
2763
        Policy1Version                 sql.NullInt16
2764
        Policy1Timelock                sql.NullInt32
2765
        Policy1FeePpm                  sql.NullInt64
2766
        Policy1BaseFeeMsat             sql.NullInt64
2767
        Policy1MinHtlcMsat             sql.NullInt64
2768
        Policy1MaxHtlcMsat             sql.NullInt64
2769
        Policy1LastUpdate              sql.NullInt64
2770
        Policy1Disabled                sql.NullBool
2771
        Policy1InboundBaseFeeMsat      sql.NullInt64
2772
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2773
        Policy1MessageFlags            sql.NullInt16
2774
        Policy1ChannelFlags            sql.NullInt16
2775
        Policy1Signature               []byte
2776
        Policy2ID                      sql.NullInt64
2777
        Policy2NodeID                  sql.NullInt64
2778
        Policy2Version                 sql.NullInt16
2779
        Policy2Timelock                sql.NullInt32
2780
        Policy2FeePpm                  sql.NullInt64
2781
        Policy2BaseFeeMsat             sql.NullInt64
2782
        Policy2MinHtlcMsat             sql.NullInt64
2783
        Policy2MaxHtlcMsat             sql.NullInt64
2784
        Policy2LastUpdate              sql.NullInt64
2785
        Policy2Disabled                sql.NullBool
2786
        Policy2InboundBaseFeeMsat      sql.NullInt64
2787
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2788
        Policy2MessageFlags            sql.NullInt16
2789
        Policy2ChannelFlags            sql.NullInt16
2790
        Policy2Signature               []byte
2791
}
2792

NEW
2793
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
NEW
2794
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
NEW
2795
        if err != nil {
×
NEW
2796
                return nil, err
×
NEW
2797
        }
×
NEW
2798
        defer rows.Close()
×
NEW
2799
        var items []ListChannelsByNodeIDRow
×
NEW
2800
        for rows.Next() {
×
NEW
2801
                var i ListChannelsByNodeIDRow
×
NEW
2802
                if err := rows.Scan(
×
NEW
2803
                        &i.GraphChannel.ID,
×
NEW
2804
                        &i.GraphChannel.Version,
×
NEW
2805
                        &i.GraphChannel.Scid,
×
NEW
2806
                        &i.GraphChannel.NodeID1,
×
NEW
2807
                        &i.GraphChannel.NodeID2,
×
NEW
2808
                        &i.GraphChannel.Outpoint,
×
NEW
2809
                        &i.GraphChannel.Capacity,
×
NEW
2810
                        &i.GraphChannel.BitcoinKey1,
×
NEW
2811
                        &i.GraphChannel.BitcoinKey2,
×
NEW
2812
                        &i.GraphChannel.Node1Signature,
×
NEW
2813
                        &i.GraphChannel.Node2Signature,
×
NEW
2814
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
2815
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
2816
                        &i.Node1Pubkey,
×
NEW
2817
                        &i.Node2Pubkey,
×
NEW
2818
                        &i.Policy1ID,
×
NEW
2819
                        &i.Policy1NodeID,
×
NEW
2820
                        &i.Policy1Version,
×
NEW
2821
                        &i.Policy1Timelock,
×
NEW
2822
                        &i.Policy1FeePpm,
×
NEW
2823
                        &i.Policy1BaseFeeMsat,
×
NEW
2824
                        &i.Policy1MinHtlcMsat,
×
NEW
2825
                        &i.Policy1MaxHtlcMsat,
×
NEW
2826
                        &i.Policy1LastUpdate,
×
NEW
2827
                        &i.Policy1Disabled,
×
NEW
2828
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
2829
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
2830
                        &i.Policy1MessageFlags,
×
NEW
2831
                        &i.Policy1ChannelFlags,
×
NEW
2832
                        &i.Policy1Signature,
×
NEW
2833
                        &i.Policy2ID,
×
NEW
2834
                        &i.Policy2NodeID,
×
NEW
2835
                        &i.Policy2Version,
×
NEW
2836
                        &i.Policy2Timelock,
×
NEW
2837
                        &i.Policy2FeePpm,
×
NEW
2838
                        &i.Policy2BaseFeeMsat,
×
NEW
2839
                        &i.Policy2MinHtlcMsat,
×
NEW
2840
                        &i.Policy2MaxHtlcMsat,
×
NEW
2841
                        &i.Policy2LastUpdate,
×
NEW
2842
                        &i.Policy2Disabled,
×
NEW
2843
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
2844
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
2845
                        &i.Policy2MessageFlags,
×
NEW
2846
                        &i.Policy2ChannelFlags,
×
NEW
2847
                        &i.Policy2Signature,
×
NEW
2848
                ); err != nil {
×
NEW
2849
                        return nil, err
×
NEW
2850
                }
×
NEW
2851
                items = append(items, i)
×
2852
        }
NEW
2853
        if err := rows.Close(); err != nil {
×
NEW
2854
                return nil, err
×
NEW
2855
        }
×
NEW
2856
        if err := rows.Err(); err != nil {
×
NEW
2857
                return nil, err
×
NEW
2858
        }
×
NEW
2859
        return items, nil
×
2860
}
2861

2862
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
2863
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,
2864
       n1.pub_key AS node1_pubkey,
2865
       n2.pub_key AS node2_pubkey,
2866

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

2887
       -- Policy 2
2888
       cp2.id AS policy2_id,
2889
       cp2.node_id AS policy2_node_id,
2890
       cp2.version AS policy2_version,
2891
       cp2.timelock AS policy2_timelock,
2892
       cp2.fee_ppm AS policy2_fee_ppm,
2893
       cp2.base_fee_msat AS policy2_base_fee_msat,
2894
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
2895
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
2896
       cp2.last_update AS policy2_last_update,
2897
       cp2.disabled AS policy2_disabled,
2898
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2899
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2900
       cp2.message_flags AS policy2_message_flags,
2901
       cp2.channel_flags AS policy2_channel_flags,
2902
       cp2.signature AS policy2_signature
2903

2904
FROM graph_channels c
2905
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2906
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2907
         LEFT JOIN graph_channel_policies cp1
2908
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2909
         LEFT JOIN graph_channel_policies cp2
2910
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2911
WHERE c.version = $1
2912
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
2913
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
2914
`
2915

2916
type ListChannelsForNodeIDsParams struct {
2917
        Version  int16
2918
        Node1Ids []int64
2919
        Node2Ids []int64
2920
}
2921

2922
type ListChannelsForNodeIDsRow struct {
2923
        GraphChannel                   GraphChannel
2924
        Node1Pubkey                    []byte
2925
        Node2Pubkey                    []byte
2926
        Policy1ID                      sql.NullInt64
2927
        Policy1NodeID                  sql.NullInt64
2928
        Policy1Version                 sql.NullInt16
2929
        Policy1Timelock                sql.NullInt32
2930
        Policy1FeePpm                  sql.NullInt64
2931
        Policy1BaseFeeMsat             sql.NullInt64
2932
        Policy1MinHtlcMsat             sql.NullInt64
2933
        Policy1MaxHtlcMsat             sql.NullInt64
2934
        Policy1LastUpdate              sql.NullInt64
2935
        Policy1Disabled                sql.NullBool
2936
        Policy1InboundBaseFeeMsat      sql.NullInt64
2937
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2938
        Policy1MessageFlags            sql.NullInt16
2939
        Policy1ChannelFlags            sql.NullInt16
2940
        Policy1Signature               []byte
2941
        Policy2ID                      sql.NullInt64
2942
        Policy2NodeID                  sql.NullInt64
2943
        Policy2Version                 sql.NullInt16
2944
        Policy2Timelock                sql.NullInt32
2945
        Policy2FeePpm                  sql.NullInt64
2946
        Policy2BaseFeeMsat             sql.NullInt64
2947
        Policy2MinHtlcMsat             sql.NullInt64
2948
        Policy2MaxHtlcMsat             sql.NullInt64
2949
        Policy2LastUpdate              sql.NullInt64
2950
        Policy2Disabled                sql.NullBool
2951
        Policy2InboundBaseFeeMsat      sql.NullInt64
2952
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2953
        Policy2MessageFlags            sql.NullInt16
2954
        Policy2ChannelFlags            sql.NullInt16
2955
        Policy2Signature               []byte
2956
}
2957

NEW
2958
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
NEW
2959
        query := listChannelsForNodeIDs
×
NEW
2960
        var queryParams []interface{}
×
NEW
2961
        queryParams = append(queryParams, arg.Version)
×
NEW
2962
        if len(arg.Node1Ids) > 0 {
×
NEW
2963
                for _, v := range arg.Node1Ids {
×
NEW
2964
                        queryParams = append(queryParams, v)
×
NEW
2965
                }
×
NEW
2966
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
NEW
2967
        } else {
×
NEW
2968
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
NEW
2969
        }
×
NEW
2970
        if len(arg.Node2Ids) > 0 {
×
NEW
2971
                for _, v := range arg.Node2Ids {
×
NEW
2972
                        queryParams = append(queryParams, v)
×
NEW
2973
                }
×
NEW
2974
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
NEW
2975
        } else {
×
NEW
2976
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
NEW
2977
        }
×
NEW
2978
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
2979
        if err != nil {
×
NEW
2980
                return nil, err
×
NEW
2981
        }
×
NEW
2982
        defer rows.Close()
×
NEW
2983
        var items []ListChannelsForNodeIDsRow
×
NEW
2984
        for rows.Next() {
×
NEW
2985
                var i ListChannelsForNodeIDsRow
×
NEW
2986
                if err := rows.Scan(
×
NEW
2987
                        &i.GraphChannel.ID,
×
NEW
2988
                        &i.GraphChannel.Version,
×
NEW
2989
                        &i.GraphChannel.Scid,
×
NEW
2990
                        &i.GraphChannel.NodeID1,
×
NEW
2991
                        &i.GraphChannel.NodeID2,
×
NEW
2992
                        &i.GraphChannel.Outpoint,
×
NEW
2993
                        &i.GraphChannel.Capacity,
×
NEW
2994
                        &i.GraphChannel.BitcoinKey1,
×
NEW
2995
                        &i.GraphChannel.BitcoinKey2,
×
NEW
2996
                        &i.GraphChannel.Node1Signature,
×
NEW
2997
                        &i.GraphChannel.Node2Signature,
×
NEW
2998
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
2999
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
3000
                        &i.Node1Pubkey,
×
NEW
3001
                        &i.Node2Pubkey,
×
NEW
3002
                        &i.Policy1ID,
×
NEW
3003
                        &i.Policy1NodeID,
×
NEW
3004
                        &i.Policy1Version,
×
NEW
3005
                        &i.Policy1Timelock,
×
NEW
3006
                        &i.Policy1FeePpm,
×
NEW
3007
                        &i.Policy1BaseFeeMsat,
×
NEW
3008
                        &i.Policy1MinHtlcMsat,
×
NEW
3009
                        &i.Policy1MaxHtlcMsat,
×
NEW
3010
                        &i.Policy1LastUpdate,
×
NEW
3011
                        &i.Policy1Disabled,
×
NEW
3012
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
3013
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
3014
                        &i.Policy1MessageFlags,
×
NEW
3015
                        &i.Policy1ChannelFlags,
×
NEW
3016
                        &i.Policy1Signature,
×
NEW
3017
                        &i.Policy2ID,
×
NEW
3018
                        &i.Policy2NodeID,
×
NEW
3019
                        &i.Policy2Version,
×
NEW
3020
                        &i.Policy2Timelock,
×
NEW
3021
                        &i.Policy2FeePpm,
×
NEW
3022
                        &i.Policy2BaseFeeMsat,
×
NEW
3023
                        &i.Policy2MinHtlcMsat,
×
NEW
3024
                        &i.Policy2MaxHtlcMsat,
×
NEW
3025
                        &i.Policy2LastUpdate,
×
NEW
3026
                        &i.Policy2Disabled,
×
NEW
3027
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
3028
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
3029
                        &i.Policy2MessageFlags,
×
NEW
3030
                        &i.Policy2ChannelFlags,
×
NEW
3031
                        &i.Policy2Signature,
×
NEW
3032
                ); err != nil {
×
NEW
3033
                        return nil, err
×
NEW
3034
                }
×
NEW
3035
                items = append(items, i)
×
3036
        }
NEW
3037
        if err := rows.Close(); err != nil {
×
NEW
3038
                return nil, err
×
NEW
3039
        }
×
NEW
3040
        if err := rows.Err(); err != nil {
×
NEW
3041
                return nil, err
×
NEW
3042
        }
×
NEW
3043
        return items, nil
×
3044
}
3045

3046
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3047
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3048
FROM graph_channels c
3049
WHERE c.version = $1 AND c.id > $2
3050
ORDER BY c.id
3051
LIMIT $3
3052
`
3053

3054
type ListChannelsPaginatedParams struct {
3055
        Version int16
3056
        ID      int64
3057
        Limit   int32
3058
}
3059

3060
type ListChannelsPaginatedRow struct {
3061
        ID          int64
3062
        BitcoinKey1 []byte
3063
        BitcoinKey2 []byte
3064
        Outpoint    string
3065
}
3066

NEW
3067
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
NEW
3068
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
NEW
3069
        if err != nil {
×
NEW
3070
                return nil, err
×
NEW
3071
        }
×
NEW
3072
        defer rows.Close()
×
NEW
3073
        var items []ListChannelsPaginatedRow
×
NEW
3074
        for rows.Next() {
×
NEW
3075
                var i ListChannelsPaginatedRow
×
NEW
3076
                if err := rows.Scan(
×
NEW
3077
                        &i.ID,
×
NEW
3078
                        &i.BitcoinKey1,
×
NEW
3079
                        &i.BitcoinKey2,
×
NEW
3080
                        &i.Outpoint,
×
NEW
3081
                ); err != nil {
×
NEW
3082
                        return nil, err
×
NEW
3083
                }
×
NEW
3084
                items = append(items, i)
×
3085
        }
NEW
3086
        if err := rows.Close(); err != nil {
×
NEW
3087
                return nil, err
×
NEW
3088
        }
×
NEW
3089
        if err := rows.Err(); err != nil {
×
NEW
3090
                return nil, err
×
NEW
3091
        }
×
NEW
3092
        return items, nil
×
3093
}
3094

3095
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3096
SELECT
3097
    c.id as id,
3098
    c.scid as scid,
3099
    c.capacity AS capacity,
3100

3101
    -- Join node pubkeys
3102
    n1.pub_key AS node1_pubkey,
3103
    n2.pub_key AS node2_pubkey,
3104

3105
    -- Node 1 policy
3106
    cp1.timelock AS policy_1_timelock,
3107
    cp1.fee_ppm AS policy_1_fee_ppm,
3108
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3109
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3110
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3111
    cp1.disabled AS policy_1_disabled,
3112
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3113
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3114
    cp1.message_flags AS policy1_message_flags,
3115
    cp1.channel_flags AS policy1_channel_flags,
3116

3117
    -- Node 2 policy
3118
    cp2.timelock AS policy_2_timelock,
3119
    cp2.fee_ppm AS policy_2_fee_ppm,
3120
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3121
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3122
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3123
    cp2.disabled AS policy_2_disabled,
3124
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3125
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3126
    cp2.message_flags AS policy2_message_flags,
3127
    cp2.channel_flags AS policy2_channel_flags
3128

3129
FROM graph_channels c
3130
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3131
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3132
LEFT JOIN graph_channel_policies cp1
3133
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3134
LEFT JOIN graph_channel_policies cp2
3135
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3136
WHERE c.version = $1 AND c.id > $2
3137
ORDER BY c.id
3138
LIMIT $3
3139
`
3140

3141
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3142
        Version int16
3143
        ID      int64
3144
        Limit   int32
3145
}
3146

3147
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3148
        ID                             int64
3149
        Scid                           []byte
3150
        Capacity                       sql.NullInt64
3151
        Node1Pubkey                    []byte
3152
        Node2Pubkey                    []byte
3153
        Policy1Timelock                sql.NullInt32
3154
        Policy1FeePpm                  sql.NullInt64
3155
        Policy1BaseFeeMsat             sql.NullInt64
3156
        Policy1MinHtlcMsat             sql.NullInt64
3157
        Policy1MaxHtlcMsat             sql.NullInt64
3158
        Policy1Disabled                sql.NullBool
3159
        Policy1InboundBaseFeeMsat      sql.NullInt64
3160
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3161
        Policy1MessageFlags            sql.NullInt16
3162
        Policy1ChannelFlags            sql.NullInt16
3163
        Policy2Timelock                sql.NullInt32
3164
        Policy2FeePpm                  sql.NullInt64
3165
        Policy2BaseFeeMsat             sql.NullInt64
3166
        Policy2MinHtlcMsat             sql.NullInt64
3167
        Policy2MaxHtlcMsat             sql.NullInt64
3168
        Policy2Disabled                sql.NullBool
3169
        Policy2InboundBaseFeeMsat      sql.NullInt64
3170
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3171
        Policy2MessageFlags            sql.NullInt16
3172
        Policy2ChannelFlags            sql.NullInt16
3173
}
3174

NEW
3175
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
NEW
3176
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
NEW
3177
        if err != nil {
×
NEW
3178
                return nil, err
×
NEW
3179
        }
×
NEW
3180
        defer rows.Close()
×
NEW
3181
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
NEW
3182
        for rows.Next() {
×
NEW
3183
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
NEW
3184
                if err := rows.Scan(
×
NEW
3185
                        &i.ID,
×
NEW
3186
                        &i.Scid,
×
NEW
3187
                        &i.Capacity,
×
NEW
3188
                        &i.Node1Pubkey,
×
NEW
3189
                        &i.Node2Pubkey,
×
NEW
3190
                        &i.Policy1Timelock,
×
NEW
3191
                        &i.Policy1FeePpm,
×
NEW
3192
                        &i.Policy1BaseFeeMsat,
×
NEW
3193
                        &i.Policy1MinHtlcMsat,
×
NEW
3194
                        &i.Policy1MaxHtlcMsat,
×
NEW
3195
                        &i.Policy1Disabled,
×
NEW
3196
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
3197
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
3198
                        &i.Policy1MessageFlags,
×
NEW
3199
                        &i.Policy1ChannelFlags,
×
NEW
3200
                        &i.Policy2Timelock,
×
NEW
3201
                        &i.Policy2FeePpm,
×
NEW
3202
                        &i.Policy2BaseFeeMsat,
×
NEW
3203
                        &i.Policy2MinHtlcMsat,
×
NEW
3204
                        &i.Policy2MaxHtlcMsat,
×
NEW
3205
                        &i.Policy2Disabled,
×
NEW
3206
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
3207
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
3208
                        &i.Policy2MessageFlags,
×
NEW
3209
                        &i.Policy2ChannelFlags,
×
NEW
3210
                ); err != nil {
×
NEW
3211
                        return nil, err
×
NEW
3212
                }
×
NEW
3213
                items = append(items, i)
×
3214
        }
NEW
3215
        if err := rows.Close(); err != nil {
×
NEW
3216
                return nil, err
×
NEW
3217
        }
×
NEW
3218
        if err := rows.Err(); err != nil {
×
NEW
3219
                return nil, err
×
NEW
3220
        }
×
NEW
3221
        return items, nil
×
3222
}
3223

3224
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3225
SELECT
3226
    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,
3227

3228
    -- Join node pubkeys
3229
    n1.pub_key AS node1_pubkey,
3230
    n2.pub_key AS node2_pubkey,
3231

3232
    -- Node 1 policy
3233
    cp1.id AS policy_1_id,
3234
    cp1.node_id AS policy_1_node_id,
3235
    cp1.version AS policy_1_version,
3236
    cp1.timelock AS policy_1_timelock,
3237
    cp1.fee_ppm AS policy_1_fee_ppm,
3238
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3239
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3240
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3241
    cp1.last_update AS policy_1_last_update,
3242
    cp1.disabled AS policy_1_disabled,
3243
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3244
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3245
    cp1.message_flags AS policy1_message_flags,
3246
    cp1.channel_flags AS policy1_channel_flags,
3247
    cp1.signature AS policy_1_signature,
3248

3249
    -- Node 2 policy
3250
    cp2.id AS policy_2_id,
3251
    cp2.node_id AS policy_2_node_id,
3252
    cp2.version AS policy_2_version,
3253
    cp2.timelock AS policy_2_timelock,
3254
    cp2.fee_ppm AS policy_2_fee_ppm,
3255
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3256
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3257
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3258
    cp2.last_update AS policy_2_last_update,
3259
    cp2.disabled AS policy_2_disabled,
3260
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3261
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3262
    cp2.message_flags AS policy2_message_flags,
3263
    cp2.channel_flags AS policy2_channel_flags,
3264
    cp2.signature AS policy_2_signature
3265

3266
FROM graph_channels c
3267
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3268
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3269
LEFT JOIN graph_channel_policies cp1
3270
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3271
LEFT JOIN graph_channel_policies cp2
3272
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3273
WHERE c.version = $1 AND c.id > $2
3274
ORDER BY c.id
3275
LIMIT $3
3276
`
3277

3278
type ListChannelsWithPoliciesPaginatedParams struct {
3279
        Version int16
3280
        ID      int64
3281
        Limit   int32
3282
}
3283

3284
type ListChannelsWithPoliciesPaginatedRow struct {
3285
        GraphChannel                   GraphChannel
3286
        Node1Pubkey                    []byte
3287
        Node2Pubkey                    []byte
3288
        Policy1ID                      sql.NullInt64
3289
        Policy1NodeID                  sql.NullInt64
3290
        Policy1Version                 sql.NullInt16
3291
        Policy1Timelock                sql.NullInt32
3292
        Policy1FeePpm                  sql.NullInt64
3293
        Policy1BaseFeeMsat             sql.NullInt64
3294
        Policy1MinHtlcMsat             sql.NullInt64
3295
        Policy1MaxHtlcMsat             sql.NullInt64
3296
        Policy1LastUpdate              sql.NullInt64
3297
        Policy1Disabled                sql.NullBool
3298
        Policy1InboundBaseFeeMsat      sql.NullInt64
3299
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3300
        Policy1MessageFlags            sql.NullInt16
3301
        Policy1ChannelFlags            sql.NullInt16
3302
        Policy1Signature               []byte
3303
        Policy2ID                      sql.NullInt64
3304
        Policy2NodeID                  sql.NullInt64
3305
        Policy2Version                 sql.NullInt16
3306
        Policy2Timelock                sql.NullInt32
3307
        Policy2FeePpm                  sql.NullInt64
3308
        Policy2BaseFeeMsat             sql.NullInt64
3309
        Policy2MinHtlcMsat             sql.NullInt64
3310
        Policy2MaxHtlcMsat             sql.NullInt64
3311
        Policy2LastUpdate              sql.NullInt64
3312
        Policy2Disabled                sql.NullBool
3313
        Policy2InboundBaseFeeMsat      sql.NullInt64
3314
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3315
        Policy2MessageFlags            sql.NullInt16
3316
        Policy2ChannelFlags            sql.NullInt16
3317
        Policy2Signature               []byte
3318
}
3319

NEW
3320
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
NEW
3321
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
NEW
3322
        if err != nil {
×
NEW
3323
                return nil, err
×
NEW
3324
        }
×
NEW
3325
        defer rows.Close()
×
NEW
3326
        var items []ListChannelsWithPoliciesPaginatedRow
×
NEW
3327
        for rows.Next() {
×
NEW
3328
                var i ListChannelsWithPoliciesPaginatedRow
×
NEW
3329
                if err := rows.Scan(
×
NEW
3330
                        &i.GraphChannel.ID,
×
NEW
3331
                        &i.GraphChannel.Version,
×
NEW
3332
                        &i.GraphChannel.Scid,
×
NEW
3333
                        &i.GraphChannel.NodeID1,
×
NEW
3334
                        &i.GraphChannel.NodeID2,
×
NEW
3335
                        &i.GraphChannel.Outpoint,
×
NEW
3336
                        &i.GraphChannel.Capacity,
×
NEW
3337
                        &i.GraphChannel.BitcoinKey1,
×
NEW
3338
                        &i.GraphChannel.BitcoinKey2,
×
NEW
3339
                        &i.GraphChannel.Node1Signature,
×
NEW
3340
                        &i.GraphChannel.Node2Signature,
×
NEW
3341
                        &i.GraphChannel.Bitcoin1Signature,
×
NEW
3342
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
3343
                        &i.Node1Pubkey,
×
NEW
3344
                        &i.Node2Pubkey,
×
NEW
3345
                        &i.Policy1ID,
×
NEW
3346
                        &i.Policy1NodeID,
×
NEW
3347
                        &i.Policy1Version,
×
NEW
3348
                        &i.Policy1Timelock,
×
NEW
3349
                        &i.Policy1FeePpm,
×
NEW
3350
                        &i.Policy1BaseFeeMsat,
×
NEW
3351
                        &i.Policy1MinHtlcMsat,
×
NEW
3352
                        &i.Policy1MaxHtlcMsat,
×
NEW
3353
                        &i.Policy1LastUpdate,
×
NEW
3354
                        &i.Policy1Disabled,
×
NEW
3355
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
3356
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
3357
                        &i.Policy1MessageFlags,
×
NEW
3358
                        &i.Policy1ChannelFlags,
×
NEW
3359
                        &i.Policy1Signature,
×
NEW
3360
                        &i.Policy2ID,
×
NEW
3361
                        &i.Policy2NodeID,
×
NEW
3362
                        &i.Policy2Version,
×
NEW
3363
                        &i.Policy2Timelock,
×
NEW
3364
                        &i.Policy2FeePpm,
×
NEW
3365
                        &i.Policy2BaseFeeMsat,
×
NEW
3366
                        &i.Policy2MinHtlcMsat,
×
NEW
3367
                        &i.Policy2MaxHtlcMsat,
×
NEW
3368
                        &i.Policy2LastUpdate,
×
NEW
3369
                        &i.Policy2Disabled,
×
NEW
3370
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
3371
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
3372
                        &i.Policy2MessageFlags,
×
NEW
3373
                        &i.Policy2ChannelFlags,
×
NEW
3374
                        &i.Policy2Signature,
×
NEW
3375
                ); err != nil {
×
NEW
3376
                        return nil, err
×
NEW
3377
                }
×
NEW
3378
                items = append(items, i)
×
3379
        }
NEW
3380
        if err := rows.Close(); err != nil {
×
NEW
3381
                return nil, err
×
NEW
3382
        }
×
NEW
3383
        if err := rows.Err(); err != nil {
×
NEW
3384
                return nil, err
×
NEW
3385
        }
×
NEW
3386
        return items, nil
×
3387
}
3388

3389
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3390
SELECT id, pub_key
3391
FROM graph_nodes
3392
WHERE version = $1  AND id > $2
3393
ORDER BY id
3394
LIMIT $3
3395
`
3396

3397
type ListNodeIDsAndPubKeysParams struct {
3398
        Version int16
3399
        ID      int64
3400
        Limit   int32
3401
}
3402

3403
type ListNodeIDsAndPubKeysRow struct {
3404
        ID     int64
3405
        PubKey []byte
3406
}
3407

NEW
3408
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
NEW
3409
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
NEW
3410
        if err != nil {
×
NEW
3411
                return nil, err
×
NEW
3412
        }
×
NEW
3413
        defer rows.Close()
×
NEW
3414
        var items []ListNodeIDsAndPubKeysRow
×
NEW
3415
        for rows.Next() {
×
NEW
3416
                var i ListNodeIDsAndPubKeysRow
×
NEW
3417
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
NEW
3418
                        return nil, err
×
NEW
3419
                }
×
NEW
3420
                items = append(items, i)
×
3421
        }
NEW
3422
        if err := rows.Close(); err != nil {
×
NEW
3423
                return nil, err
×
NEW
3424
        }
×
NEW
3425
        if err := rows.Err(); err != nil {
×
NEW
3426
                return nil, err
×
NEW
3427
        }
×
NEW
3428
        return items, nil
×
3429
}
3430

3431
const listNodesPaginated = `-- name: ListNodesPaginated :many
3432
SELECT id, version, pub_key, alias, last_update, color, signature
3433
FROM graph_nodes
3434
WHERE version = $1 AND id > $2
3435
ORDER BY id
3436
LIMIT $3
3437
`
3438

3439
type ListNodesPaginatedParams struct {
3440
        Version int16
3441
        ID      int64
3442
        Limit   int32
3443
}
3444

NEW
3445
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
NEW
3446
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
NEW
3447
        if err != nil {
×
NEW
3448
                return nil, err
×
NEW
3449
        }
×
NEW
3450
        defer rows.Close()
×
NEW
3451
        var items []GraphNode
×
NEW
3452
        for rows.Next() {
×
NEW
3453
                var i GraphNode
×
NEW
3454
                if err := rows.Scan(
×
NEW
3455
                        &i.ID,
×
NEW
3456
                        &i.Version,
×
NEW
3457
                        &i.PubKey,
×
NEW
3458
                        &i.Alias,
×
NEW
3459
                        &i.LastUpdate,
×
NEW
3460
                        &i.Color,
×
NEW
3461
                        &i.Signature,
×
NEW
3462
                ); err != nil {
×
NEW
3463
                        return nil, err
×
NEW
3464
                }
×
NEW
3465
                items = append(items, i)
×
3466
        }
NEW
3467
        if err := rows.Close(); err != nil {
×
NEW
3468
                return nil, err
×
NEW
3469
        }
×
NEW
3470
        if err := rows.Err(); err != nil {
×
NEW
3471
                return nil, err
×
NEW
3472
        }
×
NEW
3473
        return items, nil
×
3474
}
3475

3476
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3477
/* ─────────────────────────────────────────────
3478
   graph_channel_policy_extra_types table queries
3479
   ─────────────────────────────────────────────
3480
*/
3481

3482
INSERT INTO graph_channel_policy_extra_types (
3483
    channel_policy_id, type, value
3484
)
3485
VALUES ($1, $2, $3)
3486
ON CONFLICT (channel_policy_id, type)
3487
    -- If a conflict occurs on channel_policy_id and type, then we update the
3488
    -- value.
3489
    DO UPDATE SET value = EXCLUDED.value
3490
`
3491

3492
type UpsertChanPolicyExtraTypeParams struct {
3493
        ChannelPolicyID int64
3494
        Type            int64
3495
        Value           []byte
3496
}
3497

NEW
3498
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
NEW
3499
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
NEW
3500
        return err
×
NEW
3501
}
×
3502

3503
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3504
/* ─────────────────────────────────────────────
3505
   graph_channel_extra_types table queries
3506
   ─────────────────────────────────────────────
3507
*/
3508

3509
INSERT INTO graph_channel_extra_types (
3510
    channel_id, type, value
3511
)
3512
VALUES ($1, $2, $3)
3513
    ON CONFLICT (channel_id, type)
3514
    -- Update the value if a conflict occurs on channel_id and type.
3515
    DO UPDATE SET value = EXCLUDED.value
3516
`
3517

3518
type UpsertChannelExtraTypeParams struct {
3519
        ChannelID int64
3520
        Type      int64
3521
        Value     []byte
3522
}
3523

NEW
3524
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
NEW
3525
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
NEW
3526
        return err
×
NEW
3527
}
×
3528

3529
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3530
/* ─────────────────────────────────────────────
3531
   graph_channel_policies table queries
3532
   ─────────────────────────────────────────────
3533
*/
3534

3535
INSERT INTO graph_channel_policies (
3536
    version, channel_id, node_id, timelock, fee_ppm,
3537
    base_fee_msat, min_htlc_msat, last_update, disabled,
3538
    max_htlc_msat, inbound_base_fee_msat,
3539
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3540
    signature
3541
) VALUES  (
3542
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3543
)
3544
ON CONFLICT (channel_id, node_id, version)
3545
    -- Update the following fields if a conflict occurs on channel_id,
3546
    -- node_id, and version.
3547
    DO UPDATE SET
3548
        timelock = EXCLUDED.timelock,
3549
        fee_ppm = EXCLUDED.fee_ppm,
3550
        base_fee_msat = EXCLUDED.base_fee_msat,
3551
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3552
        last_update = EXCLUDED.last_update,
3553
        disabled = EXCLUDED.disabled,
3554
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3555
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3556
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3557
        message_flags = EXCLUDED.message_flags,
3558
        channel_flags = EXCLUDED.channel_flags,
3559
        signature = EXCLUDED.signature
3560
WHERE EXCLUDED.last_update > graph_channel_policies.last_update
3561
RETURNING id
3562
`
3563

3564
type UpsertEdgePolicyParams struct {
3565
        Version                 int16
3566
        ChannelID               int64
3567
        NodeID                  int64
3568
        Timelock                int32
3569
        FeePpm                  int64
3570
        BaseFeeMsat             int64
3571
        MinHtlcMsat             int64
3572
        LastUpdate              sql.NullInt64
3573
        Disabled                sql.NullBool
3574
        MaxHtlcMsat             sql.NullInt64
3575
        InboundBaseFeeMsat      sql.NullInt64
3576
        InboundFeeRateMilliMsat sql.NullInt64
3577
        MessageFlags            sql.NullInt16
3578
        ChannelFlags            sql.NullInt16
3579
        Signature               []byte
3580
}
3581

NEW
3582
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
NEW
3583
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
NEW
3584
                arg.Version,
×
NEW
3585
                arg.ChannelID,
×
NEW
3586
                arg.NodeID,
×
NEW
3587
                arg.Timelock,
×
NEW
3588
                arg.FeePpm,
×
NEW
3589
                arg.BaseFeeMsat,
×
NEW
3590
                arg.MinHtlcMsat,
×
NEW
3591
                arg.LastUpdate,
×
NEW
3592
                arg.Disabled,
×
NEW
3593
                arg.MaxHtlcMsat,
×
NEW
3594
                arg.InboundBaseFeeMsat,
×
NEW
3595
                arg.InboundFeeRateMilliMsat,
×
NEW
3596
                arg.MessageFlags,
×
NEW
3597
                arg.ChannelFlags,
×
NEW
3598
                arg.Signature,
×
NEW
3599
        )
×
NEW
3600
        var id int64
×
NEW
3601
        err := row.Scan(&id)
×
NEW
3602
        return id, err
×
NEW
3603
}
×
3604

3605
const upsertNode = `-- name: UpsertNode :one
3606
/* ─────────────────────────────────────────────
3607
   graph_nodes table queries
3608
   ───────────────────────────��─────────────────
3609
*/
3610

3611
INSERT INTO graph_nodes (
3612
    version, pub_key, alias, last_update, color, signature
3613
) VALUES (
3614
    $1, $2, $3, $4, $5, $6
3615
)
3616
ON CONFLICT (pub_key, version)
3617
    -- Update the following fields if a conflict occurs on pub_key
3618
    -- and version.
3619
    DO UPDATE SET
3620
        alias = EXCLUDED.alias,
3621
        last_update = EXCLUDED.last_update,
3622
        color = EXCLUDED.color,
3623
        signature = EXCLUDED.signature
3624
WHERE graph_nodes.last_update IS NULL
3625
    OR EXCLUDED.last_update > graph_nodes.last_update
3626
RETURNING id
3627
`
3628

3629
type UpsertNodeParams struct {
3630
        Version    int16
3631
        PubKey     []byte
3632
        Alias      sql.NullString
3633
        LastUpdate sql.NullInt64
3634
        Color      sql.NullString
3635
        Signature  []byte
3636
}
3637

NEW
3638
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
NEW
3639
        row := q.db.QueryRowContext(ctx, upsertNode,
×
NEW
3640
                arg.Version,
×
NEW
3641
                arg.PubKey,
×
NEW
3642
                arg.Alias,
×
NEW
3643
                arg.LastUpdate,
×
NEW
3644
                arg.Color,
×
NEW
3645
                arg.Signature,
×
NEW
3646
        )
×
NEW
3647
        var id int64
×
NEW
3648
        err := row.Scan(&id)
×
NEW
3649
        return id, err
×
NEW
3650
}
×
3651

3652
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3653
/* ─────────────────────────────────────────────
3654
   graph_node_addresses table queries
3655
   ───────────────────────────────────��─────────
3656
*/
3657

3658
INSERT INTO graph_node_addresses (
3659
    node_id,
3660
    type,
3661
    address,
3662
    position
3663
) VALUES (
3664
    $1, $2, $3, $4
3665
) ON CONFLICT (node_id, type, position)
3666
    DO UPDATE SET address = EXCLUDED.address
3667
`
3668

3669
type UpsertNodeAddressParams struct {
3670
        NodeID   int64
3671
        Type     int16
3672
        Address  string
3673
        Position int32
3674
}
3675

NEW
3676
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
NEW
3677
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
NEW
3678
                arg.NodeID,
×
NEW
3679
                arg.Type,
×
NEW
3680
                arg.Address,
×
NEW
3681
                arg.Position,
×
NEW
3682
        )
×
NEW
3683
        return err
×
NEW
3684
}
×
3685

3686
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3687
/* ─────────────────────────────────────────────
3688
   graph_node_extra_types table queries
3689
   ─────────────────────────────────────────────
3690
*/
3691

3692
INSERT INTO graph_node_extra_types (
3693
    node_id, type, value
3694
)
3695
VALUES ($1, $2, $3)
3696
ON CONFLICT (type, node_id)
3697
    -- Update the value if a conflict occurs on type
3698
    -- and node_id.
3699
    DO UPDATE SET value = EXCLUDED.value
3700
`
3701

3702
type UpsertNodeExtraTypeParams struct {
3703
        NodeID int64
3704
        Type   int64
3705
        Value  []byte
3706
}
3707

NEW
3708
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
NEW
3709
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
NEW
3710
        return err
×
NEW
3711
}
×
3712

3713
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3714
/* ───────────────────────────���─────────────────
3715
    graph_prune_log table queries
3716
    ─────────────────────────────────────────────
3717
*/
3718

3719
INSERT INTO graph_prune_log (
3720
    block_height, block_hash
3721
) VALUES (
3722
    $1, $2
3723
)
3724
ON CONFLICT(block_height) DO UPDATE SET
3725
    block_hash = EXCLUDED.block_hash
3726
`
3727

3728
type UpsertPruneLogEntryParams struct {
3729
        BlockHeight int64
3730
        BlockHash   []byte
3731
}
3732

NEW
3733
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
NEW
3734
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
NEW
3735
        return err
×
NEW
3736
}
×
3737

3738
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
3739
/* ─────────────────────────────────────────────
3740
   graph_zombie_channels table queries
3741
   ─────────────────────────────────────────────
3742
*/
3743

3744
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
3745
VALUES ($1, $2, $3, $4)
3746
ON CONFLICT (scid, version)
3747
DO UPDATE SET
3748
    -- If a conflict exists for the SCID and version pair, then we
3749
    -- update the node keys.
3750
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
3751
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
3752
`
3753

3754
type UpsertZombieChannelParams struct {
3755
        Scid     []byte
3756
        Version  int16
3757
        NodeKey1 []byte
3758
        NodeKey2 []byte
3759
}
3760

NEW
3761
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
NEW
3762
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
NEW
3763
                arg.Scid,
×
NEW
3764
                arg.Version,
×
NEW
3765
                arg.NodeKey1,
×
NEW
3766
                arg.NodeKey2,
×
NEW
3767
        )
×
NEW
3768
        return err
×
NEW
3769
}
×
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

© 2025 Coveralls, Inc