Skip to content

Commit 1a284b4

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 48883d5 commit 1a284b4

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
@@ -784,9 +784,16 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
784784
#[cfg(not(test))]
785785
onchain_tx_handler: OnchainTxHandler<ChanSigner>,
786786

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

792+
// Set once we've signed a local commitment transaction and handed it over to our
793+
// OnchainTxHandler. After this is set, no future updates to our local commitment transactions
794+
// may occur, and we fail any such monitor updates.
795+
local_tx_signed: bool,
796+
790797
// We simply modify last_block_hash in Channel's block_connected so that serialization is
791798
// consistent but hopefully the users' copy handles block_connected in a consistent way.
792799
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
@@ -830,7 +837,9 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
830837
self.pending_htlcs_updated != other.pending_htlcs_updated ||
831838
self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
832839
self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf ||
833-
self.outputs_to_watch != other.outputs_to_watch
840+
self.outputs_to_watch != other.outputs_to_watch ||
841+
self.lockdown_from_offchain != other.lockdown_from_offchain ||
842+
self.local_tx_signed != other.local_tx_signed
834843
{
835844
false
836845
} else {
@@ -1031,6 +1040,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
10311040
self.onchain_tx_handler.write(writer)?;
10321041

10331042
self.lockdown_from_offchain.write(writer)?;
1043+
self.local_tx_signed.write(writer)?;
10341044

10351045
Ok(())
10361046
}
@@ -1113,6 +1123,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11131123
onchain_tx_handler,
11141124

11151125
lockdown_from_offchain: false,
1126+
local_tx_signed: false,
11161127

11171128
last_block_hash: Default::default(),
11181129
secp_ctx: Secp256k1::new(),
@@ -1229,6 +1240,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12291240
/// up-to-date as our local commitment transaction is updated.
12301241
/// Panics if set_their_to_self_delay has never been called.
12311242
pub(super) fn provide_latest_local_commitment_tx_info(&mut self, commitment_tx: LocalCommitmentTransaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1243+
if self.local_tx_signed {
1244+
return Err(MonitorUpdateError("A local commitment tx has already been signed, no new local commitment txn can be sent to our counterparty"));
1245+
}
12321246
let txid = commitment_tx.txid();
12331247
let sequence = commitment_tx.without_valid_witness().input[0].sequence as u64;
12341248
let locktime = commitment_tx.without_valid_witness().lock_time as u64;
@@ -1756,6 +1770,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17561770
/// In any-case, choice is up to the user.
17571771
pub fn get_latest_local_commitment_txn(&mut self) -> Vec<Transaction> {
17581772
log_trace!(self, "Getting signed latest local commitment transaction!");
1773+
self.local_tx_signed = true;
17591774
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx() {
17601775
let txid = commitment_tx.txid();
17611776
let mut res = vec![commitment_tx];
@@ -2415,6 +2430,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
24152430
let onchain_tx_handler = ReadableArgs::read(reader, logger.clone())?;
24162431

24172432
let lockdown_from_offchain = Readable::read(reader)?;
2433+
let local_tx_signed = Readable::read(reader)?;
24182434

24192435
Ok((last_block_hash.clone(), ChannelMonitor {
24202436
latest_update_id,
@@ -2459,6 +2475,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
24592475
onchain_tx_handler,
24602476

24612477
lockdown_from_offchain,
2478+
local_tx_signed,
24622479

24632480
last_block_hash,
24642481
secp_ctx: Secp256k1::new(),

0 commit comments

Comments
 (0)