Commit graph

113 commits

Author SHA1 Message Date
Viktor Torstensson
a493b6a3d6
accounts: optionally cap account balances at the node channel balance
Add an opt-in check that prevents the sum of all account balances from
exceeding the node's available local (outbound) channel balance. When
enabled via the new accounts.check-channel-balance config option, the
service rejects balance allocations that would over-provision the node:
creating an account, an administrative credit, or an administrative
balance increase now fails with ErrBalanceReservationExceeded if it
would push the total allocated balance above the node's local channel
balance.

Note that the total channel balance may still decrease below the already
allocated account balance. This can occur if the node operator decreases
the total channel balance through non-account related activity.

The check is a no-op by default to preserve the historical behaviour
where the operator manages over-provisioning themselves (accounts can
legitimately be created before channels are funded). Invoice-driven
credits are unaffected, as they are backed by real inbound payments.
2026-08-07 12:26:29 +02:00
Viktor Torstensson
ae74392433
accounts: add a configurable maximum account payment size
Add an opt-in cap on the total amount a single account payment may
debit, including routing fees, enforced at the account interceptor for
both SendPaymentV2 and SendToRouteV2. When the new
accounts.max-payment-size-msat config option is set to a non-zero
value, payments whose amount plus fees exceeds it are rejected with
ErrPaymentExceedsMaxSize before the balance check and before any funds
are reserved.

For SendPaymentV2, the configured fee limit is included in the capped
amount. For SendToRouteV2, the route's stated fee is included.

This gives operators a guard rail against a compromised or misbehaving
account macaroon draining its balance in a single large payment. The
cap defaults to 0 (disabled), preserving existing behaviour, and is a
first step towards the finer-grained per-account spending controls
requested in the issue.
2026-08-07 00:04:20 +02:00
cyberguru1
d553cb28d7
accounts: implement AccountPayments RPC handler
Implement the AccountPayments handler on the Accounts RPC server.

The handler resolves accounts by ID or label, loads its payment hashes,
applies pagination constraints, and fetches complete payment details
concurrently from LND's TrackPaymentV2. It also returns pagination
metadata including the total count of payments.
2026-07-22 14:06:26 -05:00
cyberguru1
009a5c28bc
accounts: add payment methods to Store interface
Introduce the ListAccountPayments and CountAccountPayments methods to
the accounts Store interface.

ListAccountPayments enables retrieval of a paginated list of payment
entries associated with a given account ID, supporting offset and
limit. CountAccountPayments returns the total number of payments
associated with the account.
2026-07-22 14:06:25 -05:00
cyberguru1
857fb900fd
accounts: implement ListAccountPayments in BoltStore
Implement ListAccountPayments in the Bolt-based kvdb account store.
The retrieved account payments are sorted in ascending lexicographical
order of their payment hash.
2026-07-22 14:06:25 -05:00
cyberguru1
5632998030
accounts: implement ListAccountPayments in SQLStore
Add sqlc queries to select account payment hashes from the database.
Implement the ListAccountPayments method in the SQLStore, allowing
retrieval of stored payment hashes for SQLite and Postgres backends.

Also define the AccountPaymentEntry helper struct in
accounts/interface.go to wrap payment hashes and details.
2026-07-22 14:06:25 -05:00
Vandit Singh
73569014f2 accounts: don't mask payment errors when request values are absent 2026-07-19 01:19:35 +05:30
ziggie
2a4a57b55b
build: bump sqldb/v2 to v2.0.0 2026-07-10 19:34:19 -03:00
Viktor Torstensson
69eb14cc63
Merge pull request #1334 from 0xfandom/accounts/test-checklabel
Some checks failed
CI / frontend tests on macOS-latest (push) Has been cancelled
CI / frontend tests on ubuntu-latest (push) Has been cancelled
CI / frontend tests on windows-latest (push) Has been cancelled
CI / backend build on macOS-latest (push) Has been cancelled
CI / backend build on ubuntu-latest (push) Has been cancelled
CI / backend build on windows-latest (push) Has been cancelled
CI / cross compilation (push) Has been cancelled
CI / cross compilation-1 (push) Has been cancelled
CI / cross compilation-2 (push) Has been cancelled
CI / RPC proto compilation check (push) Has been cancelled
CI / check commits (push) Has been cancelled
CI / Sqlc check (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / run unit tests (push) Has been cancelled
CI / run unit tests-1 (push) Has been cancelled
CI / run unit tests-2 (push) Has been cancelled
CI / run unit tests-3 (push) Has been cancelled
CI / build itest binaries (push) Has been cancelled
CI / check release notes updated (push) Has been cancelled
CI / integration test (push) Has been cancelled
CI / integration test-1 (push) Has been cancelled
CI / integration test-2 (push) Has been cancelled
accounts: add test coverage for checkLabel
2026-06-24 12:07:30 +02:00
0xfandom
163cccba89 test: add coverage for account checkLabel validation
checkLabel rejects labels that could be mistaken for a hex-encoded
account ID. Add a table-driven test covering the accepted cases (empty,
plain text, short hex, and non-hex strings of account-ID length) as well
as the rejected cases (lower- and upper-case hex strings of account-ID
length).

Signed-off-by: 0xfandom <kashyapshivank01@gmail.com>
2026-06-23 14:59:05 +05:30
Viktor Torstensson
9016bbb3d2
multi: confirm kvdb migration at startup
Prompt before automatically migrating legacy kvdb state to SQL when
litd starts with a SQL backend and active bbolt data is still present.

Detect prior migrations by checking for the SQL tombstone marker so
already-migrated stores can start without prompting. Add unit coverage
for the prompt flow and wire stdin through the itest harness so the
migration restart path can acknowledge the prompt automatically.
2026-06-08 21:11:51 +02:00
Calvin Zachman
862c1dba9d
multi: bump lnd, lndclient, and taproot-assets dependencies
Bumps lnd to v0.21.0-beta, lndclient to v0.21.0-1, and
taproot-assets to v0.8.0 across the root, litrpc, and perms
modules. taprpc uses the proper v1.1.0 tag.

lnd removed the deprecated SendPaymentSync, SendToRouteSync,
SendPayment (streaming), and SendToRoute (streaming) RPCs. The
account interceptor checkers and their tests for these RPCs are
removed since no client can call them anymore. The V2 checkers
already handle account tracking for payments.

The itest harness is updated for lnd v0.21's miner API changes
(SendOutputs -> SendOutput, Miner.Client.Generate -> GenerateBlocks,
waitForNTxsInMempool replaced by Miner.AssertNumTxsInMempool) and
TimeLockDelta bumped from 20 to 40 since lnd v0.21 raised
MinCLTVDelta from 18 to 24.
2026-06-08 13:18:37 -04:00
Elle
4af5788819
Merge pull request #1309 from ViktorT-11/2026-05-add-master-additions-to-migration-code
[sql-72] Add `master` additions to migration code
2026-05-19 17:28:45 -07:00
Viktor Torstensson
2f5de800c8
multi: improve kvdb migration progress logging
Add clearer progress logging across the KVDB to SQL migration paths
for accounts, sessions, and firewalldb.

Log the total number of items to migrate where it is cheap to
determine up front, and emit progress updates every 100 migrated
items.
2026-05-19 11:22:45 +02:00
Viktor Torstensson
ddb750251e
accounts: update account mig to use 21933e8
commit 21933e8 in PR 1281 in master resolved the following issue:

"Get returns a byte slice that points directly into bbolt's
mmap'd file. This slice is only valid for the lifetime of the
transaction. In Account() and LastIndexes(), the slices were escaping
the View callback and being read after the transaction closed. Under
concurrent write transactions the file gets remapped, invalidating
the pointer and causing a segmentation fault.

Copy the byte slices inside the transaction so deserialization
operates on stable, heap-allocated memory."

This commit also updates the SQL migration to apply the same fix when
reading the Last Indexes.
2026-05-14 12:04:09 +02:00
Viktor Torstensson
000d28a4fb
multi: deprecate kvdb stores after SQL migration
Mark the legacy kvdb stores as deprecated once the kvdb -> SQL
migration commits successfully. This prevents normal bbolt startup
from reopening accounts.db, session.db, or rules.db after their data
has already been migrated.

Add explicit deprecation checks to the three kvdb store open paths and
provide migration-only constructors that can still reopen deprecated
files when the SQL database is deleted or downgraded and the migration
must be rerun.

Use store-specific tombstones for the deprecation markers and add
tests that verify deprecated stores are rejected while migration
reruns continue to work.
2026-05-14 11:39:14 +02:00
Viktor Torstensson
f78ddf1702
multi: rename MigrationStream names to MigrationSet 2026-05-14 11:39:09 +02:00
Viktor Torstensson
1f25ea60a1
accounts: add TestAccountStoreAccountsGrouping
Add a store test that verifies that Accounts groups invoices and
payments by account ID and does not leak linked data across accounts
when using the store.Accounts function.
2026-05-14 11:38:52 +02:00
Viktor Torstensson
fb5b0af8d8
accounts+sqlc: improve ListAccounts for SQL store
Optimize SQL account listing by preloading linked invoices and payments
for all accounts in bulk.

Before this change, Accounts() queried ListAllAccounts and then did two
extra queries per account (ListAccountInvoices/ListAccountPayments),
which scales poorly as account count grows.

Add ListAllAccountInvoices and ListAllAccountPayments queries, group
their rows by account_id in memory, and marshal each account from the
preloaded data. Keep conversion logic shared through
marshalDBAccountWithLinkedData to preserve behavior between
single-account and list-account paths.

This reduces query count from 1 + 2N to 3 and improves list-path
performance without changing external semantics.
2026-05-14 11:38:48 +02:00
Viktor Torstensson
fe28ff7e60
accounts: add database backend benchmark test 2026-05-14 11:38:37 +02:00
Viktor Torstensson
9c6464f7e4
accounts: export kvdb DB
The next commit that adds the kvdb to sql code migration will need to
initialize the accounts kvdb DB.

This commit exports that struct so that it can be referenced outside of
the accounts package.
2026-05-14 11:38:32 +02:00
Viktor Torstensson
f77ae5dcf2
multi: introduce migration stream for unit tests
In upcoming commits, we will introduce a new migration stream package
that will need to reference the db package, as well as the accounts,
session and firewalldb package in future commits. To avoid circular
dependencies, we therefore introduce a new migration stream that unit
tests can use, in order to avoid having to import the new migration
stream package.
2026-05-14 11:38:24 +02:00
Viktor Torstensson
ff0eaa9774
sqlc: remove UpdateAccountAliasForTests query
As no code path uses the sqlc.UpdateAccountAliasForTests query, now that
migrations tests have been updated to use the sqlcmig6 version, we can
remove the query from the sqlc package. As this query is only intended
to be used for testing, it is beneficial to not expose it to the rest of
the codebase to avoid accidental usage in the future.
2026-05-14 11:38:18 +02:00
Viktor Torstensson
0cb4901749
accounts: use sqlcmig6 for kvdb to sql migration
This commit updates the accounts package to use the new `sqlcmig6`
package for kvdb to SQL migration.
2026-05-14 11:38:04 +02:00
Viktor Torstensson
eb26c2b1c5
mutli: rename db.NewTestPostgresV2DB function
As the legacy `NewTestPostgresDB` function is no longer used and has
been removed, it no longer makes sense to have a `V2` suffix on the
`NewTestPostgresV2DB` function. This commit renames it to
`NewTestPostgresDB`, to indicate that this function now replaces the
legacy function.
2026-05-14 11:37:54 +02:00
Viktor Torstensson
b7ee351cb0
multi: use sqldb/v2 in accounts package
Update the accounts package to use the `sqldb/v2` package instead of the
older version.
2026-05-14 11:37:41 +02:00
cyberguru1
afe032a50d
accounts, litcli: address description for the command update and the space removal
This commit updates the account update command description and ArgsUsage
for better clarity.

Additionally, a formatting nit is fixed in the checkLabel error message
to ensure no line starts with a space character.
2026-05-05 13:39:14 -05:00
cyberguru1
21ae1c74b0
accounts: implement account rename support in stores 2026-05-05 13:39:14 -05:00
cyberguru1
85997ceefe
accounts: extract checkLabel helper from NewAccount 2026-05-05 13:38:27 -05:00
bitromortac
21933e8c8e
accounts: copy bbolt values before closing read transaction
bucket.Get returns a byte slice that points directly into bbolt's
mmap'd file. This slice is only valid for the lifetime of the
transaction. In Account() and LastIndexes(), the slices were escaping
the View callback and being read after the transaction closed. Under
concurrent write transactions the file gets remapped, invalidating
the pointer and causing a segmentation fault.

Copy the byte slices inside the transaction so deserialization
operates on stable, heap-allocated memory.
2026-04-09 10:54:38 +02:00
bitromortac
50376dd429
accounts: add regression test for concurrent bbolt read/write
Exercises the Account() and LastIndexes() code paths under concurrent
write pressure. Writers create accounts in a tight loop, growing the
database and forcing bbolt to remap the underlying file. Readers
simultaneously fetch a known account and verify its fields are not
corrupted.

Without the copy fix in the previous commit, this test triggers a
segfault or data corruption because the mmap'd memory backing the
byte slices returned by bucket.Get is invalidated by the remap.

Run with
go test ./accounts/ -run TestAccountReadConcurrentWrite -v -count=1 -race
2026-04-09 10:54:18 +02:00
ffranr
a0e63124c0
multi: reformat long lines for readability
- Replace occurrences of `// nolint:lll` with `// nolint:ll` across
  files for consistency.
- Reformat multiline strings, comments, and function parameters to
  improve clarity and adhere to style guidelines.
- Add `// nolint:ll` comments where necessary to prevent linter
  warnings.
2025-12-09 16:12:03 +00:00
George Tsagkarelis
537ed05776
build: bump lnd, lndclient, tapd, loop 2025-10-15 11:07:21 +02:00
Viktor Torstensson
4ccb56a771
multi: add UpdateAccountAliasForTests query
In the upcoming kvdb to SQL migration of the actions store, we need to
simulate in tests that two or more accounts have colliding account
aliases for the first 4 bytes of the alias. In order to allow creation
of such accounts, we need to be able to update the alias of an account
in tests, and this commit adds the a SQL query enabling this
functionality.

Note that the `UpdateAccountAliasForTests` query is only intended for
use in tests and should not be used in production code.
2025-10-07 02:13:41 +02:00
Viktor Tigerström
d52b4565f2
multi: separate account RPC server init & start
In the upcoming actions migration, we need fetch LNDs macaroons prior to
initializing the stores, as the macaroons will be required during the
migration.

This requires that we setup the connection to LND before we initialize
the stores, as we can only fetch the macaroons after the LND connection
is established.

In preparation of doing so, we cannot reference the stores when
initializing/creating the accounts RPC server object, as we do so prior
to setting up the LND connection.

This commit therefore refactors the accounts RPC server so that we
separate the initializing from the starting of the RPC server, and only
require the store reference during the actual startup of the RPC server.

Note that we still keep the init of the accounts RPC server reference
prior to setting up the LND connection, as not doing so would require
that we'd refactor the registering of `GrpcSubserver`s in a more complex
and in a less elegant way.
2025-09-01 10:30:19 +02:00
Oliver Gugger
c417adf318
mod+accounts+session: update to latest lnd RC 2025-07-11 18:41:16 +02:00
Viktor Tigerström
12343cf4ba
accounts: ensure that test SQL store is closed
We add a helper function to the functions that creates the test SQL
stores, in order to ensure that the store is properly closed when the
test is cleaned up.
2025-06-09 13:01:28 +02:00
Viktor Tigerström
8986d41616
accounts: iterate over bbolt buckets in migration
Update the SQL migration to instead of using the public functions of the
BoltStore struct, we instead iterate over the buckets directly. This
ensures that the BoltStore struct Acccounts & LastIndexes functions of
the BoltStore implementation can be changed, without effecting the
SQL migration.
2025-06-09 13:01:28 +02:00
Elle Mouton
8527969031
accounts: remove accountFromMacaroon helper
We only use it in one place and we can just handle the unwrap of the
option there.
2025-05-21 14:14:12 +02:00
Viktor Tigerström
1e0e88b32f
accounts: add randomized accounts migration tests 2025-05-19 14:29:15 +02:00
Viktor Tigerström
dbb5d73079
accounts: add migration code from kvdb to SQL
This commit introduces the migration logic for transitioning the
accounts store from kvdb to SQL.

Note that as of this commit, the migration is not yet triggered by any
production code, i.e. only tests execute the migration logic.
2025-05-19 14:29:15 +02:00
Viktor Tigerström
92689c546c
accounts: update NewTestDB func to return Store
In preparation for upcoming migration tests from a kvdb to an SQL store,
this commit updates the NewTestDB function to return the Store interface
rather than a concrete store implementation.

This change ensures that migration tests can call NewTestDB under any
build tag while receiving a consistent return type.
2025-05-02 15:44:50 +07:00
Elle
9e5578cf2d
Merge pull request #974 from ViktorTigerstrom/2025-02-credit-debit-accounts-impl
accounts: Add credit and debit account functionality
2025-04-01 13:13:43 +02:00
Viktor Tigerström
b2ec5974df
accounts: implement CreditAccount & DebitAccount
This commit implements the `CreditAccount` and `DebitAccount` endpoints
for the accounts subsystem.
2025-03-31 02:18:56 +02:00
Elle Mouton
dbec9da31b
multi: update LND and friends
This commit updates:
- LND
- pool
- loop
- taproot-assets
- faraday
- lighting-node-connect
- aperture
- lndclient
- btclog

We start using the new btclog v2 library and the associated breaking
changes in the lnd/build package.

With this commit, we deprecate the `--remote.lit-maxlogfiles` and
`--remote.lit-maxlogfilesize` options and introduce new logging options
under the `--remote.lit-logging` namespace.

Finally, the LND update introduced a new `MaxBlocksMinedPerTest`
variable in the `lntest` package that we now need to override in order
for our itests to pass.
2025-03-26 09:20:52 -05:00
Elle Mouton
03712e6067
accounts: add an IDFromCaveates helper
And use that from the existing accountFromMacaroon helper (which will
then test the new helper by proxy).

We add this helper so that we can use it later on from the sessions
package where we want to extract an account ID from a caveat (we wont
have a full macaroon available).
2025-03-04 13:43:13 +02:00
Elle Mouton
a82cda6ab4
accounts: keep the full *db.BaseDB pointer
This will make it possible in SQL unit tests to extract the BaseDB used
for accounts and use it to init another Store backed by the same BaseDB.
2025-03-02 13:36:57 +02:00
Elle Mouton
92d0ddb897
accounts: add SQL store implementation
In this commit, we add the SQLStore type which implements the
accounts.Store interface. To demonstrate that it works as expected, we
also plug this implementation into all the account unit tests to show
that they pass against the sqlite and postgres backends.

One can use `make unit pkg=accounts tags=test_db_postgres` or
`make unit pkg=accounts tags=test_db_sqlite` to test locally.

Note that 2 small timestamp related changes are made to the unit tests.
This is to compensate for timestamp precision in postgres.
2025-02-21 10:35:06 -03:00
Viktor Tigerström
3d67a7e7cb
accounts: implement kvdb DebitAccount
This commit introduces the `DebitAccount` function in the kvdb store,
to support decreasing an existing off-chain account’s balance by a
specified amount in the database.
2025-02-18 12:19:49 +01:00
Viktor Tigerström
cc003ab7a7
accounts: rename store IncreaseAccountBalance
In preparation for the upcoming credit/debit accounts feature, rename
the store `IncreaseAccountBalance` method to `CreditAccount` to better
reflect its purpose.
2025-02-18 11:15:12 +01:00