Skip to content

Commit bed2ba6

Browse files
committed
Add FundingSigned event
The `FundingSigned` event indicates that we have received `funding_signed` message from our counterparty but we are yet to broadcast the funding transaction. This event is only emitted if upon generating the funding transaction, you call `ChannelManager::unsafe_manual_funding_transaction_generated` that will emit this event instead of `ChannelPending` event. `ChannelManager::unsafe_manual_funding_transaction_generated` wont check if the funding transaction is signed, those its unsafe. It is manual because you are responsibile on broadcasting the transaction once the event is received.
1 parent bd16a1e commit bed2ba6

File tree

5 files changed

+338
-16
lines changed

5 files changed

+338
-16
lines changed

lightning/src/events/mod.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,35 @@ pub enum Event {
532532
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
533533
user_channel_id: u128,
534534
},
535+
/// Used to indicate that the counterparty node has sent us `funding_signed` message but we are
536+
/// yet to broadcast the funding transaction.
537+
///
538+
/// This event is only emitted if you called
539+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`] instead of
540+
/// [`ChannelManager::funding_transaction_generated`].
541+
///
542+
/// Upon receiving this event, it is your responsibility to broadcast the funding transaction.
543+
///
544+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]:
545+
/// crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
546+
/// [`ChannelManager::funding_transaction_generated`]:
547+
/// crate::ln::channelmanager::ChannelManager::funding_transaction_generated
548+
FundingSigned {
549+
/// The `channel_id` indicating which channel has completed the `FundingSigned` stage.
550+
channel_id: ChannelId,
551+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
552+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
553+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
554+
/// `user_channel_id` will be randomized for an inbound channel. This may be zero for objects
555+
/// serialized with LDK versions prior to 0.0.113.
556+
///
557+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
558+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
559+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
560+
user_channel_id: u128,
561+
/// The funding transaction which was signed by the counterparty.
562+
funding_tx: Transaction,
563+
},
535564
/// Indicates that we've been offered a payment and it needs to be claimed via calling
536565
/// [`ChannelManager::claim_funds`] with the preimage given in [`PaymentPurpose`].
537566
///
@@ -969,9 +998,15 @@ pub enum Event {
969998
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
970999
/// confirmation on-chain.
9711000
///
1001+
/// Note that this event wont be emitted for channels created using
1002+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]:
1003+
///
9721004
/// This event is emitted when the funding transaction has been signed and is broadcast to the
9731005
/// network. For 0conf channels it will be immediately followed by the corresponding
9741006
/// [`Event::ChannelReady`] event.
1007+
///
1008+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]:
1009+
/// crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
9751010
ChannelPending {
9761011
/// The `channel_id` of the channel that is pending confirmation.
9771012
channel_id: ChannelId,
@@ -1447,7 +1482,15 @@ impl Writeable for Event {
14471482
write_tlv_fields!(writer, {
14481483
(0, peer_node_id, required),
14491484
});
1450-
}
1485+
},
1486+
&Event::FundingSigned { ref channel_id, ref user_channel_id, ref funding_tx } => {
1487+
41u8.write(writer)?;
1488+
write_tlv_fields!(writer, {
1489+
(0, channel_id, required),
1490+
(2, user_channel_id, required),
1491+
(4, funding_tx, required),
1492+
});
1493+
},
14511494
// Note that, going forward, all new events must only write data inside of
14521495
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
14531496
// data via `write_tlv_fields`.
@@ -1884,6 +1927,21 @@ impl MaybeReadable for Event {
18841927
};
18851928
f()
18861929
},
1930+
41u8 => {
1931+
let mut channel_id = ChannelId::new_zero();
1932+
let mut user_channel_id: u128 = 0;
1933+
let mut funding_tx = Transaction { version: Version::TWO, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
1934+
read_tlv_fields!(reader, {
1935+
(0, channel_id, required),
1936+
(2, user_channel_id, required),
1937+
(4, funding_tx, required),
1938+
});
1939+
Ok(Some(Event::FundingSigned {
1940+
channel_id,
1941+
user_channel_id,
1942+
funding_tx,
1943+
}))
1944+
},
18871945
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
18881946
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
18891947
// reads.

lightning/src/ln/channel.rs

+70-7
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15301530

15311531
// We track whether we already emitted a `ChannelPending` event.
15321532
channel_pending_event_emitted: bool,
1533+
1534+
// We track whether we already emitted a `FundingSigned` event.
1535+
funding_signed_event_emitted: bool,
15331536

15341537
// We track whether we already emitted a `ChannelReady` event.
15351538
channel_ready_event_emitted: bool,
@@ -1547,6 +1550,14 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15471550
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
15481551
/// store it here and only release it to the `ChannelManager` once it asks for it.
15491552
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1553+
1554+
/// Using this flag will prevent the funding transaction from being broadcasted
1555+
/// and will allow the user to manually broadcast it.
1556+
///
1557+
/// The funding transaction can be accessed through the [`Event::FundingSigned`] event.
1558+
///
1559+
/// [`Event::FundingSigned`]: crate::events::Event::FundingSigned
1560+
manually_broadcast_outbound_channels: Option<()>,
15501561
}
15511562

15521563
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
@@ -1867,6 +1878,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18671878
outbound_scid_alias: 0,
18681879

18691880
channel_pending_event_emitted: false,
1881+
funding_signed_event_emitted: false,
18701882
channel_ready_event_emitted: false,
18711883

18721884
#[cfg(any(test, fuzzing))]
@@ -1878,6 +1890,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18781890
local_initiated_shutdown: None,
18791891

18801892
blocked_monitor_updates: Vec::new(),
1893+
1894+
manually_broadcast_outbound_channels: None,
18811895
};
18821896

18831897
Ok(channel_context)
@@ -2088,6 +2102,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20882102
outbound_scid_alias,
20892103

20902104
channel_pending_event_emitted: false,
2105+
funding_signed_event_emitted: false,
20912106
channel_ready_event_emitted: false,
20922107

20932108
#[cfg(any(test, fuzzing))]
@@ -2098,6 +2113,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20982113

20992114
blocked_monitor_updates: Vec::new(),
21002115
local_initiated_shutdown: None,
2116+
manually_broadcast_outbound_channels: None,
21012117
})
21022118
}
21032119

@@ -2118,6 +2134,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21182134
self.channel_transaction_parameters.is_outbound_from_holder
21192135
}
21202136

2137+
pub fn get_funding_transaction(&self) -> Option<&Transaction> {
2138+
self.funding_transaction.as_ref()
2139+
}
2140+
21212141
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
21222142
/// Allowed in any state (including after shutdown)
21232143
pub fn get_outbound_forwarding_fee_base_msat(&self) -> u32 {
@@ -2339,6 +2359,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23392359
self.config.options.forwarding_fee_proportional_millionths
23402360
}
23412361

2362+
pub fn get_manually_broadcast_outbound_channels(&self) -> Option<()> {
2363+
self.manually_broadcast_outbound_channels
2364+
}
2365+
23422366
pub fn get_cltv_expiry_delta(&self) -> u16 {
23432367
cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
23442368
}
@@ -2365,13 +2389,25 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23652389

23662390
// Checks whether we should emit a `ChannelPending` event.
23672391
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
2368-
self.is_funding_broadcast() && !self.channel_pending_event_emitted
2392+
self.is_funding_broadcast() && !self.channel_pending_event_emitted && self.manually_broadcast_outbound_channels.is_none()
2393+
}
2394+
2395+
// Checks whether we should emit a `FundingSigned` event.
2396+
pub(crate) fn should_emit_funding_signed_event(&self) -> bool {
2397+
self.get_funding_transaction().is_some()
2398+
&& self.manually_broadcast_outbound_channels.is_some()
2399+
&& !self.funding_signed_event_emitted
23692400
}
23702401

23712402
// Returns whether we already emitted a `ChannelPending` event.
23722403
pub(crate) fn channel_pending_event_emitted(&self) -> bool {
23732404
self.channel_pending_event_emitted
23742405
}
2406+
2407+
// Returns whether we already emitted a `FundingSigned` event.
2408+
pub(crate) fn funding_signed_event_emitted(&self) -> bool {
2409+
self.funding_signed_event_emitted
2410+
}
23752411

23762412
// Remembers that we already emitted a `ChannelPending` event.
23772413
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
@@ -2388,6 +2424,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23882424
self.channel_ready_event_emitted = true;
23892425
}
23902426

2427+
// Remembers that we already emitted a `FundingSigned` event.
2428+
pub(crate) fn set_funding_signed_event_emitted(&mut self) {
2429+
self.funding_signed_event_emitted = true;
2430+
}
2431+
23912432
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
23922433
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
23932434
/// no longer be considered when forwarding HTLCs.
@@ -2424,11 +2465,23 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24242465
did_channel_update
24252466
}
24262467

2468+
/// Marking the channel as manual broadcast is used in order to prevent LDK from automatically
2469+
/// broadcasting the funding transaction.
2470+
///
2471+
/// This is useful if you wish to get hold of the funding transaction before it is broadcasted
2472+
/// via [`Events::FundingSigned`] event.
2473+
///
2474+
/// [`Event::FundingSigned`]: crate::events::Event::FundingSigned
2475+
pub fn mark_channel_as_manual_broadcast(&mut self) {
2476+
self.manually_broadcast_outbound_channels = Some(());
2477+
}
2478+
24272479
/// Returns true if funding_signed was sent/received and the
24282480
/// funding transaction has been broadcast if necessary.
24292481
pub fn is_funding_broadcast(&self) -> bool {
2430-
!self.channel_state.is_pre_funded_state() &&
2431-
!matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
2482+
!self.channel_state.is_pre_funded_state()
2483+
&& !matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if
2484+
flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
24322485
}
24332486

24342487
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -5219,10 +5272,11 @@ impl<SP: Deref> Channel<SP> where
52195272
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
52205273
// first received the funding_signed.
52215274
let mut funding_broadcastable =
5222-
if self.context.is_outbound() &&
5223-
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
5224-
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
5225-
{
5275+
if self.context.manually_broadcast_outbound_channels.is_none()
5276+
&& self.context.is_outbound()
5277+
&& (matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
5278+
if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
5279+
|| matches!(self.context.channel_state, ChannelState::ChannelReady(_))) {
52265280
self.context.funding_transaction.take()
52275281
} else { None };
52285282
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -8767,6 +8821,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87678821

87688822
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
87698823
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8824+
let funding_signed_event_emitted = Some(self.context.funding_signed_event_emitted);
87708825

87718826
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
87728827
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
@@ -8818,6 +8873,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88188873
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
88198874
// 45 and 47 are reserved for async signing
88208875
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8876+
(51, self.context.manually_broadcast_outbound_channels, option),
8877+
(53, funding_signed_event_emitted, option)
88218878
});
88228879

88238880
Ok(())
@@ -9106,6 +9163,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91069163
let mut outbound_scid_alias = None;
91079164
let mut channel_pending_event_emitted = None;
91089165
let mut channel_ready_event_emitted = None;
9166+
let mut funding_signed_event_emitted = None;
91099167

91109168
let mut user_id_high_opt: Option<u64> = None;
91119169
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9118,6 +9176,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91189176
let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
91199177

91209178
let mut is_batch_funding: Option<()> = None;
9179+
let mut manually_broadcast_outbound_channels: Option<()> = None;
91219180

91229181
let mut local_initiated_shutdown: Option<()> = None;
91239182

@@ -9159,6 +9218,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91599218
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
91609219
// 45 and 47 are reserved for async signing
91619220
(49, local_initiated_shutdown, option),
9221+
(51, manually_broadcast_outbound_channels, option),
9222+
(53, funding_signed_event_emitted, option),
91629223
});
91639224

91649225
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -9382,6 +9443,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93829443
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
93839444

93849445
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
9446+
funding_signed_event_emitted: funding_signed_event_emitted.unwrap_or(true),
93859447
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
93869448

93879449
#[cfg(any(test, fuzzing))]
@@ -9393,6 +9455,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93939455
local_initiated_shutdown,
93949456

93959457
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9458+
manually_broadcast_outbound_channels,
93969459
},
93979460
#[cfg(any(dual_funding, splicing))]
93989461
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)