Skip to content

Commit 1aa96df

Browse files
committed
Add FundingSigned event
*document manual* *document_safety*
1 parent 7d2d047 commit 1aa96df

File tree

5 files changed

+318
-14
lines changed

5 files changed

+318
-14
lines changed

lightning/src/events/mod.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,29 @@ impl_writeable_tlv_based_enum!(PaymentFailureReason,
497497
/// written as it makes no sense to respond to it after reconnecting to peers).
498498
#[derive(Clone, Debug, PartialEq, Eq)]
499499
pub enum Event {
500+
/// Used to indicate that the counterparty node has sent `FundingSigned` msg but we are yet to
501+
/// broadcast the funding transaction.
502+
///
503+
/// After you receive this event, you should broadcast the funding transaction and then call
504+
//// [`ChannelManager::funding_transaction_broadcasted`]. <-- not implemtened yet
505+
/// // should we have some timeout for this?
506+
//// [`ChannelManager::funding_transaction_broadcasted`]: crate::ln::channelmanager::ChannelManager::funding_transaction_broadcasted
507+
FundingSigned {
508+
/// The `channel_id` indicating which channel has completed the `FundingSigned` stage.
509+
channel_id: ChannelId,
510+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
511+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
512+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
513+
/// `user_channel_id` will be randomized for an inbound channel. This may be zero for objects
514+
/// serialized with LDK versions prior to 0.0.113.
515+
///
516+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
517+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
518+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
519+
user_channel_id: u128,
520+
/// The funding transaction which was signed by the counterparty.
521+
funding_tx: Transaction,
522+
},
500523
/// Used to indicate that the client should generate a funding transaction with the given
501524
/// parameters and then call [`ChannelManager::funding_transaction_generated`].
502525
/// Generated in [`ChannelManager`] message handling.
@@ -1446,7 +1469,15 @@ impl Writeable for Event {
14461469
write_tlv_fields!(writer, {
14471470
(0, peer_node_id, required),
14481471
});
1449-
}
1472+
},
1473+
&Event::FundingSigned { ref channel_id, ref user_channel_id, ref funding_tx } => {
1474+
41u8.write(writer)?;
1475+
write_tlv_fields!(writer, {
1476+
(0, channel_id, required),
1477+
(2, user_channel_id, required),
1478+
(4, funding_tx, required),
1479+
});
1480+
},
14501481
// Note that, going forward, all new events must only write data inside of
14511482
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
14521483
// data via `write_tlv_fields`.
@@ -1883,6 +1914,21 @@ impl MaybeReadable for Event {
18831914
};
18841915
f()
18851916
},
1917+
41u8 => {
1918+
let mut channel_id = ChannelId::new_zero();
1919+
let mut user_channel_id: u128 = 0;
1920+
let mut funding_tx = Transaction { version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
1921+
read_tlv_fields!(reader, {
1922+
(0, channel_id, required),
1923+
(2, user_channel_id, required),
1924+
(4, funding_tx, required),
1925+
});
1926+
Ok(Some(Event::FundingSigned {
1927+
channel_id,
1928+
user_channel_id,
1929+
funding_tx,
1930+
}))
1931+
},
18861932
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
18871933
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
18881934
// reads.

lightning/src/ln/channel.rs

+66-5
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15291529

15301530
// We track whether we already emitted a `ChannelPending` event.
15311531
channel_pending_event_emitted: bool,
1532+
1533+
// We track whether we already emitted a `FundingSigned` event.
1534+
funding_signed_event_emitted: bool,
15321535

15331536
// We track whether we already emitted a `ChannelReady` event.
15341537
channel_ready_event_emitted: bool,
@@ -1546,6 +1549,16 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15461549
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
15471550
/// store it here and only release it to the `ChannelManager` once it asks for it.
15481551
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1552+
1553+
/// Upon receving a `FundingSigned` message, if the channel is
1554+
/// outbound and we are ready to broadcast the funding
1555+
/// transaction, LDK will broadcast it and set the channel state
1556+
/// to `ChannelPending`.
1557+
///
1558+
/// Using this flag will allow you to halt the broadcast of the
1559+
/// funding transaction and the channel state will be set to
1560+
/// `FundingSigned`.
1561+
manually_broadcast_outbound_channels: Option<()>,
15491562
}
15501563

15511564
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
@@ -1866,6 +1879,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18661879
outbound_scid_alias: 0,
18671880

18681881
channel_pending_event_emitted: false,
1882+
funding_signed_event_emitted: false,
18691883
channel_ready_event_emitted: false,
18701884

18711885
#[cfg(any(test, fuzzing))]
@@ -1877,6 +1891,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18771891
local_initiated_shutdown: None,
18781892

18791893
blocked_monitor_updates: Vec::new(),
1894+
1895+
manually_broadcast_outbound_channels: None,
18801896
};
18811897

18821898
Ok(channel_context)
@@ -2087,6 +2103,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20872103
outbound_scid_alias,
20882104

20892105
channel_pending_event_emitted: false,
2106+
funding_signed_event_emitted: false,
20902107
channel_ready_event_emitted: false,
20912108

20922109
#[cfg(any(test, fuzzing))]
@@ -2097,6 +2114,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20972114

20982115
blocked_monitor_updates: Vec::new(),
20992116
local_initiated_shutdown: None,
2117+
manually_broadcast_outbound_channels: None,
21002118
})
21012119
}
21022120

@@ -2117,6 +2135,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21172135
self.channel_transaction_parameters.is_outbound_from_holder
21182136
}
21192137

2138+
pub fn get_funding_transaction(&self) -> Option<&Transaction> {
2139+
self.funding_transaction.as_ref()
2140+
}
2141+
21202142
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
21212143
/// Allowed in any state (including after shutdown)
21222144
pub fn get_outbound_forwarding_fee_base_msat(&self) -> u32 {
@@ -2338,6 +2360,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23382360
self.config.options.forwarding_fee_proportional_millionths
23392361
}
23402362

2363+
pub fn get_manually_broadcast_outbound_channels(&self) -> Option<()> {
2364+
self.manually_broadcast_outbound_channels
2365+
}
2366+
23412367
pub fn get_cltv_expiry_delta(&self) -> u16 {
23422368
cmp::max(self.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
23432369
}
@@ -2364,13 +2390,25 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23642390

23652391
// Checks whether we should emit a `ChannelPending` event.
23662392
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
2367-
self.is_funding_broadcast() && !self.channel_pending_event_emitted
2393+
self.is_funding_broadcast() && !self.channel_pending_event_emitted && self.manually_broadcast_outbound_channels.is_none()
2394+
}
2395+
2396+
// Checks whether we should emit a `FundingSigned` event.
2397+
pub(crate) fn should_emit_funding_signed_event(&self) -> bool {
2398+
self.get_funding_transaction().is_some()
2399+
&& self.manually_broadcast_outbound_channels.is_some()
2400+
&& !self.funding_signed_event_emitted
23682401
}
23692402

23702403
// Returns whether we already emitted a `ChannelPending` event.
23712404
pub(crate) fn channel_pending_event_emitted(&self) -> bool {
23722405
self.channel_pending_event_emitted
23732406
}
2407+
2408+
// Returns whether we already emitted a `FundingSigned` event.
2409+
pub(crate) fn funding_signed_event_emitted(&self) -> bool {
2410+
self.funding_signed_event_emitted
2411+
}
23742412

23752413
// Remembers that we already emitted a `ChannelPending` event.
23762414
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
@@ -2387,6 +2425,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23872425
self.channel_ready_event_emitted = true;
23882426
}
23892427

2428+
// Remembers that we already emitted a `FundingSigned` event.
2429+
pub(crate) fn set_funding_signed_event_emitted(&mut self) {
2430+
self.funding_signed_event_emitted = true;
2431+
}
2432+
23902433
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
23912434
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
23922435
/// no longer be considered when forwarding HTLCs.
@@ -2423,11 +2466,17 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24232466
did_channel_update
24242467
}
24252468

2469+
/// Fix It
2470+
pub fn update_manual_channel(&mut self) {
2471+
self.manually_broadcast_outbound_channels = Some(());
2472+
}
2473+
24262474
/// Returns true if funding_signed was sent/received and the
24272475
/// funding transaction has been broadcast if necessary.
24282476
pub fn is_funding_broadcast(&self) -> bool {
2429-
!self.channel_state.is_pre_funded_state() &&
2430-
!matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
2477+
!self.channel_state.is_pre_funded_state()
2478+
&& !matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if
2479+
flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
24312480
}
24322481

24332482
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -4131,7 +4180,7 @@ impl<SP: Deref> Channel<SP> where
41314180
// If we reconnected before sending our `channel_ready` they may still resend theirs.
41324181
check_reconnection = true;
41334182
} else if flags.clone().clear(AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty() {
4134-
self.context.channel_state.set_their_channel_ready();
4183+
self.context.channel_state.set_their_channel_ready();
41354184
} else if flags == AwaitingChannelReadyFlags::OUR_CHANNEL_READY {
41364185
self.context.channel_state = ChannelState::ChannelReady(self.context.channel_state.with_funded_state_flags_mask().into());
41374186
self.context.update_time_counter += 1;
@@ -5222,10 +5271,13 @@ impl<SP: Deref> Channel<SP> where
52225271
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
52235272
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
52245273
{
5225-
self.context.funding_transaction.take()
5274+
// this clone is added so the value can still be accessed after the take
5275+
// (which is necessary for the FundingSigned event)
5276+
self.context.funding_transaction.clone().take()
52265277
} else { None };
52275278
// That said, if the funding transaction is already confirmed (ie we're active with a
52285279
// minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx.
5280+
52295281
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) && self.context.minimum_depth != Some(0) {
52305282
funding_broadcastable = None;
52315283
}
@@ -8766,6 +8818,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87668818

87678819
let channel_pending_event_emitted = Some(self.context.channel_pending_event_emitted);
87688820
let channel_ready_event_emitted = Some(self.context.channel_ready_event_emitted);
8821+
let funding_signed_event_emitted = Some(self.context.funding_signed_event_emitted);
87698822

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

88228877
Ok(())
@@ -9105,6 +9160,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91059160
let mut outbound_scid_alias = None;
91069161
let mut channel_pending_event_emitted = None;
91079162
let mut channel_ready_event_emitted = None;
9163+
let mut funding_signed_event_emitted = None;
91089164

91099165
let mut user_id_high_opt: Option<u64> = None;
91109166
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9117,6 +9173,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91179173
let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
91189174

91199175
let mut is_batch_funding: Option<()> = None;
9176+
let mut manually_broadcast_outbound_channels: Option<()> = None;
91209177

91219178
let mut local_initiated_shutdown: Option<()> = None;
91229179

@@ -9158,6 +9215,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91589215
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
91599216
// 45 and 47 are reserved for async signing
91609217
(49, local_initiated_shutdown, option),
9218+
(51, manually_broadcast_outbound_channels, option),
9219+
(53, funding_signed_event_emitted, option),
91619220
});
91629221

91639222
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -9381,6 +9440,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93819440
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
93829441

93839442
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
9443+
funding_signed_event_emitted: funding_signed_event_emitted.unwrap_or(true),
93849444
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
93859445

93869446
#[cfg(any(test, fuzzing))]
@@ -9392,6 +9452,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93929452
local_initiated_shutdown,
93939453

93949454
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
9455+
manually_broadcast_outbound_channels,
93959456
},
93969457
#[cfg(any(dual_funding, splicing))]
93979458
dual_funding_channel_context: None,

0 commit comments

Comments
 (0)