Skip to content

Commit

Permalink
sqldb+invoices: add migration to fix incorrectly stored invoice expiries
Browse files Browse the repository at this point in the history
Previously, when using the native schema, invoice expiries were incorrectly
stored as 64-bit values (expiry in nanoseconds instead of seconds), causing
overflow issues. Since we cannot determine the original values, we will set
the expiries for existing invoices to 1 hour with this migration.
  • Loading branch information
bhandras committed Jul 2, 2024
1 parent 097cea1 commit 24fb330
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion invoices/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) {
i := &invpkg.Invoice{
CreationDate: testNow,
Terms: invpkg.ContractTerm{
Expiry: 4000,
Expiry: time.Duration(4000) * time.Second,
PaymentPreimage: &pre,
PaymentAddr: payAddr,
Value: value,
Expand Down
6 changes: 4 additions & 2 deletions invoices/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (i *SQLStore) AddInvoice(ctx context.Context,
CltvDelta: sqldb.SQLInt32(
newInvoice.Terms.FinalCltvDelta,
),
Expiry: int32(newInvoice.Terms.Expiry),
Expiry: int32(newInvoice.Terms.Expiry.Seconds()),
// Note: keysend invoices don't have a payment request.
PaymentRequest: sqldb.SQLStr(string(
newInvoice.PaymentRequest),
Expand Down Expand Up @@ -1598,6 +1598,8 @@ func unmarshalInvoice(row sqlc.Invoice) (*lntypes.Hash, *Invoice,
cltvDelta = row.CltvDelta.Int32
}

expiry := time.Duration(row.Expiry) * time.Second

invoice := &Invoice{
SettleIndex: uint64(settleIndex),
SettleDate: settledAt,
Expand All @@ -1606,7 +1608,7 @@ func unmarshalInvoice(row sqlc.Invoice) (*lntypes.Hash, *Invoice,
CreationDate: row.CreatedAt.Local(),
Terms: ContractTerm{
FinalCltvDelta: cltvDelta,
Expiry: time.Duration(row.Expiry),
Expiry: expiry,
PaymentPreimage: preimage,
Value: lnwire.MilliSatoshi(row.AmountMsat),
PaymentAddr: paymentAddr,
Expand Down
2 changes: 2 additions & 0 deletions sqldb/sqlc/migrations/000004_invoice_expiry_fix.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Given that all expiries are changed in this migration we won't be able to
-- roll back to the previous values.
15 changes: 15 additions & 0 deletions sqldb/sqlc/migrations/000004_invoice_expiry_fix.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Update the expiry for all records in the invoices table. This is needed as
-- previously we stored raw time.Duration values which are 64 bit integers and
-- are used to express duration in nanoseconds however the intent is to store
-- invoice expiry in seconds.

-- Update the expiry to 86400 seconds (24 hours) for non-AMP invoices, including
-- NULL values
UPDATE invoices
SET expiry = 86400
WHERE is_amp = FALSE OR is_amp IS NULL;

-- Update the expiry to 2592000 seconds (30 days) for AMP invoices
UPDATE invoices
SET expiry = 2592000
WHERE is_amp = TRUE;

0 comments on commit 24fb330

Please sign in to comment.