@@ -1474,32 +1474,56 @@ impl<SP: Deref> Channel<SP> where
14741474 where
14751475 L::Target: Logger
14761476 {
1477- let phase = core::mem::replace(&mut self.phase, ChannelPhase::Undefined);
1478- let result = if let ChannelPhase::UnfundedV2(chan) = phase {
1477+ let result = if let ChannelPhase::UnfundedV2(chan) = &mut self.phase {
14791478 let logger = WithChannelContext::from(logger, &chan.context, None);
1480- match chan.funding_tx_constructed(signing_session, &&logger) {
1481- Ok((chan, commitment_signed, event)) => {
1482- self.phase = ChannelPhase::Funded(chan);
1483- Ok((commitment_signed, event))
1484- },
1485- Err((chan, e)) => {
1486- self.phase = ChannelPhase::UnfundedV2(chan);
1487- Err(e)
1488- },
1489- }
1479+ chan.funding_tx_constructed(signing_session, &&logger)
14901480 } else {
1491- self.phase = phase;
14921481 Err(ChannelError::Warn("Got a tx_complete message with no interactive transaction construction expected or in-progress".to_owned()))
14931482 };
14941483
1495- debug_assert!(!matches!(self.phase, ChannelPhase::Undefined));
14961484 result
14971485 }
14981486
14991487 pub fn force_shutdown(&mut self, should_broadcast: bool, closure_reason: ClosureReason) -> ShutdownResult {
15001488 let (funding, context) = self.funding_and_context_mut();
15011489 context.force_shutdown(funding, should_broadcast, closure_reason)
15021490 }
1491+
1492+ pub fn commitment_signed<L: Deref>(
1493+ &mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
1494+ ) -> Result<(Option<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError>
1495+ where
1496+ L::Target: Logger
1497+ {
1498+ let phase = core::mem::replace(&mut self.phase, ChannelPhase::Undefined);
1499+ match phase {
1500+ ChannelPhase::UnfundedV2(chan) => {
1501+ let mut funded_channel = match chan.into_funded_channel() {
1502+ Ok(funded_channel) => funded_channel,
1503+ Err((pending_channel, err)) => {
1504+ self.phase = ChannelPhase::UnfundedV2(pending_channel);
1505+ return Err(err);
1506+ }
1507+ };
1508+ let res = match funded_channel.commitment_signed_initial_v2(msg, best_block, signer_provider, logger) {
1509+ Ok(monitor) => {
1510+ Ok((Some(monitor), None))
1511+ },
1512+ Err(err) => {
1513+ Err(err)
1514+ }
1515+ };
1516+ self.phase = ChannelPhase::Funded(funded_channel);
1517+ res
1518+ },
1519+ ChannelPhase::Funded(mut funded_channel) => {
1520+ let res = funded_channel.commitment_signed(msg, logger).map(|monitor_update_opt| (None, monitor_update_opt));
1521+ self.phase = ChannelPhase::Funded(funded_channel);
1522+ res
1523+ },
1524+ _ => Err(ChannelError::close("Got a commitment_signed message for an unfunded V1 channel!".into())),
1525+ }
1526+ }
15031527}
15041528
15051529impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -2190,8 +2214,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21902214 }
21912215
21922216 pub fn funding_tx_constructed<L: Deref>(
2193- mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2194- ) -> Result<(FundedChannel<SP>, msgs::CommitmentSigned, Option<Event>), (PendingV2Channel<SP>, ChannelError) >
2217+ & mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2218+ ) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
21952219 where
21962220 L::Target: Logger
21972221 {
@@ -2207,7 +2231,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22072231 (
22082232 "Multiple outputs matched the expected script and value".to_owned(),
22092233 ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2210- ))).map_err(|e| (self, e)) ;
2234+ )));
22112235 }
22122236 output_index = Some(idx as u16);
22132237 }
@@ -2219,7 +2243,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22192243 (
22202244 "No output matched the funding script_pubkey".to_owned(),
22212245 ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2222- ))).map_err(|e| (self, e)) ;
2246+ )));
22232247 };
22242248 self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
22252249 self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
@@ -2233,8 +2257,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22332257 },
22342258 Err(err) => {
22352259 self.context.channel_transaction_parameters.funding_outpoint = None;
2236- return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
2237- .map_err(|e| (self, e));
2260+ return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })));
22382261 },
22392262 };
22402263
@@ -2245,10 +2268,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22452268 false,
22462269 "Zero inputs were provided & zero witnesses were provided, but a count mismatch was somehow found",
22472270 );
2248- return Err((self, ChannelError::Close((
2271+ return Err(ChannelError::Close((
22492272 "V2 channel rejected due to sender error".into(),
22502273 ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) }
2251- )))) ;
2274+ )));
22522275 }
22532276 None
22542277 } else {
@@ -2270,37 +2293,19 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22702293 false,
22712294 "We don't support users providing inputs but somehow we had more than zero inputs",
22722295 );
2273- return Err((self, ChannelError::Close((
2296+ return Err(ChannelError::Close((
22742297 "V2 channel rejected due to sender error".into(),
22752298 ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) }
2276- )))) ;
2299+ )));
22772300 };
22782301
22792302 self.context.channel_state = ChannelState::FundingNegotiated;
22802303
22812304 // Clear the interactive transaction constructor
22822305 self.interactive_tx_constructor.take();
2306+ self.interactive_tx_signing_session = Some(signing_session);
22832307
2284- match self.unfunded_context.holder_commitment_point {
2285- Some(holder_commitment_point) => {
2286- let funded_chan = FundedChannel {
2287- funding: self.funding,
2288- context: self.context,
2289- interactive_tx_signing_session: Some(signing_session),
2290- holder_commitment_point,
2291- is_v2_established: true,
2292- };
2293- Ok((funded_chan, commitment_signed, funding_ready_for_sig_event))
2294- },
2295- None => {
2296- Err(ChannelError::close(
2297- format!(
2298- "Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
2299- self.context.channel_id(),
2300- )))
2301- .map_err(|e| (self, e))
2302- },
2303- }
2308+ Ok((commitment_signed, funding_ready_for_sig_event))
23042309 }
23052310}
23062311
@@ -9530,6 +9535,8 @@ pub(super) struct PendingV2Channel<SP: Deref> where SP::Target: SignerProvider {
95309535 pub dual_funding_context: DualFundingChannelContext,
95319536 /// The current interactive transaction construction session under negotiation.
95329537 pub interactive_tx_constructor: Option<InteractiveTxConstructor>,
9538+ /// The signing session created after `tx_complete` handling
9539+ pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
95339540}
95349541
95359542impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
@@ -9595,6 +9602,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
95959602 our_funding_inputs: funding_inputs,
95969603 },
95979604 interactive_tx_constructor: None,
9605+ interactive_tx_signing_session: None,
95989606 };
95999607 Ok(chan)
96009608 }
@@ -9766,6 +9774,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
97669774 context,
97679775 dual_funding_context,
97689776 interactive_tx_constructor,
9777+ interactive_tx_signing_session: None,
97699778 unfunded_context,
97709779 })
97719780 }
@@ -9843,6 +9852,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
98439852 pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
98449853 self.generate_accept_channel_v2_message()
98459854 }
9855+
9856+ pub fn into_funded_channel(self) -> Result<FundedChannel<SP>, (PendingV2Channel<SP>, ChannelError)> {
9857+ let holder_commitment_point = match self.unfunded_context.holder_commitment_point {
9858+ Some(point) => point,
9859+ None => {
9860+ let channel_id = self.context.channel_id();
9861+ return Err((self, ChannelError::close(
9862+ format!("Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
9863+ channel_id))));
9864+ }
9865+ };
9866+ Ok(FundedChannel {
9867+ funding: self.funding,
9868+ context: self.context,
9869+ interactive_tx_signing_session: self.interactive_tx_signing_session,
9870+ holder_commitment_point,
9871+ is_v2_established: true,
9872+ })
9873+ }
98469874}
98479875
98489876// Unfunded channel utilities
0 commit comments