Commit Graph

216 Commits

Author SHA1 Message Date
Rusty Russell
873d4102c8 bookkeeper: restore limit on asking for all channelmoves at once.
Now we've found all the issues, the latency spike (4 seconds on my laptop)
for querying 2M elements remains.

Restore the limited sampling which we reverted, but make it 10,000 now.

This doesn't help our worst-case latency, because sql still asks for all 2M entries on
first access.  We address that next.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-20 16:30:50 +10:30
Rusty Russell
673729bdb9 Revert "bookkeeper: don't flood logs if we have many channelmoves all at once."
This reverts commit 1dda0c0753 so we can test
what its like to be flooded with logs again.

This benefits from other improvements we've made this release, to handling
plugin input (i.e. converting to use common/jsonrpc_io), so this doesn't
make much difference.

tests/test_coinmoves.py::test_generate_coinmoves (100,000, sqlite3):
	Time (from start to end of l2 node):	211 seconds
	Worst latency:				108 seconds

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-20 16:30:50 +10:30
Rusty Russell
2f2eacaac7 bookkeeper: no longer read listchannelmoves 1000 entries at a time.
This reverts `bookkeeper: only read listchannelmoves 1000 entries at a time.` commit,
so we can properly fix the scalability in the coming patches.

tests/test_coinmoves.py::test_generate_coinmoves (100,000):
	Time (from start to end of l2 node):	207 seconds
	Worst latency:				106 seconds

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-20 16:30:50 +10:30
Rusty Russell
ef5a9bd0de bookkeeper: fix printing of bad JSON results.
I noticed this in the logs:

```
listinvoices: description/bolt11/bolt12 not found (

{"jsonrpc":"2)
```

And we make the same formatting mistake in several places.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-20 16:30:50 +10:30
Rusty Russell
35f65c5d91 common: add amount_msat_deduct / amount_msat_deduct_sub.
I added amount_msat_accumulate for the "a+=b" case, but I was struggling
with a name for the subtractive equivalent.  After some prompting, ChatGPT
suggested deduct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-17 10:56:18 +10:30
Rusty Russell
8b9020d7b9 global: use clock_time in place of time_now().
Except for tracing, that sticks with time_now().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-13 21:21:29 +10:30
Rusty Russell
1dda0c0753 bookkeeper: don't flood logs if we have many channelmoves all at once.
Since we're synchronous, these only reach lightningd after we're done:
in the case of 1.6M channelmoves, that can give it major heartburn.

In practice, this reduces the first bkpr command on a fresh upgrade
from 349 to 235 seconds (but this was before other improvements we did
this release).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: Plugins: `bookkeeper` reduced logging for large imports to increase speed.
2025-11-04 12:47:37 +10:30
Rusty Russell
bd1798323a bookkeeper: fix assert() which happens with parallel queries.
```
bookkeeper: plugins/bkpr/bookkeeper.c:1226: parse_and_log_chain_move: Assertion `e->db_id > bkpr->chainmoves_index' failed.
bookkeeper: FATAL SIGNAL 6 (version v25.09-245-g901714b-modded)
0x5d7d8718b40f send_backtrace
        common/daemon.c:36
0x5d7d8718b4ab crashdump
        common/daemon.c:81
0x7a6086c4532f ???
        ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
0x7a6086c9eb2c __pthread_kill_implementation
        ./nptl/pthread_kill.c:44
0x7a6086c9eb2c __pthread_kill_internal
        ./nptl/pthread_kill.c:78
0x7a6086c9eb2c __GI___pthread_kill
        ./nptl/pthread_kill.c:89
0x7a6086c4527d __GI_raise
        ../sysdeps/posix/raise.c:26
0x7a6086c288fe __GI_abort
        ./stdlib/abort.c:79
0x7a6086c2881a __assert_fail_base
        ./assert/assert.c:96
0x7a6086c3b516 __assert_fail
        ./assert/assert.c:105
0x5d7d8717505d parse_and_log_chain_move
        plugins/bkpr/bookkeeper.c:1226
0x5d7d871754f4 listchainmoves_done
        plugins/bkpr/bookkeeper.c:169
0x5d7d87182a4b handle_rpc_reply
        plugins/libplugin.c:1072
0x5d7d87182b5c rpc_conn_read_response
        plugins/libplugin.c:1361
0x5d7d871ba660 next_plan
        ccan/ccan/io/io.c:60
0x5d7d871bab31 do_plan
        ccan/ccan/io/io.c:422
0x5d7d871babee io_ready
        ccan/ccan/io/io.c:439
```

Reported-by: @michael1011
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: plugins: assertion crash in bookkeeper when fresh records arrive while multiple queries in progress.
2025-11-03 14:03:25 +10:30
Rusty Russell
5106440c32 bookkeeper: fix restoration of derived wallet blockheights on restart.
We complain:
```
lightningd-1 2025-10-31T00:55:00.377Z **BROKEN** plugin-bookkeeper: Unparsable blockheight datastore entry: {"key":["bookkeeper","blockheights","756999f870a7a7c97f5c143f12b9096a50d1b1acd74aeb9ab2dc251a5c361494"],"generation":0,"hex":"00000067"}
```

And we don't have the blockheight:

```
                   {
                       'account': 'external',
         -             'blockheight': 103,
         ?                            - -
         +             'blockheight': 0,
                       'credit_msat': 555555000,
                       'currency': 'bcrt',
                       'debit_msat': 0,
                       'origin': 'wallet',
                       'outpoint': '756999f870a7a7c97f5c143f12b9096a50d1b1acd74aeb9ab2dc251a5c361494:0',
                       'tag': 'deposit',
                       'timestamp': 1761872097,
                       'type': 'chain',
                   },
```

Reported-by: @michael1011
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: Plugins: `bookkeeper` now correctly restores chain event blockheights it has derived.
2025-11-03 12:36:38 +10:30
Rusty Russell
f6a4e79420 global: remove unnecessary includes from headers.
Each header should only include the other headers it needs to compile;
`devtools/reduce-includes.sh */*.h` does this.  The C files then need
additional includes if they don't compile.

And remove the entirely useless wire/onion_wire.h, which only serves to include wire/onion_wiregen.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30
Rusty Russell
e120f87083 Makefile: create a library containing common, wire and bitcoin objects.
This means we don't have to manually choose what to link against,
which is much of the complexity of our Makefiles: the compiler will
automatically use any object files it needs to link.

We already do this for ccan as libccan.a, now we have libcommon.a.

We don't link against it for *everything*, as some tests require their own
versions.

Notes:
1. I get rid of the weird plugins/test/Makefile2 (accidental commit?)
2. Many tests change due to update-mocks.
3. In some places I added the missing dependency on the Makefile itself, though most are in the next
   patch.

Before:
	Total program size:     221366528
	Total tests size:       364243856

After:
	Total program size:     190733656
	Total tests size:       337880888

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.227000-44.245000(41.8222+/-1.6)s
	user    3m2.105000-33.696000(23.1442+/-8.4)s
	sys     0m35.054000-42.269000(39.7231+/-2)s
After:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m18.928000-22.776000(21.5084+/-1.1)s
	user    2m8.613000-36.567000(27.7281+/-7.7)s
	sys     0m20.458000-23.436000(22.3963+/-0.77)s

After:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>

rusty@rusty-Framework:~/devel/cvs/lightni
2025-10-23 06:44:04 +10:30
Rusty Russell
c758672ac0 bookkeeper: honor start and ent times when consolidating onchain fees.
Fixes: https://github.com/ElementsProject/lightning/issues/8318

Reported-by: Shahaha
Changelog-Fixed: Plugins: `bkpr_listincome` now honors `start_time` and `end_time` parameters for onchain fees.
2025-10-21 20:21:56 +10:30
Rusty Russell
2539b4f199 bookkeeper: save last timestamp to avoid another query in find_consolidated_fees.
If the fees are not *all* of the fees (as we do in next patch), the
query would be wrong.  Plus, as the FIXME suggests, we should just save
it as we're getting the fee_sums, not do a whole new query!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-21 20:21:56 +10:30
Rusty Russell
e522aedc94 bookkeeper: only read listchannelmoves 1000 entries at a time.
If we read all of them, we might get 1.6M at once (after initial
migration).  Then we submit a few hundred thousand simultaneous
requests to lightningd, and it gets upset, queueing them all on the
xpay command hook and running out of memory.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: plugins: bookkeeper first invocation after migration from prior to 25.09 with very large databases will not crash.
2025-10-20 11:19:22 +10:30
Rusty Russell
343d30ae5b bookkeeper: fix reloading of rebalances.
Parse key correctly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: bookkeeper: failed reload of rebalances on restart.
2025-10-01 15:35:50 +09:30
Rusty Russell
40a44d9aab build: fix build with no sqlite3 support.
Reported-by: whitslack
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fixes: https://github.com/ElementsProject/lightning/issues/8473
Changelog-Fixed: build: we can now build without sqlite3 support.
2025-09-29 13:11:44 +09:30
Rusty Russell
3e4c922114 bkpr: don't leak event in fillin_apy_acct_details.
Now accounts are not transient, we can't use them as temporary parents:

```
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper: MEMLEAK: 0x5e3bf926ce28
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:   label=plugins/bkpr/sql.c:109:struct chain_event
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:   alloc:
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     ccan/ccan/tal/tal.c:488 (tal_alloc_)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/sql.c:109 (chain_events)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/sql.c:197 (chain_events_from_sql)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/recorder.c:352 (find_chain_event_by_id)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/channelsapy.c:120 (fillin_apy_acct_details)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/channelsapy.c:172 (compute_channel_apys)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/bookkeeper.c:220 (getblockheight_done)
lightningd-2 2025-08-28T02:36:35.629Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1134 (handle_rpc_reply)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1438 (rpc_read_response_one)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1462 (rpc_conn_read_response)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:60 (next_plan)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:422 (do_plan)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:439 (io_ready)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/poll.c:455 (io_loop)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:2564 (plugin_main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/bookkeeper.c:1547 (main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ../sysdeps/nptl/libc_start_call_main.h:58 (__libc_start_call_main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ../csu/libc-start.c:360 (__libc_start_main_impl)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:   steal:
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/tal/tal.c:559 (tal_steal_)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/recorder.c:361 (find_chain_event_by_id)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/channelsapy.c:120 (fillin_apy_acct_details)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/channelsapy.c:172 (compute_channel_apys)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/bookkeeper.c:220 (getblockheight_done)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1134 (handle_rpc_reply)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1438 (rpc_read_response_one)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:1462 (rpc_conn_read_response)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:60 (next_plan)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:422 (do_plan)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/io.c:439 (io_ready)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ccan/ccan/io/poll.c:455 (io_loop)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/libplugin.c:2564 (plugin_main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/bookkeeper.c:1547 (main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ../sysdeps/nptl/libc_start_call_main.h:58 (__libc_start_call_main)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     ../csu/libc-start.c:360 (__libc_start_main_impl)
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:   parents:
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/account.c:51:struct account
lightningd-2 2025-08-28T02:36:35.630Z **BROKEN** plugin-bookkeeper:     plugins/bkpr/bookkeeper.c:1546:struct bkpr
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-28 13:42:04 +09:30
Rusty Russell
22a5e9e7e6 common: reintroduce "ignored" primary tag.
Prior to 23.05, we used this tag to mark onchain to-self inputs we didn't
wait for (because they were too small).  This fixes migration if that happened
(and we are debating whether we should re-introduce this!).

```
lightningd: FATAL SIGNAL 6 (version v25.09rc2)                      
0x100c8683 send_backtrace                                           
        common/daemon.c:33                                          
0x100c876f crashdump                                                
        common/daemon.c:78                                          
0x7fffb2080493 ???                                                  
        ???:0                                                       
0x7fffb1ab0cac ???                                                  
        __pthread_kill_implementation+0x1bc:0
0x7fffb1a48a5b ???                                                  
        __GI_raise+0x2b:0                                           
0x7fffb1a2a3db ???                                                  
        __GI_abort+0x153:0                                          
0x100935b7 migrate_from_account_db
        wallet/account_migration.c:424
0x10093ff7 db_migrate                                               
        wallet/db.c:1139                                            
0x10096763 db_setup                                                 
        wallet/db.c:1185                                            
0x100a1bcb wallet_new                                               
        wallet/wallet.c:223                                         
0x1004485f main                                                     
        lightningd/lightningd.c:1311
0x7fffb1a2aba3 ???                                                  
        __libc_start_call_main+0x93:0
0x7fffb1a2adeb ???                                                  
        __libc_start_main_alias_1+0x1ab:0
0xffffffffffffffff ???                                              
        ???:0                                                       
lightningd: Died with signal 6                                  
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fixes: https://github.com/ElementsProject/lightning/issues/8484
2025-08-28 13:41:44 +09:30
Rusty Russell
66741dda79 plugins/bkpr/test/run-recorder: don't hand NULL cmd.
ubsan complains that we declared a function not to take NULL.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
f8a44d911d bkpr: restore run-recorder.
This requires us to turn "sql" calls into calls to a local db, which
means pulling in a lot of infrastructure.  But it's possible.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
194febe873 wallet: generate fixup chainmoves and channelmoves when first starting.
If we don't have an accountdb from bookkeeper:

1. Generate a deposit chain event for every confirmed UTXO.
2. Generate an open chain event for every open, confirmed channel.
3. Generate a push/lease event if necessary.
4. Generate a fixup "journal" entry if balance is different from initial.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
52d69df10f lightningd: migrate events from bookkeeper at startup.
We take over the --bookkeeper-dir and --bookkeeper-db options, and
then if we can find the bookkeeper db we extract the records to
initialize our chain_moves and channel_moves tables.

Of course, bookkeeper now needs to not register those options.

When bookkeeper gets invoked the first time, it will reconstruct
everything from listchannelmoves and listcoinmoves.  It cannot
preserve manually-added descriptions, so we put those in the datastore
for it ready to go.

Note that the order of onchain_fee changes slightly from the original.
But this is fine.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
41ac9db7ea bookkeeper: remove database.
Changelog-Changed: Plugins: `bookkeeper` now uses the lightningd database, not "accounts.db".
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
8748755834 bkpr: remove missing event handling.
There will be no more missing events (and at initialization time, we will do
that as a migration).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
b70f4f6184 bkpr: convert all the local db sql queries into calls to sql plugin.
With some help (and hinderance!) from ChatGPT: the field names
differ slightly from our internal db.

The particilar wrinkle is that we have to restrict all queries to
limit them to entries we've seen already.  Our code expects this (we
used to only enter it into the db when we processed it), and it would
otherwise be confusing if a sql query returned inconsistent results
because an event occurred while bookkeeper was processing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
2a191479a3 bkpr: take, don't steal in new_channel_event.
Cleaner (I'm about to hand it a sha256 on the stack).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
45e860ad58 bkpr: helpers to query sql plugin for chainmoves and channelmoves.
We're going to be using this instead of our internal db.

I also made json_out_obj() take the str arg, as it didn't and I
expected it to.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
70d19e852e bkpr: use list commands instead of subscribing to notifications.
This is reliable, meaning we should never get replayed events.

We have to reference count to make sure all commands are complete,
before we return.  In particular, annotating with descriptions can
involve several calls to list commands.  We need to give them the
results *after* this is all complete.

test_bookkeeping_descriptions() relied on log messages from
notifications, which now only happen when a command is called.  This
changes the test a bit.

Since we no longer subscribe to the balance_snapshot event, we
need to create the wallet account at initialization, as callers
expect it to exist.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
f50ceb4ce6 bookkeeper: call refresh before processing any commands.
Rearrange all the JSON interfaces to call refresh_moves() (async)
before doing anything.

This does nothing for now, but it will be useful once we transition
from notifications to using the list commands.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
9cc08bf06d plugins/bkpr/test/run-recorder: remove.
It's a great test, but it's very hard to simulate now we are going to be
going from the internal db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
1f7905259b bkpr: forward utxo_deposit / utxo_spend notifications to new injectutxodeposit / injectutxospend calls.
And thus we absorb them as normal when they come back as "foreign" entries.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
980241fc36 bookkeeper: don't set origin account to "null" if that's specified as transfer_from.
Before bkpr_listaccountevents() gave entries with origin like:

	{'account': "nifty's secret stash",
         'blockheight': 111,
         'credit_msat': 180000000,
         'currency': 'bcrt',
         'debit_msat': 0,
         'origin': 'null',
         'outpoint': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:0',
         'tag': 'deposit',
         'timestamp': 1679955976,
         'type': 'chain'},

Changelog-Changed: Plugins: "utxo_deposit" is allows to have missing `transfer_from`, and null is not considered an account name.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
78f88c1c0c common: use "foreign" in extra_tags to indicate a chain movement is injected.
This allows the bookkeeper plugin to know it's not actually a channel account.

Remove the "ignored" tag from the schema too: we removed it previously.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
254b7ed666 lightningd: new internal-use commands to allow utxo spend / deposit injection.
For the moment, we'll continue to use bookkeeper to monitor the
notifications to insert these (we don't have the internal infrastructure
for that, and actually these commands are probably better than using
notifications).

We hoist param_outpoint() into common code, since there are already
two uses.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
c5e359c7ee bkpr: add in-mem & datastore storage for external blockheights.
We won't be able to "UPDATE chain_events", so keep a separate record
of these blockheights, and lookup that when the blockheight is 0.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
e28443ed40 bkpr: pass bkpr struct into all functions in recorder.c.
We want to access it in stmt2chain_event, so plumb it through.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
0afc4c0346 bookkeeper: use the in-memory rebalances.
Remove the rebalance field from channel_event, and use the
find_rebalance(bkpr, ev->db_id) to look it up instead.

chain_event's also had a `rebalance` field, but it was only ever set
(to false), never read.

Note: list_rebalances() was only used by tests, not a public API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
6dcc580612 bkpr: infrastructure to keep rebalance pairs in mem & datastore.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
af2e403490 bkpr: use the in-memory event descriptions, not the database ones.
The new access APIs are more symmetrical:

1. edit_utxo_description -> add_utxo_description
2. add_payment_hash_desc -> add_payment_hash_description

And to read it, instead of accessing ->ev_desc (now removed) we use
chain_event_description() & channel_event_description(), threading bkpr though
as needed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
e012a82c60 bookkeeper: infrastructure to keep descriptions (annotations) in mem and datastore.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
66d3680f0c bkpr: remove onchain_fees table.
This proves we don't access it any more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
bf9c864e0f bkpr: save and restore onchain_fees from the datastore, not the db.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
4e49a66c03 bkpr: keep onchain_fees in memory & datastore.
This is not quite as efficient, perhaps, but in practice there are only
a handful of onchain fee records per account.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
568f12ea71 bkpr: remove redundant account_onchain_fees
It's account_get_chain_fees() without the ORDER BY clause, *and* it's
only used on the run-recorder.c test.

I also got ChatGPT to compare them, in case I missed something subtle.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
5dc88c9e84 bkpr: move onchain fees access into bkpr/onchain_fee.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
902f821f88 bkpr: remove accounts table from db altogether.
This proves we don't access it any more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
fd657e8483 ]bkpr: save accounts in the datastore instead of database.
Note that the test where we remove the database causes the bookkeeper
plugin to assert, since we have removed part (but not all!) of its data
by removing the datastore.

Once the transition to the datastore is complete, this can be restored.

Note that we destroy the request before receiving a response, which causes
a message in the trace span which was confusing our test.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
9c786a20a7 bkpr: use direct names, not another table, in db.
This simplifies the transition (since accounts are going to be outside
the db).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
f72df6da9c bkpr: cache accounts table in memory.
When we don't have our own db, we're going to need to keep this information
in memory (and the datastore).  As a first step, simply cache it in memory
and still write through to the db.

This introduces some changes:
1. The account structures are not temporary, but in the hash table (so don't steal them).
2. test_forward_pad_fees_and_cltv assumed ordering, which was a latent bug.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30
Rusty Russell
fb20a55706 bkpr: find_or_create_account() instead of raw new_account().
In practice, it's always either find_account() or find_or_create_account().

This means account_add can be made internal: we rename it to
account_db_add.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-08-19 13:37:50 +09:30