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

lightningnetwork / lnd / 20917664041

12 Jan 2026 11:26AM UTC coverage: 65.133% (+0.06%) from 65.074%
20917664041

Pull #10414

github

web-flow
Merge 343104337 into 39a1421d1
Pull Request #10414: [g175] graph/db: merge g175 types-prep side branch

449 of 836 new or added lines in 17 files covered. (53.71%)

101 existing lines in 23 files now uncovered.

138153 of 212110 relevant lines covered (65.13%)

20715.84 hits per line

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

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

6
package sqlc
7

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

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

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

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

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

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

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

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

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

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

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

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

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

129
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
130
DELETE FROM graph_channel_policy_extra_types
131
WHERE channel_policy_id = $1
132
`
133

134
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
135
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
136
        return err
×
137
}
×
138

139
const deleteChannels = `-- name: DeleteChannels :exec
140
DELETE FROM graph_channels
141
WHERE id IN (/*SLICE:ids*/?)
142
`
143

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

159
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
160
DELETE FROM graph_node_extra_types
161
WHERE node_id = $1
162
  AND type = $2
163
`
164

165
type DeleteExtraNodeTypeParams struct {
166
        NodeID int64
167
        Type   int64
168
}
169

170
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
171
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
172
        return err
×
173
}
×
174

175
const deleteNode = `-- name: DeleteNode :exec
176
DELETE FROM graph_nodes
177
WHERE id = $1
178
`
179

180
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
181
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
182
        return err
×
183
}
×
184

185
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
186
DELETE FROM graph_node_addresses
187
WHERE node_id = $1
188
`
189

190
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
191
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
192
        return err
×
193
}
×
194

195
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
196
DELETE FROM graph_nodes
197
WHERE pub_key = $1
198
  AND version = $2
199
`
200

201
type DeleteNodeByPubKeyParams struct {
202
        PubKey  []byte
203
        Version int16
204
}
205

206
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
207
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
208
}
×
209

210
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
211
DELETE FROM graph_node_features
212
WHERE node_id = $1
213
  AND feature_bit = $2
214
`
215

216
type DeleteNodeFeatureParams struct {
217
        NodeID     int64
218
        FeatureBit int32
219
}
220

221
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
222
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
223
        return err
×
224
}
×
225

226
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
227
DELETE FROM graph_prune_log
228
WHERE block_height >= $1
229
  AND block_height <= $2
230
`
231

232
type DeletePruneLogEntriesInRangeParams struct {
233
        StartHeight int64
234
        EndHeight   int64
235
}
236

237
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
238
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
239
        return err
×
240
}
×
241

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

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

282
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
283
DELETE FROM graph_zombie_channels
284
WHERE scid = $1
285
AND version = $2
286
`
287

288
type DeleteZombieChannelParams struct {
289
        Scid    []byte
290
        Version int16
291
}
292

293
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
294
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
295
}
×
296

297
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
298
SELECT
299
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
300
    n1.pub_key AS node1_pub_key,
301
    n2.pub_key AS node2_pub_key
302
FROM graph_channels c
303
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
304
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
305
WHERE c.scid = $1
306
  AND c.version = $2
307
`
308

309
type GetChannelAndNodesBySCIDParams struct {
310
        Scid    []byte
311
        Version int16
312
}
313

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

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

361
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
362
SELECT
363
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
364

365
    n1.pub_key AS node1_pubkey,
366
    n2.pub_key AS node2_pubkey,
367

368
    -- Node 1 policy
369
    cp1.id AS policy_1_id,
370
    cp1.node_id AS policy_1_node_id,
371
    cp1.version AS policy_1_version,
372
    cp1.timelock AS policy_1_timelock,
373
    cp1.fee_ppm AS policy_1_fee_ppm,
374
    cp1.base_fee_msat AS policy_1_base_fee_msat,
375
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
376
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
377
    cp1.last_update AS policy_1_last_update,
378
    cp1.disabled AS policy_1_disabled,
379
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
380
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
381
    cp1.message_flags AS policy_1_message_flags,
382
    cp1.channel_flags AS policy_1_channel_flags,
383
    cp1.signature AS policy_1_signature,
384

385
    -- Node 2 policy
386
    cp2.id AS policy_2_id,
387
    cp2.node_id AS policy_2_node_id,
388
    cp2.version AS policy_2_version,
389
    cp2.timelock AS policy_2_timelock,
390
    cp2.fee_ppm AS policy_2_fee_ppm,
391
    cp2.base_fee_msat AS policy_2_base_fee_msat,
392
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
393
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
394
    cp2.last_update AS policy_2_last_update,
395
    cp2.disabled AS policy_2_disabled,
396
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
397
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
398
    cp2.message_flags AS policy_2_message_flags,
399
    cp2.channel_flags AS policy_2_channel_flags,
400
    cp2.signature AS policy_2_signature
401
FROM graph_channels c
402
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
403
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
404
    LEFT JOIN graph_channel_policies cp1
405
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
406
    LEFT JOIN graph_channel_policies cp2
407
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
408
WHERE c.outpoint = $1 AND c.version = $2
409
`
410

411
type GetChannelByOutpointWithPoliciesParams struct {
412
        Outpoint string
413
        Version  int16
414
}
415

416
type GetChannelByOutpointWithPoliciesRow struct {
417
        GraphChannel                   GraphChannel
418
        Node1Pubkey                    []byte
419
        Node2Pubkey                    []byte
420
        Policy1ID                      sql.NullInt64
421
        Policy1NodeID                  sql.NullInt64
422
        Policy1Version                 sql.NullInt16
423
        Policy1Timelock                sql.NullInt32
424
        Policy1FeePpm                  sql.NullInt64
425
        Policy1BaseFeeMsat             sql.NullInt64
426
        Policy1MinHtlcMsat             sql.NullInt64
427
        Policy1MaxHtlcMsat             sql.NullInt64
428
        Policy1LastUpdate              sql.NullInt64
429
        Policy1Disabled                sql.NullBool
430
        Policy1InboundBaseFeeMsat      sql.NullInt64
431
        Policy1InboundFeeRateMilliMsat sql.NullInt64
432
        Policy1MessageFlags            sql.NullInt16
433
        Policy1ChannelFlags            sql.NullInt16
434
        Policy1Signature               []byte
435
        Policy2ID                      sql.NullInt64
436
        Policy2NodeID                  sql.NullInt64
437
        Policy2Version                 sql.NullInt16
438
        Policy2Timelock                sql.NullInt32
439
        Policy2FeePpm                  sql.NullInt64
440
        Policy2BaseFeeMsat             sql.NullInt64
441
        Policy2MinHtlcMsat             sql.NullInt64
442
        Policy2MaxHtlcMsat             sql.NullInt64
443
        Policy2LastUpdate              sql.NullInt64
444
        Policy2Disabled                sql.NullBool
445
        Policy2InboundBaseFeeMsat      sql.NullInt64
446
        Policy2InboundFeeRateMilliMsat sql.NullInt64
447
        Policy2MessageFlags            sql.NullInt16
448
        Policy2ChannelFlags            sql.NullInt16
449
        Policy2Signature               []byte
450
}
451

452
func (q *Queries) GetChannelByOutpointWithPolicies(ctx context.Context, arg GetChannelByOutpointWithPoliciesParams) (GetChannelByOutpointWithPoliciesRow, error) {
×
453
        row := q.db.QueryRowContext(ctx, getChannelByOutpointWithPolicies, arg.Outpoint, arg.Version)
×
454
        var i GetChannelByOutpointWithPoliciesRow
×
455
        err := row.Scan(
×
456
                &i.GraphChannel.ID,
×
457
                &i.GraphChannel.Version,
×
458
                &i.GraphChannel.Scid,
×
459
                &i.GraphChannel.NodeID1,
×
460
                &i.GraphChannel.NodeID2,
×
461
                &i.GraphChannel.Outpoint,
×
462
                &i.GraphChannel.Capacity,
×
463
                &i.GraphChannel.BitcoinKey1,
×
464
                &i.GraphChannel.BitcoinKey2,
×
465
                &i.GraphChannel.Node1Signature,
×
466
                &i.GraphChannel.Node2Signature,
×
467
                &i.GraphChannel.Bitcoin1Signature,
×
468
                &i.GraphChannel.Bitcoin2Signature,
×
NEW
469
                &i.GraphChannel.Signature,
×
NEW
470
                &i.GraphChannel.FundingPkScript,
×
NEW
471
                &i.GraphChannel.MerkleRootHash,
×
472
                &i.Node1Pubkey,
×
473
                &i.Node2Pubkey,
×
474
                &i.Policy1ID,
×
475
                &i.Policy1NodeID,
×
476
                &i.Policy1Version,
×
477
                &i.Policy1Timelock,
×
478
                &i.Policy1FeePpm,
×
479
                &i.Policy1BaseFeeMsat,
×
480
                &i.Policy1MinHtlcMsat,
×
481
                &i.Policy1MaxHtlcMsat,
×
482
                &i.Policy1LastUpdate,
×
483
                &i.Policy1Disabled,
×
484
                &i.Policy1InboundBaseFeeMsat,
×
485
                &i.Policy1InboundFeeRateMilliMsat,
×
486
                &i.Policy1MessageFlags,
×
487
                &i.Policy1ChannelFlags,
×
488
                &i.Policy1Signature,
×
489
                &i.Policy2ID,
×
490
                &i.Policy2NodeID,
×
491
                &i.Policy2Version,
×
492
                &i.Policy2Timelock,
×
493
                &i.Policy2FeePpm,
×
494
                &i.Policy2BaseFeeMsat,
×
495
                &i.Policy2MinHtlcMsat,
×
496
                &i.Policy2MaxHtlcMsat,
×
497
                &i.Policy2LastUpdate,
×
498
                &i.Policy2Disabled,
×
499
                &i.Policy2InboundBaseFeeMsat,
×
500
                &i.Policy2InboundFeeRateMilliMsat,
×
501
                &i.Policy2MessageFlags,
×
502
                &i.Policy2ChannelFlags,
×
503
                &i.Policy2Signature,
×
504
        )
×
505
        return i, err
×
506
}
×
507

508
const getChannelBySCID = `-- name: GetChannelBySCID :one
509
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature, signature, funding_pk_script, merkle_root_hash FROM graph_channels
510
WHERE scid = $1 AND version = $2
511
`
512

513
type GetChannelBySCIDParams struct {
514
        Scid    []byte
515
        Version int16
516
}
517

518
func (q *Queries) GetChannelBySCID(ctx context.Context, arg GetChannelBySCIDParams) (GraphChannel, error) {
×
519
        row := q.db.QueryRowContext(ctx, getChannelBySCID, arg.Scid, arg.Version)
×
520
        var i GraphChannel
×
521
        err := row.Scan(
×
522
                &i.ID,
×
523
                &i.Version,
×
524
                &i.Scid,
×
525
                &i.NodeID1,
×
526
                &i.NodeID2,
×
527
                &i.Outpoint,
×
528
                &i.Capacity,
×
529
                &i.BitcoinKey1,
×
530
                &i.BitcoinKey2,
×
531
                &i.Node1Signature,
×
532
                &i.Node2Signature,
×
533
                &i.Bitcoin1Signature,
×
534
                &i.Bitcoin2Signature,
×
NEW
535
                &i.Signature,
×
NEW
536
                &i.FundingPkScript,
×
NEW
537
                &i.MerkleRootHash,
×
538
        )
×
539
        return i, err
×
540
}
×
541

542
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
543
SELECT
544
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
545
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
546
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
547

548
    -- Policy 1
549
    cp1.id AS policy1_id,
550
    cp1.node_id AS policy1_node_id,
551
    cp1.version AS policy1_version,
552
    cp1.timelock AS policy1_timelock,
553
    cp1.fee_ppm AS policy1_fee_ppm,
554
    cp1.base_fee_msat AS policy1_base_fee_msat,
555
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
556
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
557
    cp1.last_update AS policy1_last_update,
558
    cp1.disabled AS policy1_disabled,
559
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
560
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
561
    cp1.message_flags AS policy1_message_flags,
562
    cp1.channel_flags AS policy1_channel_flags,
563
    cp1.signature AS policy1_signature,
564

565
    -- Policy 2
566
    cp2.id AS policy2_id,
567
    cp2.node_id AS policy2_node_id,
568
    cp2.version AS policy2_version,
569
    cp2.timelock AS policy2_timelock,
570
    cp2.fee_ppm AS policy2_fee_ppm,
571
    cp2.base_fee_msat AS policy2_base_fee_msat,
572
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
573
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
574
    cp2.last_update AS policy2_last_update,
575
    cp2.disabled AS policy2_disabled,
576
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
577
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
578
    cp2.message_flags AS policy_2_message_flags,
579
    cp2.channel_flags AS policy_2_channel_flags,
580
    cp2.signature AS policy2_signature
581

582
FROM graph_channels c
583
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
584
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
585
    LEFT JOIN graph_channel_policies cp1
586
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
587
    LEFT JOIN graph_channel_policies cp2
588
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
589
WHERE c.scid = $1
590
  AND c.version = $2
591
`
592

593
type GetChannelBySCIDWithPoliciesParams struct {
594
        Scid    []byte
595
        Version int16
596
}
597

598
type GetChannelBySCIDWithPoliciesRow struct {
599
        GraphChannel                   GraphChannel
600
        GraphNode                      GraphNode
601
        GraphNode_2                    GraphNode
602
        Policy1ID                      sql.NullInt64
603
        Policy1NodeID                  sql.NullInt64
604
        Policy1Version                 sql.NullInt16
605
        Policy1Timelock                sql.NullInt32
606
        Policy1FeePpm                  sql.NullInt64
607
        Policy1BaseFeeMsat             sql.NullInt64
608
        Policy1MinHtlcMsat             sql.NullInt64
609
        Policy1MaxHtlcMsat             sql.NullInt64
610
        Policy1LastUpdate              sql.NullInt64
611
        Policy1Disabled                sql.NullBool
612
        Policy1InboundBaseFeeMsat      sql.NullInt64
613
        Policy1InboundFeeRateMilliMsat sql.NullInt64
614
        Policy1MessageFlags            sql.NullInt16
615
        Policy1ChannelFlags            sql.NullInt16
616
        Policy1Signature               []byte
617
        Policy2ID                      sql.NullInt64
618
        Policy2NodeID                  sql.NullInt64
619
        Policy2Version                 sql.NullInt16
620
        Policy2Timelock                sql.NullInt32
621
        Policy2FeePpm                  sql.NullInt64
622
        Policy2BaseFeeMsat             sql.NullInt64
623
        Policy2MinHtlcMsat             sql.NullInt64
624
        Policy2MaxHtlcMsat             sql.NullInt64
625
        Policy2LastUpdate              sql.NullInt64
626
        Policy2Disabled                sql.NullBool
627
        Policy2InboundBaseFeeMsat      sql.NullInt64
628
        Policy2InboundFeeRateMilliMsat sql.NullInt64
629
        Policy2MessageFlags            sql.NullInt16
630
        Policy2ChannelFlags            sql.NullInt16
631
        Policy2Signature               []byte
632
}
633

634
func (q *Queries) GetChannelBySCIDWithPolicies(ctx context.Context, arg GetChannelBySCIDWithPoliciesParams) (GetChannelBySCIDWithPoliciesRow, error) {
×
635
        row := q.db.QueryRowContext(ctx, getChannelBySCIDWithPolicies, arg.Scid, arg.Version)
×
636
        var i GetChannelBySCIDWithPoliciesRow
×
637
        err := row.Scan(
×
638
                &i.GraphChannel.ID,
×
639
                &i.GraphChannel.Version,
×
640
                &i.GraphChannel.Scid,
×
641
                &i.GraphChannel.NodeID1,
×
642
                &i.GraphChannel.NodeID2,
×
643
                &i.GraphChannel.Outpoint,
×
644
                &i.GraphChannel.Capacity,
×
645
                &i.GraphChannel.BitcoinKey1,
×
646
                &i.GraphChannel.BitcoinKey2,
×
647
                &i.GraphChannel.Node1Signature,
×
648
                &i.GraphChannel.Node2Signature,
×
649
                &i.GraphChannel.Bitcoin1Signature,
×
650
                &i.GraphChannel.Bitcoin2Signature,
×
NEW
651
                &i.GraphChannel.Signature,
×
NEW
652
                &i.GraphChannel.FundingPkScript,
×
NEW
653
                &i.GraphChannel.MerkleRootHash,
×
654
                &i.GraphNode.ID,
×
655
                &i.GraphNode.Version,
×
656
                &i.GraphNode.PubKey,
×
657
                &i.GraphNode.Alias,
×
658
                &i.GraphNode.LastUpdate,
×
659
                &i.GraphNode.Color,
×
660
                &i.GraphNode.Signature,
×
NEW
661
                &i.GraphNode.BlockHeight,
×
662
                &i.GraphNode_2.ID,
×
663
                &i.GraphNode_2.Version,
×
664
                &i.GraphNode_2.PubKey,
×
665
                &i.GraphNode_2.Alias,
×
666
                &i.GraphNode_2.LastUpdate,
×
667
                &i.GraphNode_2.Color,
×
668
                &i.GraphNode_2.Signature,
×
NEW
669
                &i.GraphNode_2.BlockHeight,
×
670
                &i.Policy1ID,
×
671
                &i.Policy1NodeID,
×
672
                &i.Policy1Version,
×
673
                &i.Policy1Timelock,
×
674
                &i.Policy1FeePpm,
×
675
                &i.Policy1BaseFeeMsat,
×
676
                &i.Policy1MinHtlcMsat,
×
677
                &i.Policy1MaxHtlcMsat,
×
678
                &i.Policy1LastUpdate,
×
679
                &i.Policy1Disabled,
×
680
                &i.Policy1InboundBaseFeeMsat,
×
681
                &i.Policy1InboundFeeRateMilliMsat,
×
682
                &i.Policy1MessageFlags,
×
683
                &i.Policy1ChannelFlags,
×
684
                &i.Policy1Signature,
×
685
                &i.Policy2ID,
×
686
                &i.Policy2NodeID,
×
687
                &i.Policy2Version,
×
688
                &i.Policy2Timelock,
×
689
                &i.Policy2FeePpm,
×
690
                &i.Policy2BaseFeeMsat,
×
691
                &i.Policy2MinHtlcMsat,
×
692
                &i.Policy2MaxHtlcMsat,
×
693
                &i.Policy2LastUpdate,
×
694
                &i.Policy2Disabled,
×
695
                &i.Policy2InboundBaseFeeMsat,
×
696
                &i.Policy2InboundFeeRateMilliMsat,
×
697
                &i.Policy2MessageFlags,
×
698
                &i.Policy2ChannelFlags,
×
699
                &i.Policy2Signature,
×
700
        )
×
701
        return i, err
×
702
}
×
703

704
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
705
SELECT
706
    channel_id,
707
    type,
708
    value
709
FROM graph_channel_extra_types
710
WHERE channel_id IN (/*SLICE:chan_ids*/?)
711
ORDER BY channel_id, type
712
`
713

714
func (q *Queries) GetChannelExtrasBatch(ctx context.Context, chanIds []int64) ([]GraphChannelExtraType, error) {
×
715
        query := getChannelExtrasBatch
×
716
        var queryParams []interface{}
×
717
        if len(chanIds) > 0 {
×
718
                for _, v := range chanIds {
×
719
                        queryParams = append(queryParams, v)
×
720
                }
×
721
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", makeQueryParams(len(queryParams), len(chanIds)), 1)
×
722
        } else {
×
723
                query = strings.Replace(query, "/*SLICE:chan_ids*/?", "NULL", 1)
×
724
        }
×
725
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
726
        if err != nil {
×
727
                return nil, err
×
728
        }
×
729
        defer rows.Close()
×
730
        var items []GraphChannelExtraType
×
731
        for rows.Next() {
×
732
                var i GraphChannelExtraType
×
733
                if err := rows.Scan(&i.ChannelID, &i.Type, &i.Value); err != nil {
×
734
                        return nil, err
×
735
                }
×
736
                items = append(items, i)
×
737
        }
738
        if err := rows.Close(); err != nil {
×
739
                return nil, err
×
740
        }
×
741
        if err := rows.Err(); err != nil {
×
742
                return nil, err
×
743
        }
×
744
        return items, nil
×
745
}
746

747
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
748
SELECT
749
    channel_id,
750
    feature_bit
751
FROM graph_channel_features
752
WHERE channel_id IN (/*SLICE:chan_ids*/?)
753
ORDER BY channel_id, feature_bit
754
`
755

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

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

797
type GetChannelPolicyByChannelAndNodeParams struct {
798
        ChannelID int64
799
        NodeID    int64
800
        Version   int16
801
}
802

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

829
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
830
SELECT
831
    channel_policy_id as policy_id,
832
    type,
833
    value
834
FROM graph_channel_policy_extra_types
835
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
836
ORDER BY channel_policy_id, type
837
`
838

839
type GetChannelPolicyExtraTypesBatchRow struct {
840
        PolicyID int64
841
        Type     int64
842
        Value    []byte
843
}
844

845
func (q *Queries) GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]GetChannelPolicyExtraTypesBatchRow, error) {
×
846
        query := getChannelPolicyExtraTypesBatch
×
847
        var queryParams []interface{}
×
848
        if len(policyIds) > 0 {
×
849
                for _, v := range policyIds {
×
850
                        queryParams = append(queryParams, v)
×
851
                }
×
852
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", makeQueryParams(len(queryParams), len(policyIds)), 1)
×
853
        } else {
×
854
                query = strings.Replace(query, "/*SLICE:policy_ids*/?", "NULL", 1)
×
855
        }
×
856
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
857
        if err != nil {
×
858
                return nil, err
×
859
        }
×
860
        defer rows.Close()
×
861
        var items []GetChannelPolicyExtraTypesBatchRow
×
862
        for rows.Next() {
×
863
                var i GetChannelPolicyExtraTypesBatchRow
×
864
                if err := rows.Scan(&i.PolicyID, &i.Type, &i.Value); err != nil {
×
865
                        return nil, err
×
866
                }
×
867
                items = append(items, i)
×
868
        }
869
        if err := rows.Close(); err != nil {
×
870
                return nil, err
×
871
        }
×
872
        if err := rows.Err(); err != nil {
×
873
                return nil, err
×
874
        }
×
875
        return items, nil
×
876
}
877

878
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
879
SELECT
880
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
881

882
    -- Minimal node data.
883
    n1.id AS node1_id,
884
    n1.pub_key AS node1_pub_key,
885
    n2.id AS node2_id,
886
    n2.pub_key AS node2_pub_key,
887

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

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

922
FROM graph_channels c
923
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
924
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
925
    LEFT JOIN graph_channel_policies cp1
926
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
927
    LEFT JOIN graph_channel_policies cp2
928
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
929
WHERE c.id IN (/*SLICE:ids*/?)
930
`
931

932
type GetChannelsByIDsRow struct {
933
        GraphChannel                   GraphChannel
934
        Node1ID                        int64
935
        Node1PubKey                    []byte
936
        Node2ID                        int64
937
        Node2PubKey                    []byte
938
        Policy1ID                      sql.NullInt64
939
        Policy1NodeID                  sql.NullInt64
940
        Policy1Version                 sql.NullInt16
941
        Policy1Timelock                sql.NullInt32
942
        Policy1FeePpm                  sql.NullInt64
943
        Policy1BaseFeeMsat             sql.NullInt64
944
        Policy1MinHtlcMsat             sql.NullInt64
945
        Policy1MaxHtlcMsat             sql.NullInt64
946
        Policy1LastUpdate              sql.NullInt64
947
        Policy1Disabled                sql.NullBool
948
        Policy1InboundBaseFeeMsat      sql.NullInt64
949
        Policy1InboundFeeRateMilliMsat sql.NullInt64
950
        Policy1MessageFlags            sql.NullInt16
951
        Policy1ChannelFlags            sql.NullInt16
952
        Policy1Signature               []byte
953
        Policy2ID                      sql.NullInt64
954
        Policy2NodeID                  sql.NullInt64
955
        Policy2Version                 sql.NullInt16
956
        Policy2Timelock                sql.NullInt32
957
        Policy2FeePpm                  sql.NullInt64
958
        Policy2BaseFeeMsat             sql.NullInt64
959
        Policy2MinHtlcMsat             sql.NullInt64
960
        Policy2MaxHtlcMsat             sql.NullInt64
961
        Policy2LastUpdate              sql.NullInt64
962
        Policy2Disabled                sql.NullBool
963
        Policy2InboundBaseFeeMsat      sql.NullInt64
964
        Policy2InboundFeeRateMilliMsat sql.NullInt64
965
        Policy2MessageFlags            sql.NullInt16
966
        Policy2ChannelFlags            sql.NullInt16
967
        Policy2Signature               []byte
968
}
969

970
func (q *Queries) GetChannelsByIDs(ctx context.Context, ids []int64) ([]GetChannelsByIDsRow, error) {
×
971
        query := getChannelsByIDs
×
972
        var queryParams []interface{}
×
973
        if len(ids) > 0 {
×
974
                for _, v := range ids {
×
975
                        queryParams = append(queryParams, v)
×
976
                }
×
977
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
978
        } else {
×
979
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
980
        }
×
981
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
982
        if err != nil {
×
983
                return nil, err
×
984
        }
×
985
        defer rows.Close()
×
986
        var items []GetChannelsByIDsRow
×
987
        for rows.Next() {
×
988
                var i GetChannelsByIDsRow
×
989
                if err := rows.Scan(
×
990
                        &i.GraphChannel.ID,
×
991
                        &i.GraphChannel.Version,
×
992
                        &i.GraphChannel.Scid,
×
993
                        &i.GraphChannel.NodeID1,
×
994
                        &i.GraphChannel.NodeID2,
×
995
                        &i.GraphChannel.Outpoint,
×
996
                        &i.GraphChannel.Capacity,
×
997
                        &i.GraphChannel.BitcoinKey1,
×
998
                        &i.GraphChannel.BitcoinKey2,
×
999
                        &i.GraphChannel.Node1Signature,
×
1000
                        &i.GraphChannel.Node2Signature,
×
1001
                        &i.GraphChannel.Bitcoin1Signature,
×
1002
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1003
                        &i.GraphChannel.Signature,
×
NEW
1004
                        &i.GraphChannel.FundingPkScript,
×
NEW
1005
                        &i.GraphChannel.MerkleRootHash,
×
1006
                        &i.Node1ID,
×
1007
                        &i.Node1PubKey,
×
1008
                        &i.Node2ID,
×
1009
                        &i.Node2PubKey,
×
1010
                        &i.Policy1ID,
×
1011
                        &i.Policy1NodeID,
×
1012
                        &i.Policy1Version,
×
1013
                        &i.Policy1Timelock,
×
1014
                        &i.Policy1FeePpm,
×
1015
                        &i.Policy1BaseFeeMsat,
×
1016
                        &i.Policy1MinHtlcMsat,
×
1017
                        &i.Policy1MaxHtlcMsat,
×
1018
                        &i.Policy1LastUpdate,
×
1019
                        &i.Policy1Disabled,
×
1020
                        &i.Policy1InboundBaseFeeMsat,
×
1021
                        &i.Policy1InboundFeeRateMilliMsat,
×
1022
                        &i.Policy1MessageFlags,
×
1023
                        &i.Policy1ChannelFlags,
×
1024
                        &i.Policy1Signature,
×
1025
                        &i.Policy2ID,
×
1026
                        &i.Policy2NodeID,
×
1027
                        &i.Policy2Version,
×
1028
                        &i.Policy2Timelock,
×
1029
                        &i.Policy2FeePpm,
×
1030
                        &i.Policy2BaseFeeMsat,
×
1031
                        &i.Policy2MinHtlcMsat,
×
1032
                        &i.Policy2MaxHtlcMsat,
×
1033
                        &i.Policy2LastUpdate,
×
1034
                        &i.Policy2Disabled,
×
1035
                        &i.Policy2InboundBaseFeeMsat,
×
1036
                        &i.Policy2InboundFeeRateMilliMsat,
×
1037
                        &i.Policy2MessageFlags,
×
1038
                        &i.Policy2ChannelFlags,
×
1039
                        &i.Policy2Signature,
×
1040
                ); err != nil {
×
1041
                        return nil, err
×
1042
                }
×
1043
                items = append(items, i)
×
1044
        }
1045
        if err := rows.Close(); err != nil {
×
1046
                return nil, err
×
1047
        }
×
1048
        if err := rows.Err(); err != nil {
×
1049
                return nil, err
×
1050
        }
×
1051
        return items, nil
×
1052
}
1053

1054
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1055
SELECT
1056
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1057
    n1.pub_key AS node1_pubkey,
1058
    n2.pub_key AS node2_pubkey
1059
FROM graph_channels c
1060
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1061
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1062
WHERE c.outpoint IN
1063
    (/*SLICE:outpoints*/?)
1064
`
1065

1066
type GetChannelsByOutpointsRow struct {
1067
        GraphChannel GraphChannel
1068
        Node1Pubkey  []byte
1069
        Node2Pubkey  []byte
1070
}
1071

1072
func (q *Queries) GetChannelsByOutpoints(ctx context.Context, outpoints []string) ([]GetChannelsByOutpointsRow, error) {
×
1073
        query := getChannelsByOutpoints
×
1074
        var queryParams []interface{}
×
1075
        if len(outpoints) > 0 {
×
1076
                for _, v := range outpoints {
×
1077
                        queryParams = append(queryParams, v)
×
1078
                }
×
1079
                query = strings.Replace(query, "/*SLICE:outpoints*/?", makeQueryParams(len(queryParams), len(outpoints)), 1)
×
1080
        } else {
×
1081
                query = strings.Replace(query, "/*SLICE:outpoints*/?", "NULL", 1)
×
1082
        }
×
1083
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1084
        if err != nil {
×
1085
                return nil, err
×
1086
        }
×
1087
        defer rows.Close()
×
1088
        var items []GetChannelsByOutpointsRow
×
1089
        for rows.Next() {
×
1090
                var i GetChannelsByOutpointsRow
×
1091
                if err := rows.Scan(
×
1092
                        &i.GraphChannel.ID,
×
1093
                        &i.GraphChannel.Version,
×
1094
                        &i.GraphChannel.Scid,
×
1095
                        &i.GraphChannel.NodeID1,
×
1096
                        &i.GraphChannel.NodeID2,
×
1097
                        &i.GraphChannel.Outpoint,
×
1098
                        &i.GraphChannel.Capacity,
×
1099
                        &i.GraphChannel.BitcoinKey1,
×
1100
                        &i.GraphChannel.BitcoinKey2,
×
1101
                        &i.GraphChannel.Node1Signature,
×
1102
                        &i.GraphChannel.Node2Signature,
×
1103
                        &i.GraphChannel.Bitcoin1Signature,
×
1104
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1105
                        &i.GraphChannel.Signature,
×
NEW
1106
                        &i.GraphChannel.FundingPkScript,
×
NEW
1107
                        &i.GraphChannel.MerkleRootHash,
×
1108
                        &i.Node1Pubkey,
×
1109
                        &i.Node2Pubkey,
×
1110
                ); err != nil {
×
1111
                        return nil, err
×
1112
                }
×
1113
                items = append(items, i)
×
1114
        }
1115
        if err := rows.Close(); err != nil {
×
1116
                return nil, err
×
1117
        }
×
1118
        if err := rows.Err(); err != nil {
×
1119
                return nil, err
×
1120
        }
×
1121
        return items, nil
×
1122
}
1123

1124
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1125
SELECT
1126
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1127
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1128
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1129

1130
    -- Policy 1 (node_id_1)
1131
    cp1.id AS policy1_id,
1132
    cp1.node_id AS policy1_node_id,
1133
    cp1.version AS policy1_version,
1134
    cp1.timelock AS policy1_timelock,
1135
    cp1.fee_ppm AS policy1_fee_ppm,
1136
    cp1.base_fee_msat AS policy1_base_fee_msat,
1137
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
1138
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
1139
    cp1.last_update AS policy1_last_update,
1140
    cp1.disabled AS policy1_disabled,
1141
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
1142
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
1143
    cp1.message_flags AS policy1_message_flags,
1144
    cp1.channel_flags AS policy1_channel_flags,
1145
    cp1.signature AS policy1_signature,
1146

1147
    -- Policy 2 (node_id_2)
1148
    cp2.id AS policy2_id,
1149
    cp2.node_id AS policy2_node_id,
1150
    cp2.version AS policy2_version,
1151
    cp2.timelock AS policy2_timelock,
1152
    cp2.fee_ppm AS policy2_fee_ppm,
1153
    cp2.base_fee_msat AS policy2_base_fee_msat,
1154
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1155
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1156
    cp2.last_update AS policy2_last_update,
1157
    cp2.disabled AS policy2_disabled,
1158
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1159
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1160
    cp2.message_flags AS policy2_message_flags,
1161
    cp2.channel_flags AS policy2_channel_flags,
1162
    cp2.signature AS policy2_signature
1163

1164
FROM graph_channels c
1165
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1166
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1167
    LEFT JOIN graph_channel_policies cp1
1168
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1169
    LEFT JOIN graph_channel_policies cp2
1170
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1171
WHERE c.version = $1
1172
  AND (
1173
       (cp1.last_update >= $2 AND cp1.last_update < $3)
1174
       OR
1175
       (cp2.last_update >= $2 AND cp2.last_update < $3)
1176
  )
1177
  -- Pagination using compound cursor (max_update_time, id).
1178
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
1179
  AND (
1180
       (CASE
1181
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1182
               THEN COALESCE(cp1.last_update, 0)
1183
           ELSE COALESCE(cp2.last_update, 0)
1184
       END > COALESCE($4, -1))
1185
       OR 
1186
       (CASE
1187
           WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1188
               THEN COALESCE(cp1.last_update, 0)
1189
           ELSE COALESCE(cp2.last_update, 0)
1190
       END = COALESCE($4, -1) 
1191
       AND c.id > COALESCE($5, -1))
1192
  )
1193
ORDER BY
1194
    CASE
1195
        WHEN COALESCE(cp1.last_update, 0) >= COALESCE(cp2.last_update, 0)
1196
            THEN COALESCE(cp1.last_update, 0)
1197
        ELSE COALESCE(cp2.last_update, 0)
1198
    END ASC,
1199
    c.id ASC
1200
LIMIT COALESCE($6, 999999999)
1201
`
1202

1203
type GetChannelsByPolicyLastUpdateRangeParams struct {
1204
        Version        int16
1205
        StartTime      sql.NullInt64
1206
        EndTime        sql.NullInt64
1207
        LastUpdateTime sql.NullInt64
1208
        LastID         sql.NullInt64
1209
        MaxResults     interface{}
1210
}
1211

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

1248
func (q *Queries) GetChannelsByPolicyLastUpdateRange(ctx context.Context, arg GetChannelsByPolicyLastUpdateRangeParams) ([]GetChannelsByPolicyLastUpdateRangeRow, error) {
×
1249
        rows, err := q.db.QueryContext(ctx, getChannelsByPolicyLastUpdateRange,
×
1250
                arg.Version,
×
1251
                arg.StartTime,
×
1252
                arg.EndTime,
×
1253
                arg.LastUpdateTime,
×
1254
                arg.LastID,
×
1255
                arg.MaxResults,
×
1256
        )
×
1257
        if err != nil {
×
1258
                return nil, err
×
1259
        }
×
1260
        defer rows.Close()
×
1261
        var items []GetChannelsByPolicyLastUpdateRangeRow
×
1262
        for rows.Next() {
×
1263
                var i GetChannelsByPolicyLastUpdateRangeRow
×
1264
                if err := rows.Scan(
×
1265
                        &i.GraphChannel.ID,
×
1266
                        &i.GraphChannel.Version,
×
1267
                        &i.GraphChannel.Scid,
×
1268
                        &i.GraphChannel.NodeID1,
×
1269
                        &i.GraphChannel.NodeID2,
×
1270
                        &i.GraphChannel.Outpoint,
×
1271
                        &i.GraphChannel.Capacity,
×
1272
                        &i.GraphChannel.BitcoinKey1,
×
1273
                        &i.GraphChannel.BitcoinKey2,
×
1274
                        &i.GraphChannel.Node1Signature,
×
1275
                        &i.GraphChannel.Node2Signature,
×
1276
                        &i.GraphChannel.Bitcoin1Signature,
×
1277
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1278
                        &i.GraphChannel.Signature,
×
NEW
1279
                        &i.GraphChannel.FundingPkScript,
×
NEW
1280
                        &i.GraphChannel.MerkleRootHash,
×
1281
                        &i.GraphNode.ID,
×
1282
                        &i.GraphNode.Version,
×
1283
                        &i.GraphNode.PubKey,
×
1284
                        &i.GraphNode.Alias,
×
1285
                        &i.GraphNode.LastUpdate,
×
1286
                        &i.GraphNode.Color,
×
1287
                        &i.GraphNode.Signature,
×
NEW
1288
                        &i.GraphNode.BlockHeight,
×
1289
                        &i.GraphNode_2.ID,
×
1290
                        &i.GraphNode_2.Version,
×
1291
                        &i.GraphNode_2.PubKey,
×
1292
                        &i.GraphNode_2.Alias,
×
1293
                        &i.GraphNode_2.LastUpdate,
×
1294
                        &i.GraphNode_2.Color,
×
1295
                        &i.GraphNode_2.Signature,
×
NEW
1296
                        &i.GraphNode_2.BlockHeight,
×
1297
                        &i.Policy1ID,
×
1298
                        &i.Policy1NodeID,
×
1299
                        &i.Policy1Version,
×
1300
                        &i.Policy1Timelock,
×
1301
                        &i.Policy1FeePpm,
×
1302
                        &i.Policy1BaseFeeMsat,
×
1303
                        &i.Policy1MinHtlcMsat,
×
1304
                        &i.Policy1MaxHtlcMsat,
×
1305
                        &i.Policy1LastUpdate,
×
1306
                        &i.Policy1Disabled,
×
1307
                        &i.Policy1InboundBaseFeeMsat,
×
1308
                        &i.Policy1InboundFeeRateMilliMsat,
×
1309
                        &i.Policy1MessageFlags,
×
1310
                        &i.Policy1ChannelFlags,
×
1311
                        &i.Policy1Signature,
×
1312
                        &i.Policy2ID,
×
1313
                        &i.Policy2NodeID,
×
1314
                        &i.Policy2Version,
×
1315
                        &i.Policy2Timelock,
×
1316
                        &i.Policy2FeePpm,
×
1317
                        &i.Policy2BaseFeeMsat,
×
1318
                        &i.Policy2MinHtlcMsat,
×
1319
                        &i.Policy2MaxHtlcMsat,
×
1320
                        &i.Policy2LastUpdate,
×
1321
                        &i.Policy2Disabled,
×
1322
                        &i.Policy2InboundBaseFeeMsat,
×
1323
                        &i.Policy2InboundFeeRateMilliMsat,
×
1324
                        &i.Policy2MessageFlags,
×
1325
                        &i.Policy2ChannelFlags,
×
1326
                        &i.Policy2Signature,
×
1327
                ); err != nil {
×
1328
                        return nil, err
×
1329
                }
×
1330
                items = append(items, i)
×
1331
        }
1332
        if err := rows.Close(); err != nil {
×
1333
                return nil, err
×
1334
        }
×
1335
        if err := rows.Err(); err != nil {
×
1336
                return nil, err
×
1337
        }
×
1338
        return items, nil
×
1339
}
1340

1341
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1342
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
1343
    n1.pub_key AS node1_pub_key,
1344
    n2.pub_key AS node2_pub_key
1345
FROM graph_channels c
1346
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1347
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1348
WHERE scid >= $1
1349
  AND scid < $2
1350
`
1351

1352
type GetChannelsBySCIDRangeParams struct {
1353
        StartScid []byte
1354
        EndScid   []byte
1355
}
1356

1357
type GetChannelsBySCIDRangeRow struct {
1358
        GraphChannel GraphChannel
1359
        Node1PubKey  []byte
1360
        Node2PubKey  []byte
1361
}
1362

1363
func (q *Queries) GetChannelsBySCIDRange(ctx context.Context, arg GetChannelsBySCIDRangeParams) ([]GetChannelsBySCIDRangeRow, error) {
×
1364
        rows, err := q.db.QueryContext(ctx, getChannelsBySCIDRange, arg.StartScid, arg.EndScid)
×
1365
        if err != nil {
×
1366
                return nil, err
×
1367
        }
×
1368
        defer rows.Close()
×
1369
        var items []GetChannelsBySCIDRangeRow
×
1370
        for rows.Next() {
×
1371
                var i GetChannelsBySCIDRangeRow
×
1372
                if err := rows.Scan(
×
1373
                        &i.GraphChannel.ID,
×
1374
                        &i.GraphChannel.Version,
×
1375
                        &i.GraphChannel.Scid,
×
1376
                        &i.GraphChannel.NodeID1,
×
1377
                        &i.GraphChannel.NodeID2,
×
1378
                        &i.GraphChannel.Outpoint,
×
1379
                        &i.GraphChannel.Capacity,
×
1380
                        &i.GraphChannel.BitcoinKey1,
×
1381
                        &i.GraphChannel.BitcoinKey2,
×
1382
                        &i.GraphChannel.Node1Signature,
×
1383
                        &i.GraphChannel.Node2Signature,
×
1384
                        &i.GraphChannel.Bitcoin1Signature,
×
1385
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1386
                        &i.GraphChannel.Signature,
×
NEW
1387
                        &i.GraphChannel.FundingPkScript,
×
NEW
1388
                        &i.GraphChannel.MerkleRootHash,
×
1389
                        &i.Node1PubKey,
×
1390
                        &i.Node2PubKey,
×
1391
                ); err != nil {
×
1392
                        return nil, err
×
1393
                }
×
1394
                items = append(items, i)
×
1395
        }
1396
        if err := rows.Close(); err != nil {
×
1397
                return nil, err
×
1398
        }
×
1399
        if err := rows.Err(); err != nil {
×
1400
                return nil, err
×
1401
        }
×
1402
        return items, nil
×
1403
}
1404

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

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

1428
    -- Policy 2
1429
    cp2.id AS policy2_id,
1430
    cp2.node_id AS policy2_node_id,
1431
    cp2.version AS policy2_version,
1432
    cp2.timelock AS policy2_timelock,
1433
    cp2.fee_ppm AS policy2_fee_ppm,
1434
    cp2.base_fee_msat AS policy2_base_fee_msat,
1435
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
1436
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
1437
    cp2.last_update AS policy2_last_update,
1438
    cp2.disabled AS policy2_disabled,
1439
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
1440
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
1441
    cp2.message_flags AS policy_2_message_flags,
1442
    cp2.channel_flags AS policy_2_channel_flags,
1443
    cp2.signature AS policy2_signature
1444

1445
FROM graph_channels c
1446
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1447
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1448
    LEFT JOIN graph_channel_policies cp1
1449
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1450
    LEFT JOIN graph_channel_policies cp2
1451
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1452
WHERE
1453
    c.version = $1
1454
  AND c.scid IN (/*SLICE:scids*/?)
1455
`
1456

1457
type GetChannelsBySCIDWithPoliciesParams struct {
1458
        Version int16
1459
        Scids   [][]byte
1460
}
1461

1462
type GetChannelsBySCIDWithPoliciesRow struct {
1463
        GraphChannel                   GraphChannel
1464
        GraphNode                      GraphNode
1465
        GraphNode_2                    GraphNode
1466
        Policy1ID                      sql.NullInt64
1467
        Policy1NodeID                  sql.NullInt64
1468
        Policy1Version                 sql.NullInt16
1469
        Policy1Timelock                sql.NullInt32
1470
        Policy1FeePpm                  sql.NullInt64
1471
        Policy1BaseFeeMsat             sql.NullInt64
1472
        Policy1MinHtlcMsat             sql.NullInt64
1473
        Policy1MaxHtlcMsat             sql.NullInt64
1474
        Policy1LastUpdate              sql.NullInt64
1475
        Policy1Disabled                sql.NullBool
1476
        Policy1InboundBaseFeeMsat      sql.NullInt64
1477
        Policy1InboundFeeRateMilliMsat sql.NullInt64
1478
        Policy1MessageFlags            sql.NullInt16
1479
        Policy1ChannelFlags            sql.NullInt16
1480
        Policy1Signature               []byte
1481
        Policy2ID                      sql.NullInt64
1482
        Policy2NodeID                  sql.NullInt64
1483
        Policy2Version                 sql.NullInt16
1484
        Policy2Timelock                sql.NullInt32
1485
        Policy2FeePpm                  sql.NullInt64
1486
        Policy2BaseFeeMsat             sql.NullInt64
1487
        Policy2MinHtlcMsat             sql.NullInt64
1488
        Policy2MaxHtlcMsat             sql.NullInt64
1489
        Policy2LastUpdate              sql.NullInt64
1490
        Policy2Disabled                sql.NullBool
1491
        Policy2InboundBaseFeeMsat      sql.NullInt64
1492
        Policy2InboundFeeRateMilliMsat sql.NullInt64
1493
        Policy2MessageFlags            sql.NullInt16
1494
        Policy2ChannelFlags            sql.NullInt16
1495
        Policy2Signature               []byte
1496
}
1497

1498
func (q *Queries) GetChannelsBySCIDWithPolicies(ctx context.Context, arg GetChannelsBySCIDWithPoliciesParams) ([]GetChannelsBySCIDWithPoliciesRow, error) {
×
1499
        query := getChannelsBySCIDWithPolicies
×
1500
        var queryParams []interface{}
×
1501
        queryParams = append(queryParams, arg.Version)
×
1502
        if len(arg.Scids) > 0 {
×
1503
                for _, v := range arg.Scids {
×
1504
                        queryParams = append(queryParams, v)
×
1505
                }
×
1506
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1507
        } else {
×
1508
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1509
        }
×
1510
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1511
        if err != nil {
×
1512
                return nil, err
×
1513
        }
×
1514
        defer rows.Close()
×
1515
        var items []GetChannelsBySCIDWithPoliciesRow
×
1516
        for rows.Next() {
×
1517
                var i GetChannelsBySCIDWithPoliciesRow
×
1518
                if err := rows.Scan(
×
1519
                        &i.GraphChannel.ID,
×
1520
                        &i.GraphChannel.Version,
×
1521
                        &i.GraphChannel.Scid,
×
1522
                        &i.GraphChannel.NodeID1,
×
1523
                        &i.GraphChannel.NodeID2,
×
1524
                        &i.GraphChannel.Outpoint,
×
1525
                        &i.GraphChannel.Capacity,
×
1526
                        &i.GraphChannel.BitcoinKey1,
×
1527
                        &i.GraphChannel.BitcoinKey2,
×
1528
                        &i.GraphChannel.Node1Signature,
×
1529
                        &i.GraphChannel.Node2Signature,
×
1530
                        &i.GraphChannel.Bitcoin1Signature,
×
1531
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
1532
                        &i.GraphChannel.Signature,
×
NEW
1533
                        &i.GraphChannel.FundingPkScript,
×
NEW
1534
                        &i.GraphChannel.MerkleRootHash,
×
1535
                        &i.GraphNode.ID,
×
1536
                        &i.GraphNode.Version,
×
1537
                        &i.GraphNode.PubKey,
×
1538
                        &i.GraphNode.Alias,
×
1539
                        &i.GraphNode.LastUpdate,
×
1540
                        &i.GraphNode.Color,
×
1541
                        &i.GraphNode.Signature,
×
NEW
1542
                        &i.GraphNode.BlockHeight,
×
1543
                        &i.GraphNode_2.ID,
×
1544
                        &i.GraphNode_2.Version,
×
1545
                        &i.GraphNode_2.PubKey,
×
1546
                        &i.GraphNode_2.Alias,
×
1547
                        &i.GraphNode_2.LastUpdate,
×
1548
                        &i.GraphNode_2.Color,
×
1549
                        &i.GraphNode_2.Signature,
×
NEW
1550
                        &i.GraphNode_2.BlockHeight,
×
1551
                        &i.Policy1ID,
×
1552
                        &i.Policy1NodeID,
×
1553
                        &i.Policy1Version,
×
1554
                        &i.Policy1Timelock,
×
1555
                        &i.Policy1FeePpm,
×
1556
                        &i.Policy1BaseFeeMsat,
×
1557
                        &i.Policy1MinHtlcMsat,
×
1558
                        &i.Policy1MaxHtlcMsat,
×
1559
                        &i.Policy1LastUpdate,
×
1560
                        &i.Policy1Disabled,
×
1561
                        &i.Policy1InboundBaseFeeMsat,
×
1562
                        &i.Policy1InboundFeeRateMilliMsat,
×
1563
                        &i.Policy1MessageFlags,
×
1564
                        &i.Policy1ChannelFlags,
×
1565
                        &i.Policy1Signature,
×
1566
                        &i.Policy2ID,
×
1567
                        &i.Policy2NodeID,
×
1568
                        &i.Policy2Version,
×
1569
                        &i.Policy2Timelock,
×
1570
                        &i.Policy2FeePpm,
×
1571
                        &i.Policy2BaseFeeMsat,
×
1572
                        &i.Policy2MinHtlcMsat,
×
1573
                        &i.Policy2MaxHtlcMsat,
×
1574
                        &i.Policy2LastUpdate,
×
1575
                        &i.Policy2Disabled,
×
1576
                        &i.Policy2InboundBaseFeeMsat,
×
1577
                        &i.Policy2InboundFeeRateMilliMsat,
×
1578
                        &i.Policy2MessageFlags,
×
1579
                        &i.Policy2ChannelFlags,
×
1580
                        &i.Policy2Signature,
×
1581
                ); err != nil {
×
1582
                        return nil, err
×
1583
                }
×
1584
                items = append(items, i)
×
1585
        }
1586
        if err := rows.Close(); err != nil {
×
1587
                return nil, err
×
1588
        }
×
1589
        if err := rows.Err(); err != nil {
×
1590
                return nil, err
×
1591
        }
×
1592
        return items, nil
×
1593
}
1594

1595
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1596
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature, signature, funding_pk_script, merkle_root_hash FROM graph_channels
1597
WHERE version = $1
1598
  AND scid IN (/*SLICE:scids*/?)
1599
`
1600

1601
type GetChannelsBySCIDsParams struct {
1602
        Version int16
1603
        Scids   [][]byte
1604
}
1605

1606
func (q *Queries) GetChannelsBySCIDs(ctx context.Context, arg GetChannelsBySCIDsParams) ([]GraphChannel, error) {
×
1607
        query := getChannelsBySCIDs
×
1608
        var queryParams []interface{}
×
1609
        queryParams = append(queryParams, arg.Version)
×
1610
        if len(arg.Scids) > 0 {
×
1611
                for _, v := range arg.Scids {
×
1612
                        queryParams = append(queryParams, v)
×
1613
                }
×
1614
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
1615
        } else {
×
1616
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1617
        }
×
1618
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1619
        if err != nil {
×
1620
                return nil, err
×
1621
        }
×
1622
        defer rows.Close()
×
1623
        var items []GraphChannel
×
1624
        for rows.Next() {
×
1625
                var i GraphChannel
×
1626
                if err := rows.Scan(
×
1627
                        &i.ID,
×
1628
                        &i.Version,
×
1629
                        &i.Scid,
×
1630
                        &i.NodeID1,
×
1631
                        &i.NodeID2,
×
1632
                        &i.Outpoint,
×
1633
                        &i.Capacity,
×
1634
                        &i.BitcoinKey1,
×
1635
                        &i.BitcoinKey2,
×
1636
                        &i.Node1Signature,
×
1637
                        &i.Node2Signature,
×
1638
                        &i.Bitcoin1Signature,
×
1639
                        &i.Bitcoin2Signature,
×
NEW
1640
                        &i.Signature,
×
NEW
1641
                        &i.FundingPkScript,
×
NEW
1642
                        &i.MerkleRootHash,
×
1643
                ); err != nil {
×
1644
                        return nil, err
×
1645
                }
×
1646
                items = append(items, i)
×
1647
        }
1648
        if err := rows.Close(); err != nil {
×
1649
                return nil, err
×
1650
        }
×
1651
        if err := rows.Err(); err != nil {
×
1652
                return nil, err
×
1653
        }
×
1654
        return items, nil
×
1655
}
1656

1657
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1658
SELECT scid
1659
FROM graph_closed_scids
1660
WHERE scid IN (/*SLICE:scids*/?)
1661
`
1662

1663
func (q *Queries) GetClosedChannelsSCIDs(ctx context.Context, scids [][]byte) ([][]byte, error) {
×
1664
        query := getClosedChannelsSCIDs
×
1665
        var queryParams []interface{}
×
1666
        if len(scids) > 0 {
×
1667
                for _, v := range scids {
×
1668
                        queryParams = append(queryParams, v)
×
1669
                }
×
1670
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(scids)), 1)
×
1671
        } else {
×
1672
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
1673
        }
×
1674
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1675
        if err != nil {
×
1676
                return nil, err
×
1677
        }
×
1678
        defer rows.Close()
×
1679
        var items [][]byte
×
1680
        for rows.Next() {
×
1681
                var scid []byte
×
1682
                if err := rows.Scan(&scid); err != nil {
×
1683
                        return nil, err
×
1684
                }
×
1685
                items = append(items, scid)
×
1686
        }
1687
        if err := rows.Close(); err != nil {
×
1688
                return nil, err
×
1689
        }
×
1690
        if err := rows.Err(); err != nil {
×
1691
                return nil, err
×
1692
        }
×
1693
        return items, nil
×
1694
}
1695

1696
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1697
SELECT node_id, type, value
1698
FROM graph_node_extra_types
1699
WHERE node_id = $1
1700
`
1701

1702
func (q *Queries) GetExtraNodeTypes(ctx context.Context, nodeID int64) ([]GraphNodeExtraType, error) {
×
1703
        rows, err := q.db.QueryContext(ctx, getExtraNodeTypes, nodeID)
×
1704
        if err != nil {
×
1705
                return nil, err
×
1706
        }
×
1707
        defer rows.Close()
×
1708
        var items []GraphNodeExtraType
×
1709
        for rows.Next() {
×
1710
                var i GraphNodeExtraType
×
1711
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
1712
                        return nil, err
×
1713
                }
×
1714
                items = append(items, i)
×
1715
        }
1716
        if err := rows.Close(); err != nil {
×
1717
                return nil, err
×
1718
        }
×
1719
        if err := rows.Err(); err != nil {
×
1720
                return nil, err
×
1721
        }
×
1722
        return items, nil
×
1723
}
1724

1725
const getNodeAddresses = `-- name: GetNodeAddresses :many
1726
SELECT type, address
1727
FROM graph_node_addresses
1728
WHERE node_id = $1
1729
ORDER BY type ASC, position ASC
1730
`
1731

1732
type GetNodeAddressesRow struct {
1733
        Type    int16
1734
        Address string
1735
}
1736

1737
func (q *Queries) GetNodeAddresses(ctx context.Context, nodeID int64) ([]GetNodeAddressesRow, error) {
×
1738
        rows, err := q.db.QueryContext(ctx, getNodeAddresses, nodeID)
×
1739
        if err != nil {
×
1740
                return nil, err
×
1741
        }
×
1742
        defer rows.Close()
×
1743
        var items []GetNodeAddressesRow
×
1744
        for rows.Next() {
×
1745
                var i GetNodeAddressesRow
×
1746
                if err := rows.Scan(&i.Type, &i.Address); err != nil {
×
1747
                        return nil, err
×
1748
                }
×
1749
                items = append(items, i)
×
1750
        }
1751
        if err := rows.Close(); err != nil {
×
1752
                return nil, err
×
1753
        }
×
1754
        if err := rows.Err(); err != nil {
×
1755
                return nil, err
×
1756
        }
×
1757
        return items, nil
×
1758
}
1759

1760
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
1761
SELECT node_id, type, position, address
1762
FROM graph_node_addresses
1763
WHERE node_id IN (/*SLICE:ids*/?)
1764
ORDER BY node_id, type, position
1765
`
1766

1767
func (q *Queries) GetNodeAddressesBatch(ctx context.Context, ids []int64) ([]GraphNodeAddress, error) {
×
1768
        query := getNodeAddressesBatch
×
1769
        var queryParams []interface{}
×
1770
        if len(ids) > 0 {
×
1771
                for _, v := range ids {
×
1772
                        queryParams = append(queryParams, v)
×
1773
                }
×
1774
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1775
        } else {
×
1776
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1777
        }
×
1778
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1779
        if err != nil {
×
1780
                return nil, err
×
1781
        }
×
1782
        defer rows.Close()
×
1783
        var items []GraphNodeAddress
×
1784
        for rows.Next() {
×
1785
                var i GraphNodeAddress
×
1786
                if err := rows.Scan(
×
1787
                        &i.NodeID,
×
1788
                        &i.Type,
×
1789
                        &i.Position,
×
1790
                        &i.Address,
×
1791
                ); err != nil {
×
1792
                        return nil, err
×
1793
                }
×
1794
                items = append(items, i)
×
1795
        }
1796
        if err := rows.Close(); err != nil {
×
1797
                return nil, err
×
1798
        }
×
1799
        if err := rows.Err(); err != nil {
×
1800
                return nil, err
×
1801
        }
×
1802
        return items, nil
×
1803
}
1804

1805
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1806
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
1807
FROM graph_nodes
1808
WHERE pub_key = $1
1809
  AND version = $2
1810
`
1811

1812
type GetNodeByPubKeyParams struct {
1813
        PubKey  []byte
1814
        Version int16
1815
}
1816

1817
func (q *Queries) GetNodeByPubKey(ctx context.Context, arg GetNodeByPubKeyParams) (GraphNode, error) {
×
1818
        row := q.db.QueryRowContext(ctx, getNodeByPubKey, arg.PubKey, arg.Version)
×
1819
        var i GraphNode
×
1820
        err := row.Scan(
×
1821
                &i.ID,
×
1822
                &i.Version,
×
1823
                &i.PubKey,
×
1824
                &i.Alias,
×
1825
                &i.LastUpdate,
×
1826
                &i.Color,
×
1827
                &i.Signature,
×
NEW
1828
                &i.BlockHeight,
×
1829
        )
×
1830
        return i, err
×
1831
}
×
1832

1833
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
1834
SELECT node_id, type, value
1835
FROM graph_node_extra_types
1836
WHERE node_id IN (/*SLICE:ids*/?)
1837
ORDER BY node_id, type
1838
`
1839

1840
func (q *Queries) GetNodeExtraTypesBatch(ctx context.Context, ids []int64) ([]GraphNodeExtraType, error) {
×
1841
        query := getNodeExtraTypesBatch
×
1842
        var queryParams []interface{}
×
1843
        if len(ids) > 0 {
×
1844
                for _, v := range ids {
×
1845
                        queryParams = append(queryParams, v)
×
1846
                }
×
1847
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1848
        } else {
×
1849
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1850
        }
×
1851
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1852
        if err != nil {
×
1853
                return nil, err
×
1854
        }
×
1855
        defer rows.Close()
×
1856
        var items []GraphNodeExtraType
×
1857
        for rows.Next() {
×
1858
                var i GraphNodeExtraType
×
1859
                if err := rows.Scan(&i.NodeID, &i.Type, &i.Value); err != nil {
×
1860
                        return nil, err
×
1861
                }
×
1862
                items = append(items, i)
×
1863
        }
1864
        if err := rows.Close(); err != nil {
×
1865
                return nil, err
×
1866
        }
×
1867
        if err := rows.Err(); err != nil {
×
1868
                return nil, err
×
1869
        }
×
1870
        return items, nil
×
1871
}
1872

1873
const getNodeFeatures = `-- name: GetNodeFeatures :many
1874
SELECT node_id, feature_bit
1875
FROM graph_node_features
1876
WHERE node_id = $1
1877
`
1878

1879
func (q *Queries) GetNodeFeatures(ctx context.Context, nodeID int64) ([]GraphNodeFeature, error) {
×
1880
        rows, err := q.db.QueryContext(ctx, getNodeFeatures, nodeID)
×
1881
        if err != nil {
×
1882
                return nil, err
×
1883
        }
×
1884
        defer rows.Close()
×
1885
        var items []GraphNodeFeature
×
1886
        for rows.Next() {
×
1887
                var i GraphNodeFeature
×
1888
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
1889
                        return nil, err
×
1890
                }
×
1891
                items = append(items, i)
×
1892
        }
1893
        if err := rows.Close(); err != nil {
×
1894
                return nil, err
×
1895
        }
×
1896
        if err := rows.Err(); err != nil {
×
1897
                return nil, err
×
1898
        }
×
1899
        return items, nil
×
1900
}
1901

1902
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
1903
SELECT node_id, feature_bit
1904
FROM graph_node_features
1905
WHERE node_id IN (/*SLICE:ids*/?)
1906
ORDER BY node_id, feature_bit
1907
`
1908

1909
func (q *Queries) GetNodeFeaturesBatch(ctx context.Context, ids []int64) ([]GraphNodeFeature, error) {
×
1910
        query := getNodeFeaturesBatch
×
1911
        var queryParams []interface{}
×
1912
        if len(ids) > 0 {
×
1913
                for _, v := range ids {
×
1914
                        queryParams = append(queryParams, v)
×
1915
                }
×
1916
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
1917
        } else {
×
1918
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
1919
        }
×
1920
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
1921
        if err != nil {
×
1922
                return nil, err
×
1923
        }
×
1924
        defer rows.Close()
×
1925
        var items []GraphNodeFeature
×
1926
        for rows.Next() {
×
1927
                var i GraphNodeFeature
×
1928
                if err := rows.Scan(&i.NodeID, &i.FeatureBit); err != nil {
×
1929
                        return nil, err
×
1930
                }
×
1931
                items = append(items, i)
×
1932
        }
1933
        if err := rows.Close(); err != nil {
×
1934
                return nil, err
×
1935
        }
×
1936
        if err := rows.Err(); err != nil {
×
1937
                return nil, err
×
1938
        }
×
1939
        return items, nil
×
1940
}
1941

1942
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
1943
SELECT f.feature_bit
1944
FROM graph_nodes n
1945
    JOIN graph_node_features f ON f.node_id = n.id
1946
WHERE n.pub_key = $1
1947
  AND n.version = $2
1948
`
1949

1950
type GetNodeFeaturesByPubKeyParams struct {
1951
        PubKey  []byte
1952
        Version int16
1953
}
1954

1955
func (q *Queries) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) {
×
1956
        rows, err := q.db.QueryContext(ctx, getNodeFeaturesByPubKey, arg.PubKey, arg.Version)
×
1957
        if err != nil {
×
1958
                return nil, err
×
1959
        }
×
1960
        defer rows.Close()
×
1961
        var items []int32
×
1962
        for rows.Next() {
×
1963
                var feature_bit int32
×
1964
                if err := rows.Scan(&feature_bit); err != nil {
×
1965
                        return nil, err
×
1966
                }
×
1967
                items = append(items, feature_bit)
×
1968
        }
1969
        if err := rows.Close(); err != nil {
×
1970
                return nil, err
×
1971
        }
×
1972
        if err := rows.Err(); err != nil {
×
1973
                return nil, err
×
1974
        }
×
1975
        return items, nil
×
1976
}
1977

1978
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
1979
SELECT id
1980
FROM graph_nodes
1981
WHERE pub_key = $1
1982
  AND version = $2
1983
`
1984

1985
type GetNodeIDByPubKeyParams struct {
1986
        PubKey  []byte
1987
        Version int16
1988
}
1989

1990
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
1991
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
1992
        var id int64
×
1993
        err := row.Scan(&id)
×
1994
        return id, err
×
1995
}
×
1996

1997
const getNodesByIDs = `-- name: GetNodesByIDs :many
1998
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
1999
FROM graph_nodes
2000
WHERE id IN (/*SLICE:ids*/?)
2001
`
2002

2003
func (q *Queries) GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error) {
×
2004
        query := getNodesByIDs
×
2005
        var queryParams []interface{}
×
2006
        if len(ids) > 0 {
×
2007
                for _, v := range ids {
×
2008
                        queryParams = append(queryParams, v)
×
2009
                }
×
2010
                query = strings.Replace(query, "/*SLICE:ids*/?", makeQueryParams(len(queryParams), len(ids)), 1)
×
2011
        } else {
×
2012
                query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
×
2013
        }
×
2014
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2015
        if err != nil {
×
2016
                return nil, err
×
2017
        }
×
2018
        defer rows.Close()
×
2019
        var items []GraphNode
×
2020
        for rows.Next() {
×
2021
                var i GraphNode
×
2022
                if err := rows.Scan(
×
2023
                        &i.ID,
×
2024
                        &i.Version,
×
2025
                        &i.PubKey,
×
2026
                        &i.Alias,
×
2027
                        &i.LastUpdate,
×
2028
                        &i.Color,
×
2029
                        &i.Signature,
×
NEW
2030
                        &i.BlockHeight,
×
2031
                ); err != nil {
×
2032
                        return nil, err
×
2033
                }
×
2034
                items = append(items, i)
×
2035
        }
2036
        if err := rows.Close(); err != nil {
×
2037
                return nil, err
×
2038
        }
×
2039
        if err := rows.Err(); err != nil {
×
2040
                return nil, err
×
2041
        }
×
2042
        return items, nil
×
2043
}
2044

2045
const getNodesByLastUpdateRange = `-- name: GetNodesByLastUpdateRange :many
2046
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
2047
FROM graph_nodes
2048
WHERE last_update >= $1
2049
  AND last_update <= $2
2050
  -- Pagination: We use (last_update, pub_key) as a compound cursor.
2051
  -- This ensures stable ordering and allows us to resume from where we left off.
2052
  -- We use COALESCE with -1 as sentinel since timestamps are always positive.
2053
  AND (
2054
    -- Include rows with last_update greater than cursor (or all rows if cursor is -1)
2055
    last_update > COALESCE($3, -1)
2056
    OR 
2057
    -- For rows with same last_update, use pub_key as tiebreaker
2058
    (last_update = COALESCE($3, -1) 
2059
     AND pub_key > $4)
2060
  )
2061
  -- Optional filter for public nodes only
2062
  AND (
2063
    -- If only_public is false or not provided, include all nodes
2064
    COALESCE($5, FALSE) IS FALSE
2065
    OR 
2066
    -- For V1 protocol, a node is public if it has at least one public channel.
2067
    -- A public channel has bitcoin_1_signature set (channel announcement received).
2068
    EXISTS (
2069
      SELECT 1
2070
      FROM graph_channels c
2071
      WHERE c.version = 1
2072
        AND c.bitcoin_1_signature IS NOT NULL
2073
        AND (c.node_id_1 = graph_nodes.id OR c.node_id_2 = graph_nodes.id)
2074
    )
2075
  )
2076
ORDER BY last_update ASC, pub_key ASC
2077
LIMIT COALESCE($6, 999999999)
2078
`
2079

2080
type GetNodesByLastUpdateRangeParams struct {
2081
        StartTime  sql.NullInt64
2082
        EndTime    sql.NullInt64
2083
        LastUpdate sql.NullInt64
2084
        LastPubKey []byte
2085
        OnlyPublic interface{}
2086
        MaxResults interface{}
2087
}
2088

2089
func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error) {
×
2090
        rows, err := q.db.QueryContext(ctx, getNodesByLastUpdateRange,
×
2091
                arg.StartTime,
×
2092
                arg.EndTime,
×
2093
                arg.LastUpdate,
×
2094
                arg.LastPubKey,
×
2095
                arg.OnlyPublic,
×
2096
                arg.MaxResults,
×
2097
        )
×
2098
        if err != nil {
×
2099
                return nil, err
×
2100
        }
×
2101
        defer rows.Close()
×
2102
        var items []GraphNode
×
2103
        for rows.Next() {
×
2104
                var i GraphNode
×
2105
                if err := rows.Scan(
×
2106
                        &i.ID,
×
2107
                        &i.Version,
×
2108
                        &i.PubKey,
×
2109
                        &i.Alias,
×
2110
                        &i.LastUpdate,
×
2111
                        &i.Color,
×
2112
                        &i.Signature,
×
NEW
2113
                        &i.BlockHeight,
×
2114
                ); err != nil {
×
2115
                        return nil, err
×
2116
                }
×
2117
                items = append(items, i)
×
2118
        }
2119
        if err := rows.Close(); err != nil {
×
2120
                return nil, err
×
2121
        }
×
2122
        if err := rows.Err(); err != nil {
×
2123
                return nil, err
×
2124
        }
×
2125
        return items, nil
×
2126
}
2127

2128
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2129
SELECT block_height, block_hash
2130
FROM graph_prune_log
2131
WHERE block_height
2132
   IN (/*SLICE:heights*/?)
2133
`
2134

2135
func (q *Queries) GetPruneEntriesForHeights(ctx context.Context, heights []int64) ([]GraphPruneLog, error) {
×
2136
        query := getPruneEntriesForHeights
×
2137
        var queryParams []interface{}
×
2138
        if len(heights) > 0 {
×
2139
                for _, v := range heights {
×
2140
                        queryParams = append(queryParams, v)
×
2141
                }
×
2142
                query = strings.Replace(query, "/*SLICE:heights*/?", makeQueryParams(len(queryParams), len(heights)), 1)
×
2143
        } else {
×
2144
                query = strings.Replace(query, "/*SLICE:heights*/?", "NULL", 1)
×
2145
        }
×
2146
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2147
        if err != nil {
×
2148
                return nil, err
×
2149
        }
×
2150
        defer rows.Close()
×
2151
        var items []GraphPruneLog
×
2152
        for rows.Next() {
×
2153
                var i GraphPruneLog
×
2154
                if err := rows.Scan(&i.BlockHeight, &i.BlockHash); err != nil {
×
2155
                        return nil, err
×
2156
                }
×
2157
                items = append(items, i)
×
2158
        }
2159
        if err := rows.Close(); err != nil {
×
2160
                return nil, err
×
2161
        }
×
2162
        if err := rows.Err(); err != nil {
×
2163
                return nil, err
×
2164
        }
×
2165
        return items, nil
×
2166
}
2167

2168
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2169
SELECT block_hash
2170
FROM graph_prune_log
2171
WHERE block_height = $1
2172
`
2173

2174
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2175
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2176
        var block_hash []byte
×
2177
        err := row.Scan(&block_hash)
×
2178
        return block_hash, err
×
2179
}
×
2180

2181
const getPruneTip = `-- name: GetPruneTip :one
2182
SELECT block_height, block_hash
2183
FROM graph_prune_log
2184
ORDER BY block_height DESC
2185
LIMIT 1
2186
`
2187

2188
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2189
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2190
        var i GraphPruneLog
×
2191
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2192
        return i, err
×
2193
}
×
2194

2195
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2196
SELECT id, version, scid, node_id_1, node_id_2, outpoint, capacity, bitcoin_key_1, bitcoin_key_2, node_1_signature, node_2_signature, bitcoin_1_signature, bitcoin_2_signature, signature, funding_pk_script, merkle_root_hash
2197
FROM graph_channels
2198
WHERE node_1_signature IS NOT NULL
2199
  AND scid >= $1
2200
  AND scid < $2
2201
`
2202

2203
type GetPublicV1ChannelsBySCIDParams struct {
2204
        StartScid []byte
2205
        EndScid   []byte
2206
}
2207

2208
func (q *Queries) GetPublicV1ChannelsBySCID(ctx context.Context, arg GetPublicV1ChannelsBySCIDParams) ([]GraphChannel, error) {
×
2209
        rows, err := q.db.QueryContext(ctx, getPublicV1ChannelsBySCID, arg.StartScid, arg.EndScid)
×
2210
        if err != nil {
×
2211
                return nil, err
×
2212
        }
×
2213
        defer rows.Close()
×
2214
        var items []GraphChannel
×
2215
        for rows.Next() {
×
2216
                var i GraphChannel
×
2217
                if err := rows.Scan(
×
2218
                        &i.ID,
×
2219
                        &i.Version,
×
2220
                        &i.Scid,
×
2221
                        &i.NodeID1,
×
2222
                        &i.NodeID2,
×
2223
                        &i.Outpoint,
×
2224
                        &i.Capacity,
×
2225
                        &i.BitcoinKey1,
×
2226
                        &i.BitcoinKey2,
×
2227
                        &i.Node1Signature,
×
2228
                        &i.Node2Signature,
×
2229
                        &i.Bitcoin1Signature,
×
2230
                        &i.Bitcoin2Signature,
×
NEW
2231
                        &i.Signature,
×
NEW
2232
                        &i.FundingPkScript,
×
NEW
2233
                        &i.MerkleRootHash,
×
2234
                ); err != nil {
×
2235
                        return nil, err
×
2236
                }
×
2237
                items = append(items, i)
×
2238
        }
2239
        if err := rows.Close(); err != nil {
×
2240
                return nil, err
×
2241
        }
×
2242
        if err := rows.Err(); err != nil {
×
2243
                return nil, err
×
2244
        }
×
2245
        return items, nil
×
2246
}
2247

2248
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2249
SELECT scid from graph_channels
2250
WHERE outpoint = $1 AND version = $2
2251
`
2252

2253
type GetSCIDByOutpointParams struct {
2254
        Outpoint string
2255
        Version  int16
2256
}
2257

2258
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2259
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2260
        var scid []byte
×
2261
        err := row.Scan(&scid)
×
2262
        return scid, err
×
2263
}
×
2264

2265
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2266
SELECT sn.node_id, n.pub_key
2267
FROM graph_source_nodes sn
2268
    JOIN graph_nodes n ON sn.node_id = n.id
2269
WHERE n.version = $1
2270
`
2271

2272
type GetSourceNodesByVersionRow struct {
2273
        NodeID int64
2274
        PubKey []byte
2275
}
2276

2277
func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) {
×
2278
        rows, err := q.db.QueryContext(ctx, getSourceNodesByVersion, version)
×
2279
        if err != nil {
×
2280
                return nil, err
×
2281
        }
×
2282
        defer rows.Close()
×
2283
        var items []GetSourceNodesByVersionRow
×
2284
        for rows.Next() {
×
2285
                var i GetSourceNodesByVersionRow
×
2286
                if err := rows.Scan(&i.NodeID, &i.PubKey); err != nil {
×
2287
                        return nil, err
×
2288
                }
×
2289
                items = append(items, i)
×
2290
        }
2291
        if err := rows.Close(); err != nil {
×
2292
                return nil, err
×
2293
        }
×
2294
        if err := rows.Err(); err != nil {
×
2295
                return nil, err
×
2296
        }
×
2297
        return items, nil
×
2298
}
2299

2300
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2301
SELECT c.scid
2302
FROM graph_channels c
2303
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2304
WHERE cp.disabled = true
2305
AND c.version = 1
2306
GROUP BY c.scid
2307
HAVING COUNT(*) > 1
2308
`
2309

2310
// NOTE: this is V1 specific since for V1, disabled is a
2311
// simple, single boolean. The proposed V2 policy
2312
// structure will have a more complex disabled bit vector
2313
// and so the query for V2 may differ.
2314
func (q *Queries) GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error) {
×
2315
        rows, err := q.db.QueryContext(ctx, getV1DisabledSCIDs)
×
2316
        if err != nil {
×
2317
                return nil, err
×
2318
        }
×
2319
        defer rows.Close()
×
2320
        var items [][]byte
×
2321
        for rows.Next() {
×
2322
                var scid []byte
×
2323
                if err := rows.Scan(&scid); err != nil {
×
2324
                        return nil, err
×
2325
                }
×
2326
                items = append(items, scid)
×
2327
        }
2328
        if err := rows.Close(); err != nil {
×
2329
                return nil, err
×
2330
        }
×
2331
        if err := rows.Err(); err != nil {
×
2332
                return nil, err
×
2333
        }
×
2334
        return items, nil
×
2335
}
2336

2337
const getZombieChannel = `-- name: GetZombieChannel :one
2338
SELECT scid, version, node_key_1, node_key_2
2339
FROM graph_zombie_channels
2340
WHERE scid = $1
2341
AND version = $2
2342
`
2343

2344
type GetZombieChannelParams struct {
2345
        Scid    []byte
2346
        Version int16
2347
}
2348

2349
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2350
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2351
        var i GraphZombieChannel
×
2352
        err := row.Scan(
×
2353
                &i.Scid,
×
2354
                &i.Version,
×
2355
                &i.NodeKey1,
×
2356
                &i.NodeKey2,
×
2357
        )
×
2358
        return i, err
×
2359
}
×
2360

2361
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2362
SELECT scid, version, node_key_1, node_key_2
2363
FROM graph_zombie_channels
2364
WHERE version = $1
2365
  AND scid IN (/*SLICE:scids*/?)
2366
`
2367

2368
type GetZombieChannelsSCIDsParams struct {
2369
        Version int16
2370
        Scids   [][]byte
2371
}
2372

2373
func (q *Queries) GetZombieChannelsSCIDs(ctx context.Context, arg GetZombieChannelsSCIDsParams) ([]GraphZombieChannel, error) {
×
2374
        query := getZombieChannelsSCIDs
×
2375
        var queryParams []interface{}
×
2376
        queryParams = append(queryParams, arg.Version)
×
2377
        if len(arg.Scids) > 0 {
×
2378
                for _, v := range arg.Scids {
×
2379
                        queryParams = append(queryParams, v)
×
2380
                }
×
2381
                query = strings.Replace(query, "/*SLICE:scids*/?", makeQueryParams(len(queryParams), len(arg.Scids)), 1)
×
2382
        } else {
×
2383
                query = strings.Replace(query, "/*SLICE:scids*/?", "NULL", 1)
×
2384
        }
×
2385
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
2386
        if err != nil {
×
2387
                return nil, err
×
2388
        }
×
2389
        defer rows.Close()
×
2390
        var items []GraphZombieChannel
×
2391
        for rows.Next() {
×
2392
                var i GraphZombieChannel
×
2393
                if err := rows.Scan(
×
2394
                        &i.Scid,
×
2395
                        &i.Version,
×
2396
                        &i.NodeKey1,
×
2397
                        &i.NodeKey2,
×
2398
                ); err != nil {
×
2399
                        return nil, err
×
2400
                }
×
2401
                items = append(items, i)
×
2402
        }
2403
        if err := rows.Close(); err != nil {
×
2404
                return nil, err
×
2405
        }
×
2406
        if err := rows.Err(); err != nil {
×
2407
                return nil, err
×
2408
        }
×
2409
        return items, nil
×
2410
}
2411

2412
const highestSCID = `-- name: HighestSCID :one
2413
SELECT scid
2414
FROM graph_channels
2415
WHERE version = $1
2416
ORDER BY scid DESC
2417
LIMIT 1
2418
`
2419

2420
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2421
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2422
        var scid []byte
×
2423
        err := row.Scan(&scid)
×
2424
        return scid, err
×
2425
}
×
2426

2427
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2428
/* ─────────────────────────────────────────────
2429
   graph_channel_features table queries
2430
   ─────────────────────────────────────────────
2431
*/
2432

2433
INSERT INTO graph_channel_features (
2434
    channel_id, feature_bit
2435
) VALUES (
2436
    $1, $2
2437
) ON CONFLICT (channel_id, feature_bit)
2438
    -- Do nothing if the channel_id and feature_bit already exist.
2439
    DO NOTHING
2440
`
2441

2442
type InsertChannelFeatureParams struct {
2443
        ChannelID  int64
2444
        FeatureBit int32
2445
}
2446

2447
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2448
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2449
        return err
×
2450
}
×
2451

2452
const insertChannelMig = `-- name: InsertChannelMig :one
2453
INSERT INTO graph_channels (
2454
    version, scid, node_id_1, node_id_2,
2455
    outpoint, capacity, bitcoin_key_1, bitcoin_key_2,
2456
    node_1_signature, node_2_signature, bitcoin_1_signature,
2457
    bitcoin_2_signature
2458
) VALUES (
2459
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
2460
) ON CONFLICT (scid, version)
2461
    -- If a conflict occurs, we have already migrated this channel. However, we
2462
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2463
    -- otherwise, the "RETURNING id" part does not work.
2464
    DO UPDATE SET
2465
        node_id_1 = EXCLUDED.node_id_1,
2466
        node_id_2 = EXCLUDED.node_id_2,
2467
        outpoint = EXCLUDED.outpoint,
2468
        capacity = EXCLUDED.capacity,
2469
        bitcoin_key_1 = EXCLUDED.bitcoin_key_1,
2470
        bitcoin_key_2 = EXCLUDED.bitcoin_key_2,
2471
        node_1_signature = EXCLUDED.node_1_signature,
2472
        node_2_signature = EXCLUDED.node_2_signature,
2473
        bitcoin_1_signature = EXCLUDED.bitcoin_1_signature,
2474
        bitcoin_2_signature = EXCLUDED.bitcoin_2_signature
2475
RETURNING id
2476
`
2477

2478
type InsertChannelMigParams struct {
2479
        Version           int16
2480
        Scid              []byte
2481
        NodeID1           int64
2482
        NodeID2           int64
2483
        Outpoint          string
2484
        Capacity          sql.NullInt64
2485
        BitcoinKey1       []byte
2486
        BitcoinKey2       []byte
2487
        Node1Signature    []byte
2488
        Node2Signature    []byte
2489
        Bitcoin1Signature []byte
2490
        Bitcoin2Signature []byte
2491
}
2492

2493
// NOTE: This query is only meant to be used by the graph SQL migration since
2494
// for that migration, in order to be retry-safe, we don't want to error out if
2495
// we re-insert the same channel again (which would error if the normal
2496
// CreateChannel query is used because of the uniqueness constraint on the scid
2497
// and version columns).
2498
func (q *Queries) InsertChannelMig(ctx context.Context, arg InsertChannelMigParams) (int64, error) {
×
2499
        row := q.db.QueryRowContext(ctx, insertChannelMig,
×
2500
                arg.Version,
×
2501
                arg.Scid,
×
2502
                arg.NodeID1,
×
2503
                arg.NodeID2,
×
2504
                arg.Outpoint,
×
2505
                arg.Capacity,
×
2506
                arg.BitcoinKey1,
×
2507
                arg.BitcoinKey2,
×
2508
                arg.Node1Signature,
×
2509
                arg.Node2Signature,
×
2510
                arg.Bitcoin1Signature,
×
2511
                arg.Bitcoin2Signature,
×
2512
        )
×
2513
        var id int64
×
2514
        err := row.Scan(&id)
×
2515
        return id, err
×
2516
}
×
2517

2518
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2519
/* ─────────────────────────────────────────────
2520
   graph_closed_scid table queries
2521
   ────────────────────────────────────────────-
2522
*/
2523

2524
INSERT INTO graph_closed_scids (scid)
2525
VALUES ($1)
2526
ON CONFLICT (scid) DO NOTHING
2527
`
2528

2529
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
2530
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
2531
        return err
×
2532
}
×
2533

2534
const insertEdgePolicyMig = `-- name: InsertEdgePolicyMig :one
2535
INSERT INTO graph_channel_policies (
2536
    version, channel_id, node_id, timelock, fee_ppm,
2537
    base_fee_msat, min_htlc_msat, last_update, disabled,
2538
    max_htlc_msat, inbound_base_fee_msat,
2539
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
2540
    signature
2541
) VALUES  (
2542
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
2543
)
2544
ON CONFLICT (channel_id, node_id, version)
2545
    -- If a conflict occurs, we have already migrated this policy. However, we
2546
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2547
    -- otherwise, the "RETURNING id" part does not work.
2548
    DO UPDATE SET
2549
        timelock = EXCLUDED.timelock,
2550
        fee_ppm = EXCLUDED.fee_ppm,
2551
        base_fee_msat = EXCLUDED.base_fee_msat,
2552
        min_htlc_msat = EXCLUDED.min_htlc_msat,
2553
        last_update = EXCLUDED.last_update,
2554
        disabled = EXCLUDED.disabled,
2555
        max_htlc_msat = EXCLUDED.max_htlc_msat,
2556
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
2557
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
2558
        message_flags = EXCLUDED.message_flags,
2559
        channel_flags = EXCLUDED.channel_flags,
2560
        signature = EXCLUDED.signature
2561
RETURNING id
2562
`
2563

2564
type InsertEdgePolicyMigParams struct {
2565
        Version                 int16
2566
        ChannelID               int64
2567
        NodeID                  int64
2568
        Timelock                int32
2569
        FeePpm                  int64
2570
        BaseFeeMsat             int64
2571
        MinHtlcMsat             int64
2572
        LastUpdate              sql.NullInt64
2573
        Disabled                sql.NullBool
2574
        MaxHtlcMsat             sql.NullInt64
2575
        InboundBaseFeeMsat      sql.NullInt64
2576
        InboundFeeRateMilliMsat sql.NullInt64
2577
        MessageFlags            sql.NullInt16
2578
        ChannelFlags            sql.NullInt16
2579
        Signature               []byte
2580
}
2581

2582
// NOTE: This query is only meant to be used by the graph SQL migration since
2583
// for that migration, in order to be retry-safe, we don't want to error out if
2584
// we re-insert the same policy (which would error if the normal
2585
// UpsertEdgePolicy query is used because of the constraint in that query that
2586
// requires a policy update to have a newer last_update than the existing one).
2587
func (q *Queries) InsertEdgePolicyMig(ctx context.Context, arg InsertEdgePolicyMigParams) (int64, error) {
×
2588
        row := q.db.QueryRowContext(ctx, insertEdgePolicyMig,
×
2589
                arg.Version,
×
2590
                arg.ChannelID,
×
2591
                arg.NodeID,
×
2592
                arg.Timelock,
×
2593
                arg.FeePpm,
×
2594
                arg.BaseFeeMsat,
×
2595
                arg.MinHtlcMsat,
×
2596
                arg.LastUpdate,
×
2597
                arg.Disabled,
×
2598
                arg.MaxHtlcMsat,
×
2599
                arg.InboundBaseFeeMsat,
×
2600
                arg.InboundFeeRateMilliMsat,
×
2601
                arg.MessageFlags,
×
2602
                arg.ChannelFlags,
×
2603
                arg.Signature,
×
2604
        )
×
2605
        var id int64
×
2606
        err := row.Scan(&id)
×
2607
        return id, err
×
2608
}
×
2609

2610
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2611
/* ─────────────────────────────────────────────
2612
   graph_node_features table queries
2613
   ─────────────────────────────────────────────
2614
*/
2615

2616
INSERT INTO graph_node_features (
2617
    node_id, feature_bit
2618
) VALUES (
2619
    $1, $2
2620
) ON CONFLICT (node_id, feature_bit)
2621
    -- Do nothing if the feature already exists for the node.
2622
    DO NOTHING
2623
`
2624

2625
type InsertNodeFeatureParams struct {
2626
        NodeID     int64
2627
        FeatureBit int32
2628
}
2629

2630
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
2631
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
2632
        return err
×
2633
}
×
2634

2635
const insertNodeMig = `-- name: InsertNodeMig :one
2636
/* ─────────────────────────────────────────────
2637
   Migration specific queries
2638

2639
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2640
   dedicated to the migration that requires it, and so we can then remove
2641
   it from the main set of "live" queries that the code-base has access to.
2642
   ────────────────────────────────────────────-
2643
*/
2644

2645
INSERT INTO graph_nodes (
2646
    version, pub_key, alias, last_update, color, signature
2647
) VALUES (
2648
    $1, $2, $3, $4, $5, $6
2649
)
2650
ON CONFLICT (pub_key, version)
2651
    -- If a conflict occurs, we have already migrated this node. However, we
2652
    -- still need to do an "UPDATE SET" here instead of "DO NOTHING" because
2653
    -- otherwise, the "RETURNING id" part does not work.
2654
    DO UPDATE SET
2655
        alias = EXCLUDED.alias,
2656
        last_update = EXCLUDED.last_update,
2657
        color = EXCLUDED.color,
2658
        signature = EXCLUDED.signature
2659
RETURNING id
2660
`
2661

2662
type InsertNodeMigParams struct {
2663
        Version    int16
2664
        PubKey     []byte
2665
        Alias      sql.NullString
2666
        LastUpdate sql.NullInt64
2667
        Color      sql.NullString
2668
        Signature  []byte
2669
}
2670

2671
// NOTE: This query is only meant to be used by the graph SQL migration since
2672
// for that migration, in order to be retry-safe, we don't want to error out if
2673
// we re-insert the same node (which would error if the normal UpsertNode query
2674
// is used because of the constraint in that query that requires a node update
2675
// to have a newer last_update than the existing node).
2676
func (q *Queries) InsertNodeMig(ctx context.Context, arg InsertNodeMigParams) (int64, error) {
×
2677
        row := q.db.QueryRowContext(ctx, insertNodeMig,
×
2678
                arg.Version,
×
2679
                arg.PubKey,
×
2680
                arg.Alias,
×
2681
                arg.LastUpdate,
×
2682
                arg.Color,
×
2683
                arg.Signature,
×
2684
        )
×
2685
        var id int64
×
2686
        err := row.Scan(&id)
×
2687
        return id, err
×
2688
}
×
2689

2690
const isClosedChannel = `-- name: IsClosedChannel :one
2691
SELECT EXISTS (
2692
    SELECT 1
2693
    FROM graph_closed_scids
2694
    WHERE scid = $1
2695
)
2696
`
2697

2698
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
2699
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
2700
        var exists bool
×
2701
        err := row.Scan(&exists)
×
2702
        return exists, err
×
2703
}
×
2704

2705
const isPublicV1Node = `-- name: IsPublicV1Node :one
2706
SELECT EXISTS (
2707
    SELECT 1
2708
    FROM graph_channels c
2709
    JOIN graph_nodes n ON n.id = c.node_id_1
2710
    -- NOTE: we hard-code the version here since the clauses
2711
    -- here that determine if a node is public is specific
2712
    -- to the V1 gossip protocol. In V1, a node is public
2713
    -- if it has a public channel and a public channel is one
2714
    -- where we have the set of signatures of the channel
2715
    -- announcement. It is enough to just check that we have
2716
    -- one of the signatures since we only ever set them
2717
    -- together.
2718
    WHERE c.version = 1
2719
      AND c.bitcoin_1_signature IS NOT NULL
2720
      AND n.pub_key = $1
2721
    UNION ALL
2722
    SELECT 1
2723
    FROM graph_channels c
2724
    JOIN graph_nodes n ON n.id = c.node_id_2
2725
    WHERE c.version = 1
2726
      AND c.bitcoin_1_signature IS NOT NULL
2727
      AND n.pub_key = $1
2728
)
2729
`
2730

2731
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2732
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2733
        var exists bool
×
2734
        err := row.Scan(&exists)
×
2735
        return exists, err
×
2736
}
×
2737

2738
const isZombieChannel = `-- name: IsZombieChannel :one
2739
SELECT EXISTS (
2740
    SELECT 1
2741
    FROM graph_zombie_channels
2742
    WHERE scid = $1
2743
    AND version = $2
2744
) AS is_zombie
2745
`
2746

2747
type IsZombieChannelParams struct {
2748
        Scid    []byte
2749
        Version int16
2750
}
2751

2752
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2753
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2754
        var is_zombie bool
×
2755
        err := row.Scan(&is_zombie)
×
2756
        return is_zombie, err
×
2757
}
×
2758

2759
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2760
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
2761
    n1.pub_key AS node1_pubkey,
2762
    n2.pub_key AS node2_pubkey,
2763

2764
    -- Policy 1
2765
    -- TODO(elle): use sqlc.embed to embed policy structs
2766
    --  once this issue is resolved:
2767
    --  https://github.com/sqlc-dev/sqlc/issues/2997
2768
    cp1.id AS policy1_id,
2769
    cp1.node_id AS policy1_node_id,
2770
    cp1.version AS policy1_version,
2771
    cp1.timelock AS policy1_timelock,
2772
    cp1.fee_ppm AS policy1_fee_ppm,
2773
    cp1.base_fee_msat AS policy1_base_fee_msat,
2774
    cp1.min_htlc_msat AS policy1_min_htlc_msat,
2775
    cp1.max_htlc_msat AS policy1_max_htlc_msat,
2776
    cp1.last_update AS policy1_last_update,
2777
    cp1.disabled AS policy1_disabled,
2778
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2779
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2780
    cp1.message_flags AS policy1_message_flags,
2781
    cp1.channel_flags AS policy1_channel_flags,
2782
    cp1.signature AS policy1_signature,
2783

2784
       -- Policy 2
2785
    cp2.id AS policy2_id,
2786
    cp2.node_id AS policy2_node_id,
2787
    cp2.version AS policy2_version,
2788
    cp2.timelock AS policy2_timelock,
2789
    cp2.fee_ppm AS policy2_fee_ppm,
2790
    cp2.base_fee_msat AS policy2_base_fee_msat,
2791
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
2792
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
2793
    cp2.last_update AS policy2_last_update,
2794
    cp2.disabled AS policy2_disabled,
2795
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2796
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2797
    cp2.message_flags AS policy2_message_flags,
2798
    cp2.channel_flags AS policy2_channel_flags,
2799
    cp2.signature AS policy2_signature
2800

2801
FROM graph_channels c
2802
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2803
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2804
    LEFT JOIN graph_channel_policies cp1
2805
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2806
    LEFT JOIN graph_channel_policies cp2
2807
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2808
WHERE c.version = $1
2809
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2810
`
2811

2812
type ListChannelsByNodeIDParams struct {
2813
        Version int16
2814
        NodeID1 int64
2815
}
2816

2817
type ListChannelsByNodeIDRow struct {
2818
        GraphChannel                   GraphChannel
2819
        Node1Pubkey                    []byte
2820
        Node2Pubkey                    []byte
2821
        Policy1ID                      sql.NullInt64
2822
        Policy1NodeID                  sql.NullInt64
2823
        Policy1Version                 sql.NullInt16
2824
        Policy1Timelock                sql.NullInt32
2825
        Policy1FeePpm                  sql.NullInt64
2826
        Policy1BaseFeeMsat             sql.NullInt64
2827
        Policy1MinHtlcMsat             sql.NullInt64
2828
        Policy1MaxHtlcMsat             sql.NullInt64
2829
        Policy1LastUpdate              sql.NullInt64
2830
        Policy1Disabled                sql.NullBool
2831
        Policy1InboundBaseFeeMsat      sql.NullInt64
2832
        Policy1InboundFeeRateMilliMsat sql.NullInt64
2833
        Policy1MessageFlags            sql.NullInt16
2834
        Policy1ChannelFlags            sql.NullInt16
2835
        Policy1Signature               []byte
2836
        Policy2ID                      sql.NullInt64
2837
        Policy2NodeID                  sql.NullInt64
2838
        Policy2Version                 sql.NullInt16
2839
        Policy2Timelock                sql.NullInt32
2840
        Policy2FeePpm                  sql.NullInt64
2841
        Policy2BaseFeeMsat             sql.NullInt64
2842
        Policy2MinHtlcMsat             sql.NullInt64
2843
        Policy2MaxHtlcMsat             sql.NullInt64
2844
        Policy2LastUpdate              sql.NullInt64
2845
        Policy2Disabled                sql.NullBool
2846
        Policy2InboundBaseFeeMsat      sql.NullInt64
2847
        Policy2InboundFeeRateMilliMsat sql.NullInt64
2848
        Policy2MessageFlags            sql.NullInt16
2849
        Policy2ChannelFlags            sql.NullInt16
2850
        Policy2Signature               []byte
2851
}
2852

2853
func (q *Queries) ListChannelsByNodeID(ctx context.Context, arg ListChannelsByNodeIDParams) ([]ListChannelsByNodeIDRow, error) {
×
2854
        rows, err := q.db.QueryContext(ctx, listChannelsByNodeID, arg.Version, arg.NodeID1)
×
2855
        if err != nil {
×
2856
                return nil, err
×
2857
        }
×
2858
        defer rows.Close()
×
2859
        var items []ListChannelsByNodeIDRow
×
2860
        for rows.Next() {
×
2861
                var i ListChannelsByNodeIDRow
×
2862
                if err := rows.Scan(
×
2863
                        &i.GraphChannel.ID,
×
2864
                        &i.GraphChannel.Version,
×
2865
                        &i.GraphChannel.Scid,
×
2866
                        &i.GraphChannel.NodeID1,
×
2867
                        &i.GraphChannel.NodeID2,
×
2868
                        &i.GraphChannel.Outpoint,
×
2869
                        &i.GraphChannel.Capacity,
×
2870
                        &i.GraphChannel.BitcoinKey1,
×
2871
                        &i.GraphChannel.BitcoinKey2,
×
2872
                        &i.GraphChannel.Node1Signature,
×
2873
                        &i.GraphChannel.Node2Signature,
×
2874
                        &i.GraphChannel.Bitcoin1Signature,
×
2875
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
2876
                        &i.GraphChannel.Signature,
×
NEW
2877
                        &i.GraphChannel.FundingPkScript,
×
NEW
2878
                        &i.GraphChannel.MerkleRootHash,
×
2879
                        &i.Node1Pubkey,
×
2880
                        &i.Node2Pubkey,
×
2881
                        &i.Policy1ID,
×
2882
                        &i.Policy1NodeID,
×
2883
                        &i.Policy1Version,
×
2884
                        &i.Policy1Timelock,
×
2885
                        &i.Policy1FeePpm,
×
2886
                        &i.Policy1BaseFeeMsat,
×
2887
                        &i.Policy1MinHtlcMsat,
×
2888
                        &i.Policy1MaxHtlcMsat,
×
2889
                        &i.Policy1LastUpdate,
×
2890
                        &i.Policy1Disabled,
×
2891
                        &i.Policy1InboundBaseFeeMsat,
×
2892
                        &i.Policy1InboundFeeRateMilliMsat,
×
2893
                        &i.Policy1MessageFlags,
×
2894
                        &i.Policy1ChannelFlags,
×
2895
                        &i.Policy1Signature,
×
2896
                        &i.Policy2ID,
×
2897
                        &i.Policy2NodeID,
×
2898
                        &i.Policy2Version,
×
2899
                        &i.Policy2Timelock,
×
2900
                        &i.Policy2FeePpm,
×
2901
                        &i.Policy2BaseFeeMsat,
×
2902
                        &i.Policy2MinHtlcMsat,
×
2903
                        &i.Policy2MaxHtlcMsat,
×
2904
                        &i.Policy2LastUpdate,
×
2905
                        &i.Policy2Disabled,
×
2906
                        &i.Policy2InboundBaseFeeMsat,
×
2907
                        &i.Policy2InboundFeeRateMilliMsat,
×
2908
                        &i.Policy2MessageFlags,
×
2909
                        &i.Policy2ChannelFlags,
×
2910
                        &i.Policy2Signature,
×
2911
                ); err != nil {
×
2912
                        return nil, err
×
2913
                }
×
2914
                items = append(items, i)
×
2915
        }
2916
        if err := rows.Close(); err != nil {
×
2917
                return nil, err
×
2918
        }
×
2919
        if err := rows.Err(); err != nil {
×
2920
                return nil, err
×
2921
        }
×
2922
        return items, nil
×
2923
}
2924

2925
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
2926
SELECT c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
2927
       n1.pub_key AS node1_pubkey,
2928
       n2.pub_key AS node2_pubkey,
2929

2930
       -- Policy 1
2931
       -- TODO(elle): use sqlc.embed to embed policy structs
2932
       --  once this issue is resolved:
2933
       --  https://github.com/sqlc-dev/sqlc/issues/2997
2934
       cp1.id AS policy1_id,
2935
       cp1.node_id AS policy1_node_id,
2936
       cp1.version AS policy1_version,
2937
       cp1.timelock AS policy1_timelock,
2938
       cp1.fee_ppm AS policy1_fee_ppm,
2939
       cp1.base_fee_msat AS policy1_base_fee_msat,
2940
       cp1.min_htlc_msat AS policy1_min_htlc_msat,
2941
       cp1.max_htlc_msat AS policy1_max_htlc_msat,
2942
       cp1.last_update AS policy1_last_update,
2943
       cp1.disabled AS policy1_disabled,
2944
       cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
2945
       cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
2946
       cp1.message_flags AS policy1_message_flags,
2947
       cp1.channel_flags AS policy1_channel_flags,
2948
       cp1.signature AS policy1_signature,
2949

2950
       -- Policy 2
2951
       cp2.id AS policy2_id,
2952
       cp2.node_id AS policy2_node_id,
2953
       cp2.version AS policy2_version,
2954
       cp2.timelock AS policy2_timelock,
2955
       cp2.fee_ppm AS policy2_fee_ppm,
2956
       cp2.base_fee_msat AS policy2_base_fee_msat,
2957
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
2958
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
2959
       cp2.last_update AS policy2_last_update,
2960
       cp2.disabled AS policy2_disabled,
2961
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2962
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2963
       cp2.message_flags AS policy2_message_flags,
2964
       cp2.channel_flags AS policy2_channel_flags,
2965
       cp2.signature AS policy2_signature
2966

2967
FROM graph_channels c
2968
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2969
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2970
         LEFT JOIN graph_channel_policies cp1
2971
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2972
         LEFT JOIN graph_channel_policies cp2
2973
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2974
WHERE c.version = $1
2975
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
2976
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
2977
`
2978

2979
type ListChannelsForNodeIDsParams struct {
2980
        Version  int16
2981
        Node1Ids []int64
2982
        Node2Ids []int64
2983
}
2984

2985
type ListChannelsForNodeIDsRow struct {
2986
        GraphChannel                   GraphChannel
2987
        Node1Pubkey                    []byte
2988
        Node2Pubkey                    []byte
2989
        Policy1ID                      sql.NullInt64
2990
        Policy1NodeID                  sql.NullInt64
2991
        Policy1Version                 sql.NullInt16
2992
        Policy1Timelock                sql.NullInt32
2993
        Policy1FeePpm                  sql.NullInt64
2994
        Policy1BaseFeeMsat             sql.NullInt64
2995
        Policy1MinHtlcMsat             sql.NullInt64
2996
        Policy1MaxHtlcMsat             sql.NullInt64
2997
        Policy1LastUpdate              sql.NullInt64
2998
        Policy1Disabled                sql.NullBool
2999
        Policy1InboundBaseFeeMsat      sql.NullInt64
3000
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3001
        Policy1MessageFlags            sql.NullInt16
3002
        Policy1ChannelFlags            sql.NullInt16
3003
        Policy1Signature               []byte
3004
        Policy2ID                      sql.NullInt64
3005
        Policy2NodeID                  sql.NullInt64
3006
        Policy2Version                 sql.NullInt16
3007
        Policy2Timelock                sql.NullInt32
3008
        Policy2FeePpm                  sql.NullInt64
3009
        Policy2BaseFeeMsat             sql.NullInt64
3010
        Policy2MinHtlcMsat             sql.NullInt64
3011
        Policy2MaxHtlcMsat             sql.NullInt64
3012
        Policy2LastUpdate              sql.NullInt64
3013
        Policy2Disabled                sql.NullBool
3014
        Policy2InboundBaseFeeMsat      sql.NullInt64
3015
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3016
        Policy2MessageFlags            sql.NullInt16
3017
        Policy2ChannelFlags            sql.NullInt16
3018
        Policy2Signature               []byte
3019
}
3020

3021
func (q *Queries) ListChannelsForNodeIDs(ctx context.Context, arg ListChannelsForNodeIDsParams) ([]ListChannelsForNodeIDsRow, error) {
×
3022
        query := listChannelsForNodeIDs
×
3023
        var queryParams []interface{}
×
3024
        queryParams = append(queryParams, arg.Version)
×
3025
        if len(arg.Node1Ids) > 0 {
×
3026
                for _, v := range arg.Node1Ids {
×
3027
                        queryParams = append(queryParams, v)
×
3028
                }
×
3029
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", makeQueryParams(len(queryParams), len(arg.Node1Ids)), 1)
×
3030
        } else {
×
3031
                query = strings.Replace(query, "/*SLICE:node1_ids*/?", "NULL", 1)
×
3032
        }
×
3033
        if len(arg.Node2Ids) > 0 {
×
3034
                for _, v := range arg.Node2Ids {
×
3035
                        queryParams = append(queryParams, v)
×
3036
                }
×
3037
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", makeQueryParams(len(queryParams), len(arg.Node2Ids)), 1)
×
3038
        } else {
×
3039
                query = strings.Replace(query, "/*SLICE:node2_ids*/?", "NULL", 1)
×
3040
        }
×
3041
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
3042
        if err != nil {
×
3043
                return nil, err
×
3044
        }
×
3045
        defer rows.Close()
×
3046
        var items []ListChannelsForNodeIDsRow
×
3047
        for rows.Next() {
×
3048
                var i ListChannelsForNodeIDsRow
×
3049
                if err := rows.Scan(
×
3050
                        &i.GraphChannel.ID,
×
3051
                        &i.GraphChannel.Version,
×
3052
                        &i.GraphChannel.Scid,
×
3053
                        &i.GraphChannel.NodeID1,
×
3054
                        &i.GraphChannel.NodeID2,
×
3055
                        &i.GraphChannel.Outpoint,
×
3056
                        &i.GraphChannel.Capacity,
×
3057
                        &i.GraphChannel.BitcoinKey1,
×
3058
                        &i.GraphChannel.BitcoinKey2,
×
3059
                        &i.GraphChannel.Node1Signature,
×
3060
                        &i.GraphChannel.Node2Signature,
×
3061
                        &i.GraphChannel.Bitcoin1Signature,
×
3062
                        &i.GraphChannel.Bitcoin2Signature,
×
NEW
3063
                        &i.GraphChannel.Signature,
×
NEW
3064
                        &i.GraphChannel.FundingPkScript,
×
NEW
3065
                        &i.GraphChannel.MerkleRootHash,
×
3066
                        &i.Node1Pubkey,
×
3067
                        &i.Node2Pubkey,
×
3068
                        &i.Policy1ID,
×
3069
                        &i.Policy1NodeID,
×
3070
                        &i.Policy1Version,
×
3071
                        &i.Policy1Timelock,
×
3072
                        &i.Policy1FeePpm,
×
3073
                        &i.Policy1BaseFeeMsat,
×
3074
                        &i.Policy1MinHtlcMsat,
×
3075
                        &i.Policy1MaxHtlcMsat,
×
3076
                        &i.Policy1LastUpdate,
×
3077
                        &i.Policy1Disabled,
×
3078
                        &i.Policy1InboundBaseFeeMsat,
×
3079
                        &i.Policy1InboundFeeRateMilliMsat,
×
3080
                        &i.Policy1MessageFlags,
×
3081
                        &i.Policy1ChannelFlags,
×
3082
                        &i.Policy1Signature,
×
3083
                        &i.Policy2ID,
×
3084
                        &i.Policy2NodeID,
×
3085
                        &i.Policy2Version,
×
3086
                        &i.Policy2Timelock,
×
3087
                        &i.Policy2FeePpm,
×
3088
                        &i.Policy2BaseFeeMsat,
×
3089
                        &i.Policy2MinHtlcMsat,
×
3090
                        &i.Policy2MaxHtlcMsat,
×
3091
                        &i.Policy2LastUpdate,
×
3092
                        &i.Policy2Disabled,
×
3093
                        &i.Policy2InboundBaseFeeMsat,
×
3094
                        &i.Policy2InboundFeeRateMilliMsat,
×
3095
                        &i.Policy2MessageFlags,
×
3096
                        &i.Policy2ChannelFlags,
×
3097
                        &i.Policy2Signature,
×
3098
                ); err != nil {
×
3099
                        return nil, err
×
3100
                }
×
3101
                items = append(items, i)
×
3102
        }
3103
        if err := rows.Close(); err != nil {
×
3104
                return nil, err
×
3105
        }
×
3106
        if err := rows.Err(); err != nil {
×
3107
                return nil, err
×
3108
        }
×
3109
        return items, nil
×
3110
}
3111

3112
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3113
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3114
FROM graph_channels c
3115
WHERE c.version = $1 AND c.id > $2
3116
ORDER BY c.id
3117
LIMIT $3
3118
`
3119

3120
type ListChannelsPaginatedParams struct {
3121
        Version int16
3122
        ID      int64
3123
        Limit   int32
3124
}
3125

3126
type ListChannelsPaginatedRow struct {
3127
        ID          int64
3128
        BitcoinKey1 []byte
3129
        BitcoinKey2 []byte
3130
        Outpoint    string
3131
}
3132

3133
func (q *Queries) ListChannelsPaginated(ctx context.Context, arg ListChannelsPaginatedParams) ([]ListChannelsPaginatedRow, error) {
×
3134
        rows, err := q.db.QueryContext(ctx, listChannelsPaginated, arg.Version, arg.ID, arg.Limit)
×
3135
        if err != nil {
×
3136
                return nil, err
×
3137
        }
×
3138
        defer rows.Close()
×
3139
        var items []ListChannelsPaginatedRow
×
3140
        for rows.Next() {
×
3141
                var i ListChannelsPaginatedRow
×
3142
                if err := rows.Scan(
×
3143
                        &i.ID,
×
3144
                        &i.BitcoinKey1,
×
3145
                        &i.BitcoinKey2,
×
3146
                        &i.Outpoint,
×
3147
                ); err != nil {
×
3148
                        return nil, err
×
3149
                }
×
3150
                items = append(items, i)
×
3151
        }
3152
        if err := rows.Close(); err != nil {
×
3153
                return nil, err
×
3154
        }
×
3155
        if err := rows.Err(); err != nil {
×
3156
                return nil, err
×
3157
        }
×
3158
        return items, nil
×
3159
}
3160

3161
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3162
SELECT
3163
    c.id as id,
3164
    c.scid as scid,
3165
    c.capacity AS capacity,
3166

3167
    -- Join node pubkeys
3168
    n1.pub_key AS node1_pubkey,
3169
    n2.pub_key AS node2_pubkey,
3170

3171
    -- Node 1 policy
3172
    cp1.timelock AS policy_1_timelock,
3173
    cp1.fee_ppm AS policy_1_fee_ppm,
3174
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3175
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3176
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3177
    cp1.disabled AS policy_1_disabled,
3178
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3179
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3180
    cp1.message_flags AS policy1_message_flags,
3181
    cp1.channel_flags AS policy1_channel_flags,
3182

3183
    -- Node 2 policy
3184
    cp2.timelock AS policy_2_timelock,
3185
    cp2.fee_ppm AS policy_2_fee_ppm,
3186
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3187
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3188
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3189
    cp2.disabled AS policy_2_disabled,
3190
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3191
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3192
    cp2.message_flags AS policy2_message_flags,
3193
    cp2.channel_flags AS policy2_channel_flags
3194

3195
FROM graph_channels c
3196
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3197
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3198
LEFT JOIN graph_channel_policies cp1
3199
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3200
LEFT JOIN graph_channel_policies cp2
3201
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3202
WHERE c.version = $1 AND c.id > $2
3203
ORDER BY c.id
3204
LIMIT $3
3205
`
3206

3207
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3208
        Version int16
3209
        ID      int64
3210
        Limit   int32
3211
}
3212

3213
type ListChannelsWithPoliciesForCachePaginatedRow struct {
3214
        ID                             int64
3215
        Scid                           []byte
3216
        Capacity                       sql.NullInt64
3217
        Node1Pubkey                    []byte
3218
        Node2Pubkey                    []byte
3219
        Policy1Timelock                sql.NullInt32
3220
        Policy1FeePpm                  sql.NullInt64
3221
        Policy1BaseFeeMsat             sql.NullInt64
3222
        Policy1MinHtlcMsat             sql.NullInt64
3223
        Policy1MaxHtlcMsat             sql.NullInt64
3224
        Policy1Disabled                sql.NullBool
3225
        Policy1InboundBaseFeeMsat      sql.NullInt64
3226
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3227
        Policy1MessageFlags            sql.NullInt16
3228
        Policy1ChannelFlags            sql.NullInt16
3229
        Policy2Timelock                sql.NullInt32
3230
        Policy2FeePpm                  sql.NullInt64
3231
        Policy2BaseFeeMsat             sql.NullInt64
3232
        Policy2MinHtlcMsat             sql.NullInt64
3233
        Policy2MaxHtlcMsat             sql.NullInt64
3234
        Policy2Disabled                sql.NullBool
3235
        Policy2InboundBaseFeeMsat      sql.NullInt64
3236
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3237
        Policy2MessageFlags            sql.NullInt16
3238
        Policy2ChannelFlags            sql.NullInt16
3239
}
3240

3241
func (q *Queries) ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error) {
×
3242
        rows, err := q.db.QueryContext(ctx, listChannelsWithPoliciesForCachePaginated, arg.Version, arg.ID, arg.Limit)
×
3243
        if err != nil {
×
3244
                return nil, err
×
3245
        }
×
3246
        defer rows.Close()
×
3247
        var items []ListChannelsWithPoliciesForCachePaginatedRow
×
3248
        for rows.Next() {
×
3249
                var i ListChannelsWithPoliciesForCachePaginatedRow
×
3250
                if err := rows.Scan(
×
3251
                        &i.ID,
×
3252
                        &i.Scid,
×
3253
                        &i.Capacity,
×
3254
                        &i.Node1Pubkey,
×
3255
                        &i.Node2Pubkey,
×
3256
                        &i.Policy1Timelock,
×
3257
                        &i.Policy1FeePpm,
×
3258
                        &i.Policy1BaseFeeMsat,
×
3259
                        &i.Policy1MinHtlcMsat,
×
3260
                        &i.Policy1MaxHtlcMsat,
×
3261
                        &i.Policy1Disabled,
×
3262
                        &i.Policy1InboundBaseFeeMsat,
×
3263
                        &i.Policy1InboundFeeRateMilliMsat,
×
3264
                        &i.Policy1MessageFlags,
×
3265
                        &i.Policy1ChannelFlags,
×
3266
                        &i.Policy2Timelock,
×
3267
                        &i.Policy2FeePpm,
×
3268
                        &i.Policy2BaseFeeMsat,
×
3269
                        &i.Policy2MinHtlcMsat,
×
3270
                        &i.Policy2MaxHtlcMsat,
×
3271
                        &i.Policy2Disabled,
×
3272
                        &i.Policy2InboundBaseFeeMsat,
×
3273
                        &i.Policy2InboundFeeRateMilliMsat,
×
3274
                        &i.Policy2MessageFlags,
×
3275
                        &i.Policy2ChannelFlags,
×
3276
                ); err != nil {
×
3277
                        return nil, err
×
3278
                }
×
3279
                items = append(items, i)
×
3280
        }
3281
        if err := rows.Close(); err != nil {
×
3282
                return nil, err
×
3283
        }
×
3284
        if err := rows.Err(); err != nil {
×
3285
                return nil, err
×
3286
        }
×
3287
        return items, nil
×
3288
}
3289

3290
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3291
SELECT
3292
    c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
3293

3294
    -- Join node pubkeys
3295
    n1.pub_key AS node1_pubkey,
3296
    n2.pub_key AS node2_pubkey,
3297

3298
    -- Node 1 policy
3299
    cp1.id AS policy_1_id,
3300
    cp1.node_id AS policy_1_node_id,
3301
    cp1.version AS policy_1_version,
3302
    cp1.timelock AS policy_1_timelock,
3303
    cp1.fee_ppm AS policy_1_fee_ppm,
3304
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3305
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3306
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3307
    cp1.last_update AS policy_1_last_update,
3308
    cp1.disabled AS policy_1_disabled,
3309
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3310
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3311
    cp1.message_flags AS policy1_message_flags,
3312
    cp1.channel_flags AS policy1_channel_flags,
3313
    cp1.signature AS policy_1_signature,
3314

3315
    -- Node 2 policy
3316
    cp2.id AS policy_2_id,
3317
    cp2.node_id AS policy_2_node_id,
3318
    cp2.version AS policy_2_version,
3319
    cp2.timelock AS policy_2_timelock,
3320
    cp2.fee_ppm AS policy_2_fee_ppm,
3321
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3322
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3323
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3324
    cp2.last_update AS policy_2_last_update,
3325
    cp2.disabled AS policy_2_disabled,
3326
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3327
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3328
    cp2.message_flags AS policy2_message_flags,
3329
    cp2.channel_flags AS policy2_channel_flags,
3330
    cp2.signature AS policy_2_signature
3331

3332
FROM graph_channels c
3333
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3334
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3335
LEFT JOIN graph_channel_policies cp1
3336
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3337
LEFT JOIN graph_channel_policies cp2
3338
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3339
WHERE c.version = $1 AND c.id > $2
3340
ORDER BY c.id
3341
LIMIT $3
3342
`
3343

3344
type ListChannelsWithPoliciesPaginatedParams struct {
3345
        Version int16
3346
        ID      int64
3347
        Limit   int32
3348
}
3349

3350
type ListChannelsWithPoliciesPaginatedRow struct {
3351
        GraphChannel                   GraphChannel
3352
        Node1Pubkey                    []byte
3353
        Node2Pubkey                    []byte
3354
        Policy1ID                      sql.NullInt64
3355
        Policy1NodeID                  sql.NullInt64
3356
        Policy1Version                 sql.NullInt16
3357
        Policy1Timelock                sql.NullInt32
3358
        Policy1FeePpm                  sql.NullInt64
3359
        Policy1BaseFeeMsat             sql.NullInt64
3360
        Policy1MinHtlcMsat             sql.NullInt64
3361
        Policy1MaxHtlcMsat             sql.NullInt64
3362
        Policy1LastUpdate              sql.NullInt64
3363
        Policy1Disabled                sql.NullBool
3364
        Policy1InboundBaseFeeMsat      sql.NullInt64
3365
        Policy1InboundFeeRateMilliMsat sql.NullInt64
3366
        Policy1MessageFlags            sql.NullInt16
3367
        Policy1ChannelFlags            sql.NullInt16
3368
        Policy1Signature               []byte
3369
        Policy2ID                      sql.NullInt64
3370
        Policy2NodeID                  sql.NullInt64
3371
        Policy2Version                 sql.NullInt16
3372
        Policy2Timelock                sql.NullInt32
3373
        Policy2FeePpm                  sql.NullInt64
3374
        Policy2BaseFeeMsat             sql.NullInt64
3375
        Policy2MinHtlcMsat             sql.NullInt64
3376
        Policy2MaxHtlcMsat             sql.NullInt64
3377
        Policy2LastUpdate              sql.NullInt64
3378
        Policy2Disabled                sql.NullBool
3379
        Policy2InboundBaseFeeMsat      sql.NullInt64
3380
        Policy2InboundFeeRateMilliMsat sql.NullInt64
3381
        Policy2MessageFlags            sql.NullInt16
3382
        Policy2ChannelFlags            sql.NullInt16
3383
        Policy2Signature               []byte
3384
}
3385

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

3458
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3459
SELECT id, pub_key
3460
FROM graph_nodes
3461
WHERE version = $1  AND id > $2
3462
ORDER BY id
3463
LIMIT $3
3464
`
3465

3466
type ListNodeIDsAndPubKeysParams struct {
3467
        Version int16
3468
        ID      int64
3469
        Limit   int32
3470
}
3471

3472
type ListNodeIDsAndPubKeysRow struct {
3473
        ID     int64
3474
        PubKey []byte
3475
}
3476

3477
func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error) {
×
3478
        rows, err := q.db.QueryContext(ctx, listNodeIDsAndPubKeys, arg.Version, arg.ID, arg.Limit)
×
3479
        if err != nil {
×
3480
                return nil, err
×
3481
        }
×
3482
        defer rows.Close()
×
3483
        var items []ListNodeIDsAndPubKeysRow
×
3484
        for rows.Next() {
×
3485
                var i ListNodeIDsAndPubKeysRow
×
3486
                if err := rows.Scan(&i.ID, &i.PubKey); err != nil {
×
3487
                        return nil, err
×
3488
                }
×
3489
                items = append(items, i)
×
3490
        }
3491
        if err := rows.Close(); err != nil {
×
3492
                return nil, err
×
3493
        }
×
3494
        if err := rows.Err(); err != nil {
×
3495
                return nil, err
×
3496
        }
×
3497
        return items, nil
×
3498
}
3499

3500
const listNodesPaginated = `-- name: ListNodesPaginated :many
3501
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
3502
FROM graph_nodes
3503
WHERE version = $1 AND id > $2
3504
ORDER BY id
3505
LIMIT $3
3506
`
3507

3508
type ListNodesPaginatedParams struct {
3509
        Version int16
3510
        ID      int64
3511
        Limit   int32
3512
}
3513

3514
func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
×
3515
        rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
×
3516
        if err != nil {
×
3517
                return nil, err
×
3518
        }
×
3519
        defer rows.Close()
×
3520
        var items []GraphNode
×
3521
        for rows.Next() {
×
3522
                var i GraphNode
×
3523
                if err := rows.Scan(
×
3524
                        &i.ID,
×
3525
                        &i.Version,
×
3526
                        &i.PubKey,
×
3527
                        &i.Alias,
×
3528
                        &i.LastUpdate,
×
3529
                        &i.Color,
×
3530
                        &i.Signature,
×
NEW
3531
                        &i.BlockHeight,
×
3532
                ); err != nil {
×
3533
                        return nil, err
×
3534
                }
×
3535
                items = append(items, i)
×
3536
        }
3537
        if err := rows.Close(); err != nil {
×
3538
                return nil, err
×
3539
        }
×
3540
        if err := rows.Err(); err != nil {
×
3541
                return nil, err
×
3542
        }
×
3543
        return items, nil
×
3544
}
3545

3546
const nodeExists = `-- name: NodeExists :one
3547
SELECT EXISTS (
3548
    SELECT 1
3549
    FROM graph_nodes
3550
    WHERE pub_key = $1
3551
      AND version = $2
3552
) AS node_exists
3553
`
3554

3555
type NodeExistsParams struct {
3556
        PubKey  []byte
3557
        Version int16
3558
}
3559

NEW
3560
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
NEW
3561
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
NEW
3562
        var node_exists bool
×
NEW
3563
        err := row.Scan(&node_exists)
×
NEW
3564
        return node_exists, err
×
NEW
3565
}
×
3566

3567
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3568
/* ─────────────────────────────────────────────
3569
   graph_channel_policy_extra_types table queries
3570
   ─────────────────────────────────────────────
3571
*/
3572

3573
INSERT INTO graph_channel_policy_extra_types (
3574
    channel_policy_id, type, value
3575
)
3576
VALUES ($1, $2, $3)
3577
ON CONFLICT (channel_policy_id, type)
3578
    -- If a conflict occurs on channel_policy_id and type, then we update the
3579
    -- value.
3580
    DO UPDATE SET value = EXCLUDED.value
3581
`
3582

3583
type UpsertChanPolicyExtraTypeParams struct {
3584
        ChannelPolicyID int64
3585
        Type            int64
3586
        Value           []byte
3587
}
3588

3589
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
3590
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
3591
        return err
×
3592
}
×
3593

3594
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3595
/* ─────────────────────────────────────────────
3596
   graph_channel_extra_types table queries
3597
   ─────────────────────────────────────────────
3598
*/
3599

3600
INSERT INTO graph_channel_extra_types (
3601
    channel_id, type, value
3602
)
3603
VALUES ($1, $2, $3)
3604
    ON CONFLICT (channel_id, type)
3605
    -- Update the value if a conflict occurs on channel_id and type.
3606
    DO UPDATE SET value = EXCLUDED.value
3607
`
3608

3609
type UpsertChannelExtraTypeParams struct {
3610
        ChannelID int64
3611
        Type      int64
3612
        Value     []byte
3613
}
3614

3615
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
3616
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
3617
        return err
×
3618
}
×
3619

3620
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3621
/* ─────────────────────────────────────────────
3622
   graph_channel_policies table queries
3623
   ─────────────────────────────────────────────
3624
*/
3625

3626
INSERT INTO graph_channel_policies (
3627
    version, channel_id, node_id, timelock, fee_ppm,
3628
    base_fee_msat, min_htlc_msat, last_update, disabled,
3629
    max_htlc_msat, inbound_base_fee_msat,
3630
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3631
    signature
3632
) VALUES  (
3633
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3634
)
3635
ON CONFLICT (channel_id, node_id, version)
3636
    -- Update the following fields if a conflict occurs on channel_id,
3637
    -- node_id, and version.
3638
    DO UPDATE SET
3639
        timelock = EXCLUDED.timelock,
3640
        fee_ppm = EXCLUDED.fee_ppm,
3641
        base_fee_msat = EXCLUDED.base_fee_msat,
3642
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3643
        last_update = EXCLUDED.last_update,
3644
        disabled = EXCLUDED.disabled,
3645
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3646
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3647
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3648
        message_flags = EXCLUDED.message_flags,
3649
        channel_flags = EXCLUDED.channel_flags,
3650
        signature = EXCLUDED.signature
3651
WHERE EXCLUDED.last_update > graph_channel_policies.last_update
3652
RETURNING id
3653
`
3654

3655
type UpsertEdgePolicyParams struct {
3656
        Version                 int16
3657
        ChannelID               int64
3658
        NodeID                  int64
3659
        Timelock                int32
3660
        FeePpm                  int64
3661
        BaseFeeMsat             int64
3662
        MinHtlcMsat             int64
3663
        LastUpdate              sql.NullInt64
3664
        Disabled                sql.NullBool
3665
        MaxHtlcMsat             sql.NullInt64
3666
        InboundBaseFeeMsat      sql.NullInt64
3667
        InboundFeeRateMilliMsat sql.NullInt64
3668
        MessageFlags            sql.NullInt16
3669
        ChannelFlags            sql.NullInt16
3670
        Signature               []byte
3671
}
3672

3673
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
3674
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
3675
                arg.Version,
×
3676
                arg.ChannelID,
×
3677
                arg.NodeID,
×
3678
                arg.Timelock,
×
3679
                arg.FeePpm,
×
3680
                arg.BaseFeeMsat,
×
3681
                arg.MinHtlcMsat,
×
3682
                arg.LastUpdate,
×
3683
                arg.Disabled,
×
3684
                arg.MaxHtlcMsat,
×
3685
                arg.InboundBaseFeeMsat,
×
3686
                arg.InboundFeeRateMilliMsat,
×
3687
                arg.MessageFlags,
×
3688
                arg.ChannelFlags,
×
3689
                arg.Signature,
×
3690
        )
×
3691
        var id int64
×
3692
        err := row.Scan(&id)
×
3693
        return id, err
×
3694
}
×
3695

3696
const upsertNode = `-- name: UpsertNode :one
3697
/* ─────────────────────────────────────────────
3698
   graph_nodes table queries
3699
   ───────────────────────────��─────────────────
3700
*/
3701

3702
INSERT INTO graph_nodes (
3703
    version, pub_key, alias, last_update, block_height, color, signature
3704
) VALUES (
3705
    $1, $2, $3, $4, $5, $6, $7
3706
)
3707
ON CONFLICT (pub_key, version)
3708
    -- Update the following fields if a conflict occurs on pub_key
3709
    -- and version.
3710
    DO UPDATE SET
3711
        alias = EXCLUDED.alias,
3712
        last_update = EXCLUDED.last_update,
3713
        block_height = EXCLUDED.block_height,
3714
        color = EXCLUDED.color,
3715
        signature = EXCLUDED.signature
3716
WHERE (graph_nodes.last_update IS NULL
3717
    OR EXCLUDED.last_update > graph_nodes.last_update)
3718
AND (graph_nodes.block_height IS NULL
3719
    OR EXCLUDED.block_height >= graph_nodes.block_height)
3720
RETURNING id
3721
`
3722

3723
type UpsertNodeParams struct {
3724
        Version     int16
3725
        PubKey      []byte
3726
        Alias       sql.NullString
3727
        LastUpdate  sql.NullInt64
3728
        BlockHeight sql.NullInt64
3729
        Color       sql.NullString
3730
        Signature   []byte
3731
}
3732

3733
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3734
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3735
                arg.Version,
×
3736
                arg.PubKey,
×
3737
                arg.Alias,
×
3738
                arg.LastUpdate,
×
NEW
3739
                arg.BlockHeight,
×
3740
                arg.Color,
×
3741
                arg.Signature,
×
3742
        )
×
3743
        var id int64
×
3744
        err := row.Scan(&id)
×
3745
        return id, err
×
3746
}
×
3747

3748
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3749
/* ─────────────────────────────────────────────
3750
   graph_node_addresses table queries
3751
   ───────────────────────────────────��─────────
3752
*/
3753

3754
INSERT INTO graph_node_addresses (
3755
    node_id,
3756
    type,
3757
    address,
3758
    position
3759
) VALUES (
3760
    $1, $2, $3, $4
3761
) ON CONFLICT (node_id, type, position)
3762
    DO UPDATE SET address = EXCLUDED.address
3763
`
3764

3765
type UpsertNodeAddressParams struct {
3766
        NodeID   int64
3767
        Type     int16
3768
        Address  string
3769
        Position int32
3770
}
3771

3772
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3773
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3774
                arg.NodeID,
×
3775
                arg.Type,
×
3776
                arg.Address,
×
3777
                arg.Position,
×
3778
        )
×
3779
        return err
×
3780
}
×
3781

3782
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3783
/* ─────────────────────────────────────────────
3784
   graph_node_extra_types table queries
3785
   ─────────────────────────────────────────────
3786
*/
3787

3788
INSERT INTO graph_node_extra_types (
3789
    node_id, type, value
3790
)
3791
VALUES ($1, $2, $3)
3792
ON CONFLICT (type, node_id)
3793
    -- Update the value if a conflict occurs on type
3794
    -- and node_id.
3795
    DO UPDATE SET value = EXCLUDED.value
3796
`
3797

3798
type UpsertNodeExtraTypeParams struct {
3799
        NodeID int64
3800
        Type   int64
3801
        Value  []byte
3802
}
3803

3804
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
3805
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
3806
        return err
×
3807
}
×
3808

3809
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3810
/* ───────────────────────────���─────────────────
3811
    graph_prune_log table queries
3812
    ─────────────────────────────────────────────
3813
*/
3814

3815
INSERT INTO graph_prune_log (
3816
    block_height, block_hash
3817
) VALUES (
3818
    $1, $2
3819
)
3820
ON CONFLICT(block_height) DO UPDATE SET
3821
    block_hash = EXCLUDED.block_hash
3822
`
3823

3824
type UpsertPruneLogEntryParams struct {
3825
        BlockHeight int64
3826
        BlockHash   []byte
3827
}
3828

3829
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
3830
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
3831
        return err
×
3832
}
×
3833

3834
const upsertSourceNode = `-- name: UpsertSourceNode :one
3835
INSERT INTO graph_nodes (
3836
    version, pub_key, alias, last_update, block_height, color, signature
3837
) VALUES (
3838
    $1, $2, $3, $4, $5, $6, $7
3839
)
3840
ON CONFLICT (pub_key, version)
3841
    -- Update the following fields if a conflict occurs on pub_key
3842
    -- and version.
3843
    DO UPDATE SET
3844
        alias = EXCLUDED.alias,
3845
        last_update = EXCLUDED.last_update,
3846
        block_height = EXCLUDED.block_height,
3847
        color = EXCLUDED.color,
3848
        signature = EXCLUDED.signature
3849
WHERE graph_nodes.last_update IS NULL
3850
    OR EXCLUDED.last_update >= graph_nodes.last_update
3851
AND (graph_nodes.block_height IS NULL
3852
   OR EXCLUDED.block_height >= graph_nodes.block_height)
3853
RETURNING id
3854
`
3855

3856
type UpsertSourceNodeParams struct {
3857
        Version     int16
3858
        PubKey      []byte
3859
        Alias       sql.NullString
3860
        LastUpdate  sql.NullInt64
3861
        BlockHeight sql.NullInt64
3862
        Color       sql.NullString
3863
        Signature   []byte
3864
}
3865

3866
// We use a separate upsert for our own node since we want to be less strict
3867
// about the last_update field. For our own node, we always want to
3868
// update the record even if the last_update is the same as what we have.
3869
func (q *Queries) UpsertSourceNode(ctx context.Context, arg UpsertSourceNodeParams) (int64, error) {
×
3870
        row := q.db.QueryRowContext(ctx, upsertSourceNode,
×
3871
                arg.Version,
×
3872
                arg.PubKey,
×
3873
                arg.Alias,
×
3874
                arg.LastUpdate,
×
NEW
3875
                arg.BlockHeight,
×
3876
                arg.Color,
×
3877
                arg.Signature,
×
3878
        )
×
3879
        var id int64
×
3880
        err := row.Scan(&id)
×
3881
        return id, err
×
3882
}
×
3883

3884
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
3885
/* ─────────────────────────────────────────────
3886
   graph_zombie_channels table queries
3887
   ─────────────────────────────────────────────
3888
*/
3889

3890
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
3891
VALUES ($1, $2, $3, $4)
3892
ON CONFLICT (scid, version)
3893
DO UPDATE SET
3894
    -- If a conflict exists for the SCID and version pair, then we
3895
    -- update the node keys.
3896
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
3897
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
3898
`
3899

3900
type UpsertZombieChannelParams struct {
3901
        Scid     []byte
3902
        Version  int16
3903
        NodeKey1 []byte
3904
        NodeKey2 []byte
3905
}
3906

3907
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
3908
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
3909
                arg.Scid,
×
3910
                arg.Version,
×
3911
                arg.NodeKey1,
×
3912
                arg.NodeKey2,
×
3913
        )
×
3914
        return err
×
3915
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc