@@ -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.compute_txid());
1751
+ };
1752
+
1753
+ HandleTxCompleteResult(Ok(tx_complete))
1721
1754
}
1722
1755
1723
1756
fn funding_tx_constructed<L: Deref>(
@@ -2077,6 +2110,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2077
2110
monitor_pending_failures: Vec::new(),
2078
2111
monitor_pending_finalized_fulfills: Vec::new(),
2079
2112
monitor_pending_update_adds: Vec::new(),
2113
+ monitor_pending_tx_signatures: None,
2080
2114
2081
2115
signer_pending_revoke_and_ack: false,
2082
2116
signer_pending_commitment_update: false,
@@ -2170,6 +2204,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2170
2204
blocked_monitor_updates: Vec::new(),
2171
2205
2172
2206
is_manual_broadcast: false,
2207
+
2208
+ next_funding_txid: None,
2173
2209
};
2174
2210
2175
2211
Ok(channel_context)
@@ -2311,6 +2347,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2311
2347
monitor_pending_failures: Vec::new(),
2312
2348
monitor_pending_finalized_fulfills: Vec::new(),
2313
2349
monitor_pending_update_adds: Vec::new(),
2350
+ monitor_pending_tx_signatures: None,
2314
2351
2315
2352
signer_pending_revoke_and_ack: false,
2316
2353
signer_pending_commitment_update: false,
@@ -2401,6 +2438,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2401
2438
blocked_monitor_updates: Vec::new(),
2402
2439
local_initiated_shutdown: None,
2403
2440
is_manual_broadcast: false,
2441
+ next_funding_txid: None,
2404
2442
})
2405
2443
}
2406
2444
@@ -4955,6 +4993,14 @@ impl<SP: Deref> Channel<SP> where
4955
4993
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
4956
4994
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
4957
4995
4996
+ if let Some(tx_signatures) = self.interactive_tx_signing_session.as_mut().and_then(
4997
+ |session| session.received_commitment_signed()
4998
+ ) {
4999
+ // We're up first for submitting our tx_signatures, but our monitor has not persisted yet
5000
+ // so they'll be sent as soon as that's done.
5001
+ self.context.monitor_pending_tx_signatures = Some(tx_signatures);
5002
+ }
5003
+
4958
5004
Ok(channel_monitor)
4959
5005
}
4960
5006
@@ -5628,7 +5674,13 @@ impl<SP: Deref> Channel<SP> where
5628
5674
}
5629
5675
}
5630
5676
5631
- pub fn tx_signatures(&mut self, msg: &msgs::TxSignatures) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError> {
5677
+ pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError>
5678
+ where L::Target: Logger
5679
+ {
5680
+ if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
5681
+ return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
5682
+ }
5683
+
5632
5684
if let Some(ref mut signing_session) = self.interactive_tx_signing_session {
5633
5685
if msg.witnesses.len() != signing_session.remote_inputs_count() {
5634
5686
return Err(ChannelError::Warn(
@@ -5666,9 +5718,17 @@ impl<SP: Deref> Channel<SP> where
5666
5718
}
5667
5719
self.context.funding_transaction = funding_tx_opt.clone();
5668
5720
5721
+ self.context.next_funding_txid = None;
5722
+
5669
5723
// Clear out the signing session
5670
5724
self.interactive_tx_signing_session = None;
5671
5725
5726
+ if tx_signatures_opt.is_some() && self.context.channel_state.is_monitor_update_in_progress() {
5727
+ log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
5728
+ self.context.monitor_pending_tx_signatures = tx_signatures_opt;
5729
+ return Ok((None, None));
5730
+ }
5731
+
5672
5732
Ok((tx_signatures_opt, funding_tx_opt))
5673
5733
} else {
5674
5734
Err(ChannelError::Close((
@@ -5911,14 +5971,18 @@ impl<SP: Deref> Channel<SP> where
5911
5971
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
5912
5972
let mut pending_update_adds = Vec::new();
5913
5973
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
5974
+ // For channels established with V2 establishment we won't send a `tx_signatures` when we're in
5975
+ // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
5976
+ // transaction and waits for us to do it).
5977
+ let tx_signatures = self.context.monitor_pending_tx_signatures.take();
5914
5978
5915
5979
if self.context.channel_state.is_peer_disconnected() {
5916
5980
self.context.monitor_pending_revoke_and_ack = false;
5917
5981
self.context.monitor_pending_commitment_signed = false;
5918
5982
return MonitorRestoreUpdates {
5919
5983
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
5920
5984
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
5921
- funding_broadcastable, channel_ready, announcement_sigs
5985
+ funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
5922
5986
};
5923
5987
}
5924
5988
@@ -5952,7 +6016,7 @@ impl<SP: Deref> Channel<SP> where
5952
6016
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
5953
6017
MonitorRestoreUpdates {
5954
6018
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
5955
- pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
6019
+ pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
5956
6020
}
5957
6021
}
5958
6022
@@ -7723,10 +7787,7 @@ impl<SP: Deref> Channel<SP> where
7723
7787
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
7724
7788
your_last_per_commitment_secret: remote_last_secret,
7725
7789
my_current_per_commitment_point: dummy_pubkey,
7726
- // TODO(dual_funding): If we've sent `commtiment_signed` for an interactive transaction
7727
- // construction but have not received `tx_signatures` we MUST set `next_funding_txid` to the
7728
- // txid of that interactive transaction, else we MUST NOT set it.
7729
- next_funding_txid: None,
7790
+ next_funding_txid: self.context.next_funding_txid,
7730
7791
}
7731
7792
}
7732
7793
@@ -9427,7 +9488,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
9427
9488
(47, next_holder_commitment_point, option),
9428
9489
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
9429
9490
(51, is_manual_broadcast, option), // Added in 0.0.124
9430
- (53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124
9491
+ (53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
9492
+ (55, self.context.next_funding_txid, option) // Added in 0.1.0
9431
9493
});
9432
9494
9433
9495
Ok(())
@@ -9717,6 +9779,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9717
9779
let mut channel_pending_event_emitted = None;
9718
9780
let mut channel_ready_event_emitted = None;
9719
9781
let mut funding_tx_broadcast_safe_event_emitted = None;
9782
+ let mut next_funding_txid = funding_transaction.as_ref().map(|tx| tx.compute_txid());
9720
9783
9721
9784
let mut user_id_high_opt: Option<u64> = None;
9722
9785
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9777,6 +9840,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9777
9840
(49, local_initiated_shutdown, option),
9778
9841
(51, is_manual_broadcast, option),
9779
9842
(53, funding_tx_broadcast_safe_event_emitted, option),
9843
+ (55, next_funding_txid, option) // Added in 0.0.125
9780
9844
});
9781
9845
9782
9846
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -9950,6 +10014,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9950
10014
monitor_pending_failures,
9951
10015
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
9952
10016
monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or_default(),
10017
+ monitor_pending_tx_signatures: None,
9953
10018
9954
10019
signer_pending_revoke_and_ack: false,
9955
10020
signer_pending_commitment_update: false,
@@ -10036,6 +10101,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
10036
10101
10037
10102
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
10038
10103
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10104
+ // If we've sent `commtiment_signed` for an interactively constructed transaction
10105
+ // during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10106
+ // to the txid of that interactive transaction, else we MUST NOT set it.
10107
+ next_funding_txid,
10039
10108
},
10040
10109
interactive_tx_signing_session: None,
10041
10110
})
0 commit comments