Update rust-bitcoin
[rust-lightning] / lightning / src / ln / onion_route_tests.rs
index 9a91efa498033314188decb11c369f41a8125d95..551db24e9d7786f0a85816c00de8736dec8bcf6b 100644 (file)
@@ -11,8 +11,8 @@
 //! These tests work by standing up full nodes and route payments across the network, checking the
 //! returned errors decode to the correct thing.
 
+use chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
 use ln::channelmanager::{HTLCForwardInfo, PaymentPreimage, PaymentHash};
-use ln::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
 use ln::onion_utils;
 use routing::router::{Route, get_route};
 use ln::features::InitFeatures;
@@ -21,8 +21,9 @@ use ln::msgs::{ChannelMessageHandler, HTLCFailChannelUpdate, OptionalField};
 use util::test_utils;
 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
 use util::ser::{Writeable, Writer};
+use util::config::UserConfig;
 
-use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::block::{Block, BlockHeader};
 use bitcoin::hash_types::BlockHash;
 
 use bitcoin::hashes::sha256::Hash as Sha256;
@@ -58,9 +59,12 @@ fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case:
 {
 
        // reset block height
-       let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       let block = Block {
+               header: BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
+               txdata: vec![],
+       };
        for ix in 0..nodes.len() {
-               nodes[ix].block_notifier.block_connected_checked(&header, 1, &[], &[]);
+               connect_block(&nodes[ix], &block, 1);
        }
 
        macro_rules! expect_event {
@@ -215,7 +219,7 @@ impl msgs::ChannelUpdate {
                use bitcoin::secp256k1::ffi::Signature as FFISignature;
                use bitcoin::secp256k1::Signature;
                msgs::ChannelUpdate {
-                       signature: Signature::from(FFISignature::new()),
+                       signature: Signature::from(unsafe { FFISignature::new() }),
                        contents: msgs::UnsignedChannelUpdate {
                                chain_hash: BlockHash::hash(&vec![0u8][..]),
                                short_channel_id: 0,
@@ -257,9 +261,19 @@ fn test_onion_failure() {
        const NODE: u16 = 0x2000;
        const UPDATE: u16 = 0x1000;
 
+       // When we check for amount_below_minimum below, we want to test that we're using the *right*
+       // amount, thus we need different htlc_minimum_msat values. We set node[2]'s htlc_minimum_msat
+       // to 2000, which is above the default value of 1000 set in create_node_chanmgrs.
+       // This exposed a previous bug because we were using the wrong value all the way down in
+       // Channel::get_counterparty_htlc_minimum_msat().
+       let mut node_2_cfg: UserConfig = Default::default();
+       node_2_cfg.own_channel_config.our_htlc_minimum_msat = 2000;
+       node_2_cfg.channel_options.announced_channel = true;
+       node_2_cfg.peer_channel_config_limits.force_announced_channel_preference = false;
+
        let chanmon_cfgs = create_chanmon_cfgs(3);
        let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
-       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
+       let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, Some(node_2_cfg)]);
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        for node in nodes.iter() {
                *node.keys_manager.override_session_priv.lock().unwrap() = Some([3; 32]);
@@ -411,6 +425,11 @@ fn test_onion_failure() {
        bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward;
        run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(UPDATE|11), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
 
+       // Test a positive test-case with one extra msat, meeting the minimum.
+       bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward + 1;
+       let (preimage, _) = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1);
+       claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], preimage, amt_to_forward+1);
+
        //TODO: with new config API, we will be able to generate both valid and
        //invalid channel_update cases.
        run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, |msg| {
@@ -424,9 +443,12 @@ fn test_onion_failure() {
 
        run_onion_failure_test("expiry_too_soon", 0, &nodes, &route, &payment_hash, |msg| {
                let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               let block = Block {
+                       header: BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
+                       txdata: vec![],
+               };
 
-               nodes[1].block_notifier.block_connected_checked(&header, height, &[], &[]);
+               connect_block(&nodes[1], &block, height);
        }, ||{}, true, Some(UPDATE|14), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
 
        run_onion_failure_test("unknown_payment_hash", 2, &nodes, &route, &payment_hash, |_| {}, || {
@@ -435,9 +457,12 @@ fn test_onion_failure() {
 
        run_onion_failure_test("final_expiry_too_soon", 1, &nodes, &route, &payment_hash, |msg| {
                let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
-               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               let block = Block {
+                       header: BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
+                       txdata: vec![],
+               };
 
-               nodes[2].block_notifier.block_connected_checked(&header, height, &[], &[]);
+               connect_block(&nodes[2], &block, height);
        }, || {}, true, Some(17), None);
 
        run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, |_| {}, || {