Skip to content

[RFC] Add ChannelMonitor::get_justice_txs for simplified watchtower integration#4453

Draft
FreeOnlineUser wants to merge 1 commit intolightningdevkit:mainfrom
FreeOnlineUser:watchtower-justice-api
Draft

[RFC] Add ChannelMonitor::get_justice_txs for simplified watchtower integration#4453
FreeOnlineUser wants to merge 1 commit intolightningdevkit:mainfrom
FreeOnlineUser:watchtower-justice-api

Conversation

@FreeOnlineUser
Copy link

Draft PR for design feedback. Implements the approach @TheBlueMatt suggested in lightningdevkit/ldk-node#813 and in review of #2552: move justice tx state tracking inside ChannelMonitor so Persist implementors don't need external queues.

What this does

Adds a single method that replaces the current 3-step dance of counterparty_commitment_txs_from_update() + manual queue + sign_to_local_justice_tx():

pub struct JusticeTransaction {
    pub tx: Transaction,
    pub revoked_commitment_txid: Txid,
    pub commitment_number: u64,
}

impl ChannelMonitor {
    pub fn get_justice_txs(
        &self,
        feerate_per_kw: u64,
        destination_script: ScriptBuf,
    ) -> Vec<JusticeTransaction>;
}

Internally, ChannelMonitorImpl stores recent counterparty CommitmentTransactions (populated during update application, pruned to entries within one revocation of current). get_justice_txs checks which have revocation secrets available, builds and signs the justice tx, and returns the result.

Changes

  • channelmonitor.rs: new JusticeTransaction struct, latest_counterparty_commitment_txs field, TLV 39 serialization (optional, backwards-compatible), get_justice_txs() method
  • test_utils.rs: simplified WatchtowerPersister (removed JusticeTxData, unsigned_justice_tx_data queue, form_justice_data_from_commitment)
  • 127 additions, 83 deletions. All existing tests pass.

Splice handling

Pruning uses commitment numbers, not entry count. During a splice, multiple entries share the same commitment number (one per funding scope) and all are retained.

Backwards compatibility

  • TLV 39 is optional. Old monitors load with empty vec, populated on next update.
  • Old nodes reading monitors written by new code skip the unknown TLV field.
  • Existing counterparty_commitment_txs_from_update() and sign_to_local_justice_tx() APIs unchanged.

Design questions

Looking for input on these before moving out of draft:

  1. Signed vs unsigned return? Currently returns fully signed transactions. Returning unsigned gives callers more flexibility on fee choice at broadcast time. Preference?

  2. HTLC outputs? This only covers to_local justice. HTLC-timeout and HTLC-success outputs on revoked commitments are not included. Worth adding here, or separate follow-up?

  3. Feerate source? Caller provides feerate_per_kw. An alternative would be accepting a fee estimator. Any preference?

  4. Dust filtering? If the revokeable output is dust, revokeable_output_index() returns None and the entry is skipped silently. Right behavior, or surface this to the caller?

@ldk-reviews-bot
Copy link

👋 Hi! I see this is a draft PR.
I'll wait to assign reviewers until you mark it as ready for review.
Just convert it out of draft status when you're ready for review!

@codecov
Copy link

codecov bot commented Mar 1, 2026

Codecov Report

❌ Patch coverage is 92.15686% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.93%. Comparing base (ec03159) to head (034c377).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/chain/channelmonitor.rs 95.12% 0 Missing and 4 partials ⚠️
lightning/src/util/test_utils.rs 80.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4453      +/-   ##
==========================================
- Coverage   85.93%   85.93%   -0.01%     
==========================================
  Files         159      159              
  Lines      104693   104736      +43     
  Branches   104693   104736      +43     
==========================================
+ Hits        89972    90001      +29     
- Misses      12213    12222       +9     
- Partials     2508     2513       +5     
Flag Coverage Δ
tests 85.93% <92.15%> (-0.01%) ⬇️

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.

/// Contains the current and previous counterparty commitment(s). With splicing,
/// there may be multiple entries per commitment number (one per funding scope).
/// Pruned to remove entries more than one revocation old.
latest_counterparty_commitment_txs: Vec<CommitmentTransaction>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be in the funding scope to support splicing. Also should probably just be cur_... and prev_... to match existing API pattern rather than a vec that is max-size 2.

Copy link
Author

Choose a reason for hiding this comment

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

Moved to FundingScope as cur/prev fields. TLV 13/15 on FundingScope, 39/41 on the outer scope for backwards compat.

Err(_) => break,
}
}
// Use the simplified get_justice_txs API
Copy link
Collaborator

Choose a reason for hiding this comment

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

Claude loves to leave comments about how it changed the code that are totally useless - reading the code I don't care what changes claude made in the past, I care if there's something useful to know now. Probably can just drop this.

Copy link
Author

Choose a reason for hiding this comment

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

Removed, and noted for future.

///
/// Returns a list of [`JusticeTransaction`]s, each containing a fully signed
/// transaction and metadata about the revoked commitment it punishes.
pub fn get_justice_txs(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This API doesn't really make sense. There's no information on when I should call this or when the monitor "knows about" a revoked tx. We probably want to do soemthing like the old API where you can fetch revoked transactions in relation to a specific monitor update.

Copy link
Author

Choose a reason for hiding this comment

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

Replaced with sign_initial_justice_tx() for persist_new_channel and sign_justice_txs_from_update(update) for update_persisted_channel. Each is tied to a specific point in the persistence pipeline.

@FreeOnlineUser FreeOnlineUser force-pushed the watchtower-justice-api branch from d6c7903 to 9af37e1 Compare March 2, 2026 22:11
Adds sign_initial_justice_tx() and sign_justice_txs_from_update() to
ChannelMonitor, allowing Persist implementors to obtain signed justice
transactions without maintaining external state.

Storage uses cur/prev counterparty commitment fields on FundingScope,
matching the existing pattern and supporting splicing.

Simplifies WatchtowerPersister in test_utils by removing the manual
queue and signing logic.

Addresses feedback from lightningdevkit/ldk-node#813 and picks up
the intent of lightningdevkit#2552.
@FreeOnlineUser FreeOnlineUser force-pushed the watchtower-justice-api branch from 9af37e1 to 034c377 Compare March 2, 2026 23:38
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.

3 participants