Skip to content

Commit 24682dc

Browse files
committed
Expose signing session from InteractivelyFunded
To support async signing, the InteractiveTxSigningSession returned when handling tx_complete needs to be saved in order to retry the funding_tx_constructed method when the signer is unblocked. This unfortunately means an Option as needed since the unfunded V2 channel phase variant is left unfunded until the signer completes.
1 parent 8b0f4b5 commit 24682dc

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,10 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
16821682

16831683
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor>;
16841684

1685+
fn interactive_tx_signing_session(&self) -> &Option<InteractiveTxSigningSession>;
1686+
1687+
fn interactive_tx_signing_session_mut(&mut self) -> &mut Option<InteractiveTxSigningSession>;
1688+
16851689
fn dual_funding_context(&self) -> &DualFundingChannelContext;
16861690

16871691
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
@@ -1755,17 +1759,25 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17551759
}
17561760

17571761
fn funding_tx_constructed<L: Deref>(
1758-
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
1762+
&mut self, logger: &L
17591763
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
17601764
where
17611765
L::Target: Logger
17621766
{
17631767
let our_funding_satoshis = self.dual_funding_context().our_funding_satoshis;
1764-
let context = self.context_mut();
1768+
let unsigned_tx = match self.interactive_tx_signing_session() {
1769+
Some(signing_session) => signing_session.unsigned_tx.clone(),
1770+
None => return Err(ChannelError::Close(
1771+
(
1772+
"No signing session available for commitment signing".to_owned(),
1773+
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
1774+
))),
1775+
};
17651776

1777+
let context = self.context_mut();
17661778
let mut output_index = None;
17671779
let expected_spk = context.get_funding_redeemscript().to_p2wsh();
1768-
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
1780+
for (idx, outp) in unsigned_tx.outputs().enumerate() {
17691781
if outp.script_pubkey() == &expected_spk && outp.value() == context.get_value_satoshis() {
17701782
if output_index.is_some() {
17711783
return Err(ChannelError::Close(
@@ -1778,7 +1790,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17781790
}
17791791
}
17801792
let outpoint = if let Some(output_index) = output_index {
1781-
OutPoint { txid: signing_session.unsigned_tx.txid(), index: output_index }
1793+
OutPoint { txid: unsigned_tx.txid(), index: output_index }
17821794
} else {
17831795
return Err(ChannelError::Close(
17841796
(
@@ -1792,15 +1804,19 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17921804
let commitment_signed = context.get_initial_commitment_signed(logger);
17931805
let commitment_signed = match commitment_signed {
17941806
Ok(commitment_signed) => {
1795-
context.funding_transaction = Some(signing_session.unsigned_tx.clone().into_unsigned_tx());
1807+
context.funding_transaction = Some(unsigned_tx.into_unsigned_tx());
17961808
commitment_signed
17971809
},
17981810
Err(err) => return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) }))),
17991811
};
18001812

18011813
let funding_ready_for_sig_event = None;
18021814
if our_funding_satoshis == 0 {
1803-
signing_session.provide_holder_witnesses(context.channel_id, Vec::new());
1815+
let channel_id = context.channel_id;
1816+
self.interactive_tx_signing_session_mut()
1817+
.as_mut()
1818+
.expect("interactive_tx_signing_session_mut should be set")
1819+
.provide_holder_witnesses(channel_id, Vec::new());
18041820
} else {
18051821
// TODO(dual_funding): Send event for signing if we've contributed funds.
18061822
// Inform the user that SIGHASH_ALL must be used for all signatures when contributing
@@ -1818,7 +1834,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
18181834
// </div>
18191835
}
18201836

1821-
context.channel_state = ChannelState::FundingNegotiated;
1837+
self.context_mut().channel_state = ChannelState::FundingNegotiated;
18221838

18231839
// Clear the interactive transaction constructor
18241840
self.interactive_tx_constructor_mut().take();
@@ -1840,6 +1856,12 @@ impl<SP: Deref> InteractivelyFunded<SP> for OutboundV2Channel<SP> where SP::Targ
18401856
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
18411857
&mut self.interactive_tx_constructor
18421858
}
1859+
fn interactive_tx_signing_session(&self) -> &Option<InteractiveTxSigningSession> {
1860+
&self.signing_session
1861+
}
1862+
fn interactive_tx_signing_session_mut(&mut self) -> &mut Option<InteractiveTxSigningSession> {
1863+
&mut self.signing_session
1864+
}
18431865
}
18441866

18451867
impl<SP: Deref> InteractivelyFunded<SP> for InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -1855,6 +1877,12 @@ impl<SP: Deref> InteractivelyFunded<SP> for InboundV2Channel<SP> where SP::Targe
18551877
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
18561878
&mut self.interactive_tx_constructor
18571879
}
1880+
fn interactive_tx_signing_session(&self) -> &Option<InteractiveTxSigningSession> {
1881+
&self.signing_session
1882+
}
1883+
fn interactive_tx_signing_session_mut(&mut self) -> &mut Option<InteractiveTxSigningSession> {
1884+
&mut self.signing_session
1885+
}
18581886
}
18591887

18601888
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
@@ -4028,9 +4056,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
40284056
}
40294057
}
40304058

4031-
fn get_initial_commitment_signed<L: Deref>(
4032-
&mut self, logger: &L
4033-
) -> Result<msgs::CommitmentSigned, ChannelError>
4059+
fn get_initial_commitment_signed<L: Deref>(&mut self, logger: &L) -> Result<msgs::CommitmentSigned, ChannelError>
40344060
where
40354061
SP::Target: SignerProvider,
40364062
L::Target: Logger
@@ -8719,6 +8745,7 @@ pub(super) struct OutboundV2Channel<SP: Deref> where SP::Target: SignerProvider
87198745
pub dual_funding_context: DualFundingChannelContext,
87208746
/// The current interactive transaction construction session under negotiation.
87218747
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8748+
signing_session: Option<InteractiveTxSigningSession>,
87228749
}
87238750

87248751
impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8778,6 +8805,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
87788805
our_funding_inputs: funding_inputs,
87798806
},
87808807
interactive_tx_constructor: None,
8808+
signing_session: None,
87818809
};
87828810
Ok(chan)
87838811
}
@@ -8845,10 +8873,11 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
88458873
}
88468874
}
88478875

8848-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
8876+
pub fn into_channel(self) -> Result<Channel<SP>, ChannelError>{
8877+
debug_assert!(self.signing_session.is_some());
88498878
let channel = Channel {
88508879
context: self.context,
8851-
interactive_tx_signing_session: Some(signing_session),
8880+
interactive_tx_signing_session: self.signing_session,
88528881
};
88538882

88548883
Ok(channel)
@@ -8862,6 +8891,7 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
88628891
pub dual_funding_context: DualFundingChannelContext,
88638892
/// The current interactive transaction construction session under negotiation.
88648893
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8894+
signing_session: Option<InteractiveTxSigningSession>,
88658895
}
88668896

88678897
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8964,6 +8994,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
89648994
dual_funding_context,
89658995
interactive_tx_constructor,
89668996
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
8997+
signing_session: None,
89678998
})
89688999
}
89699000

@@ -9039,10 +9070,11 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
90399070
self.generate_accept_channel_v2_message()
90409071
}
90419072

9042-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
9073+
pub fn into_channel(self) -> Result<Channel<SP>, ChannelError>{
9074+
debug_assert!(self.signing_session.is_some());
90439075
let channel = Channel {
90449076
context: self.context,
9045-
interactive_tx_signing_session: Some(signing_session),
9077+
interactive_tx_signing_session: self.signing_session,
90469078
};
90479079

90489080
Ok(channel)

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8306,22 +8306,25 @@ where
83068306
if let Some(msg_send_event) = msg_send_event_opt {
83078307
peer_state.pending_msg_events.push(msg_send_event);
83088308
};
8309-
if let Some(mut signing_session) = signing_session_opt {
8309+
if let Some(signing_session) = signing_session_opt {
83108310
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
83118311
ChannelPhase::UnfundedOutboundV2(chan) => {
8312-
chan.funding_tx_constructed(&mut signing_session, &self.logger)
8312+
*chan.interactive_tx_signing_session_mut() = Some(signing_session);
8313+
chan.funding_tx_constructed(&self.logger)
83138314
},
83148315
ChannelPhase::UnfundedInboundV2(chan) => {
8315-
chan.funding_tx_constructed(&mut signing_session, &self.logger)
8316+
*chan.interactive_tx_signing_session_mut() = Some(signing_session);
8317+
chan.funding_tx_constructed(&self.logger)
83168318
},
83178319
_ => Err(ChannelError::Warn(
83188320
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
83198321
.into())),
83208322
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8323+
83218324
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
83228325
let channel = match channel_phase {
8323-
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
8324-
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
8326+
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(),
8327+
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(),
83258328
_ => {
83268329
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
83278330
Err(ChannelError::Warn(

0 commit comments

Comments
 (0)