Skip to content

Commit

Permalink
f Test and fix 0conf channel open
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed May 4, 2023
1 parent dac4ce6 commit e0bb02c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 58 deletions.
90 changes: 32 additions & 58 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,78 +574,52 @@ where
temporary_channel_id,
counterparty_node_id,
funding_satoshis,
channel_type,
channel_type: _,
push_msat: _,
} => {
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();
if channel_type.requires_zero_conf() {
if let Some(peer_info) = self.peer_store.get_peer(&counterparty_node_id) {
if peer_info.trusted_0conf {
match self
.channel_manager
.accept_inbound_channel_from_trusted_peer_0conf(
&temporary_channel_id,
&counterparty_node_id,
user_channel_id,
) {
Ok(()) => {
log_info!(
self.logger,
"Accepting inbound 0conf channel of {}sats from {}",
funding_satoshis,
counterparty_node_id,
);
}
Err(e) => {
log_error!(
self.logger,
"Error while accepting inbound 0conf channel: {:?}",
e
);
}
}
}
} else {
log_error!(
self.logger,
"Rejecting request for inbound 0conf channel from untrusted peer {}",
counterparty_node_id,
);
match self.channel_manager.force_close_without_broadcasting_txn(
if let Some(peer_info) = self.peer_store.get_peer(&counterparty_node_id) {
if peer_info.trusted_0conf {
match self.channel_manager.accept_inbound_channel_from_trusted_peer_0conf(
&temporary_channel_id,
&counterparty_node_id,
user_channel_id,
) {
Ok(()) => {}
Ok(()) => {
log_info!(
self.logger,
"Accepting inbound 0conf channel of {}sats from {}",
funding_satoshis,
counterparty_node_id,
);
return;
}
Err(e) => {
log_error!(
self.logger,
"Error while rejecting untrusted inbound 0conf channel: {:?}",
"Error while accepting inbound 0conf channel: {:?}",
e
);
return;
}
}
}
} else {
match self.channel_manager.accept_inbound_channel(
&temporary_channel_id,
&counterparty_node_id,
user_channel_id,
) {
Ok(()) => {
log_info!(
self.logger,
"Accepting inbound channel of {}sats from {}",
funding_satoshis,
counterparty_node_id,
);
}
Err(e) => {
log_error!(
self.logger,
"Error while accepting inbound channel: {:?}",
e
);
}
}
match self.channel_manager.accept_inbound_channel(
&temporary_channel_id,
&counterparty_node_id,
user_channel_id,
) {
Ok(()) => {
log_info!(
self.logger,
"Accepting inbound channel of {}sats from {}",
funding_satoshis,
counterparty_node_id,
);
}
Err(e) => {
log_error!(self.logger, "Error while accepting inbound channel: {:?}", e);
}
}
}
Expand Down
93 changes: 93 additions & 0 deletions src/test/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,96 @@ fn onchain_spend_receive() {
assert!(node_b.onchain_balance().unwrap().get_spendable() > 99000);
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
}

#[test]
fn channel_full_cycle_0conf() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
println!("== Node A ==");
let esplora_url = electrsd.esplora_url.as_ref().unwrap();
let config_a = random_config(esplora_url);
let node_a = Builder::from_config(config_a).build();
node_a.start().unwrap();
let addr_a = node_a.new_funding_address().unwrap();

println!("\n== Node B ==");
let config_b = random_config(esplora_url);
let node_b = Builder::from_config(config_b).build();
node_b.start().unwrap();
let addr_b = node_b.new_funding_address().unwrap();

let premine_amount_sat = 100_000;

premine_and_distribute_funds(
&bitcoind,
&electrsd,
vec![addr_a, addr_b],
Amount::from_sat(premine_amount_sat),
);
node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
println!("\nB -- connect -> A");
node_b.connect(node_a.node_id(), node_a.listening_address().unwrap(), true, true).unwrap();

std::thread::sleep(std::time::Duration::from_secs(1));

println!("\nA -- connect_open_channel -> B");
let funding_amount_sat = 80_000;
let push_msat = (funding_amount_sat / 2) * 1000; // balance the channel
node_a
.connect_open_channel(
node_b.node_id(),
node_b.listening_address().unwrap(),
funding_amount_sat,
Some(push_msat),
true,
true,
)
.unwrap();

node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();

expect_event!(node_a, ChannelPending);

let _funding_txo = match node_b.next_event() {
ref e @ Event::ChannelPending { funding_txo, .. } => {
println!("{} got event {:?}", std::stringify!(node_b), e);
node_b.event_handled();
funding_txo
}
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
}
};

node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();

expect_event!(node_a, ChannelReady);
let _channel_id = match node_b.next_event() {
ref e @ Event::ChannelReady { ref channel_id, .. } => {
println!("{} got event {:?}", std::stringify!(node_b), e);
node_b.event_handled();
channel_id.clone()
}
ref e => {
panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e);
}
};

node_a.sync_wallets().unwrap();
node_b.sync_wallets().unwrap();

println!("\nB receive_payment");
let invoice_amount_1_msat = 1000000;
let invoice = node_b.receive_payment(invoice_amount_1_msat, &"asdf", 9217).unwrap();

println!("\nA send_payment");
let payment_hash = node_a.send_payment(&invoice).unwrap();
expect_event!(node_a, PaymentSuccessful);
expect_event!(node_b, PaymentReceived);
assert_eq!(node_a.payment(&payment_hash).unwrap().status, PaymentStatus::Succeeded);
assert_eq!(node_a.payment(&payment_hash).unwrap().direction, PaymentDirection::Outbound);
}

0 comments on commit e0bb02c

Please sign in to comment.