Skip to content

Commit e46e183

Browse files
author
Antoine Riard
committed
Prevent any update of local commitment transaction once signed
To prevent any unsafe state discrepancy between offchain and onchain, once local commitment transaction has been signed due to an event (either block height for HTLC-timeout or channel force-closure), don't allow any further update of local commitment transaction view to avoid delivery of revocation secret to counterparty for the aformentionned signed transaction.
1 parent 73e0a01 commit e46e183

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,18 @@ pub struct LocalCommitmentTransaction {
522522
impl LocalCommitmentTransaction {
523523
#[cfg(test)]
524524
pub fn dummy() -> Self {
525+
let dummy_input = TxIn {
526+
previous_output: OutPoint {
527+
txid: Default::default(),
528+
vout: 0,
529+
},
530+
script_sig: Default::default(),
531+
sequence: 0,
532+
witness: vec![vec![], vec![], vec![]]
533+
};
525534
Self { tx: Transaction {
526535
version: 2,
527-
input: Vec::new(),
536+
input: vec![dummy_input],
528537
output: Vec::new(),
529538
lock_time: 0,
530539
} }

lightning/src/ln/channelmonitor.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12411241
if self.their_to_self_delay.is_none() {
12421242
return Err(MonitorUpdateError("Got a local commitment tx info update before we'd set basic information about the channel"));
12431243
}
1244+
// Returning a monitor error before updating tracking points means in case of using
1245+
// a concurrent watchtower implementation for same channel, if this one doesn't
1246+
// reject update as we do, you MAY have the latest local valid commitment tx onchain
1247+
// for which you want to spend outputs. We're NOT robust again this scenario right
1248+
// now but we should consider it later.
1249+
if let Err(_) = self.onchain_tx_handler.provide_latest_local_tx(commitment_tx.clone()) {
1250+
return Err(MonitorUpdateError("Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed"));
1251+
}
12441252
self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
12451253
self.current_local_signed_commitment_tx = Some(LocalSignedTx {
12461254
txid: commitment_tx.txid(),
1247-
tx: commitment_tx.clone(),
1255+
tx: commitment_tx,
12481256
revocation_key: local_keys.revocation_key,
12491257
a_htlc_key: local_keys.a_htlc_key,
12501258
b_htlc_key: local_keys.b_htlc_key,
@@ -1253,7 +1261,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12531261
feerate_per_kw,
12541262
htlc_outputs,
12551263
});
1256-
self.onchain_tx_handler.provide_latest_local_tx(commitment_tx);
12571264
Ok(())
12581265
}
12591266

lightning/src/ln/onchaintx.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,17 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
725725
}
726726
}
727727

728-
pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) {
728+
pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) -> Result<(), ()> {
729+
// To prevent any unsafe state discrepancy between offchain and onchain, once local
730+
// commitment transaction has been signed due to an event (either block height for
731+
// HTLC-timeout or channel force-closure), don't allow any further update of local
732+
// commitment transaction view to avoid delivery of revocation secret to counterparty
733+
// for the aformentionned signed transaction.
734+
if let Some(ref local_commitment) = self.local_commitment {
735+
if local_commitment.has_local_sig() { return Err(()) }
736+
}
729737
self.prev_local_commitment = self.local_commitment.take();
730738
self.local_commitment = Some(tx);
739+
Ok(())
731740
}
732741
}

0 commit comments

Comments
 (0)