Skip to content

Commit a079946

Browse files
committed
Drop Clone requirement from Sign
Now that we opt to always re-derive channel secrets whenever required, we can drop the Clone requirement from Sign.
1 parent 531bda9 commit a079946

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,14 @@ pub trait BaseSign {
382382
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters);
383383
}
384384

385-
/// A cloneable signer.
385+
/// A writeable signer.
386386
///
387-
/// Although we require signers to be cloneable, it may be useful for developers to be able to use
388-
/// signers in an un-sized way, for example as `dyn BaseSign`. Therefore we separate the Clone trait,
389-
/// which implies Sized, into this derived trait.
390-
pub trait Sign: BaseSign + Writeable + Clone {
387+
/// There will always be two instances of a signer per channel, one occupied by the
388+
/// [`ChannelManager`] and another by the channel's [`ChannelMonitor`].
389+
///
390+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
391+
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
392+
pub trait Sign: BaseSign + Writeable {
391393
}
392394

393395
/// Specifies the recipient of an invoice, to indicate to [`KeysInterface::sign_invoice`] what node

lightning/src/ln/channel.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,13 @@ impl<Signer: Sign> Channel<Signer> {
21982198
&self.get_counterparty_pubkeys().funding_pubkey
21992199
}
22002200

2201-
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>, Option<msgs::ChannelReady>), ChannelError> where L::Target: Logger {
2201+
pub fn funding_created<K: Deref, L: Deref>(
2202+
&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, keys_source: &K, logger: &L
2203+
) -> Result<(msgs::FundingSigned, ChannelMonitor<<K::Target as KeysInterface>::Signer>, Option<msgs::ChannelReady>), ChannelError>
2204+
where
2205+
K::Target: KeysInterface,
2206+
L::Target: Logger
2207+
{
22022208
if self.is_outbound() {
22032209
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
22042210
}
@@ -2253,7 +2259,9 @@ impl<Signer: Sign> Channel<Signer> {
22532259
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
22542260
let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
22552261
let shutdown_script = self.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
2256-
let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
2262+
let mut monitor_signer = keys_source.derive_channel_signer(self.channel_value_satoshis, self.channel_keys_id);
2263+
monitor_signer.provide_channel_parameters(&self.channel_transaction_parameters);
2264+
let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), monitor_signer,
22572265
shutdown_script, self.get_holder_selected_contest_delay(),
22582266
&self.destination_script, (funding_txo, funding_txo_script.clone()),
22592267
&self.channel_transaction_parameters,
@@ -2278,7 +2286,13 @@ impl<Signer: Sign> Channel<Signer> {
22782286

22792287
/// Handles a funding_signed message from the remote end.
22802288
/// If this call is successful, broadcast the funding transaction (and not before!)
2281-
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, logger: &L) -> Result<(ChannelMonitor<Signer>, Transaction, Option<msgs::ChannelReady>), ChannelError> where L::Target: Logger {
2289+
pub fn funding_signed<K: Deref, L: Deref>(
2290+
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, keys_source: &K, logger: &L
2291+
) -> Result<(ChannelMonitor<<K::Target as KeysInterface>::Signer>, Transaction, Option<msgs::ChannelReady>), ChannelError>
2292+
where
2293+
K::Target: KeysInterface,
2294+
L::Target: Logger
2295+
{
22822296
if !self.is_outbound() {
22832297
return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
22842298
}
@@ -2330,7 +2344,9 @@ impl<Signer: Sign> Channel<Signer> {
23302344
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
23312345
let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.get_holder_pubkeys().payment_point, &self.get_counterparty_pubkeys().payment_point, self.is_outbound());
23322346
let shutdown_script = self.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
2333-
let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), self.holder_signer.clone(),
2347+
let mut monitor_signer = keys_source.derive_channel_signer(self.channel_value_satoshis, self.channel_keys_id);
2348+
monitor_signer.provide_channel_parameters(&self.channel_transaction_parameters);
2349+
let channel_monitor = ChannelMonitor::new(self.secp_ctx.clone(), monitor_signer,
23342350
shutdown_script, self.get_holder_selected_contest_delay(),
23352351
&self.destination_script, (funding_txo, funding_txo_script),
23362352
&self.channel_transaction_parameters,
@@ -7058,10 +7074,10 @@ mod tests {
70587074
}]};
70597075
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
70607076
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
7061-
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
7077+
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).unwrap();
70627078

70637079
// Node B --> Node A: funding signed
7064-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&logger);
7080+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger);
70657081

70667082
// Now disconnect the two nodes and check that the commitment point in
70677083
// Node B's channel_reestablish message is sane.

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4801,7 +4801,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48014801
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
48024802
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
48034803
}
4804-
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.logger), chan), chan.remove())
4804+
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.keys_manager, &self.logger), chan), chan.remove())
48054805
},
48064806
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
48074807
}
@@ -4872,7 +4872,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48724872
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
48734873
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
48744874
}
4875-
let (monitor, funding_tx, channel_ready) = match chan.get_mut().funding_signed(&msg, best_block, &self.logger) {
4875+
let (monitor, funding_tx, channel_ready) = match chan.get_mut().funding_signed(&msg, best_block, &self.keys_manager, &self.logger) {
48764876
Ok(update) => update,
48774877
Err(e) => try_chan_entry!(self, Err(e), chan),
48784878
};

0 commit comments

Comments
 (0)