Skip to content

Commit b70e716

Browse files
committed
Drop Clone from ChannelMonitor.
This removes the somewhat-easy-to-misuse Clone from ChannelMonitors, opening us up to being able to track Events in ChannelMonitors with less risk of misuse. Sadly it doesn't remove the Clone requirement for ChannelKeys, though gets us much closer - we now just need to request a second copy once when we go to create the ChannelMonitors.
1 parent 6612fa4 commit b70e716

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ pub trait KeysInterface: Send + Sync {
135135
/// (TODO: We shouldn't require that, and should have an API to get them at deser time, due mostly
136136
/// to the possibility of reentrancy issues by calling the user's code during our deserialization
137137
/// routine).
138-
/// TODO: remove Clone once we start returning ChannelUpdate objects instead of copying ChannelMonitor
138+
/// TODO: We should remove Clone by instead requesting a new ChannelKeys copy when we create
139+
/// ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors.
139140
pub trait ChannelKeys : Send+Clone {
140141
/// Gets the private key for the anchor tx
141142
fn funding_key<'a>(&'a self) -> &'a SecretKey;

lightning/src/ln/channel.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,16 +1481,25 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14811481
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
14821482
let funding_redeemscript = self.get_funding_redeemscript();
14831483
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
1484-
self.channel_monitor = Some(ChannelMonitor::new(self.local_keys.clone(),
1485-
&self.shutdown_pubkey, self.our_to_self_delay,
1486-
&self.destination_script, (funding_txo, funding_txo_script),
1487-
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
1488-
self.their_to_self_delay, funding_redeemscript, self.channel_value_satoshis,
1489-
self.get_commitment_transaction_number_obscure_factor(),
1490-
self.logger.clone()));
1491-
1492-
self.channel_monitor.as_mut().unwrap().provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
1493-
self.channel_monitor.as_mut().unwrap().provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new()).unwrap();
1484+
macro_rules! create_monitor {
1485+
() => { {
1486+
let mut channel_monitor = ChannelMonitor::new(self.local_keys.clone(),
1487+
&self.shutdown_pubkey, self.our_to_self_delay,
1488+
&self.destination_script, (funding_txo, funding_txo_script.clone()),
1489+
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
1490+
self.their_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
1491+
self.get_commitment_transaction_number_obscure_factor(),
1492+
self.logger.clone());
1493+
1494+
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());
1495+
channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys.clone(), self.feerate_per_kw, Vec::new()).unwrap();
1496+
channel_monitor
1497+
} }
1498+
}
1499+
1500+
self.channel_monitor = Some(create_monitor!());
1501+
let channel_monitor = create_monitor!();
1502+
14941503
self.channel_state = ChannelState::FundingSent as u32;
14951504
self.channel_id = funding_txo.to_channel_id();
14961505
self.cur_remote_commitment_transaction_number -= 1;
@@ -1499,7 +1508,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14991508
Ok((msgs::FundingSigned {
15001509
channel_id: self.channel_id,
15011510
signature: our_signature
1502-
}, self.channel_monitor.as_ref().unwrap().clone()))
1511+
}, channel_monitor))
15031512
}
15041513

15051514
/// Handles a funding_signed message from the remote end.
@@ -3323,15 +3332,24 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33233332
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
33243333
let funding_redeemscript = self.get_funding_redeemscript();
33253334
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
3326-
self.channel_monitor = Some(ChannelMonitor::new(self.local_keys.clone(),
3327-
&self.shutdown_pubkey, self.our_to_self_delay,
3328-
&self.destination_script, (funding_txo, funding_txo_script),
3329-
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
3330-
self.their_to_self_delay, funding_redeemscript, self.channel_value_satoshis,
3331-
self.get_commitment_transaction_number_obscure_factor(),
3332-
self.logger.clone()));
3333-
3334-
self.channel_monitor.as_mut().unwrap().provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
3335+
macro_rules! create_monitor {
3336+
() => { {
3337+
let mut channel_monitor = ChannelMonitor::new(self.local_keys.clone(),
3338+
&self.shutdown_pubkey, self.our_to_self_delay,
3339+
&self.destination_script, (funding_txo, funding_txo_script.clone()),
3340+
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
3341+
self.their_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
3342+
self.get_commitment_transaction_number_obscure_factor(),
3343+
self.logger.clone());
3344+
3345+
channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
3346+
channel_monitor
3347+
} }
3348+
}
3349+
3350+
self.channel_monitor = Some(create_monitor!());
3351+
let channel_monitor = create_monitor!();
3352+
33353353
self.channel_state = ChannelState::FundingCreated as u32;
33363354
self.channel_id = funding_txo.to_channel_id();
33373355
self.cur_remote_commitment_transaction_number -= 1;
@@ -3341,7 +3359,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33413359
funding_txid: funding_txo.txid,
33423360
funding_output_index: funding_txo.index,
33433361
signature: our_signature
3344-
}, self.channel_monitor.as_ref().unwrap().clone()))
3362+
}, channel_monitor))
33453363
}
33463364

33473365
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our

lightning/src/ln/channelmonitor.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
381381
/// keeping bumping another claim tx to solve the outpoint.
382382
pub(crate) const ANTI_REORG_DELAY: u32 = 6;
383383

384-
#[derive(Clone)]
385384
enum Storage<ChanSigner: ChannelKeys> {
386385
Local {
387386
keys: ChanSigner,
@@ -773,7 +772,6 @@ impl<R: ::std::io::Read> Readable<R> for ChannelMonitorUpdateStep {
773772
///
774773
/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
775774
/// information and are actively monitoring the chain.
776-
#[derive(Clone)]
777775
pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
778776
latest_update_id: u64,
779777
commitment_transaction_number_obscure_factor: u64,

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6494,7 +6494,7 @@ fn test_data_loss_protect() {
64946494
}).unwrap().1
64956495
};
64966496
nodes[0].node = &node_state_0;
6497-
assert!(monitor.add_monitor(OutPoint { txid: chan.3.txid(), index: 0 }, chan_monitor.clone()).is_ok());
6497+
assert!(monitor.add_monitor(OutPoint { txid: chan.3.txid(), index: 0 }, chan_monitor).is_ok());
64986498
nodes[0].chan_monitor = &monitor;
64996499
nodes[0].chain_monitor = chain_monitor;
65006500

lightning/src/util/test_utils.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
6666
// to a watchtower and disk...
6767
let mut w = TestVecWriter(Vec::new());
6868
monitor.write_for_disk(&mut w).unwrap();
69-
assert!(<(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
70-
&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == monitor);
69+
let new_monitor = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
70+
&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1;
71+
assert!(new_monitor == monitor);
7172
w.0.clear();
7273
monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
73-
self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
7474
self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(), (funding_txo, monitor.get_latest_update_id()));
75-
assert!(self.simple_monitor.add_monitor(funding_txo, monitor).is_ok());
75+
self.added_monitors.lock().unwrap().push((funding_txo, monitor));
76+
assert!(self.simple_monitor.add_monitor(funding_txo, new_monitor).is_ok());
7677
self.update_ret.lock().unwrap().clone()
7778
}
7879

@@ -91,11 +92,12 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
9192
let monitor = monitors.get(&funding_txo).unwrap();
9293
w.0.clear();
9394
monitor.write_for_disk(&mut w).unwrap();
94-
assert!(<(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
95-
&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == *monitor);
95+
let new_monitor = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
96+
&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1;
97+
assert!(new_monitor == *monitor);
9698
w.0.clear();
9799
monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
98-
self.added_monitors.lock().unwrap().push((funding_txo, monitor.clone()));
100+
self.added_monitors.lock().unwrap().push((funding_txo, new_monitor));
99101
self.update_ret.lock().unwrap().clone()
100102
}
101103

0 commit comments

Comments
 (0)