Skip to content

Commit 8403755

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 bfc20f8 commit 8403755

File tree

4 files changed

+321
-31
lines changed

4 files changed

+321
-31
lines changed

lightning/src/events/mod.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,32 @@ 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`].
598+
///
599+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
600+
user_channel_id: u128,
601+
/// The outpoint of the channel's funding transaction.
602+
funding_txo: OutPoint,
603+
/// The `node_id` of the channel counterparty.
604+
counterparty_node_id: PublicKey,
605+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
606+
former_temporary_channel_id: ChannelId,
607+
},
582608
/// Indicates that we've been offered a payment and it needs to be claimed via calling
583609
/// [`ChannelManager::claim_funds`] with the preimage given in [`PaymentPurpose`].
584610
///
@@ -1528,7 +1554,17 @@ impl Writeable for Event {
15281554
(0, payment_id, required),
15291555
(2, invoice, required),
15301556
(4, responder, option),
1531-
})
1557+
});
1558+
},
1559+
&Event::FundingTxBroadcastSafe { ref channel_id, ref user_channel_id, ref funding_txo, ref counterparty_node_id, ref former_temporary_channel_id} => {
1560+
43u8.write(writer)?;
1561+
write_tlv_fields!(writer, {
1562+
(0, channel_id, required),
1563+
(2, user_channel_id, required),
1564+
(4, funding_txo, required),
1565+
(6, counterparty_node_id, required),
1566+
(8, former_temporary_channel_id, required),
1567+
});
15321568
},
15331569
// Note that, going forward, all new events must only write data inside of
15341570
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
@@ -1981,6 +2017,27 @@ impl MaybeReadable for Event {
19812017
};
19822018
f()
19832019
},
2020+
43u8 => {
2021+
let mut channel_id = RequiredWrapper(None);
2022+
let mut user_channel_id = RequiredWrapper(None);
2023+
let mut funding_txo = RequiredWrapper(None);
2024+
let mut counterparty_node_id = RequiredWrapper(None);
2025+
let mut former_temporary_channel_id = RequiredWrapper(None);
2026+
read_tlv_fields!(reader, {
2027+
(0, channel_id, required),
2028+
(2, user_channel_id, required),
2029+
(4, funding_txo, required),
2030+
(6, counterparty_node_id, required),
2031+
(8, former_temporary_channel_id, required)
2032+
});
2033+
Ok(Some(Event::FundingTxBroadcastSafe {
2034+
channel_id: channel_id.0.unwrap(),
2035+
user_channel_id: user_channel_id.0.unwrap(),
2036+
funding_txo: funding_txo.0.unwrap(),
2037+
counterparty_node_id: counterparty_node_id.0.unwrap(),
2038+
former_temporary_channel_id: former_temporary_channel_id.0.unwrap(),
2039+
}))
2040+
},
19842041
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
19852042
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
19862043
// reads.

lightning/src/ln/channel.rs

Lines changed: 48 additions & 0 deletions
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 {
@@ -8706,6 +8744,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87068744

87078745
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
87088746
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8747+
let funding_tx_broadcast_safe_event_emitted = Some(self.context.funding_tx_broadcast_safe_event_emitted);
87098748

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

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

87678809
Ok(())
@@ -9050,6 +9092,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
90509092
let mut outbound_scid_alias = None;
90519093
let mut channel_pending_event_emitted = None;
90529094
let mut channel_ready_event_emitted = None;
9095+
let mut funding_tx_broadcast_safe_event_emitted = None;
90539096

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

90749117
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
90759118
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9119+
let mut is_manual_broadcast = None;
90769120

90779121
read_tlv_fields!(reader, {
90789122
(0, announcement_sigs, option),
@@ -9107,6 +9151,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91079151
(45, cur_holder_commitment_point_opt, option),
91089152
(47, next_holder_commitment_point_opt, option),
91099153
(49, local_initiated_shutdown, option),
9154+
(51, is_manual_broadcast, option),
9155+
(53, funding_tx_broadcast_safe_event_emitted, option),
91109156
});
91119157

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

9396+
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
93509397
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
93519398
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
93529399

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

93619408
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9409+
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
93629410
},
93639411
#[cfg(any(dual_funding, splicing))]
93649412
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)