Skip to content

Commit a917b01

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 78fee88 commit a917b01

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

lightning/src/ln/channel.rs

+21-34
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::ln::types::ChannelId;
3030
use crate::types::payment::{PaymentPreimage, PaymentHash};
3131
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3232
use crate::ln::interactivetxs::{
33-
calculate_change_output_value, get_output_weight, AbortReason, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
33+
calculate_change_output_value, get_output_weight, AbortReason, HandleTxCompleteResult, InteractiveTxConstructor,
3434
InteractiveTxConstructorArgs, InteractiveTxMessageSend, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
3535
OutputOwned, SharedOwnedOutput, TX_COMMON_FIELDS_WEIGHT,
3636
};
@@ -2179,22 +2179,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
21792179
/// store it here and only release it to the `ChannelManager` once it asks for it.
21802180
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
21812181

2182-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
2183-
// transaction construction, or safely abort that transaction if it was not signed by one of the
2184-
// peers, who has thus already removed it from its state.
2185-
//
2186-
// If we've sent `commtiment_signed` for an interactively constructed transaction
2187-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
2188-
// to the txid of that interactive transaction, else we MUST NOT set it.
2189-
//
2190-
// See the spec for further details on this:
2191-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
2192-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
2193-
//
2194-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
2195-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
2196-
next_funding_txid: Option<Txid>,
2197-
21982182
/// Only set when a counterparty `stfu` has been processed to track which node is allowed to
21992183
/// propose "something fundamental" upon becoming quiescent.
22002184
is_holder_quiescence_initiator: Option<bool>,
@@ -2542,10 +2526,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
25422526
}
25432527
};
25442528

2545-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2546-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2547-
};
2548-
25492529
HandleTxCompleteResult(Ok(tx_complete))
25502530
}
25512531

@@ -2981,8 +2961,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29812961

29822962
is_manual_broadcast: false,
29832963

2984-
next_funding_txid: None,
2985-
29862964
is_holder_quiescence_initiator: None,
29872965
};
29882966

@@ -3214,7 +3192,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32143192
blocked_monitor_updates: Vec::new(),
32153193
local_initiated_shutdown: None,
32163194
is_manual_broadcast: false,
3217-
next_funding_txid: None,
32183195

32193196
is_holder_quiescence_initiator: None,
32203197
};
@@ -6603,7 +6580,6 @@ impl<SP: Deref> FundedChannel<SP> where
66036580
// We have a finalized funding transaction, so we can set the funding transaction and reset the
66046581
// signing session fields.
66056582
self.funding.funding_transaction = funding_tx_opt;
6606-
self.context.next_funding_txid = None;
66076583
self.interactive_tx_signing_session = None;
66086584
}
66096585

@@ -8654,6 +8630,25 @@ impl<SP: Deref> FundedChannel<SP> where
86548630
self.sign_channel_announcement(node_signer, announcement).ok()
86558631
}
86568632

8633+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8634+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8635+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8636+
// to the txid of that interactive transaction, else we MUST NOT set it.
8637+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8638+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8639+
if !signing_session.counterparty_sent_tx_signatures {
8640+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8641+
Some(signing_session.unsigned_tx.compute_txid())
8642+
} else {
8643+
// ...and we received a `tx_signatures` from the counterparty.
8644+
None
8645+
}
8646+
} else {
8647+
// We don't have an active signing session.
8648+
None
8649+
}
8650+
}
8651+
86578652
/// May panic if called on a channel that wasn't immediately-previously
86588653
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
86598654
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8703,7 +8698,7 @@ impl<SP: Deref> FundedChannel<SP> where
87038698
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
87048699
your_last_per_commitment_secret: remote_last_secret,
87058700
my_current_per_commitment_point: dummy_pubkey,
8706-
next_funding_txid: self.context.next_funding_txid,
8701+
next_funding_txid: self.maybe_get_next_funding_txid(),
87078702
}
87088703
}
87098704

@@ -11569,14 +11564,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1156911564
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1157011565
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
1157111566

11572-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
11573-
// funding transaction and other channel state.
11574-
//
11575-
// If we've sent `commtiment_signed` for an interactively constructed transaction
11576-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
11577-
// to the txid of that interactive transaction, else we MUST NOT set it.
11578-
next_funding_txid: None,
11579-
1158011567
is_holder_quiescence_initiator: None,
1158111568
},
1158211569
interactive_tx_signing_session: None,

lightning/src/ln/interactivetxs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ impl ConstructedTransaction {
310310
#[derive(Debug, Clone, PartialEq)]
311311
pub(crate) struct InteractiveTxSigningSession {
312312
pub unsigned_tx: ConstructedTransaction,
313+
pub counterparty_sent_tx_signatures: bool,
313314
holder_sends_tx_signatures_first: bool,
314315
received_commitment_signed: bool,
315316
holder_tx_signatures: Option<TxSignatures>,
316-
counterparty_sent_tx_signatures: bool,
317317
}
318318

319319
impl InteractiveTxSigningSession {

lightning/src/ln/msgs.rs

+12
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,18 @@ pub struct ChannelReestablish {
866866
/// The sender's per-commitment point for their current commitment transaction
867867
pub my_current_per_commitment_point: PublicKey,
868868
/// The next funding transaction ID
869+
///
870+
/// Allows peers to finalize the signing steps of an interactive transaction construction, or
871+
/// safely abort that transaction if it was not signed by one of the peers, who has thus already
872+
/// removed it from its state.
873+
///
874+
/// If we've sent `commtiment_signed` for an interactively constructed transaction
875+
/// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
876+
/// to the txid of that interactive transaction, else we MUST NOT set it.
877+
///
878+
/// See the spec for further details on this:
879+
/// * `channel_reestablish`-sending node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
880+
/// * `channel_reestablish`-receiving node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
869881
pub next_funding_txid: Option<Txid>,
870882
}
871883

0 commit comments

Comments
 (0)