Skip to content

Commit c25cd7c

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 f866a81 commit c25cd7c

File tree

2 files changed

+93
-90
lines changed

2 files changed

+93
-90
lines changed

lightning/src/ln/channel.rs

Lines changed: 59 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,7 @@ 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+
self.channel_monitor.as_mut().unwrap().last_block_hash = self.last_block_connected;
31663165
if self.funding_tx_confirmations > 0 {
31673166
if self.funding_tx_confirmations == self.minimum_depth as u64 {
31683167
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
@@ -3222,7 +3221,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32223221
self.funding_tx_confirmations = self.minimum_depth as u64 - 1;
32233222
}
32243223
self.last_block_connected = header.bitcoin_hash();
3225-
self.channel_monitor.last_block_hash = self.last_block_connected;
3224+
self.channel_monitor.as_mut().unwrap().last_block_hash = self.last_block_connected;
32263225
false
32273226
}
32283227

@@ -3336,16 +3335,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33363335
}
33373336
};
33383337

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));
33453338
let temporary_channel_id = self.channel_id;
33463339

33473340
// 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());
3341+
3342+
let their_pubkeys = self.their_pubkeys.as_ref().unwrap();
3343+
let funding_redeemscript = self.get_funding_redeemscript();
3344+
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
3345+
self.channel_monitor = Some(ChannelMonitor::new(self.local_keys.clone(),
3346+
&self.shutdown_pubkey, self.our_to_self_delay,
3347+
&self.destination_script, (funding_txo, funding_txo_script),
3348+
&their_pubkeys.htlc_basepoint, &their_pubkeys.delayed_payment_basepoint,
3349+
self.their_to_self_delay, funding_redeemscript, self.channel_value_satoshis,
3350+
self.get_commitment_transaction_number_obscure_factor(),
3351+
self.logger.clone()));
3352+
3353+
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());
33493354
self.channel_state = ChannelState::FundingCreated as u32;
33503355
self.channel_id = funding_txo.to_channel_id();
33513356
self.cur_remote_commitment_transaction_number -= 1;
@@ -3355,7 +3360,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33553360
funding_txid: funding_txo.txid,
33563361
funding_output_index: funding_txo.index,
33573362
signature: our_signature
3358-
}, self.channel_monitor.clone()))
3363+
}, self.channel_monitor.as_ref().unwrap().clone()))
33593364
}
33603365

33613366
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3600,7 +3605,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
36003605
their_revocation_point: self.their_cur_commitment_point.unwrap()
36013606
}]
36023607
};
3603-
self.channel_monitor.update_monitor_ooo(monitor_update.clone()).unwrap();
3608+
self.channel_monitor.as_mut().unwrap().update_monitor_ooo(monitor_update.clone()).unwrap();
36043609
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
36053610
Ok((res, monitor_update))
36063611
}
@@ -3744,7 +3749,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37443749

37453750
self.channel_state = ChannelState::ShutdownComplete as u32;
37463751
self.channel_update_count += 1;
3747-
(self.channel_monitor.get_latest_local_commitment_txn(), dropped_outbound_htlcs)
3752+
if self.channel_monitor.is_some() {
3753+
(self.channel_monitor.as_mut().unwrap().get_latest_local_commitment_txn(), dropped_outbound_htlcs)
3754+
} else {
3755+
// We aren't even signed funding yet, so can't broadcast anything
3756+
(Vec::new(), dropped_outbound_htlcs)
3757+
}
37483758
}
37493759
}
37503760

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

38043814
self.local_keys.write(writer)?;
38053815
self.shutdown_pubkey.write(writer)?;
3816+
self.destination_script.write(writer)?;
38063817

38073818
self.cur_local_commitment_transaction_number.write(writer)?;
38083819
self.cur_remote_commitment_transaction_number.write(writer)?;
@@ -3974,7 +3985,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
39743985

39753986
self.commitment_secrets.write(writer)?;
39763987

3977-
self.channel_monitor.write_for_disk(writer)?;
3988+
self.channel_monitor.as_ref().unwrap().write_for_disk(writer)?;
39783989
Ok(())
39793990
}
39803991
}
@@ -3999,6 +4010,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39994010

40004011
let local_keys = Readable::read(reader)?;
40014012
let shutdown_pubkey = Readable::read(reader)?;
4013+
let destination_script = Readable::read(reader)?;
40024014

40034015
let cur_local_commitment_transaction_number = Readable::read(reader)?;
40044016
let cur_remote_commitment_transaction_number = Readable::read(reader)?;
@@ -4149,6 +4161,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
41494161

41504162
local_keys,
41514163
shutdown_pubkey,
4164+
destination_script,
41524165

41534166
cur_local_commitment_transaction_number,
41544167
cur_remote_commitment_transaction_number,
@@ -4205,7 +4218,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
42054218

42064219
their_shutdown_scriptpubkey,
42074220

4208-
channel_monitor,
4221+
channel_monitor: Some(channel_monitor),
42094222
commitment_secrets,
42104223

42114224
network_sync: UpdateStatus::Fresh,

0 commit comments

Comments
 (0)