Skip to content

Commit 184d873

Browse files
committed
Add ChannelPending event emitted upon funding_signed
Currently, users don't have good way of being notified when channel open negotiations have succeeded and new channels are pending confirmation on chain. To this end, we add a new `ChannelPending` event that is emitted when send or receive a `funding_signed` message, i.e., at the last moment before waiting for the confirmation period.
1 parent 9fd6127 commit 184d873

File tree

10 files changed

+194
-13
lines changed

10 files changed

+194
-13
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 11 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ mod tests {
664664
use lightning::chain::keysinterface::{InMemorySigner, KeysManager};
665665
use lightning::chain::transaction::OutPoint;
666666
use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent};
667-
use lightning::get_event_msg;
667+
use lightning::{get_event_msg, get_event};
668668
use lightning::ln::PaymentHash;
669669
use lightning::ln::channelmanager;
670670
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId};
@@ -1012,7 +1012,10 @@ mod tests {
10121012
($node_a: expr, $node_b: expr, $temporary_channel_id: expr, $tx: expr) => {{
10131013
$node_a.node.funding_transaction_generated(&$temporary_channel_id, &$node_b.node.get_our_node_id(), $tx.clone()).unwrap();
10141014
$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()));
1015+
get_event!($node_b, Event::ChannelPending);
1016+
10151017
$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()));
1018+
get_event!($node_a, Event::ChannelPending);
10161019
}}
10171020
}
10181021

lightning/src/events/mod.rs

Lines changed: 68 additions & 4 deletions
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;
@@ -604,12 +606,39 @@ pub enum Event {
604606
/// transaction.
605607
claim_from_onchain_tx: bool,
606608
},
609+
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
610+
/// confirmation on-chain.
611+
///
612+
/// This event is emitted when the funding transaction has been signed and is broadcast to the
613+
/// network. For 0conf channels it will be immediately followed by the corresponding
614+
/// [`Event::ChannelReady`] event.
615+
ChannelPending {
616+
/// The `channel_id` of the channel that is pending confirmation.
617+
channel_id: [u8; 32],
618+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
619+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
620+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
621+
/// `user_channel_id` will be randomized for an inbound channel.
622+
///
623+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
624+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
625+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
626+
user_channel_id: u128,
627+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
628+
///
629+
/// Will be `None` for channels created prior to LDK version 0.0.115.
630+
former_temporary_channel_id: Option<[u8; 32]>,
631+
/// The `node_id` of the channel counterparty.
632+
counterparty_node_id: PublicKey,
633+
/// The outpoint of the channel's funding transaction.
634+
funding_txo: OutPoint,
635+
},
607636
/// Used to indicate that a channel with the given `channel_id` is ready to
608637
/// be used. This event is emitted either when the funding transaction has been confirmed
609638
/// on-chain, or, in case of a 0conf channel, when both parties have confirmed the channel
610639
/// establishment.
611640
ChannelReady {
612-
/// The channel_id of the channel that is ready.
641+
/// The `channel_id` of the channel that is ready.
613642
channel_id: [u8; 32],
614643
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
615644
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
@@ -620,15 +649,15 @@ pub enum Event {
620649
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
621650
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
622651
user_channel_id: u128,
623-
/// The node_id of the channel counterparty.
652+
/// The `node_id` of the channel counterparty.
624653
counterparty_node_id: PublicKey,
625654
/// The features that this channel will operate with.
626655
channel_type: ChannelTypeFeatures,
627656
},
628657
/// Used to indicate that a previously opened channel with the given `channel_id` is in the
629658
/// process of closure.
630659
ChannelClosed {
631-
/// The channel_id of the channel which has been closed. Note that on-chain transactions
660+
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
632661
/// resolving the channel are likely still awaiting confirmation.
633662
channel_id: [u8; 32],
634663
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
@@ -923,6 +952,16 @@ impl Writeable for Event {
923952
(6, channel_type, required),
924953
});
925954
},
955+
&Event::ChannelPending { ref channel_id, ref user_channel_id, ref former_temporary_channel_id, ref counterparty_node_id, ref funding_txo } => {
956+
31u8.write(writer)?;
957+
write_tlv_fields!(writer, {
958+
(0, channel_id, required),
959+
(2, user_channel_id, required),
960+
(4, former_temporary_channel_id, required),
961+
(6, counterparty_node_id, required),
962+
(8, funding_txo, required),
963+
});
964+
},
926965
// Note that, going forward, all new events must only write data inside of
927966
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
928967
// data via `write_tlv_fields`.
@@ -1258,6 +1297,31 @@ impl MaybeReadable for Event {
12581297
};
12591298
f()
12601299
},
1300+
31u8 => {
1301+
let f = || {
1302+
let mut channel_id = [0; 32];
1303+
let mut user_channel_id: u128 = 0;
1304+
let mut former_temporary_channel_id = None;
1305+
let mut counterparty_node_id = RequiredWrapper(None);
1306+
let mut funding_txo = RequiredWrapper(None);
1307+
read_tlv_fields!(reader, {
1308+
(0, channel_id, required),
1309+
(2, user_channel_id, required),
1310+
(4, former_temporary_channel_id, required),
1311+
(6, counterparty_node_id, required),
1312+
(8, funding_txo, required),
1313+
});
1314+
1315+
Ok(Some(Event::ChannelPending {
1316+
channel_id,
1317+
user_channel_id,
1318+
former_temporary_channel_id,
1319+
counterparty_node_id: counterparty_node_id.0.unwrap(),
1320+
funding_txo: funding_txo.0.unwrap()
1321+
}))
1322+
};
1323+
f()
1324+
},
12611325
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
12621326
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
12631327
// reads.

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,10 +1854,12 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18541854
let channel_id = OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
18551855
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
18561856
check_added_monitors!(nodes[1], 1);
1857+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
18571858

18581859
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
18591860
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
18601861
check_added_monitors!(nodes[0], 1);
1862+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
18611863
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
18621864
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
18631865
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
@@ -2808,6 +2810,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
28082810
let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
28092811
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
28102812
check_added_monitors!(nodes[1], 1);
2813+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
28112814

28122815
let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
28132816
assert_eq!(bs_signed_locked.len(), if use_0conf { 2 } else { 1 });
@@ -2817,6 +2820,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
28172820

28182821
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
28192822
check_added_monitors!(nodes[0], 1);
2823+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
28202824
}
28212825
_ => panic!("Unexpected event"),
28222826
}
@@ -2898,6 +2902,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
28982902
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
28992903
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
29002904
check_added_monitors!(nodes[1], 1);
2905+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
29012906

29022907
// nodes[1] happily sends its funding_signed even though its awaiting the persistence of the
29032908
// initial ChannelMonitor, but it will decline to send its channel_ready even if the funding
@@ -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

Lines changed: 17 additions & 1 deletion
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
@@ -991,6 +992,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
991992
}
992993
}
993994

995+
let temporary_channel_id = entropy_source.get_secure_random_bytes();
996+
994997
Ok(Channel {
995998
user_id,
996999

@@ -1004,7 +1007,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
10041007

10051008
inbound_handshake_limits_override: Some(config.channel_handshake_limits.clone()),
10061009

1007-
channel_id: entropy_source.get_secure_random_bytes(),
1010+
channel_id: temporary_channel_id,
1011+
temporary_channel_id: Some(temporary_channel_id),
10081012
channel_state: ChannelState::OurInitSent as u32,
10091013
announcement_sigs_state: AnnouncementSigsState::NotSent,
10101014
secp_ctx,
@@ -1350,6 +1354,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
13501354
inbound_handshake_limits_override: None,
13511355

13521356
channel_id: msg.temporary_channel_id,
1357+
temporary_channel_id: Some(msg.temporary_channel_id),
13531358
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
13541359
announcement_sigs_state: AnnouncementSigsState::NotSent,
13551360
secp_ctx,
@@ -4550,6 +4555,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
45504555
self.channel_id
45514556
}
45524557

4558+
// Return the `temporary_channel_id` used during channel establishment.
4559+
//
4560+
// Will return `None` for channels created prior to LDK version 0.0.115.
4561+
pub fn temporary_channel_id(&self) -> Option<[u8; 32]> {
4562+
self.temporary_channel_id
4563+
}
4564+
45534565
pub fn minimum_depth(&self) -> Option<u32> {
45544566
self.minimum_depth
45554567
}
@@ -6452,6 +6464,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64526464
(23, channel_ready_event_emitted, option),
64536465
(25, user_id_high_opt, option),
64546466
(27, self.channel_keys_id, required),
6467+
(29, self.temporary_channel_id, option),
64556468
});
64566469

64576470
Ok(())
@@ -6723,6 +6736,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67236736

67246737
let mut user_id_high_opt: Option<u64> = None;
67256738
let mut channel_keys_id: Option<[u8; 32]> = None;
6739+
let mut temporary_channel_id: Option<[u8; 32]> = None;
67266740

67276741
read_tlv_fields!(reader, {
67286742
(0, announcement_sigs, option),
@@ -6743,6 +6757,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67436757
(23, channel_ready_event_emitted, option),
67446758
(25, user_id_high_opt, option),
67456759
(27, channel_keys_id, option),
6760+
(29, temporary_channel_id, option),
67466761
});
67476762

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

68096824
channel_id,
6825+
temporary_channel_id,
68106826
channel_state,
68116827
announcement_sigs_state: announcement_sigs_state.unwrap(),
68126828
secp_ctx,

0 commit comments

Comments
 (0)