Skip to content

Conversation

tnull
Copy link
Contributor

@tnull tnull commented Sep 5, 2025

This is the second PR in a series of PRs adding persistence to lightning-liquidity (see #4058). As this is already >1000LoC, I now decided to put this up as an intermediary step instead of adding everything in one go.

In this PR we add the serialization logic for for the LSPS2 and LSPS5 service handlers as well as for the event queue. We also have LiquidityManager take a KVStore towards which it persists the respetive peer states keyed by the counterparty's node id. LiquidityManager::new now also deserializes any previously-persisted state from that given KVStore. Note that so far we don't actually persist anything, as wiring up BackgroundProcessor to drive persistence will be part of the next PR (which will also make further optimizations, such as only persisting when needed, and persisting some imporant things in-line).

This also adds a bunch of boilerplate to account for both KVStore and KVStoreSync variants, following the approach we previously took with OutputSweeper etc.

cc @martinsaposnic

@tnull tnull requested a review from TheBlueMatt September 5, 2025 14:31
@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Sep 5, 2025

👋 Thanks for assigning @TheBlueMatt as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tnull tnull force-pushed the 2025-01-liquidity-persistence branch 2 times, most recently from 124211d to 26f3ce3 Compare September 5, 2025 14:41
@tnull tnull self-assigned this Sep 5, 2025
@tnull tnull added the weekly goal Someone wants to land this this week label Sep 5, 2025
@tnull tnull added this to the 0.2 milestone Sep 5, 2025
@tnull tnull moved this to Goal: Merge in Weekly Goals Sep 5, 2025
@tnull tnull force-pushed the 2025-01-liquidity-persistence branch 4 times, most recently from a98dff6 to d630c4e Compare September 5, 2025 14:58
Copy link

codecov bot commented Sep 5, 2025

Codecov Report

❌ Patch coverage is 45.02924% with 282 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.54%. Comparing base (ecce859) to head (2971982).

Files with missing lines Patch % Lines
lightning-liquidity/src/manager.rs 63.77% 68 Missing and 3 partials ⚠️
lightning-liquidity/src/persist.rs 47.12% 41 Missing and 5 partials ⚠️
lightning-liquidity/src/lsps2/service.rs 23.07% 38 Missing and 2 partials ⚠️
lightning-liquidity/src/lsps5/service.rs 20.51% 31 Missing ⚠️
lightning-liquidity/src/events/event_queue.rs 14.70% 29 Missing ⚠️
lightning-liquidity/src/events/mod.rs 0.00% 28 Missing ⚠️
lightning-liquidity/src/lsps5/msgs.rs 0.00% 16 Missing ⚠️
lightning-liquidity/src/lsps0/ser.rs 53.57% 10 Missing and 3 partials ⚠️
lightning-liquidity/src/lsps5/url_utils.rs 33.33% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4059      +/-   ##
==========================================
- Coverage   88.76%   88.54%   -0.22%     
==========================================
  Files         176      178       +2     
  Lines      129518   130019     +501     
  Branches   129518   130019     +501     
==========================================
+ Hits       114968   115127     +159     
- Misses      11945    12269     +324     
- Partials     2605     2623      +18     
Flag Coverage Δ
fuzzing 21.96% <13.20%> (-0.06%) ⬇️
tests 88.37% <44.05%> (-0.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tnull tnull force-pushed the 2025-01-liquidity-persistence branch from d630c4e to 70118e7 Compare September 5, 2025 15:15
@tnull tnull force-pushed the 2025-01-liquidity-persistence branch from 70118e7 to dd43edc Compare September 5, 2025 15:28
@martinsaposnic
Copy link
Contributor

this all LGTM.

I have a small concern: maybe I’m being a little paranoid, but read_lsps2_service_peer_states and read_lsps5_service_peer_states pull every entry from the KVStore into memory with no limit. That could lead to unbounded state, exhausting memory and crash. Maybe we can add a limit on how many entries we load into memory to protect against this dos?

not sure how realistic this is though. maybe an attacker could have access to or share the same storage with the victim, and they could dump effectively infinite data onto disk. in this scenario, probably the victim would be vulnerable to other attacks too, but still..

@tnull
Copy link
Contributor Author

tnull commented Sep 5, 2025

I have a small concern: maybe I’m being a little paranoid, but read_lsps2_service_peer_states and read_lsps5_service_peer_states pull every entry from the KVStore into memory with no limit. That could lead to unbounded state, exhausting memory and crash. Maybe we can add a limit on how many entries we load into memory to protect against this dos?

Reading state from disk (currently) happens on startup only, so crashing wouldn't be the worst thing, we would simply fail to start up properly. Some even argue that we need to panic if we hit any IO errors at this point to escalate to an operator. We could add some safeguard/upper bound, but I'm honestly not sure what it would protect against.

not sure how realistic this is though. maybe an attacker could have access to or share the same storage with the victim, and they could dump effectively infinite data onto disk. in this scenario, probably the victim would be vulnerable to other attacks too, but still..

Heh, well, if we assume the attacker has write access to our KVStore, we're very very screwed either way. Crashing could be the favorable outcome then, actually.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @TheBlueMatt! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@tnull tnull force-pushed the 2025-01-liquidity-persistence branch from dd43edc to f73146b Compare September 8, 2025 07:37
entropy_source: ES, node_signer: NS, channel_manager: CM, chain_source: Option<C>,
chain_params: Option<ChainParameters>, service_config: Option<LiquidityServiceConfig>,
chain_params: Option<ChainParameters>, kv_store: Arc<dyn KVStore + Send + Sync>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does the KVStore need to be dyn or an Arc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above #4059 (comment): generally dealing with all the generics gets very cumbersome. In this particular case, if we'd make it a generic, it would need to propagate to all the inner service handlers, in turn requiring we need to provide Sync wrappers for all of them. FWIW, we might need such wrappers at least for some of them once we do in-line persistence for some of the API calls, but still dealing with both KVStore and KVStoreSync generics across the LiquidityManager codebase would be a huge mess then.

/// Wraps [`LiquidityManager::new`].
pub fn new(
entropy_source: ES, node_signer: NS, channel_manager: CM, chain_source: Option<C>,
chain_params: Option<ChainParameters>, kv_store_sync: Arc<dyn KVStoreSync + Send + Sync>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, I see no reason why KVStoreSync needs to be dyn or an Arc? I also don't see why it needs to be Send + Sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above.

As to why we need Send + Sync, it's required for spawning the async BP task. If we remove the bounds, we get a lot of errors like:

error[E0277]: `(dyn KVStore + 'static)` cannot be shared between threads safely
    --> lightning-background-processor/src/lib.rs:2463:4
     |
2455 |         let bg_processor = BackgroundProcessor::start(
     |                            -------------------------- required by a bound introduced by this call
...
2463 |             Some(Arc::clone(&nodes[0].liquidity_manager)),
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn KVStore + 'static)` cannot be shared between threads safely
     |
     = help: the trait `std::marker::Sync` is not implemented for `(dyn KVStore + 'static)`
     = note: required for `Arc<(dyn KVStore + 'static)>` to implement `std::marker::Sync`

...

error[E0277]: `(dyn KVStore + 'static)` cannot be sent between threads safely
    --> lightning-background-processor/src/lib.rs:3376:25
     |
3376 |         let t1 = tokio::spawn(bp_future);
     |                  ------------ ^^^^^^^^^ `(dyn KVStore + 'static)` cannot be sent between threads safely
     |                  |
     |                  required by a bound introduced by this call
     |
     = help: the trait `Send` is not implemented for `(dyn KVStore + 'static)`
     = note: required for `Arc<(dyn KVStore + 'static)>` to implement `std::marker::Sync`
note: required because it appears within the type `lightning_liquidity::events::event_queue::EventQueue`
    --> /home/tnull/workspace/rust-lightning/lightning-liquidity/src/events/event_queue.rs:24:19

in the background processor tests.

@@ -45,6 +46,10 @@ pub struct LSPS2GetInfoRequest {
pub token: Option<String>,
}

impl_writeable_tlv_based!(LSPS2GetInfoRequest, {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really want to have two ways to serialize all these types? Wouldn't it make more sense to just use the serde serialization we already have and wrap that so that it can't all be misused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think I'd be in favor of using TLV serialization for our own persistence.

Note that the compat guarantees of LSPS0/the JSON/serde format might not exactly match what we require in LDK, and our Rust representation might also diverge from the pure JSON impl. On top of that JSON is of course much less efficient.

) -> Pin<Box<dyn Future<Output = Result<(), lightning::io::Error>> + Send>> {
let outer_state_lock = self.per_peer_state.read().unwrap();
let mut futures = Vec::new();
for (counterparty_node_id, peer_state) in outer_state_lock.iter() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh? Why would we ever want to do a single huge persist pass and write every peer's state at once? Shouldn't we be doing this iteratively? Same applies in the LSPS2 service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, only persisting what's needed/changed will be part of the next PR as it ties into how we wake the BP to drive persistence (cf. "Avoid re-persisting peer states if no changes happened (needs_persist flag everywhere)" bullet over at #4058 (comment)).

We add `KVStore` to `LiquidityManager`, which will be used in the next
commits. We also add a `LiquidityManagerSync` wrapper that wraps a the
`LiquidityManager` interface which will soon become async due to usage
of the async `KVStore`.
We add simple `persist` call to `LSPS2ServiceHandler` that sequentially
persist all the peer states under a key that encodes their node id.
We add simple `persist` call to `LSPS5ServiceHandler` that sequentially
persist all the peer states under a key that encodes their node id.
We add simple `persist` call to `EventQueue` that persists it under a
`event_queue` key.
.. this is likely only temporary necessary as we can drop our own
`dummy_waker` implementation once we bump MSRV.
We read any previously-persisted state upon construction of
`LiquidityManager`.
We read any previously-persisted state upon construction of
`LiquidityManager`.
We read any previously-persisted state upon construction of
`LiquidityManager`.
@tnull tnull force-pushed the 2025-01-liquidity-persistence branch from f73146b to 2971982 Compare September 9, 2025 07:35
@tnull
Copy link
Contributor Author

tnull commented Sep 9, 2025

Rebased to address minor conflict.

@tnull tnull requested a review from TheBlueMatt September 10, 2025 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
Status: Goal: Merge
Development

Successfully merging this pull request may close these issues.

4 participants