Skip to content

Commit d0e779f

Browse files
committed
Add FundingTxBroadcastSafe event
The `FundingTxBroadcastSafe` event indicates that we have received `funding_signed` message from our counterparty and that you should 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 87fc324 commit d0e779f

File tree

5 files changed

+399
-40
lines changed

5 files changed

+399
-40
lines changed

lightning/src/events/mod.rs

+63-1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,37 @@ pub enum Event {
579579
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
580580
user_channel_id: u128,
581581
},
582+
/// Used to indicate that the counterparty node has provided the signature(s) required to
583+
/// recover our funds in case they go offline.
584+
///
585+
/// It is safe (and your responsibility) to broadcast the funding transaction upon receiving this
586+
/// event.
587+
///
588+
/// This event is only emitted if you called
589+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`] instead of
590+
/// [`ChannelManager::funding_transaction_generated`].
591+
///
592+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
593+
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
594+
FundingTxBroadcastSafe {
595+
/// The `channel_id` indicating which channel has reached this stage.
596+
channel_id: ChannelId,
597+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
598+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
599+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to `true`. Otherwise
600+
/// `user_channel_id` will be randomized for an inbound channel.
601+
///
602+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
603+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
604+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
605+
user_channel_id: u128,
606+
/// The outpoint of the channel's funding transaction.
607+
funding_txo: OutPoint,
608+
/// The `node_id` of the channel counterparty.
609+
counterparty_node_id: PublicKey,
610+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
611+
former_temporary_channel_id: ChannelId,
612+
},
582613
/// Indicates that we've been offered a payment and it needs to be claimed via calling
583614
/// [`ChannelManager::claim_funds`] with the preimage given in [`PaymentPurpose`].
584615
///
@@ -1528,7 +1559,17 @@ impl Writeable for Event {
15281559
(0, payment_id, required),
15291560
(2, invoice, required),
15301561
(4, responder, option),
1531-
})
1562+
});
1563+
},
1564+
&Event::FundingTxBroadcastSafe { ref channel_id, ref user_channel_id, ref funding_txo, ref counterparty_node_id, ref former_temporary_channel_id} => {
1565+
43u8.write(writer)?;
1566+
write_tlv_fields!(writer, {
1567+
(0, channel_id, required),
1568+
(2, user_channel_id, required),
1569+
(4, funding_txo, required),
1570+
(6, counterparty_node_id, required),
1571+
(8, former_temporary_channel_id, required),
1572+
});
15321573
},
15331574
// Note that, going forward, all new events must only write data inside of
15341575
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
@@ -1981,6 +2022,27 @@ impl MaybeReadable for Event {
19812022
};
19822023
f()
19832024
},
2025+
43u8 => {
2026+
let mut channel_id = RequiredWrapper(None);
2027+
let mut user_channel_id = RequiredWrapper(None);
2028+
let mut funding_txo = RequiredWrapper(None);
2029+
let mut counterparty_node_id = RequiredWrapper(None);
2030+
let mut former_temporary_channel_id = RequiredWrapper(None);
2031+
read_tlv_fields!(reader, {
2032+
(0, channel_id, required),
2033+
(2, user_channel_id, required),
2034+
(4, funding_txo, required),
2035+
(6, counterparty_node_id, required),
2036+
(8, former_temporary_channel_id, required)
2037+
});
2038+
Ok(Some(Event::FundingTxBroadcastSafe {
2039+
channel_id: channel_id.0.unwrap(),
2040+
user_channel_id: user_channel_id.0.unwrap(),
2041+
funding_txo: funding_txo.0.unwrap(),
2042+
counterparty_node_id: counterparty_node_id.0.unwrap(),
2043+
former_temporary_channel_id: former_temporary_channel_id.0.unwrap(),
2044+
}))
2045+
},
19842046
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
19852047
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
19862048
// reads.

lightning/src/ln/channel.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,12 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13311331
counterparty_forwarding_info: Option<CounterpartyForwardingInfo>,
13321332

13331333
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
1334+
/// The transaction which funds this channel. Note that for manually-funded channels (i.e.,
1335+
/// is_manual_broadcast is true) this will be a dummy empty transaction.
13341336
funding_transaction: Option<Transaction>,
1337+
/// This flag indicates that it is the user's responsibility to validated and broadcast the
1338+
/// funding transaction.
1339+
is_manual_broadcast: bool,
13351340
is_batch_funding: Option<()>,
13361341

13371342
counterparty_cur_commitment_point: Option<PublicKey>,
@@ -1419,6 +1424,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14191424
// We track whether we already emitted a `ChannelPending` event.
14201425
channel_pending_event_emitted: bool,
14211426

1427+
// We track whether we already emitted a `FundingTxBroadcastSafe` event.
1428+
funding_tx_broadcast_safe_event_emitted: bool,
1429+
14221430
// We track whether we already emitted a `ChannelReady` event.
14231431
channel_ready_event_emitted: bool,
14241432

@@ -1758,6 +1766,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17581766
outbound_scid_alias: 0,
17591767

17601768
channel_pending_event_emitted: false,
1769+
funding_tx_broadcast_safe_event_emitted: false,
17611770
channel_ready_event_emitted: false,
17621771

17631772
#[cfg(any(test, fuzzing))]
@@ -1769,6 +1778,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17691778
local_initiated_shutdown: None,
17701779

17711780
blocked_monitor_updates: Vec::new(),
1781+
1782+
is_manual_broadcast: false,
17721783
};
17731784

17741785
Ok(channel_context)
@@ -1982,6 +1993,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19821993
outbound_scid_alias,
19831994

19841995
channel_pending_event_emitted: false,
1996+
funding_tx_broadcast_safe_event_emitted: false,
19851997
channel_ready_event_emitted: false,
19861998

19871999
#[cfg(any(test, fuzzing))]
@@ -1992,6 +2004,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19922004

19932005
blocked_monitor_updates: Vec::new(),
19942006
local_initiated_shutdown: None,
2007+
is_manual_broadcast: false,
19952008
})
19962009
}
19972010

@@ -2370,6 +2383,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23702383
self.config.options.forwarding_fee_proportional_millionths
23712384
}
23722385

2386+
pub fn is_manual_broadcast(&self) -> bool {
2387+
self.is_manual_broadcast
2388+
}
2389+
23732390
pub fn get_cltv_expiry_delta(&self) -> u16 {
23742391
cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
23752392
}
@@ -2404,6 +2421,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24042421
self.channel_pending_event_emitted
24052422
}
24062423

2424+
// Returns whether we already emitted a `FundingTxBroadcastSafe` event.
2425+
pub(crate) fn funding_tx_broadcast_safe_event_emitted(&self) -> bool {
2426+
self.funding_tx_broadcast_safe_event_emitted
2427+
}
2428+
24072429
// Remembers that we already emitted a `ChannelPending` event.
24082430
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
24092431
self.channel_pending_event_emitted = true;
@@ -2419,6 +2441,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24192441
self.channel_ready_event_emitted = true;
24202442
}
24212443

2444+
// Remembers that we already emitted a `FundingTxBroadcastSafe` event.
2445+
pub(crate) fn set_funding_tx_broadcast_safe_event_emitted(&mut self) {
2446+
self.funding_tx_broadcast_safe_event_emitted = true;
2447+
}
2448+
24222449
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
24232450
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
24242451
/// no longer be considered when forwarding HTLCs.
@@ -2455,6 +2482,17 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24552482
did_channel_update
24562483
}
24572484

2485+
/// Marking the channel as manual broadcast is used in order to prevent LDK from automatically
2486+
/// broadcasting the funding transaction.
2487+
///
2488+
/// This is useful if you wish to get hold of the funding transaction before it is broadcasted
2489+
/// via [`Event::FundingTxBroadcastSafe`] event.
2490+
///
2491+
/// [`Event::FundingTxBroadcastSafe`]: crate::events::Event::FundingTxBroadcastSafe
2492+
pub fn set_manual_broadcast(&mut self) {
2493+
self.is_manual_broadcast = true;
2494+
}
2495+
24582496
/// Returns true if funding_signed was sent/received and the
24592497
/// funding transaction has been broadcast if necessary.
24602498
pub fn is_funding_broadcast(&self) -> bool {
@@ -8705,6 +8743,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87058743

87068744
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
87078745
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8746+
let funding_tx_broadcast_safe_event_emitted = Some(self.context.funding_tx_broadcast_safe_event_emitted);
87088747

87098748
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
87108749
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
@@ -8717,6 +8756,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87178756
if !self.context.monitor_pending_update_adds.is_empty() {
87188757
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
87198758
}
8759+
let is_manual_broadcast = Some(self.context.is_manual_broadcast);
87208760

87218761
// `current_point` will become optional when async signing is implemented.
87228762
let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
@@ -8761,6 +8801,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87618801
(45, cur_holder_commitment_point, option),
87628802
(47, next_holder_commitment_point, option),
87638803
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8804+
(51, is_manual_broadcast, option), // Added in 0.0.124
8805+
(53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124
87648806
});
87658807

87668808
Ok(())
@@ -9049,6 +9091,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
90499091
let mut outbound_scid_alias = None;
90509092
let mut channel_pending_event_emitted = None;
90519093
let mut channel_ready_event_emitted = None;
9094+
let mut funding_tx_broadcast_safe_event_emitted = None;
90529095

90539096
let mut user_id_high_opt: Option<u64> = None;
90549097
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9072,6 +9115,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
90729115

90739116
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
90749117
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9118+
let mut is_manual_broadcast = None;
90759119

90769120
read_tlv_fields!(reader, {
90779121
(0, announcement_sigs, option),
@@ -9106,6 +9150,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91069150
(45, cur_holder_commitment_point_opt, option),
91079151
(47, next_holder_commitment_point_opt, option),
91089152
(49, local_initiated_shutdown, option),
9153+
(51, is_manual_broadcast, option),
9154+
(53, funding_tx_broadcast_safe_event_emitted, option),
91099155
});
91109156

91119157
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -9346,6 +9392,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93469392
// Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
93479393
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
93489394

9395+
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
93499396
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
93509397
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
93519398

@@ -9358,6 +9405,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93589405
local_initiated_shutdown,
93599406

93609407
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9408+
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
93619409
},
93629410
#[cfg(any(dual_funding, splicing))]
93639411
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)