Skip to content

Commit 7e11701

Browse files
committed
Track signing of local txn in channelmonitor and refuse updates
In e46e183 we began tracking whether a local commitment transaction had been signed and broadcast in OnchainTxHandler, refusing to update the local commitment transaction state in the ChannelMonitor on that basis. This is fine, except that it doesn't make a lot of sense to store the full local transaction state in OnchainTxHandler - we should be providing it the unsigned local transaction at the time we wish to broadcast and no more (just like we do all other transaction data).
1 parent 06fc27d commit 7e11701

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,16 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
768768
#[cfg(not(test))]
769769
onchain_tx_handler: OnchainTxHandler<ChanSigner>,
770770

771-
// Used to detect programming bug due to unsafe monitor update sequence { ChannelForceClosed, LatestLocalCommitmentTXInfo }
771+
// This is set when the Channel[Manager] generated a ChannelMonitorUpdate which indicated the
772+
// channel has been force-closed. After this is set, no further local commitment transaction
773+
// updates may occur, and we panic!() if one is provided.
772774
lockdown_from_offchain: bool,
773775

776+
// Set once we've signed a local commitment transaction and handed it over to our
777+
// OnchainTxHandler. After this is set, no future updates to our local commitment transactions
778+
// may occur, and we fail any such monitor updates.
779+
local_tx_signed: bool,
780+
774781
// We simply modify last_block_hash in Channel's block_connected so that serialization is
775782
// consistent but hopefully the users' copy handles block_connected in a consistent way.
776783
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
@@ -814,7 +821,9 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
814821
self.pending_htlcs_updated != other.pending_htlcs_updated ||
815822
self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
816823
self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf ||
817-
self.outputs_to_watch != other.outputs_to_watch
824+
self.outputs_to_watch != other.outputs_to_watch ||
825+
self.lockdown_from_offchain != other.lockdown_from_offchain ||
826+
self.local_tx_signed != other.local_tx_signed
818827
{
819828
false
820829
} else {
@@ -1015,6 +1024,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
10151024
self.onchain_tx_handler.write(writer)?;
10161025

10171026
self.lockdown_from_offchain.write(writer)?;
1027+
self.local_tx_signed.write(writer)?;
10181028

10191029
Ok(())
10201030
}
@@ -1097,6 +1107,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10971107
onchain_tx_handler,
10981108

10991109
lockdown_from_offchain: false,
1110+
local_tx_signed: false,
11001111

11011112
last_block_hash: Default::default(),
11021113
secp_ctx: Secp256k1::new(),
@@ -1213,6 +1224,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12131224
/// up-to-date as our local commitment transaction is updated.
12141225
/// Panics if set_their_to_self_delay has never been called.
12151226
pub(super) fn provide_latest_local_commitment_tx_info(&mut self, commitment_tx: LocalCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1227+
if self.local_tx_signed {
1228+
return Err(MonitorUpdateError("A local commitment tx has already been signed, no new local commitment txn can be sent to our counterparty"));
1229+
}
12161230
let txid = commitment_tx.txid();
12171231
let sequence = commitment_tx.without_valid_witness().input[0].sequence as u64;
12181232
let locktime = commitment_tx.without_valid_witness().lock_time as u64;
@@ -1740,6 +1754,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17401754
/// In any-case, choice is up to the user.
17411755
pub fn get_latest_local_commitment_txn(&mut self) -> Vec<Transaction> {
17421756
log_trace!(self, "Getting signed latest local commitment transaction!");
1757+
self.local_tx_signed = true;
17431758
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx() {
17441759
let txid = commitment_tx.txid();
17451760
let mut res = vec![commitment_tx];
@@ -2399,6 +2414,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
23992414
let onchain_tx_handler = ReadableArgs::read(reader, logger.clone())?;
24002415

24012416
let lockdown_from_offchain = Readable::read(reader)?;
2417+
let local_tx_signed = Readable::read(reader)?;
24022418

24032419
Ok((last_block_hash.clone(), ChannelMonitor {
24042420
latest_update_id,
@@ -2443,6 +2459,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
24432459
onchain_tx_handler,
24442460

24452461
lockdown_from_offchain,
2462+
local_tx_signed,
24462463

24472464
last_block_hash,
24482465
secp_ctx: Secp256k1::new(),

0 commit comments

Comments
 (0)