X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Ffunctional_test_utils.rs;h=b8f5e5835a359f5a30bc9a821685f5bd5be26577;hb=72c5423fd5eca2221641687df57ddf7fe505f756;hp=4ce090a2881ac718fc942a68aa8602dcbd1e1bf6;hpb=a6f0281017f1223ef961fb3a000915e37b5719b0;p=rust-lightning diff --git a/src/ln/functional_test_utils.rs b/src/ln/functional_test_utils.rs index 4ce090a2..b8f5e583 100644 --- a/src/ln/functional_test_utils.rs +++ b/src/ln/functional_test_utils.rs @@ -20,6 +20,7 @@ use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::network::constants::Network; use bitcoin_hashes::sha256::Hash as Sha256; +use bitcoin_hashes::sha256d::Hash as Sha256d; use bitcoin_hashes::Hash; use secp256k1::Secp256k1; @@ -46,6 +47,15 @@ pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: } } +pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) { + let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new()); + for i in 2..depth + 1 { + header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new()); + } +} + pub struct Node { pub chain_monitor: Arc, pub tx_broadcaster: Arc, @@ -542,6 +552,33 @@ macro_rules! expect_pending_htlcs_forwardable { }} } +macro_rules! expect_payment_received { + ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => { + let events = $node.node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PaymentReceived { ref payment_hash, amt } => { + assert_eq!($expected_payment_hash, *payment_hash); + assert_eq!($expected_recv_value, amt); + }, + _ => panic!("Unexpected event"), + } + } +} + +macro_rules! expect_payment_sent { + ($node: expr, $expected_payment_preimage: expr) => { + let events = $node.node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PaymentSent { ref payment_preimage } => { + assert_eq!($expected_payment_preimage, *payment_preimage); + }, + _ => panic!("Unexpected event"), + } + } +} + pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) { let mut payment_event = { origin_node.node.send_payment(route, our_payment_hash).unwrap(); @@ -664,14 +701,7 @@ pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], s if !skip_last { last_update_fulfill_dance!(origin_node, expected_route.first().unwrap()); - let events = origin_node.node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PaymentSent { payment_preimage } => { - assert_eq!(payment_preimage, our_payment_preimage); - }, - _ => panic!("Unexpected event"), - } + expect_payment_sent!(origin_node, our_payment_preimage); } } @@ -702,7 +732,7 @@ pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value let err = origin_node.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 our 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"), _ => panic!("Unknown error variants"), }; } @@ -713,7 +743,7 @@ pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) { } pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) { - assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, 0)); + assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash)); expect_pending_htlcs_forwardable!(expected_route.last().unwrap()); check_added_monitors!(expected_route.last().unwrap(), 1); @@ -801,10 +831,10 @@ pub fn create_network(node_count: usize) -> Vec { let mut seed = [0; 32]; rng.fill_bytes(&mut seed); let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger))); - let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone())); + let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone())); let mut config = UserConfig::new(); config.channel_options.announced_channel = true; - config.channel_limits.force_announced_channel_preference = false; + config.peer_channel_config_limits.force_announced_channel_preference = false; let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap(); let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger)); nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed, @@ -935,20 +965,6 @@ pub fn get_announce_close_broadcast_events(nodes: &Vec, a: usize, b: usize } } -macro_rules! expect_payment_received { - ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => { - let events = $node.node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PaymentReceived { ref payment_hash, amt } => { - assert_eq!($expected_payment_hash, *payment_hash); - assert_eq!($expected_recv_value, amt); - }, - _ => panic!("Unexpected event"), - } - } -} - macro_rules! get_channel_value_stat { ($node: expr, $channel_id: expr) => {{ let chan_lock = $node.node.channel_state.lock().unwrap();