Skip to content

Commit 7ca3709

Browse files
authored
Merge pull request #2098 from tnull/2023-03-add-channel-pending-event
Add `ChannelPending` event emitted upon `funding_signed`
2 parents 34e176b + 9873c7d commit 7ca3709

File tree

10 files changed

+245
-26
lines changed

10 files changed

+245
-26
lines changed

fuzz/src/chanmon_consistency.rs

+11
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,18 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
526526
msg.clone()
527527
} else { panic!("Wrong event type"); }
528528
};
529+
let events = $dest.get_and_clear_pending_events();
530+
assert_eq!(events.len(), 1);
531+
if let events::Event::ChannelPending{ ref counterparty_node_id, .. } = events[0] {
532+
assert_eq!(counterparty_node_id, &$source.get_our_node_id());
533+
} else { panic!("Wrong event type"); }
534+
529535
$source.handle_funding_signed(&$dest.get_our_node_id(), &funding_signed);
536+
let events = $source.get_and_clear_pending_events();
537+
assert_eq!(events.len(), 1);
538+
if let events::Event::ChannelPending{ ref counterparty_node_id, .. } = events[0] {
539+
assert_eq!(counterparty_node_id, &$dest.get_our_node_id());
540+
} else { panic!("Wrong event type"); }
530541

531542
funding_output
532543
} }

lightning-background-processor/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ mod tests {
710710
use lightning::chain::keysinterface::{InMemorySigner, KeysManager};
711711
use lightning::chain::transaction::OutPoint;
712712
use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent};
713-
use lightning::get_event_msg;
713+
use lightning::{get_event_msg, get_event};
714714
use lightning::ln::PaymentHash;
715715
use lightning::ln::channelmanager;
716716
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId};
@@ -1058,7 +1058,10 @@ mod tests {
10581058
($node_a: expr, $node_b: expr, $temporary_channel_id: expr, $tx: expr) => {{
10591059
$node_a.node.funding_transaction_generated(&$temporary_channel_id, &$node_b.node.get_our_node_id(), $tx.clone()).unwrap();
10601060
$node_b.node.handle_funding_created(&$node_a.node.get_our_node_id(), &get_event_msg!($node_a, MessageSendEvent::SendFundingCreated, $node_b.node.get_our_node_id()));
1061+
get_event!($node_b, Event::ChannelPending);
1062+
10611063
$node_a.node.handle_funding_signed(&$node_b.node.get_our_node_id(), &get_event_msg!($node_b, MessageSendEvent::SendFundingSigned, $node_a.node.get_our_node_id()));
1064+
get_event!($node_a, Event::ChannelPending);
10621065
}}
10631066
}
10641067

lightning/src/events/mod.rs

+68-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReada
3232
use crate::util::string::UntrustedString;
3333
use crate::routing::router::{RouteHop, RouteParameters};
3434

35-
use bitcoin::{PackedLockTime, Transaction};
35+
use bitcoin::{PackedLockTime, Transaction, OutPoint};
36+
#[cfg(anchors)]
37+
use bitcoin::{Txid, TxIn, TxOut, Witness};
3638
use bitcoin::blockdata::script::Script;
3739
use bitcoin::hashes::Hash;
3840
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -608,12 +610,39 @@ pub enum Event {
608610
/// The caveat described above the `fee_earned_msat` field applies here as well.
609611
outbound_amount_forwarded_msat: Option<u64>,
610612
},
613+
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
614+
/// confirmation on-chain.
615+
///
616+
/// This event is emitted when the funding transaction has been signed and is broadcast to the
617+
/// network. For 0conf channels it will be immediately followed by the corresponding
618+
/// [`Event::ChannelReady`] event.
619+
ChannelPending {
620+
/// The `channel_id` of the channel that is pending confirmation.
621+
channel_id: [u8; 32],
622+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
623+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
624+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
625+
/// `user_channel_id` will be randomized for an inbound channel.
626+
///
627+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
628+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
629+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
630+
user_channel_id: u128,
631+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
632+
///
633+
/// Will be `None` for channels created prior to LDK version 0.0.115.
634+
former_temporary_channel_id: Option<[u8; 32]>,
635+
/// The `node_id` of the channel counterparty.
636+
counterparty_node_id: PublicKey,
637+
/// The outpoint of the channel's funding transaction.
638+
funding_txo: OutPoint,
639+
},
611640
/// Used to indicate that a channel with the given `channel_id` is ready to
612641
/// be used. This event is emitted either when the funding transaction has been confirmed
613642
/// on-chain, or, in case of a 0conf channel, when both parties have confirmed the channel
614643
/// establishment.
615644
ChannelReady {
616-
/// The channel_id of the channel that is ready.
645+
/// The `channel_id` of the channel that is ready.
617646
channel_id: [u8; 32],
618647
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
619648
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
@@ -624,15 +653,15 @@ pub enum Event {
624653
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
625654
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
626655
user_channel_id: u128,
627-
/// The node_id of the channel counterparty.
656+
/// The `node_id` of the channel counterparty.
628657
counterparty_node_id: PublicKey,
629658
/// The features that this channel will operate with.
630659
channel_type: ChannelTypeFeatures,
631660
},
632661
/// Used to indicate that a previously opened channel with the given `channel_id` is in the
633662
/// process of closure.
634663
ChannelClosed {
635-
/// The channel_id of the channel which has been closed. Note that on-chain transactions
664+
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
636665
/// resolving the channel are likely still awaiting confirmation.
637666
channel_id: [u8; 32],
638667
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
@@ -931,6 +960,16 @@ impl Writeable for Event {
931960
(6, channel_type, required),
932961
});
933962
},
963+
&Event::ChannelPending { ref channel_id, ref user_channel_id, ref former_temporary_channel_id, ref counterparty_node_id, ref funding_txo } => {
964+
31u8.write(writer)?;
965+
write_tlv_fields!(writer, {
966+
(0, channel_id, required),
967+
(2, user_channel_id, required),
968+
(4, former_temporary_channel_id, required),
969+
(6, counterparty_node_id, required),
970+
(8, funding_txo, required),
971+
});
972+
},
934973
// Note that, going forward, all new events must only write data inside of
935974
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
936975
// data via `write_tlv_fields`.
@@ -1271,6 +1310,31 @@ impl MaybeReadable for Event {
12711310
};
12721311
f()
12731312
},
1313+
31u8 => {
1314+
let f = || {
1315+
let mut channel_id = [0; 32];
1316+
let mut user_channel_id: u128 = 0;
1317+
let mut former_temporary_channel_id = None;
1318+
let mut counterparty_node_id = RequiredWrapper(None);
1319+
let mut funding_txo = RequiredWrapper(None);
1320+
read_tlv_fields!(reader, {
1321+
(0, channel_id, required),
1322+
(2, user_channel_id, required),
1323+
(4, former_temporary_channel_id, required),
1324+
(6, counterparty_node_id, required),
1325+
(8, funding_txo, required),
1326+
});
1327+
1328+
Ok(Some(Event::ChannelPending {
1329+
channel_id,
1330+
user_channel_id,
1331+
former_temporary_channel_id,
1332+
counterparty_node_id: counterparty_node_id.0.unwrap(),
1333+
funding_txo: funding_txo.0.unwrap()
1334+
}))
1335+
};
1336+
f()
1337+
},
12741338
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
12751339
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
12761340
// reads.

lightning/src/ln/chanmon_update_fail_tests.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,7 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18641864
let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
18651865
nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
18661866
check_added_monitors!(nodes[0], 0);
1867+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
18671868

18681869
let events = nodes[0].node.get_and_clear_pending_events();
18691870
assert_eq!(events.len(), 0);
@@ -1885,6 +1886,9 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18851886
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
18861887
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
18871888
reconnect_nodes(&nodes[0], &nodes[1], (false, confirm_a_first), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
1889+
1890+
// But we want to re-emit ChannelPending
1891+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
18881892
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
18891893
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
18901894

@@ -2808,6 +2812,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
28082812
let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
28092813
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
28102814
check_added_monitors!(nodes[1], 1);
2815+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
28112816

28122817
let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
28132818
assert_eq!(bs_signed_locked.len(), if use_0conf { 2 } else { 1 });
@@ -2906,6 +2911,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
29062911

29072912
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
29082913
check_added_monitors!(nodes[0], 1);
2914+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
29092915

29102916
let as_funding_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
29112917
if lock_commitment {

lightning/src/ln/channel.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
499499
user_id: u128,
500500

501501
channel_id: [u8; 32],
502+
temporary_channel_id: Option<[u8; 32]>, // Will be `None` for channels created prior to 0.0.115.
502503
channel_state: u32,
503504

504505
// When we reach max(6 blocks, minimum_depth), we need to send an AnnouncementSigs message to
@@ -729,6 +730,9 @@ pub(super) struct Channel<Signer: ChannelSigner> {
729730
// blinded paths instead of simple scid+node_id aliases.
730731
outbound_scid_alias: u64,
731732

733+
// We track whether we already emitted a `ChannelPending` event.
734+
channel_pending_event_emitted: bool,
735+
732736
// We track whether we already emitted a `ChannelReady` event.
733737
channel_ready_event_emitted: bool,
734738

@@ -991,6 +995,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
991995
}
992996
}
993997

998+
let temporary_channel_id = entropy_source.get_secure_random_bytes();
999+
9941000
Ok(Channel {
9951001
user_id,
9961002

@@ -1004,7 +1010,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
10041010

10051011
inbound_handshake_limits_override: Some(config.channel_handshake_limits.clone()),
10061012

1007-
channel_id: entropy_source.get_secure_random_bytes(),
1013+
channel_id: temporary_channel_id,
1014+
temporary_channel_id: Some(temporary_channel_id),
10081015
channel_state: ChannelState::OurInitSent as u32,
10091016
announcement_sigs_state: AnnouncementSigsState::NotSent,
10101017
secp_ctx,
@@ -1103,6 +1110,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
11031110
latest_inbound_scid_alias: None,
11041111
outbound_scid_alias,
11051112

1113+
channel_pending_event_emitted: false,
11061114
channel_ready_event_emitted: false,
11071115

11081116
#[cfg(any(test, fuzzing))]
@@ -1350,6 +1358,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
13501358
inbound_handshake_limits_override: None,
13511359

13521360
channel_id: msg.temporary_channel_id,
1361+
temporary_channel_id: Some(msg.temporary_channel_id),
13531362
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
13541363
announcement_sigs_state: AnnouncementSigsState::NotSent,
13551364
secp_ctx,
@@ -1451,6 +1460,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
14511460
latest_inbound_scid_alias: None,
14521461
outbound_scid_alias,
14531462

1463+
channel_pending_event_emitted: false,
14541464
channel_ready_event_emitted: false,
14551465

14561466
#[cfg(any(test, fuzzing))]
@@ -4550,6 +4560,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
45504560
self.channel_id
45514561
}
45524562

4563+
// Return the `temporary_channel_id` used during channel establishment.
4564+
//
4565+
// Will return `None` for channels created prior to LDK version 0.0.115.
4566+
pub fn temporary_channel_id(&self) -> Option<[u8; 32]> {
4567+
self.temporary_channel_id
4568+
}
4569+
45534570
pub fn minimum_depth(&self) -> Option<u32> {
45544571
self.minimum_depth
45554572
}
@@ -4694,6 +4711,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
46944711
self.prev_config.map(|prev_config| prev_config.0)
46954712
}
46964713

4714+
// Checks whether we should emit a `ChannelPending` event.
4715+
pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
4716+
self.is_funding_initiated() && !self.channel_pending_event_emitted
4717+
}
4718+
4719+
// Returns whether we already emitted a `ChannelPending` event.
4720+
pub(crate) fn channel_pending_event_emitted(&self) -> bool {
4721+
self.channel_pending_event_emitted
4722+
}
4723+
4724+
// Remembers that we already emitted a `ChannelPending` event.
4725+
pub(crate) fn set_channel_pending_event_emitted(&mut self) {
4726+
self.channel_pending_event_emitted = true;
4727+
}
4728+
46974729
// Checks whether we should emit a `ChannelReady` event.
46984730
pub(crate) fn should_emit_channel_ready_event(&mut self) -> bool {
46994731
self.is_usable() && !self.channel_ready_event_emitted
@@ -6420,6 +6452,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64206452
if self.holder_max_htlc_value_in_flight_msat != Self::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis, &old_max_in_flight_percent_config)
64216453
{ Some(self.holder_max_htlc_value_in_flight_msat) } else { None };
64226454

6455+
let channel_pending_event_emitted = Some(self.channel_pending_event_emitted);
64236456
let channel_ready_event_emitted = Some(self.channel_ready_event_emitted);
64246457

64256458
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
@@ -6452,6 +6485,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64526485
(23, channel_ready_event_emitted, option),
64536486
(25, user_id_high_opt, option),
64546487
(27, self.channel_keys_id, required),
6488+
(29, self.temporary_channel_id, option),
6489+
(31, channel_pending_event_emitted, option),
64556490
});
64566491

64576492
Ok(())
@@ -6719,10 +6754,12 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67196754
let mut announcement_sigs_state = Some(AnnouncementSigsState::NotSent);
67206755
let mut latest_inbound_scid_alias = None;
67216756
let mut outbound_scid_alias = None;
6757+
let mut channel_pending_event_emitted = None;
67226758
let mut channel_ready_event_emitted = None;
67236759

67246760
let mut user_id_high_opt: Option<u64> = None;
67256761
let mut channel_keys_id: Option<[u8; 32]> = None;
6762+
let mut temporary_channel_id: Option<[u8; 32]> = None;
67266763

67276764
read_tlv_fields!(reader, {
67286765
(0, announcement_sigs, option),
@@ -6743,6 +6780,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67436780
(23, channel_ready_event_emitted, option),
67446781
(25, user_id_high_opt, option),
67456782
(27, channel_keys_id, option),
6783+
(29, temporary_channel_id, option),
6784+
(31, channel_pending_event_emitted, option),
67466785
});
67476786

67486787
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -6807,6 +6846,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68076846
inbound_handshake_limits_override: None,
68086847

68096848
channel_id,
6849+
temporary_channel_id,
68106850
channel_state,
68116851
announcement_sigs_state: announcement_sigs_state.unwrap(),
68126852
secp_ctx,
@@ -6899,6 +6939,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68996939
// Later in the ChannelManager deserialization phase we scan for channels and assign scid aliases if its missing
69006940
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
69016941

6942+
channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
69026943
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
69036944

69046945
#[cfg(any(test, fuzzing))]

0 commit comments

Comments
 (0)