Prevent any update of local commitment transaction once signed
[rust-lightning] / lightning / src / ln / onchaintx.rs
index 4c935ea654843ab66f6e5a36f0794652e06d3c01..d0d1f29e691f5ea5118d0a06b2e5bf4030fd73aa 100644 (file)
@@ -725,8 +725,17 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                }
        }
 
-       pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) {
+       pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) -> Result<(), ()> {
+               // 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.
+               if let Some(ref local_commitment) = self.local_commitment {
+                       if local_commitment.has_local_sig() { return Err(()) }
+               }
                self.prev_local_commitment = self.local_commitment.take();
                self.local_commitment = Some(tx);
+               Ok(())
        }
 }