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

lightningnetwork / lnd / 19512294102

19 Nov 2025 06:31PM UTC coverage: 65.184% (-0.06%) from 65.243%
19512294102

Pull #10339

github

web-flow
Merge 5aad165f2 into 194a9f759
Pull Request #10339: [g175:2] graph/db: v2 columns and v2 node CRUD

205 of 389 new or added lines in 11 files covered. (52.7%)

119 existing lines in 28 files now uncovered.

137707 of 211260 relevant lines covered (65.18%)

20761.01 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
82
) VALUES (
83
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
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
}
102

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

123
const deleteChannelPolicyExtraTypes = `-- name: DeleteChannelPolicyExtraTypes :exec
124
DELETE FROM graph_channel_policy_extra_types
125
WHERE channel_policy_id = $1
126
`
127

128
func (q *Queries) DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error {
×
129
        _, err := q.db.ExecContext(ctx, deleteChannelPolicyExtraTypes, channelPolicyID)
×
130
        return err
×
131
}
×
132

133
const deleteChannels = `-- name: DeleteChannels :exec
134
DELETE FROM graph_channels
135
WHERE id IN (/*SLICE:ids*/?)
136
`
137

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

153
const deleteExtraNodeType = `-- name: DeleteExtraNodeType :exec
154
DELETE FROM graph_node_extra_types
155
WHERE node_id = $1
156
  AND type = $2
157
`
158

159
type DeleteExtraNodeTypeParams struct {
160
        NodeID int64
161
        Type   int64
162
}
163

164
func (q *Queries) DeleteExtraNodeType(ctx context.Context, arg DeleteExtraNodeTypeParams) error {
×
165
        _, err := q.db.ExecContext(ctx, deleteExtraNodeType, arg.NodeID, arg.Type)
×
166
        return err
×
167
}
×
168

169
const deleteNode = `-- name: DeleteNode :exec
170
DELETE FROM graph_nodes
171
WHERE id = $1
172
`
173

174
func (q *Queries) DeleteNode(ctx context.Context, id int64) error {
×
175
        _, err := q.db.ExecContext(ctx, deleteNode, id)
×
176
        return err
×
177
}
×
178

179
const deleteNodeAddresses = `-- name: DeleteNodeAddresses :exec
180
DELETE FROM graph_node_addresses
181
WHERE node_id = $1
182
`
183

184
func (q *Queries) DeleteNodeAddresses(ctx context.Context, nodeID int64) error {
×
185
        _, err := q.db.ExecContext(ctx, deleteNodeAddresses, nodeID)
×
186
        return err
×
187
}
×
188

189
const deleteNodeByPubKey = `-- name: DeleteNodeByPubKey :execresult
190
DELETE FROM graph_nodes
191
WHERE pub_key = $1
192
  AND version = $2
193
`
194

195
type DeleteNodeByPubKeyParams struct {
196
        PubKey  []byte
197
        Version int16
198
}
199

200
func (q *Queries) DeleteNodeByPubKey(ctx context.Context, arg DeleteNodeByPubKeyParams) (sql.Result, error) {
×
201
        return q.db.ExecContext(ctx, deleteNodeByPubKey, arg.PubKey, arg.Version)
×
202
}
×
203

204
const deleteNodeFeature = `-- name: DeleteNodeFeature :exec
205
DELETE FROM graph_node_features
206
WHERE node_id = $1
207
  AND feature_bit = $2
208
`
209

210
type DeleteNodeFeatureParams struct {
211
        NodeID     int64
212
        FeatureBit int32
213
}
214

215
func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeatureParams) error {
×
216
        _, err := q.db.ExecContext(ctx, deleteNodeFeature, arg.NodeID, arg.FeatureBit)
×
217
        return err
×
218
}
×
219

220
const deletePruneLogEntriesInRange = `-- name: DeletePruneLogEntriesInRange :exec
221
DELETE FROM graph_prune_log
222
WHERE block_height >= $1
223
  AND block_height <= $2
224
`
225

226
type DeletePruneLogEntriesInRangeParams struct {
227
        StartHeight int64
228
        EndHeight   int64
229
}
230

231
func (q *Queries) DeletePruneLogEntriesInRange(ctx context.Context, arg DeletePruneLogEntriesInRangeParams) error {
×
232
        _, err := q.db.ExecContext(ctx, deletePruneLogEntriesInRange, arg.StartHeight, arg.EndHeight)
×
233
        return err
×
234
}
×
235

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

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

276
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
277
DELETE FROM graph_zombie_channels
278
WHERE scid = $1
279
AND version = $2
280
`
281

282
type DeleteZombieChannelParams struct {
283
        Scid    []byte
284
        Version int16
285
}
286

287
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
×
288
        return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
×
289
}
×
290

291
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
292
SELECT
293
    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,
294
    n1.pub_key AS node1_pub_key,
295
    n2.pub_key AS node2_pub_key
296
FROM graph_channels c
297
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
298
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
299
WHERE c.scid = $1
300
  AND c.version = $2
301
`
302

303
type GetChannelAndNodesBySCIDParams struct {
304
        Scid    []byte
305
        Version int16
306
}
307

308
type GetChannelAndNodesBySCIDRow struct {
309
        ID                int64
310
        Version           int16
311
        Scid              []byte
312
        NodeID1           int64
313
        NodeID2           int64
314
        Outpoint          string
315
        Capacity          sql.NullInt64
316
        BitcoinKey1       []byte
317
        BitcoinKey2       []byte
318
        Node1Signature    []byte
319
        Node2Signature    []byte
320
        Bitcoin1Signature []byte
321
        Bitcoin2Signature []byte
322
        Signature         []byte
323
        FundingPkScript   []byte
324
        MerkleRootHash    []byte
325
        Node1PubKey       []byte
326
        Node2PubKey       []byte
327
}
328

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

355
const getChannelByOutpointWithPolicies = `-- name: GetChannelByOutpointWithPolicies :one
356
SELECT
357
    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,
358

359
    n1.pub_key AS node1_pubkey,
360
    n2.pub_key AS node2_pubkey,
361

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

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

405
type GetChannelByOutpointWithPoliciesParams struct {
406
        Outpoint string
407
        Version  int16
408
}
409

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

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

502
const getChannelBySCID = `-- name: GetChannelBySCID :one
503
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
504
WHERE scid = $1 AND version = $2
505
`
506

507
type GetChannelBySCIDParams struct {
508
        Scid    []byte
509
        Version int16
510
}
511

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

536
const getChannelBySCIDWithPolicies = `-- name: GetChannelBySCIDWithPolicies :one
537
SELECT
538
    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,
539
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
540
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
541

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

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

576
FROM graph_channels c
577
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
578
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
579
    LEFT JOIN graph_channel_policies cp1
580
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
581
    LEFT JOIN graph_channel_policies cp2
582
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
583
WHERE c.scid = $1
584
  AND c.version = $2
585
`
586

587
type GetChannelBySCIDWithPoliciesParams struct {
588
        Scid    []byte
589
        Version int16
590
}
591

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

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

698
const getChannelExtrasBatch = `-- name: GetChannelExtrasBatch :many
699
SELECT
700
    channel_id,
701
    type,
702
    value
703
FROM graph_channel_extra_types
704
WHERE channel_id IN (/*SLICE:chan_ids*/?)
705
ORDER BY channel_id, type
706
`
707

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

741
const getChannelFeaturesBatch = `-- name: GetChannelFeaturesBatch :many
742
SELECT
743
    channel_id,
744
    feature_bit
745
FROM graph_channel_features
746
WHERE channel_id IN (/*SLICE:chan_ids*/?)
747
ORDER BY channel_id, feature_bit
748
`
749

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

783
const getChannelPolicyByChannelAndNode = `-- name: GetChannelPolicyByChannelAndNode :one
784
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
785
FROM graph_channel_policies
786
WHERE channel_id = $1
787
  AND node_id = $2
788
  AND version = $3
789
`
790

791
type GetChannelPolicyByChannelAndNodeParams struct {
792
        ChannelID int64
793
        NodeID    int64
794
        Version   int16
795
}
796

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

823
const getChannelPolicyExtraTypesBatch = `-- name: GetChannelPolicyExtraTypesBatch :many
824
SELECT
825
    channel_policy_id as policy_id,
826
    type,
827
    value
828
FROM graph_channel_policy_extra_types
829
WHERE channel_policy_id IN (/*SLICE:policy_ids*/?)
830
ORDER BY channel_policy_id, type
831
`
832

833
type GetChannelPolicyExtraTypesBatchRow struct {
834
        PolicyID int64
835
        Type     int64
836
        Value    []byte
837
}
838

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

872
const getChannelsByIDs = `-- name: GetChannelsByIDs :many
873
SELECT
874
    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,
875

876
    -- Minimal node data.
877
    n1.id AS node1_id,
878
    n1.pub_key AS node1_pub_key,
879
    n2.id AS node2_id,
880
    n2.pub_key AS node2_pub_key,
881

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

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

916
FROM graph_channels c
917
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
918
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
919
    LEFT JOIN graph_channel_policies cp1
920
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
921
    LEFT JOIN graph_channel_policies cp2
922
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
923
WHERE c.id IN (/*SLICE:ids*/?)
924
`
925

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

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

1048
const getChannelsByOutpoints = `-- name: GetChannelsByOutpoints :many
1049
SELECT
1050
    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,
1051
    n1.pub_key AS node1_pubkey,
1052
    n2.pub_key AS node2_pubkey
1053
FROM graph_channels c
1054
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1055
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1056
WHERE c.outpoint IN
1057
    (/*SLICE:outpoints*/?)
1058
`
1059

1060
type GetChannelsByOutpointsRow struct {
1061
        GraphChannel GraphChannel
1062
        Node1Pubkey  []byte
1063
        Node2Pubkey  []byte
1064
}
1065

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

1118
const getChannelsByPolicyLastUpdateRange = `-- name: GetChannelsByPolicyLastUpdateRange :many
1119
SELECT
1120
    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,
1121
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1122
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1123

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

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

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

1197
type GetChannelsByPolicyLastUpdateRangeParams struct {
1198
        Version        int16
1199
        StartTime      sql.NullInt64
1200
        EndTime        sql.NullInt64
1201
        LastUpdateTime sql.NullInt64
1202
        LastID         sql.NullInt64
1203
        MaxResults     interface{}
1204
}
1205

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

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

1335
const getChannelsBySCIDRange = `-- name: GetChannelsBySCIDRange :many
1336
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,
1337
    n1.pub_key AS node1_pub_key,
1338
    n2.pub_key AS node2_pub_key
1339
FROM graph_channels c
1340
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1341
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1342
WHERE scid >= $1
1343
  AND scid < $2
1344
`
1345

1346
type GetChannelsBySCIDRangeParams struct {
1347
        StartScid []byte
1348
        EndScid   []byte
1349
}
1350

1351
type GetChannelsBySCIDRangeRow struct {
1352
        GraphChannel GraphChannel
1353
        Node1PubKey  []byte
1354
        Node2PubKey  []byte
1355
}
1356

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

1399
const getChannelsBySCIDWithPolicies = `-- name: GetChannelsBySCIDWithPolicies :many
1400
SELECT
1401
    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,
1402
    n1.id, n1.version, n1.pub_key, n1.alias, n1.last_update, n1.color, n1.signature, n1.block_height,
1403
    n2.id, n2.version, n2.pub_key, n2.alias, n2.last_update, n2.color, n2.signature, n2.block_height,
1404

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

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

1439
FROM graph_channels c
1440
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
1441
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
1442
    LEFT JOIN graph_channel_policies cp1
1443
        ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
1444
    LEFT JOIN graph_channel_policies cp2
1445
        ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
1446
WHERE
1447
    c.version = $1
1448
  AND c.scid IN (/*SLICE:scids*/?)
1449
`
1450

1451
type GetChannelsBySCIDWithPoliciesParams struct {
1452
        Version int16
1453
        Scids   [][]byte
1454
}
1455

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

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

1589
const getChannelsBySCIDs = `-- name: GetChannelsBySCIDs :many
1590
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
1591
WHERE version = $1
1592
  AND scid IN (/*SLICE:scids*/?)
1593
`
1594

1595
type GetChannelsBySCIDsParams struct {
1596
        Version int16
1597
        Scids   [][]byte
1598
}
1599

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

1651
const getClosedChannelsSCIDs = `-- name: GetClosedChannelsSCIDs :many
1652
SELECT scid
1653
FROM graph_closed_scids
1654
WHERE scid IN (/*SLICE:scids*/?)
1655
`
1656

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

1690
const getExtraNodeTypes = `-- name: GetExtraNodeTypes :many
1691
SELECT node_id, type, value
1692
FROM graph_node_extra_types
1693
WHERE node_id = $1
1694
`
1695

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

1719
const getNodeAddresses = `-- name: GetNodeAddresses :many
1720
SELECT type, address
1721
FROM graph_node_addresses
1722
WHERE node_id = $1
1723
ORDER BY type ASC, position ASC
1724
`
1725

1726
type GetNodeAddressesRow struct {
1727
        Type    int16
1728
        Address string
1729
}
1730

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

1754
const getNodeAddressesBatch = `-- name: GetNodeAddressesBatch :many
1755
SELECT node_id, type, position, address
1756
FROM graph_node_addresses
1757
WHERE node_id IN (/*SLICE:ids*/?)
1758
ORDER BY node_id, type, position
1759
`
1760

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

1799
const getNodeByPubKey = `-- name: GetNodeByPubKey :one
1800
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
1801
FROM graph_nodes
1802
WHERE pub_key = $1
1803
  AND version = $2
1804
`
1805

1806
type GetNodeByPubKeyParams struct {
1807
        PubKey  []byte
1808
        Version int16
1809
}
1810

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

1827
const getNodeExtraTypesBatch = `-- name: GetNodeExtraTypesBatch :many
1828
SELECT node_id, type, value
1829
FROM graph_node_extra_types
1830
WHERE node_id IN (/*SLICE:ids*/?)
1831
ORDER BY node_id, type
1832
`
1833

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

1867
const getNodeFeatures = `-- name: GetNodeFeatures :many
1868
SELECT node_id, feature_bit
1869
FROM graph_node_features
1870
WHERE node_id = $1
1871
`
1872

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

1896
const getNodeFeaturesBatch = `-- name: GetNodeFeaturesBatch :many
1897
SELECT node_id, feature_bit
1898
FROM graph_node_features
1899
WHERE node_id IN (/*SLICE:ids*/?)
1900
ORDER BY node_id, feature_bit
1901
`
1902

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

1936
const getNodeFeaturesByPubKey = `-- name: GetNodeFeaturesByPubKey :many
1937
SELECT f.feature_bit
1938
FROM graph_nodes n
1939
    JOIN graph_node_features f ON f.node_id = n.id
1940
WHERE n.pub_key = $1
1941
  AND n.version = $2
1942
`
1943

1944
type GetNodeFeaturesByPubKeyParams struct {
1945
        PubKey  []byte
1946
        Version int16
1947
}
1948

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

1972
const getNodeIDByPubKey = `-- name: GetNodeIDByPubKey :one
1973
SELECT id
1974
FROM graph_nodes
1975
WHERE pub_key = $1
1976
  AND version = $2
1977
`
1978

1979
type GetNodeIDByPubKeyParams struct {
1980
        PubKey  []byte
1981
        Version int16
1982
}
1983

1984
func (q *Queries) GetNodeIDByPubKey(ctx context.Context, arg GetNodeIDByPubKeyParams) (int64, error) {
×
1985
        row := q.db.QueryRowContext(ctx, getNodeIDByPubKey, arg.PubKey, arg.Version)
×
1986
        var id int64
×
1987
        err := row.Scan(&id)
×
1988
        return id, err
×
1989
}
×
1990

1991
const getNodesByIDs = `-- name: GetNodesByIDs :many
1992
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
1993
FROM graph_nodes
1994
WHERE id IN (/*SLICE:ids*/?)
1995
`
1996

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

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

2074
type GetNodesByLastUpdateRangeParams struct {
2075
        StartTime  sql.NullInt64
2076
        EndTime    sql.NullInt64
2077
        LastUpdate sql.NullInt64
2078
        LastPubKey []byte
2079
        OnlyPublic interface{}
2080
        MaxResults interface{}
2081
}
2082

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

2122
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
2123
SELECT block_height, block_hash
2124
FROM graph_prune_log
2125
WHERE block_height
2126
   IN (/*SLICE:heights*/?)
2127
`
2128

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

2162
const getPruneHashByHeight = `-- name: GetPruneHashByHeight :one
2163
SELECT block_hash
2164
FROM graph_prune_log
2165
WHERE block_height = $1
2166
`
2167

2168
func (q *Queries) GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error) {
×
2169
        row := q.db.QueryRowContext(ctx, getPruneHashByHeight, blockHeight)
×
2170
        var block_hash []byte
×
2171
        err := row.Scan(&block_hash)
×
2172
        return block_hash, err
×
2173
}
×
2174

2175
const getPruneTip = `-- name: GetPruneTip :one
2176
SELECT block_height, block_hash
2177
FROM graph_prune_log
2178
ORDER BY block_height DESC
2179
LIMIT 1
2180
`
2181

2182
func (q *Queries) GetPruneTip(ctx context.Context) (GraphPruneLog, error) {
×
2183
        row := q.db.QueryRowContext(ctx, getPruneTip)
×
2184
        var i GraphPruneLog
×
2185
        err := row.Scan(&i.BlockHeight, &i.BlockHash)
×
2186
        return i, err
×
2187
}
×
2188

2189
const getPublicV1ChannelsBySCID = `-- name: GetPublicV1ChannelsBySCID :many
2190
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
2191
FROM graph_channels
2192
WHERE node_1_signature IS NOT NULL
2193
  AND scid >= $1
2194
  AND scid < $2
2195
`
2196

2197
type GetPublicV1ChannelsBySCIDParams struct {
2198
        StartScid []byte
2199
        EndScid   []byte
2200
}
2201

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

2242
const getSCIDByOutpoint = `-- name: GetSCIDByOutpoint :one
2243
SELECT scid from graph_channels
2244
WHERE outpoint = $1 AND version = $2
2245
`
2246

2247
type GetSCIDByOutpointParams struct {
2248
        Outpoint string
2249
        Version  int16
2250
}
2251

2252
func (q *Queries) GetSCIDByOutpoint(ctx context.Context, arg GetSCIDByOutpointParams) ([]byte, error) {
×
2253
        row := q.db.QueryRowContext(ctx, getSCIDByOutpoint, arg.Outpoint, arg.Version)
×
2254
        var scid []byte
×
2255
        err := row.Scan(&scid)
×
2256
        return scid, err
×
2257
}
×
2258

2259
const getSourceNodesByVersion = `-- name: GetSourceNodesByVersion :many
2260
SELECT sn.node_id, n.pub_key
2261
FROM graph_source_nodes sn
2262
    JOIN graph_nodes n ON sn.node_id = n.id
2263
WHERE n.version = $1
2264
`
2265

2266
type GetSourceNodesByVersionRow struct {
2267
        NodeID int64
2268
        PubKey []byte
2269
}
2270

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

2294
const getV1DisabledSCIDs = `-- name: GetV1DisabledSCIDs :many
2295
SELECT c.scid
2296
FROM graph_channels c
2297
    JOIN graph_channel_policies cp ON cp.channel_id = c.id
2298
WHERE cp.disabled = true
2299
AND c.version = 1
2300
GROUP BY c.scid
2301
HAVING COUNT(*) > 1
2302
`
2303

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

2331
const getZombieChannel = `-- name: GetZombieChannel :one
2332
SELECT scid, version, node_key_1, node_key_2
2333
FROM graph_zombie_channels
2334
WHERE scid = $1
2335
AND version = $2
2336
`
2337

2338
type GetZombieChannelParams struct {
2339
        Scid    []byte
2340
        Version int16
2341
}
2342

2343
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (GraphZombieChannel, error) {
×
2344
        row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
×
2345
        var i GraphZombieChannel
×
2346
        err := row.Scan(
×
2347
                &i.Scid,
×
2348
                &i.Version,
×
2349
                &i.NodeKey1,
×
2350
                &i.NodeKey2,
×
2351
        )
×
2352
        return i, err
×
2353
}
×
2354

2355
const getZombieChannelsSCIDs = `-- name: GetZombieChannelsSCIDs :many
2356
SELECT scid, version, node_key_1, node_key_2
2357
FROM graph_zombie_channels
2358
WHERE version = $1
2359
  AND scid IN (/*SLICE:scids*/?)
2360
`
2361

2362
type GetZombieChannelsSCIDsParams struct {
2363
        Version int16
2364
        Scids   [][]byte
2365
}
2366

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

2406
const highestSCID = `-- name: HighestSCID :one
2407
SELECT scid
2408
FROM graph_channels
2409
WHERE version = $1
2410
ORDER BY scid DESC
2411
LIMIT 1
2412
`
2413

2414
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
×
2415
        row := q.db.QueryRowContext(ctx, highestSCID, version)
×
2416
        var scid []byte
×
2417
        err := row.Scan(&scid)
×
2418
        return scid, err
×
2419
}
×
2420

2421
const insertChannelFeature = `-- name: InsertChannelFeature :exec
2422
/* ─────────────────────────────────────────────
2423
   graph_channel_features table queries
2424
   ─────────────────────────────────────────────
2425
*/
2426

2427
INSERT INTO graph_channel_features (
2428
    channel_id, feature_bit
2429
) VALUES (
2430
    $1, $2
2431
) ON CONFLICT (channel_id, feature_bit)
2432
    -- Do nothing if the channel_id and feature_bit already exist.
2433
    DO NOTHING
2434
`
2435

2436
type InsertChannelFeatureParams struct {
2437
        ChannelID  int64
2438
        FeatureBit int32
2439
}
2440

2441
func (q *Queries) InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error {
×
2442
        _, err := q.db.ExecContext(ctx, insertChannelFeature, arg.ChannelID, arg.FeatureBit)
×
2443
        return err
×
2444
}
×
2445

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

2472
type InsertChannelMigParams struct {
2473
        Version           int16
2474
        Scid              []byte
2475
        NodeID1           int64
2476
        NodeID2           int64
2477
        Outpoint          string
2478
        Capacity          sql.NullInt64
2479
        BitcoinKey1       []byte
2480
        BitcoinKey2       []byte
2481
        Node1Signature    []byte
2482
        Node2Signature    []byte
2483
        Bitcoin1Signature []byte
2484
        Bitcoin2Signature []byte
2485
}
2486

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

2512
const insertClosedChannel = `-- name: InsertClosedChannel :exec
2513
/* ─────────────────────────────────────────────
2514
   graph_closed_scid table queries
2515
   ────────────────────────────────────────────-
2516
*/
2517

2518
INSERT INTO graph_closed_scids (scid)
2519
VALUES ($1)
2520
ON CONFLICT (scid) DO NOTHING
2521
`
2522

2523
func (q *Queries) InsertClosedChannel(ctx context.Context, scid []byte) error {
×
2524
        _, err := q.db.ExecContext(ctx, insertClosedChannel, scid)
×
2525
        return err
×
2526
}
×
2527

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

2558
type InsertEdgePolicyMigParams struct {
2559
        Version                 int16
2560
        ChannelID               int64
2561
        NodeID                  int64
2562
        Timelock                int32
2563
        FeePpm                  int64
2564
        BaseFeeMsat             int64
2565
        MinHtlcMsat             int64
2566
        LastUpdate              sql.NullInt64
2567
        Disabled                sql.NullBool
2568
        MaxHtlcMsat             sql.NullInt64
2569
        InboundBaseFeeMsat      sql.NullInt64
2570
        InboundFeeRateMilliMsat sql.NullInt64
2571
        MessageFlags            sql.NullInt16
2572
        ChannelFlags            sql.NullInt16
2573
        Signature               []byte
2574
}
2575

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

2604
const insertNodeFeature = `-- name: InsertNodeFeature :exec
2605
/* ─────────────────────────────────────────────
2606
   graph_node_features table queries
2607
   ─────────────────────────────────────────────
2608
*/
2609

2610
INSERT INTO graph_node_features (
2611
    node_id, feature_bit
2612
) VALUES (
2613
    $1, $2
2614
) ON CONFLICT (node_id, feature_bit)
2615
    -- Do nothing if the feature already exists for the node.
2616
    DO NOTHING
2617
`
2618

2619
type InsertNodeFeatureParams struct {
2620
        NodeID     int64
2621
        FeatureBit int32
2622
}
2623

2624
func (q *Queries) InsertNodeFeature(ctx context.Context, arg InsertNodeFeatureParams) error {
×
2625
        _, err := q.db.ExecContext(ctx, insertNodeFeature, arg.NodeID, arg.FeatureBit)
×
2626
        return err
×
2627
}
×
2628

2629
const insertNodeMig = `-- name: InsertNodeMig :one
2630
/* ─────────────────────────────────────────────
2631
   Migration specific queries
2632

2633
   NOTE: once sqldbv2 is in place, these queries can be contained to a package
2634
   dedicated to the migration that requires it, and so we can then remove
2635
   it from the main set of "live" queries that the code-base has access to.
2636
   ────────────────────────────────────────────-
2637
*/
2638

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

2656
type InsertNodeMigParams struct {
2657
        Version    int16
2658
        PubKey     []byte
2659
        Alias      sql.NullString
2660
        LastUpdate sql.NullInt64
2661
        Color      sql.NullString
2662
        Signature  []byte
2663
}
2664

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

2684
const isClosedChannel = `-- name: IsClosedChannel :one
2685
SELECT EXISTS (
2686
    SELECT 1
2687
    FROM graph_closed_scids
2688
    WHERE scid = $1
2689
)
2690
`
2691

2692
func (q *Queries) IsClosedChannel(ctx context.Context, scid []byte) (bool, error) {
×
2693
        row := q.db.QueryRowContext(ctx, isClosedChannel, scid)
×
2694
        var exists bool
×
2695
        err := row.Scan(&exists)
×
2696
        return exists, err
×
2697
}
×
2698

2699
const isPublicV1Node = `-- name: IsPublicV1Node :one
2700
SELECT EXISTS (
2701
    SELECT 1
2702
    FROM graph_channels c
2703
    JOIN graph_nodes n ON n.id = c.node_id_1 OR n.id = c.node_id_2
2704
    -- NOTE: we hard-code the version here since the clauses
2705
    -- here that determine if a node is public is specific
2706
    -- to the V1 gossip protocol. In V1, a node is public
2707
    -- if it has a public channel and a public channel is one
2708
    -- where we have the set of signatures of the channel
2709
    -- announcement. It is enough to just check that we have
2710
    -- one of the signatures since we only ever set them
2711
    -- together.
2712
    WHERE c.version = 1
2713
      AND c.bitcoin_1_signature IS NOT NULL
2714
      AND n.pub_key = $1
2715
)
2716
`
2717

2718
func (q *Queries) IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error) {
×
2719
        row := q.db.QueryRowContext(ctx, isPublicV1Node, pubKey)
×
2720
        var exists bool
×
2721
        err := row.Scan(&exists)
×
2722
        return exists, err
×
2723
}
×
2724

2725
const isZombieChannel = `-- name: IsZombieChannel :one
2726
SELECT EXISTS (
2727
    SELECT 1
2728
    FROM graph_zombie_channels
2729
    WHERE scid = $1
2730
    AND version = $2
2731
) AS is_zombie
2732
`
2733

2734
type IsZombieChannelParams struct {
2735
        Scid    []byte
2736
        Version int16
2737
}
2738

2739
func (q *Queries) IsZombieChannel(ctx context.Context, arg IsZombieChannelParams) (bool, error) {
×
2740
        row := q.db.QueryRowContext(ctx, isZombieChannel, arg.Scid, arg.Version)
×
2741
        var is_zombie bool
×
2742
        err := row.Scan(&is_zombie)
×
2743
        return is_zombie, err
×
2744
}
×
2745

2746
const listChannelsByNodeID = `-- name: ListChannelsByNodeID :many
2747
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,
2748
    n1.pub_key AS node1_pubkey,
2749
    n2.pub_key AS node2_pubkey,
2750

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

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

2788
FROM graph_channels c
2789
    JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2790
    JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2791
    LEFT JOIN graph_channel_policies cp1
2792
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2793
    LEFT JOIN graph_channel_policies cp2
2794
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2795
WHERE c.version = $1
2796
  AND (c.node_id_1 = $2 OR c.node_id_2 = $2)
2797
`
2798

2799
type ListChannelsByNodeIDParams struct {
2800
        Version int16
2801
        NodeID1 int64
2802
}
2803

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

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

2912
const listChannelsForNodeIDs = `-- name: ListChannelsForNodeIDs :many
2913
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,
2914
       n1.pub_key AS node1_pubkey,
2915
       n2.pub_key AS node2_pubkey,
2916

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

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

2954
FROM graph_channels c
2955
         JOIN graph_nodes n1 ON c.node_id_1 = n1.id
2956
         JOIN graph_nodes n2 ON c.node_id_2 = n2.id
2957
         LEFT JOIN graph_channel_policies cp1
2958
                   ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
2959
         LEFT JOIN graph_channel_policies cp2
2960
                   ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
2961
WHERE c.version = $1
2962
  AND (c.node_id_1 IN (/*SLICE:node1_ids*/?)
2963
   OR c.node_id_2 IN (/*SLICE:node2_ids*/?))
2964
`
2965

2966
type ListChannelsForNodeIDsParams struct {
2967
        Version  int16
2968
        Node1Ids []int64
2969
        Node2Ids []int64
2970
}
2971

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

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

3099
const listChannelsPaginated = `-- name: ListChannelsPaginated :many
3100
SELECT id, bitcoin_key_1, bitcoin_key_2, outpoint
3101
FROM graph_channels c
3102
WHERE c.version = $1 AND c.id > $2
3103
ORDER BY c.id
3104
LIMIT $3
3105
`
3106

3107
type ListChannelsPaginatedParams struct {
3108
        Version int16
3109
        ID      int64
3110
        Limit   int32
3111
}
3112

3113
type ListChannelsPaginatedRow struct {
3114
        ID          int64
3115
        BitcoinKey1 []byte
3116
        BitcoinKey2 []byte
3117
        Outpoint    string
3118
}
3119

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

3148
const listChannelsWithPoliciesForCachePaginated = `-- name: ListChannelsWithPoliciesForCachePaginated :many
3149
SELECT
3150
    c.id as id,
3151
    c.scid as scid,
3152
    c.capacity AS capacity,
3153

3154
    -- Join node pubkeys
3155
    n1.pub_key AS node1_pubkey,
3156
    n2.pub_key AS node2_pubkey,
3157

3158
    -- Node 1 policy
3159
    cp1.timelock AS policy_1_timelock,
3160
    cp1.fee_ppm AS policy_1_fee_ppm,
3161
    cp1.base_fee_msat AS policy_1_base_fee_msat,
3162
    cp1.min_htlc_msat AS policy_1_min_htlc_msat,
3163
    cp1.max_htlc_msat AS policy_1_max_htlc_msat,
3164
    cp1.disabled AS policy_1_disabled,
3165
    cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
3166
    cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
3167
    cp1.message_flags AS policy1_message_flags,
3168
    cp1.channel_flags AS policy1_channel_flags,
3169

3170
    -- Node 2 policy
3171
    cp2.timelock AS policy_2_timelock,
3172
    cp2.fee_ppm AS policy_2_fee_ppm,
3173
    cp2.base_fee_msat AS policy_2_base_fee_msat,
3174
    cp2.min_htlc_msat AS policy_2_min_htlc_msat,
3175
    cp2.max_htlc_msat AS policy_2_max_htlc_msat,
3176
    cp2.disabled AS policy_2_disabled,
3177
    cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
3178
    cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
3179
    cp2.message_flags AS policy2_message_flags,
3180
    cp2.channel_flags AS policy2_channel_flags
3181

3182
FROM graph_channels c
3183
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3184
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3185
LEFT JOIN graph_channel_policies cp1
3186
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3187
LEFT JOIN graph_channel_policies cp2
3188
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3189
WHERE c.version = $1 AND c.id > $2
3190
ORDER BY c.id
3191
LIMIT $3
3192
`
3193

3194
type ListChannelsWithPoliciesForCachePaginatedParams struct {
3195
        Version int16
3196
        ID      int64
3197
        Limit   int32
3198
}
3199

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

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

3277
const listChannelsWithPoliciesPaginated = `-- name: ListChannelsWithPoliciesPaginated :many
3278
SELECT
3279
    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,
3280

3281
    -- Join node pubkeys
3282
    n1.pub_key AS node1_pubkey,
3283
    n2.pub_key AS node2_pubkey,
3284

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

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

3319
FROM graph_channels c
3320
JOIN graph_nodes n1 ON c.node_id_1 = n1.id
3321
JOIN graph_nodes n2 ON c.node_id_2 = n2.id
3322
LEFT JOIN graph_channel_policies cp1
3323
    ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
3324
LEFT JOIN graph_channel_policies cp2
3325
    ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
3326
WHERE c.version = $1 AND c.id > $2
3327
ORDER BY c.id
3328
LIMIT $3
3329
`
3330

3331
type ListChannelsWithPoliciesPaginatedParams struct {
3332
        Version int16
3333
        ID      int64
3334
        Limit   int32
3335
}
3336

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

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

3445
const listNodeIDsAndPubKeys = `-- name: ListNodeIDsAndPubKeys :many
3446
SELECT id, pub_key
3447
FROM graph_nodes
3448
WHERE version = $1  AND id > $2
3449
ORDER BY id
3450
LIMIT $3
3451
`
3452

3453
type ListNodeIDsAndPubKeysParams struct {
3454
        Version int16
3455
        ID      int64
3456
        Limit   int32
3457
}
3458

3459
type ListNodeIDsAndPubKeysRow struct {
3460
        ID     int64
3461
        PubKey []byte
3462
}
3463

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

3487
const listNodesPaginated = `-- name: ListNodesPaginated :many
3488
SELECT id, version, pub_key, alias, last_update, color, signature, block_height
3489
FROM graph_nodes
3490
WHERE version = $1 AND id > $2
3491
ORDER BY id
3492
LIMIT $3
3493
`
3494

3495
type ListNodesPaginatedParams struct {
3496
        Version int16
3497
        ID      int64
3498
        Limit   int32
3499
}
3500

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

3533
const upsertChanPolicyExtraType = `-- name: UpsertChanPolicyExtraType :exec
3534
/* ─────────────────────────────────────────────
3535
   graph_channel_policy_extra_types table queries
3536
   ─────────────────────────────────────────────
3537
*/
3538

3539
INSERT INTO graph_channel_policy_extra_types (
3540
    channel_policy_id, type, value
3541
)
3542
VALUES ($1, $2, $3)
3543
ON CONFLICT (channel_policy_id, type)
3544
    -- If a conflict occurs on channel_policy_id and type, then we update the
3545
    -- value.
3546
    DO UPDATE SET value = EXCLUDED.value
3547
`
3548

3549
type UpsertChanPolicyExtraTypeParams struct {
3550
        ChannelPolicyID int64
3551
        Type            int64
3552
        Value           []byte
3553
}
3554

3555
func (q *Queries) UpsertChanPolicyExtraType(ctx context.Context, arg UpsertChanPolicyExtraTypeParams) error {
×
3556
        _, err := q.db.ExecContext(ctx, upsertChanPolicyExtraType, arg.ChannelPolicyID, arg.Type, arg.Value)
×
3557
        return err
×
3558
}
×
3559

3560
const upsertChannelExtraType = `-- name: UpsertChannelExtraType :exec
3561
/* ─────────────────────────────────────────────
3562
   graph_channel_extra_types table queries
3563
   ─────────────────────────────────────────────
3564
*/
3565

3566
INSERT INTO graph_channel_extra_types (
3567
    channel_id, type, value
3568
)
3569
VALUES ($1, $2, $3)
3570
    ON CONFLICT (channel_id, type)
3571
    -- Update the value if a conflict occurs on channel_id and type.
3572
    DO UPDATE SET value = EXCLUDED.value
3573
`
3574

3575
type UpsertChannelExtraTypeParams struct {
3576
        ChannelID int64
3577
        Type      int64
3578
        Value     []byte
3579
}
3580

3581
func (q *Queries) UpsertChannelExtraType(ctx context.Context, arg UpsertChannelExtraTypeParams) error {
×
3582
        _, err := q.db.ExecContext(ctx, upsertChannelExtraType, arg.ChannelID, arg.Type, arg.Value)
×
3583
        return err
×
3584
}
×
3585

3586
const upsertEdgePolicy = `-- name: UpsertEdgePolicy :one
3587
/* ─────────────────────────────────────────────
3588
   graph_channel_policies table queries
3589
   ─────────────────────────────────────────────
3590
*/
3591

3592
INSERT INTO graph_channel_policies (
3593
    version, channel_id, node_id, timelock, fee_ppm,
3594
    base_fee_msat, min_htlc_msat, last_update, disabled,
3595
    max_htlc_msat, inbound_base_fee_msat,
3596
    inbound_fee_rate_milli_msat, message_flags, channel_flags,
3597
    signature
3598
) VALUES  (
3599
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
3600
)
3601
ON CONFLICT (channel_id, node_id, version)
3602
    -- Update the following fields if a conflict occurs on channel_id,
3603
    -- node_id, and version.
3604
    DO UPDATE SET
3605
        timelock = EXCLUDED.timelock,
3606
        fee_ppm = EXCLUDED.fee_ppm,
3607
        base_fee_msat = EXCLUDED.base_fee_msat,
3608
        min_htlc_msat = EXCLUDED.min_htlc_msat,
3609
        last_update = EXCLUDED.last_update,
3610
        disabled = EXCLUDED.disabled,
3611
        max_htlc_msat = EXCLUDED.max_htlc_msat,
3612
        inbound_base_fee_msat = EXCLUDED.inbound_base_fee_msat,
3613
        inbound_fee_rate_milli_msat = EXCLUDED.inbound_fee_rate_milli_msat,
3614
        message_flags = EXCLUDED.message_flags,
3615
        channel_flags = EXCLUDED.channel_flags,
3616
        signature = EXCLUDED.signature
3617
WHERE EXCLUDED.last_update > graph_channel_policies.last_update
3618
RETURNING id
3619
`
3620

3621
type UpsertEdgePolicyParams struct {
3622
        Version                 int16
3623
        ChannelID               int64
3624
        NodeID                  int64
3625
        Timelock                int32
3626
        FeePpm                  int64
3627
        BaseFeeMsat             int64
3628
        MinHtlcMsat             int64
3629
        LastUpdate              sql.NullInt64
3630
        Disabled                sql.NullBool
3631
        MaxHtlcMsat             sql.NullInt64
3632
        InboundBaseFeeMsat      sql.NullInt64
3633
        InboundFeeRateMilliMsat sql.NullInt64
3634
        MessageFlags            sql.NullInt16
3635
        ChannelFlags            sql.NullInt16
3636
        Signature               []byte
3637
}
3638

3639
func (q *Queries) UpsertEdgePolicy(ctx context.Context, arg UpsertEdgePolicyParams) (int64, error) {
×
3640
        row := q.db.QueryRowContext(ctx, upsertEdgePolicy,
×
3641
                arg.Version,
×
3642
                arg.ChannelID,
×
3643
                arg.NodeID,
×
3644
                arg.Timelock,
×
3645
                arg.FeePpm,
×
3646
                arg.BaseFeeMsat,
×
3647
                arg.MinHtlcMsat,
×
3648
                arg.LastUpdate,
×
3649
                arg.Disabled,
×
3650
                arg.MaxHtlcMsat,
×
3651
                arg.InboundBaseFeeMsat,
×
3652
                arg.InboundFeeRateMilliMsat,
×
3653
                arg.MessageFlags,
×
3654
                arg.ChannelFlags,
×
3655
                arg.Signature,
×
3656
        )
×
3657
        var id int64
×
3658
        err := row.Scan(&id)
×
3659
        return id, err
×
3660
}
×
3661

3662
const upsertNode = `-- name: UpsertNode :one
3663
/* ─────────────────────────────────────────────
3664
   graph_nodes table queries
3665
   ───────────────────────────��─────────────────
3666
*/
3667

3668
INSERT INTO graph_nodes (
3669
    version, pub_key, alias, last_update, block_height, color, signature
3670
) VALUES (
3671
    $1, $2, $3, $4, $5, $6, $7
3672
)
3673
ON CONFLICT (pub_key, version)
3674
    -- Update the following fields if a conflict occurs on pub_key
3675
    -- and version.
3676
    DO UPDATE SET
3677
        alias = EXCLUDED.alias,
3678
        last_update = EXCLUDED.last_update,
3679
        block_height = EXCLUDED.block_height,
3680
        color = EXCLUDED.color,
3681
        signature = EXCLUDED.signature
3682
WHERE (graph_nodes.last_update IS NULL
3683
    OR EXCLUDED.last_update > graph_nodes.last_update)
3684
AND (graph_nodes.block_height IS NULL
3685
    OR EXCLUDED.block_height >= graph_nodes.block_height)
3686
RETURNING id
3687
`
3688

3689
type UpsertNodeParams struct {
3690
        Version     int16
3691
        PubKey      []byte
3692
        Alias       sql.NullString
3693
        LastUpdate  sql.NullInt64
3694
        BlockHeight sql.NullInt64
3695
        Color       sql.NullString
3696
        Signature   []byte
3697
}
3698

3699
func (q *Queries) UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error) {
×
3700
        row := q.db.QueryRowContext(ctx, upsertNode,
×
3701
                arg.Version,
×
3702
                arg.PubKey,
×
3703
                arg.Alias,
×
3704
                arg.LastUpdate,
×
NEW
3705
                arg.BlockHeight,
×
3706
                arg.Color,
×
3707
                arg.Signature,
×
3708
        )
×
3709
        var id int64
×
3710
        err := row.Scan(&id)
×
3711
        return id, err
×
3712
}
×
3713

3714
const upsertNodeAddress = `-- name: UpsertNodeAddress :exec
3715
/* ─────────────────────────────────────────────
3716
   graph_node_addresses table queries
3717
   ───────────────────────────────────��─────────
3718
*/
3719

3720
INSERT INTO graph_node_addresses (
3721
    node_id,
3722
    type,
3723
    address,
3724
    position
3725
) VALUES (
3726
    $1, $2, $3, $4
3727
) ON CONFLICT (node_id, type, position)
3728
    DO UPDATE SET address = EXCLUDED.address
3729
`
3730

3731
type UpsertNodeAddressParams struct {
3732
        NodeID   int64
3733
        Type     int16
3734
        Address  string
3735
        Position int32
3736
}
3737

3738
func (q *Queries) UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error {
×
3739
        _, err := q.db.ExecContext(ctx, upsertNodeAddress,
×
3740
                arg.NodeID,
×
3741
                arg.Type,
×
3742
                arg.Address,
×
3743
                arg.Position,
×
3744
        )
×
3745
        return err
×
3746
}
×
3747

3748
const upsertNodeExtraType = `-- name: UpsertNodeExtraType :exec
3749
/* ─────────────────────────────────────────────
3750
   graph_node_extra_types table queries
3751
   ─────────────────────────────────────────────
3752
*/
3753

3754
INSERT INTO graph_node_extra_types (
3755
    node_id, type, value
3756
)
3757
VALUES ($1, $2, $3)
3758
ON CONFLICT (type, node_id)
3759
    -- Update the value if a conflict occurs on type
3760
    -- and node_id.
3761
    DO UPDATE SET value = EXCLUDED.value
3762
`
3763

3764
type UpsertNodeExtraTypeParams struct {
3765
        NodeID int64
3766
        Type   int64
3767
        Value  []byte
3768
}
3769

3770
func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error {
×
3771
        _, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
×
3772
        return err
×
3773
}
×
3774

3775
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
3776
/* ───────────────────────────���─────────────────
3777
    graph_prune_log table queries
3778
    ─────────────────────────────────────────────
3779
*/
3780

3781
INSERT INTO graph_prune_log (
3782
    block_height, block_hash
3783
) VALUES (
3784
    $1, $2
3785
)
3786
ON CONFLICT(block_height) DO UPDATE SET
3787
    block_hash = EXCLUDED.block_hash
3788
`
3789

3790
type UpsertPruneLogEntryParams struct {
3791
        BlockHeight int64
3792
        BlockHash   []byte
3793
}
3794

3795
func (q *Queries) UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error {
×
3796
        _, err := q.db.ExecContext(ctx, upsertPruneLogEntry, arg.BlockHeight, arg.BlockHash)
×
3797
        return err
×
3798
}
×
3799

3800
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
3801
/* ─────────────────────────────────────────────
3802
   graph_zombie_channels table queries
3803
   ─────────────────────────────────────────────
3804
*/
3805

3806
INSERT INTO graph_zombie_channels (scid, version, node_key_1, node_key_2)
3807
VALUES ($1, $2, $3, $4)
3808
ON CONFLICT (scid, version)
3809
DO UPDATE SET
3810
    -- If a conflict exists for the SCID and version pair, then we
3811
    -- update the node keys.
3812
    node_key_1 = COALESCE(EXCLUDED.node_key_1, graph_zombie_channels.node_key_1),
3813
    node_key_2 = COALESCE(EXCLUDED.node_key_2, graph_zombie_channels.node_key_2)
3814
`
3815

3816
type UpsertZombieChannelParams struct {
3817
        Scid     []byte
3818
        Version  int16
3819
        NodeKey1 []byte
3820
        NodeKey2 []byte
3821
}
3822

3823
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
×
3824
        _, err := q.db.ExecContext(ctx, upsertZombieChannel,
×
3825
                arg.Scid,
×
3826
                arg.Version,
×
3827
                arg.NodeKey1,
×
3828
                arg.NodeKey2,
×
3829
        )
×
3830
        return err
×
3831
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc