Skip to content

Commit 76786f5

Browse files
committed
De-Option<> some fields in ChannelMonitor which are set at init
After we moved the ChannelMonitor creation later during Channel init, we never went back and cleaned up ChannelMonitor to remove a number of now-useless Option<>s, so we do that now.
1 parent f5b0663 commit 76786f5

File tree

5 files changed

+53
-88
lines changed

5 files changed

+53
-88
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
22752275
};
22762276
// Because we have exclusive ownership of the channel here we can release the channel_state
22772277
// lock before add_monitor
2278-
if let Err(e) = self.monitor.add_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
2278+
if let Err(e) = self.monitor.add_monitor(monitor_update.get_funding_txo(), monitor_update) {
22792279
match e {
22802280
ChannelMonitorUpdateErr::PermanentFailure => {
22812281
// Note that we reply with the new channel_id in error messages if we gave up on the

lightning/src/ln/channelmonitor.rs

Lines changed: 41 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,9 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
290290
hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")),
291291
hash_map::Entry::Vacant(e) => e,
292292
};
293-
match monitor.funding_info {
294-
None => {
295-
return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !"));
296-
},
297-
Some((ref outpoint, ref script)) => {
298-
log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
299-
self.chain_monitor.install_watch_tx(&outpoint.txid, script);
300-
self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
301-
},
302-
}
293+
log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(monitor.funding_info.0.to_channel_id()[..]));
294+
self.chain_monitor.install_watch_tx(&monitor.funding_info.0.txid, &monitor.funding_info.1);
295+
self.chain_monitor.install_watch_outpoint((monitor.funding_info.0.txid, monitor.funding_info.0.index as u32), &monitor.funding_info.1);
303296
for (txid, outputs) in monitor.get_outputs_to_watch().iter() {
304297
for (idx, script) in outputs.iter().enumerate() {
305298
self.chain_monitor.install_watch_outpoint((*txid, idx as u32), script);
@@ -721,19 +714,19 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
721714
shutdown_script: Script,
722715

723716
keys: ChanSigner,
724-
funding_info: Option<(OutPoint, Script)>,
717+
funding_info: (OutPoint, Script),
725718
current_remote_commitment_txid: Option<Sha256dHash>,
726719
prev_remote_commitment_txid: Option<Sha256dHash>,
727720

728-
their_htlc_base_key: Option<PublicKey>,
729-
their_delayed_payment_base_key: Option<PublicKey>,
730-
funding_redeemscript: Option<Script>,
731-
channel_value_satoshis: Option<u64>,
721+
their_htlc_base_key: PublicKey,
722+
their_delayed_payment_base_key: PublicKey,
723+
funding_redeemscript: Script,
724+
channel_value_satoshis: u64,
732725
// first is the idx of the first of the two revocation points
733726
their_cur_revocation_points: Option<(u64, PublicKey, Option<PublicKey>)>,
734727

735728
our_to_self_delay: u16,
736-
their_to_self_delay: Option<u16>,
729+
their_to_self_delay: u16,
737730

738731
commitment_secrets: CounterpartyCommitmentSecrets,
739732
remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
@@ -872,23 +865,16 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
872865
self.shutdown_script.write(writer)?;
873866

874867
self.keys.write(writer)?;
875-
match self.funding_info {
876-
Some((ref outpoint, ref script)) => {
877-
writer.write_all(&outpoint.txid[..])?;
878-
writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
879-
script.write(writer)?;
880-
},
881-
None => {
882-
debug_assert!(false, "Try to serialize a useless Local monitor !");
883-
},
884-
}
868+
writer.write_all(&self.funding_info.0.txid[..])?;
869+
writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
870+
self.funding_info.1.write(writer)?;
885871
self.current_remote_commitment_txid.write(writer)?;
886872
self.prev_remote_commitment_txid.write(writer)?;
887873

888-
writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
889-
writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
890-
self.funding_redeemscript.as_ref().unwrap().write(writer)?;
891-
self.channel_value_satoshis.unwrap().write(writer)?;
874+
writer.write_all(&self.their_htlc_base_key.serialize())?;
875+
writer.write_all(&self.their_delayed_payment_base_key.serialize())?;
876+
self.funding_redeemscript.write(writer)?;
877+
self.channel_value_satoshis.write(writer)?;
892878

893879
match self.their_cur_revocation_points {
894880
Some((idx, pubkey, second_option)) => {
@@ -909,7 +895,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
909895
}
910896

911897
writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
912-
writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
898+
writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay))?;
913899

914900
self.commitment_secrets.write(writer)?;
915901

@@ -1099,18 +1085,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10991085
shutdown_script,
11001086

11011087
keys: keys.clone(),
1102-
funding_info: Some(funding_info),
1088+
funding_info,
11031089
current_remote_commitment_txid: None,
11041090
prev_remote_commitment_txid: None,
11051091

1106-
their_htlc_base_key: Some(their_htlc_base_key.clone()),
1107-
their_delayed_payment_base_key: Some(their_delayed_payment_base_key.clone()),
1108-
funding_redeemscript: Some(funding_redeemscript.clone()),
1109-
channel_value_satoshis: Some(channel_value_satoshis),
1092+
their_htlc_base_key: their_htlc_base_key.clone(),
1093+
their_delayed_payment_base_key: their_delayed_payment_base_key.clone(),
1094+
funding_redeemscript: funding_redeemscript.clone(),
1095+
channel_value_satoshis: channel_value_satoshis,
11101096
their_cur_revocation_points: None,
11111097

1112-
our_to_self_delay: our_to_self_delay,
1113-
their_to_self_delay: Some(their_to_self_delay),
1098+
our_to_self_delay,
1099+
their_to_self_delay,
11141100

11151101
commitment_secrets: CounterpartyCommitmentSecrets::new(),
11161102
remote_claimable_outpoints: HashMap::new(),
@@ -1248,9 +1234,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12481234
/// up-to-date as our local commitment transaction is updated.
12491235
/// Panics if set_their_to_self_delay has never been called.
12501236
pub(super) fn provide_latest_local_commitment_tx_info(&mut self, mut commitment_tx: LocalCommitmentTransaction, local_keys: chan_utils::TxCreationKeys, feerate_per_kw: u64, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>) -> Result<(), MonitorUpdateError> {
1251-
if self.their_to_self_delay.is_none() {
1252-
return Err(MonitorUpdateError("Got a local commitment tx info update before we'd set basic information about the channel"));
1253-
}
12541237
let txid = commitment_tx.txid();
12551238
let sequence = commitment_tx.without_valid_witness().input[0].sequence as u64;
12561239
let locktime = commitment_tx.without_valid_witness().lock_time as u64;
@@ -1366,11 +1349,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13661349
}
13671350

13681351
/// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
1369-
pub fn get_funding_txo(&self) -> Option<OutPoint> {
1370-
if let Some((outp, _)) = self.funding_info {
1371-
return Some(outp)
1372-
}
1373-
None
1352+
pub fn get_funding_txo(&self) -> OutPoint {
1353+
self.funding_info.0
13741354
}
13751355

13761356
/// Gets a list of txids, with their output scripts (in the order they appear in the
@@ -1463,11 +1443,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14631443
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.keys.revocation_base_key()));
14641444
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().htlc_basepoint));
14651445
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &self.keys.payment_base_key()));
1466-
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key.unwrap()));
1467-
let a_htlc_key = match self.their_htlc_base_key {
1468-
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
1469-
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &their_htlc_base_key)),
1470-
};
1446+
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key));
1447+
let a_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_htlc_base_key));
14711448

14721449
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
14731450
let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
@@ -1618,10 +1595,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16181595
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().revocation_basepoint));
16191596
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.keys.pubkeys().htlc_basepoint));
16201597
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.htlc_base_key()));
1621-
let a_htlc_key = match self.their_htlc_base_key {
1622-
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
1623-
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
1624-
};
1598+
let a_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.their_htlc_base_key));
16251599
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.keys.payment_base_key()));
16261600

16271601
self.broadcasted_remote_payment_script = {
@@ -1675,10 +1649,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16751649
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
16761650
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.keys.pubkeys().revocation_basepoint));
16771651
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.keys.revocation_base_key()));
1678-
let delayed_key = match self.their_delayed_payment_base_key {
1679-
None => return (Vec::new(), None),
1680-
Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
1681-
};
1652+
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.their_delayed_payment_base_key));
16821653
let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
16831654

16841655
log_trace!(self, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
@@ -1691,7 +1662,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16911662
let mut claim_requests = Vec::with_capacity(local_tx.htlc_outputs.len());
16921663
let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
16931664

1694-
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
1665+
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay, &local_tx.delayed_payment_key);
16951666
let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, self.keys.delayed_payment_base_key()) {
16961667
Some((redeemscript.to_v0_p2wsh(), local_delayedkey, redeemscript))
16971668
} else { None };
@@ -1873,8 +1844,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18731844
// which is an easy way to filter out any potential non-matching txn for lazy
18741845
// filters.
18751846
let prevout = &tx.input[0].previous_output;
1876-
let funding_txo = self.funding_info.clone();
1877-
if funding_txo.is_none() || (prevout.txid == funding_txo.as_ref().unwrap().0.txid && prevout.vout == funding_txo.as_ref().unwrap().0.index as u32) {
1847+
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
18781848
if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
18791849
let (mut new_outpoints, new_outputs) = self.check_spend_remote_transaction(&tx, height);
18801850
if !new_outputs.1.is_empty() {
@@ -1910,7 +1880,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19101880
self.would_broadcast_at_height(height)
19111881
} else { false };
19121882
if should_broadcast {
1913-
claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, outpoint: BitcoinOutPoint { txid: self.funding_info.as_ref().unwrap().0.txid.clone(), vout: self.funding_info.as_ref().unwrap().0.index as u32 }, witness_data: InputMaterial::Funding { channel_value: self.channel_value_satoshis.unwrap() }});
1883+
claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, outpoint: BitcoinOutPoint { txid: self.funding_info.0.txid.clone(), vout: self.funding_info.0.index as u32 }, witness_data: InputMaterial::Funding { channel_value: self.channel_value_satoshis }});
19141884
}
19151885
if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
19161886
if should_broadcast {
@@ -2188,7 +2158,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
21882158
outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
21892159
key: broadcasted_local_revokable_script.1,
21902160
witness_script: broadcasted_local_revokable_script.2.clone(),
2191-
to_self_delay: self.their_to_self_delay.unwrap(),
2161+
to_self_delay: self.their_to_self_delay,
21922162
output: outp.clone(),
21932163
});
21942164
break;
@@ -2275,14 +2245,14 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
22752245
txid: Readable::read(reader)?,
22762246
index: Readable::read(reader)?,
22772247
};
2278-
let funding_info = Some((outpoint, Readable::read(reader)?));
2248+
let funding_info = (outpoint, Readable::read(reader)?);
22792249
let current_remote_commitment_txid = Readable::read(reader)?;
22802250
let prev_remote_commitment_txid = Readable::read(reader)?;
22812251

2282-
let their_htlc_base_key = Some(Readable::read(reader)?);
2283-
let their_delayed_payment_base_key = Some(Readable::read(reader)?);
2284-
let funding_redeemscript = Some(Readable::read(reader)?);
2285-
let channel_value_satoshis = Some(Readable::read(reader)?);
2252+
let their_htlc_base_key = Readable::read(reader)?;
2253+
let their_delayed_payment_base_key = Readable::read(reader)?;
2254+
let funding_redeemscript = Readable::read(reader)?;
2255+
let channel_value_satoshis = Readable::read(reader)?;
22862256

22872257
let their_cur_revocation_points = {
22882258
let first_idx = <U48 as Readable>::read(reader)?.0;
@@ -2300,7 +2270,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
23002270
};
23012271

23022272
let our_to_self_delay: u16 = Readable::read(reader)?;
2303-
let their_to_self_delay: Option<u16> = Some(Readable::read(reader)?);
2273+
let their_to_self_delay: u16 = Readable::read(reader)?;
23042274

23052275
let commitment_secrets = Readable::read(reader)?;
23062276

@@ -2643,9 +2613,7 @@ mod tests {
26432613
(OutPoint { txid: Sha256dHash::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
26442614
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
26452615
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2646-
0, Script::new(), 46, 0, logger.clone());
2647-
2648-
monitor.their_to_self_delay = Some(10);
2616+
10, Script::new(), 46, 0, logger.clone());
26492617

26502618
monitor.provide_latest_local_commitment_tx_info(LocalCommitmentTransaction::dummy(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10])).unwrap();
26512619
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
148148
{
149149
let mut channel_monitors = HashMap::new();
150150
for monitor in deserialized_monitors.iter_mut() {
151-
channel_monitors.insert(monitor.get_funding_txo().unwrap(), monitor);
151+
channel_monitors.insert(monitor.get_funding_txo(), monitor);
152152
}
153153

154154
let mut w = test_utils::TestVecWriter(Vec::new());
@@ -167,7 +167,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
167167
let chain_watch = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&self.logger) as Arc<Logger>));
168168
let channel_monitor = test_utils::TestChannelMonitor::new(chain_watch.clone(), self.tx_broadcaster.clone(), self.logger.clone(), &feeest);
169169
for deserialized_monitor in deserialized_monitors.drain(..) {
170-
if let Err(_) = channel_monitor.add_monitor(deserialized_monitor.get_funding_txo().unwrap(), deserialized_monitor) {
170+
if let Err(_) = channel_monitor.add_monitor(deserialized_monitor.get_funding_txo(), deserialized_monitor) {
171171
panic!();
172172
}
173173
}

0 commit comments

Comments
 (0)