Skip to content

Commit 454ebf2

Browse files
committed
Add ChannelPending event
1 parent 6aa4a86 commit 454ebf2

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ description = "A ready-to-go node implementation built using LDK."
2525
#lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
2626
#lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["esplora-async"] }
2727

28-
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518", features = ["max_level_trace", "std"] }
29-
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518" }
30-
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518" }
31-
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518" }
32-
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518" }
33-
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518" }
34-
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev="7b85ebadb64058127350b83fb4b76dcb409ea518", features = ["esplora-async"] }
28+
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907", features = ["max_level_trace", "std"] }
29+
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907" }
30+
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907" }
31+
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907" }
32+
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907" }
33+
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907" }
34+
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev="9873c7dad851a0ffa16f38a4788115cb5e0f5907", features = ["esplora-async"] }
3535

3636
#lightning = { git = "https://github.com/tnull/rust-lightning", branch="2023-03-expose-impl-writeable-tlv-based-enum-common", features = ["max_level_trace", "std"] }
3737
#lightning-invoice = { git = "https://github.com/tnull/rust-lightning", branch="2023-03-expose-impl-writeable-tlv-based-enum-common" }

src/event.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ use crate::{
66
use crate::logger::{log_error, log_info, Logger};
77

88
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
9+
use lightning::events::Event as LdkEvent;
10+
use lightning::events::EventHandler as LdkEventHandler;
11+
use lightning::events::PaymentPurpose;
912
use lightning::impl_writeable_tlv_based_enum;
1013
use lightning::ln::PaymentHash;
1114
use lightning::routing::gossip::NodeId;
1215
use lightning::util::errors::APIError;
13-
use lightning::util::events::Event as LdkEvent;
14-
use lightning::util::events::EventHandler as LdkEventHandler;
15-
use lightning::util::events::PaymentPurpose;
1616
use lightning::util::persist::KVStorePersister;
1717
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
1818

19-
use bitcoin::secp256k1::Secp256k1;
19+
use bitcoin::secp256k1::{PublicKey, Secp256k1};
20+
use bitcoin::OutPoint;
2021
use rand::{thread_rng, Rng};
2122
use std::collections::{hash_map, VecDeque};
2223
use std::ops::Deref;
@@ -48,6 +49,19 @@ pub enum Event {
4849
/// The value, in thousandths of a satoshi, that has been received.
4950
amount_msat: u64,
5051
},
52+
/// A channel has been created and is pending confirmation on-chain.
53+
ChannelPending {
54+
/// The `channel_id` of the channel.
55+
channel_id: [u8; 32],
56+
/// The `user_channel_id` of the channel.
57+
user_channel_id: u128,
58+
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
59+
former_temporary_channel_id: [u8; 32],
60+
/// The `node_id` of the channel counterparty.
61+
counterparty_node_id: PublicKey,
62+
/// The outpoint of the channel's funding transaction.
63+
funding_txo: OutPoint,
64+
},
5165
/// A channel is ready to be used.
5266
ChannelReady {
5367
/// The `channel_id` of the channel.
@@ -79,7 +93,14 @@ impl_writeable_tlv_based_enum!(Event,
7993
(0, channel_id, required),
8094
(1, user_channel_id, required),
8195
},
82-
(4, ChannelClosed) => {
96+
(4, ChannelPending) => {
97+
(0, channel_id, required),
98+
(1, user_channel_id, required),
99+
(2, former_temporary_channel_id, required),
100+
(3, counterparty_node_id, required),
101+
(4, funding_txo, required),
102+
},
103+
(5, ChannelClosed) => {
83104
(0, channel_id, required),
84105
(1, user_channel_id, required),
85106
};
@@ -455,6 +476,7 @@ where
455476
next_channel_id,
456477
fee_earned_msat,
457478
claim_from_onchain_tx,
479+
outbound_amount_forwarded_msat,
458480
} => {
459481
let read_only_network_graph = self.network_graph.read_only();
460482
let nodes = read_only_network_graph.nodes();
@@ -474,6 +496,9 @@ where
474496
})
475497
})
476498
};
499+
500+
let outbound_amount_str =
501+
outbound_amount_forwarded_msat.unwrap_or_default().to_string();
477502
let channel_str = |channel_id: &Option<[u8; 32]>| {
478503
channel_id
479504
.map(|channel_id| {
@@ -493,7 +518,8 @@ where
493518
if claim_from_onchain_tx {
494519
log_info!(
495520
self.logger,
496-
"Forwarded payment{}{}, earning {} msats in fees from claiming onchain.",
521+
"Forwarded payment of {}msats{}{}, earning {} msats in fees from claiming onchain.",
522+
outbound_amount_str,
497523
from_prev_str,
498524
to_next_str,
499525
fee_earned,
@@ -508,12 +534,35 @@ where
508534
);
509535
}
510536
}
537+
LdkEvent::ChannelPending {
538+
channel_id,
539+
user_channel_id,
540+
former_temporary_channel_id,
541+
counterparty_node_id,
542+
funding_txo,
543+
} => {
544+
log_info!(
545+
self.logger,
546+
"New channel {} with counterparty {} has been created and is pending confirmation on chain.",
547+
hex_utils::to_string(&channel_id),
548+
counterparty_node_id,
549+
);
550+
self.event_queue
551+
.add_event(Event::ChannelPending {
552+
channel_id,
553+
user_channel_id,
554+
former_temporary_channel_id: former_temporary_channel_id.unwrap(),
555+
counterparty_node_id,
556+
funding_txo,
557+
})
558+
.expect("Failed to push to event queue");
559+
}
511560
LdkEvent::ChannelReady {
512561
channel_id, user_channel_id, counterparty_node_id, ..
513562
} => {
514563
log_info!(
515564
self.logger,
516-
"Channel {} with {} ready to be used.",
565+
"Channel {} with counterparty {} ready to be used.",
517566
hex_utils::to_string(&channel_id),
518567
counterparty_node_id,
519568
);

src/tests/functional_tests.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,16 @@ fn channel_full_cycle() {
183183
let node_b_addr = format!("{}@{}", node_b.node_id(), node_b.listening_address().unwrap());
184184
node_a.connect_open_channel(&node_b_addr, 50000, true).unwrap();
185185

186-
let funding_txo = loop {
187-
let details = node_a.list_channels();
186+
expect_event!(node_a, ChannelPending);
188187

189-
if details.is_empty() || details[0].funding_txo.is_none() {
190-
std::thread::sleep(Duration::from_secs(1));
191-
} else {
192-
break details[0].funding_txo.unwrap();
188+
let funding_txo = match node_b.next_event() {
189+
ref e @ Event::ChannelPending { funding_txo, .. } => {
190+
println!("{} got event {:?}", std::stringify!(node_b), e);
191+
node_b.event_handled();
192+
funding_txo
193+
}
194+
ref e => {
195+
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
193196
}
194197
};
195198

@@ -276,7 +279,7 @@ fn channel_full_cycle() {
276279
expect_event!(node_a, ChannelClosed);
277280
expect_event!(node_b, ChannelClosed);
278281

279-
wait_for_outpoint_spend(funding_txo.into_bitcoin_outpoint());
282+
wait_for_outpoint_spend(funding_txo);
280283

281284
generate_blocks_and_wait(1);
282285
node_a.sync_wallets().unwrap();

0 commit comments

Comments
 (0)