Commit Graph
20326 Commits
Author SHA1 Message Date
ziggie f174b60b94 payments/migration1: wire KV→SQL migration in the main pkg
Hook the payments KV→SQL migration into the SQL migration config.
The migration is still only available when building with the build tag
"test_native_sql".

Moreover a tombstone protection similar to the invoice migration is added
to prevent re-runningi with the KV backend  once migration completes.
2026-02-25 18:52:33 +01:00
ziggie 4b30bed0ac payments/migration1: add external migration test
Add a developer-facing migration_external_test that allows
running the KV→SQL payments migration against a real channel.db
backend to debug migration failures on actual data. The accompanying
testdata README documents how to supply a database file and configure
the test, so users can validate their data and confirm the migration
completes successfully.

The test is skipped by default and meant for manual diagnostics.
2026-02-25 18:52:33 +01:00
ziggie f058f3329d payments/migration1: add migration test suite and helpers
Add test helpers plus sql_migration_test coverage for KV→SQL migration.
Basic migration, sequence ordering, data integrity, and feature-specific cases
(MPP/AMP, custom records, blinded routes, metadata, failure messages). Also
cover duplicate payment migration to payment_duplicates, including missing
attempt info to ensure terminal failure is recorded.

This gives broad regression coverage for the migration path and its edge-cases.
2026-02-25 18:52:33 +01:00
ziggie a9747207f6 payments/migration1: add the payments mig code
Implement the KV→SQL payment migration and add an in-migration
validation pass that deep-compares KV and SQL payment data in batches.
Duplicate payments are migrated into the payment_duplicates table,
and duplicates without attempt info or explicit resolution are marked
failed to ensure terminal state. Validation checks those rows as well.
2026-02-25 18:52:32 +01:00
ziggie fb705bb0f9 payments/migration1: freeze core payment code
Copy the core payments/db code into payments/db/migration1 and
add the required sqlc-generated types/queries from sqldb/sqlc.
This effectively freezes the migration code so it stays robust
against future query or schema changes in the main payments package.

Replace the delegation to channeldb.ReadElement/WriteElement with
self-contained, frozen implementations that only handle the exact types
required by this migration package. This removes the dependency on the
live channeldb codec so that future changes to channeldb serialization
cannot silently corrupt or break the migration.

UnknownElementType is also defined locally for the same reason.
2026-02-25 18:52:32 +01:00
ziggie 74f8f2d9e6 sqldb+payments: add payment_duplicates for legacy duplicate payments
Older LND versions could create multiple payments for the same hash.
We need to preserve those historical records during KV→SQL migration,
but they don’t fit the normal payment schema because we enforce a
unique payment hash constraint. Introduce a lean payment_duplicates
table to store only the essential fields (identifier, amount,
timestamps, settle/fail data).

This keeps the primary payment records stable and makes the migration
deterministic even when duplicate records lack attempt info. The table
is intentionally minimal and can be dropped after migration if no
duplicate payments exist.

For now there is no logic in place which allows the noderunner to
fetch duplicate payments after the migration.
2026-02-25 18:52:32 +01:00
ziggie 2596d34619 paymentsdb+sqldb: add migration related query
Add a migration specific query which allows to set the failure
reason when inserting a payment into the db.
2026-02-25 18:52:31 +01:00
ziggie e9a88267ff paymentsdb: fix duplicate interface check and down migration drop order
- Remove duplicate compile-time interface assertion for SQLStore.

- Fix the down migration to drop payment_intents before payments to
respect the foreign key dependency order. This was not a bug in the
first place bc we have the CASCADE when deleting payments.
2026-02-25 18:36:14 +01:00
ziggie 216de55dff paymentsdb: sort FetchInFlightPayments result by sequence number
The SQL implementation collects payments into a map before converting
to a slice, resulting in non-deterministic iteration order due to Go's
intentional map randomisation. Sort the result by SequenceNum to produce
a deterministic, insertion-ordered output.

Note that the current sole caller (resumePayments in router.go) processes
each payment independently, so this ordering does not affect any existing
behaviour.
2026-02-25 18:36:14 +01:00
ziggie 0c2951aa05 paymentsdb: fix SettleAttempt and FailAttempt to use caller-provided timestamps
The SQL backend introduced in this PR was ignoring the SettleTime and
FailTime fields provided in HTLCSettleInfo and HTLCFailInfo, instead
always recording time.Now() as the resolution timestamp. The KV backend
correctly serializes and deserializes these fields.

The timestamps are set by the caller using a mockable clock
(p.router.cfg.Clock.Now() in payment_lifecycle.go), so ignoring them
means the stored timestamp reflects when the DB write happened rather
than when the event occurred, breaking deterministic testing.

This commit also extends the test assertions in assertPaymentInfo to
verify that SettleTime and FailTime are correctly stored and retrieved
by the SQL backend, and updates the relevant call sites to pass explicit
timestamps so regressions are caught.
2026-02-25 18:36:14 +01:00
ziggie a2cb753428 sqldb: rename 000009_payments to 000010_payments
The 000009 schema version slot is now taken by 000009_graph_v2_columns
which was merged ahead of the payments schema. Bump the payments schema
file number to 000010 to avoid the collision.

Moreover update the migration_dev.go file to reflect this change and
update the order of migration.
2026-02-25 08:02:27 +01:00
ziggie 2548c15167 docs: add release-notes 2026-02-25 08:02:27 +01:00
ziggie 372fbbd70b paymentsdb: add more unit tests to increase coverage
We add a couple of additional tests to increase the unit test
coverage of the sql store but also the kv store. We only create
db agnostic unit tests so both backends are tested effectively.
2026-02-25 08:02:26 +01:00
ziggie a0a22b7ff9 paymentsdb: add additional test for first hop data
We add a test which tests the retrieval of first hop data like
the first hop amount or the custom records on the route level.
2026-02-25 08:02:26 +01:00
ziggie a861ac8d46 paymentsdb: refactor test helpers 2026-02-25 08:02:26 +01:00
ziggie 995ad7e1b5 multi: move failed attempt cfg option to the router subsytem
Previously we had db and application logic mixed on the db level.
We now move the config option KeepFailedPaymentAttempts to the
ChannelRouter level and move it out of the db level.
2026-02-25 08:02:26 +01:00
ziggie 253e4fd832 routing: add TODO to also delete payments without HTLCs 2026-02-25 08:02:26 +01:00
ziggie 3ae0ccc6f5 paymentsdb: make delete payments test db agnostic
We make the TestDeleteNonInFlight and separate all the logic out
for the duplicate payment test case. The deletion of duplicate
payments is now tested in isolation only for the kv backend.
2026-02-25 08:02:26 +01:00
ziggie 9da39e5655 docs: add release-notes 2026-02-25 08:02:25 +01:00
ziggie d8d5d3f990 multi: thread context through SendPayment 2026-02-25 08:02:25 +01:00
ziggie 4cc5428087 routing: add context to reloadPayment method 2026-02-25 08:02:25 +01:00
ziggie 03d891d52d routing: add context to reloadInflightAttempts 2026-02-25 08:02:25 +01:00
ziggie 5b06854850 routing: add context to failAttempt 2026-02-25 08:02:25 +01:00
ziggie e130ccc494 routing: Thread context through failPaymentAndAttempt
A context is added to failPaymentAndAttempt and its dependant
function calls.
2026-02-25 08:02:25 +01:00
ziggie 8038dc0054 multi: thread context through payment lifecyle functions 2026-02-25 08:02:25 +01:00
ziggie dfe0c43c7d routing: Add context to requestRoute 2026-02-25 08:02:24 +01:00
ziggie 33597ad35d docs: add release notes 2026-02-25 08:02:24 +01:00
ziggie 7f2df4b088 multi: thread context through DeleteFailedAttempts 2026-02-25 08:02:24 +01:00
ziggie 635a67c94a multi: thread context through Fail payment functions 2026-02-25 08:02:24 +01:00
ziggie 446300984e multi: thread context through FailAttempt 2026-02-25 08:02:24 +01:00
ziggie 3b7fdabaec multi: thread context through SettleAttempt 2026-02-25 08:02:24 +01:00
ziggie 8bb7150dc2 multi: thread context through RegisterAttempt method 2026-02-25 08:02:24 +01:00
ziggie 60026494b1 multi: thread context through InitPayment 2026-02-25 08:02:23 +01:00
ziggie 10062ca9ac multi: thread context through FetchInflightPayments 2026-02-25 08:02:23 +01:00
ziggie 25d05be038 multi: thread context through FetchPayment 2026-02-25 08:02:19 +01:00
ziggie 563a2ac9d3 multi: thread context through DeletePayments 2026-02-25 08:02:19 +01:00
ziggie bc15d4bb50 multi: thread context through DeletePayment 2026-02-25 08:02:19 +01:00
ziggie fcb8929ea5 docs: add release-notes 2026-02-25 08:02:19 +01:00
ziggie dfacb9ec00 paymentsdb: add unit test for FetchInflightPayments method 2026-02-25 08:02:18 +01:00
ziggie e40c8df16b paymentsdb: add firstcustom records to unit tests 2026-02-25 08:02:18 +01:00
ziggie bfd59aac22 paymentsdb: add more comments 2026-02-25 08:02:18 +01:00
ziggie ea4e183c39 lnrpc: fix linter 2026-02-25 08:02:18 +01:00
ziggie f518931cf0 itest: fix list_payments accuracy edge case 2026-02-25 08:02:18 +01:00
ziggie 5f1f413596 paymentsdb: make specific kv store tests only available via build tag 2026-02-25 08:02:18 +01:00
ziggie 080fd78fb4 paymentsdb: introduce a harness interface
The design of the sql and kv db are a bit different. A harness
interface is introduced which allows us to unit most of the test
and keep the backend specific tests at a minimum.
2026-02-25 08:02:17 +01:00
ziggie 570863ca26 paymentsdb: add harness to run payment db agnostic tests
In commit adds the harness which will be used to run db agnostic
tests against the kv and sql backend. We have adopted all the
unit tests so far so that with this commit all the payment tests
not specifically put into the kv_store_test.go should all pass
for all backends.
2026-02-25 08:02:17 +01:00
ziggie 2eadbfb54d paymentsdb: fix test case before testing sql backend
We are now not supporting the LegacyPayload for the onion packet
anymore. All payments and their onion payload need to be tlv
encoded. The sql backend assumes tlv so we have to always set the
in memory presentation of a hop where the legacy parameter is still
available but deprecated to false, otherwise the hops will not be
equal and unit tests for the sql backend will fail when switched
on in the next commits.
2026-02-25 08:02:17 +01:00
ziggie de50f471e8 paymentsdb: make QueryPayments test db agnostic
We make the QueryPayments test db agnostic and also keep a small
test for querying the duplicate payments case in the kv world.
2026-02-25 08:02:17 +01:00
ziggie fdfa1430f8 paymentsdb: refactor test helpers
Since now the sql backend is more strict in using the same
session key we refactor the helper so that we can easily change
the session key for every new attempt.
2026-02-25 08:02:17 +01:00
ziggie 9ad3f2195b paymentsdb: remove kvstore from sql db implementation
Now that every method of the interface was implemented we can
remove the embedded reference we put into place for the sql store
implementation so that the interface would succeed. This is now
removed.
2026-02-25 08:02:17 +01:00