X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Ffunctional_tests.rs;h=044c1673011dd66d81112fe7c673a9aaf19287a0;hb=6de175575b6cd3e4f087a123a6dd699c9ec8ef00;hp=9debe08fa28889f74dc97379b1412a27eca32eb2;hpb=41def659c0089ba3f0c7d85726b379f400bb6eb2;p=rust-lightning diff --git a/src/ln/functional_tests.rs b/src/ln/functional_tests.rs index 9debe08fa..044c16730 100644 --- a/src/ln/functional_tests.rs +++ b/src/ln/functional_tests.rs @@ -49,6 +49,58 @@ use rand::{thread_rng, Rng}; use ln::functional_test_utils::*; +#[test] +fn test_insane_channel_opens() { + // Stand up a network of 2 nodes + let nodes = create_network(2, &[None, None]); + + // Instantiate channel parameters where we push the maximum msats given our + // funding satoshis + let channel_value_sat = 31337; // same as funding satoshis + let channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(channel_value_sat); + let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000; + + // Have node0 initiate a channel to node1 with aforementioned parameters + nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42).unwrap(); + + // Extract the channel open message from node0 to node1 + let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); + + // Test helper that asserts we get the correct error string given a mutator + // that supposedly makes the channel open message insane + let insane_open_helper = |expected_error_str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| { + match nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &message_mutator(open_channel_message.clone())) { + Err(msgs::HandleError{ err: error_str, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) => { + assert_eq!(error_str, expected_error_str, "unexpected HandleError string (expected `{}`, actual `{}`)", expected_error_str, error_str) + }, + Err(msgs::HandleError{..}) => {panic!("unexpected HandleError action")}, + _ => panic!("insane OpenChannel message was somehow Ok"), + } + }; + + use ln::channel::MAX_FUNDING_SATOSHIS; + use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT; + + // Test all mutations that would make the channel open message insane + insane_open_helper("funding value > 2^24", |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg }); + + insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg }); + + insane_open_helper("push_msat larger than funding value", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg }); + + insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg }); + + insane_open_helper("Bogus; channel reserve is less than dust limit", |mut msg| { msg.dust_limit_satoshis = msg.channel_reserve_satoshis + 1; msg }); + + insane_open_helper("Minimum htlc value is full channel value", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg }); + + insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg }); + + insane_open_helper("0 max_accpted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg }); + + insane_open_helper("max_accpted_htlcs > 483", |mut msg| { msg.max_accepted_htlcs = 484; msg }); +} + #[test] fn test_async_inbound_update_fee() { let mut nodes = create_network(2, &[None, None]); @@ -1194,7 +1246,6 @@ fn duplicate_htlc_test() { } fn do_channel_reserve_test(test_recv: bool) { - use std::sync::atomic::Ordering; use ln::msgs::HandleError; let mut nodes = create_network(3, &[None, None, None]); @@ -1236,7 +1287,7 @@ fn do_channel_reserve_test(test_recv: bool) { assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat)); let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap(); match err { - APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"), + APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"), _ => panic!("Unknown error variants"), } } @@ -1272,7 +1323,7 @@ fn do_channel_reserve_test(test_recv: bool) { let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1); let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap(); match err { - APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"), + APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"), _ => panic!("Unknown error variants"), } } @@ -1297,7 +1348,7 @@ fn do_channel_reserve_test(test_recv: bool) { { let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1); match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() { - APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"), + APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"), _ => panic!("Unknown error variants"), } } @@ -1361,7 +1412,7 @@ fn do_channel_reserve_test(test_recv: bool) { { let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1); match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() { - APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"), + APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over their reserve value"), _ => panic!("Unknown error variants"), } } @@ -1978,11 +2029,16 @@ fn claim_htlc_outputs_single_tx() { assert_eq!(node_txn[1].input.len(), 1); assert_eq!(node_txn[2].input.len(), 1); - let mut revoked_tx_map = HashMap::new(); - revoked_tx_map.insert(revoked_local_txn[0].txid(), revoked_local_txn[0].clone()); - node_txn[0].verify(&revoked_tx_map).unwrap(); - node_txn[1].verify(&revoked_tx_map).unwrap(); - node_txn[2].verify(&revoked_tx_map).unwrap(); + fn get_txout(out_point: &BitcoinOutPoint, tx: &Transaction) -> Option { + if out_point.txid == tx.txid() { + tx.output.get(out_point.vout as usize).cloned() + } else { + None + } + } + node_txn[0].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap(); + node_txn[1].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap(); + node_txn[2].verify(|out|get_txout(out, &revoked_local_txn[0])).unwrap(); let mut witness_lens = BTreeSet::new(); witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len()); @@ -4461,7 +4517,6 @@ fn run_onion_failure_test_with_fail_intercept(_name: &str, test_case: F2: for <'a> FnMut(&'a mut msgs::UpdateFailHTLC), F3: FnMut(), { - use ln::msgs::HTLCFailChannelUpdate; // reset block height let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; @@ -5019,7 +5074,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() { let err = nodes[0].node.send_payment(route, our_payment_hash); if let Err(APIError::ChannelUnavailable{err}) = err { - assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"); + assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"); } else { assert!(false); } @@ -5143,7 +5198,7 @@ fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() { let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { - assert_eq!(err,"Remote HTLC add would put them over their max HTLC value in flight"); + assert_eq!(err,"Remote HTLC add would put them over our max HTLC value"); } else { assert!(false); }