From 1ce922c631aac879b3569047a8f17e2a5df5e76a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 17 Oct 2021 21:23:51 +0000 Subject: [PATCH] Prefer fully-specified paths in test macros This avoids macros being context-specific use-dependent. --- lightning-persister/src/lib.rs | 3 +- lightning/src/ln/chanmon_update_fail_tests.rs | 6 +-- lightning/src/ln/functional_test_utils.rs | 49 ++++++++++++------- lightning/src/ln/functional_tests.rs | 3 -- lightning/src/ln/monitor_tests.rs | 7 +-- lightning/src/ln/onion_route_tests.rs | 3 +- lightning/src/ln/payment_tests.rs | 5 +- lightning/src/ln/reorg_tests.rs | 5 +- lightning/src/ln/shutdown_tests.rs | 4 -- 9 files changed, 39 insertions(+), 46 deletions(-) diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index eba069248..b853b5796 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -191,8 +191,7 @@ mod tests { use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors}; use lightning::ln::features::InitFeatures; use lightning::ln::functional_test_utils::*; - use lightning::ln::msgs::ErrorAction; - use lightning::util::events::{ClosureReason, Event, MessageSendEventsProvider, MessageSendEvent}; + use lightning::util::events::{ClosureReason, MessageSendEventsProvider}; use lightning::util::test_utils; use std::fs; #[cfg(target_os = "windows")] diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index cb39b64a4..236b1e498 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -19,11 +19,10 @@ use bitcoin::network::constants::Network; use chain::channelmonitor::ChannelMonitor; use chain::transaction::OutPoint; use chain::{ChannelMonitorUpdateErr, Listen, Watch}; -use ln::{PaymentPreimage, PaymentHash}; use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure}; use ln::features::InitFeatures; use ln::msgs; -use ln::msgs::{ChannelMessageHandler, ErrorAction, RoutingMessageHandler}; +use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler}; use util::config::UserConfig; use util::enforcing_trait_impls::EnforcingSigner; use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason}; @@ -31,9 +30,6 @@ use util::errors::APIError; use util::ser::{ReadableArgs, Writeable}; use util::test_utils::TestBroadcaster; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; - use ln::functional_test_utils::*; use util::test_utils; diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index bf3e192b2..000c31964 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -33,8 +33,6 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::network::constants::Network; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; use bitcoin::hash_types::BlockHash; use bitcoin::secp256k1::key::PublicKey; @@ -337,9 +335,11 @@ pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, (announcement, as_update, bs_update, channel_id, tx) } +#[macro_export] macro_rules! get_revoke_commit_msgs { ($node: expr, $node_id: expr) => { { + use util::events::MessageSendEvent; let events = $node.node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 2); (match events[0] { @@ -401,13 +401,14 @@ macro_rules! get_event { } #[cfg(test)] +#[macro_export] macro_rules! get_htlc_update_msgs { ($node: expr, $node_id: expr) => { { let events = $node.node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); match events[0] { - MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => { + $crate::util::events::MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => { assert_eq!(*node_id, $node_id); (*updates).clone() }, @@ -704,6 +705,7 @@ pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(nodes: &'a Vec { { @@ -768,6 +770,9 @@ macro_rules! get_closing_signed_broadcast { #[macro_export] macro_rules! check_closed_broadcast { ($node: expr, $with_error_msg: expr) => {{ + use $crate::util::events::MessageSendEvent; + use $crate::ln::msgs::ErrorAction; + let msg_events = $node.node.get_and_clear_pending_msg_events(); assert_eq!(msg_events.len(), if $with_error_msg { 2 } else { 1 }); match msg_events[0] { @@ -795,6 +800,8 @@ macro_rules! check_closed_event { check_closed_event!($node, $events, $reason, false); }; ($node: expr, $events: expr, $reason: expr, $is_check_discard_funding: expr) => {{ + use $crate::util::events::Event; + let events = $node.node.get_and_clear_pending_events(); assert_eq!(events.len(), $events); let expected_reason = $reason; @@ -1003,10 +1010,12 @@ macro_rules! get_payment_preimage_hash { }; ($dest_node: expr, $min_value_msat: expr) => { { + use bitcoin::hashes::Hash as _; let mut payment_count = $dest_node.network_payment_count.borrow_mut(); - let payment_preimage = PaymentPreimage([*payment_count; 32]); + let payment_preimage = $crate::ln::PaymentPreimage([*payment_count; 32]); *payment_count += 1; - let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()); + let payment_hash = $crate::ln::PaymentHash( + bitcoin::hashes::sha256::Hash::hash(&payment_preimage.0[..]).into_inner()); let payment_secret = $dest_node.node.create_inbound_payment_for_hash(payment_hash, $min_value_msat, 7200).unwrap(); (payment_preimage, payment_hash, payment_secret) } @@ -1014,17 +1023,18 @@ macro_rules! get_payment_preimage_hash { } #[cfg(test)] +#[macro_export] macro_rules! get_route_and_payment_hash { ($send_node: expr, $recv_node: expr, $recv_value: expr) => {{ - get_route_and_payment_hash!($send_node, $recv_node, vec![], $recv_value, TEST_FINAL_CLTV) + $crate::get_route_and_payment_hash!($send_node, $recv_node, vec![], $recv_value, TEST_FINAL_CLTV) }}; ($send_node: expr, $recv_node: expr, $last_hops: expr, $recv_value: expr, $cltv: expr) => {{ - let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!($recv_node, Some($recv_value)); + let (payment_preimage, payment_hash, payment_secret) = $crate::get_payment_preimage_hash!($recv_node, Some($recv_value)); let payee = $crate::routing::router::Payee::from_node_id($recv_node.node.get_our_node_id()) .with_features($crate::ln::features::InvoiceFeatures::known()) .with_route_hints($last_hops); - let scorer = ::util::test_utils::TestScorer::with_fixed_penalty(0); - let route = ::routing::router::get_route( + let scorer = $crate::util::test_utils::TestScorer::with_fixed_penalty(0); + let route = $crate::routing::router::get_route( &$send_node.node.get_our_node_id(), &payee, $send_node.network_graph, Some(&$send_node.node.list_usable_channels().iter().collect::>()), $recv_value, $cltv, $send_node.logger, &scorer @@ -1088,6 +1098,7 @@ macro_rules! expect_payment_received { } #[cfg(test)] +#[macro_export] macro_rules! expect_payment_sent_without_paths { ($node: expr, $expected_payment_preimage: expr) => { expect_payment_sent!($node, $expected_payment_preimage, None::, false); @@ -1097,23 +1108,26 @@ macro_rules! expect_payment_sent_without_paths { } } +#[macro_export] macro_rules! expect_payment_sent { ($node: expr, $expected_payment_preimage: expr) => { - expect_payment_sent!($node, $expected_payment_preimage, None::, true); + $crate::expect_payment_sent!($node, $expected_payment_preimage, None::, true); }; ($node: expr, $expected_payment_preimage: expr, $expected_fee_msat_opt: expr) => { - expect_payment_sent!($node, $expected_payment_preimage, $expected_fee_msat_opt, true); + $crate::expect_payment_sent!($node, $expected_payment_preimage, $expected_fee_msat_opt, true); }; - ($node: expr, $expected_payment_preimage: expr, $expected_fee_msat_opt: expr, $expect_paths: expr) => { + ($node: expr, $expected_payment_preimage: expr, $expected_fee_msat_opt: expr, $expect_paths: expr) => { { + use bitcoin::hashes::Hash as _; let events = $node.node.get_and_clear_pending_events(); - let expected_payment_hash = PaymentHash(Sha256::hash(&$expected_payment_preimage.0).into_inner()); + let expected_payment_hash = $crate::ln::PaymentHash( + bitcoin::hashes::sha256::Hash::hash(&$expected_payment_preimage.0).into_inner()); if $expect_paths { assert!(events.len() > 1); } else { assert_eq!(events.len(), 1); } let expected_payment_id = match events[0] { - Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => { + $crate::util::events::Event::PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => { assert_eq!($expected_payment_preimage, *payment_preimage); assert_eq!(expected_payment_hash, *payment_hash); assert!(fee_paid_msat.is_some()); @@ -1127,7 +1141,7 @@ macro_rules! expect_payment_sent { if $expect_paths { for i in 1..events.len() { match events[i] { - Event::PaymentPathSuccessful { payment_id, payment_hash, .. } => { + $crate::util::events::Event::PaymentPathSuccessful { payment_id, payment_hash, .. } => { assert_eq!(payment_id, expected_payment_id); assert_eq!(payment_hash, Some(expected_payment_hash)); }, @@ -1135,16 +1149,17 @@ macro_rules! expect_payment_sent { } } } - } + } } } #[cfg(test)] +#[macro_export] macro_rules! expect_payment_path_successful { ($node: expr) => { let events = $node.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - Event::PaymentPathSuccessful { .. } => {}, + $crate::util::events::Event::PaymentPathSuccessful { .. } => {}, _ => panic!("Unexpected event"), } } diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index f144dbd9a..37c319c57 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -42,9 +42,6 @@ use bitcoin::blockdata::opcodes; use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; - use bitcoin::secp256k1::Secp256k1; use bitcoin::secp256k1::key::{PublicKey,SecretKey}; diff --git a/lightning/src/ln/monitor_tests.rs b/lightning/src/ln/monitor_tests.rs index cfde643bc..d836b736a 100644 --- a/lightning/src/ln/monitor_tests.rs +++ b/lightning/src/ln/monitor_tests.rs @@ -11,16 +11,13 @@ use chain::channelmonitor::{ANTI_REORG_DELAY, Balance}; use chain::transaction::OutPoint; -use ln::{channel, PaymentPreimage, PaymentHash}; +use ln::channel; use ln::channelmanager::BREAKDOWN_TIMEOUT; use ln::features::InitFeatures; -use ln::msgs::{ChannelMessageHandler, ErrorAction}; +use ln::msgs::ChannelMessageHandler; use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason}; use routing::network_graph::NetworkUpdate; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; - use bitcoin::blockdata::script::Builder; use bitcoin::blockdata::opcodes; use bitcoin::secp256k1::Secp256k1; diff --git a/lightning/src/ln/onion_route_tests.rs b/lightning/src/ln/onion_route_tests.rs index e80cf98d7..05b108fbf 100644 --- a/lightning/src/ln/onion_route_tests.rs +++ b/lightning/src/ln/onion_route_tests.rs @@ -12,7 +12,7 @@ //! returned errors decode to the correct thing. use chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS}; -use ln::{PaymentPreimage, PaymentHash, PaymentSecret}; +use ln::{PaymentHash, PaymentSecret}; use ln::channelmanager::{HTLCForwardInfo, CLTV_FAR_FAR_AWAY}; use ln::onion_utils; use routing::network_graph::NetworkUpdate; @@ -26,7 +26,6 @@ use util::config::UserConfig; use bitcoin::hash_types::BlockHash; -use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::Hash; use bitcoin::secp256k1; diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index 7c9ff3199..518ccd3b0 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -14,11 +14,10 @@ use chain::{ChannelMonitorUpdateErr, Confirm, Listen, Watch}; use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor, LATENCY_GRACE_PERIOD_BLOCKS}; use chain::transaction::OutPoint; -use ln::{PaymentPreimage, PaymentHash}; use ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, ChannelManagerReadArgs, PaymentId, PaymentSendFailure}; use ln::features::InitFeatures; use ln::msgs; -use ln::msgs::{ChannelMessageHandler, ErrorAction}; +use ln::msgs::ChannelMessageHandler; use util::events::{ClosureReason, Event, MessageSendEvent, MessageSendEventsProvider}; use util::test_utils; use util::errors::APIError; @@ -26,8 +25,6 @@ use util::enforcing_trait_impls::EnforcingSigner; use util::ser::{ReadableArgs, Writeable}; use io; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; use bitcoin::{Block, BlockHeader, BlockHash}; use prelude::*; diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index 5cd1a890c..570dc3d0e 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -12,10 +12,9 @@ use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor}; use chain::transaction::OutPoint; use chain::{Confirm, Watch}; -use ln::PaymentHash; use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs}; use ln::features::InitFeatures; -use ln::msgs::{ChannelMessageHandler, ErrorAction}; +use ln::msgs::ChannelMessageHandler; use routing::network_graph::NetworkUpdate; use util::enforcing_trait_impls::EnforcingSigner; use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason}; @@ -25,8 +24,6 @@ use util::ser::{ReadableArgs, Writeable}; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::script::Builder; use bitcoin::blockdata::opcodes; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; use bitcoin::hash_types::BlockHash; use bitcoin::secp256k1::Secp256k1; diff --git a/lightning/src/ln/shutdown_tests.rs b/lightning/src/ln/shutdown_tests.rs index db76a2c76..ffd809bc7 100644 --- a/lightning/src/ln/shutdown_tests.rs +++ b/lightning/src/ln/shutdown_tests.rs @@ -11,7 +11,6 @@ use chain::keysinterface::KeysInterface; use chain::transaction::OutPoint; -use ln::{PaymentPreimage, PaymentHash}; use ln::channelmanager::PaymentSendFailure; use routing::router::{Payee, get_route}; use routing::network_graph::NetworkUpdate; @@ -28,9 +27,6 @@ use util::config::UserConfig; use bitcoin::blockdata::script::Builder; use bitcoin::blockdata::opcodes; -use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::hashes::Hash; - use regex; use core::default::Default; -- 2.39.5