Skip to content

Commit b124eca

Browse files
committed
Add ChannelPending event
1 parent 65d14dc commit b124eca

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

src/event.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use lightning::routing::gossip::NodeId;
1919
use lightning::util::errors::APIError;
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
};
@@ -607,20 +628,34 @@ where
607628
}
608629
}
609630
LdkEvent::ChannelPending {
610-
channel_id: _,
611-
user_channel_id: _,
612-
former_temporary_channel_id: _,
613-
counterparty_node_id: _,
614-
funding_txo: _,
631+
channel_id,
632+
user_channel_id,
633+
former_temporary_channel_id,
634+
counterparty_node_id,
635+
funding_txo,
615636
} => {
616-
// TODO!
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");
617652
}
618653
LdkEvent::ChannelReady {
619654
channel_id, user_channel_id, counterparty_node_id, ..
620655
} => {
621656
log_info!(
622657
self.logger,
623-
"Channel {} with {} ready to be used.",
658+
"Channel {} with counterparty {} ready to be used.",
624659
hex_utils::to_string(&channel_id),
625660
counterparty_node_id,
626661
);

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)