Skip to content

Improve data stored in AuditDb#3245

Merged
t-bast merged 7 commits intomasterfrom
audit-db-rework
Mar 26, 2026
Merged

Improve data stored in AuditDb#3245
t-bast merged 7 commits intomasterfrom
audit-db-rework

Conversation

@t-bast
Copy link
Copy Markdown
Member

@t-bast t-bast commented Jan 30, 2026

In this PR, we make a collection of changes to all tables of the AuditDb. The main change is that it is way more relevant to track statistics for peer nodes instead of individual channels, so we want to track the node_id associated with each event. We also track more data about transactions we make and relayed payments, to more easily score peers based on the fees we're earning vs the fees we're paying (for on-chain transactions or for liquidity purchases).

Each commit should be reviewed individually, as they are scoped to different tables of the DB and will make the review easier.

Note that we cannot migrate existing data (since it is lacking information that we now need), so we simply rename older tables and create new ones. Past data will thus not be accessible through the APIs, but can be queried directly using SQL if necessary. It should be fine, since liquidity decisions should be taken based on relatively recent data (a few weeks) in order to be economically relevant (nodes that generated fees months ago but aren't generating any new fees since then are probably not good peers).

Fixes #2977

@t-bast t-bast force-pushed the audit-db-rework branch from 33fd5cb to bc1162c Compare March 3, 2026 10:33
@t-bast t-bast marked this pull request as ready for review March 17, 2026 16:17
@t-bast t-bast requested a review from pm47 March 17, 2026 16:17
t-bast added 6 commits March 20, 2026 12:02
We improve the `channel_events` table in the `AuditDb` by:

- adding the `funding_txid` from each event
- adding the `channel_type` from each event
- using hex instead of blobs in sqlite
- providing APIs to list these events
- adding indexes on `channel_id` and `remote_node_id`

The added data isn't available in past events. We decide to simply
rename the older tables and start fresh: previous data will not be
available from the API, but can still be queried directly in SQL if
necessary.
We improve the `transactions_published` and `transactions_confirmed`
tables in the `AuditDb` by:

- adding more details about mining fees and feerates
- adding input and output counts
- using hex instead of blobs in sqlite
- providing APIs to list transactions by `remote_node_id`
- adding indexes for listing APIs

The added data isn't available in past events. We decide to simply
rename the older tables and start fresh: previous data will not be
available from the API, but can still be queried directly in SQL if
necessary.
We improve the `sent` and `received` payment tables in the `AuditDb` by:

- adding `start_at` / `settled_at` timestamps to measure duration
- adding `remote_node_id` for every channel used
- using hex instead of blobs in sqlite
- reordering columns and renaming them wherever useful

The added data isn't available in past events. We decide to simply
rename the older tables and start fresh: previous data will not be
available from the API, but can still be queried directly in SQL if
necessary.
We improve the `relayed` and `relayed_trampoline` payment tables in the
`AuditDb` by:

- unifying the way we handle multi-part payments, by having one row per
  payment, whether incoming or outgoing, and reconciling based on type
  and direction when listing events
- adding `remote_node_id` for every channel used
- using hex instead of blobs in sqlite
- renaming columns for better clarify

The added data isn't available in past events. We decide to simply
rename the older tables and start fresh: previous data will not be
available from the API, but can still be queried directly in SQL if
necessary.
We change the relay statistics we compute, to more accurately reflect
nodes that are good or bad peers and inform liquidity allocation
decisions.
We now track liquidity purchases as well in transaction events, as their
fees must be taken into account when evaluating the overall fees earned
from a node.

We also add the number of on-chain transactions made with a given peer
to its relay stats.
Copy link
Copy Markdown
Member

@pm47 pm47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding some temporary code preventing boot, to allow adjusting the DB model before the next release? (already in place)

This would probably be the right time to do optimizations such as indexing based on sequences instead of long identifiers. I'm not sure the trade-off is worth it, in terms of additonal JOINs at read time.

@t-bast t-bast merged commit 890ccb3 into master Mar 26, 2026
1 check passed
@t-bast t-bast deleted the audit-db-rework branch March 26, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improvements to the data stored in AuditDb

2 participants