Skip to content

Commit 8a02797

Browse files
committed
Create ChannelMonitors with basic_channel_info and funding_info set
This removes most of the reliance on ChannelMonitor Clone, creating them in Channel only at the time when we need to start monitoring the chain.
1 parent 3279269 commit 8a02797

File tree

2 files changed

+97
-90
lines changed

2 files changed

+97
-90
lines changed

lightning/src/ln/channel.rs

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
250250
#[cfg(test)]
251251
pub(super) local_keys: ChanSigner,
252252
shutdown_pubkey: PublicKey,
253+
destination_script: Script,
253254

254255
// Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
255256
// generation start at 0 and count up...this simplifies some parts of implementation at the
@@ -354,7 +355,9 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
354355

355356
their_shutdown_scriptpubkey: Option<Script>,
356357

357-
channel_monitor: ChannelMonitor<ChanSigner>,
358+
/// Used exclusively to broadcast the latest local state, mostly a historical quirk that this
359+
/// is here:
360+
channel_monitor: Option<ChannelMonitor<ChanSigner>>,
358361
commitment_secrets: CounterpartyCommitmentSecrets,
359362

360363
network_sync: UpdateStatus,
@@ -459,26 +462,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
459462

460463
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
461464

462-
let secp_ctx = Secp256k1::new();
463-
let channel_monitor = ChannelMonitor::new(chan_keys.clone(),
464-
chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
465-
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
466-
keys_provider.get_destination_script(), logger.clone());
467-
468465
Ok(Channel {
469466
user_id: user_id,
470467
config: config.channel_options.clone(),
471468

472469
channel_id: keys_provider.get_channel_id(),
473470
channel_state: ChannelState::OurInitSent as u32,
474471
channel_outbound: true,
475-
secp_ctx: secp_ctx,
472+
secp_ctx: Secp256k1::new(),
476473
channel_value_satoshis: channel_value_satoshis,
477474

478475
latest_monitor_update_id: 0,
479476

480477
local_keys: chan_keys,
481478
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
479+
destination_script: keys_provider.get_destination_script(),
480+
482481
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
483482
cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
484483
value_to_self_msat: channel_value_satoshis * 1000 - push_msat,
@@ -533,7 +532,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
533532

534533
their_shutdown_scriptpubkey: None,
535534

536-
channel_monitor: channel_monitor,
535+
channel_monitor: None,
537536
commitment_secrets: CounterpartyCommitmentSecrets::new(),
538537

539538
network_sync: UpdateStatus::Fresh,
@@ -662,12 +661,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
662661
return Err(ChannelError::Close("Insufficient funding amount for initial commitment"));
663662
}
664663

665-
let secp_ctx = Secp256k1::new();
666-
let channel_monitor = ChannelMonitor::new(chan_keys.clone(),
667-
chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
668-
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
669-
keys_provider.get_destination_script(), logger.clone());
670-
671664
let their_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
672665
match &msg.shutdown_scriptpubkey {
673666
&OptionalField::Present(ref script) => {
@@ -696,12 +689,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
696689
channel_id: msg.temporary_channel_id,
697690
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
698691
channel_outbound: false,
699-
secp_ctx: secp_ctx,
692+
secp_ctx: Secp256k1::new(),
700693

701694
latest_monitor_update_id: 0,
702695

703696
local_keys: chan_keys,
704697
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
698+
destination_script: keys_provider.get_destination_script(),
699+
705700
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
706701
cur_remote_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
707702
value_to_self_msat: msg.push_msat,
@@ -757,7 +752,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
757752

758753
their_shutdown_scriptpubkey,
759754

760-
channel_monitor: channel_monitor,
755+
channel_monitor: None,
761756
commitment_secrets: CounterpartyCommitmentSecrets::new(),
762757

763758
network_sync: UpdateStatus::Fresh,
@@ -1196,7 +1191,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
11961191
payment_preimage: payment_preimage_arg.clone(),
11971192
}],
11981193
};
1199-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
1194+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
12001195

12011196
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateFailed as u32)) != 0 {
12021197
for pending_update in self.holding_cell_htlc_updates.iter() {
@@ -1491,17 +1486,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14911486
}
14921487
};
14931488

1489+
// Now that we're past error-generating stuff, update our local state:
1490+
14941491
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
14951492
let funding_redeemscript = self.get_funding_redeemscript();
1496-
self.channel_monitor.set_basic_channel_info(&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint, self.their_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis, self.get_commitment_transaction_number_obscure_factor());
1497-
14981493
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
1499-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
1500-
1501-
// Now that we're past error-generating stuff, update our local state:
1502-
1503-
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());
1504-
self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new()).unwrap();
1494+
self.channel_monitor = Some(ChannelMonitor::new(self.local_keys.clone(),
1495+
&self.shutdown_pubkey, self.our_to_self_delay,
1496+
&self.destination_script, (funding_txo, funding_txo_script),
1497+
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
1498+
self.their_to_self_delay, funding_redeemscript, self.channel_value_satoshis,
1499+
self.get_commitment_transaction_number_obscure_factor(),
1500+
self.logger.clone()));
1501+
1502+
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());
1503+
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();
15051504
self.channel_state = ChannelState::FundingSent as u32;
15061505
self.channel_id = funding_txo.to_channel_id();
15071506
self.cur_remote_commitment_transaction_number -= 1;
@@ -1510,7 +1509,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15101509
Ok((msgs::FundingSigned {
15111510
channel_id: self.channel_id,
15121511
signature: our_signature
1513-
}, self.channel_monitor.clone()))
1512+
}, self.channel_monitor.as_ref().unwrap().clone()))
15141513
}
15151514

15161515
/// Handles a funding_signed message from the remote end.
@@ -1549,7 +1548,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15491548
local_keys, feerate_per_kw: self.feerate_per_kw, htlc_outputs: Vec::new(),
15501549
}]
15511550
};
1552-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
1551+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
15531552
self.channel_state = ChannelState::FundingSent as u32 | (self.channel_state & (ChannelState::MonitorUpdateFailed as u32));
15541553
self.cur_local_commitment_transaction_number -= 1;
15551554

@@ -1860,7 +1859,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
18601859
local_keys, feerate_per_kw: self.feerate_per_kw, htlc_outputs: htlcs_and_sigs
18611860
}]
18621861
};
1863-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
1862+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
18641863

18651864
for htlc in self.pending_inbound_htlcs.iter_mut() {
18661865
let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
@@ -2093,7 +2092,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20932092
secret: msg.per_commitment_secret,
20942093
}],
20952094
};
2096-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
2095+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
20972096

20982097
// Update state now that we've passed all the can-fail calls...
20992098
// (note that we may still fail to generate the new commitment_signed message, but that's
@@ -2563,7 +2562,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
25632562
their_current_per_commitment_point: data_loss.my_current_per_commitment_point
25642563
}]
25652564
};
2566-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
2565+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
25672566
return Err(ChannelError::CloseDelayBroadcast {
25682567
msg: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting",
25692568
update: monitor_update
@@ -2913,7 +2912,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29132912
if self.channel_state < ChannelState::FundingCreated as u32 {
29142913
panic!("Can't get a channel monitor until funding has been created");
29152914
}
2916-
&mut self.channel_monitor
2915+
self.channel_monitor.as_mut().unwrap()
29172916
}
29182917

29192918
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
@@ -3162,7 +3161,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31623161
}
31633162
if header.bitcoin_hash() != self.last_block_connected {
31643163
self.last_block_connected = header.bitcoin_hash();
3165-
self.channel_monitor.last_block_hash = self.last_block_connected;
3164+
if let Some(channel_monitor) = self.channel_monitor.as_mut() {
3165+
channel_monitor.last_block_hash = self.last_block_connected;
3166+
}
31663167
if self.funding_tx_confirmations > 0 {
31673168
if self.funding_tx_confirmations == self.minimum_depth as u64 {
31683169
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
@@ -3222,7 +3223,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32223223
self.funding_tx_confirmations = self.minimum_depth as u64 - 1;
32233224
}
32243225
self.last_block_connected = header.bitcoin_hash();
3225-
self.channel_monitor.last_block_hash = self.last_block_connected;
3226+
if let Some(channel_monitor) = self.channel_monitor.as_mut() {
3227+
channel_monitor.last_block_hash = self.last_block_connected;
3228+
}
32263229
false
32273230
}
32283231

@@ -3336,16 +3339,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33363339
}
33373340
};
33383341

3339-
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
3340-
let funding_redeemscript = self.get_funding_redeemscript();
3341-
self.channel_monitor.set_basic_channel_info(&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint, self.their_to_self_delay, funding_redeemscript.clone(), self.channel_value_satoshis, self.get_commitment_transaction_number_obscure_factor());
3342-
3343-
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
3344-
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
33453342
let temporary_channel_id = self.channel_id;
33463343

33473344
// Now that we're past error-generating stuff, update our local state:
3348-
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());
3345+
3346+
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
3347+
let funding_redeemscript = self.get_funding_redeemscript();
3348+
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
3349+
self.channel_monitor = Some(ChannelMonitor::new(self.local_keys.clone(),
3350+
&self.shutdown_pubkey, self.our_to_self_delay,
3351+
&self.destination_script, (funding_txo, funding_txo_script),
3352+
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
3353+
self.their_to_self_delay, funding_redeemscript, self.channel_value_satoshis,
3354+
self.get_commitment_transaction_number_obscure_factor(),
3355+
self.logger.clone()));
3356+
3357+
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());
33493358
self.channel_state = ChannelState::FundingCreated as u32;
33503359
self.channel_id = funding_txo.to_channel_id();
33513360
self.cur_remote_commitment_transaction_number -= 1;
@@ -3355,7 +3364,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33553364
funding_txid: funding_txo.txid,
33563365
funding_output_index: funding_txo.index,
33573366
signature: our_signature
3358-
}, self.channel_monitor.clone()))
3367+
}, self.channel_monitor.as_ref().unwrap().clone()))
33593368
}
33603369

33613370
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3600,7 +3609,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
36003609
their_revocation_point: self.their_cur_commitment_point.unwrap()
36013610
}]
36023611
};
3603-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
3612+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
36043613
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
36053614
Ok((res, monitor_update))
36063615
}
@@ -3744,7 +3753,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37443753

37453754
self.channel_state = ChannelState::ShutdownComplete as u32;
37463755
self.channel_update_count += 1;
3747-
(self.channel_monitor.get_latest_local_commitment_txn(), dropped_outbound_htlcs)
3756+
if self.channel_monitor.is_some() {
3757+
(self.channel_monitor.as_mut().unwrap().get_latest_local_commitment_txn(), dropped_outbound_htlcs)
3758+
} else {
3759+
// We aren't even signed funding yet, so can't broadcast anything
3760+
(Vec::new(), dropped_outbound_htlcs)
3761+
}
37483762
}
37493763
}
37503764

@@ -3803,6 +3817,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
38033817

38043818
self.local_keys.write(writer)?;
38053819
self.shutdown_pubkey.write(writer)?;
3820+
self.destination_script.write(writer)?;
38063821

38073822
self.cur_local_commitment_transaction_number.write(writer)?;
38083823
self.cur_remote_commitment_transaction_number.write(writer)?;
@@ -3977,7 +3992,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
39773992

39783993
self.commitment_secrets.write(writer)?;
39793994

3980-
self.channel_monitor.write_for_disk(writer)?;
3995+
self.channel_monitor.as_ref().unwrap().write_for_disk(writer)?;
39813996
Ok(())
39823997
}
39833998
}
@@ -4002,6 +4017,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40024017

40034018
let local_keys = Readable::read(reader)?;
40044019
let shutdown_pubkey = Readable::read(reader)?;
4020+
let destination_script = Readable::read(reader)?;
40054021

40064022
let cur_local_commitment_transaction_number = Readable::read(reader)?;
40074023
let cur_remote_commitment_transaction_number = Readable::read(reader)?;
@@ -4152,6 +4168,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
41524168

41534169
local_keys,
41544170
shutdown_pubkey,
4171+
destination_script,
41554172

41564173
cur_local_commitment_transaction_number,
41574174
cur_remote_commitment_transaction_number,
@@ -4208,7 +4225,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
42084225

42094226
their_shutdown_scriptpubkey,
42104227

4211-
channel_monitor,
4228+
channel_monitor: Some(channel_monitor),
42124229
commitment_secrets,
42134230

42144231
network_sync: UpdateStatus::Fresh,

0 commit comments

Comments
 (0)