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

lightningnetwork / lnd / 15847774552

24 Jun 2025 10:18AM UTC coverage: 67.965% (-0.2%) from 68.172%
15847774552

Pull #9936

github

web-flow
Merge 3687171cd into 45c15646c
Pull Request #9936: [12] graph/db: Implement more graph SQLStore methods

8 of 650 new or added lines in 3 files covered. (1.23%)

56 existing lines in 19 files now uncovered.

134725 of 198227 relevant lines covered (67.97%)

22070.37 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 (
455
       (cp1.last_update >= $2 AND cp1.last_update < $3)
456
       OR
457
       (cp2.last_update >= $2 AND cp2.last_update < $3)
458
  )
459
ORDER BY
460
    CASE
461
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
462
            THEN COALESCE(cp1.last_update, 0)
463
        ELSE COALESCE(cp2.last_update, 0)
464
        END ASC
465
`
466

467
type GetChannelsByPolicyLastUpdateRangeParams struct {
468
        Version   int16
469
        StartTime sql.NullInt64
470
        EndTime   sql.NullInt64
471
}
472

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

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

582
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
583
SELECT node_id, type, value
584
FROM node_extra_types
585
WHERE node_id = $1
586
`
587

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

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

619
type GetNodeAddressesByPubKeyParams struct {
620
        PubKey  []byte
621
        Version int16
622
}
623

624
type GetNodeAddressesByPubKeyRow struct {
625
        Type    sql.NullInt16
626
        Address sql.NullString
627
}
628

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

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

659
type GetNodeByPubKeyParams struct {
660
        PubKey  []byte
661
        Version int16
662
}
663

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

679
const getNodeFeatures = `-- name: GetNodeFeatures :many
680
SELECT node_id, feature_bit
681
FROM node_features
682
WHERE node_id = $1
683
`
684

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

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

716
type GetNodeFeaturesByPubKeyParams struct {
717
        PubKey  []byte
718
        Version int16
719
}
720

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

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

751
type GetNodeIDByPubKeyParams struct {
752
        PubKey  []byte
753
        Version int16
754
}
755

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

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

770
type GetNodesByLastUpdateRangeParams struct {
771
        StartTime sql.NullInt64
772
        EndTime   sql.NullInt64
773
}
774

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

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

814
type GetPublicV1ChannelsBySCIDParams struct {
815
        StartScid []byte
816
        EndScid   []byte
817
}
818

NEW
819
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]Channel, error) {
×
NEW
820
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
NEW
821
        if err != nil {
×
NEW
822
                return nil, err
×
NEW
823
        }
×
NEW
824
        defer rows.Close()
×
NEW
825
        var items []Channel
×
NEW
826
        for rows.Next() {
×
NEW
827
                var i Channel
×
NEW
828
                if err := rows.Scan(
×
NEW
829
                        &i.ID,
×
NEW
830
                        &i.Version,
×
NEW
831
                        &i.Scid,
×
NEW
832
                        &i.NodeID1,
×
NEW
833
                        &i.NodeID2,
×
NEW
834
                        &i.Outpoint,
×
NEW
835
                        &i.Capacity,
×
NEW
836
                        &i.BitcoinKey1,
×
NEW
837
                        &i.BitcoinKey2,
×
NEW
838
                        &i.Node1Signature,
×
NEW
839
                        &i.Node2Signature,
×
NEW
840
                        &i.Bitcoin1Signature,
×
NEW
841
                        &i.Bitcoin2Signature,
×
NEW
842
                ); err != nil {
×
NEW
843
                        return nil, err
×
NEW
844
                }
×
NEW
845
                items = append(items, i)
×
846
        }
NEW
847
        if err := rows.Close(); err != nil {
×
NEW
848
                return nil, err
×
NEW
849
        }
×
NEW
850
        if err := rows.Err(); err != nil {
×
NEW
851
                return nil, err
×
NEW
852
        }
×
NEW
853
        return items, nil
×
854
}
855

856
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
857
SELECT sn.node_id, n.pub_key
858
FROM source_nodes sn
859
    JOIN nodes n ON sn.node_id = n.id
860
WHERE n.version = $1
861
`
862

863
type GetSourceNodesByVersionRow struct {
864
        NodeID int64
865
        PubKey []byte
866
}
867

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

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

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

906
const insertChanPolicyExtraType = `-- name: InsertChanPolicyExtraType :exec
907
/* ─────────────────────────────────────────────
908
   channel_policy_extra_types table queries
909
   ─────────────────────────────────────────────
910
*/
911

912
INSERT INTO channel_policy_extra_types (
913
    channel_policy_id, type, value
914
)
915
VALUES ($1, $2, $3)
916
`
917

918
type InsertChanPolicyExtraTypeParams struct {
919
        ChannelPolicyID int64
920
        Type            int64
921
        Value           []byte
922
}
923

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

929
const insertChannelFeature = `-- name: InsertChannelFeature :exec
930
/* ─────────────────────────────────────────────
931
   channel_features table queries
932
   ─────────────────────────────────────────────
933
*/
934

935
INSERT INTO channel_features (
936
    channel_id, feature_bit
937
) VALUES (
938
    $1, $2
939
)
940
`
941

942
type InsertChannelFeatureParams struct {
943
        ChannelID  int64
944
        FeatureBit int32
945
}
946

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

952
const insertNodeAddress = `-- name: InsertNodeAddress :exec
953
/* ─────────────────────────────────────────────
954
   node_addresses table queries
955
   ─────────────────────────────────────────────
956
*/
957

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

968
type InsertNodeAddressParams struct {
969
        NodeID   int64
970
        Type     int16
971
        Address  string
972
        Position int32
973
}
974

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

985
const insertNodeFeature = `-- name: InsertNodeFeature :exec
986
/* ─────────────────────────────────────────────
987
   node_features table queries
988
   ─────────────────────────────────────────────
989
*/
990

991
INSERT INTO node_features (
992
    node_id, feature_bit
993
) VALUES (
994
    $1, $2
995
)
996
`
997

998
type InsertNodeFeatureParams struct {
999
        NodeID     int64
1000
        FeatureBit int32
1001
}
1002

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

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

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

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

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

1057
type ListChannelsByNodeIDParams struct {
1058
        Version int16
1059
        NodeID1 int64
1060
}
1061

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

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

1159
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
1160
SELECT
1161
    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,
1162

1163
    -- Join node pubkeys
1164
    n1.pub_key AS node1_pubkey,
1165
    n2.pub_key AS node2_pubkey,
1166

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

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

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

1209
type ListChannelsWithPoliciesPaginatedParams struct {
1210
        Version int16
1211
        ID      int64
1212
        Limit   int32
1213
}
1214

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

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

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

1320
type ListNodeIDsAndPubKeysParams struct {
1321
        Version int16
1322
        ID      int64
1323
        Limit   int32
1324
}
1325

1326
type ListNodeIDsAndPubKeysRow struct {
1327
        ID     int64
1328
        PubKey []byte
1329
}
1330

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

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

1362
type ListNodesPaginatedParams struct {
1363
        Version int16
1364
        ID      int64
1365
        Limit   int32
1366
}
1367

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

1399
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
1400
/* ─────────────────────────────────────────────
1401
   channel_policies table queries
1402
   ─────────────────────────────────────────────
1403
*/
1404

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

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

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

1468
const upsertNode = `-- name: UpsertNode :one
1469
/* ─────────────────────────────────────────────
1470
   nodes table queries
1471
   ─────────────────────────────────────────────
1472
*/
1473

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

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

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

1515
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
1516
/* ─────────────────────────────────────────────
1517
   node_extra_types table queries
1518
   ─────────────────────────────────────────────
1519
*/
1520

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

1531
type UpsertNodeExtraTypeParams struct {
1532
        NodeID int64
1533
        Type   int64
1534
        Value  []byte
1535
}
1536

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