htlcswitch: batch preimage writes/consistency fix#2501
Conversation
ad5f3d5 to
d9ad7e6
Compare
70f2c6d to
c173aa0
Compare
|
@joostjager @halseth ready for another round of review. the changes sprawled quite a bit as you'll see. I plan to consolidate bc01a41 (the move to lntypes) and a5fc555 (first commit), but wanted to get your feedback on the additional commits before doing so. Along the way:
ptal |
joostjager
left a comment
There was a problem hiding this comment.
I think having those Hash and Preimage types is proving good (thanks @halseth). I like the strictness it introduces.
08a4cf0 to
f921e0a
Compare
|
@joostjager @halseth comments addressed and commits restructured, ptal |
f921e0a to
5f62111
Compare
5f62111 to
981eaac
Compare
|
@joostjager revamped the witness cache to only expose the specific interface, makes everything much cleaner imo, ptal |
e8f02a9 to
73a041a
Compare
|
note: planning to squash |
joostjager
left a comment
There was a problem hiding this comment.
Yes, could use some squashing indeed. PR is looking good.
halseth
left a comment
There was a problem hiding this comment.
Most changes look good in general, but I think this PR has sprawled a bit, which can be a bit dangerous when we are dealing with important stuff s.a. preimages 🙊
Would prefer splitting changes unrelated to the original change out 😄
re: squashing commits "bc01a41 (the move to lntypes) and a5fc555 (first commit)", I don't think that is necessary, as it is nice to separate commits that just moves/refactors code, from commits that actually changes behavior.
73a041a to
d062cbc
Compare
In this commit, we modify the WitnessCache's AddPreimage method to accept a variadic number of preimages. This enables callers to batch preimage writes in performance critical areas of the codebase, e.g. the htlcswitch. Additionally, we lift the computation of the witnesses' keys outside of the db transaction. This saves us from having to do hashing inside and blocking other callers, and limits extraneous blocking at the call site.
This commit makes use of the batched AddWitness method of the WitnessCache, in order to avoid performing one write for each accepted preimage. Additionally, this fixes an existing hole in the consistency guarantees since the batched writes are now guaranteed to take place before accepting the next CommitSig. Previously, these writes were processed in an unsynchronized go routine that could be delayed arbitrarily long before being executed. With this change, the async_payments_benchmarks actually shows a slight improvement in performance, presumably because we no longer do an individual write per preimage, even though the execution is now explicitly in the critical path. There is likely also a marginal performance improvement from the reduction in goroutine overhead.
d062cbc to
0a3e1cf
Compare
|
@halseth @joostjager i took out the commits that removed the preimage cache from lnwallet, should be a little more contained now. it still includes the commits that make the switch to |
halseth
left a comment
There was a problem hiding this comment.
Thanks, I'm much more comfortable with the current scope of the PR!
Gave it a thorough review, the commit structure made it a breeze. LGTM! 🥇
This PR adds batched writes for witnesses discovered in HTLC forwarding. At the same time, we correct a nuanced consistency issue related to a lack of synchronization with the channel state machine. Forcing the individual preimage writes to be synchronized with the link incurs a heavy performance penalty (about 80% from my measurements). Batching these allows us to minimize the number of db transactions required to write the preimages, allowing us to reinsert the batched write into the link's critical path and resolve the possible inconsistency. In fact, the benchmarks actually showed a slight performance improvement, even with the extra write in the critical path.
See commit messages for more details.