@@ -31,8 +31,9 @@ use crate::ln::types::ChannelId;
31
31
use crate::types::payment::{PaymentPreimage, PaymentHash};
32
32
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
33
33
use crate::ln::interactivetxs::{
34
- get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxConstructorArgs,
35
- InteractiveTxSigningSession, InteractiveTxMessageSendResult, TX_COMMON_FIELDS_WEIGHT,
34
+ get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
35
+ InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
36
+ TX_COMMON_FIELDS_WEIGHT,
36
37
};
37
38
use crate::ln::msgs;
38
39
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
@@ -901,6 +902,7 @@ pub(super) struct MonitorRestoreUpdates {
901
902
pub funding_broadcastable: Option<Transaction>,
902
903
pub channel_ready: Option<msgs::ChannelReady>,
903
904
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
905
+ pub tx_signatures: Option<msgs::TxSignatures>,
904
906
}
905
907
906
908
/// The return value of `signer_maybe_unblocked`
@@ -1252,6 +1254,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1252
1254
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
1253
1255
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
1254
1256
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
1257
+ monitor_pending_tx_signatures: Option<msgs::TxSignatures>,
1255
1258
1256
1259
/// If we went to send a revoke_and_ack but our signer was unable to give us a signature,
1257
1260
/// we should retry at some point in the future when the signer indicates it may have a
@@ -1494,6 +1497,21 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1494
1497
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
1495
1498
/// store it here and only release it to the `ChannelManager` once it asks for it.
1496
1499
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1500
+ // The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1501
+ // transaction construction, or safely abort that transaction if it was not signed by one of the
1502
+ // peers, who has thus already removed it from its state.
1503
+ //
1504
+ // If we've sent `commtiment_signed` for an interactively constructed transaction
1505
+ // during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1506
+ // to the txid of that interactive transaction, else we MUST NOT set it.
1507
+ //
1508
+ // See the spec for further details on this:
1509
+ // * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1510
+ // * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1511
+ //
1512
+ // TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1513
+ // send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1514
+ next_funding_txid: Option<Txid>,
1497
1515
}
1498
1516
1499
1517
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -1710,14 +1728,29 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
1710
1728
}
1711
1729
1712
1730
fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
1713
- HandleTxCompleteResult(match self.interactive_tx_constructor_mut() {
1714
- Some(ref mut tx_constructor) => tx_constructor.handle_tx_complete(msg).map_err(
1715
- |reason| reason.into_tx_abort_msg(self.context().channel_id())),
1716
- None => Err(msgs::TxAbort {
1717
- channel_id: self.context().channel_id(),
1718
- data: b"No interactive transaction negotiation in progress".to_vec()
1719
- }),
1720
- })
1731
+ let tx_constructor = match self.interactive_tx_constructor_mut() {
1732
+ Some(ref mut tx_constructor) => tx_constructor,
1733
+ None => {
1734
+ let tx_abort = msgs::TxAbort {
1735
+ channel_id: msg.channel_id,
1736
+ data: b"No interactive transaction negotiation in progress".to_vec(),
1737
+ };
1738
+ return HandleTxCompleteResult(Err(tx_abort));
1739
+ },
1740
+ };
1741
+
1742
+ let tx_complete = match tx_constructor.handle_tx_complete(msg) {
1743
+ Ok(tx_complete) => tx_complete,
1744
+ Err(reason) => {
1745
+ return HandleTxCompleteResult(Err(reason.into_tx_abort_msg(msg.channel_id)))
1746
+ }
1747
+ };
1748
+
1749
+ if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
1750
+ self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.txid());
1751
+ };
1752
+
1753
+ HandleTxCompleteResult(Ok(tx_complete))
1721
1754
}
1722
1755
1723
1756
fn funding_tx_constructed<L: Deref>(
@@ -2071,6 +2104,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2071
2104
monitor_pending_failures: Vec::new(),
2072
2105
monitor_pending_finalized_fulfills: Vec::new(),
2073
2106
monitor_pending_update_adds: Vec::new(),
2107
+ monitor_pending_tx_signatures: None,
2074
2108
2075
2109
signer_pending_revoke_and_ack: false,
2076
2110
signer_pending_commitment_update: false,
@@ -2164,6 +2198,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2164
2198
blocked_monitor_updates: Vec::new(),
2165
2199
2166
2200
is_manual_broadcast: false,
2201
+
2202
+ next_funding_txid: None,
2167
2203
};
2168
2204
2169
2205
Ok(channel_context)
@@ -2305,6 +2341,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2305
2341
monitor_pending_failures: Vec::new(),
2306
2342
monitor_pending_finalized_fulfills: Vec::new(),
2307
2343
monitor_pending_update_adds: Vec::new(),
2344
+ monitor_pending_tx_signatures: None,
2308
2345
2309
2346
signer_pending_revoke_and_ack: false,
2310
2347
signer_pending_commitment_update: false,
@@ -2395,6 +2432,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2395
2432
blocked_monitor_updates: Vec::new(),
2396
2433
local_initiated_shutdown: None,
2397
2434
is_manual_broadcast: false,
2435
+ next_funding_txid: None,
2398
2436
})
2399
2437
}
2400
2438
@@ -4949,6 +4987,14 @@ impl<SP: Deref> Channel<SP> where
4949
4987
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
4950
4988
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
4951
4989
4990
+ if let Some(tx_signatures) = self.interactive_tx_signing_session.as_mut().and_then(
4991
+ |session| session.received_commitment_signed()
4992
+ ) {
4993
+ // We're up first for submitting our tx_signatures, but our monitor has not persisted yet
4994
+ // so they'll be sent as soon as that's done.
4995
+ self.context.monitor_pending_tx_signatures = Some(tx_signatures);
4996
+ }
4997
+
4952
4998
Ok(channel_monitor)
4953
4999
}
4954
5000
@@ -5622,7 +5668,13 @@ impl<SP: Deref> Channel<SP> where
5622
5668
}
5623
5669
}
5624
5670
5625
- pub fn tx_signatures(&mut self, msg: &msgs::TxSignatures) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError> {
5671
+ pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError>
5672
+ where L::Target: Logger
5673
+ {
5674
+ if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
5675
+ return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
5676
+ }
5677
+
5626
5678
if let Some(ref mut signing_session) = self.interactive_tx_signing_session {
5627
5679
if msg.witnesses.len() != signing_session.remote_inputs_count() {
5628
5680
return Err(ChannelError::Close(
@@ -5661,16 +5713,23 @@ impl<SP: Deref> Channel<SP> where
5661
5713
}
5662
5714
self.context.funding_transaction = funding_tx_opt.clone();
5663
5715
5716
+ self.context.next_funding_txid = None;
5717
+
5664
5718
// Clear out the signing session
5665
5719
self.interactive_tx_signing_session = None;
5666
5720
5721
+ if tx_signatures_opt.is_some() && self.context.channel_state.is_monitor_update_in_progress() {
5722
+ log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
5723
+ self.context.monitor_pending_tx_signatures = tx_signatures_opt;
5724
+ return Ok((None, None));
5725
+ }
5726
+
5667
5727
Ok((tx_signatures_opt, funding_tx_opt))
5668
5728
} else {
5669
- return Err(ChannelError::Close(
5670
- (
5729
+ Err(ChannelError::Close((
5671
5730
"Unexpected tx_signatures. No funding transaction awaiting signatures".to_string(),
5672
5731
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
5673
- )));
5732
+ )))
5674
5733
}
5675
5734
}
5676
5735
@@ -5907,14 +5966,18 @@ impl<SP: Deref> Channel<SP> where
5907
5966
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
5908
5967
let mut pending_update_adds = Vec::new();
5909
5968
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
5969
+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
5970
+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
5971
+ // transaction and waits for us to do it).
5972
+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
5910
5973
5911
5974
if self.context.channel_state.is_peer_disconnected() {
5912
5975
self.context.monitor_pending_revoke_and_ack = false;
5913
5976
self.context.monitor_pending_commitment_signed = false;
5914
5977
return MonitorRestoreUpdates {
5915
5978
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
5916
5979
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
5917
- funding_broadcastable, channel_ready, announcement_sigs
5980
+ funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
5918
5981
};
5919
5982
}
5920
5983
@@ -5948,7 +6011,7 @@ impl<SP: Deref> Channel<SP> where
5948
6011
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
5949
6012
MonitorRestoreUpdates {
5950
6013
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
5951
- pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
6014
+ pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
5952
6015
}
5953
6016
}
5954
6017
@@ -7719,10 +7782,7 @@ impl<SP: Deref> Channel<SP> where
7719
7782
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
7720
7783
your_last_per_commitment_secret: remote_last_secret,
7721
7784
my_current_per_commitment_point: dummy_pubkey,
7722
- // TODO(dual_funding): If we've sent `commtiment_signed` for an interactive transaction
7723
- // construction but have not received `tx_signatures` we MUST set `next_funding_txid` to the
7724
- // txid of that interactive transaction, else we MUST NOT set it.
7725
- next_funding_txid: None,
7785
+ next_funding_txid: self.context.next_funding_txid,
7726
7786
}
7727
7787
}
7728
7788
@@ -9423,7 +9483,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
9423
9483
(47, next_holder_commitment_point, option),
9424
9484
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
9425
9485
(51, is_manual_broadcast, option), // Added in 0.0.124
9426
- (53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124
9486
+ (53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
9487
+ (55, self.context.next_funding_txid, option) // Added in 0.1.0
9427
9488
});
9428
9489
9429
9490
Ok(())
@@ -9713,6 +9774,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9713
9774
let mut channel_pending_event_emitted = None;
9714
9775
let mut channel_ready_event_emitted = None;
9715
9776
let mut funding_tx_broadcast_safe_event_emitted = None;
9777
+ let mut next_funding_txid = funding_transaction.as_ref().map(|tx| tx.compute_txid());
9716
9778
9717
9779
let mut user_id_high_opt: Option<u64> = None;
9718
9780
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9773,6 +9835,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9773
9835
(49, local_initiated_shutdown, option),
9774
9836
(51, is_manual_broadcast, option),
9775
9837
(53, funding_tx_broadcast_safe_event_emitted, option),
9838
+ (55, next_funding_txid, option) // Added in 0.0.125
9776
9839
});
9777
9840
9778
9841
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -9946,6 +10009,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9946
10009
monitor_pending_failures,
9947
10010
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
9948
10011
monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or_default(),
10012
+ monitor_pending_tx_signatures: None,
9949
10013
9950
10014
signer_pending_revoke_and_ack: false,
9951
10015
signer_pending_commitment_update: false,
@@ -10032,6 +10096,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
10032
10096
10033
10097
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
10034
10098
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10099
+ // If we've sent `commtiment_signed` for an interactively constructed transaction
10100
+ // during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10101
+ // to the txid of that interactive transaction, else we MUST NOT set it.
10102
+ next_funding_txid,
10035
10103
},
10036
10104
interactive_tx_signing_session: None,
10037
10105
})
0 commit comments