Skip to content

Commit 3733103

Browse files
authored
Merge pull request #3024 from jbesraa/funding-signed-event
Funding signed event
2 parents 9ce3dd5 + 8403755 commit 3733103

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
@@ -583,6 +583,32 @@ pub enum Event {
583583
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
584584
user_channel_id: u128,
585585
},
586+
/// Used to indicate that the counterparty node has provided the signature(s) required to
587+
/// recover our funds in case they go offline.
588+
///
589+
/// It is safe (and your responsibility) to broadcast the funding transaction upon receiving this
590+
/// event.
591+
///
592+
/// This event is only emitted if you called
593+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`] instead of
594+
/// [`ChannelManager::funding_transaction_generated`].
595+
///
596+
/// [`ChannelManager::unsafe_manual_funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::unsafe_manual_funding_transaction_generated
597+
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
598+
FundingTxBroadcastSafe {
599+
/// The `channel_id` indicating which channel has reached this stage.
600+
channel_id: ChannelId,
601+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`].
602+
///
603+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
604+
user_channel_id: u128,
605+
/// The outpoint of the channel's funding transaction.
606+
funding_txo: OutPoint,
607+
/// The `node_id` of the channel counterparty.
608+
counterparty_node_id: PublicKey,
609+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
610+
former_temporary_channel_id: ChannelId,
611+
},
586612
/// Indicates that we've been offered a payment and it needs to be claimed via calling
587613
/// [`ChannelManager::claim_funds`] with the preimage given in [`PaymentPurpose`].
588614
///
@@ -1628,7 +1654,17 @@ impl Writeable for Event {
16281654
(0, payment_id, required),
16291655
(2, invoice, required),
16301656
(4, responder, option),
1631-
})
1657+
});
1658+
},
1659+
&Event::FundingTxBroadcastSafe { ref channel_id, ref user_channel_id, ref funding_txo, ref counterparty_node_id, ref former_temporary_channel_id} => {
1660+
43u8.write(writer)?;
1661+
write_tlv_fields!(writer, {
1662+
(0, channel_id, required),
1663+
(2, user_channel_id, required),
1664+
(4, funding_txo, required),
1665+
(6, counterparty_node_id, required),
1666+
(8, former_temporary_channel_id, required),
1667+
});
16321668
},
16331669
// Note that, going forward, all new events must only write data inside of
16341670
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
@@ -2081,6 +2117,27 @@ impl MaybeReadable for Event {
20812117
};
20822118
f()
20832119
},
2120+
43u8 => {
2121+
let mut channel_id = RequiredWrapper(None);
2122+
let mut user_channel_id = RequiredWrapper(None);
2123+
let mut funding_txo = RequiredWrapper(None);
2124+
let mut counterparty_node_id = RequiredWrapper(None);
2125+
let mut former_temporary_channel_id = RequiredWrapper(None);
2126+
read_tlv_fields!(reader, {
2127+
(0, channel_id, required),
2128+
(2, user_channel_id, required),
2129+
(4, funding_txo, required),
2130+
(6, counterparty_node_id, required),
2131+
(8, former_temporary_channel_id, required)
2132+
});
2133+
Ok(Some(Event::FundingTxBroadcastSafe {
2134+
channel_id: channel_id.0.unwrap(),
2135+
user_channel_id: user_channel_id.0.unwrap(),
2136+
funding_txo: funding_txo.0.unwrap(),
2137+
counterparty_node_id: counterparty_node_id.0.unwrap(),
2138+
former_temporary_channel_id: former_temporary_channel_id.0.unwrap(),
2139+
}))
2140+
},
20842141
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
20852142
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
20862143
// reads.

lightning/src/ln/channel.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,12 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13771377
counterparty_forwarding_info: Option<CounterpartyForwardingInfo>,
13781378

13791379
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
1380+
/// The transaction which funds this channel. Note that for manually-funded channels (i.e.,
1381+
/// is_manual_broadcast is true) this will be a dummy empty transaction.
13801382
funding_transaction: Option<Transaction>,
1383+
/// This flag indicates that it is the user's responsibility to validated and broadcast the
1384+
/// funding transaction.
1385+
is_manual_broadcast: bool,
13811386
is_batch_funding: Option<()>,
13821387

13831388
counterparty_cur_commitment_point: Option<PublicKey>,
@@ -1465,6 +1470,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14651470
// We track whether we already emitted a `ChannelPending` event.
14661471
channel_pending_event_emitted: bool,
14671472

1473+
// We track whether we already emitted a `FundingTxBroadcastSafe` event.
1474+
funding_tx_broadcast_safe_event_emitted: bool,
1475+
14681476
// We track whether we already emitted a `ChannelReady` event.
14691477
channel_ready_event_emitted: bool,
14701478

@@ -1805,6 +1813,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18051813
outbound_scid_alias: 0,
18061814

18071815
channel_pending_event_emitted: false,
1816+
funding_tx_broadcast_safe_event_emitted: false,
18081817
channel_ready_event_emitted: false,
18091818

18101819
#[cfg(any(test, fuzzing))]
@@ -1816,6 +1825,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18161825
local_initiated_shutdown: None,
18171826

18181827
blocked_monitor_updates: Vec::new(),
1828+
1829+
is_manual_broadcast: false,
18191830
};
18201831

18211832
Ok(channel_context)
@@ -2032,6 +2043,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20322043
outbound_scid_alias,
20332044

20342045
channel_pending_event_emitted: false,
2046+
funding_tx_broadcast_safe_event_emitted: false,
20352047
channel_ready_event_emitted: false,
20362048

20372049
#[cfg(any(test, fuzzing))]
@@ -2042,6 +2054,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20422054

20432055
blocked_monitor_updates: Vec::new(),
20442056
local_initiated_shutdown: None,
2057+
is_manual_broadcast: false,
20452058
})
20462059
}
20472060

@@ -2420,6 +2433,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24202433
self.config.options.forwarding_fee_proportional_millionths
24212434
}
24222435

2436+
pub fn is_manual_broadcast(&self) -> bool {
2437+
self.is_manual_broadcast
2438+
}
2439+
24232440
pub fn get_cltv_expiry_delta(&self) -> u16 {
24242441
cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
24252442
}
@@ -2454,6 +2471,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24542471
self.channel_pending_event_emitted
24552472
}
24562473

2474+
// Returns whether we already emitted a `FundingTxBroadcastSafe` event.
2475+
pub(crate) fn funding_tx_broadcast_safe_event_emitted(&self) -> bool {
2476+
self.funding_tx_broadcast_safe_event_emitted
2477+
}
2478+
24572479
// Remembers that we already emitted a `ChannelPending` event.
24582480
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
24592481
self.channel_pending_event_emitted = true;
@@ -2469,6 +2491,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24692491
self.channel_ready_event_emitted = true;
24702492
}
24712493

2494+
// Remembers that we already emitted a `FundingTxBroadcastSafe` event.
2495+
pub(crate) fn set_funding_tx_broadcast_safe_event_emitted(&mut self) {
2496+
self.funding_tx_broadcast_safe_event_emitted = true;
2497+
}
2498+
24722499
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
24732500
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
24742501
/// no longer be considered when forwarding HTLCs.
@@ -2505,6 +2532,17 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25052532
did_channel_update
25062533
}
25072534

2535+
/// Marking the channel as manual broadcast is used in order to prevent LDK from automatically
2536+
/// broadcasting the funding transaction.
2537+
///
2538+
/// This is useful if you wish to get hold of the funding transaction before it is broadcasted
2539+
/// via [`Event::FundingTxBroadcastSafe`] event.
2540+
///
2541+
/// [`Event::FundingTxBroadcastSafe`]: crate::events::Event::FundingTxBroadcastSafe
2542+
pub fn set_manual_broadcast(&mut self) {
2543+
self.is_manual_broadcast = true;
2544+
}
2545+
25082546
/// Returns true if funding_signed was sent/received and the
25092547
/// funding transaction has been broadcast if necessary.
25102548
pub fn is_funding_broadcast(&self) -> bool {
@@ -8871,6 +8909,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88718909

88728910
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
88738911
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8912+
let funding_tx_broadcast_safe_event_emitted = Some(self.context.funding_tx_broadcast_safe_event_emitted);
88748913

88758914
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
88768915
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
@@ -8883,6 +8922,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88838922
if !self.context.monitor_pending_update_adds.is_empty() {
88848923
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
88858924
}
8925+
let is_manual_broadcast = Some(self.context.is_manual_broadcast);
88868926

88878927
// `current_point` will become optional when async signing is implemented.
88888928
let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
@@ -8927,6 +8967,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
89278967
(45, cur_holder_commitment_point, option),
89288968
(47, next_holder_commitment_point, option),
89298969
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8970+
(51, is_manual_broadcast, option), // Added in 0.0.124
8971+
(53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124
89308972
});
89318973

89328974
Ok(())
@@ -9215,6 +9257,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92159257
let mut outbound_scid_alias = None;
92169258
let mut channel_pending_event_emitted = None;
92179259
let mut channel_ready_event_emitted = None;
9260+
let mut funding_tx_broadcast_safe_event_emitted = None;
92189261

92199262
let mut user_id_high_opt: Option<u64> = None;
92209263
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9238,6 +9281,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92389281

92399282
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
92409283
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9284+
let mut is_manual_broadcast = None;
92419285

92429286
read_tlv_fields!(reader, {
92439287
(0, announcement_sigs, option),
@@ -9272,6 +9316,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92729316
(45, cur_holder_commitment_point_opt, option),
92739317
(47, next_holder_commitment_point_opt, option),
92749318
(49, local_initiated_shutdown, option),
9319+
(51, is_manual_broadcast, option),
9320+
(53, funding_tx_broadcast_safe_event_emitted, option),
92759321
});
92769322

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

9564+
funding_tx_broadcast_safe_event_emitted: funding_tx_broadcast_safe_event_emitted.unwrap_or(false),
95189565
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
95199566
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
95209567

@@ -9527,6 +9574,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
95279574
local_initiated_shutdown,
95289575

95299576
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9577+
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
95309578
},
95319579
#[cfg(any(dual_funding, splicing))]
95329580
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)