mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 22:46:40 +01:00
sqldb: add a temporary index to store KV invoice hash to ID mapping
This commit is contained in:
parent
3820497d7f
commit
b7d743929d
7 changed files with 109 additions and 0 deletions
|
@ -56,6 +56,11 @@ var (
|
||||||
Version: 5,
|
Version: 5,
|
||||||
SchemaVersion: 5,
|
SchemaVersion: 5,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "000006_invoice_migration",
|
||||||
|
Version: 6,
|
||||||
|
SchemaVersion: 6,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const clearKVInvoiceHashIndex = `-- name: ClearKVInvoiceHashIndex :exec
|
||||||
|
DELETE FROM invoice_payment_hashes
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ClearKVInvoiceHashIndex(ctx context.Context) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, clearKVInvoiceHashIndex)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const deleteCanceledInvoices = `-- name: DeleteCanceledInvoices :execresult
|
const deleteCanceledInvoices = `-- name: DeleteCanceledInvoices :execresult
|
||||||
DELETE
|
DELETE
|
||||||
FROM invoices
|
FROM invoices
|
||||||
|
@ -405,6 +414,19 @@ func (q *Queries) GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]Invoi
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getKVInvoicePaymentHashByAddIndex = `-- name: GetKVInvoicePaymentHashByAddIndex :one
|
||||||
|
SELECT hash
|
||||||
|
FROM invoice_payment_hashes
|
||||||
|
WHERE add_index = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getKVInvoicePaymentHashByAddIndex, addIndex)
|
||||||
|
var hash []byte
|
||||||
|
err := row.Scan(&hash)
|
||||||
|
return hash, err
|
||||||
|
}
|
||||||
|
|
||||||
const insertInvoice = `-- name: InsertInvoice :one
|
const insertInvoice = `-- name: InsertInvoice :one
|
||||||
INSERT INTO invoices (
|
INSERT INTO invoices (
|
||||||
hash, preimage, memo, amount_msat, cltv_delta, expiry, payment_addr,
|
hash, preimage, memo, amount_msat, cltv_delta, expiry, payment_addr,
|
||||||
|
@ -533,6 +555,24 @@ func (q *Queries) InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertI
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const insertKVInvoiceKeyAndAddIndex = `-- name: InsertKVInvoiceKeyAndAddIndex :exec
|
||||||
|
INSERT INTO invoice_payment_hashes (
|
||||||
|
id, add_index
|
||||||
|
) VALUES (
|
||||||
|
$1, $2
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertKVInvoiceKeyAndAddIndexParams struct {
|
||||||
|
ID int32
|
||||||
|
AddIndex int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, insertKVInvoiceKeyAndAddIndex, arg.ID, arg.AddIndex)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const insertMigratedInvoice = `-- name: InsertMigratedInvoice :one
|
const insertMigratedInvoice = `-- name: InsertMigratedInvoice :one
|
||||||
INSERT INTO invoices (
|
INSERT INTO invoices (
|
||||||
hash, preimage, settle_index, settled_at, memo, amount_msat, cltv_delta,
|
hash, preimage, settle_index, settled_at, memo, amount_msat, cltv_delta,
|
||||||
|
@ -601,6 +641,22 @@ func (q *Queries) NextInvoiceSettleIndex(ctx context.Context) (int64, error) {
|
||||||
return current_value, err
|
return current_value, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setKVInvoicePaymentHash = `-- name: SetKVInvoicePaymentHash :exec
|
||||||
|
UPDATE invoice_payment_hashes
|
||||||
|
SET hash = $2
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type SetKVInvoicePaymentHashParams struct {
|
||||||
|
ID int32
|
||||||
|
Hash []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) SetKVInvoicePaymentHash(ctx context.Context, arg SetKVInvoicePaymentHashParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, setKVInvoicePaymentHash, arg.ID, arg.Hash)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const updateInvoiceAmountPaid = `-- name: UpdateInvoiceAmountPaid :execresult
|
const updateInvoiceAmountPaid = `-- name: UpdateInvoiceAmountPaid :execresult
|
||||||
UPDATE invoices
|
UPDATE invoices
|
||||||
SET amount_paid_msat = $2
|
SET amount_paid_msat = $2
|
||||||
|
|
1
sqldb/sqlc/migrations/000006_invoice_migration.down.sql
Normal file
1
sqldb/sqlc/migrations/000006_invoice_migration.down.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DROP TABLE IF EXISTS invoice_payment_hashes;
|
17
sqldb/sqlc/migrations/000006_invoice_migration.up.sql
Normal file
17
sqldb/sqlc/migrations/000006_invoice_migration.up.sql
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
-- invoice_payment_hashes table contains the hash of the invoices. This table
|
||||||
|
-- is used during KV to SQL invoice migration as in our KV representation we
|
||||||
|
-- don't have a mapping from hash to add index.
|
||||||
|
CREATE TABLE IF NOT EXISTS invoice_payment_hashes (
|
||||||
|
-- id represents is the key of the invoice in the KV store.
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
|
||||||
|
-- add_index is the KV add index of the invoice.
|
||||||
|
add_index BIGINT NOT NULL,
|
||||||
|
|
||||||
|
-- hash is the payment hash for this invoice.
|
||||||
|
hash BLOB
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create an indexes on the add_index and hash columns to speed up lookups.
|
||||||
|
CREATE INDEX IF NOT EXISTS invoice_payment_hashes_add_index_idx ON invoice_payment_hashes(add_index);
|
||||||
|
CREATE INDEX IF NOT EXISTS invoice_payment_hashes_hash_idx ON invoice_payment_hashes(hash);
|
|
@ -87,6 +87,12 @@ type InvoiceHtlcCustomRecord struct {
|
||||||
HtlcID int64
|
HtlcID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type InvoicePaymentHash struct {
|
||||||
|
ID int32
|
||||||
|
AddIndex int64
|
||||||
|
Hash []byte
|
||||||
|
}
|
||||||
|
|
||||||
type InvoiceSequence struct {
|
type InvoiceSequence struct {
|
||||||
Name string
|
Name string
|
||||||
CurrentValue int64
|
CurrentValue int64
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Querier interface {
|
type Querier interface {
|
||||||
|
ClearKVInvoiceHashIndex(ctx context.Context) error
|
||||||
DeleteCanceledInvoices(ctx context.Context) (sql.Result, error)
|
DeleteCanceledInvoices(ctx context.Context) (sql.Result, error)
|
||||||
DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error)
|
DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error)
|
||||||
FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error)
|
FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error)
|
||||||
|
@ -27,6 +28,7 @@ type Querier interface {
|
||||||
GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error)
|
GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error)
|
||||||
GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error)
|
GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error)
|
||||||
GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error)
|
GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error)
|
||||||
|
GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error)
|
||||||
GetMigration(ctx context.Context, version int32) (time.Time, error)
|
GetMigration(ctx context.Context, version int32) (time.Time, error)
|
||||||
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
|
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
|
||||||
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
|
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
|
||||||
|
@ -34,6 +36,7 @@ type Querier interface {
|
||||||
InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error
|
InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error
|
||||||
InsertInvoiceHTLC(ctx context.Context, arg InsertInvoiceHTLCParams) (int64, error)
|
InsertInvoiceHTLC(ctx context.Context, arg InsertInvoiceHTLCParams) (int64, error)
|
||||||
InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertInvoiceHTLCCustomRecordParams) error
|
InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertInvoiceHTLCCustomRecordParams) error
|
||||||
|
InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error
|
||||||
InsertMigratedInvoice(ctx context.Context, arg InsertMigratedInvoiceParams) (int64, error)
|
InsertMigratedInvoice(ctx context.Context, arg InsertMigratedInvoiceParams) (int64, error)
|
||||||
NextInvoiceSettleIndex(ctx context.Context) (int64, error)
|
NextInvoiceSettleIndex(ctx context.Context) (int64, error)
|
||||||
OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error
|
OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error
|
||||||
|
@ -42,6 +45,7 @@ type Querier interface {
|
||||||
OnInvoiceCanceled(ctx context.Context, arg OnInvoiceCanceledParams) error
|
OnInvoiceCanceled(ctx context.Context, arg OnInvoiceCanceledParams) error
|
||||||
OnInvoiceCreated(ctx context.Context, arg OnInvoiceCreatedParams) error
|
OnInvoiceCreated(ctx context.Context, arg OnInvoiceCreatedParams) error
|
||||||
OnInvoiceSettled(ctx context.Context, arg OnInvoiceSettledParams) error
|
OnInvoiceSettled(ctx context.Context, arg OnInvoiceSettledParams) error
|
||||||
|
SetKVInvoicePaymentHash(ctx context.Context, arg SetKVInvoicePaymentHashParams) error
|
||||||
SetMigration(ctx context.Context, arg SetMigrationParams) error
|
SetMigration(ctx context.Context, arg SetMigrationParams) error
|
||||||
UpdateAMPSubInvoiceHTLCPreimage(ctx context.Context, arg UpdateAMPSubInvoiceHTLCPreimageParams) (sql.Result, error)
|
UpdateAMPSubInvoiceHTLCPreimage(ctx context.Context, arg UpdateAMPSubInvoiceHTLCPreimageParams) (sql.Result, error)
|
||||||
UpdateAMPSubInvoiceState(ctx context.Context, arg UpdateAMPSubInvoiceStateParams) error
|
UpdateAMPSubInvoiceState(ctx context.Context, arg UpdateAMPSubInvoiceStateParams) error
|
||||||
|
|
|
@ -179,3 +179,23 @@ INSERT INTO invoice_htlc_custom_records (
|
||||||
SELECT ihcr.htlc_id, key, value
|
SELECT ihcr.htlc_id, key, value
|
||||||
FROM invoice_htlcs ih JOIN invoice_htlc_custom_records ihcr ON ih.id=ihcr.htlc_id
|
FROM invoice_htlcs ih JOIN invoice_htlc_custom_records ihcr ON ih.id=ihcr.htlc_id
|
||||||
WHERE ih.invoice_id = $1;
|
WHERE ih.invoice_id = $1;
|
||||||
|
|
||||||
|
-- name: InsertKVInvoiceKeyAndAddIndex :exec
|
||||||
|
INSERT INTO invoice_payment_hashes (
|
||||||
|
id, add_index
|
||||||
|
) VALUES (
|
||||||
|
$1, $2
|
||||||
|
);
|
||||||
|
|
||||||
|
-- name: SetKVInvoicePaymentHash :exec
|
||||||
|
UPDATE invoice_payment_hashes
|
||||||
|
SET hash = $2
|
||||||
|
WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: GetKVInvoicePaymentHashByAddIndex :one
|
||||||
|
SELECT hash
|
||||||
|
FROM invoice_payment_hashes
|
||||||
|
WHERE add_index = $1;
|
||||||
|
|
||||||
|
-- name: ClearKVInvoiceHashIndex :exec
|
||||||
|
DELETE FROM invoice_payment_hashes;
|
||||||
|
|
Loading…
Add table
Reference in a new issue