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

lightningnetwork / lnd / 16215965260

11 Jul 2025 08:52AM UTC coverage: 67.325% (+9.7%) from 57.653%
16215965260

push

github

web-flow
Merge pull request #10038 from ellemouton/graphMig3-indexes

[graph mig 3]: graph/db: migrate zombies, closed SCIDs, prune log from kvdb to SQL

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

30 existing lines in 9 files now uncovered.

135271 of 200923 relevant lines covered (67.32%)

21761.85 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 addV1ChannelProof = `-- name: AddV1ChannelProof :execresult
30
UPDATE channels
31
SET node_1_signature = $2,
32
    node_2_signature = $3,
33
    bitcoin_1_signature = $4,
34
    bitcoin_2_signature = $5
35
WHERE scid = $1
36
  AND version = 1
37
`
38

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

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

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

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

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

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

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

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

122
const createChannelExtraType = `-- name: CreateChannelExtraType :exec
123
/* ─────────────────────────────────────────────
124
   channel_extra_types table queries
125
   ─────────────────────────────────────────────
126
*/
127

128
INSERT INTO channel_extra_types (
129
    channel_id, type, value
130
)
131
VALUES ($1, $2, $3)
132
`
133

134
type CreateChannelExtraTypeParams struct {
135
        ChannelID int64
136
        Type      int64
137
        Value     []byte
138
}
139

140
func (q *Queries) CreateChannelExtraType(ctx context.Context, arg CreateChannelExtraTypeParams) error {
×
141
        _, err := q.db.ExecContext(ctx, createChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
142
        return err
×
143
}
×
144

145
const deleteChannel = `-- name: DeleteChannel :exec
146
DELETE FROM channels WHERE id = $1
147
`
148

149
func (q *Queries) DeleteChannel(ctx context.Context, id int64) error {
×
150
        _, err := q.db.ExecContext(ctx, deleteChannel, id)
×
151
        return err
×
152
}
×
153

154
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
155
DELETE FROM channel_policy_extra_types
156
WHERE channel_policy_id = $1
157
`
158

159
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
160
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
161
        return err
×
162
}
×
163

164
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
165
DELETE FROM node_extra_types
166
WHERE node_id = $1
167
  AND type = $2
168
`
169

170
type DeleteExtraNodeTypeParams struct {
171
        NodeID int64
172
        Type   int64
173
}
174

175
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
176
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
177
        return err
×
178
}
×
179

180
const deleteNode = `-- name: DeleteNode :exec
181
DELETE FROM nodes
182
WHERE id = $1
183
`
184

185
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
186
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
187
        return err
×
188
}
×
189

190
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
191
DELETE FROM node_addresses
192
WHERE node_id = $1
193
`
194

195
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
196
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
197
        return err
×
198
}
×
199

200
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
201
DELETE FROM nodes
202
WHERE pub_key = $1
203
  AND version = $2
204
`
205

206
type DeleteNodeByPubKeyParams struct {
207
        PubKey  []byte
208
        Version int16
209
}
210

211
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
212
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
213
}
×
214

215
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
216
DELETE FROM node_features
217
WHERE node_id = $1
218
  AND feature_bit = $2
219
`
220

221
type DeleteNodeFeatureParams struct {
222
        NodeID     int64
223
        FeatureBit int32
224
}
225

226
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
227
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
228
        return err
×
229
}
×
230

231
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
232
DELETE FROM prune_log
233
WHERE block_height >= $1
234
  AND block_height <= $2
235
`
236

237
type DeletePruneLogEntriesInRangeParams struct {
238
        StartHeight int64
239
        EndHeight   int64
240
}
241

242
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
243
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
244
        return err
×
245
}
×
246

247
const deleteUnconnectedNodes = `-- name: DeleteUnconnectedNodes :many
248
DELETE FROM nodes
249
WHERE
250
    -- Ignore any of our source nodes.
251
    NOT EXISTS (
252
        SELECT 1
253
        FROM source_nodes sn
254
        WHERE sn.node_id = nodes.id
255
    )
256
    -- Select all nodes that do not have any channels.
257
    AND NOT EXISTS (
258
        SELECT 1
259
        FROM channels c
260
        WHERE c.node_id_1 = nodes.id OR c.node_id_2 = nodes.id
261
) RETURNING pub_key
262
`
263

264
func (q *Queries) DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error) {
×
265
        rows, err := q.db.QueryContext(ctx, deleteUnconnectedNodes)
×
266
        if err != nil {
×
267
                return nil, err
×
268
        }
×
269
        defer rows.Close()
×
270
        var items [][]byte
×
271
        for rows.Next() {
×
272
                var pub_key []byte
×
273
                if err := rows.Scan(&pub_key); err != nil {
×
274
                        return nil, err
×
275
                }
×
276
                items = append(items, pub_key)
×
277
        }
278
        if err := rows.Close(); err != nil {
×
279
                return nil, err
×
280
        }
×
281
        if err := rows.Err(); err != nil {
×
282
                return nil, err
×
283
        }
×
284
        return items, nil
×
285
}
286

287
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
288
DELETE FROM zombie_channels
289
WHERE scid = $1
290
AND version = $2
291
`
292

293
type DeleteZombieChannelParams struct {
294
        Scid    []byte
295
        Version int16
296
}
297

298
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
299
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
300
}
×
301

302
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
303
SELECT
304
    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,
305
    n1.pub_key AS node1_pub_key,
306
    n2.pub_key AS node2_pub_key
307
FROM channels c
308
    JOIN nodes n1 ON c.node_id_1 = n1.id
309
    JOIN nodes n2 ON c.node_id_2 = n2.id
310
WHERE c.scid = $1
311
  AND c.version = $2
312
`
313

314
type GetChannelAndNodesBySCIDParams struct {
315
        Scid    []byte
316
        Version int16
317
}
318

319
type GetChannelAndNodesBySCIDRow struct {
320
        ID                int64
321
        Version           int16
322
        Scid              []byte
323
        NodeID1           int64
324
        NodeID2           int64
325
        Outpoint          string
326
        Capacity          sql.NullInt64
327
        BitcoinKey1       []byte
328
        BitcoinKey2       []byte
329
        Node1Signature    []byte
330
        Node2Signature    []byte
331
        Bitcoin1Signature []byte
332
        Bitcoin2Signature []byte
333
        Node1PubKey       []byte
334
        Node2PubKey       []byte
335
}
336

337
func (q *Queries) GetChannelAndNodesBySCID(ctx context.Context, arg GetChannelAndNodesBySCIDParams) (GetChannelAndNodesBySCIDRow, error) {
×
338
        row := q.db.QueryRowContext(ctx, getChannelAndNodesBySCID, arg.Scid, arg.Version)
×
339
        var i GetChannelAndNodesBySCIDRow
×
340
        err := row.Scan(
×
341
                &i.ID,
×
342
                &i.Version,
×
343
                &i.Scid,
×
344
                &i.NodeID1,
×
345
                &i.NodeID2,
×
346
                &i.Outpoint,
×
347
                &i.Capacity,
×
348
                &i.BitcoinKey1,
×
349
                &i.BitcoinKey2,
×
350
                &i.Node1Signature,
×
351
                &i.Node2Signature,
×
352
                &i.Bitcoin1Signature,
×
353
                &i.Bitcoin2Signature,
×
354
                &i.Node1PubKey,
×
355
                &i.Node2PubKey,
×
356
        )
×
357
        return i, err
×
358
}
×
359

360
const getChannelByOutpoint = `-- name: GetChannelByOutpoint :one
361
SELECT
362
    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,
363
    n1.pub_key AS node1_pubkey,
364
    n2.pub_key AS node2_pubkey
365
FROM channels c
366
    JOIN nodes n1 ON c.node_id_1 = n1.id
367
    JOIN nodes n2 ON c.node_id_2 = n2.id
368
WHERE c.outpoint = $1
369
`
370

371
type GetChannelByOutpointRow struct {
372
        Channel     Channel
373
        Node1Pubkey []byte
374
        Node2Pubkey []byte
375
}
376

377
func (q *Queries) GetChannelByOutpoint(ctx context.Context, outpoint string) (GetChannelByOutpointRow, error) {
×
378
        row := q.db.QueryRowContext(ctx, getChannelByOutpoint, outpoint)
×
379
        var i GetChannelByOutpointRow
×
380
        err := row.Scan(
×
381
                &i.Channel.ID,
×
382
                &i.Channel.Version,
×
383
                &i.Channel.Scid,
×
384
                &i.Channel.NodeID1,
×
385
                &i.Channel.NodeID2,
×
386
                &i.Channel.Outpoint,
×
387
                &i.Channel.Capacity,
×
388
                &i.Channel.BitcoinKey1,
×
389
                &i.Channel.BitcoinKey2,
×
390
                &i.Channel.Node1Signature,
×
391
                &i.Channel.Node2Signature,
×
392
                &i.Channel.Bitcoin1Signature,
×
393
                &i.Channel.Bitcoin2Signature,
×
394
                &i.Node1Pubkey,
×
395
                &i.Node2Pubkey,
×
396
        )
×
397
        return i, err
×
398
}
×
399

400
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
401
SELECT
402
    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,
403

404
    n1.pub_key AS node1_pubkey,
405
    n2.pub_key AS node2_pubkey,
406

407
    -- Node 1 policy
408
    cp1.id AS policy_1_id,
409
    cp1.node_id AS policy_1_node_id,
410
    cp1.version AS policy_1_version,
411
    cp1.timelock AS policy_1_timelock,
412
    cp1.fee_ppm AS policy_1_fee_ppm,
413
    cp1.base_fee_msat AS policy_1_base_fee_msat,
414
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
415
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
416
    cp1.last_update AS policy_1_last_update,
417
    cp1.disabled AS policy_1_disabled,
418
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
419
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
420
    cp1.message_flags AS policy_1_message_flags,
421
    cp1.channel_flags AS policy_1_channel_flags,
422
    cp1.signature AS policy_1_signature,
423

424
    -- Node 2 policy
425
    cp2.id AS policy_2_id,
426
    cp2.node_id AS policy_2_node_id,
427
    cp2.version AS policy_2_version,
428
    cp2.timelock AS policy_2_timelock,
429
    cp2.fee_ppm AS policy_2_fee_ppm,
430
    cp2.base_fee_msat AS policy_2_base_fee_msat,
431
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
432
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
433
    cp2.last_update AS policy_2_last_update,
434
    cp2.disabled AS policy_2_disabled,
435
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
436
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
437
    cp2.message_flags AS policy_2_message_flags,
438
    cp2.channel_flags AS policy_2_channel_flags,
439
    cp2.signature AS policy_2_signature
440
FROM channels c
441
    JOIN nodes n1 ON c.node_id_1 = n1.id
442
    JOIN nodes n2 ON c.node_id_2 = n2.id
443
    LEFT JOIN channel_policies cp1
444
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
445
    LEFT JOIN channel_policies cp2
446
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
447
WHERE c.outpoint = $1 AND c.version = $2
448
`
449

450
type GetChannelByOutpointWithPoliciesParams struct {
451
        Outpoint string
452
        Version  int16
453
}
454

455
type GetChannelByOutpointWithPoliciesRow struct {
456
        Channel                        Channel
457
        Node1Pubkey                    []byte
458
        Node2Pubkey                    []byte
459
        Policy1ID                      sql.NullInt64
460
        Policy1NodeID                  sql.NullInt64
461
        Policy1Version                 sql.NullInt16
462
        Policy1Timelock                sql.NullInt32
463
        Policy1FeePpm                  sql.NullInt64
464
        Policy1BaseFeeMsat             sql.NullInt64
465
        Policy1MinHtlcMsat             sql.NullInt64
466
        Policy1MaxHtlcMsat             sql.NullInt64
467
        Policy1LastUpdate              sql.NullInt64
468
        Policy1Disabled                sql.NullBool
469
        Policy1InboundBaseFeeMsat      sql.NullInt64
470
        Policy1InboundFeeRateMilliMsat sql.NullInt64
471
        Policy1MessageFlags            sql.NullInt16
472
        Policy1ChannelFlags            sql.NullInt16
473
        Policy1Signature               []byte
474
        Policy2ID                      sql.NullInt64
475
        Policy2NodeID                  sql.NullInt64
476
        Policy2Version                 sql.NullInt16
477
        Policy2Timelock                sql.NullInt32
478
        Policy2FeePpm                  sql.NullInt64
479
        Policy2BaseFeeMsat             sql.NullInt64
480
        Policy2MinHtlcMsat             sql.NullInt64
481
        Policy2MaxHtlcMsat             sql.NullInt64
482
        Policy2LastUpdate              sql.NullInt64
483
        Policy2Disabled                sql.NullBool
484
        Policy2InboundBaseFeeMsat      sql.NullInt64
485
        Policy2InboundFeeRateMilliMsat sql.NullInt64
486
        Policy2MessageFlags            sql.NullInt16
487
        Policy2ChannelFlags            sql.NullInt16
488
        Policy2Signature               []byte
489
}
490

491
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
492
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
493
        var i GetChannelByOutpointWithPoliciesRow
×
494
        err := row.Scan(
×
495
                &i.Channel.ID,
×
496
                &i.Channel.Version,
×
497
                &i.Channel.Scid,
×
498
                &i.Channel.NodeID1,
×
499
                &i.Channel.NodeID2,
×
500
                &i.Channel.Outpoint,
×
501
                &i.Channel.Capacity,
×
502
                &i.Channel.BitcoinKey1,
×
503
                &i.Channel.BitcoinKey2,
×
504
                &i.Channel.Node1Signature,
×
505
                &i.Channel.Node2Signature,
×
506
                &i.Channel.Bitcoin1Signature,
×
507
                &i.Channel.Bitcoin2Signature,
×
508
                &i.Node1Pubkey,
×
509
                &i.Node2Pubkey,
×
510
                &i.Policy1ID,
×
511
                &i.Policy1NodeID,
×
512
                &i.Policy1Version,
×
513
                &i.Policy1Timelock,
×
514
                &i.Policy1FeePpm,
×
515
                &i.Policy1BaseFeeMsat,
×
516
                &i.Policy1MinHtlcMsat,
×
517
                &i.Policy1MaxHtlcMsat,
×
518
                &i.Policy1LastUpdate,
×
519
                &i.Policy1Disabled,
×
520
                &i.Policy1InboundBaseFeeMsat,
×
521
                &i.Policy1InboundFeeRateMilliMsat,
×
522
                &i.Policy1MessageFlags,
×
523
                &i.Policy1ChannelFlags,
×
524
                &i.Policy1Signature,
×
525
                &i.Policy2ID,
×
526
                &i.Policy2NodeID,
×
527
                &i.Policy2Version,
×
528
                &i.Policy2Timelock,
×
529
                &i.Policy2FeePpm,
×
530
                &i.Policy2BaseFeeMsat,
×
531
                &i.Policy2MinHtlcMsat,
×
532
                &i.Policy2MaxHtlcMsat,
×
533
                &i.Policy2LastUpdate,
×
534
                &i.Policy2Disabled,
×
535
                &i.Policy2InboundBaseFeeMsat,
×
536
                &i.Policy2InboundFeeRateMilliMsat,
×
537
                &i.Policy2MessageFlags,
×
538
                &i.Policy2ChannelFlags,
×
539
                &i.Policy2Signature,
×
540
        )
×
541
        return i, err
×
542
}
×
543

544
const getChannelBySCID = `-- name: GetChannelBySCID :one
545
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
546
WHERE scid = $1 AND version = $2
547
`
548

549
type GetChannelBySCIDParams struct {
550
        Scid    []byte
551
        Version int16
552
}
553

554
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (Channel, error) {
×
555
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
556
        var i Channel
×
557
        err := row.Scan(
×
558
                &i.ID,
×
559
                &i.Version,
×
560
                &i.Scid,
×
561
                &i.NodeID1,
×
562
                &i.NodeID2,
×
563
                &i.Outpoint,
×
564
                &i.Capacity,
×
565
                &i.BitcoinKey1,
×
566
                &i.BitcoinKey2,
×
567
                &i.Node1Signature,
×
568
                &i.Node2Signature,
×
569
                &i.Bitcoin1Signature,
×
570
                &i.Bitcoin2Signature,
×
571
        )
×
572
        return i, err
×
573
}
×
574

575
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
576
SELECT
577
    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,
578
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
579
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
580

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

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

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

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

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

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

732
const getChannelFeaturesAndExtras = `-- name: GetChannelFeaturesAndExtras :many
733
SELECT
734
    cf.channel_id,
735
    true AS is_feature,
736
    cf.feature_bit AS feature_bit,
737
    NULL AS extra_key,
738
    NULL AS value
739
FROM channel_features cf
740
WHERE cf.channel_id = $1
741

742
UNION ALL
743

744
SELECT
745
    cet.channel_id,
746
    false AS is_feature,
747
    0 AS feature_bit,
748
    cet.type AS extra_key,
749
    cet.value AS value
750
FROM channel_extra_types cet
751
WHERE cet.channel_id = $1
752
`
753

754
type GetChannelFeaturesAndExtrasRow struct {
755
        ChannelID  int64
756
        IsFeature  bool
757
        FeatureBit int32
758
        ExtraKey   interface{}
759
        Value      interface{}
760
}
761

762
func (q *Queries) GetChannelFeaturesAndExtras(ctx context.Context, channelID int64) ([]GetChannelFeaturesAndExtrasRow, error) {
×
763
        rows, err := q.db.QueryContext(ctx, getChannelFeaturesAndExtras, channelID)
×
764
        if err != nil {
×
765
                return nil, err
×
766
        }
×
767
        defer rows.Close()
×
768
        var items []GetChannelFeaturesAndExtrasRow
×
769
        for rows.Next() {
×
770
                var i GetChannelFeaturesAndExtrasRow
×
771
                if err := rows.Scan(
×
772
                        &i.ChannelID,
×
773
                        &i.IsFeature,
×
774
                        &i.FeatureBit,
×
775
                        &i.ExtraKey,
×
776
                        &i.Value,
×
777
                ); err != nil {
×
778
                        return nil, err
×
779
                }
×
780
                items = append(items, i)
×
781
        }
782
        if err := rows.Close(); err != nil {
×
783
                return nil, err
×
784
        }
×
785
        if err := rows.Err(); err != nil {
×
786
                return nil, err
×
787
        }
×
788
        return items, nil
×
789
}
790

791
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
792
SELECT id, version, channel_id, node_id, timelock, fee_ppm, base_fee_msat, min_htlc_msat, max_htlc_msat, last_update, disabled, inbound_base_fee_msat, inbound_fee_rate_milli_msat, message_flags, channel_flags, signature
793
FROM channel_policies
794
WHERE channel_id = $1
795
  AND node_id = $2
796
  AND version = $3
797
`
798

799
type GetChannelPolicyByChannelAndNodeParams struct {
800
        ChannelID int64
801
        NodeID    int64
802
        Version   int16
803
}
804

805
func (q *Queries) GetChannelPolicyByChannelAndNode(ctx context.Context, arg GetChannelPolicyByChannelAndNodeParams) (ChannelPolicy, error) {
×
806
        row := q.db.QueryRowContext(ctx, getChannelPolicyByChannelAndNode, arg.ChannelID, arg.NodeID, arg.Version)
×
807
        var i ChannelPolicy
×
808
        err := row.Scan(
×
809
                &i.ID,
×
810
                &i.Version,
×
811
                &i.ChannelID,
×
812
                &i.NodeID,
×
813
                &i.Timelock,
×
814
                &i.FeePpm,
×
815
                &i.BaseFeeMsat,
×
816
                &i.MinHtlcMsat,
×
817
                &i.MaxHtlcMsat,
×
818
                &i.LastUpdate,
×
819
                &i.Disabled,
×
820
                &i.InboundBaseFeeMsat,
×
821
                &i.InboundFeeRateMilliMsat,
×
822
                &i.MessageFlags,
×
823
                &i.ChannelFlags,
×
824
                &i.Signature,
×
825
        )
×
826
        return i, err
×
827
}
×
828

829
const getChannelPolicyExtraTypes = `-- name: GetChannelPolicyExtraTypes :many
830
SELECT
831
    cp.id AS policy_id,
832
    cp.channel_id,
833
    cp.node_id,
834
    cpet.type,
835
    cpet.value
836
FROM channel_policies cp
837
JOIN channel_policy_extra_types cpet
838
ON cp.id = cpet.channel_policy_id
839
WHERE cp.id = $1 OR cp.id = $2
840
`
841

842
type GetChannelPolicyExtraTypesParams struct {
843
        ID   int64
844
        ID_2 int64
845
}
846

847
type GetChannelPolicyExtraTypesRow struct {
848
        PolicyID  int64
849
        ChannelID int64
850
        NodeID    int64
851
        Type      int64
852
        Value     []byte
853
}
854

855
func (q *Queries) GetChannelPolicyExtraTypes(ctx context.Context, arg GetChannelPolicyExtraTypesParams) ([]GetChannelPolicyExtraTypesRow, error) {
×
856
        rows, err := q.db.QueryContext(ctx, getChannelPolicyExtraTypes, arg.ID, arg.ID_2)
×
857
        if err != nil {
×
858
                return nil, err
×
859
        }
×
860
        defer rows.Close()
×
861
        var items []GetChannelPolicyExtraTypesRow
×
862
        for rows.Next() {
×
863
                var i GetChannelPolicyExtraTypesRow
×
864
                if err := rows.Scan(
×
865
                        &i.PolicyID,
×
866
                        &i.ChannelID,
×
867
                        &i.NodeID,
×
868
                        &i.Type,
×
869
                        &i.Value,
×
870
                ); err != nil {
×
871
                        return nil, err
×
872
                }
×
873
                items = append(items, i)
×
874
        }
875
        if err := rows.Close(); err != nil {
×
876
                return nil, err
×
877
        }
×
878
        if err := rows.Err(); err != nil {
×
879
                return nil, err
×
880
        }
×
881
        return items, nil
×
882
}
883

884
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
885
SELECT
886
    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,
887
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature,
888
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature,
889

890
    -- Policy 1 (node_id_1)
891
    cp1.id AS policy1_id,
892
    cp1.node_id AS policy1_node_id,
893
    cp1.version AS policy1_version,
894
    cp1.timelock AS policy1_timelock,
895
    cp1.fee_ppm AS policy1_fee_ppm,
896
    cp1.base_fee_msat AS policy1_base_fee_msat,
897
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
898
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
899
    cp1.last_update AS policy1_last_update,
900
    cp1.disabled AS policy1_disabled,
901
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
902
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
903
    cp1.message_flags AS policy1_message_flags,
904
    cp1.channel_flags AS policy1_channel_flags,
905
    cp1.signature AS policy1_signature,
906

907
    -- Policy 2 (node_id_2)
908
    cp2.id AS policy2_id,
909
    cp2.node_id AS policy2_node_id,
910
    cp2.version AS policy2_version,
911
    cp2.timelock AS policy2_timelock,
912
    cp2.fee_ppm AS policy2_fee_ppm,
913
    cp2.base_fee_msat AS policy2_base_fee_msat,
914
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
915
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
916
    cp2.last_update AS policy2_last_update,
917
    cp2.disabled AS policy2_disabled,
918
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
919
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
920
    cp2.message_flags AS policy2_message_flags,
921
    cp2.channel_flags AS policy2_channel_flags,
922
    cp2.signature AS policy2_signature
923

924
FROM channels c
925
    JOIN nodes n1 ON c.node_id_1 = n1.id
926
    JOIN nodes n2 ON c.node_id_2 = n2.id
927
    LEFT JOIN channel_policies cp1
928
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
929
    LEFT JOIN channel_policies cp2
930
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
931
WHERE c.version = $1
932
  AND (
933
       (cp1.last_update >= $2 AND cp1.last_update < $3)
934
       OR
935
       (cp2.last_update >= $2 AND cp2.last_update < $3)
936
  )
937
ORDER BY
938
    CASE
939
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
940
            THEN COALESCE(cp1.last_update, 0)
941
        ELSE COALESCE(cp2.last_update, 0)
942
        END ASC
943
`
944

945
type GetChannelsByPolicyLastUpdateRangeParams struct {
946
        Version   int16
947
        StartTime sql.NullInt64
948
        EndTime   sql.NullInt64
949
}
950

951
type GetChannelsByPolicyLastUpdateRangeRow struct {
952
        Channel                        Channel
953
        Node                           Node
954
        Node_2                         Node
955
        Policy1ID                      sql.NullInt64
956
        Policy1NodeID                  sql.NullInt64
957
        Policy1Version                 sql.NullInt16
958
        Policy1Timelock                sql.NullInt32
959
        Policy1FeePpm                  sql.NullInt64
960
        Policy1BaseFeeMsat             sql.NullInt64
961
        Policy1MinHtlcMsat             sql.NullInt64
962
        Policy1MaxHtlcMsat             sql.NullInt64
963
        Policy1LastUpdate              sql.NullInt64
964
        Policy1Disabled                sql.NullBool
965
        Policy1InboundBaseFeeMsat      sql.NullInt64
966
        Policy1InboundFeeRateMilliMsat sql.NullInt64
967
        Policy1MessageFlags            sql.NullInt16
968
        Policy1ChannelFlags            sql.NullInt16
969
        Policy1Signature               []byte
970
        Policy2ID                      sql.NullInt64
971
        Policy2NodeID                  sql.NullInt64
972
        Policy2Version                 sql.NullInt16
973
        Policy2Timelock                sql.NullInt32
974
        Policy2FeePpm                  sql.NullInt64
975
        Policy2BaseFeeMsat             sql.NullInt64
976
        Policy2MinHtlcMsat             sql.NullInt64
977
        Policy2MaxHtlcMsat             sql.NullInt64
978
        Policy2LastUpdate              sql.NullInt64
979
        Policy2Disabled                sql.NullBool
980
        Policy2InboundBaseFeeMsat      sql.NullInt64
981
        Policy2InboundFeeRateMilliMsat sql.NullInt64
982
        Policy2MessageFlags            sql.NullInt16
983
        Policy2ChannelFlags            sql.NullInt16
984
        Policy2Signature               []byte
985
}
986

987
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
988
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange, arg.Version, arg.StartTime, arg.EndTime)
×
989
        if err != nil {
×
990
                return nil, err
×
991
        }
×
992
        defer rows.Close()
×
993
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
994
        for rows.Next() {
×
995
                var i GetChannelsByPolicyLastUpdateRangeRow
×
996
                if err := rows.Scan(
×
997
                        &i.Channel.ID,
×
998
                        &i.Channel.Version,
×
999
                        &i.Channel.Scid,
×
1000
                        &i.Channel.NodeID1,
×
1001
                        &i.Channel.NodeID2,
×
1002
                        &i.Channel.Outpoint,
×
1003
                        &i.Channel.Capacity,
×
1004
                        &i.Channel.BitcoinKey1,
×
1005
                        &i.Channel.BitcoinKey2,
×
1006
                        &i.Channel.Node1Signature,
×
1007
                        &i.Channel.Node2Signature,
×
1008
                        &i.Channel.Bitcoin1Signature,
×
1009
                        &i.Channel.Bitcoin2Signature,
×
1010
                        &i.Node.ID,
×
1011
                        &i.Node.Version,
×
1012
                        &i.Node.PubKey,
×
1013
                        &i.Node.Alias,
×
1014
                        &i.Node.LastUpdate,
×
1015
                        &i.Node.Color,
×
1016
                        &i.Node.Signature,
×
1017
                        &i.Node_2.ID,
×
1018
                        &i.Node_2.Version,
×
1019
                        &i.Node_2.PubKey,
×
1020
                        &i.Node_2.Alias,
×
1021
                        &i.Node_2.LastUpdate,
×
1022
                        &i.Node_2.Color,
×
1023
                        &i.Node_2.Signature,
×
1024
                        &i.Policy1ID,
×
1025
                        &i.Policy1NodeID,
×
1026
                        &i.Policy1Version,
×
1027
                        &i.Policy1Timelock,
×
1028
                        &i.Policy1FeePpm,
×
1029
                        &i.Policy1BaseFeeMsat,
×
1030
                        &i.Policy1MinHtlcMsat,
×
1031
                        &i.Policy1MaxHtlcMsat,
×
1032
                        &i.Policy1LastUpdate,
×
1033
                        &i.Policy1Disabled,
×
1034
                        &i.Policy1InboundBaseFeeMsat,
×
1035
                        &i.Policy1InboundFeeRateMilliMsat,
×
1036
                        &i.Policy1MessageFlags,
×
1037
                        &i.Policy1ChannelFlags,
×
1038
                        &i.Policy1Signature,
×
1039
                        &i.Policy2ID,
×
1040
                        &i.Policy2NodeID,
×
1041
                        &i.Policy2Version,
×
1042
                        &i.Policy2Timelock,
×
1043
                        &i.Policy2FeePpm,
×
1044
                        &i.Policy2BaseFeeMsat,
×
1045
                        &i.Policy2MinHtlcMsat,
×
1046
                        &i.Policy2MaxHtlcMsat,
×
1047
                        &i.Policy2LastUpdate,
×
1048
                        &i.Policy2Disabled,
×
1049
                        &i.Policy2InboundBaseFeeMsat,
×
1050
                        &i.Policy2InboundFeeRateMilliMsat,
×
1051
                        &i.Policy2MessageFlags,
×
1052
                        &i.Policy2ChannelFlags,
×
1053
                        &i.Policy2Signature,
×
1054
                ); err != nil {
×
1055
                        return nil, err
×
1056
                }
×
1057
                items = append(items, i)
×
1058
        }
1059
        if err := rows.Close(); err != nil {
×
1060
                return nil, err
×
1061
        }
×
1062
        if err := rows.Err(); err != nil {
×
1063
                return nil, err
×
1064
        }
×
1065
        return items, nil
×
1066
}
1067

1068
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1069
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,
1070
    n1.pub_key AS node1_pub_key,
1071
    n2.pub_key AS node2_pub_key
1072
FROM channels c
1073
    JOIN nodes n1 ON c.node_id_1 = n1.id
1074
    JOIN nodes n2 ON c.node_id_2 = n2.id
1075
WHERE scid >= $1
1076
  AND scid < $2
1077
`
1078

1079
type GetChannelsBySCIDRangeParams struct {
1080
        StartScid []byte
1081
        EndScid   []byte
1082
}
1083

1084
type GetChannelsBySCIDRangeRow struct {
1085
        Channel     Channel
1086
        Node1PubKey []byte
1087
        Node2PubKey []byte
1088
}
1089

1090
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1091
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1092
        if err != nil {
×
1093
                return nil, err
×
1094
        }
×
1095
        defer rows.Close()
×
1096
        var items []GetChannelsBySCIDRangeRow
×
1097
        for rows.Next() {
×
1098
                var i GetChannelsBySCIDRangeRow
×
1099
                if err := rows.Scan(
×
1100
                        &i.Channel.ID,
×
1101
                        &i.Channel.Version,
×
1102
                        &i.Channel.Scid,
×
1103
                        &i.Channel.NodeID1,
×
1104
                        &i.Channel.NodeID2,
×
1105
                        &i.Channel.Outpoint,
×
1106
                        &i.Channel.Capacity,
×
1107
                        &i.Channel.BitcoinKey1,
×
1108
                        &i.Channel.BitcoinKey2,
×
1109
                        &i.Channel.Node1Signature,
×
1110
                        &i.Channel.Node2Signature,
×
1111
                        &i.Channel.Bitcoin1Signature,
×
1112
                        &i.Channel.Bitcoin2Signature,
×
1113
                        &i.Node1PubKey,
×
1114
                        &i.Node2PubKey,
×
1115
                ); err != nil {
×
1116
                        return nil, err
×
1117
                }
×
1118
                items = append(items, i)
×
1119
        }
1120
        if err := rows.Close(); err != nil {
×
1121
                return nil, err
×
1122
        }
×
1123
        if err := rows.Err(); err != nil {
×
1124
                return nil, err
×
1125
        }
×
1126
        return items, nil
×
1127
}
1128

1129
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1130
SELECT node_id, type, value
1131
FROM node_extra_types
1132
WHERE node_id = $1
1133
`
1134

1135
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]NodeExtraType, error) {
×
1136
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
1137
        if err != nil {
×
1138
                return nil, err
×
1139
        }
×
1140
        defer rows.Close()
×
1141
        var items []NodeExtraType
×
1142
        for rows.Next() {
×
1143
                var i NodeExtraType
×
1144
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
1145
                        return nil, err
×
1146
                }
×
1147
                items = append(items, i)
×
1148
        }
1149
        if err := rows.Close(); err != nil {
×
1150
                return nil, err
×
1151
        }
×
1152
        if err := rows.Err(); err != nil {
×
1153
                return nil, err
×
1154
        }
×
1155
        return items, nil
×
1156
}
1157

1158
const getNodeAddressesByPubKey = `-- name: GetNodeAddressesByPubKey :many
1159
SELECT a.type, a.address
1160
FROM nodes n
1161
LEFT JOIN node_addresses a ON a.node_id = n.id
1162
WHERE n.pub_key = $1 AND n.version = $2
1163
ORDER BY a.type ASC, a.position ASC
1164
`
1165

1166
type GetNodeAddressesByPubKeyParams struct {
1167
        PubKey  []byte
1168
        Version int16
1169
}
1170

1171
type GetNodeAddressesByPubKeyRow struct {
1172
        Type    sql.NullInt16
1173
        Address sql.NullString
1174
}
1175

1176
func (q *Queries) GetNodeAddressesByPubKey(ctx context.Context, arg GetNodeAddressesByPubKeyParams) ([]GetNodeAddressesByPubKeyRow, error) {
×
1177
        rows, err := q.db.QueryContext(ctx, getNodeAddressesByPubKey, arg.PubKey, arg.Version)
×
1178
        if err != nil {
×
1179
                return nil, err
×
1180
        }
×
1181
        defer rows.Close()
×
1182
        var items []GetNodeAddressesByPubKeyRow
×
1183
        for rows.Next() {
×
1184
                var i GetNodeAddressesByPubKeyRow
×
1185
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
1186
                        return nil, err
×
1187
                }
×
1188
                items = append(items, i)
×
1189
        }
1190
        if err := rows.Close(); err != nil {
×
1191
                return nil, err
×
1192
        }
×
1193
        if err := rows.Err(); err != nil {
×
1194
                return nil, err
×
1195
        }
×
1196
        return items, nil
×
1197
}
1198

1199
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1200
SELECT id, version, pub_key, alias, last_update, color, signature
1201
FROM nodes
1202
WHERE pub_key = $1
1203
  AND version = $2
1204
`
1205

1206
type GetNodeByPubKeyParams struct {
1207
        PubKey  []byte
1208
        Version int16
1209
}
1210

1211
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (Node, error) {
×
1212
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
1213
        var i Node
×
1214
        err := row.Scan(
×
1215
                &i.ID,
×
1216
                &i.Version,
×
1217
                &i.PubKey,
×
1218
                &i.Alias,
×
1219
                &i.LastUpdate,
×
1220
                &i.Color,
×
1221
                &i.Signature,
×
1222
        )
×
1223
        return i, err
×
1224
}
×
1225

1226
const getNodeFeatures = `-- name: GetNodeFeatures :many
1227
SELECT node_id, feature_bit
1228
FROM node_features
1229
WHERE node_id = $1
1230
`
1231

1232
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]NodeFeature, error) {
×
1233
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
1234
        if err != nil {
×
1235
                return nil, err
×
1236
        }
×
1237
        defer rows.Close()
×
1238
        var items []NodeFeature
×
1239
        for rows.Next() {
×
1240
                var i NodeFeature
×
1241
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
1242
                        return nil, err
×
1243
                }
×
1244
                items = append(items, i)
×
1245
        }
1246
        if err := rows.Close(); err != nil {
×
1247
                return nil, err
×
1248
        }
×
1249
        if err := rows.Err(); err != nil {
×
1250
                return nil, err
×
1251
        }
×
1252
        return items, nil
×
1253
}
1254

1255
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
1256
SELECT f.feature_bit
1257
FROM nodes n
1258
    JOIN node_features f ON f.node_id = n.id
1259
WHERE n.pub_key = $1
1260
  AND n.version = $2
1261
`
1262

1263
type GetNodeFeaturesByPubKeyParams struct {
1264
        PubKey  []byte
1265
        Version int16
1266
}
1267

1268
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
1269
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
1270
        if err != nil {
×
1271
                return nil, err
×
1272
        }
×
1273
        defer rows.Close()
×
1274
        var items []int32
×
1275
        for rows.Next() {
×
1276
                var feature_bit int32
×
1277
                if err := rows.Scan(&feature_bit); err != nil {
×
1278
                        return nil, err
×
1279
                }
×
1280
                items = append(items, feature_bit)
×
1281
        }
1282
        if err := rows.Close(); err != nil {
×
1283
                return nil, err
×
1284
        }
×
1285
        if err := rows.Err(); err != nil {
×
1286
                return nil, err
×
1287
        }
×
1288
        return items, nil
×
1289
}
1290

1291
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
1292
SELECT id
1293
FROM nodes
1294
WHERE pub_key = $1
1295
  AND version = $2
1296
`
1297

1298
type GetNodeIDByPubKeyParams struct {
1299
        PubKey  []byte
1300
        Version int16
1301
}
1302

1303
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
1304
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
1305
        var id int64
×
1306
        err := row.Scan(&id)
×
1307
        return id, err
×
1308
}
×
1309

1310
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
1311
SELECT id, version, pub_key, alias, last_update, color, signature
1312
FROM nodes
1313
WHERE last_update >= $1
1314
  AND last_update < $2
1315
`
1316

1317
type GetNodesByLastUpdateRangeParams struct {
1318
        StartTime sql.NullInt64
1319
        EndTime   sql.NullInt64
1320
}
1321

1322
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]Node, error) {
×
1323
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange, arg.StartTime, arg.EndTime)
×
1324
        if err != nil {
×
1325
                return nil, err
×
1326
        }
×
1327
        defer rows.Close()
×
1328
        var items []Node
×
1329
        for rows.Next() {
×
1330
                var i Node
×
1331
                if err := rows.Scan(
×
1332
                        &i.ID,
×
1333
                        &i.Version,
×
1334
                        &i.PubKey,
×
1335
                        &i.Alias,
×
1336
                        &i.LastUpdate,
×
1337
                        &i.Color,
×
1338
                        &i.Signature,
×
1339
                ); err != nil {
×
1340
                        return nil, err
×
1341
                }
×
1342
                items = append(items, i)
×
1343
        }
1344
        if err := rows.Close(); err != nil {
×
1345
                return nil, err
×
1346
        }
×
1347
        if err := rows.Err(); err != nil {
×
1348
                return nil, err
×
1349
        }
×
1350
        return items, nil
×
1351
}
1352

1353
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
1354
SELECT block_hash
1355
FROM prune_log
1356
WHERE block_height = $1
1357
`
1358

NEW
1359
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
NEW
1360
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
NEW
1361
        var block_hash []byte
×
NEW
1362
        err := row.Scan(&block_hash)
×
NEW
1363
        return block_hash, err
×
NEW
1364
}
×
1365

1366
const getPruneTip = `-- name: GetPruneTip :one
1367
SELECT block_height, block_hash
1368
FROM prune_log
1369
ORDER BY block_height DESC
1370
LIMIT 1
1371
`
1372

1373
func (q *Queries) GetPruneTip(ctx context.Context) (PruneLog, error) {
×
1374
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
1375
        var i PruneLog
×
1376
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
1377
        return i, err
×
1378
}
×
1379

1380
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
1381
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
1382
FROM channels
1383
WHERE node_1_signature IS NOT NULL
1384
  AND scid >= $1
1385
  AND scid < $2
1386
`
1387

1388
type GetPublicV1ChannelsBySCIDParams struct {
1389
        StartScid []byte
1390
        EndScid   []byte
1391
}
1392

1393
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]Channel, error) {
×
1394
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
1395
        if err != nil {
×
1396
                return nil, err
×
1397
        }
×
1398
        defer rows.Close()
×
1399
        var items []Channel
×
1400
        for rows.Next() {
×
1401
                var i Channel
×
1402
                if err := rows.Scan(
×
1403
                        &i.ID,
×
1404
                        &i.Version,
×
1405
                        &i.Scid,
×
1406
                        &i.NodeID1,
×
1407
                        &i.NodeID2,
×
1408
                        &i.Outpoint,
×
1409
                        &i.Capacity,
×
1410
                        &i.BitcoinKey1,
×
1411
                        &i.BitcoinKey2,
×
1412
                        &i.Node1Signature,
×
1413
                        &i.Node2Signature,
×
1414
                        &i.Bitcoin1Signature,
×
1415
                        &i.Bitcoin2Signature,
×
1416
                ); err != nil {
×
1417
                        return nil, err
×
1418
                }
×
1419
                items = append(items, i)
×
1420
        }
1421
        if err := rows.Close(); err != nil {
×
1422
                return nil, err
×
1423
        }
×
1424
        if err := rows.Err(); err != nil {
×
1425
                return nil, err
×
1426
        }
×
1427
        return items, nil
×
1428
}
1429

1430
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
1431
SELECT scid from channels
1432
WHERE outpoint = $1 AND version = $2
1433
`
1434

1435
type GetSCIDByOutpointParams struct {
1436
        Outpoint string
1437
        Version  int16
1438
}
1439

1440
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
1441
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
1442
        var scid []byte
×
1443
        err := row.Scan(&scid)
×
1444
        return scid, err
×
1445
}
×
1446

1447
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
1448
SELECT sn.node_id, n.pub_key
1449
FROM source_nodes sn
1450
    JOIN nodes n ON sn.node_id = n.id
1451
WHERE n.version = $1
1452
`
1453

1454
type GetSourceNodesByVersionRow struct {
1455
        NodeID int64
1456
        PubKey []byte
1457
}
1458

1459
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
1460
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
1461
        if err != nil {
×
1462
                return nil, err
×
1463
        }
×
1464
        defer rows.Close()
×
1465
        var items []GetSourceNodesByVersionRow
×
1466
        for rows.Next() {
×
1467
                var i GetSourceNodesByVersionRow
×
1468
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
1469
                        return nil, err
×
1470
                }
×
1471
                items = append(items, i)
×
1472
        }
1473
        if err := rows.Close(); err != nil {
×
1474
                return nil, err
×
1475
        }
×
1476
        if err := rows.Err(); err != nil {
×
1477
                return nil, err
×
1478
        }
×
1479
        return items, nil
×
1480
}
1481

1482
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
1483
SELECT c.scid
1484
FROM channels c
1485
    JOIN channel_policies cp ON cp.channel_id = c.id
1486
WHERE cp.disabled = true
1487
AND c.version = 1
1488
GROUP BY c.scid
1489
HAVING COUNT(*) > 1
1490
`
1491

1492
// NOTE: this is V1 specific since for V1, disabled is a
1493
// simple, single boolean. The proposed V2 policy
1494
// structure will have a more complex disabled bit vector
1495
// and so the query for V2 may differ.
1496
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
1497
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
1498
        if err != nil {
×
1499
                return nil, err
×
1500
        }
×
1501
        defer rows.Close()
×
1502
        var items [][]byte
×
1503
        for rows.Next() {
×
1504
                var scid []byte
×
1505
                if err := rows.Scan(&scid); err != nil {
×
1506
                        return nil, err
×
1507
                }
×
1508
                items = append(items, scid)
×
1509
        }
1510
        if err := rows.Close(); err != nil {
×
1511
                return nil, err
×
1512
        }
×
1513
        if err := rows.Err(); err != nil {
×
1514
                return nil, err
×
1515
        }
×
1516
        return items, nil
×
1517
}
1518

1519
const getZombieChannel = `-- name: GetZombieChannel :one
1520
SELECT scid, version, node_key_1, node_key_2
1521
FROM zombie_channels
1522
WHERE scid = $1
1523
AND version = $2
1524
`
1525

1526
type GetZombieChannelParams struct {
1527
        Scid    []byte
1528
        Version int16
1529
}
1530

1531
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (ZombieChannel, error) {
×
1532
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
1533
        var i ZombieChannel
×
1534
        err := row.Scan(
×
1535
                &i.Scid,
×
1536
                &i.Version,
×
1537
                &i.NodeKey1,
×
1538
                &i.NodeKey2,
×
1539
        )
×
1540
        return i, err
×
1541
}
×
1542

1543
const highestSCID = `-- name: HighestSCID :one
1544
SELECT scid
1545
FROM channels
1546
WHERE version = $1
1547
ORDER BY scid DESC
1548
LIMIT 1
1549
`
1550

1551
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
1552
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
1553
        var scid []byte
×
1554
        err := row.Scan(&scid)
×
1555
        return scid, err
×
1556
}
×
1557

1558
const insertChanPolicyExtraType = `-- name: InsertChanPolicyExtraType :exec
1559
/* ─────────────────────────────────────────────
1560
   channel_policy_extra_types table queries
1561
   ─────────────────────────────────────────────
1562
*/
1563

1564
INSERT INTO channel_policy_extra_types (
1565
    channel_policy_id, type, value
1566
)
1567
VALUES ($1, $2, $3)
1568
`
1569

1570
type InsertChanPolicyExtraTypeParams struct {
1571
        ChannelPolicyID int64
1572
        Type            int64
1573
        Value           []byte
1574
}
1575

1576
func (q *Queries) InsertChanPolicyExtraType(ctx context.Context, arg InsertChanPolicyExtraTypeParams) error {
×
1577
        _, err := q.db.ExecContext(ctx, insertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
1578
        return err
×
1579
}
×
1580

1581
const insertChannelFeature = `-- name: InsertChannelFeature :exec
1582
/* ─────────────────────────────────────────────
1583
   channel_features table queries
1584
   ─────────────────────────────────────────────
1585
*/
1586

1587
INSERT INTO channel_features (
1588
    channel_id, feature_bit
1589
) VALUES (
1590
    $1, $2
1591
)
1592
`
1593

1594
type InsertChannelFeatureParams struct {
1595
        ChannelID  int64
1596
        FeatureBit int32
1597
}
1598

1599
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
1600
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
1601
        return err
×
1602
}
×
1603

1604
const insertClosedChannel = `-- name: InsertClosedChannel :exec
1605
/* ─────────────────────────────────────────────
1606
   closed_scid table queries
1607
   ────────────────────────────────────────────-
1608
*/
1609

1610
INSERT INTO closed_scids (scid)
1611
VALUES ($1)
1612
ON CONFLICT (scid) DO NOTHING
1613
`
1614

1615
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
1616
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
1617
        return err
×
1618
}
×
1619

1620
const insertNodeAddress = `-- name: InsertNodeAddress :exec
1621
/* ─────────────────────────────────────────────
1622
   node_addresses table queries
1623
   ─────────────────────────────────────────────
1624
*/
1625

1626
INSERT INTO node_addresses (
1627
    node_id,
1628
    type,
1629
    address,
1630
    position
1631
) VALUES (
1632
    $1, $2, $3, $4
1633
 )
1634
`
1635

1636
type InsertNodeAddressParams struct {
1637
        NodeID   int64
1638
        Type     int16
1639
        Address  string
1640
        Position int32
1641
}
1642

1643
func (q *Queries) InsertNodeAddress(ctx context.Context, arg InsertNodeAddressParams) error {
×
1644
        _, err := q.db.ExecContext(ctx, insertNodeAddress,
×
1645
                arg.NodeID,
×
1646
                arg.Type,
×
1647
                arg.Address,
×
1648
                arg.Position,
×
1649
        )
×
1650
        return err
×
1651
}
×
1652

1653
const insertNodeFeature = `-- name: InsertNodeFeature :exec
1654
/* ─────────────────────────────────────────────
1655
   node_features table queries
1656
   ─────────────────────────────────────────────
1657
*/
1658

1659
INSERT INTO node_features (
1660
    node_id, feature_bit
1661
) VALUES (
1662
    $1, $2
1663
)
1664
`
1665

1666
type InsertNodeFeatureParams struct {
1667
        NodeID     int64
1668
        FeatureBit int32
1669
}
1670

1671
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
1672
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
1673
        return err
×
1674
}
×
1675

1676
const isClosedChannel = `-- name: IsClosedChannel :one
1677
SELECT EXISTS (
1678
    SELECT 1
1679
    FROM closed_scids
1680
    WHERE scid = $1
1681
)
1682
`
1683

1684
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
1685
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
1686
        var exists bool
×
1687
        err := row.Scan(&exists)
×
1688
        return exists, err
×
1689
}
×
1690

1691
const isPublicV1Node = `-- name: IsPublicV1Node :one
1692
SELECT EXISTS (
1693
    SELECT 1
1694
    FROM channels c
1695
    JOIN nodes n ON n.id = c.node_id_1 OR n.id = c.node_id_2
1696
    -- NOTE: we hard-code the version here since the clauses
1697
    -- here that determine if a node is public is specific
1698
    -- to the V1 gossip protocol. In V1, a node is public
1699
    -- if it has a public channel and a public channel is one
1700
    -- where we have the set of signatures of the channel
1701
    -- announcement. It is enough to just check that we have
1702
    -- one of the signatures since we only ever set them
1703
    -- together.
1704
    WHERE c.version = 1
1705
      AND c.bitcoin_1_signature IS NOT NULL
1706
      AND n.pub_key = $1
1707
)
1708
`
1709

1710
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
1711
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
1712
        var exists bool
×
1713
        err := row.Scan(&exists)
×
1714
        return exists, err
×
1715
}
×
1716

1717
const isZombieChannel = `-- name: IsZombieChannel :one
1718
SELECT EXISTS (
1719
    SELECT 1
1720
    FROM zombie_channels
1721
    WHERE scid = $1
1722
    AND version = $2
1723
) AS is_zombie
1724
`
1725

1726
type IsZombieChannelParams struct {
1727
        Scid    []byte
1728
        Version int16
1729
}
1730

1731
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
1732
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
1733
        var is_zombie bool
×
1734
        err := row.Scan(&is_zombie)
×
1735
        return is_zombie, err
×
1736
}
×
1737

1738
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
1739
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,
1740
    n1.pub_key AS node1_pubkey,
1741
    n2.pub_key AS node2_pubkey,
1742

1743
    -- Policy 1
1744
    -- TODO(elle): use sqlc.embed to embed policy structs
1745
    --  once this issue is resolved:
1746
    --  https://github.com/sqlc-dev/sqlc/issues/2997
1747
    cp1.id AS policy1_id,
1748
    cp1.node_id AS policy1_node_id,
1749
    cp1.version AS policy1_version,
1750
    cp1.timelock AS policy1_timelock,
1751
    cp1.fee_ppm AS policy1_fee_ppm,
1752
    cp1.base_fee_msat AS policy1_base_fee_msat,
1753
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1754
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1755
    cp1.last_update AS policy1_last_update,
1756
    cp1.disabled AS policy1_disabled,
1757
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1758
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1759
    cp1.message_flags AS policy1_message_flags,
1760
    cp1.channel_flags AS policy1_channel_flags,
1761
    cp1.signature AS policy1_signature,
1762

1763
       -- Policy 2
1764
    cp2.id AS policy2_id,
1765
    cp2.node_id AS policy2_node_id,
1766
    cp2.version AS policy2_version,
1767
    cp2.timelock AS policy2_timelock,
1768
    cp2.fee_ppm AS policy2_fee_ppm,
1769
    cp2.base_fee_msat AS policy2_base_fee_msat,
1770
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1771
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1772
    cp2.last_update AS policy2_last_update,
1773
    cp2.disabled AS policy2_disabled,
1774
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1775
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1776
    cp2.message_flags AS policy2_message_flags,
1777
    cp2.channel_flags AS policy2_channel_flags,
1778
    cp2.signature AS policy2_signature
1779

1780
FROM channels c
1781
    JOIN nodes n1 ON c.node_id_1 = n1.id
1782
    JOIN nodes n2 ON c.node_id_2 = n2.id
1783
    LEFT JOIN channel_policies cp1
1784
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1785
    LEFT JOIN channel_policies cp2
1786
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1787
WHERE c.version = $1
1788
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
1789
`
1790

1791
type ListChannelsByNodeIDParams struct {
1792
        Version int16
1793
        NodeID1 int64
1794
}
1795

1796
type ListChannelsByNodeIDRow struct {
1797
        Channel                        Channel
1798
        Node1Pubkey                    []byte
1799
        Node2Pubkey                    []byte
1800
        Policy1ID                      sql.NullInt64
1801
        Policy1NodeID                  sql.NullInt64
1802
        Policy1Version                 sql.NullInt16
1803
        Policy1Timelock                sql.NullInt32
1804
        Policy1FeePpm                  sql.NullInt64
1805
        Policy1BaseFeeMsat             sql.NullInt64
1806
        Policy1MinHtlcMsat             sql.NullInt64
1807
        Policy1MaxHtlcMsat             sql.NullInt64
1808
        Policy1LastUpdate              sql.NullInt64
1809
        Policy1Disabled                sql.NullBool
1810
        Policy1InboundBaseFeeMsat      sql.NullInt64
1811
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1812
        Policy1MessageFlags            sql.NullInt16
1813
        Policy1ChannelFlags            sql.NullInt16
1814
        Policy1Signature               []byte
1815
        Policy2ID                      sql.NullInt64
1816
        Policy2NodeID                  sql.NullInt64
1817
        Policy2Version                 sql.NullInt16
1818
        Policy2Timelock                sql.NullInt32
1819
        Policy2FeePpm                  sql.NullInt64
1820
        Policy2BaseFeeMsat             sql.NullInt64
1821
        Policy2MinHtlcMsat             sql.NullInt64
1822
        Policy2MaxHtlcMsat             sql.NullInt64
1823
        Policy2LastUpdate              sql.NullInt64
1824
        Policy2Disabled                sql.NullBool
1825
        Policy2InboundBaseFeeMsat      sql.NullInt64
1826
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1827
        Policy2MessageFlags            sql.NullInt16
1828
        Policy2ChannelFlags            sql.NullInt16
1829
        Policy2Signature               []byte
1830
}
1831

1832
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
1833
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
1834
        if err != nil {
×
1835
                return nil, err
×
1836
        }
×
1837
        defer rows.Close()
×
1838
        var items []ListChannelsByNodeIDRow
×
1839
        for rows.Next() {
×
1840
                var i ListChannelsByNodeIDRow
×
1841
                if err := rows.Scan(
×
1842
                        &i.Channel.ID,
×
1843
                        &i.Channel.Version,
×
1844
                        &i.Channel.Scid,
×
1845
                        &i.Channel.NodeID1,
×
1846
                        &i.Channel.NodeID2,
×
1847
                        &i.Channel.Outpoint,
×
1848
                        &i.Channel.Capacity,
×
1849
                        &i.Channel.BitcoinKey1,
×
1850
                        &i.Channel.BitcoinKey2,
×
1851
                        &i.Channel.Node1Signature,
×
1852
                        &i.Channel.Node2Signature,
×
1853
                        &i.Channel.Bitcoin1Signature,
×
1854
                        &i.Channel.Bitcoin2Signature,
×
1855
                        &i.Node1Pubkey,
×
1856
                        &i.Node2Pubkey,
×
1857
                        &i.Policy1ID,
×
1858
                        &i.Policy1NodeID,
×
1859
                        &i.Policy1Version,
×
1860
                        &i.Policy1Timelock,
×
1861
                        &i.Policy1FeePpm,
×
1862
                        &i.Policy1BaseFeeMsat,
×
1863
                        &i.Policy1MinHtlcMsat,
×
1864
                        &i.Policy1MaxHtlcMsat,
×
1865
                        &i.Policy1LastUpdate,
×
1866
                        &i.Policy1Disabled,
×
1867
                        &i.Policy1InboundBaseFeeMsat,
×
1868
                        &i.Policy1InboundFeeRateMilliMsat,
×
1869
                        &i.Policy1MessageFlags,
×
1870
                        &i.Policy1ChannelFlags,
×
1871
                        &i.Policy1Signature,
×
1872
                        &i.Policy2ID,
×
1873
                        &i.Policy2NodeID,
×
1874
                        &i.Policy2Version,
×
1875
                        &i.Policy2Timelock,
×
1876
                        &i.Policy2FeePpm,
×
1877
                        &i.Policy2BaseFeeMsat,
×
1878
                        &i.Policy2MinHtlcMsat,
×
1879
                        &i.Policy2MaxHtlcMsat,
×
1880
                        &i.Policy2LastUpdate,
×
1881
                        &i.Policy2Disabled,
×
1882
                        &i.Policy2InboundBaseFeeMsat,
×
1883
                        &i.Policy2InboundFeeRateMilliMsat,
×
1884
                        &i.Policy2MessageFlags,
×
1885
                        &i.Policy2ChannelFlags,
×
1886
                        &i.Policy2Signature,
×
1887
                ); err != nil {
×
1888
                        return nil, err
×
1889
                }
×
1890
                items = append(items, i)
×
1891
        }
1892
        if err := rows.Close(); err != nil {
×
1893
                return nil, err
×
1894
        }
×
1895
        if err := rows.Err(); err != nil {
×
1896
                return nil, err
×
1897
        }
×
1898
        return items, nil
×
1899
}
1900

1901
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
1902
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
1903
FROM channels c
1904
WHERE c.version = $1 AND c.id > $2
1905
ORDER BY c.id
1906
LIMIT $3
1907
`
1908

1909
type ListChannelsPaginatedParams struct {
1910
        Version int16
1911
        ID      int64
1912
        Limit   int32
1913
}
1914

1915
type ListChannelsPaginatedRow struct {
1916
        ID          int64
1917
        BitcoinKey1 []byte
1918
        BitcoinKey2 []byte
1919
        Outpoint    string
1920
}
1921

1922
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
1923
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
1924
        if err != nil {
×
1925
                return nil, err
×
1926
        }
×
1927
        defer rows.Close()
×
1928
        var items []ListChannelsPaginatedRow
×
1929
        for rows.Next() {
×
1930
                var i ListChannelsPaginatedRow
×
1931
                if err := rows.Scan(
×
1932
                        &i.ID,
×
1933
                        &i.BitcoinKey1,
×
1934
                        &i.BitcoinKey2,
×
1935
                        &i.Outpoint,
×
1936
                ); err != nil {
×
1937
                        return nil, err
×
1938
                }
×
1939
                items = append(items, i)
×
1940
        }
1941
        if err := rows.Close(); err != nil {
×
1942
                return nil, err
×
1943
        }
×
1944
        if err := rows.Err(); err != nil {
×
1945
                return nil, err
×
1946
        }
×
1947
        return items, nil
×
1948
}
1949

1950
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
1951
SELECT
1952
    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,
1953

1954
    -- Join node pubkeys
1955
    n1.pub_key AS node1_pubkey,
1956
    n2.pub_key AS node2_pubkey,
1957

1958
    -- Node 1 policy
1959
    cp1.id AS policy_1_id,
1960
    cp1.node_id AS policy_1_node_id,
1961
    cp1.version AS policy_1_version,
1962
    cp1.timelock AS policy_1_timelock,
1963
    cp1.fee_ppm AS policy_1_fee_ppm,
1964
    cp1.base_fee_msat AS policy_1_base_fee_msat,
1965
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
1966
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
1967
    cp1.last_update AS policy_1_last_update,
1968
    cp1.disabled AS policy_1_disabled,
1969
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1970
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1971
    cp1.message_flags AS policy1_message_flags,
1972
    cp1.channel_flags AS policy1_channel_flags,
1973
    cp1.signature AS policy_1_signature,
1974

1975
    -- Node 2 policy
1976
    cp2.id AS policy_2_id,
1977
    cp2.node_id AS policy_2_node_id,
1978
    cp2.version AS policy_2_version,
1979
    cp2.timelock AS policy_2_timelock,
1980
    cp2.fee_ppm AS policy_2_fee_ppm,
1981
    cp2.base_fee_msat AS policy_2_base_fee_msat,
1982
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
1983
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
1984
    cp2.last_update AS policy_2_last_update,
1985
    cp2.disabled AS policy_2_disabled,
1986
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1987
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1988
    cp2.message_flags AS policy2_message_flags,
1989
    cp2.channel_flags AS policy2_channel_flags,
1990
    cp2.signature AS policy_2_signature
1991

1992
FROM channels c
1993
JOIN nodes n1 ON c.node_id_1 = n1.id
1994
JOIN nodes n2 ON c.node_id_2 = n2.id
1995
LEFT JOIN channel_policies cp1
1996
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1997
LEFT JOIN channel_policies cp2
1998
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1999
WHERE c.version = $1 AND c.id > $2
2000
ORDER BY c.id
2001
LIMIT $3
2002
`
2003

2004
type ListChannelsWithPoliciesPaginatedParams struct {
2005
        Version int16
2006
        ID      int64
2007
        Limit   int32
2008
}
2009

2010
type ListChannelsWithPoliciesPaginatedRow struct {
2011
        Channel                        Channel
2012
        Node1Pubkey                    []byte
2013
        Node2Pubkey                    []byte
2014
        Policy1ID                      sql.NullInt64
2015
        Policy1NodeID                  sql.NullInt64
2016
        Policy1Version                 sql.NullInt16
2017
        Policy1Timelock                sql.NullInt32
2018
        Policy1FeePpm                  sql.NullInt64
2019
        Policy1BaseFeeMsat             sql.NullInt64
2020
        Policy1MinHtlcMsat             sql.NullInt64
2021
        Policy1MaxHtlcMsat             sql.NullInt64
2022
        Policy1LastUpdate              sql.NullInt64
2023
        Policy1Disabled                sql.NullBool
2024
        Policy1InboundBaseFeeMsat      sql.NullInt64
2025
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2026
        Policy1MessageFlags            sql.NullInt16
2027
        Policy1ChannelFlags            sql.NullInt16
2028
        Policy1Signature               []byte
2029
        Policy2ID                      sql.NullInt64
2030
        Policy2NodeID                  sql.NullInt64
2031
        Policy2Version                 sql.NullInt16
2032
        Policy2Timelock                sql.NullInt32
2033
        Policy2FeePpm                  sql.NullInt64
2034
        Policy2BaseFeeMsat             sql.NullInt64
2035
        Policy2MinHtlcMsat             sql.NullInt64
2036
        Policy2MaxHtlcMsat             sql.NullInt64
2037
        Policy2LastUpdate              sql.NullInt64
2038
        Policy2Disabled                sql.NullBool
2039
        Policy2InboundBaseFeeMsat      sql.NullInt64
2040
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2041
        Policy2MessageFlags            sql.NullInt16
2042
        Policy2ChannelFlags            sql.NullInt16
2043
        Policy2Signature               []byte
2044
}
2045

2046
func (q *Queries) ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error) {
×
2047
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesPaginated, arg.Version, arg.ID, arg.Limit)
×
2048
        if err != nil {
×
2049
                return nil, err
×
2050
        }
×
2051
        defer rows.Close()
×
2052
        var items []ListChannelsWithPoliciesPaginatedRow
×
2053
        for rows.Next() {
×
2054
                var i ListChannelsWithPoliciesPaginatedRow
×
2055
                if err := rows.Scan(
×
2056
                        &i.Channel.ID,
×
2057
                        &i.Channel.Version,
×
2058
                        &i.Channel.Scid,
×
2059
                        &i.Channel.NodeID1,
×
2060
                        &i.Channel.NodeID2,
×
2061
                        &i.Channel.Outpoint,
×
2062
                        &i.Channel.Capacity,
×
2063
                        &i.Channel.BitcoinKey1,
×
2064
                        &i.Channel.BitcoinKey2,
×
2065
                        &i.Channel.Node1Signature,
×
2066
                        &i.Channel.Node2Signature,
×
2067
                        &i.Channel.Bitcoin1Signature,
×
2068
                        &i.Channel.Bitcoin2Signature,
×
2069
                        &i.Node1Pubkey,
×
2070
                        &i.Node2Pubkey,
×
2071
                        &i.Policy1ID,
×
2072
                        &i.Policy1NodeID,
×
2073
                        &i.Policy1Version,
×
2074
                        &i.Policy1Timelock,
×
2075
                        &i.Policy1FeePpm,
×
2076
                        &i.Policy1BaseFeeMsat,
×
2077
                        &i.Policy1MinHtlcMsat,
×
2078
                        &i.Policy1MaxHtlcMsat,
×
2079
                        &i.Policy1LastUpdate,
×
2080
                        &i.Policy1Disabled,
×
2081
                        &i.Policy1InboundBaseFeeMsat,
×
2082
                        &i.Policy1InboundFeeRateMilliMsat,
×
2083
                        &i.Policy1MessageFlags,
×
2084
                        &i.Policy1ChannelFlags,
×
2085
                        &i.Policy1Signature,
×
2086
                        &i.Policy2ID,
×
2087
                        &i.Policy2NodeID,
×
2088
                        &i.Policy2Version,
×
2089
                        &i.Policy2Timelock,
×
2090
                        &i.Policy2FeePpm,
×
2091
                        &i.Policy2BaseFeeMsat,
×
2092
                        &i.Policy2MinHtlcMsat,
×
2093
                        &i.Policy2MaxHtlcMsat,
×
2094
                        &i.Policy2LastUpdate,
×
2095
                        &i.Policy2Disabled,
×
2096
                        &i.Policy2InboundBaseFeeMsat,
×
2097
                        &i.Policy2InboundFeeRateMilliMsat,
×
2098
                        &i.Policy2MessageFlags,
×
2099
                        &i.Policy2ChannelFlags,
×
2100
                        &i.Policy2Signature,
×
2101
                ); err != nil {
×
2102
                        return nil, err
×
2103
                }
×
2104
                items = append(items, i)
×
2105
        }
2106
        if err := rows.Close(); err != nil {
×
2107
                return nil, err
×
2108
        }
×
2109
        if err := rows.Err(); err != nil {
×
2110
                return nil, err
×
2111
        }
×
2112
        return items, nil
×
2113
}
2114

2115
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
2116
SELECT id, pub_key
2117
FROM nodes
2118
WHERE version = $1  AND id > $2
2119
ORDER BY id
2120
LIMIT $3
2121
`
2122

2123
type ListNodeIDsAndPubKeysParams struct {
2124
        Version int16
2125
        ID      int64
2126
        Limit   int32
2127
}
2128

2129
type ListNodeIDsAndPubKeysRow struct {
2130
        ID     int64
2131
        PubKey []byte
2132
}
2133

2134
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
2135
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
2136
        if err != nil {
×
2137
                return nil, err
×
2138
        }
×
2139
        defer rows.Close()
×
2140
        var items []ListNodeIDsAndPubKeysRow
×
2141
        for rows.Next() {
×
2142
                var i ListNodeIDsAndPubKeysRow
×
2143
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
2144
                        return nil, err
×
2145
                }
×
2146
                items = append(items, i)
×
2147
        }
2148
        if err := rows.Close(); err != nil {
×
2149
                return nil, err
×
2150
        }
×
2151
        if err := rows.Err(); err != nil {
×
2152
                return nil, err
×
2153
        }
×
2154
        return items, nil
×
2155
}
2156

2157
const listNodesPaginated = `-- name: ListNodesPaginated :many
2158
SELECT id, version, pub_key, alias, last_update, color, signature
2159
FROM nodes
2160
WHERE version = $1 AND id > $2
2161
ORDER BY id
2162
LIMIT $3
2163
`
2164

2165
type ListNodesPaginatedParams struct {
2166
        Version int16
2167
        ID      int64
2168
        Limit   int32
2169
}
2170

2171
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]Node, error) {
×
2172
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
2173
        if err != nil {
×
2174
                return nil, err
×
2175
        }
×
2176
        defer rows.Close()
×
2177
        var items []Node
×
2178
        for rows.Next() {
×
2179
                var i Node
×
2180
                if err := rows.Scan(
×
2181
                        &i.ID,
×
2182
                        &i.Version,
×
2183
                        &i.PubKey,
×
2184
                        &i.Alias,
×
2185
                        &i.LastUpdate,
×
2186
                        &i.Color,
×
2187
                        &i.Signature,
×
2188
                ); err != nil {
×
2189
                        return nil, err
×
2190
                }
×
2191
                items = append(items, i)
×
2192
        }
2193
        if err := rows.Close(); err != nil {
×
2194
                return nil, err
×
2195
        }
×
2196
        if err := rows.Err(); err != nil {
×
2197
                return nil, err
×
2198
        }
×
2199
        return items, nil
×
2200
}
2201

2202
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
2203
/* ─────────────────────────────────────────────
2204
   channel_policies table queries
2205
   ─────────────────────────────────────────────
2206
*/
2207

2208
INSERT INTO channel_policies (
2209
    version, channel_id, node_id, timelock, fee_ppm,
2210
    base_fee_msat, min_htlc_msat, last_update, disabled,
2211
    max_htlc_msat, inbound_base_fee_msat,
2212
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2213
    signature
2214
) VALUES  (
2215
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2216
)
2217
ON CONFLICT (channel_id, node_id, version)
2218
    -- Update the following fields if a conflict occurs on channel_id,
2219
    -- node_id, and version.
2220
    DO UPDATE SET
2221
        timelock = EXCLUDED.timelock,
2222
        fee_ppm = EXCLUDED.fee_ppm,
2223
        base_fee_msat = EXCLUDED.base_fee_msat,
2224
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2225
        last_update = EXCLUDED.last_update,
2226
        disabled = EXCLUDED.disabled,
2227
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2228
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2229
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2230
        message_flags = EXCLUDED.message_flags,
2231
        channel_flags = EXCLUDED.channel_flags,
2232
        signature = EXCLUDED.signature
2233
WHERE EXCLUDED.last_update > channel_policies.last_update
2234
RETURNING id
2235
`
2236

2237
type UpsertEdgePolicyParams struct {
2238
        Version                 int16
2239
        ChannelID               int64
2240
        NodeID                  int64
2241
        Timelock                int32
2242
        FeePpm                  int64
2243
        BaseFeeMsat             int64
2244
        MinHtlcMsat             int64
2245
        LastUpdate              sql.NullInt64
2246
        Disabled                sql.NullBool
2247
        MaxHtlcMsat             sql.NullInt64
2248
        InboundBaseFeeMsat      sql.NullInt64
2249
        InboundFeeRateMilliMsat sql.NullInt64
2250
        MessageFlags            sql.NullInt16
2251
        ChannelFlags            sql.NullInt16
2252
        Signature               []byte
2253
}
2254

2255
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
2256
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
2257
                arg.Version,
×
2258
                arg.ChannelID,
×
2259
                arg.NodeID,
×
2260
                arg.Timelock,
×
2261
                arg.FeePpm,
×
2262
                arg.BaseFeeMsat,
×
2263
                arg.MinHtlcMsat,
×
2264
                arg.LastUpdate,
×
2265
                arg.Disabled,
×
2266
                arg.MaxHtlcMsat,
×
2267
                arg.InboundBaseFeeMsat,
×
2268
                arg.InboundFeeRateMilliMsat,
×
2269
                arg.MessageFlags,
×
2270
                arg.ChannelFlags,
×
2271
                arg.Signature,
×
2272
        )
×
2273
        var id int64
×
2274
        err := row.Scan(&id)
×
2275
        return id, err
×
2276
}
×
2277

2278
const upsertNode = `-- name: UpsertNode :one
2279
/* ─────────────────────────────────────────────
2280
   nodes table queries
2281
   ─────────────────────────────────────────────
2282
*/
2283

2284
INSERT INTO nodes (
2285
    version, pub_key, alias, last_update, color, signature
2286
) VALUES (
2287
    $1, $2, $3, $4, $5, $6
2288
)
2289
ON CONFLICT (pub_key, version)
2290
    -- Update the following fields if a conflict occurs on pub_key
2291
    -- and version.
2292
    DO UPDATE SET
2293
        alias = EXCLUDED.alias,
2294
        last_update = EXCLUDED.last_update,
2295
        color = EXCLUDED.color,
2296
        signature = EXCLUDED.signature
2297
WHERE nodes.last_update IS NULL
2298
    OR EXCLUDED.last_update > nodes.last_update
2299
RETURNING id
2300
`
2301

2302
type UpsertNodeParams struct {
2303
        Version    int16
2304
        PubKey     []byte
2305
        Alias      sql.NullString
2306
        LastUpdate sql.NullInt64
2307
        Color      sql.NullString
2308
        Signature  []byte
2309
}
2310

2311
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
2312
        row := q.db.QueryRowContext(ctx, upsertNode,
×
2313
                arg.Version,
×
2314
                arg.PubKey,
×
2315
                arg.Alias,
×
2316
                arg.LastUpdate,
×
2317
                arg.Color,
×
2318
                arg.Signature,
×
2319
        )
×
2320
        var id int64
×
2321
        err := row.Scan(&id)
×
2322
        return id, err
×
2323
}
×
2324

2325
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
2326
/* ─────────────────────────────────────────────
2327
   node_extra_types table queries
2328
   ─────────────────────────────────────────────
2329
*/
2330

2331
INSERT INTO node_extra_types (
2332
    node_id, type, value
2333
)
2334
VALUES ($1, $2, $3)
2335
ON CONFLICT (type, node_id)
2336
    -- Update the value if a conflict occurs on type
2337
    -- and node_id.
2338
    DO UPDATE SET value = EXCLUDED.value
2339
`
2340

2341
type UpsertNodeExtraTypeParams struct {
2342
        NodeID int64
2343
        Type   int64
2344
        Value  []byte
2345
}
2346

2347
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
2348
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
2349
        return err
×
2350
}
×
2351

2352
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
2353
/* ─────────────────────────────────────────────
2354
    prune_log table queries
2355
    ─────────────────────────────────────────────
2356
*/
2357

2358
INSERT INTO prune_log (
2359
    block_height, block_hash
2360
) VALUES (
2361
    $1, $2
2362
)
2363
ON CONFLICT(block_height) DO UPDATE SET
2364
    block_hash = EXCLUDED.block_hash
2365
`
2366

2367
type UpsertPruneLogEntryParams struct {
2368
        BlockHeight int64
2369
        BlockHash   []byte
2370
}
2371

2372
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
2373
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
2374
        return err
×
2375
}
×
2376

2377
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
2378
/* ─────────────────────────────────────────────
2379
   zombie_channels table queries
2380
   ─────────────────────────────────────────────
2381
*/
2382

2383
INSERT INTO zombie_channels (scid, version, node_key_1, node_key_2)
2384
VALUES ($1, $2, $3, $4)
2385
ON CONFLICT (scid, version)
2386
DO UPDATE SET
2387
    -- If a conflict exists for the SCID and version pair, then we
2388
    -- update the node keys.
2389
    node_key_1 = COALESCE(EXCLUDED.node_key_1, zombie_channels.node_key_1),
2390
    node_key_2 = COALESCE(EXCLUDED.node_key_2, zombie_channels.node_key_2)
2391
`
2392

2393
type UpsertZombieChannelParams struct {
2394
        Scid     []byte
2395
        Version  int16
2396
        NodeKey1 []byte
2397
        NodeKey2 []byte
2398
}
2399

2400
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
2401
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
2402
                arg.Scid,
×
2403
                arg.Version,
×
2404
                arg.NodeKey1,
×
2405
                arg.NodeKey2,
×
2406
        )
×
2407
        return err
×
2408
}
×
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