@@ -1585,12 +1585,12 @@ async fn track_payment_result(
15851585#[ cfg( test) ]
15861586mod tests {
15871587 use crate :: clock:: SystemClock ;
1588- use crate :: test_utils:: MockLightningNode ;
1588+ use crate :: test_utils:: { MockLightningNode , TestNodesResult } ;
15891589 use crate :: {
15901590 get_payment_delay, test_utils, test_utils:: LightningTestNodeBuilder , LightningError ,
15911591 LightningNode , MutRng , PaymentGenerationError , PaymentGenerator ,
15921592 } ;
1593- use crate :: { Simulation , SimulationCfg } ;
1593+ use crate :: { NodeInfo , Simulation , SimulationCfg } ;
15941594 use bitcoin:: secp256k1:: PublicKey ;
15951595 use bitcoin:: Network ;
15961596 use mockall:: mock;
@@ -1915,213 +1915,95 @@ mod tests {
19151915 assert ! ( result. is_ok( ) ) ;
19161916 }
19171917
1918- #[ allow( clippy:: type_complexity) ]
1919- /// Helper to create and configure mock nodes for testing
1920- fn setup_test_nodes_for_testing_deterministic_events (
1921- fixed_pubkeys : Option < Vec < PublicKey > > ,
1922- ) -> (
1923- (
1924- crate :: NodeInfo ,
1925- crate :: NodeInfo ,
1926- crate :: NodeInfo ,
1927- crate :: NodeInfo ,
1928- ) ,
1929- HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > > ,
1930- Arc < StdMutex < Vec < PublicKey > > > ,
1918+ async fn mock_send_payment (
1919+ mock_node : & mut Arc < Mutex < MockLightningNode > > ,
1920+ node_info : NodeInfo ,
1921+ payments_list : Arc < StdMutex < Vec < PublicKey > > > ,
1922+ payment_hash : [ u8 ; 32 ] ,
19311923 ) {
1932- let network = LightningTestNodeBuilder :: new ( 4 )
1933- . with_networks ( vec ! [
1934- Network :: Regtest ,
1935- Network :: Regtest ,
1936- Network :: Regtest ,
1937- Network :: Regtest ,
1938- ] )
1939- . build_full ( ) ;
1940- let mut clients = network. get_client_hashmap ( ) ;
1941-
1942- let node_1 = & network. nodes [ 0 ] ;
1943- let mut mock_node_1 = MockLightningNode :: new ( ) ;
1944-
1945- let payments_list = Arc :: new ( StdMutex :: new ( Vec :: new ( ) ) ) ;
1946-
1947- if fixed_pubkeys. is_some ( ) {
1948- clients = HashMap :: new ( ) ;
1949- }
1950-
1951- // Set up node 1 expectations
1952- let mut node_1_clone = node_1. clone ( ) ;
1953- if let Some ( ref pubkeys) = fixed_pubkeys {
1954- node_1_clone. pubkey = pubkeys[ 0 ]
1955- }
1956- mock_node_1
1957- . expect_get_info ( )
1958- . return_const ( node_1_clone. clone ( ) ) ;
1959- mock_node_1
1924+ let mut mock_node = mock_node. lock ( ) . await ;
1925+ mock_node. expect_get_info ( ) . return_const ( node_info. clone ( ) ) ;
1926+ mock_node
19601927 . expect_get_network ( )
19611928 . returning ( || Network :: Regtest ) ;
1962- mock_node_1
1929+ mock_node
19631930 . expect_list_channels ( )
19641931 . returning ( || Ok ( vec ! [ 100_000_000 ] ) ) ;
1965- mock_node_1
1932+ mock_node
19661933 . expect_get_node_info ( )
1967- . returning ( move |_| Ok ( node_1_clone . clone ( ) ) ) ;
1968- mock_node_1 . expect_track_payment ( ) . returning ( |_, _| {
1934+ . returning ( move |_| Ok ( node_info . clone ( ) ) ) ;
1935+ mock_node . expect_track_payment ( ) . returning ( |_, _| {
19691936 Ok ( crate :: PaymentResult {
19701937 htlc_count : 1 ,
19711938 payment_outcome : crate :: PaymentOutcome :: Success ,
19721939 } )
19731940 } ) ;
1974- let pl1 = payments_list. clone ( ) ;
1975- mock_node_1. expect_send_payment ( ) . returning ( move |a, _| {
1976- pl1. lock ( ) . unwrap ( ) . push ( a) ;
1977- Ok ( lightning:: ln:: PaymentHash ( [ 0 ; 32 ] ) )
1978- } ) ;
19791941
1980- if let Some ( ref pubkeys) = fixed_pubkeys {
1981- clients. insert ( pubkeys[ 0 ] , Arc :: new ( Mutex :: new ( mock_node_1) ) ) ;
1982- } else {
1983- clients. insert ( node_1. pubkey , Arc :: new ( Mutex :: new ( mock_node_1) ) ) ;
1984- }
1985-
1986- let node_2 = & network. nodes [ 1 ] ;
1987-
1988- let mut mock_node_2 = MockLightningNode :: new ( ) ;
1989-
1990- // Set up node 2 expectations
1991- let mut node_2_clone = node_2. clone ( ) ;
1992- if let Some ( ref pubkeys) = fixed_pubkeys {
1993- node_2_clone. pubkey = pubkeys[ 1 ]
1994- }
1995- mock_node_2
1996- . expect_get_info ( )
1997- . return_const ( node_2_clone. clone ( ) ) ;
1998- mock_node_2
1999- . expect_get_network ( )
2000- . returning ( || Network :: Regtest ) ;
2001- mock_node_2
2002- . expect_list_channels ( )
2003- . returning ( || Ok ( vec ! [ 100_000_000 ] ) ) ;
2004- mock_node_2
2005- . expect_get_node_info ( )
2006- . returning ( move |_| Ok ( node_2_clone. clone ( ) ) ) ;
2007- mock_node_2. expect_track_payment ( ) . returning ( |_, _| {
2008- Ok ( crate :: PaymentResult {
2009- htlc_count : 1 ,
2010- payment_outcome : crate :: PaymentOutcome :: Success ,
2011- } )
2012- } ) ;
2013- let pl2 = payments_list. clone ( ) ;
2014- mock_node_2. expect_send_payment ( ) . returning ( move |a, _| {
2015- pl2. lock ( ) . unwrap ( ) . push ( a) ;
2016- Ok ( lightning:: ln:: PaymentHash ( [ 1 ; 32 ] ) )
2017- } ) ;
2018-
2019- if let Some ( ref pubkeys) = fixed_pubkeys {
2020- clients. insert ( pubkeys[ 1 ] , Arc :: new ( Mutex :: new ( mock_node_2) ) ) ;
2021- } else {
2022- clients. insert ( node_2. pubkey , Arc :: new ( Mutex :: new ( mock_node_2) ) ) ;
2023- }
2024-
2025- let node_3 = & network. nodes [ 2 ] ;
2026-
2027- let mut mock_node_3 = MockLightningNode :: new ( ) ;
2028-
2029- // Set up node 3 expectations
2030- let mut node_3_clone = node_3. clone ( ) ;
2031- if let Some ( ref pubkeys) = fixed_pubkeys {
2032- node_3_clone. pubkey = pubkeys[ 2 ]
2033- }
2034- mock_node_3
2035- . expect_get_info ( )
2036- . return_const ( node_3_clone. clone ( ) ) ;
2037- mock_node_3
2038- . expect_get_network ( )
2039- . returning ( || Network :: Regtest ) ;
2040- mock_node_3
2041- . expect_list_channels ( )
2042- . returning ( || Ok ( vec ! [ 100_000_000 ] ) ) ;
2043- mock_node_3
2044- . expect_get_node_info ( )
2045- . returning ( move |_| Ok ( node_3_clone. clone ( ) ) ) ;
2046- mock_node_3. expect_track_payment ( ) . returning ( |_, _| {
2047- Ok ( crate :: PaymentResult {
2048- htlc_count : 1 ,
2049- payment_outcome : crate :: PaymentOutcome :: Success ,
2050- } )
2051- } ) ;
2052- let pl3 = payments_list. clone ( ) ;
2053- mock_node_3. expect_send_payment ( ) . returning ( move |a, _| {
2054- pl3. lock ( ) . unwrap ( ) . push ( a) ;
2055- Ok ( lightning:: ln:: PaymentHash ( [ 2 ; 32 ] ) )
1942+ let pl = payments_list. clone ( ) ;
1943+ mock_node. expect_send_payment ( ) . returning ( move |a, _| {
1944+ pl. lock ( ) . unwrap ( ) . push ( a) ;
1945+ Ok ( lightning:: ln:: PaymentHash ( payment_hash) )
20561946 } ) ;
1947+ }
20571948
2058- if let Some ( ref pubkeys) = fixed_pubkeys {
2059- clients. insert ( pubkeys[ 2 ] , Arc :: new ( Mutex :: new ( mock_node_3) ) ) ;
2060- } else {
2061- clients. insert ( node_3. pubkey , Arc :: new ( Mutex :: new ( mock_node_3) ) ) ;
1949+ #[ allow( clippy:: type_complexity) ]
1950+ /// Helper to create and configure mock nodes for testing
1951+ async fn setup_test_nodes_for_testing_deterministic_events (
1952+ fixed_pubkeys : Option < Vec < PublicKey > > ,
1953+ ) -> ( TestNodesResult , Arc < StdMutex < Vec < PublicKey > > > ) {
1954+ let mut builder = LightningTestNodeBuilder :: new ( 4 ) ;
1955+ if let Some ( fixed_pubkeys) = fixed_pubkeys {
1956+ builder = builder. with_fixed_pubkeys ( fixed_pubkeys)
20621957 }
1958+ let mut network = builder. build_full ( ) ;
20631959
2064- let node_4 = & network . nodes [ 3 ] ;
1960+ let payments_list = Arc :: new ( StdMutex :: new ( Vec :: new ( ) ) ) ;
20651961
2066- let mut mock_node_4 = MockLightningNode :: new ( ) ;
1962+ mock_send_payment (
1963+ & mut network. clients [ 0 ] ,
1964+ network. nodes [ 0 ] . clone ( ) ,
1965+ payments_list. clone ( ) ,
1966+ [ 0 ; 32 ] ,
1967+ )
1968+ . await ;
20671969
2068- // Set up node 4 expectations
2069- let mut node_4_clone = node_4. clone ( ) ;
2070- if let Some ( ref pubkeys) = fixed_pubkeys {
2071- node_4_clone. pubkey = pubkeys[ 3 ]
2072- }
2073- mock_node_4
2074- . expect_get_info ( )
2075- . return_const ( node_4_clone. clone ( ) ) ;
2076- mock_node_4
2077- . expect_get_network ( )
2078- . returning ( || Network :: Regtest ) ;
2079- mock_node_4
2080- . expect_list_channels ( )
2081- . returning ( || Ok ( vec ! [ 100_000_000 ] ) ) ;
2082- mock_node_4
2083- . expect_get_node_info ( )
2084- . returning ( move |_| Ok ( node_4_clone. clone ( ) ) ) ;
2085- mock_node_4. expect_track_payment ( ) . returning ( |_, _| {
2086- Ok ( crate :: PaymentResult {
2087- htlc_count : 1 ,
2088- payment_outcome : crate :: PaymentOutcome :: Success ,
2089- } )
2090- } ) ;
2091- let pl4 = payments_list. clone ( ) ;
2092- mock_node_4. expect_send_payment ( ) . returning ( move |a, _| {
2093- pl4. lock ( ) . unwrap ( ) . push ( a) ;
2094- Ok ( lightning:: ln:: PaymentHash ( [ 3 ; 32 ] ) )
2095- } ) ;
1970+ mock_send_payment (
1971+ & mut network. clients [ 1 ] ,
1972+ network. nodes [ 1 ] . clone ( ) ,
1973+ payments_list. clone ( ) ,
1974+ [ 0 ; 32 ] ,
1975+ )
1976+ . await ;
20961977
2097- if let Some ( ref pubkeys) = fixed_pubkeys {
2098- clients. insert ( pubkeys[ 3 ] , Arc :: new ( Mutex :: new ( mock_node_4) ) ) ;
2099- } else {
2100- clients. insert ( node_4. pubkey , Arc :: new ( Mutex :: new ( mock_node_4) ) ) ;
2101- }
1978+ mock_send_payment (
1979+ & mut network. clients [ 2 ] ,
1980+ network. nodes [ 2 ] . clone ( ) ,
1981+ payments_list. clone ( ) ,
1982+ [ 0 ; 32 ] ,
1983+ )
1984+ . await ;
21021985
2103- (
2104- (
2105- node_1. clone ( ) ,
2106- node_2. clone ( ) ,
2107- node_3. clone ( ) ,
2108- node_4. clone ( ) ,
2109- ) ,
2110- clients,
2111- payments_list,
1986+ mock_send_payment (
1987+ & mut network. clients [ 3 ] ,
1988+ network. nodes [ 3 ] . clone ( ) ,
1989+ payments_list. clone ( ) ,
1990+ [ 0 ; 32 ] ,
21121991 )
1992+ . await ;
1993+
1994+ ( network, payments_list)
21131995 }
21141996
21151997 #[ tokio:: test]
21161998 async fn test_deterministic_payments_events_defined_activities ( ) {
2117- let ( ( node_1 , node_2 , node_3 , node_4 ) , clients , payments_list) =
2118- setup_test_nodes_for_testing_deterministic_events ( None ) ;
1999+ let ( network , payments_list) =
2000+ setup_test_nodes_for_testing_deterministic_events ( None ) . await ;
21192001
21202002 // Define two activities
21212003 // Activity 1: From node_1 to node_2
21222004 let activity_1 = crate :: ActivityDefinition {
2123- source : node_1 . clone ( ) ,
2124- destination : node_2 . clone ( ) ,
2005+ source : network . nodes [ 0 ] . clone ( ) ,
2006+ destination : network . nodes [ 1 ] . clone ( ) ,
21252007 start_secs : None ,
21262008 count : Some ( 5 ) ,
21272009 interval_secs : crate :: ValueOrRange :: Value ( 2 ) ,
@@ -2130,8 +2012,8 @@ mod tests {
21302012
21312013 // Activity 2: From node_3 to node_4
21322014 let activity_2 = crate :: ActivityDefinition {
2133- source : node_3 . clone ( ) ,
2134- destination : node_4 . clone ( ) ,
2015+ source : network . nodes [ 2 ] . clone ( ) ,
2016+ destination : network . nodes [ 3 ] . clone ( ) ,
21352017 start_secs : None ,
21362018 count : Some ( 5 ) ,
21372019 interval_secs : crate :: ValueOrRange :: Value ( 4 ) ,
@@ -2143,7 +2025,7 @@ mod tests {
21432025 // Create simulation without a timeout.
21442026 let simulation = Simulation :: new (
21452027 SimulationCfg :: new ( None , 100 , 2.0 , None , None ) ,
2146- clients ,
2028+ network . get_client_hashmap ( ) ,
21472029 TaskTracker :: new ( ) ,
21482030 Arc :: new ( SystemClock { } ) ,
21492031 shutdown_trigger,
@@ -2156,16 +2038,16 @@ mod tests {
21562038 let elapsed = start. elapsed ( ) ;
21572039
21582040 let expected_payment_list = vec ! [
2159- node_2 . pubkey,
2160- node_4 . pubkey,
2161- node_2 . pubkey,
2162- node_2 . pubkey,
2163- node_4 . pubkey,
2164- node_2 . pubkey,
2165- node_2 . pubkey,
2166- node_4 . pubkey,
2167- node_4 . pubkey,
2168- node_4 . pubkey,
2041+ network . nodes [ 1 ] . pubkey,
2042+ network . nodes [ 3 ] . pubkey,
2043+ network . nodes [ 1 ] . pubkey,
2044+ network . nodes [ 1 ] . pubkey,
2045+ network . nodes [ 3 ] . pubkey,
2046+ network . nodes [ 1 ] . pubkey,
2047+ network . nodes [ 1 ] . pubkey,
2048+ network . nodes [ 3 ] . pubkey,
2049+ network . nodes [ 3 ] . pubkey,
2050+ network . nodes [ 3 ] . pubkey,
21692051 ] ;
21702052
21712053 // Check that simulation ran 20ish seconds because
@@ -2203,15 +2085,15 @@ mod tests {
22032085 )
22042086 . unwrap ( ) ;
22052087
2206- let ( _ , clients , payments_list) =
2207- setup_test_nodes_for_testing_deterministic_events ( Some ( vec ! [ pk1, pk2, pk3, pk4] ) ) ;
2088+ let ( network , payments_list) =
2089+ setup_test_nodes_for_testing_deterministic_events ( Some ( vec ! [ pk1, pk2, pk3, pk4] ) ) . await ;
22082090
22092091 let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
22102092
22112093 // Create simulation with a defined seed.
22122094 let simulation = Simulation :: new (
22132095 SimulationCfg :: new ( Some ( 25 ) , 100 , 2.0 , None , Some ( 42 ) ) ,
2214- clients . clone ( ) ,
2096+ network . get_client_hashmap ( ) ,
22152097 TaskTracker :: new ( ) ,
22162098 Arc :: new ( SystemClock { } ) ,
22172099 shutdown_trigger,
@@ -2234,7 +2116,9 @@ mod tests {
22342116
22352117 assert ! (
22362118 payments_list. lock( ) . unwrap( ) . as_ref( ) == expected_payment_list,
2237- "The expected order of payments is not correct"
2119+ "The expected order of payments is not correct: {:?} vs {:?}" ,
2120+ payments_list. lock( ) . unwrap( ) ,
2121+ expected_payment_list,
22382122 ) ;
22392123
22402124 // remove all the payments made in the previous execution
@@ -2245,7 +2129,7 @@ mod tests {
22452129 // Create the same simulation as before but with different seed.
22462130 let simulation2 = Simulation :: new (
22472131 SimulationCfg :: new ( Some ( 25 ) , 100 , 2.0 , None , Some ( 500 ) ) ,
2248- clients ,
2132+ network . get_client_hashmap ( ) ,
22492133 TaskTracker :: new ( ) ,
22502134 Arc :: new ( SystemClock { } ) ,
22512135 shutdown_trigger,
0 commit comments