graph/db+sqldb: implement various zombie index methods

Here we implement the SQLStore methods:
- MarkEdgeZombie
- MarkEdgeLive
- IsZombieEdge
- NumZombies

These will be tested in the next commit as one more method
implementation is required.
This commit is contained in:
Elle Mouton
2025-06-11 17:04:21 +02:00
parent 137fc09230
commit 00b6e0204c
4 changed files with 282 additions and 0 deletions

View File

@@ -26,6 +26,19 @@ func (q *Queries) AddSourceNode(ctx context.Context, nodeID int64) error {
return err
}
const countZombieChannels = `-- name: CountZombieChannels :one
SELECT COUNT(*)
FROM zombie_channels
WHERE version = $1
`
func (q *Queries) CountZombieChannels(ctx context.Context, version int16) (int64, error) {
row := q.db.QueryRowContext(ctx, countZombieChannels, version)
var count int64
err := row.Scan(&count)
return count, err
}
const createChannel = `-- name: CreateChannel :one
/* ─────────────────────────────────────────────
channels table queries
@@ -168,6 +181,21 @@ func (q *Queries) DeleteNodeFeature(ctx context.Context, arg DeleteNodeFeaturePa
return err
}
const deleteZombieChannel = `-- name: DeleteZombieChannel :execresult
DELETE FROM zombie_channels
WHERE scid = $1
AND version = $2
`
type DeleteZombieChannelParams struct {
Scid []byte
Version int16
}
func (q *Queries) DeleteZombieChannel(ctx context.Context, arg DeleteZombieChannelParams) (sql.Result, error) {
return q.db.ExecContext(ctx, deleteZombieChannel, arg.Scid, arg.Version)
}
const getChannelAndNodesBySCID = `-- name: GetChannelAndNodesBySCID :one
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,
@@ -888,6 +916,30 @@ func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([
return items, nil
}
const getZombieChannel = `-- name: GetZombieChannel :one
SELECT scid, version, node_key_1, node_key_2
FROM zombie_channels
WHERE scid = $1
AND version = $2
`
type GetZombieChannelParams struct {
Scid []byte
Version int16
}
func (q *Queries) GetZombieChannel(ctx context.Context, arg GetZombieChannelParams) (ZombieChannel, error) {
row := q.db.QueryRowContext(ctx, getZombieChannel, arg.Scid, arg.Version)
var i ZombieChannel
err := row.Scan(
&i.Scid,
&i.Version,
&i.NodeKey1,
&i.NodeKey2,
)
return i, err
}
const highestSCID = `-- name: HighestSCID :one
SELECT scid
FROM channels
@@ -1538,3 +1590,36 @@ func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTy
_, err := q.db.ExecContext(ctx, upsertNodeExtraType, arg.NodeID, arg.Type, arg.Value)
return err
}
const upsertZombieChannel = `-- name: UpsertZombieChannel :exec
/* ─────────────────────────────────────────────
zombie_channels table queries
─────────────────────────────────────────────
*/
INSERT INTO zombie_channels (scid, version, node_key_1, node_key_2)
VALUES ($1, $2, $3, $4)
ON CONFLICT (scid, version)
DO UPDATE SET
-- If a conflict exists for the SCID and version pair, then we
-- update the node keys.
node_key_1 = COALESCE(EXCLUDED.node_key_1, zombie_channels.node_key_1),
node_key_2 = COALESCE(EXCLUDED.node_key_2, zombie_channels.node_key_2)
`
type UpsertZombieChannelParams struct {
Scid []byte
Version int16
NodeKey1 []byte
NodeKey2 []byte
}
func (q *Queries) UpsertZombieChannel(ctx context.Context, arg UpsertZombieChannelParams) error {
_, err := q.db.ExecContext(ctx, upsertZombieChannel,
arg.Scid,
arg.Version,
arg.NodeKey1,
arg.NodeKey2,
)
return err
}