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

lightningnetwork / lnd / 20342025150

18 Dec 2025 03:26PM UTC coverage: 65.128% (-0.07%) from 65.195%
20342025150

Pull #10414

github

web-flow
Merge ac96d94fa into 91423ee51
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%)

133 existing lines in 29 files now uncovered.

138014 of 211911 relevant lines covered (65.13%)

20742.58 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 OR n.id = c.node_id_2
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
)
2722
`
2723

2724
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2725
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2726
        var exists bool
×
2727
        err := row.Scan(&exists)
×
2728
        return exists, err
×
2729
}
×
2730

2731
const isZombieChannel = `-- name: IsZombieChannel :one
2732
SELECT EXISTS (
2733
    SELECT 1
2734
    FROM graph_zombie_channels
2735
    WHERE scid = $1
2736
    AND version = $2
2737
) AS is_zombie
2738
`
2739

2740
type IsZombieChannelParams struct {
2741
        Scid    []byte
2742
        Version int16
2743
}
2744

2745
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2746
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2747
        var is_zombie bool
×
2748
        err := row.Scan(&is_zombie)
×
2749
        return is_zombie, err
×
2750
}
×
2751

2752
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2753
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,
2754
    n1.pub_key AS node1_pubkey,
2755
    n2.pub_key AS node2_pubkey,
2756

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

2777
       -- Policy 2
2778
    cp2.id AS policy2_id,
2779
    cp2.node_id AS policy2_node_id,
2780
    cp2.version AS policy2_version,
2781
    cp2.timelock AS policy2_timelock,
2782
    cp2.fee_ppm AS policy2_fee_ppm,
2783
    cp2.base_fee_msat AS policy2_base_fee_msat,
2784
    cp2.min_htlc_msat AS policy2_min_htlc_msat,
2785
    cp2.max_htlc_msat AS policy2_max_htlc_msat,
2786
    cp2.last_update AS policy2_last_update,
2787
    cp2.disabled AS policy2_disabled,
2788
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2789
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2790
    cp2.message_flags AS policy2_message_flags,
2791
    cp2.channel_flags AS policy2_channel_flags,
2792
    cp2.signature AS policy2_signature
2793

2794
FROM graph_channels c
2795
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2796
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2797
    LEFT JOIN graph_channel_policies cp1
2798
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2799
    LEFT JOIN graph_channel_policies cp2
2800
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2801
WHERE c.version = $1
2802
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2803
`
2804

2805
type ListChannelsByNodeIDParams struct {
2806
        Version int16
2807
        NodeID1 int64
2808
}
2809

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

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

2918
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
2919
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,
2920
       n1.pub_key AS node1_pubkey,
2921
       n2.pub_key AS node2_pubkey,
2922

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

2943
       -- Policy 2
2944
       cp2.id AS policy2_id,
2945
       cp2.node_id AS policy2_node_id,
2946
       cp2.version AS policy2_version,
2947
       cp2.timelock AS policy2_timelock,
2948
       cp2.fee_ppm AS policy2_fee_ppm,
2949
       cp2.base_fee_msat AS policy2_base_fee_msat,
2950
       cp2.min_htlc_msat AS policy2_min_htlc_msat,
2951
       cp2.max_htlc_msat AS policy2_max_htlc_msat,
2952
       cp2.last_update AS policy2_last_update,
2953
       cp2.disabled AS policy2_disabled,
2954
       cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
2955
       cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
2956
       cp2.message_flags AS policy2_message_flags,
2957
       cp2.channel_flags AS policy2_channel_flags,
2958
       cp2.signature AS policy2_signature
2959

2960
FROM graph_channels c
2961
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2962
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2963
         LEFT JOIN graph_channel_policies cp1
2964
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2965
         LEFT JOIN graph_channel_policies cp2
2966
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2967
WHERE c.version = $1
2968
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
2969
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
2970
`
2971

2972
type ListChannelsForNodeIDsParams struct {
2973
        Version  int16
2974
        Node1Ids []int64
2975
        Node2Ids []int64
2976
}
2977

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

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

3105
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3106
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3107
FROM graph_channels c
3108
WHERE c.version = $1 AND c.id > $2
3109
ORDER BY c.id
3110
LIMIT $3
3111
`
3112

3113
type ListChannelsPaginatedParams struct {
3114
        Version int16
3115
        ID      int64
3116
        Limit   int32
3117
}
3118

3119
type ListChannelsPaginatedRow struct {
3120
        ID          int64
3121
        BitcoinKey1 []byte
3122
        BitcoinKey2 []byte
3123
        Outpoint    string
3124
}
3125

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

3154
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3155
SELECT
3156
    c.id as id,
3157
    c.scid as scid,
3158
    c.capacity AS capacity,
3159

3160
    -- Join node pubkeys
3161
    n1.pub_key AS node1_pubkey,
3162
    n2.pub_key AS node2_pubkey,
3163

3164
    -- Node 1 policy
3165
    cp1.timelock AS policy_1_timelock,
3166
    cp1.fee_ppm AS policy_1_fee_ppm,
3167
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3168
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3169
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3170
    cp1.disabled AS policy_1_disabled,
3171
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3172
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3173
    cp1.message_flags AS policy1_message_flags,
3174
    cp1.channel_flags AS policy1_channel_flags,
3175

3176
    -- Node 2 policy
3177
    cp2.timelock AS policy_2_timelock,
3178
    cp2.fee_ppm AS policy_2_fee_ppm,
3179
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3180
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3181
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3182
    cp2.disabled AS policy_2_disabled,
3183
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3184
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3185
    cp2.message_flags AS policy2_message_flags,
3186
    cp2.channel_flags AS policy2_channel_flags
3187

3188
FROM graph_channels c
3189
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3190
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3191
LEFT JOIN graph_channel_policies cp1
3192
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3193
LEFT JOIN graph_channel_policies cp2
3194
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3195
WHERE c.version = $1 AND c.id > $2
3196
ORDER BY c.id
3197
LIMIT $3
3198
`
3199

3200
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3201
        Version int16
3202
        ID      int64
3203
        Limit   int32
3204
}
3205

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

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

3283
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3284
SELECT
3285
    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,
3286

3287
    -- Join node pubkeys
3288
    n1.pub_key AS node1_pubkey,
3289
    n2.pub_key AS node2_pubkey,
3290

3291
    -- Node 1 policy
3292
    cp1.id AS policy_1_id,
3293
    cp1.node_id AS policy_1_node_id,
3294
    cp1.version AS policy_1_version,
3295
    cp1.timelock AS policy_1_timelock,
3296
    cp1.fee_ppm AS policy_1_fee_ppm,
3297
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3298
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3299
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3300
    cp1.last_update AS policy_1_last_update,
3301
    cp1.disabled AS policy_1_disabled,
3302
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3303
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3304
    cp1.message_flags AS policy1_message_flags,
3305
    cp1.channel_flags AS policy1_channel_flags,
3306
    cp1.signature AS policy_1_signature,
3307

3308
    -- Node 2 policy
3309
    cp2.id AS policy_2_id,
3310
    cp2.node_id AS policy_2_node_id,
3311
    cp2.version AS policy_2_version,
3312
    cp2.timelock AS policy_2_timelock,
3313
    cp2.fee_ppm AS policy_2_fee_ppm,
3314
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3315
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3316
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3317
    cp2.last_update AS policy_2_last_update,
3318
    cp2.disabled AS policy_2_disabled,
3319
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3320
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3321
    cp2.message_flags AS policy2_message_flags,
3322
    cp2.channel_flags AS policy2_channel_flags,
3323
    cp2.signature AS policy_2_signature
3324

3325
FROM graph_channels c
3326
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3327
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3328
LEFT JOIN graph_channel_policies cp1
3329
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3330
LEFT JOIN graph_channel_policies cp2
3331
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3332
WHERE c.version = $1 AND c.id > $2
3333
ORDER BY c.id
3334
LIMIT $3
3335
`
3336

3337
type ListChannelsWithPoliciesPaginatedParams struct {
3338
        Version int16
3339
        ID      int64
3340
        Limit   int32
3341
}
3342

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

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

3451
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3452
SELECT id, pub_key
3453
FROM graph_nodes
3454
WHERE version = $1  AND id > $2
3455
ORDER BY id
3456
LIMIT $3
3457
`
3458

3459
type ListNodeIDsAndPubKeysParams struct {
3460
        Version int16
3461
        ID      int64
3462
        Limit   int32
3463
}
3464

3465
type ListNodeIDsAndPubKeysRow struct {
3466
        ID     int64
3467
        PubKey []byte
3468
}
3469

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

3493
const listNodesPaginated = `-- name: ListNodesPaginated :many
3494
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
3495
FROM graph_nodes
3496
WHERE version = $1 AND id > $2
3497
ORDER BY id
3498
LIMIT $3
3499
`
3500

3501
type ListNodesPaginatedParams struct {
3502
        Version int16
3503
        ID      int64
3504
        Limit   int32
3505
}
3506

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

3539
const nodeExists = `-- name: NodeExists :one
3540
SELECT EXISTS (
3541
    SELECT 1
3542
    FROM graph_nodes
3543
    WHERE pub_key = $1
3544
      AND version = $2
3545
) AS node_exists
3546
`
3547

3548
type NodeExistsParams struct {
3549
        PubKey  []byte
3550
        Version int16
3551
}
3552

NEW
3553
func (q *Queries) NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error) {
×
NEW
3554
        row := q.db.QueryRowContext(ctx, nodeExists, arg.PubKey, arg.Version)
×
NEW
3555
        var node_exists bool
×
NEW
3556
        err := row.Scan(&node_exists)
×
NEW
3557
        return node_exists, err
×
NEW
3558
}
×
3559

3560
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3561
/* ─────────────────────────────────────────────
3562
   graph_channel_policy_extra_types table queries
3563
   ─────────────────────────────────────────────
3564
*/
3565

3566
INSERT INTO graph_channel_policy_extra_types (
3567
    channel_policy_id, type, value
3568
)
3569
VALUES ($1, $2, $3)
3570
ON CONFLICT (channel_policy_id, type)
3571
    -- If a conflict occurs on channel_policy_id and type, then we update the
3572
    -- value.
3573
    DO UPDATE SET value = EXCLUDED.value
3574
`
3575

3576
type UpsertChanPolicyExtraTypeParams struct {
3577
        ChannelPolicyID int64
3578
        Type            int64
3579
        Value           []byte
3580
}
3581

3582
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
3583
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
3584
        return err
×
3585
}
×
3586

3587
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3588
/* ─────────────────────────────────────────────
3589
   graph_channel_extra_types table queries
3590
   ─────────────────────────────────────────────
3591
*/
3592

3593
INSERT INTO graph_channel_extra_types (
3594
    channel_id, type, value
3595
)
3596
VALUES ($1, $2, $3)
3597
    ON CONFLICT (channel_id, type)
3598
    -- Update the value if a conflict occurs on channel_id and type.
3599
    DO UPDATE SET value = EXCLUDED.value
3600
`
3601

3602
type UpsertChannelExtraTypeParams struct {
3603
        ChannelID int64
3604
        Type      int64
3605
        Value     []byte
3606
}
3607

3608
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
3609
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
3610
        return err
×
3611
}
×
3612

3613
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3614
/* ─────────────────────────────────────────────
3615
   graph_channel_policies table queries
3616
   ─────────────────────────────────────────────
3617
*/
3618

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

3648
type UpsertEdgePolicyParams struct {
3649
        Version                 int16
3650
        ChannelID               int64
3651
        NodeID                  int64
3652
        Timelock                int32
3653
        FeePpm                  int64
3654
        BaseFeeMsat             int64
3655
        MinHtlcMsat             int64
3656
        LastUpdate              sql.NullInt64
3657
        Disabled                sql.NullBool
3658
        MaxHtlcMsat             sql.NullInt64
3659
        InboundBaseFeeMsat      sql.NullInt64
3660
        InboundFeeRateMilliMsat sql.NullInt64
3661
        MessageFlags            sql.NullInt16
3662
        ChannelFlags            sql.NullInt16
3663
        Signature               []byte
3664
}
3665

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

3689
const upsertNode = `-- name: UpsertNode :one
3690
/* ─────────────────────────────────────────────
3691
   graph_nodes table queries
3692
   ───────────────────────────��─────────────────
3693
*/
3694

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

3716
type UpsertNodeParams struct {
3717
        Version     int16
3718
        PubKey      []byte
3719
        Alias       sql.NullString
3720
        LastUpdate  sql.NullInt64
3721
        BlockHeight sql.NullInt64
3722
        Color       sql.NullString
3723
        Signature   []byte
3724
}
3725

3726
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3727
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3728
                arg.Version,
×
3729
                arg.PubKey,
×
3730
                arg.Alias,
×
3731
                arg.LastUpdate,
×
NEW
3732
                arg.BlockHeight,
×
3733
                arg.Color,
×
3734
                arg.Signature,
×
3735
        )
×
3736
        var id int64
×
3737
        err := row.Scan(&id)
×
3738
        return id, err
×
3739
}
×
3740

3741
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3742
/* ─────────────────────────────────────────────
3743
   graph_node_addresses table queries
3744
   ───────────────────────────────────��─────────
3745
*/
3746

3747
INSERT INTO graph_node_addresses (
3748
    node_id,
3749
    type,
3750
    address,
3751
    position
3752
) VALUES (
3753
    $1, $2, $3, $4
3754
) ON CONFLICT (node_id, type, position)
3755
    DO UPDATE SET address = EXCLUDED.address
3756
`
3757

3758
type UpsertNodeAddressParams struct {
3759
        NodeID   int64
3760
        Type     int16
3761
        Address  string
3762
        Position int32
3763
}
3764

3765
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3766
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3767
                arg.NodeID,
×
3768
                arg.Type,
×
3769
                arg.Address,
×
3770
                arg.Position,
×
3771
        )
×
3772
        return err
×
3773
}
×
3774

3775
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3776
/* ─────────────────────────────────────────────
3777
   graph_node_extra_types table queries
3778
   ─────────────────────────────────────────────
3779
*/
3780

3781
INSERT INTO graph_node_extra_types (
3782
    node_id, type, value
3783
)
3784
VALUES ($1, $2, $3)
3785
ON CONFLICT (type, node_id)
3786
    -- Update the value if a conflict occurs on type
3787
    -- and node_id.
3788
    DO UPDATE SET value = EXCLUDED.value
3789
`
3790

3791
type UpsertNodeExtraTypeParams struct {
3792
        NodeID int64
3793
        Type   int64
3794
        Value  []byte
3795
}
3796

3797
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
3798
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
3799
        return err
×
3800
}
×
3801

3802
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3803
/* ───────────────────────────���─────────────────
3804
    graph_prune_log table queries
3805
    ─────────────────────────────────────────────
3806
*/
3807

3808
INSERT INTO graph_prune_log (
3809
    block_height, block_hash
3810
) VALUES (
3811
    $1, $2
3812
)
3813
ON CONFLICT(block_height) DO UPDATE SET
3814
    block_hash = EXCLUDED.block_hash
3815
`
3816

3817
type UpsertPruneLogEntryParams struct {
3818
        BlockHeight int64
3819
        BlockHash   []byte
3820
}
3821

3822
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
3823
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
3824
        return err
×
3825
}
×
3826

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

3849
type UpsertSourceNodeParams struct {
3850
        Version     int16
3851
        PubKey      []byte
3852
        Alias       sql.NullString
3853
        LastUpdate  sql.NullInt64
3854
        BlockHeight sql.NullInt64
3855
        Color       sql.NullString
3856
        Signature   []byte
3857
}
3858

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

3877
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
3878
/* ─────────────────────────────────────────────
3879
   graph_zombie_channels table queries
3880
   ─────────────────────────────────────────────
3881
*/
3882

3883
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
3884
VALUES ($1, $2, $3, $4)
3885
ON CONFLICT (scid, version)
3886
DO UPDATE SET
3887
    -- If a conflict exists for the SCID and version pair, then we
3888
    -- update the node keys.
3889
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
3890
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
3891
`
3892

3893
type UpsertZombieChannelParams struct {
3894
        Scid     []byte
3895
        Version  int16
3896
        NodeKey1 []byte
3897
        NodeKey2 []byte
3898
}
3899

3900
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
3901
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
3902
                arg.Scid,
×
3903
                arg.Version,
×
3904
                arg.NodeKey1,
×
3905
                arg.NodeKey2,
×
3906
        )
×
3907
        return err
×
3908
}
×
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