Skip to content

[LSPS5 FollowUp] Simplify LSPS5/client peer state - #3969

Merged
tnull merged 3 commits into
lightningdevkit:mainfrom
martinsaposnic:simplify-lsps5-client-peer-state
Aug 5, 2025
Merged

[LSPS5 FollowUp] Simplify LSPS5/client peer state#3969
tnull merged 3 commits into
lightningdevkit:mainfrom
martinsaposnic:simplify-lsps5-client-peer-state

Conversation

@martinsaposnic

@martinsaposnicmartinsaposnic commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

Addressing some LSPS5 follow ups, specifically comments #3662 (comment) and #3662 (comment)

This PR introduces changes so the LSPS5/client drops the TimeProvider, and now instead of just expiring pending requests after a certain time, it just keeps the 5 most recent requests of each type (the 5 here can be changed, not married to the value, I put it just to put something. Thoughts @tnull@TheBlueMatt ?)

My preference for implementing this was:

  • Keep the ergonomics of the hashmap's O(1) get function
  • Have something like VecDeque with_capacity to be able to truncate / evict the last value when a new value comes in
  • LSPS5/client was recently reviewed so I wanted to minimize changes for the reviewers happiness

Some alternatives for making this change were

  • Add a new dependency like linked-hash-map (just saying it out loud makes me nervous lol)
  • Have a Vec or VecDeque with capacity to keep track of the requests, truncate when limit is reached, and then do vec.iter().position(|v, _, _| v == r_id) for retrieval (which is kind of ugly and not O(1) like in hashmaps)
  • Define a new type BoundedMap, which is a fixed size map with a defined capacity and FIFO eviction

I went with the last option, but I'm open to alternatives

Also, the last commit introduces a change where the peerstate is cleaned if there are no pending requests with that peer

@ldk-reviews-bot

ldk-reviews-bot commented Jul 28, 2025

Copy link
Copy Markdown

👋 Thanks for assigning @tnull 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.

@codecov

codecovBot commented Jul 28, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.29630% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.99%. Comparing base (61e5819) to head (cf23e91).
⚠️ Report is 7 commits behind head on main.

Files with missing linesPatch %Lines
lightning-liquidity/src/lsps5/client.rs96.25%5 Missing and 1 partial ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #3969 +/- ##
==========================================
+ Coverage 88.94% 88.99% +0.04% 
==========================================
Files 174 174 Lines 124201 124199 -2 Branches 124201 124199 -2 ==========================================
+ Hits 110472 110525 +53 + Misses 11251 11200 -51 + Partials 2478 2474 -4 
FlagCoverage Δ
fuzzing22.64% <0.00%> (+<0.01%)⬆️
tests88.81% <96.29%> (+0.04%)⬆️

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.

@martinsaposnic
martinsaposnicforce-pushed the simplify-lsps5-client-peer-state branch from c8e9948 to fba3056CompareJuly 28, 2025 19:36
@tnull
tnull requested review from tnull and removed request for valentinewallaceJuly 29, 2025 08:49

@tnulltnull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did a first pass, mostly looks good.

}

self.map.insert(key.clone(), value);
self.order.push_back(key.clone());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this clone is unnecessary?

if let Some(oldest) = self.order.pop_front() {
self.map.remove(&oldest);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we sprinkle in some debug_asserts ensuring that neither map nor order ever exceed cap at the end of the methods taking &mut?

assert!(m.get(&"key").is_none());
}

#[test]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These tests would be a prime example were proptests would be easily applicable and would extend test coverage.

Comment threadlightning-liquidity/src/utils/mod.rs Outdated
}
}

pub(crate) mod bounded_map;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Huh, why are these mod definitions at the bottom by now? Let's move them to the top please, and let's keep pub and pub(crate) definitions in separate groups.

};
self.with_peer_state(*counterparty_node_id, handle_response);

let mut outer_state_lock = self.per_peer_state.write().unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mind moving this out to a dedicated method?

@ldk-reviews-bot

Copy link
Copy Markdown

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@martinsaposnicmartinsaposnic mentioned this pull request Jul 30, 2025
18 tasks
@martinsaposnic

Copy link
Copy Markdown
ContributorAuthor

thanks for the review @tnull ! all comments are addressed in fixup commits

Comment threadlightning-liquidity/src/lsps5/client.rs
Comment threadlightning-liquidity/src/lsps5/client.rs
@martinsaposnic
martinsaposnicforce-pushed the simplify-lsps5-client-peer-state branch from 1332159 to 91c7904CompareJuly 30, 2025 19:35

@tnulltnull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Feel free to squash fixups!

@martinsaposnic
martinsaposnicforce-pushed the simplify-lsps5-client-peer-state branch from 91c7904 to 5d25ab1CompareJuly 31, 2025 12:14
@martinsaposnic

Copy link
Copy Markdown
ContributorAuthor

done! @tnull

@martinsaposnic
martinsaposnicforce-pushed the simplify-lsps5-client-peer-state branch from 5d25ab1 to 993c366CompareJuly 31, 2025 16:01
tnull
tnull previously approved these changes Aug 1, 2025

@tnulltnull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@ldk-reviews-bot

Copy link
Copy Markdown

✅ Added second reviewer: @valentinewallace

@tnull
tnull requested review from TheBlueMatt and removed request for valentinewallaceAugust 1, 2025 13:28

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Have a Vec or VecDeque with capacity to keep track of the requests, truncate when limit is reached, and then do vec.iter().position(|v, _, _| v == r_id) for retrieval (which is kind of ugly and not O(1) like in hashmaps)

Sure, but for N=5 its probably faster than doing the SipHash-1-3 required to do that HashMap O(1), not to mention it avoids a second allocated datastructure. Let's definitely not overengineer this - walking a Vec is totally fine for N=5.

Client will no longer use a time provider to expire pending requests.
Instead, it will just keep the X most recent requests (coming in next commit).
@martinsaposnic

Copy link
Copy Markdown
ContributorAuthor

Have a Vec or VecDeque with capacity to keep track of the requests, truncate when limit is reached, and then do vec.iter().position(|v, _, _| v == r_id) for retrieval (which is kind of ugly and not O(1) like in hashmaps)

Sure, but for N=5 its probably faster than doing the SipHash-1-3 required to do that HashMap O(1), not to mention it avoids a second allocated datastructure. Let's definitely not overengineer this - walking a Vec is totally fine for N=5.

thanks for reviewing @TheBlueMatt

just pushed fixup commit ec6a9f0 where I drop the bounded_map and change everything so it uses vecs for keeping track of the client's pending requests. let me know what you think

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this LGTM, feel free to squash if @tnull is fine with dropping the fancy map.

@tnulltnull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fixup LGTM, please squash so we can get this landed.

@martinsaposnic
martinsaposnicforce-pushed the simplify-lsps5-client-peer-state branch from ec6a9f0 to cf23e91CompareAugust 5, 2025 10:54
@martinsaposnic

Copy link
Copy Markdown
ContributorAuthor

thanks for taking a look @tnull. fixup squashed!

offtopic question about process:

I'm not sure if there's an easy way to verify that the squash was actually just a squash and nothing more. for example, I just split the fixup into the last two commits and reworded the second one. I did not change anything else, but there's no easy way for you to quickly confirm that I didn't accidentally change something else

@tnull

tnull commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

I'm not sure if there's an easy way to verify that the squash was actually just a squash and nothing more. for example, I just split the fixup into the last two commits and reworded the second one. I did not change anything else, but there's no easy way for you to quickly confirm that I didn't accidentally change something else

For one you can hit the 'Compare' button above to see the diff between pushes. I also always check out the working branch locally to review, so can simply do a diff to see if/what changed. As a bonus you can use range-diff to review the diff even after a rebase.

@tnulltnull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Going ahead landing this given that the current state had two 'LGTM' before

@tnull
tnull merged commit d704eb8 into lightningdevkit:mainAug 5, 2025
23 of 24 checks passed
Sign up for freeto 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.

4 participants

@martinsaposnic@ldk-reviews-bot@tnull@TheBlueMatt