invoices: migrate legacy AMP invoice HTLCs - #11106
Conversation
838b4f3 to
86510e7
Compare
🟠 PR Severity: HIGH
🟠 High (1 file)
🟢 Low (3 files)
AnalysisThe substantive change is in To override, add a |
Legacy AMP invoice migration: before and afterLegacy AMP invoices store their HTLCs inline. Each HTLC contains its AMP set flowchart TB
legacy["Legacy KV AMP invoice<br/><br/>IsAMP = true<br/>AMPState = empty"]
htlcs["Inline HTLCs<br/><br/>Set ID<br/>AMP metadata<br/>Custom records"]
legacy --> htlcs
htlcs --> oldMigration
htlcs --> normalize
subgraph before["Before the fix"]
direction TB
oldMigration["Original SQL migration"]
genericRows["Insert invoice and generic HTLC rows<br/>inside the migration transaction"]
emptyState["Iterate AMPState<br/>0 entries"]
missingSubInvoice["No AMP sub-invoice"]
missingAssociation["No AMP-to-HTLC association"]
oldReader["SQL AMP reader follows<br/>AMP associations"]
emptyResult["Read-back result<br/><br/>Htlcs = empty<br/>AMPState = empty"]
mismatch["Strict comparison fails"]
rollback["Entire migration transaction rolls back"]
oldMigration --> genericRows
oldMigration --> emptyState
emptyState --> missingSubInvoice
missingSubInvoice --> missingAssociation
genericRows --> oldReader
missingAssociation --> oldReader
oldReader --> emptyResult
emptyResult --> mismatch
mismatch --> rollback
end
subgraph after["With the fix"]
direction TB
normalize["Normalize the legacy representation<br/>in memory"]
group["Group existing HTLCs by<br/>their embedded AMP set ID"]
ampState["Reconstruct AMPState<br/><br/>InvoiceKeys<br/>State and AmtPaid<br/>Settle index and date"]
existingWriter["Existing SQL migration writer"]
invoiceHtlcs["invoice_htlcs<br/><br/>Original HTLC data<br/>and custom records"]
subInvoices["amp_sub_invoices<br/><br/>One row per AMP set"]
associations["amp_sub_invoice_htlcs<br/><br/>AMP-to-HTLC associations<br/>and AMP metadata"]
newReader["SQL AMP reader"]
restored["Read-back result<br/><br/>Htlcs restored<br/>AMPState reconstructed"]
match["Strict comparison passes"]
commit["Migration transaction commits"]
normalize --> group
group --> ampState
ampState --> existingWriter
existingWriter --> invoiceHtlcs
existingWriter --> subInvoices
existingWriter --> associations
invoiceHtlcs --> newReader
subInvoices --> newReader
associations --> newReader
newReader --> restored
restored --> match
match --> commit
end
The HTLC data itself was already available in the legacy record. The missing The fix reconstructs only that derivable |
1752cbe to
15f1063
Compare
Legacy AMP settlement representation in SQLThe migrated legacy invoice intentionally differs from a reusable AMP invoice:
Before reusable AMP invoices existed, settling one AMP set made the parent Migration reconstructs the missing AMP sub-invoice rows because SQL needs them |
15f1063 to
2f167f7
Compare
56a158e to
5dbba3f
Compare
5dbba3f to
f4f67e2
Compare
AMP invoices created before reusable invoices store their HTLCs inline and have no AMPState metadata. Reconstruct the sub-invoice state from each HTLC set ID before inserting it into SQL. This preserves the AMP associations during read-back verification. Add regression coverage for settled and canceled legacy sets, AMP metadata, and custom records.
f4f67e2 to
b7a2f15
Compare
|
Created backport PR for
Please cherry-pick the changes locally and resolve any conflicts. git fetch origin backport-11106-to-v0.20.x-branch
git worktree add --checkout .worktree/backport-11106-to-v0.20.x-branch backport-11106-to-v0.20.x-branch
cd .worktree/backport-11106-to-v0.20.x-branch
git reset --hard HEAD^
git cherry-pick -x cb9c86195251422dc217c35b915e54a55d8f8580 b7a2f153962c34b3920533da0ae70f05e01d7a4f
git push --force-with-lease |
|
Created backport PR for
Please cherry-pick the changes locally and resolve any conflicts. git fetch origin backport-11106-to-v0.21.x-branch
git worktree add --checkout .worktree/backport-11106-to-v0.21.x-branch backport-11106-to-v0.21.x-branch
cd .worktree/backport-11106-to-v0.21.x-branch
git reset --hard HEAD^
git cherry-pick -x b7a2f153962c34b3920533da0ae70f05e01d7a4f
git push --force-with-lease |
Change Description
Legacy AMP invoices created before reusable AMP invoices store their HTLCs
inline and don't contain
AMPStatemetadata.The KV-to-SQL migration inserted the generic HTLC rows for these invoices, but
without
AMPStateit did not create AMP sub-invoices or associate the HTLCswith them. SQL read-back therefore returned an empty HTLC map, and strict
migration verification aborted startup.
Reconstruct the modern AMP sub-invoice state from each inline HTLC's set ID
before insertion. This preserves the HTLC associations, AMP metadata, custom
records, amounts, and settlement metadata while keeping the compatibility
handling isolated to the migration.
The migration fails closed if the legacy data is structurally inconsistent.
Release notes are included for v0.21.3 and v0.20.4.
Steps to Test
The regression test migrates a settled legacy AMP invoice with a canceled
competing set, AMP metadata, a large child index, and custom records, then
verifies complete SQL read-back equality.