Skip to content

Commit e0bb02c

Browse files
committed
f Test and fix 0conf channel open
1 parent dac4ce6 commit e0bb02c

File tree

2 files changed

+125
-58
lines changed

2 files changed

+125
-58
lines changed

src/event.rs

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -574,78 +574,52 @@ where
574574
temporary_channel_id,
575575
counterparty_node_id,
576576
funding_satoshis,
577-
channel_type,
577+
channel_type: _,
578578
push_msat: _,
579579
} => {
580580
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();
581-
if channel_type.requires_zero_conf() {
582-
if let Some(peer_info) = self.peer_store.get_peer(&counterparty_node_id) {
583-
if peer_info.trusted_0conf {
584-
match self
585-
.channel_manager
586-
.accept_inbound_channel_from_trusted_peer_0conf(
587-
&temporary_channel_id,
588-
&counterparty_node_id,
589-
user_channel_id,
590-
) {
591-
Ok(()) => {
592-
log_info!(
593-
self.logger,
594-
"Accepting inbound 0conf channel of {}sats from {}",
595-
funding_satoshis,
596-
counterparty_node_id,
597-
);
598-
}
599-
Err(e) => {
600-
log_error!(
601-
self.logger,
602-
"Error while accepting inbound 0conf channel: {:?}",
603-
e
604-
);
605-
}
606-
}
607-
}
608-
} else {
609-
log_error!(
610-
self.logger,
611-
"Rejecting request for inbound 0conf channel from untrusted peer {}",
612-
counterparty_node_id,
613-
);
614-
match self.channel_manager.force_close_without_broadcasting_txn(
581+
if let Some(peer_info) = self.peer_store.get_peer(&counterparty_node_id) {
582+
if peer_info.trusted_0conf {
583+
match self.channel_manager.accept_inbound_channel_from_trusted_peer_0conf(
615584
&temporary_channel_id,
616585
&counterparty_node_id,
586+
user_channel_id,
617587
) {
618-
Ok(()) => {}
588+
Ok(()) => {
589+
log_info!(
590+
self.logger,
591+
"Accepting inbound 0conf channel of {}sats from {}",
592+
funding_satoshis,
593+
counterparty_node_id,
594+
);
595+
return;
596+
}
619597
Err(e) => {
620598
log_error!(
621599
self.logger,
622-
"Error while rejecting untrusted inbound 0conf channel: {:?}",
600+
"Error while accepting inbound 0conf channel: {:?}",
623601
e
624602
);
603+
return;
625604
}
626605
}
627606
}
628-
} else {
629-
match self.channel_manager.accept_inbound_channel(
630-
&temporary_channel_id,
631-
&counterparty_node_id,
632-
user_channel_id,
633-
) {
634-
Ok(()) => {
635-
log_info!(
636-
self.logger,
637-
"Accepting inbound channel of {}sats from {}",
638-
funding_satoshis,
639-
counterparty_node_id,
640-
);
641-
}
642-
Err(e) => {
643-
log_error!(
644-
self.logger,
645-
"Error while accepting inbound channel: {:?}",
646-
e
647-
);
648-
}
607+
}
608+
match self.channel_manager.accept_inbound_channel(
609+
&temporary_channel_id,
610+
&counterparty_node_id,
611+
user_channel_id,
612+
) {
613+
Ok(()) => {
614+
log_info!(
615+
self.logger,
616+
"Accepting inbound channel of {}sats from {}",
617+
funding_satoshis,
618+
counterparty_node_id,
619+
);
620+
}
621+
Err(e) => {
622+
log_error!(self.logger, "Error while accepting inbound channel: {:?}", e);
649623
}
650624
}
651625
}

src/test/functional_tests.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,96 @@ fn onchain_spend_receive() {
364364
assert!(node_b.onchain_balance().unwrap().get_spendable() > 99000);
365365
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
366366
}
367+
368+
#[test]
369+
fn channel_full_cycle_0conf() {
370+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
371+
println!("== Node A ==");
372+
let esplora_url = electrsd.esplora_url.as_ref().unwrap();
373+
let config_a = random_config(esplora_url);
374+
let node_a = Builder::from_config(config_a).build();
375+
node_a.start().unwrap();
376+
let addr_a = node_a.new_funding_address().unwrap();
377+
378+
println!("\n== Node B ==");
379+
let config_b = random_config(esplora_url);
380+
let node_b = Builder::from_config(config_b).build();
381+
node_b.start().unwrap();
382+
let addr_b = node_b.new_funding_address().unwrap();
383+
384+
let premine_amount_sat = 100_000;
385+
386+
premine_and_distribute_funds(
387+
&bitcoind,
388+
&electrsd,
389+
vec![addr_a, addr_b],
390+
Amount::from_sat(premine_amount_sat),
391+
);
392+
node_a.sync_wallets().unwrap();
393+
node_b.sync_wallets().unwrap();
394+
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
395+
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
396+
println!("\nB -- connect -> A");
397+
node_b.connect(node_a.node_id(), node_a.listening_address().unwrap(), true, true).unwrap();
398+
399+
std::thread::sleep(std::time::Duration::from_secs(1));
400+
401+
println!("\nA -- connect_open_channel -> B");
402+
let funding_amount_sat = 80_000;
403+
let push_msat = (funding_amount_sat / 2) * 1000; // balance the channel
404+
node_a
405+
.connect_open_channel(
406+
node_b.node_id(),
407+
node_b.listening_address().unwrap(),
408+
funding_amount_sat,
409+
Some(push_msat),
410+
true,
411+
true,
412+
)
413+
.unwrap();
414+
415+
node_a.sync_wallets().unwrap();
416+
node_b.sync_wallets().unwrap();
417+
418+
expect_event!(node_a, ChannelPending);
419+
420+
let _funding_txo = match node_b.next_event() {
421+
ref e @ Event::ChannelPending { funding_txo, .. } => {
422+
println!("{} got event {:?}", std::stringify!(node_b), e);
423+
node_b.event_handled();
424+
funding_txo
425+
}
426+
ref e => {
427+
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
428+
}
429+
};
430+
431+
node_a.sync_wallets().unwrap();
432+
node_b.sync_wallets().unwrap();
433+
434+
expect_event!(node_a, ChannelReady);
435+
let _channel_id = match node_b.next_event() {
436+
ref e @ Event::ChannelReady { ref channel_id, .. } => {
437+
println!("{} got event {:?}", std::stringify!(node_b), e);
438+
node_b.event_handled();
439+
channel_id.clone()
440+
}
441+
ref e => {
442+
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
443+
}
444+
};
445+
446+
node_a.sync_wallets().unwrap();
447+
node_b.sync_wallets().unwrap();
448+
449+
println!("\nB receive_payment");
450+
let invoice_amount_1_msat = 1000000;
451+
let invoice = node_b.receive_payment(invoice_amount_1_msat, &"asdf", 9217).unwrap();
452+
453+
println!("\nA send_payment");
454+
let payment_hash = node_a.send_payment(&invoice).unwrap();
455+
expect_event!(node_a, PaymentSuccessful);
456+
expect_event!(node_b, PaymentReceived);
457+
assert_eq!(node_a.payment(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
458+
assert_eq!(node_a.payment(&payment_hash).unwrap().direction, PaymentDirection::Outbound);
459+
}

0 commit comments

Comments
 (0)