Skip to content

Commit 2185397

Browse files
committed
Add ChannelPending event
1 parent 8cdca11 commit 2185397

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
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 = { path = "../rust-lightning/lightning", features = ["max_level_trace", "std"] }
3737
#lightning-invoice = { path = "../rust-lightning/lightning-invoice" }

src/event.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ use crate::io::{
1010
use crate::logger::{log_error, log_info, Logger};
1111

1212
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
13+
use lightning::events::Event as LdkEvent;
14+
use lightning::events::EventHandler as LdkEventHandler;
15+
use lightning::events::PaymentPurpose;
1316
use lightning::impl_writeable_tlv_based_enum;
1417
use lightning::ln::PaymentHash;
1518
use lightning::routing::gossip::NodeId;
1619
use lightning::util::errors::APIError;
17-
use lightning::util::events::Event as LdkEvent;
18-
use lightning::util::events::EventHandler as LdkEventHandler;
19-
use lightning::util::events::PaymentPurpose;
2020
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2121

22-
use bitcoin::secp256k1::Secp256k1;
22+
use bitcoin::secp256k1::{PublicKey, Secp256k1};
23+
use bitcoin::OutPoint;
2324
use rand::{thread_rng, Rng};
2425
use std::collections::VecDeque;
2526
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
};
@@ -548,6 +569,7 @@ where
548569
next_channel_id,
549570
fee_earned_msat,
550571
claim_from_onchain_tx,
572+
outbound_amount_forwarded_msat,
551573
} => {
552574
let read_only_network_graph = self.network_graph.read_only();
553575
let nodes = read_only_network_graph.nodes();
@@ -567,6 +589,9 @@ where
567589
})
568590
})
569591
};
592+
593+
let outbound_amount_str =
594+
outbound_amount_forwarded_msat.unwrap_or_default().to_string();
570595
let channel_str = |channel_id: &Option<[u8; 32]>| {
571596
channel_id
572597
.map(|channel_id| {
@@ -586,7 +611,8 @@ where
586611
if claim_from_onchain_tx {
587612
log_info!(
588613
self.logger,
589-
"Forwarded payment{}{}, earning {}msat in fees from claiming onchain.",
614+
"Forwarded payment of {}msats{}{}, earning {} msats in fees from claiming onchain.",
615+
outbound_amount_str,
590616
from_prev_str,
591617
to_next_str,
592618
fee_earned,
@@ -601,12 +627,35 @@ where
601627
);
602628
}
603629
}
630+
LdkEvent::ChannelPending {
631+
channel_id,
632+
user_channel_id,
633+
former_temporary_channel_id,
634+
counterparty_node_id,
635+
funding_txo,
636+
} => {
637+
log_info!(
638+
self.logger,
639+
"New channel {} with counterparty {} has been created and is pending confirmation on chain.",
640+
hex_utils::to_string(&channel_id),
641+
counterparty_node_id,
642+
);
643+
self.event_queue
644+
.add_event(Event::ChannelPending {
645+
channel_id,
646+
user_channel_id,
647+
former_temporary_channel_id: former_temporary_channel_id.unwrap(),
648+
counterparty_node_id,
649+
funding_txo,
650+
})
651+
.expect("Failed to push to event queue");
652+
}
604653
LdkEvent::ChannelReady {
605654
channel_id, user_channel_id, counterparty_node_id, ..
606655
} => {
607656
log_info!(
608657
self.logger,
609-
"Channel {} with {} ready to be used.",
658+
"Channel {} with counterparty {} ready to be used.",
610659
hex_utils::to_string(&channel_id),
611660
counterparty_node_id,
612661
);

src/test/functional_tests.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{Builder, Error, Event, PaymentDirection, PaymentStatus};
44

55
use bitcoin::Amount;
66

7-
use std::time::Duration;
87
#[test]
98
fn channel_full_cycle() {
109
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
@@ -36,13 +35,16 @@ fn channel_full_cycle() {
3635
let node_b_addr = format!("{}@{}", node_b.node_id(), node_b.listening_address().unwrap());
3736
node_a.connect_open_channel(&node_b_addr, 50000, true).unwrap();
3837

39-
let funding_txo = loop {
40-
let details = node_a.list_channels();
38+
expect_event!(node_a, ChannelPending);
4139

42-
if details.is_empty() || details[0].funding_txo.is_none() {
43-
std::thread::sleep(Duration::from_secs(1));
44-
} else {
45-
break details[0].funding_txo.unwrap();
40+
let funding_txo = match node_b.next_event() {
41+
ref e @ Event::ChannelPending { funding_txo, .. } => {
42+
println!("{} got event {:?}", std::stringify!(node_b), e);
43+
node_b.event_handled();
44+
funding_txo
45+
}
46+
ref e => {
47+
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
4648
}
4749
};
4850

@@ -168,7 +170,7 @@ fn channel_full_cycle() {
168170
expect_event!(node_a, ChannelClosed);
169171
expect_event!(node_b, ChannelClosed);
170172

171-
wait_for_outpoint_spend(&electrsd, funding_txo.into_bitcoin_outpoint());
173+
wait_for_outpoint_spend(&electrsd, funding_txo);
172174

173175
generate_blocks_and_wait(&bitcoind, &electrsd, 1);
174176
node_a.sync_wallets().unwrap();

0 commit comments

Comments
 (0)