Skip to content

Commit 5692231

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 4a1977a commit 5692231

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 35 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};
@@ -1498,21 +1497,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14981497
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
14991498
/// store it here and only release it to the `ChannelManager` once it asks for it.
15001499
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1501-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1502-
// transaction construction, or safely abort that transaction if it was not signed by one of the
1503-
// peers, who has thus already removed it from its state.
1504-
//
1505-
// If we've sent `commtiment_signed` for an interactively constructed transaction
1506-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1507-
// to the txid of that interactive transaction, else we MUST NOT set it.
1508-
//
1509-
// See the spec for further details on this:
1510-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1511-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1512-
//
1513-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1514-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1515-
next_funding_txid: Option<Txid>,
15161500
}
15171501

15181502
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -1747,10 +1731,6 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17471731
}
17481732
};
17491733

1750-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
1751-
self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
1752-
};
1753-
17541734
HandleTxCompleteResult(Ok(tx_complete))
17551735
}
17561736

@@ -2217,8 +2197,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22172197
blocked_monitor_updates: Vec::new(),
22182198

22192199
is_manual_broadcast: false,
2220-
2221-
next_funding_txid: None,
22222200
};
22232201

22242202
Ok(channel_context)
@@ -2451,7 +2429,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24512429
blocked_monitor_updates: Vec::new(),
24522430
local_initiated_shutdown: None,
24532431
is_manual_broadcast: false,
2454-
next_funding_txid: None,
24552432
})
24562433
}
24572434

@@ -5755,8 +5732,6 @@ impl<SP: Deref> Channel<SP> where
57555732
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
57565733
self.context.funding_transaction = Some(funding_tx);
57575734

5758-
self.context.next_funding_txid = None;
5759-
57605735
// Clear out the signing session
57615736
self.interactive_tx_signing_session = None;
57625737

@@ -7780,6 +7755,25 @@ impl<SP: Deref> Channel<SP> where
77807755
}
77817756
}
77827757

7758+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
7759+
// If we've sent `commtiment_signed` for an interactively constructed transaction
7760+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
7761+
// to the txid of that interactive transaction, else we MUST NOT set it.
7762+
if let Some(signing_session) = &self.interactive_tx_signing_session {
7763+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
7764+
if signing_session.counterparty_sent_tx_signatures {
7765+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
7766+
None
7767+
} else {
7768+
// ...and we received a `tx_signatures` from the counterparty.
7769+
Some(self.funding_outpoint().txid)
7770+
}
7771+
} else {
7772+
// We don't have an active signing session.
7773+
None
7774+
}
7775+
}
7776+
77837777
/// May panic if called on a channel that wasn't immediately-previously
77847778
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
77857779
pub fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -7824,7 +7818,7 @@ impl<SP: Deref> Channel<SP> where
78247818
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
78257819
your_last_per_commitment_secret: remote_last_secret,
78267820
my_current_per_commitment_point: dummy_pubkey,
7827-
next_funding_txid: self.context.next_funding_txid,
7821+
next_funding_txid: self.maybe_get_next_funding_txid(),
78287822
}
78297823
}
78307824

@@ -10139,13 +10133,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1013910133

1014010134
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1014110135
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10142-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10143-
// funding transaction and other channel state.
10144-
//
10145-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10146-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10147-
// to the txid of that interactive transaction, else we MUST NOT set it.
10148-
next_funding_txid: None,
1014910136
},
1015010137
interactive_tx_signing_session: None,
1015110138
})

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

0 commit comments

Comments
 (0)