rust-lightning icon indicating copy to clipboard operation
rust-lightning copied to clipboard

[Memory] Persist `counterparty_claimable_outpoints` out of channel_monitor

Open G8XSU opened this issue 1 year ago • 2 comments

Problem: After every commitment signed, counterparty_claimable_outpoints keeps on growing without bounds within a channel_monitor, with a new hashmap entry for each commitment_tx.

It poses two problems mainly:

  1. Increased memory footprint, since all the active channel_monitors are stored in-memory.
  2. Increased channel_monitor on-disk size, where these are currently stored.

We don't want to keep on storing outpoints which will only be used for revoked_tx / funding_spend, instead we would like to store them in cold storage and read them only when required.

Ideal outcome: After doing this, Ldk's memory footprint should be drastically decreased owing to removed in-memory hashmap entries of counterparty_claimable_outpoints

  • [x] Outline approach [Doc]
  • [x] Minimize current set of non-current/previous_commitment reads to counterparty_claimable_outpoints by re-using existing get calls and refactors [#3057 ]
  • [x] Remove watched_outputs dependency on per_commitment_claimable_data. (because if they depend on claimable_data, we won't outputs untill user provides us with ClaimInfo, and they won't be returned from tx_confirmed) [#3081]
  • [ ] Introduce new event variants: PersistClaimInfo and RequestClaimInfo [#3067]
  • [ ] Add pub fn in chain-monitor that can be called after both of those events. [#3217]
  • [ ] Start publishing 2 new events, one in provide_secret and other in check_spend [#3106]
  • [ ] In case when no htlcs have been forwarded, counterparty_claimable_outpoints will not have ClaimInfo. We don't want to treat missing ClaimInfo as acceptable, hence we need to write empty ClaimInfo so that user can always provide us with Some() ClaimInfo after ClaimInfoRequest.
  • [x] Refactor check_spend_counterparty_transaction and cancel_prev_commitment_claims to act after we receive ClaimInfo
  • [x] Once funding_spent_seen and we receive ClaimInfo, Re-insert ClaimInfo in counterparty_claimable_outpoints, so that get_claimable_balances can account for it. (Done as part of handle events functions.)

G8XSU avatar May 07 '24 20:05 G8XSU

Outlined approach in https://github.com/orgs/lightningdevkit/discussions/3050

G8XSU avatar May 07 '24 21:05 G8XSU

++

  • [x] Remove watched_outputs dependency on per_commitment_claimable_data. (because if they depend on claimable_data, we won't outputs untill user provides us with ClaimInfo, and they won't be returned from tx_confirmed)
  • [x] In case when no htlcs have been forwarded, counterparty_claimable_outpoints will not have ClaimInfo. We don't want to treat missing ClaimInfo as acceptable, hence we need to write empty ClaimInfo so that user can always provide us with Some() ClaimInfo after ClaimInfoRequest.

G8XSU avatar May 17 '24 20:05 G8XSU