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

lightningnetwork / lnd / 15758703842

19 Jun 2025 01:11PM UTC coverage: 67.933% (-0.2%) from 68.161%
15758703842

Pull #9936

github

web-flow
Merge 2ee302abf into e0a9705d5
Pull Request #9936: [12] graph/db: Implement more graph SQLStore methods

0 of 651 new or added lines in 2 files covered. (0.0%)

71 existing lines in 21 files now uncovered.

134493 of 197979 relevant lines covered (67.93%)

22115.6 hits per line

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

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

6
package sqlc
7

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

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

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

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

29
const createChannel = `-- name: CreateChannel :one
30
/* ─────────────────────────────────────────────
31
   channels table queries
32
   ─────────────────────────────────────────────
33
*/
34

35
INSERT INTO channels (
36
    version, scid, node_id_1, node_id_2,
37
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
38
    node_1_signature, node_2_signature, bitcoin_1_signature,
39
    bitcoin_2_signature
40
) VALUES (
41
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
42
)
43
RETURNING id
44
`
45

46
type CreateChannelParams struct {
47
        Version           int16
48
        Scid              []byte
49
        NodeID1           int64
50
        NodeID2           int64
51
        Outpoint          string
52
        Capacity          sql.NullInt64
53
        BitcoinKey1       []byte
54
        BitcoinKey2       []byte
55
        Node1Signature    []byte
56
        Node2Signature    []byte
57
        Bitcoin1Signature []byte
58
        Bitcoin2Signature []byte
59
}
60

61
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (int64, error) {
×
62
        row := q.db.QueryRowContext(ctx, createChannel,
×
63
                arg.Version,
×
64
                arg.Scid,
×
65
                arg.NodeID1,
×
66
                arg.NodeID2,
×
67
                arg.Outpoint,
×
68
                arg.Capacity,
×
69
                arg.BitcoinKey1,
×
70
                arg.BitcoinKey2,
×
71
                arg.Node1Signature,
×
72
                arg.Node2Signature,
×
73
                arg.Bitcoin1Signature,
×
74
                arg.Bitcoin2Signature,
×
75
        )
×
76
        var id int64
×
77
        err := row.Scan(&id)
×
78
        return id, err
×
79
}
×
80

81
const createChannelExtraType = `-- name: CreateChannelExtraType :exec
82
/* ─────────────────────────────────────────────
83
   channel_extra_types table queries
84
   ─────────────────────────────────────────────
85
*/
86

87
INSERT INTO channel_extra_types (
88
    channel_id, type, value
89
)
90
VALUES ($1, $2, $3)
91
`
92

93
type CreateChannelExtraTypeParams struct {
94
        ChannelID int64
95
        Type      int64
96
        Value     []byte
97
}
98

99
func (q *Queries) CreateChannelExtraType(ctx context.Context, arg CreateChannelExtraTypeParams) error {
×
100
        _, err := q.db.ExecContext(ctx, createChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
101
        return err
×
102
}
×
103

104
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
105
DELETE FROM channel_policy_extra_types
106
WHERE channel_policy_id = $1
107
`
108

109
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
110
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
111
        return err
×
112
}
×
113

114
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
115
DELETE FROM node_extra_types
116
WHERE node_id = $1
117
  AND type = $2
118
`
119

120
type DeleteExtraNodeTypeParams struct {
121
        NodeID int64
122
        Type   int64
123
}
124

125
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
126
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
127
        return err
×
128
}
×
129

130
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
131
DELETE FROM node_addresses
132
WHERE node_id = $1
133
`
134

135
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
136
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
137
        return err
×
138
}
×
139

140
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
141
DELETE FROM nodes
142
WHERE pub_key = $1
143
  AND version = $2
144
`
145

146
type DeleteNodeByPubKeyParams struct {
147
        PubKey  []byte
148
        Version int16
149
}
150

151
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
152
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
153
}
×
154

155
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
156
DELETE FROM node_features
157
WHERE node_id = $1
158
  AND feature_bit = $2
159
`
160

161
type DeleteNodeFeatureParams struct {
162
        NodeID     int64
163
        FeatureBit int32
164
}
165

166
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
167
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
168
        return err
×
169
}
×
170

171
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
172
SELECT
173
    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,
174
    n1.pub_key AS node1_pub_key,
175
    n2.pub_key AS node2_pub_key
176
FROM channels c
177
    JOIN nodes n1 ON c.node_id_1 = n1.id
178
    JOIN nodes n2 ON c.node_id_2 = n2.id
179
WHERE c.scid = $1
180
  AND c.version = $2
181
`
182

183
type GetChannelAndNodesBySCIDParams struct {
184
        Scid    []byte
185
        Version int16
186
}
187

188
type GetChannelAndNodesBySCIDRow struct {
189
        ID                int64
190
        Version           int16
191
        Scid              []byte
192
        NodeID1           int64
193
        NodeID2           int64
194
        Outpoint          string
195
        Capacity          sql.NullInt64
196
        BitcoinKey1       []byte
197
        BitcoinKey2       []byte
198
        Node1Signature    []byte
199
        Node2Signature    []byte
200
        Bitcoin1Signature []byte
201
        Bitcoin2Signature []byte
202
        Node1PubKey       []byte
203
        Node2PubKey       []byte
204
}
205

206
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
207
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
208
        var i GetChannelAndNodesBySCIDRow
×
209
        err := row.Scan(
×
210
                &i.ID,
×
211
                &i.Version,
×
212
                &i.Scid,
×
213
                &i.NodeID1,
×
214
                &i.NodeID2,
×
215
                &i.Outpoint,
×
216
                &i.Capacity,
×
217
                &i.BitcoinKey1,
×
218
                &i.BitcoinKey2,
×
219
                &i.Node1Signature,
×
220
                &i.Node2Signature,
×
221
                &i.Bitcoin1Signature,
×
222
                &i.Bitcoin2Signature,
×
223
                &i.Node1PubKey,
×
224
                &i.Node2PubKey,
×
225
        )
×
226
        return i, err
×
227
}
×
228

229
const getChannelBySCID = `-- name: GetChannelBySCID :one
230
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 channels
231
WHERE scid = $1 AND version = $2
232
`
233

234
type GetChannelBySCIDParams struct {
235
        Scid    []byte
236
        Version int16
237
}
238

239
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (Channel, error) {
×
240
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
241
        var i Channel
×
242
        err := row.Scan(
×
243
                &i.ID,
×
244
                &i.Version,
×
245
                &i.Scid,
×
246
                &i.NodeID1,
×
247
                &i.NodeID2,
×
248
                &i.Outpoint,
×
249
                &i.Capacity,
×
250
                &i.BitcoinKey1,
×
251
                &i.BitcoinKey2,
×
252
                &i.Node1Signature,
×
253
                &i.Node2Signature,
×
254
                &i.Bitcoin1Signature,
×
255
                &i.Bitcoin2Signature,
×
256
        )
×
257
        return i, err
×
258
}
×
259

260
const getChannelFeaturesAndExtras = `-- name: GetChannelFeaturesAndExtras :many
261
SELECT
262
    cf.channel_id,
263
    true AS is_feature,
264
    cf.feature_bit AS feature_bit,
265
    NULL AS extra_key,
266
    NULL AS value
267
FROM channel_features cf
268
WHERE cf.channel_id = $1
269

270
UNION ALL
271

272
SELECT
273
    cet.channel_id,
274
    false AS is_feature,
275
    0 AS feature_bit,
276
    cet.type AS extra_key,
277
    cet.value AS value
278
FROM channel_extra_types cet
279
WHERE cet.channel_id = $1
280
`
281

282
type GetChannelFeaturesAndExtrasRow struct {
283
        ChannelID  int64
284
        IsFeature  bool
285
        FeatureBit int32
286
        ExtraKey   interface{}
287
        Value      interface{}
288
}
289

290
func (q *Queries) GetChannelFeaturesAndExtras(ctx context.Context, channelID int64) ([]GetChannelFeaturesAndExtrasRow, error) {
×
291
        rows, err := q.db.QueryContext(ctx, getChannelFeaturesAndExtras, channelID)
×
292
        if err != nil {
×
293
                return nil, err
×
294
        }
×
295
        defer rows.Close()
×
296
        var items []GetChannelFeaturesAndExtrasRow
×
297
        for rows.Next() {
×
298
                var i GetChannelFeaturesAndExtrasRow
×
299
                if err := rows.Scan(
×
300
                        &i.ChannelID,
×
301
                        &i.IsFeature,
×
302
                        &i.FeatureBit,
×
303
                        &i.ExtraKey,
×
304
                        &i.Value,
×
305
                ); err != nil {
×
306
                        return nil, err
×
307
                }
×
308
                items = append(items, i)
×
309
        }
310
        if err := rows.Close(); err != nil {
×
311
                return nil, err
×
312
        }
×
313
        if err := rows.Err(); err != nil {
×
314
                return nil, err
×
315
        }
×
316
        return items, nil
×
317
}
318

319
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
320
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, signature
321
FROM channel_policies
322
WHERE channel_id = $1
323
  AND node_id = $2
324
  AND version = $3
325
`
326

327
type GetChannelPolicyByChannelAndNodeParams struct {
328
        ChannelID int64
329
        NodeID    int64
330
        Version   int16
331
}
332

NEW
333
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (ChannelPolicy, error) {
×
NEW
334
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
NEW
335
        var i ChannelPolicy
×
NEW
336
        err := row.Scan(
×
NEW
337
                &i.ID,
×
NEW
338
                &i.Version,
×
NEW
339
                &i.ChannelID,
×
NEW
340
                &i.NodeID,
×
NEW
341
                &i.Timelock,
×
NEW
342
                &i.FeePpm,
×
NEW
343
                &i.BaseFeeMsat,
×
NEW
344
                &i.MinHtlcMsat,
×
NEW
345
                &i.MaxHtlcMsat,
×
NEW
346
                &i.LastUpdate,
×
NEW
347
                &i.Disabled,
×
NEW
348
                &i.InboundBaseFeeMsat,
×
NEW
349
                &i.InboundFeeRateMilliMsat,
×
NEW
350
                &i.Signature,
×
NEW
351
        )
×
NEW
352
        return i, err
×
NEW
353
}
×
354

355
const getChannelPolicyExtraTypes = `-- name: GetChannelPolicyExtraTypes :many
356
SELECT
357
    cp.id AS policy_id,
358
    cp.channel_id,
359
    cp.node_id,
360
    cpet.type,
361
    cpet.value
362
FROM channel_policies cp
363
JOIN channel_policy_extra_types cpet
364
ON cp.id = cpet.channel_policy_id
365
WHERE cp.id = $1 OR cp.id = $2
366
`
367

368
type GetChannelPolicyExtraTypesParams struct {
369
        ID   int64
370
        ID_2 int64
371
}
372

373
type GetChannelPolicyExtraTypesRow struct {
374
        PolicyID  int64
375
        ChannelID int64
376
        NodeID    int64
377
        Type      int64
378
        Value     []byte
379
}
380

381
func (q *Queries) GetChannelPolicyExtraTypes(ctx context.Context, arg GetChannelPolicyExtraTypesParams) ([]GetChannelPolicyExtraTypesRow, error) {
×
382
        rows, err := q.db.QueryContext(ctx, getChannelPolicyExtraTypes, arg.ID, arg.ID_2)
×
383
        if err != nil {
×
384
                return nil, err
×
385
        }
×
386
        defer rows.Close()
×
387
        var items []GetChannelPolicyExtraTypesRow
×
388
        for rows.Next() {
×
389
                var i GetChannelPolicyExtraTypesRow
×
390
                if err := rows.Scan(
×
391
                        &i.PolicyID,
×
392
                        &i.ChannelID,
×
393
                        &i.NodeID,
×
394
                        &i.Type,
×
395
                        &i.Value,
×
396
                ); err != nil {
×
397
                        return nil, err
×
398
                }
×
399
                items = append(items, i)
×
400
        }
401
        if err := rows.Close(); err != nil {
×
402
                return nil, err
×
403
        }
×
404
        if err := rows.Err(); err != nil {
×
405
                return nil, err
×
406
        }
×
407
        return items, nil
×
408
}
409

410
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
411
SELECT
412
    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,
413
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
414
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
415

416
    -- Policy 1 (node_id_1)
417
    cp1.id AS policy1_id,
418
    cp1.node_id AS policy1_node_id,
419
    cp1.version AS policy1_version,
420
    cp1.timelock AS policy1_timelock,
421
    cp1.fee_ppm AS policy1_fee_ppm,
422
    cp1.base_fee_msat AS policy1_base_fee_msat,
423
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
424
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
425
    cp1.last_update AS policy1_last_update,
426
    cp1.disabled AS policy1_disabled,
427
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
428
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
429
    cp1.signature AS policy1_signature,
430

431
    -- Policy 2 (node_id_2)
432
    cp2.id AS policy2_id,
433
    cp2.node_id AS policy2_node_id,
434
    cp2.version AS policy2_version,
435
    cp2.timelock AS policy2_timelock,
436
    cp2.fee_ppm AS policy2_fee_ppm,
437
    cp2.base_fee_msat AS policy2_base_fee_msat,
438
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
439
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
440
    cp2.last_update AS policy2_last_update,
441
    cp2.disabled AS policy2_disabled,
442
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
443
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
444
    cp2.signature AS policy2_signature
445

446
FROM channels c
447
    JOIN nodes n1 ON c.node_id_1 = n1.id
448
    JOIN nodes n2 ON c.node_id_2 = n2.id
449
    LEFT JOIN channel_policies cp1
450
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
451
    LEFT JOIN channel_policies cp2
452
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
453
WHERE c.version = $1
454
  AND (cp1.last_update >= $2 AND cp1.last_update < $3
455
    OR cp2.last_update >= $2 AND cp2.last_update < $3)
456
ORDER BY
457
    CASE
458
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
459
            THEN COALESCE(cp1.last_update, 0)
460
        ELSE COALESCE(cp2.last_update, 0)
461
        END ASC
462
`
463

464
type GetChannelsByPolicyLastUpdateRangeParams struct {
465
        Version   int16
466
        StartTime sql.NullInt64
467
        EndTime   sql.NullInt64
468
}
469

470
type GetChannelsByPolicyLastUpdateRangeRow struct {
471
        Channel                        Channel
472
        Node                           Node
473
        Node_2                         Node
474
        Policy1ID                      sql.NullInt64
475
        Policy1NodeID                  sql.NullInt64
476
        Policy1Version                 sql.NullInt16
477
        Policy1Timelock                sql.NullInt32
478
        Policy1FeePpm                  sql.NullInt64
479
        Policy1BaseFeeMsat             sql.NullInt64
480
        Policy1MinHtlcMsat             sql.NullInt64
481
        Policy1MaxHtlcMsat             sql.NullInt64
482
        Policy1LastUpdate              sql.NullInt64
483
        Policy1Disabled                sql.NullBool
484
        Policy1InboundBaseFeeMsat      sql.NullInt64
485
        Policy1InboundFeeRateMilliMsat sql.NullInt64
486
        Policy1Signature               []byte
487
        Policy2ID                      sql.NullInt64
488
        Policy2NodeID                  sql.NullInt64
489
        Policy2Version                 sql.NullInt16
490
        Policy2Timelock                sql.NullInt32
491
        Policy2FeePpm                  sql.NullInt64
492
        Policy2BaseFeeMsat             sql.NullInt64
493
        Policy2MinHtlcMsat             sql.NullInt64
494
        Policy2MaxHtlcMsat             sql.NullInt64
495
        Policy2LastUpdate              sql.NullInt64
496
        Policy2Disabled                sql.NullBool
497
        Policy2InboundBaseFeeMsat      sql.NullInt64
498
        Policy2InboundFeeRateMilliMsat sql.NullInt64
499
        Policy2Signature               []byte
500
}
501

NEW
502
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
NEW
503
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange, arg.Version, arg.StartTime, arg.EndTime)
×
NEW
504
        if err != nil {
×
NEW
505
                return nil, err
×
NEW
506
        }
×
NEW
507
        defer rows.Close()
×
NEW
508
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
NEW
509
        for rows.Next() {
×
NEW
510
                var i GetChannelsByPolicyLastUpdateRangeRow
×
NEW
511
                if err := rows.Scan(
×
NEW
512
                        &i.Channel.ID,
×
NEW
513
                        &i.Channel.Version,
×
NEW
514
                        &i.Channel.Scid,
×
NEW
515
                        &i.Channel.NodeID1,
×
NEW
516
                        &i.Channel.NodeID2,
×
NEW
517
                        &i.Channel.Outpoint,
×
NEW
518
                        &i.Channel.Capacity,
×
NEW
519
                        &i.Channel.BitcoinKey1,
×
NEW
520
                        &i.Channel.BitcoinKey2,
×
NEW
521
                        &i.Channel.Node1Signature,
×
NEW
522
                        &i.Channel.Node2Signature,
×
NEW
523
                        &i.Channel.Bitcoin1Signature,
×
NEW
524
                        &i.Channel.Bitcoin2Signature,
×
NEW
525
                        &i.Node.ID,
×
NEW
526
                        &i.Node.Version,
×
NEW
527
                        &i.Node.PubKey,
×
NEW
528
                        &i.Node.Alias,
×
NEW
529
                        &i.Node.LastUpdate,
×
NEW
530
                        &i.Node.Color,
×
NEW
531
                        &i.Node.Signature,
×
NEW
532
                        &i.Node_2.ID,
×
NEW
533
                        &i.Node_2.Version,
×
NEW
534
                        &i.Node_2.PubKey,
×
NEW
535
                        &i.Node_2.Alias,
×
NEW
536
                        &i.Node_2.LastUpdate,
×
NEW
537
                        &i.Node_2.Color,
×
NEW
538
                        &i.Node_2.Signature,
×
NEW
539
                        &i.Policy1ID,
×
NEW
540
                        &i.Policy1NodeID,
×
NEW
541
                        &i.Policy1Version,
×
NEW
542
                        &i.Policy1Timelock,
×
NEW
543
                        &i.Policy1FeePpm,
×
NEW
544
                        &i.Policy1BaseFeeMsat,
×
NEW
545
                        &i.Policy1MinHtlcMsat,
×
NEW
546
                        &i.Policy1MaxHtlcMsat,
×
NEW
547
                        &i.Policy1LastUpdate,
×
NEW
548
                        &i.Policy1Disabled,
×
NEW
549
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
550
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
551
                        &i.Policy1Signature,
×
NEW
552
                        &i.Policy2ID,
×
NEW
553
                        &i.Policy2NodeID,
×
NEW
554
                        &i.Policy2Version,
×
NEW
555
                        &i.Policy2Timelock,
×
NEW
556
                        &i.Policy2FeePpm,
×
NEW
557
                        &i.Policy2BaseFeeMsat,
×
NEW
558
                        &i.Policy2MinHtlcMsat,
×
NEW
559
                        &i.Policy2MaxHtlcMsat,
×
NEW
560
                        &i.Policy2LastUpdate,
×
NEW
561
                        &i.Policy2Disabled,
×
NEW
562
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
563
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
564
                        &i.Policy2Signature,
×
NEW
565
                ); err != nil {
×
NEW
566
                        return nil, err
×
NEW
567
                }
×
NEW
568
                items = append(items, i)
×
569
        }
NEW
570
        if err := rows.Close(); err != nil {
×
NEW
571
                return nil, err
×
NEW
572
        }
×
NEW
573
        if err := rows.Err(); err != nil {
×
NEW
574
                return nil, err
×
NEW
575
        }
×
NEW
576
        return items, nil
×
577
}
578

579
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
580
SELECT node_id, type, value
581
FROM node_extra_types
582
WHERE node_id = $1
583
`
584

585
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]NodeExtraType, error) {
×
586
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
587
        if err != nil {
×
588
                return nil, err
×
589
        }
×
590
        defer rows.Close()
×
591
        var items []NodeExtraType
×
592
        for rows.Next() {
×
593
                var i NodeExtraType
×
594
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
595
                        return nil, err
×
596
                }
×
597
                items = append(items, i)
×
598
        }
599
        if err := rows.Close(); err != nil {
×
600
                return nil, err
×
601
        }
×
602
        if err := rows.Err(); err != nil {
×
603
                return nil, err
×
604
        }
×
605
        return items, nil
×
606
}
607

608
const getNodeAddressesByPubKey = `-- name: GetNodeAddressesByPubKey :many
609
SELECT a.type, a.address
610
FROM nodes n
611
LEFT JOIN node_addresses a ON a.node_id = n.id
612
WHERE n.pub_key = $1 AND n.version = $2
613
ORDER BY a.type ASC, a.position ASC
614
`
615

616
type GetNodeAddressesByPubKeyParams struct {
617
        PubKey  []byte
618
        Version int16
619
}
620

621
type GetNodeAddressesByPubKeyRow struct {
622
        Type    sql.NullInt16
623
        Address sql.NullString
624
}
625

626
func (q *Queries) GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error) {
×
627
        rows, err := q.db.QueryContext(ctx, getNodeAddressesByPubKey, arg.PubKey, arg.Version)
×
628
        if err != nil {
×
629
                return nil, err
×
630
        }
×
631
        defer rows.Close()
×
632
        var items []GetNodeAddressesByPubKeyRow
×
633
        for rows.Next() {
×
634
                var i GetNodeAddressesByPubKeyRow
×
635
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
636
                        return nil, err
×
637
                }
×
638
                items = append(items, i)
×
639
        }
640
        if err := rows.Close(); err != nil {
×
641
                return nil, err
×
642
        }
×
643
        if err := rows.Err(); err != nil {
×
644
                return nil, err
×
645
        }
×
646
        return items, nil
×
647
}
648

649
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
650
SELECT id, version, pub_key, alias, last_update, color, signature
651
FROM nodes
652
WHERE pub_key = $1
653
  AND version = $2
654
`
655

656
type GetNodeByPubKeyParams struct {
657
        PubKey  []byte
658
        Version int16
659
}
660

661
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (Node, error) {
×
662
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
663
        var i Node
×
664
        err := row.Scan(
×
665
                &i.ID,
×
666
                &i.Version,
×
667
                &i.PubKey,
×
668
                &i.Alias,
×
669
                &i.LastUpdate,
×
670
                &i.Color,
×
671
                &i.Signature,
×
672
        )
×
673
        return i, err
×
674
}
×
675

676
const getNodeFeatures = `-- name: GetNodeFeatures :many
677
SELECT node_id, feature_bit
678
FROM node_features
679
WHERE node_id = $1
680
`
681

682
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]NodeFeature, error) {
×
683
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
684
        if err != nil {
×
685
                return nil, err
×
686
        }
×
687
        defer rows.Close()
×
688
        var items []NodeFeature
×
689
        for rows.Next() {
×
690
                var i NodeFeature
×
691
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
692
                        return nil, err
×
693
                }
×
694
                items = append(items, i)
×
695
        }
696
        if err := rows.Close(); err != nil {
×
697
                return nil, err
×
698
        }
×
699
        if err := rows.Err(); err != nil {
×
700
                return nil, err
×
701
        }
×
702
        return items, nil
×
703
}
704

705
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
706
SELECT f.feature_bit
707
FROM nodes n
708
    JOIN node_features f ON f.node_id = n.id
709
WHERE n.pub_key = $1
710
  AND n.version = $2
711
`
712

713
type GetNodeFeaturesByPubKeyParams struct {
714
        PubKey  []byte
715
        Version int16
716
}
717

718
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
719
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
720
        if err != nil {
×
721
                return nil, err
×
722
        }
×
723
        defer rows.Close()
×
724
        var items []int32
×
725
        for rows.Next() {
×
726
                var feature_bit int32
×
727
                if err := rows.Scan(&feature_bit); err != nil {
×
728
                        return nil, err
×
729
                }
×
730
                items = append(items, feature_bit)
×
731
        }
732
        if err := rows.Close(); err != nil {
×
733
                return nil, err
×
734
        }
×
735
        if err := rows.Err(); err != nil {
×
736
                return nil, err
×
737
        }
×
738
        return items, nil
×
739
}
740

741
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
742
SELECT id
743
FROM nodes
744
WHERE pub_key = $1
745
  AND version = $2
746
`
747

748
type GetNodeIDByPubKeyParams struct {
749
        PubKey  []byte
750
        Version int16
751
}
752

753
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
754
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
755
        var id int64
×
756
        err := row.Scan(&id)
×
757
        return id, err
×
758
}
×
759

760
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
761
SELECT id, version, pub_key, alias, last_update, color, signature
762
FROM nodes
763
WHERE last_update >= $1
764
  AND last_update < $2
765
`
766

767
type GetNodesByLastUpdateRangeParams struct {
768
        StartTime sql.NullInt64
769
        EndTime   sql.NullInt64
770
}
771

772
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]Node, error) {
×
773
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange, arg.StartTime, arg.EndTime)
×
774
        if err != nil {
×
775
                return nil, err
×
776
        }
×
777
        defer rows.Close()
×
778
        var items []Node
×
779
        for rows.Next() {
×
780
                var i Node
×
781
                if err := rows.Scan(
×
782
                        &i.ID,
×
783
                        &i.Version,
×
784
                        &i.PubKey,
×
785
                        &i.Alias,
×
786
                        &i.LastUpdate,
×
787
                        &i.Color,
×
788
                        &i.Signature,
×
789
                ); err != nil {
×
790
                        return nil, err
×
791
                }
×
792
                items = append(items, i)
×
793
        }
794
        if err := rows.Close(); err != nil {
×
795
                return nil, err
×
796
        }
×
797
        if err := rows.Err(); err != nil {
×
798
                return nil, err
×
799
        }
×
800
        return items, nil
×
801
}
802

803
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
804
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
805
FROM channels
806
WHERE node_1_signature IS NOT NULL
807
  AND scid >= $1
808
  AND scid < $2
809
`
810

811
type GetPublicV1ChannelsBySCIDParams struct {
812
        StartScid []byte
813
        EndScid   []byte
814
}
815

NEW
816
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]Channel, error) {
×
NEW
817
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
NEW
818
        if err != nil {
×
NEW
819
                return nil, err
×
NEW
820
        }
×
NEW
821
        defer rows.Close()
×
NEW
822
        var items []Channel
×
NEW
823
        for rows.Next() {
×
NEW
824
                var i Channel
×
NEW
825
                if err := rows.Scan(
×
NEW
826
                        &i.ID,
×
NEW
827
                        &i.Version,
×
NEW
828
                        &i.Scid,
×
NEW
829
                        &i.NodeID1,
×
NEW
830
                        &i.NodeID2,
×
NEW
831
                        &i.Outpoint,
×
NEW
832
                        &i.Capacity,
×
NEW
833
                        &i.BitcoinKey1,
×
NEW
834
                        &i.BitcoinKey2,
×
NEW
835
                        &i.Node1Signature,
×
NEW
836
                        &i.Node2Signature,
×
NEW
837
                        &i.Bitcoin1Signature,
×
NEW
838
                        &i.Bitcoin2Signature,
×
NEW
839
                ); 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 getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
854
SELECT sn.node_id, n.pub_key
855
FROM source_nodes sn
856
    JOIN nodes n ON sn.node_id = n.id
857
WHERE n.version = $1
858
`
859

860
type GetSourceNodesByVersionRow struct {
861
        NodeID int64
862
        PubKey []byte
863
}
864

865
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
866
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
867
        if err != nil {
×
868
                return nil, err
×
869
        }
×
870
        defer rows.Close()
×
871
        var items []GetSourceNodesByVersionRow
×
872
        for rows.Next() {
×
873
                var i GetSourceNodesByVersionRow
×
874
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
875
                        return nil, err
×
876
                }
×
877
                items = append(items, i)
×
878
        }
879
        if err := rows.Close(); err != nil {
×
880
                return nil, err
×
881
        }
×
882
        if err := rows.Err(); err != nil {
×
883
                return nil, err
×
884
        }
×
885
        return items, nil
×
886
}
887

888
const highestSCID = `-- name: HighestSCID :one
889
SELECT scid
890
FROM channels
891
WHERE version = $1
892
ORDER BY scid DESC
893
LIMIT 1
894
`
895

896
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
897
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
898
        var scid []byte
×
899
        err := row.Scan(&scid)
×
900
        return scid, err
×
901
}
×
902

903
const insertChanPolicyExtraType = `-- name: InsertChanPolicyExtraType :exec
904
/* ─────────────────────────────────────────────
905
   channel_policy_extra_types table queries
906
   ─────────────────────────────────────────────
907
*/
908

909
INSERT INTO channel_policy_extra_types (
910
    channel_policy_id, type, value
911
)
912
VALUES ($1, $2, $3)
913
`
914

915
type InsertChanPolicyExtraTypeParams struct {
916
        ChannelPolicyID int64
917
        Type            int64
918
        Value           []byte
919
}
920

921
func (q *Queries) InsertChanPolicyExtraType(ctx context.Context, arg InsertChanPolicyExtraTypeParams) error {
×
922
        _, err := q.db.ExecContext(ctx, insertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
923
        return err
×
924
}
×
925

926
const insertChannelFeature = `-- name: InsertChannelFeature :exec
927
/* ─────────────────────────────────────────────
928
   channel_features table queries
929
   ─────────────────────────────────────────────
930
*/
931

932
INSERT INTO channel_features (
933
    channel_id, feature_bit
934
) VALUES (
935
    $1, $2
936
)
937
`
938

939
type InsertChannelFeatureParams struct {
940
        ChannelID  int64
941
        FeatureBit int32
942
}
943

944
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
945
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
946
        return err
×
947
}
×
948

949
const insertNodeAddress = `-- name: InsertNodeAddress :exec
950
/* ─────────────────────────────────────────────
951
   node_addresses table queries
952
   ─────────────────────────────────────────────
953
*/
954

955
INSERT INTO node_addresses (
956
    node_id,
957
    type,
958
    address,
959
    position
960
) VALUES (
961
    $1, $2, $3, $4
962
 )
963
`
964

965
type InsertNodeAddressParams struct {
966
        NodeID   int64
967
        Type     int16
968
        Address  string
969
        Position int32
970
}
971

972
func (q *Queries) InsertNodeAddress(ctx context.Context, arg InsertNodeAddressParams) error {
×
973
        _, err := q.db.ExecContext(ctx, insertNodeAddress,
×
974
                arg.NodeID,
×
975
                arg.Type,
×
976
                arg.Address,
×
977
                arg.Position,
×
978
        )
×
979
        return err
×
980
}
×
981

982
const insertNodeFeature = `-- name: InsertNodeFeature :exec
983
/* ─────────────────────────────────────────────
984
   node_features table queries
985
   ─────────────────────────────────────────────
986
*/
987

988
INSERT INTO node_features (
989
    node_id, feature_bit
990
) VALUES (
991
    $1, $2
992
)
993
`
994

995
type InsertNodeFeatureParams struct {
996
        NodeID     int64
997
        FeatureBit int32
998
}
999

1000
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
1001
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
1002
        return err
×
1003
}
×
1004

1005
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
1006
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,
1007
    n1.pub_key AS node1_pubkey,
1008
    n2.pub_key AS node2_pubkey,
1009

1010
    -- Policy 1
1011
    -- TODO(elle): use sqlc.embed to embed policy structs
1012
    --  once this issue is resolved:
1013
    --  https://github.com/sqlc-dev/sqlc/issues/2997
1014
    cp1.id AS policy1_id,
1015
    cp1.node_id AS policy1_node_id,
1016
    cp1.version AS policy1_version,
1017
    cp1.timelock AS policy1_timelock,
1018
    cp1.fee_ppm AS policy1_fee_ppm,
1019
    cp1.base_fee_msat AS policy1_base_fee_msat,
1020
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1021
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1022
    cp1.last_update AS policy1_last_update,
1023
    cp1.disabled AS policy1_disabled,
1024
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1025
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1026
    cp1.signature AS policy1_signature,
1027

1028
       -- Policy 2
1029
    cp2.id AS policy2_id,
1030
    cp2.node_id AS policy2_node_id,
1031
    cp2.version AS policy2_version,
1032
    cp2.timelock AS policy2_timelock,
1033
    cp2.fee_ppm AS policy2_fee_ppm,
1034
    cp2.base_fee_msat AS policy2_base_fee_msat,
1035
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1036
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1037
    cp2.last_update AS policy2_last_update,
1038
    cp2.disabled AS policy2_disabled,
1039
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1040
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1041
    cp2.signature AS policy2_signature
1042

1043
FROM channels c
1044
    JOIN nodes n1 ON c.node_id_1 = n1.id
1045
    JOIN nodes n2 ON c.node_id_2 = n2.id
1046
    LEFT JOIN channel_policies cp1
1047
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1048
    LEFT JOIN channel_policies cp2
1049
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1050
WHERE c.version = $1
1051
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
1052
`
1053

1054
type ListChannelsByNodeIDParams struct {
1055
        Version int16
1056
        NodeID1 int64
1057
}
1058

1059
type ListChannelsByNodeIDRow struct {
1060
        Channel                        Channel
1061
        Node1Pubkey                    []byte
1062
        Node2Pubkey                    []byte
1063
        Policy1ID                      sql.NullInt64
1064
        Policy1NodeID                  sql.NullInt64
1065
        Policy1Version                 sql.NullInt16
1066
        Policy1Timelock                sql.NullInt32
1067
        Policy1FeePpm                  sql.NullInt64
1068
        Policy1BaseFeeMsat             sql.NullInt64
1069
        Policy1MinHtlcMsat             sql.NullInt64
1070
        Policy1MaxHtlcMsat             sql.NullInt64
1071
        Policy1LastUpdate              sql.NullInt64
1072
        Policy1Disabled                sql.NullBool
1073
        Policy1InboundBaseFeeMsat      sql.NullInt64
1074
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1075
        Policy1Signature               []byte
1076
        Policy2ID                      sql.NullInt64
1077
        Policy2NodeID                  sql.NullInt64
1078
        Policy2Version                 sql.NullInt16
1079
        Policy2Timelock                sql.NullInt32
1080
        Policy2FeePpm                  sql.NullInt64
1081
        Policy2BaseFeeMsat             sql.NullInt64
1082
        Policy2MinHtlcMsat             sql.NullInt64
1083
        Policy2MaxHtlcMsat             sql.NullInt64
1084
        Policy2LastUpdate              sql.NullInt64
1085
        Policy2Disabled                sql.NullBool
1086
        Policy2InboundBaseFeeMsat      sql.NullInt64
1087
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1088
        Policy2Signature               []byte
1089
}
1090

1091
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
1092
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
1093
        if err != nil {
×
1094
                return nil, err
×
1095
        }
×
1096
        defer rows.Close()
×
1097
        var items []ListChannelsByNodeIDRow
×
1098
        for rows.Next() {
×
1099
                var i ListChannelsByNodeIDRow
×
1100
                if err := rows.Scan(
×
1101
                        &i.Channel.ID,
×
1102
                        &i.Channel.Version,
×
1103
                        &i.Channel.Scid,
×
1104
                        &i.Channel.NodeID1,
×
1105
                        &i.Channel.NodeID2,
×
1106
                        &i.Channel.Outpoint,
×
1107
                        &i.Channel.Capacity,
×
1108
                        &i.Channel.BitcoinKey1,
×
1109
                        &i.Channel.BitcoinKey2,
×
1110
                        &i.Channel.Node1Signature,
×
1111
                        &i.Channel.Node2Signature,
×
1112
                        &i.Channel.Bitcoin1Signature,
×
1113
                        &i.Channel.Bitcoin2Signature,
×
1114
                        &i.Node1Pubkey,
×
1115
                        &i.Node2Pubkey,
×
1116
                        &i.Policy1ID,
×
1117
                        &i.Policy1NodeID,
×
1118
                        &i.Policy1Version,
×
1119
                        &i.Policy1Timelock,
×
1120
                        &i.Policy1FeePpm,
×
1121
                        &i.Policy1BaseFeeMsat,
×
1122
                        &i.Policy1MinHtlcMsat,
×
1123
                        &i.Policy1MaxHtlcMsat,
×
1124
                        &i.Policy1LastUpdate,
×
1125
                        &i.Policy1Disabled,
×
1126
                        &i.Policy1InboundBaseFeeMsat,
×
1127
                        &i.Policy1InboundFeeRateMilliMsat,
×
1128
                        &i.Policy1Signature,
×
1129
                        &i.Policy2ID,
×
1130
                        &i.Policy2NodeID,
×
1131
                        &i.Policy2Version,
×
1132
                        &i.Policy2Timelock,
×
1133
                        &i.Policy2FeePpm,
×
1134
                        &i.Policy2BaseFeeMsat,
×
1135
                        &i.Policy2MinHtlcMsat,
×
1136
                        &i.Policy2MaxHtlcMsat,
×
1137
                        &i.Policy2LastUpdate,
×
1138
                        &i.Policy2Disabled,
×
1139
                        &i.Policy2InboundBaseFeeMsat,
×
1140
                        &i.Policy2InboundFeeRateMilliMsat,
×
1141
                        &i.Policy2Signature,
×
1142
                ); err != nil {
×
1143
                        return nil, err
×
1144
                }
×
1145
                items = append(items, i)
×
1146
        }
1147
        if err := rows.Close(); err != nil {
×
1148
                return nil, err
×
1149
        }
×
1150
        if err := rows.Err(); err != nil {
×
1151
                return nil, err
×
1152
        }
×
1153
        return items, nil
×
1154
}
1155

1156
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
1157
SELECT
1158
    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,
1159

1160
    -- Join node pubkeys
1161
    n1.pub_key AS node1_pubkey,
1162
    n2.pub_key AS node2_pubkey,
1163

1164
    -- Node 1 policy
1165
    cp1.id AS policy_1_id,
1166
    cp1.node_id AS policy_1_node_id,
1167
    cp1.version AS policy_1_version,
1168
    cp1.timelock AS policy_1_timelock,
1169
    cp1.fee_ppm AS policy_1_fee_ppm,
1170
    cp1.base_fee_msat AS policy_1_base_fee_msat,
1171
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
1172
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
1173
    cp1.last_update AS policy_1_last_update,
1174
    cp1.disabled AS policy_1_disabled,
1175
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1176
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1177
    cp1.signature AS policy_1_signature,
1178

1179
    -- Node 2 policy
1180
    cp2.id AS policy_2_id,
1181
    cp2.node_id AS policy_2_node_id,
1182
    cp2.version AS policy_2_version,
1183
    cp2.timelock AS policy_2_timelock,
1184
    cp2.fee_ppm AS policy_2_fee_ppm,
1185
    cp2.base_fee_msat AS policy_2_base_fee_msat,
1186
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
1187
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
1188
    cp2.last_update AS policy_2_last_update,
1189
    cp2.disabled AS policy_2_disabled,
1190
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1191
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1192
    cp2.signature AS policy_2_signature
1193

1194
FROM channels c
1195
JOIN nodes n1 ON c.node_id_1 = n1.id
1196
JOIN nodes n2 ON c.node_id_2 = n2.id
1197
LEFT JOIN channel_policies cp1
1198
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1199
LEFT JOIN channel_policies cp2
1200
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1201
WHERE c.version = $1 AND c.id > $2
1202
ORDER BY c.id
1203
LIMIT $3
1204
`
1205

1206
type ListChannelsPaginatedParams struct {
1207
        Version int16
1208
        ID      int64
1209
        Limit   int32
1210
}
1211

1212
type ListChannelsPaginatedRow struct {
1213
        Channel                        Channel
1214
        Node1Pubkey                    []byte
1215
        Node2Pubkey                    []byte
1216
        Policy1ID                      sql.NullInt64
1217
        Policy1NodeID                  sql.NullInt64
1218
        Policy1Version                 sql.NullInt16
1219
        Policy1Timelock                sql.NullInt32
1220
        Policy1FeePpm                  sql.NullInt64
1221
        Policy1BaseFeeMsat             sql.NullInt64
1222
        Policy1MinHtlcMsat             sql.NullInt64
1223
        Policy1MaxHtlcMsat             sql.NullInt64
1224
        Policy1LastUpdate              sql.NullInt64
1225
        Policy1Disabled                sql.NullBool
1226
        Policy1InboundBaseFeeMsat      sql.NullInt64
1227
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1228
        Policy1Signature               []byte
1229
        Policy2ID                      sql.NullInt64
1230
        Policy2NodeID                  sql.NullInt64
1231
        Policy2Version                 sql.NullInt16
1232
        Policy2Timelock                sql.NullInt32
1233
        Policy2FeePpm                  sql.NullInt64
1234
        Policy2BaseFeeMsat             sql.NullInt64
1235
        Policy2MinHtlcMsat             sql.NullInt64
1236
        Policy2MaxHtlcMsat             sql.NullInt64
1237
        Policy2LastUpdate              sql.NullInt64
1238
        Policy2Disabled                sql.NullBool
1239
        Policy2InboundBaseFeeMsat      sql.NullInt64
1240
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1241
        Policy2Signature               []byte
1242
}
1243

NEW
1244
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
NEW
1245
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
NEW
1246
        if err != nil {
×
NEW
1247
                return nil, err
×
NEW
1248
        }
×
NEW
1249
        defer rows.Close()
×
NEW
1250
        var items []ListChannelsPaginatedRow
×
NEW
1251
        for rows.Next() {
×
NEW
1252
                var i ListChannelsPaginatedRow
×
NEW
1253
                if err := rows.Scan(
×
NEW
1254
                        &i.Channel.ID,
×
NEW
1255
                        &i.Channel.Version,
×
NEW
1256
                        &i.Channel.Scid,
×
NEW
1257
                        &i.Channel.NodeID1,
×
NEW
1258
                        &i.Channel.NodeID2,
×
NEW
1259
                        &i.Channel.Outpoint,
×
NEW
1260
                        &i.Channel.Capacity,
×
NEW
1261
                        &i.Channel.BitcoinKey1,
×
NEW
1262
                        &i.Channel.BitcoinKey2,
×
NEW
1263
                        &i.Channel.Node1Signature,
×
NEW
1264
                        &i.Channel.Node2Signature,
×
NEW
1265
                        &i.Channel.Bitcoin1Signature,
×
NEW
1266
                        &i.Channel.Bitcoin2Signature,
×
NEW
1267
                        &i.Node1Pubkey,
×
NEW
1268
                        &i.Node2Pubkey,
×
NEW
1269
                        &i.Policy1ID,
×
NEW
1270
                        &i.Policy1NodeID,
×
NEW
1271
                        &i.Policy1Version,
×
NEW
1272
                        &i.Policy1Timelock,
×
NEW
1273
                        &i.Policy1FeePpm,
×
NEW
1274
                        &i.Policy1BaseFeeMsat,
×
NEW
1275
                        &i.Policy1MinHtlcMsat,
×
NEW
1276
                        &i.Policy1MaxHtlcMsat,
×
NEW
1277
                        &i.Policy1LastUpdate,
×
NEW
1278
                        &i.Policy1Disabled,
×
NEW
1279
                        &i.Policy1InboundBaseFeeMsat,
×
NEW
1280
                        &i.Policy1InboundFeeRateMilliMsat,
×
NEW
1281
                        &i.Policy1Signature,
×
NEW
1282
                        &i.Policy2ID,
×
NEW
1283
                        &i.Policy2NodeID,
×
NEW
1284
                        &i.Policy2Version,
×
NEW
1285
                        &i.Policy2Timelock,
×
NEW
1286
                        &i.Policy2FeePpm,
×
NEW
1287
                        &i.Policy2BaseFeeMsat,
×
NEW
1288
                        &i.Policy2MinHtlcMsat,
×
NEW
1289
                        &i.Policy2MaxHtlcMsat,
×
NEW
1290
                        &i.Policy2LastUpdate,
×
NEW
1291
                        &i.Policy2Disabled,
×
NEW
1292
                        &i.Policy2InboundBaseFeeMsat,
×
NEW
1293
                        &i.Policy2InboundFeeRateMilliMsat,
×
NEW
1294
                        &i.Policy2Signature,
×
NEW
1295
                ); err != nil {
×
NEW
1296
                        return nil, err
×
NEW
1297
                }
×
NEW
1298
                items = append(items, i)
×
1299
        }
NEW
1300
        if err := rows.Close(); err != nil {
×
NEW
1301
                return nil, err
×
NEW
1302
        }
×
NEW
1303
        if err := rows.Err(); err != nil {
×
NEW
1304
                return nil, err
×
NEW
1305
        }
×
NEW
1306
        return items, nil
×
1307
}
1308

1309
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
1310
SELECT id, pub_key
1311
FROM nodes
1312
WHERE version = $1  AND id > $2
1313
ORDER BY id
1314
LIMIT $3
1315
`
1316

1317
type ListNodeIDsAndPubKeysParams struct {
1318
        Version int16
1319
        ID      int64
1320
        Limit   int32
1321
}
1322

1323
type ListNodeIDsAndPubKeysRow struct {
1324
        ID     int64
1325
        PubKey []byte
1326
}
1327

1328
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
1329
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
1330
        if err != nil {
×
1331
                return nil, err
×
1332
        }
×
1333
        defer rows.Close()
×
1334
        var items []ListNodeIDsAndPubKeysRow
×
1335
        for rows.Next() {
×
1336
                var i ListNodeIDsAndPubKeysRow
×
1337
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
1338
                        return nil, err
×
1339
                }
×
1340
                items = append(items, i)
×
1341
        }
1342
        if err := rows.Close(); err != nil {
×
1343
                return nil, err
×
1344
        }
×
1345
        if err := rows.Err(); err != nil {
×
1346
                return nil, err
×
1347
        }
×
1348
        return items, nil
×
1349
}
1350

1351
const listNodesPaginated = `-- name: ListNodesPaginated :many
1352
SELECT id, version, pub_key, alias, last_update, color, signature
1353
FROM nodes
1354
WHERE version = $1 AND id > $2
1355
ORDER BY id
1356
LIMIT $3
1357
`
1358

1359
type ListNodesPaginatedParams struct {
1360
        Version int16
1361
        ID      int64
1362
        Limit   int32
1363
}
1364

1365
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]Node, error) {
×
1366
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
1367
        if err != nil {
×
1368
                return nil, err
×
1369
        }
×
1370
        defer rows.Close()
×
1371
        var items []Node
×
1372
        for rows.Next() {
×
1373
                var i Node
×
1374
                if err := rows.Scan(
×
1375
                        &i.ID,
×
1376
                        &i.Version,
×
1377
                        &i.PubKey,
×
1378
                        &i.Alias,
×
1379
                        &i.LastUpdate,
×
1380
                        &i.Color,
×
1381
                        &i.Signature,
×
1382
                ); err != nil {
×
1383
                        return nil, err
×
1384
                }
×
1385
                items = append(items, i)
×
1386
        }
1387
        if err := rows.Close(); err != nil {
×
1388
                return nil, err
×
1389
        }
×
1390
        if err := rows.Err(); err != nil {
×
1391
                return nil, err
×
1392
        }
×
1393
        return items, nil
×
1394
}
1395

1396
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
1397
/* ─────────────────────────────────────────────
1398
   channel_policies table queries
1399
   ─────────────────────────────────────────────
1400
*/
1401

1402
INSERT INTO channel_policies (
1403
    version, channel_id, node_id, timelock, fee_ppm,
1404
    base_fee_msat, min_htlc_msat, last_update, disabled,
1405
    max_htlc_msat, inbound_base_fee_msat,
1406
    inbound_fee_rate_milli_msat, signature
1407
) VALUES  (
1408
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
1409
)
1410
ON CONFLICT (channel_id, node_id, version)
1411
    -- Update the following fields if a conflict occurs on channel_id,
1412
    -- node_id, and version.
1413
    DO UPDATE SET
1414
        timelock = EXCLUDED.timelock,
1415
        fee_ppm = EXCLUDED.fee_ppm,
1416
        base_fee_msat = EXCLUDED.base_fee_msat,
1417
        min_htlc_msat = EXCLUDED.min_htlc_msat,
1418
        last_update = EXCLUDED.last_update,
1419
        disabled = EXCLUDED.disabled,
1420
        max_htlc_msat = EXCLUDED.max_htlc_msat,
1421
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
1422
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
1423
        signature = EXCLUDED.signature
1424
WHERE EXCLUDED.last_update > channel_policies.last_update
1425
RETURNING id
1426
`
1427

1428
type UpsertEdgePolicyParams struct {
1429
        Version                 int16
1430
        ChannelID               int64
1431
        NodeID                  int64
1432
        Timelock                int32
1433
        FeePpm                  int64
1434
        BaseFeeMsat             int64
1435
        MinHtlcMsat             int64
1436
        LastUpdate              sql.NullInt64
1437
        Disabled                sql.NullBool
1438
        MaxHtlcMsat             sql.NullInt64
1439
        InboundBaseFeeMsat      sql.NullInt64
1440
        InboundFeeRateMilliMsat sql.NullInt64
1441
        Signature               []byte
1442
}
1443

1444
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
1445
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
1446
                arg.Version,
×
1447
                arg.ChannelID,
×
1448
                arg.NodeID,
×
1449
                arg.Timelock,
×
1450
                arg.FeePpm,
×
1451
                arg.BaseFeeMsat,
×
1452
                arg.MinHtlcMsat,
×
1453
                arg.LastUpdate,
×
1454
                arg.Disabled,
×
1455
                arg.MaxHtlcMsat,
×
1456
                arg.InboundBaseFeeMsat,
×
1457
                arg.InboundFeeRateMilliMsat,
×
1458
                arg.Signature,
×
1459
        )
×
1460
        var id int64
×
1461
        err := row.Scan(&id)
×
1462
        return id, err
×
1463
}
×
1464

1465
const upsertNode = `-- name: UpsertNode :one
1466
/* ─────────────────────────────────────────────
1467
   nodes table queries
1468
   ─────────────────────────────────────────────
1469
*/
1470

1471
INSERT INTO nodes (
1472
    version, pub_key, alias, last_update, color, signature
1473
) VALUES (
1474
    $1, $2, $3, $4, $5, $6
1475
)
1476
ON CONFLICT (pub_key, version)
1477
    -- Update the following fields if a conflict occurs on pub_key
1478
    -- and version.
1479
    DO UPDATE SET
1480
        alias = EXCLUDED.alias,
1481
        last_update = EXCLUDED.last_update,
1482
        color = EXCLUDED.color,
1483
        signature = EXCLUDED.signature
1484
WHERE nodes.last_update IS NULL
1485
    OR EXCLUDED.last_update > nodes.last_update
1486
RETURNING id
1487
`
1488

1489
type UpsertNodeParams struct {
1490
        Version    int16
1491
        PubKey     []byte
1492
        Alias      sql.NullString
1493
        LastUpdate sql.NullInt64
1494
        Color      sql.NullString
1495
        Signature  []byte
1496
}
1497

1498
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
1499
        row := q.db.QueryRowContext(ctx, upsertNode,
×
1500
                arg.Version,
×
1501
                arg.PubKey,
×
1502
                arg.Alias,
×
1503
                arg.LastUpdate,
×
1504
                arg.Color,
×
1505
                arg.Signature,
×
1506
        )
×
1507
        var id int64
×
1508
        err := row.Scan(&id)
×
1509
        return id, err
×
1510
}
×
1511

1512
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
1513
/* ─────────────────────────────────────────────
1514
   node_extra_types table queries
1515
   ─────────────────────────────────────────────
1516
*/
1517

1518
INSERT INTO node_extra_types (
1519
    node_id, type, value
1520
)
1521
VALUES ($1, $2, $3)
1522
ON CONFLICT (type, node_id)
1523
    -- Update the value if a conflict occurs on type
1524
    -- and node_id.
1525
    DO UPDATE SET value = EXCLUDED.value
1526
`
1527

1528
type UpsertNodeExtraTypeParams struct {
1529
        NodeID int64
1530
        Type   int64
1531
        Value  []byte
1532
}
1533

1534
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
1535
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
1536
        return err
×
1537
}
×
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