Skip to content

Commit b21c02c

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 993cd1e commit b21c02c

File tree

5 files changed

+321
-6
lines changed

5 files changed

+321
-6
lines changed

lightning/src/events/mod.rs

+70-1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,39 @@ 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`]: crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
545+
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
546+
FundingSigned {
547+
/// The `channel_id` indicating which channel has completed the `FundingSigned` stage.
548+
channel_id: ChannelId,
549+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
550+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
551+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
552+
/// `user_channel_id` will be randomized for an inbound channel. This may be zero for objects
553+
/// serialized with LDK versions prior to 0.0.113.
554+
///
555+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
556+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
557+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
558+
user_channel_id: u128,
559+
/// The funding transaction which was signed by the counterparty.
560+
funding_tx: Transaction,
561+
/// The `node_id` of the channel counterparty.
562+
counterparty_node_id: PublicKey,
563+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
564+
///
565+
/// Will be `None` for channels created prior to LDK version 0.0.115.
566+
former_temporary_channel_id: Option<ChannelId>,
567+
},
535568
/// Indicates that we've been offered a payment and it needs to be claimed via calling
536569
/// [`ChannelManager::claim_funds`] with the preimage given in [`PaymentPurpose`].
537570
///
@@ -969,9 +1002,14 @@ pub enum Event {
9691002
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
9701003
/// confirmation on-chain.
9711004
///
1005+
/// Note that this event wont be emitted for channels created using
1006+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]:
1007+
///
9721008
/// This event is emitted when the funding transaction has been signed and is broadcast to the
9731009
/// network. For 0conf channels it will be immediately followed by the corresponding
9741010
/// [`Event::ChannelReady`] event.
1011+
///
1012+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
9751013
ChannelPending {
9761014
/// The `channel_id` of the channel that is pending confirmation.
9771015
channel_id: ChannelId,
@@ -1447,7 +1485,17 @@ impl Writeable for Event {
14471485
write_tlv_fields!(writer, {
14481486
(0, peer_node_id, required),
14491487
});
1450-
}
1488+
},
1489+
&Event::FundingSigned { ref channel_id, ref user_channel_id, ref funding_tx, ref counterparty_node_id, ref former_temporary_channel_id} => {
1490+
41u8.write(writer)?;
1491+
write_tlv_fields!(writer, {
1492+
(0, channel_id, required),
1493+
(2, user_channel_id, required),
1494+
(4, funding_tx, required),
1495+
(6, counterparty_node_id, required),
1496+
(8, former_temporary_channel_id, required),
1497+
});
1498+
},
14511499
// Note that, going forward, all new events must only write data inside of
14521500
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
14531501
// data via `write_tlv_fields`.
@@ -1884,6 +1932,27 @@ impl MaybeReadable for Event {
18841932
};
18851933
f()
18861934
},
1935+
41u8 => {
1936+
let mut channel_id = RequiredWrapper(None);
1937+
let mut user_channel_id = RequiredWrapper(None);
1938+
let mut funding_tx = RequiredWrapper(None);
1939+
let mut counterparty_node_id = RequiredWrapper(None);
1940+
let mut former_temporary_channel_id = None;
1941+
read_tlv_fields!(reader, {
1942+
(0, channel_id, required),
1943+
(2, user_channel_id, required),
1944+
(4, funding_tx, required),
1945+
(6, counterparty_node_id, required),
1946+
(8, former_temporary_channel_id, required)
1947+
});
1948+
Ok(Some(Event::FundingSigned {
1949+
channel_id: channel_id.0.unwrap(),
1950+
user_channel_id: user_channel_id.0.unwrap(),
1951+
funding_tx: funding_tx.0.unwrap(),
1952+
counterparty_node_id: counterparty_node_id.0.unwrap(),
1953+
former_temporary_channel_id
1954+
}))
1955+
},
18871956
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
18881957
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
18891958
// reads.

lightning/src/ln/channel.rs

+50
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

@@ -2339,6 +2355,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23392355
self.config.options.forwarding_fee_proportional_millionths
23402356
}
23412357

2358+
pub fn get_manually_broadcast_outbound_channels(&self) -> Option<()> {
2359+
self.manually_broadcast_outbound_channels
2360+
}
2361+
23422362
pub fn get_cltv_expiry_delta(&self) -> u16 {
23432363
cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
23442364
}
@@ -2373,6 +2393,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23732393
self.channel_pending_event_emitted
23742394
}
23752395

2396+
// Returns whether we already emitted a `FundingSigned` event.
2397+
pub(crate) fn funding_signed_event_emitted(&self) -> bool {
2398+
self.funding_signed_event_emitted
2399+
}
2400+
23762401
// Remembers that we already emitted a `ChannelPending` event.
23772402
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
23782403
self.channel_pending_event_emitted = true;
@@ -2388,6 +2413,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23882413
self.channel_ready_event_emitted = true;
23892414
}
23902415

2416+
// Remembers that we already emitted a `FundingSigned` event.
2417+
pub(crate) fn set_funding_signed_event_emitted(&mut self) {
2418+
self.funding_signed_event_emitted = true;
2419+
}
2420+
23912421
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
23922422
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
23932423
/// no longer be considered when forwarding HTLCs.
@@ -2424,6 +2454,17 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24242454
did_channel_update
24252455
}
24262456

2457+
/// Marking the channel as manual broadcast is used in order to prevent LDK from automatically
2458+
/// broadcasting the funding transaction.
2459+
///
2460+
/// This is useful if you wish to get hold of the funding transaction before it is broadcasted
2461+
/// via [`Event::FundingSigned`] event.
2462+
///
2463+
/// [`Event::FundingSigned`]: crate::events::Event::FundingSigned
2464+
pub fn mark_channel_as_manual_broadcast(&mut self) {
2465+
self.manually_broadcast_outbound_channels = Some(());
2466+
}
2467+
24272468
/// Returns true if funding_signed was sent/received and the
24282469
/// funding transaction has been broadcast if necessary.
24292470
pub fn is_funding_broadcast(&self) -> bool {
@@ -8767,6 +8808,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87678808

87688809
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
87698810
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8811+
let funding_signed_event_emitted = Some(self.context.funding_signed_event_emitted);
87708812

87718813
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
87728814
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
@@ -8818,6 +8860,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88188860
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
88198861
// 45 and 47 are reserved for async signing
88208862
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8863+
(51, self.context.manually_broadcast_outbound_channels, option),
8864+
(53, funding_signed_event_emitted, option)
88218865
});
88228866

88238867
Ok(())
@@ -9106,6 +9150,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91069150
let mut outbound_scid_alias = None;
91079151
let mut channel_pending_event_emitted = None;
91089152
let mut channel_ready_event_emitted = None;
9153+
let mut funding_signed_event_emitted = None;
91099154

91109155
let mut user_id_high_opt: Option<u64> = None;
91119156
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9118,6 +9163,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91189163
let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
91199164

91209165
let mut is_batch_funding: Option<()> = None;
9166+
let mut manually_broadcast_outbound_channels: Option<()> = None;
91219167

91229168
let mut local_initiated_shutdown: Option<()> = None;
91239169

@@ -9159,6 +9205,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91599205
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
91609206
// 45 and 47 are reserved for async signing
91619207
(49, local_initiated_shutdown, option),
9208+
(51, manually_broadcast_outbound_channels, option),
9209+
(53, funding_signed_event_emitted, option),
91629210
});
91639211

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

93849432
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
9433+
funding_signed_event_emitted: funding_signed_event_emitted.unwrap_or(true),
93859434
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
93869435

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

93959444
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9445+
manually_broadcast_outbound_channels,
93969446
},
93979447
#[cfg(any(dual_funding, splicing))]
93989448
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)