Skip to content

Commit 8467702

Browse files
committed
Get next_funding_txid from the funding_outpoint based on state
Instead of having an explicit `ChannelContext::next_funding_txid` to set and read, we can get this value on the fly when it is appropriate to do so.
1 parent d43ba3c commit 8467702

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ use crate::ln::types::ChannelId;
3131
use crate::types::payment::{PaymentPreimage, PaymentHash};
3232
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3333
use crate::ln::interactivetxs::{
34-
get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
35-
InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
36-
TX_COMMON_FIELDS_WEIGHT,
34+
get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxConstructorArgs,
35+
InteractiveTxSigningSession, InteractiveTxMessageSendResult, TX_COMMON_FIELDS_WEIGHT,
3736
};
3837
use crate::ln::msgs;
3938
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
@@ -1874,21 +1873,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
18741873
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
18751874
/// store it here and only release it to the `ChannelManager` once it asks for it.
18761875
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1877-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1878-
// transaction construction, or safely abort that transaction if it was not signed by one of the
1879-
// peers, who has thus already removed it from its state.
1880-
//
1881-
// If we've sent `commtiment_signed` for an interactively constructed transaction
1882-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1883-
// to the txid of that interactive transaction, else we MUST NOT set it.
1884-
//
1885-
// See the spec for further details on this:
1886-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1887-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1888-
//
1889-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1890-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1891-
next_funding_txid: Option<Txid>,
18921876
}
18931877

18941878
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -2119,10 +2103,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21192103
}
21202104
};
21212105

2122-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2123-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2124-
};
2125-
21262106
HandleTxCompleteResult(Ok(tx_complete))
21272107
}
21282108

@@ -2555,8 +2535,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25552535
blocked_monitor_updates: Vec::new(),
25562536

25572537
is_manual_broadcast: false,
2558-
2559-
next_funding_txid: None,
25602538
};
25612539

25622540
Ok(channel_context)
@@ -2783,7 +2761,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27832761
blocked_monitor_updates: Vec::new(),
27842762
local_initiated_shutdown: None,
27852763
is_manual_broadcast: false,
2786-
next_funding_txid: None,
27872764
})
27882765
}
27892766

@@ -6066,7 +6043,6 @@ impl<SP: Deref> FundedChannel<SP> where
60666043
// We have a persisted channel monitor and and a finalized funding transaction, so we can move
60676044
// the channel state forward, set the funding transaction and reset the signing session fields.
60686045
self.context.funding_transaction = funding_tx_opt;
6069-
self.context.next_funding_txid = None;
60706046
self.interactive_tx_signing_session = None;
60716047
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
60726048
}
@@ -8087,6 +8063,25 @@ impl<SP: Deref> FundedChannel<SP> where
80878063
}
80888064
}
80898065

8066+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8067+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8068+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8069+
// to the txid of that interactive transaction, else we MUST NOT set it.
8070+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8071+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8072+
if !signing_session.counterparty_sent_tx_signatures {
8073+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8074+
Some(self.funding_outpoint().txid)
8075+
} else {
8076+
// ...and we received a `tx_signatures` from the counterparty.
8077+
None
8078+
}
8079+
} else {
8080+
// We don't have an active signing session.
8081+
None
8082+
}
8083+
}
8084+
80908085
/// May panic if called on a channel that wasn't immediately-previously
80918086
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
80928087
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8137,7 +8132,7 @@ impl<SP: Deref> FundedChannel<SP> where
81378132
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
81388133
your_last_per_commitment_secret: remote_last_secret,
81398134
my_current_per_commitment_point: dummy_pubkey,
8140-
next_funding_txid: self.context.next_funding_txid,
8135+
next_funding_txid: self.maybe_get_next_funding_txid(),
81418136
}
81428137
}
81438138

@@ -10503,13 +10498,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1050310498

1050410499
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1050510500
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10506-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10507-
// funding transaction and other channel state.
10508-
//
10509-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10510-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10511-
// to the txid of that interactive transaction, else we MUST NOT set it.
10512-
next_funding_txid: None,
1051310501
},
1051410502
interactive_tx_signing_session: None,
1051510503
is_v2_established,

lightning/src/ln/interactivetxs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ impl ConstructedTransaction {
290290
#[derive(Debug, Clone, PartialEq)]
291291
pub(crate) struct InteractiveTxSigningSession {
292292
pub unsigned_tx: ConstructedTransaction,
293+
pub counterparty_sent_tx_signatures: bool,
293294
holder_sends_tx_signatures_first: bool,
294295
received_commitment_signed: bool,
295296
holder_tx_signatures: Option<TxSignatures>,
296-
counterparty_sent_tx_signatures: bool,
297297
}
298298

299299
impl InteractiveTxSigningSession {

lightning/src/ln/msgs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,18 @@ pub struct ChannelReestablish {
823823
/// The sender's per-commitment point for their current commitment transaction
824824
pub my_current_per_commitment_point: PublicKey,
825825
/// The next funding transaction ID
826+
///
827+
/// Allows peers to finalize the signing steps of an interactive transaction construction, or
828+
/// safely abort that transaction if it was not signed by one of the peers, who has thus already
829+
/// removed it from its state.
830+
///
831+
/// If we've sent `commtiment_signed` for an interactively constructed transaction
832+
/// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
833+
/// to the txid of that interactive transaction, else we MUST NOT set it.
834+
///
835+
/// See the spec for further details on this:
836+
/// * `channel_reestablish`-sending node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
837+
/// * `channel_reestablish`-receiving node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
826838
pub next_funding_txid: Option<Txid>,
827839
}
828840

0 commit comments

Comments
 (0)