diff --git a/sqldb/sqlc/blinded_paths.sql.go b/sqldb/sqlc/blinded_paths.sql.go new file mode 100644 index 00000000000..c0e6450242d --- /dev/null +++ b/sqldb/sqlc/blinded_paths.sql.go @@ -0,0 +1,140 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: blinded_paths.sql + +package sqlc + +import ( + "context" +) + +const fetchBlindedPathHops = `-- name: FetchBlindedPathHops :many +SELECT blinded_path_id, hop_index, channel_id, node_pub_key, amount_to_fwd +FROM blinded_path_hops +WHERE blinded_path_id = $1 +ORDER BY hop_index +` + +func (q *Queries) FetchBlindedPathHops(ctx context.Context, blindedPathID int64) ([]BlindedPathHop, error) { + rows, err := q.db.QueryContext(ctx, fetchBlindedPathHops, blindedPathID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []BlindedPathHop + for rows.Next() { + var i BlindedPathHop + if err := rows.Scan( + &i.BlindedPathID, + &i.HopIndex, + &i.ChannelID, + &i.NodePubKey, + &i.AmountToFwd, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const fetchBlindedPaths = `-- name: FetchBlindedPaths :many +SELECT id, invoice_id, last_ephemeral_pub, session_key, introduction_node, amount_msat +FROM blinded_paths +WHERE invoice_id = $1 +` + +func (q *Queries) FetchBlindedPaths(ctx context.Context, invoiceID int64) ([]BlindedPath, error) { + rows, err := q.db.QueryContext(ctx, fetchBlindedPaths, invoiceID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []BlindedPath + for rows.Next() { + var i BlindedPath + if err := rows.Scan( + &i.ID, + &i.InvoiceID, + &i.LastEphemeralPub, + &i.SessionKey, + &i.IntroductionNode, + &i.AmountMsat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const insertBlindedPath = `-- name: InsertBlindedPath :one +INSERT INTO blinded_paths ( + invoice_id, last_ephemeral_pub, session_key, introduction_node, + amount_msat +) VALUES ( + $1, $2, $3, $4, $5 +) RETURNING id +` + +type InsertBlindedPathParams struct { + InvoiceID int64 + LastEphemeralPub []byte + SessionKey []byte + IntroductionNode []byte + AmountMsat int64 +} + +func (q *Queries) InsertBlindedPath(ctx context.Context, arg InsertBlindedPathParams) (int64, error) { + row := q.db.QueryRowContext(ctx, insertBlindedPath, + arg.InvoiceID, + arg.LastEphemeralPub, + arg.SessionKey, + arg.IntroductionNode, + arg.AmountMsat, + ) + var id int64 + err := row.Scan(&id) + return id, err +} + +const insertBlindedPathHop = `-- name: InsertBlindedPathHop :exec +INSERT INTO blinded_path_hops ( + blinded_path_id, hop_index, channel_id, node_pub_key, + amount_to_fwd +) VALUES ( + $1, $2, $3, $4, $5 +) +` + +type InsertBlindedPathHopParams struct { + BlindedPathID int64 + HopIndex int64 + ChannelID int64 + NodePubKey []byte + AmountToFwd int64 +} + +func (q *Queries) InsertBlindedPathHop(ctx context.Context, arg InsertBlindedPathHopParams) error { + _, err := q.db.ExecContext(ctx, insertBlindedPathHop, + arg.BlindedPathID, + arg.HopIndex, + arg.ChannelID, + arg.NodePubKey, + arg.AmountToFwd, + ) + return err +} diff --git a/sqldb/sqlc/migrations/000005_invoice_blinded_paths.down.sql b/sqldb/sqlc/migrations/000005_invoice_blinded_paths.down.sql new file mode 100644 index 00000000000..c539f6f275d --- /dev/null +++ b/sqldb/sqlc/migrations/000005_invoice_blinded_paths.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS blinded_paths; +DROP TABLE IF EXISTS blinded_path_hops; diff --git a/sqldb/sqlc/migrations/000005_invoice_blinded_paths.up.sql b/sqldb/sqlc/migrations/000005_invoice_blinded_paths.up.sql new file mode 100644 index 00000000000..00344dada8f --- /dev/null +++ b/sqldb/sqlc/migrations/000005_invoice_blinded_paths.up.sql @@ -0,0 +1,50 @@ +-- blinded_paths contains information about blinded paths included in the +-- associated invoice. +CREATE TABLE IF NOT EXISTS blinded_paths( + -- The id of the blinded path + id BIGINT PRIMARY KEY, + + -- invoice_id is the reference to the invoice this blinded_path was created + -- for. + invoice_id BIGINT NOT NULL REFERENCES invoices(id) ON DELETE CASCADE, + + -- last_ephemeral_pub is the public key of the last ephemeral blinding + -- point of this path. + last_ephemeral_pub BYTEA NOT NULL UNIQUE, + + -- session_key is the private key used for the first ephemeral blinding + -- key of this path. + session_key BYTEA NOT NULL, + + -- introduction_node is the public key of the first hop of the path. + introduction_node BYTEA NOT NULL, + + -- amount_msat is the total amount in millisatoshis expected to be + -- forwarded along this path. + amount_msat BIGINT NOT NULL +); + +-- blinded_paths_hops holds information about a specific hop of a blinded path in +-- blinded_paths. +CREATE TABLE IF NOT EXISTS blinded_path_hops( + -- blinded_path_id is the reference to the blinded_path_id this + -- blinded_path_hop is part of. + blinded_path_id BIGINT NOT NULL REFERENCES blinded_paths(id) ON DELETE CASCADE, + + -- hop_index is the index of this hop along the associated blinded path. + hop_index BIGINT NOT NULL, + + -- channel_id is the ID of the channel that connects this hop to the previous one. + channel_id BIGINT NOT NULL, + + -- node_pub_key is the public key of the node of this hop + node_pub_key BYTEA NOT NULL, + + -- amount_to_fwd is the amount that this hop was instructed to forward. + amount_to_fwd BIGINT NOT NULL, + + -- The hop_index is unique per path. + UNIQUE (blinded_path_id, hop_index) +); + +CREATE INDEX IF NOT EXISTS blinded_path_hops_path_id_idx ON blinded_path_hops(blinded_path_id); diff --git a/sqldb/sqlc/models.go b/sqldb/sqlc/models.go index 83be5a708fa..e2dabd4163a 100644 --- a/sqldb/sqlc/models.go +++ b/sqldb/sqlc/models.go @@ -28,6 +28,23 @@ type AmpSubInvoiceHtlc struct { Preimage []byte } +type BlindedPath struct { + ID int64 + InvoiceID int64 + LastEphemeralPub []byte + SessionKey []byte + IntroductionNode []byte + AmountMsat int64 +} + +type BlindedPathHop struct { + BlindedPathID int64 + HopIndex int64 + ChannelID int64 + NodePubKey []byte + AmountToFwd int64 +} + type Invoice struct { ID int64 Hash []byte diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 04b61c7007f..ef17ae1bdf8 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -14,6 +14,8 @@ type Querier interface { DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error) FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error) FetchAMPSubInvoices(ctx context.Context, arg FetchAMPSubInvoicesParams) ([]AmpSubInvoice, error) + FetchBlindedPathHops(ctx context.Context, blindedPathID int64) ([]BlindedPathHop, error) + FetchBlindedPaths(ctx context.Context, invoiceID int64) ([]BlindedPath, error) FetchSettledAMPSubInvoices(ctx context.Context, arg FetchSettledAMPSubInvoicesParams) ([]FetchSettledAMPSubInvoicesRow, error) FilterInvoices(ctx context.Context, arg FilterInvoicesParams) ([]Invoice, error) GetAMPInvoiceID(ctx context.Context, setID []byte) (int64, error) @@ -26,6 +28,8 @@ type Querier interface { GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error) GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error) InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error + InsertBlindedPath(ctx context.Context, arg InsertBlindedPathParams) (int64, error) + InsertBlindedPathHop(ctx context.Context, arg InsertBlindedPathHopParams) error InsertInvoice(ctx context.Context, arg InsertInvoiceParams) (int64, error) InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error InsertInvoiceHTLC(ctx context.Context, arg InsertInvoiceHTLCParams) (int64, error) diff --git a/sqldb/sqlc/queries/blinded_paths.sql b/sqldb/sqlc/queries/blinded_paths.sql new file mode 100644 index 00000000000..cb9ae8afb6c --- /dev/null +++ b/sqldb/sqlc/queries/blinded_paths.sql @@ -0,0 +1,26 @@ +-- name: InsertBlindedPath :one +INSERT INTO blinded_paths ( + invoice_id, last_ephemeral_pub, session_key, introduction_node, + amount_msat +) VALUES ( + $1, $2, $3, $4, $5 +) RETURNING id; + +-- name: FetchBlindedPaths :many +SELECT * +FROM blinded_paths +WHERE invoice_id = $1; + +-- name: InsertBlindedPathHop :exec +INSERT INTO blinded_path_hops ( + blinded_path_id, hop_index, channel_id, node_pub_key, + amount_to_fwd +) VALUES ( + $1, $2, $3, $4, $5 +); + +-- name: FetchBlindedPathHops :many +SELECT * +FROM blinded_path_hops +WHERE blinded_path_id = $1 +ORDER BY hop_index;