Skip to content

Commit 765c757

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 76409df commit 765c757

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};
@@ -1737,21 +1736,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
17371736
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
17381737
/// store it here and only release it to the `ChannelManager` once it asks for it.
17391738
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1740-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1741-
// transaction construction, or safely abort that transaction if it was not signed by one of the
1742-
// peers, who has thus already removed it from its state.
1743-
//
1744-
// If we've sent `commtiment_signed` for an interactively constructed transaction
1745-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1746-
// to the txid of that interactive transaction, else we MUST NOT set it.
1747-
//
1748-
// See the spec for further details on this:
1749-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1750-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1751-
//
1752-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1753-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1754-
next_funding_txid: Option<Txid>,
17551739
}
17561740

17571741
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -1980,10 +1964,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
19801964
}
19811965
};
19821966

1983-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
1984-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
1985-
};
1986-
19871967
HandleTxCompleteResult(Ok(tx_complete))
19881968
}
19891969

@@ -2419,8 +2399,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24192399
blocked_monitor_updates: Vec::new(),
24202400

24212401
is_manual_broadcast: false,
2422-
2423-
next_funding_txid: None,
24242402
};
24252403

24262404
Ok(channel_context)
@@ -2650,7 +2628,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26502628
blocked_monitor_updates: Vec::new(),
26512629
local_initiated_shutdown: None,
26522630
is_manual_broadcast: false,
2653-
next_funding_txid: None,
26542631
})
26552632
}
26562633

@@ -5963,7 +5940,6 @@ impl<SP: Deref> FundedChannel<SP> where
59635940
// We have a persisted channel monitor and and a finalized funding transaction, so we can move
59645941
// the channel state forward, set the funding transaction and reset the signing session fields.
59655942
self.context.funding_transaction = funding_tx_opt;
5966-
self.context.next_funding_txid = None;
59675943
self.interactive_tx_signing_session = None;
59685944
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
59695945
}
@@ -7982,6 +7958,25 @@ impl<SP: Deref> FundedChannel<SP> where
79827958
}
79837959
}
79847960

7961+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
7962+
// If we've sent `commtiment_signed` for an interactively constructed transaction
7963+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
7964+
// to the txid of that interactive transaction, else we MUST NOT set it.
7965+
if let Some(signing_session) = &self.interactive_tx_signing_session {
7966+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
7967+
if !signing_session.counterparty_sent_tx_signatures {
7968+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
7969+
Some(self.funding_outpoint().txid)
7970+
} else {
7971+
// ...and we received a `tx_signatures` from the counterparty.
7972+
None
7973+
}
7974+
} else {
7975+
// We don't have an active signing session.
7976+
None
7977+
}
7978+
}
7979+
79857980
/// May panic if called on a channel that wasn't immediately-previously
79867981
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
79877982
pub fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8032,7 +8027,7 @@ impl<SP: Deref> FundedChannel<SP> where
80328027
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
80338028
your_last_per_commitment_secret: remote_last_secret,
80348029
my_current_per_commitment_point: dummy_pubkey,
8035-
next_funding_txid: self.context.next_funding_txid,
8030+
next_funding_txid: self.maybe_get_next_funding_txid(),
80368031
}
80378032
}
80388033

@@ -10437,13 +10432,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1043710432

1043810433
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1043910434
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10440-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10441-
// funding transaction and other channel state.
10442-
//
10443-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10444-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10445-
// to the txid of that interactive transaction, else we MUST NOT set it.
10446-
next_funding_txid: None,
1044710435
},
1044810436
interactive_tx_signing_session: None,
1044910437
holder_commitment_point,

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

0 commit comments

Comments
 (0)