Skip to content

Commit d498352

Browse files
committed
Set basic channel info in chanmon all at once, add a bit more info
1 parent b5de93b commit d498352

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
644644
}
645645

646646
let secp_ctx = Secp256k1::new();
647-
let mut channel_monitor = ChannelMonitor::new(chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
648-
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
649-
keys_provider.get_destination_script(), logger.clone());
650-
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
651-
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
647+
let channel_monitor = ChannelMonitor::new(chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
648+
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
649+
keys_provider.get_destination_script(), logger.clone());
652650

653651
let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
654652
match &msg.shutdown_scriptpubkey {
@@ -748,7 +746,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
748746
};
749747

750748
let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();
751-
chan.channel_monitor.set_commitment_obscure_factor(obscure_factor);
749+
let funding_redeemscript = chan.get_funding_redeemscript();
750+
chan.channel_monitor.set_basic_channel_info(&msg.htlc_basepoint, &msg.delayed_payment_basepoint, msg.to_self_delay, funding_redeemscript, msg.funding_satoshis, obscure_factor);
752751

753752
Ok(chan)
754753
}
@@ -1469,8 +1468,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14691468
}
14701469
} else { None };
14711470

1472-
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
1473-
14741471
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
14751472
self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
14761473
self.their_channel_reserve_satoshis = msg.channel_reserve_satoshis;
@@ -1487,8 +1484,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14871484
self.their_shutdown_scriptpubkey = their_shutdown_scriptpubkey;
14881485

14891486
let obscure_factor = self.get_commitment_transaction_number_obscure_factor();
1490-
self.channel_monitor.set_commitment_obscure_factor(obscure_factor);
1491-
self.channel_monitor.set_their_to_self_delay(msg.to_self_delay);
1487+
let funding_redeemscript = self.get_funding_redeemscript();
1488+
self.channel_monitor.set_basic_channel_info(&msg.htlc_basepoint, &msg.delayed_payment_basepoint, msg.to_self_delay, funding_redeemscript, self.channel_value_satoshis, obscure_factor);
14921489

14931490
self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
14941491

lightning/src/ln/channelmonitor.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ pub struct ChannelMonitor {
571571
key_storage: Storage,
572572
their_htlc_base_key: Option<PublicKey>,
573573
their_delayed_payment_base_key: Option<PublicKey>,
574+
funding_redeemscript: Option<Script>,
575+
channel_value_satoshis: Option<u64>,
574576
// first is the idx of the first of the two revocation points
575577
their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
576578

@@ -696,6 +698,8 @@ impl PartialEq for ChannelMonitor {
696698
self.key_storage != other.key_storage ||
697699
self.their_htlc_base_key != other.their_htlc_base_key ||
698700
self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
701+
self.funding_redeemscript != other.funding_redeemscript ||
702+
self.channel_value_satoshis != other.channel_value_satoshis ||
699703
self.their_cur_revocation_points != other.their_cur_revocation_points ||
700704
self.our_to_self_delay != other.our_to_self_delay ||
701705
self.their_to_self_delay != other.their_to_self_delay ||
@@ -743,6 +747,8 @@ impl ChannelMonitor {
743747
},
744748
their_htlc_base_key: None,
745749
their_delayed_payment_base_key: None,
750+
funding_redeemscript: None,
751+
channel_value_satoshis: None,
746752
their_cur_revocation_points: None,
747753

748754
our_to_self_delay: our_to_self_delay,
@@ -1052,12 +1058,6 @@ impl ChannelMonitor {
10521058
Ok(())
10531059
}
10541060

1055-
/// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
1056-
pub(super) fn set_commitment_obscure_factor(&mut self, commitment_transaction_number_obscure_factor: u64) {
1057-
assert!(commitment_transaction_number_obscure_factor < (1 << 48));
1058-
self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
1059-
}
1060-
10611061
/// Allows this monitor to scan only for transactions which are applicable. Note that this is
10621062
/// optional, without it this monitor cannot be used in an SPV client, but you may wish to
10631063
/// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
@@ -1076,13 +1076,15 @@ impl ChannelMonitor {
10761076
}
10771077

10781078
/// We log these base keys at channel opening to being able to rebuild redeemscript in case of leaked revoked commit tx
1079-
pub(super) fn set_their_base_keys(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey) {
1079+
/// Panics if commitment_transaction_number_obscure_factor doesn't fit in 48 bits
1080+
pub(super) fn set_basic_channel_info(&mut self, their_htlc_base_key: &PublicKey, their_delayed_payment_base_key: &PublicKey, their_to_self_delay: u16, funding_redeemscript: Script, channel_value_satoshis: u64, commitment_transaction_number_obscure_factor: u64) {
10801081
self.their_htlc_base_key = Some(their_htlc_base_key.clone());
10811082
self.their_delayed_payment_base_key = Some(their_delayed_payment_base_key.clone());
1082-
}
1083-
1084-
pub(super) fn set_their_to_self_delay(&mut self, their_to_self_delay: u16) {
10851083
self.their_to_self_delay = Some(their_to_self_delay);
1084+
self.funding_redeemscript = Some(funding_redeemscript);
1085+
self.channel_value_satoshis = Some(channel_value_satoshis);
1086+
assert!(commitment_transaction_number_obscure_factor < (1 << 48));
1087+
self.commitment_transaction_number_obscure_factor = commitment_transaction_number_obscure_factor;
10861088
}
10871089

10881090
pub(super) fn unset_funding_info(&mut self) {
@@ -1175,6 +1177,8 @@ impl ChannelMonitor {
11751177

11761178
writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
11771179
writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
1180+
self.funding_redeemscript.as_ref().unwrap().write(writer)?;
1181+
self.channel_value_satoshis.unwrap().write(writer)?;
11781182

11791183
match self.their_cur_revocation_points {
11801184
Some((idx, pubkey, second_option)) => {
@@ -2994,6 +2998,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
29942998

29952999
let their_htlc_base_key = Some(Readable::read(reader)?);
29963000
let their_delayed_payment_base_key = Some(Readable::read(reader)?);
3001+
let funding_redeemscript = Some(Readable::read(reader)?);
3002+
let channel_value_satoshis = Some(Readable::read(reader)?);
29973003

29983004
let their_cur_revocation_points = {
29993005
let first_idx = <U48 as Readable<R>>::read(reader)?.0;
@@ -3214,6 +3220,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
32143220
key_storage,
32153221
their_htlc_base_key,
32163222
their_delayed_payment_base_key,
3223+
funding_redeemscript,
3224+
channel_value_satoshis,
32173225
their_cur_revocation_points,
32183226

32193227
our_to_self_delay,
@@ -3695,7 +3703,7 @@ mod tests {
36953703
// Prune with one old state and a local commitment tx holding a few overlaps with the
36963704
// old state.
36973705
let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
3698-
monitor.set_their_to_self_delay(10);
3706+
monitor.their_to_self_delay = Some(10);
36993707

37003708
monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
37013709
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);

0 commit comments

Comments
 (0)