Add feerate and balances to `LatestCounterpartyCommitmentTXInfo`
authorAlec Chen <alecchendev@gmail.com>
Wed, 14 Jun 2023 20:14:14 +0000 (15:14 -0500)
committerAlec Chen <alecchendev@gmail.com>
Wed, 23 Aug 2023 15:48:19 +0000 (10:48 -0500)
This adds the feerate and local and remote output values to this channel
monitor update step so that a monitor can reconstruct the counterparty's
commitment transaction from an update. These commitment transactions
will be exposed to users in the following commits to support third-party
watchtowers in the persistence pipeline.

With only the HTLC outputs currently available in the monitor update, we
can tell how much of the channel balance is in-flight and towards which
side, however it doesn't tell us the amount that resides on either side.
Because of dust, we can't reliably derive the remote value from the
local value and visa versa. Thus, it seems these are the minimum fields
that need to be added.

lightning/src/chain/channelmonitor.rs
lightning/src/ln/channel.rs

index 4d3b4d53ad75c0ce69959c0041a68f7b92eed3c5..56228906ede3ed68916916ceaec9ce0f5bb6268c 100644 (file)
@@ -502,6 +502,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
                htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
                commitment_number: u64,
                their_per_commitment_point: PublicKey,
+               feerate_per_kw: Option<u32>,
+               to_broadcaster_value_sat: Option<u64>,
+               to_countersignatory_value_sat: Option<u64>,
        },
        PaymentPreimage {
                payment_preimage: PaymentPreimage,
@@ -544,8 +547,11 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
        },
        (1, LatestCounterpartyCommitmentTXInfo) => {
                (0, commitment_txid, required),
+               (1, feerate_per_kw, option),
                (2, commitment_number, required),
+               (3, to_broadcaster_value_sat, option),
                (4, their_per_commitment_point, required),
+               (5, to_countersignatory_value_sat, option),
                (6, htlc_outputs, required_vec),
        },
        (2, PaymentPreimage) => {
@@ -2471,7 +2477,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                                ret = Err(());
                                        }
                                }
-                               ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point } => {
+                               ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
                                        log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
                                        self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_per_commitment_point, logger)
                                },
index f66f48c50499daf83bc4b1ab4669ff9ddc76078c..0dcb56ff65c67f5644c197bb68dd889fd3fbdc23 100644 (file)
@@ -5289,7 +5289,9 @@ impl<SP: Deref> Channel<SP> where
                }
                self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
 
-               let (counterparty_commitment_txid, mut htlcs_ref) = self.build_commitment_no_state_update(logger);
+               let (mut htlcs_ref, counterparty_commitment_tx) =
+                       self.build_commitment_no_state_update(logger);
+               let counterparty_commitment_txid = counterparty_commitment_tx.trust().txid();
                let htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
                        htlcs_ref.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
 
@@ -5304,17 +5306,23 @@ impl<SP: Deref> Channel<SP> where
                                commitment_txid: counterparty_commitment_txid,
                                htlc_outputs: htlcs.clone(),
                                commitment_number: self.context.cur_counterparty_commitment_transaction_number,
-                               their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap()
+                               their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap(),
+                               feerate_per_kw: Some(counterparty_commitment_tx.feerate_per_kw()),
+                               to_broadcaster_value_sat: Some(counterparty_commitment_tx.to_broadcaster_value_sat()),
+                               to_countersignatory_value_sat: Some(counterparty_commitment_tx.to_countersignatory_value_sat()),
                        }]
                };
                self.context.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
                monitor_update
        }
 
-       fn build_commitment_no_state_update<L: Deref>(&self, logger: &L) -> (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) where L::Target: Logger {
+       fn build_commitment_no_state_update<L: Deref>(&self, logger: &L)
+       -> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
+       where L::Target: Logger
+       {
                let counterparty_keys = self.context.build_remote_transaction_keys();
                let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
-               let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
+               let counterparty_commitment_tx = commitment_stats.tx;
 
                #[cfg(any(test, fuzzing))]
                {
@@ -5334,7 +5342,7 @@ impl<SP: Deref> Channel<SP> where
                        }
                }
 
-               (counterparty_commitment_txid, commitment_stats.htlcs_included)
+               (commitment_stats.htlcs_included, counterparty_commitment_tx)
        }
 
        /// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed