Skip to content

Commit 41785dc

Browse files
committed
Provide commitment point to monitor with the remote txn update
This extends 1b33064 by re-simplifying the ChannelMonitor <-> Channel interface a bit as we never have any use for the latest remote commitment point until we have knowledge of a remote transaction generated using it.
1 parent 3a7b40e commit 41785dc

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ impl Channel {
627627
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
628628
keys_provider.get_destination_script(), logger.clone());
629629
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
630-
channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
631630
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
632631

633632
let mut chan = Channel {
@@ -1348,7 +1347,6 @@ impl Channel {
13481347
}
13491348

13501349
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
1351-
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
13521350

13531351
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
13541352
self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
@@ -1424,7 +1422,7 @@ impl Channel {
14241422

14251423
// Now that we're past error-generating stuff, update our local state:
14261424

1427-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
1425+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
14281426
self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
14291427
self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
14301428
self.channel_state = ChannelState::FundingSent as u32;
@@ -1496,7 +1494,6 @@ impl Channel {
14961494
return Err(ChannelError::Close("Peer sent a funding_locked at a strange time"));
14971495
}
14981496

1499-
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER - 1 , msg.next_per_commitment_point)));
15001497
self.their_prev_commitment_point = self.their_cur_commitment_point;
15011498
self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
15021499
Ok(())
@@ -1877,7 +1874,6 @@ impl Channel {
18771874
}
18781875
}
18791876
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)?;
1880-
self.channel_monitor.provide_their_next_revocation_point(Some((self.cur_remote_commitment_transaction_number - 1, msg.next_per_commitment_point)));
18811877

18821878
// Update state now that we've passed all the can-fail calls...
18831879
// (note that we may still fail to generate the new commitment_signed message, but that's
@@ -2976,7 +2972,7 @@ impl Channel {
29762972
let temporary_channel_id = self.channel_id;
29772973

29782974
// Now that we're past error-generating stuff, update our local state:
2979-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
2975+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
29802976
self.channel_state = ChannelState::FundingCreated as u32;
29812977
self.channel_id = funding_txo.to_channel_id();
29822978
self.cur_remote_commitment_transaction_number -= 1;
@@ -3188,7 +3184,7 @@ impl Channel {
31883184
match self.send_commitment_no_state_update() {
31893185
Ok((res, remote_commitment_tx)) => {
31903186
// Update state now that we've passed all the can-fail calls...
3191-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
3187+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
31923188
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
31933189
Ok((res, self.channel_monitor.clone()))
31943190
},

src/ln/channelmonitor.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -453,37 +453,11 @@ impl ChannelMonitor {
453453
Ok(())
454454
}
455455

456-
/// Tracks the next revocation point which may be required to claim HTLC outputs which we know
457-
/// the preimage of in case the remote end force-closes using their latest state. When called at
458-
/// channel opening revocation point is the CURRENT one used for first commitment tx. Needed in case of sizeable push_msat.
459-
pub(super) fn provide_their_next_revocation_point(&mut self, their_next_revocation_point: Option<(u64, PublicKey)>) {
460-
if let Some(new_revocation_point) = their_next_revocation_point {
461-
match self.their_cur_revocation_points {
462-
Some(old_points) => {
463-
if old_points.0 == new_revocation_point.0 + 1 {
464-
self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(new_revocation_point.1)));
465-
} else if old_points.0 == new_revocation_point.0 + 2 {
466-
if let Some(old_second_point) = old_points.2 {
467-
self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(new_revocation_point.1)));
468-
} else {
469-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
470-
}
471-
} else {
472-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
473-
}
474-
},
475-
None => {
476-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
477-
}
478-
}
479-
}
480-
}
481-
482456
/// Informs this monitor of the latest remote (ie non-broadcastable) commitment transaction.
483457
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
484458
/// possibly future revocation/preimage information) to claim outputs where possible.
485459
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
486-
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<HTLCOutputInCommitment>, commitment_number: u64) {
460+
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<HTLCOutputInCommitment>, commitment_number: u64, their_revocation_point: PublicKey) {
487461
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
488462
// so that a remote monitor doesn't learn anything unless there is a malicious close.
489463
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -493,6 +467,25 @@ impl ChannelMonitor {
493467
}
494468
self.remote_claimable_outpoints.insert(unsigned_commitment_tx.txid(), htlc_outputs);
495469
self.current_remote_commitment_number = commitment_number;
470+
//TODO: Merge this into the other per-remote-transaction output storage stuff
471+
match self.their_cur_revocation_points {
472+
Some(old_points) => {
473+
if old_points.0 == commitment_number + 1 {
474+
self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(their_revocation_point)));
475+
} else if old_points.0 == commitment_number + 2 {
476+
if let Some(old_second_point) = old_points.2 {
477+
self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(their_revocation_point)));
478+
} else {
479+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
480+
}
481+
} else {
482+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
483+
}
484+
},
485+
None => {
486+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
487+
}
488+
}
496489
}
497490

498491
/// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
@@ -2153,10 +2146,10 @@ mod tests {
21532146
let logger = Arc::new(TestLogger::new());
21542147
let dummy_sig = Signature::from_der(&secp_ctx, &hex::decode("3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd").unwrap()[..]).unwrap();
21552148

2149+
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
21562150
macro_rules! dummy_keys {
21572151
() => {
21582152
{
2159-
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
21602153
TxCreationKeys {
21612154
per_commitment_point: dummy_key.clone(),
21622155
revocation_key: dummy_key.clone(),
@@ -2225,10 +2218,10 @@ mod tests {
22252218
monitor.set_their_to_self_delay(10);
22262219

22272220
monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
2228-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655);
2229-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654);
2230-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653);
2231-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652);
2221+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);
2222+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key);
2223+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key);
2224+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key);
22322225
for &(ref preimage, ref hash) in preimages.iter() {
22332226
monitor.provide_payment_preimage(hash, preimage);
22342227
}

0 commit comments

Comments
 (0)