@@ -20,7 +20,9 @@ use bitcoin::network::constants::Network;
2020use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
2121
2222use crate :: io;
23- use crate :: sync:: Arc ;
23+ use crate :: sync:: { Arc , Mutex } ;
24+
25+ use crate :: prelude:: * ;
2426
2527struct MessengerNode {
2628 keys_manager : Arc < test_utils:: TestKeysInterface > ,
@@ -32,7 +34,7 @@ struct MessengerNode {
3234 Arc < TestOffersMessageHandler > ,
3335 Arc < TestCustomMessageHandler >
3436 > ,
35- logger : Arc < test_utils :: TestLogger > ,
37+ custom_handler : Arc < TestCustomMessageHandler > ,
3638}
3739
3840impl MessengerNode {
@@ -45,7 +47,7 @@ struct TestMessageRouter {}
4547
4648impl MessageRouter for TestMessageRouter {
4749 fn find_route ( & self , _sender : & PublicKey , _destination : & Destination ) -> Vec < PublicKey > {
48- todo ! ( )
50+ vec ! [ ]
4951 }
5052}
5153
@@ -63,7 +65,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
6365 }
6466}
6567
66- #[ derive( Clone ) ]
68+ #[ derive( Clone , Debug , PartialEq ) ]
6769enum TestCustomMessage {
6870 Request ,
6971 Response ,
@@ -92,11 +94,38 @@ impl Writeable for TestCustomMessage {
9294 }
9395}
9496
95- struct TestCustomMessageHandler { }
97+ struct TestCustomMessageHandler {
98+ expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
99+ }
100+
101+ impl TestCustomMessageHandler {
102+ fn new ( ) -> Self {
103+ Self { expected_messages : Mutex :: new ( VecDeque :: new ( ) ) }
104+ }
105+
106+ fn expect_message ( & self , message : TestCustomMessage ) {
107+ self . expected_messages . lock ( ) . unwrap ( ) . push_back ( message) ;
108+ }
109+ }
110+
111+ impl Drop for TestCustomMessageHandler {
112+ fn drop ( & mut self ) {
113+ #[ cfg( feature = "std" ) ] {
114+ if !std:: thread:: panicking ( ) {
115+ assert ! ( self . expected_messages. lock( ) . unwrap( ) . is_empty( ) ) ;
116+ }
117+ }
118+ }
119+ }
96120
97121impl CustomOnionMessageHandler for TestCustomMessageHandler {
98122 type CustomMessage = TestCustomMessage ;
99123 fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
124+ match self . expected_messages . lock ( ) . unwrap ( ) . pop_front ( ) {
125+ Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
126+ None => panic ! ( "Unexpected message: {:?}" , msg) ,
127+ }
128+
100129 match msg {
101130 TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
102131 TestCustomMessage :: Response => None ,
@@ -133,14 +162,14 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
133162 let logger = Arc :: new ( test_utils:: TestLogger :: with_id ( format ! ( "node {}" , i) ) ) ;
134163 let seed = [ i as u8 ; 32 ] ;
135164 let keys_manager = Arc :: new ( test_utils:: TestKeysInterface :: new ( & seed, Network :: Testnet ) ) ;
165+ let custom_handler = Arc :: new ( TestCustomMessageHandler :: new ( ) ) ;
136166 nodes. push ( MessengerNode {
137167 keys_manager : keys_manager. clone ( ) ,
138168 messenger : OnionMessenger :: new (
139- keys_manager. clone ( ) , keys_manager. clone ( ) , logger. clone ( ) ,
140- Arc :: new ( TestMessageRouter { } ) , Arc :: new ( TestOffersMessageHandler { } ) ,
141- Arc :: new ( TestCustomMessageHandler { } )
169+ Arc :: clone ( & keys_manager) , keys_manager, logger, Arc :: new ( TestMessageRouter { } ) ,
170+ Arc :: new ( TestOffersMessageHandler { } ) , Arc :: clone ( & custom_handler)
142171 ) ,
143- logger ,
172+ custom_handler ,
144173 } ) ;
145174 }
146175 for idx in 0 ..num_messengers - 1 {
@@ -156,20 +185,14 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
156185
157186fn pass_along_path ( path : & Vec < MessengerNode > , expected_path_id : Option < [ u8 ; 32 ] > ) {
158187 let mut prev_node = & path[ 0 ] ;
159- let num_nodes = path. len ( ) ;
160- for ( idx, node) in path. into_iter ( ) . skip ( 1 ) . enumerate ( ) {
188+ for node in path. into_iter ( ) . skip ( 1 ) {
161189 let events = prev_node. messenger . release_pending_msgs ( ) ;
162190 let onion_msg = {
163191 let msgs = events. get ( & node. get_node_pk ( ) ) . unwrap ( ) ;
164192 assert_eq ! ( msgs. len( ) , 1 ) ;
165193 msgs[ 0 ] . clone ( )
166194 } ;
167195 node. messenger . handle_onion_message ( & prev_node. get_node_pk ( ) , & onion_msg) ;
168- if idx == num_nodes - 1 {
169- node. logger . assert_log_contains (
170- "lightning::onion_message::messenger" . to_string ( ) ,
171- format ! ( "Received an onion message with path_id: {:02x?}" , expected_path_id) . to_string ( ) , 1 ) ;
172- }
173196 prev_node = node;
174197 }
175198}
@@ -180,6 +203,7 @@ fn one_hop() {
180203 let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
181204
182205 nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
206+ nodes[ 1 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
183207 pass_along_path ( & nodes, None ) ;
184208}
185209
@@ -189,6 +213,7 @@ fn two_unblinded_hops() {
189213 let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
190214
191215 nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
216+ nodes[ 2 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
192217 pass_along_path ( & nodes, None ) ;
193218}
194219
@@ -201,6 +226,7 @@ fn two_unblinded_two_blinded() {
201226 let blinded_path = BlindedPath :: new ( & [ nodes[ 3 ] . get_node_pk ( ) , nodes[ 4 ] . get_node_pk ( ) ] , & * nodes[ 4 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
202227
203228 nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , Destination :: BlindedPath ( blinded_path) , test_msg, None ) . unwrap ( ) ;
229+ nodes[ 4 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
204230 pass_along_path ( & nodes, None ) ;
205231}
206232
@@ -213,6 +239,7 @@ fn three_blinded_hops() {
213239 let blinded_path = BlindedPath :: new ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) , nodes[ 3 ] . get_node_pk ( ) ] , & * nodes[ 3 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
214240
215241 nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , test_msg, None ) . unwrap ( ) ;
242+ nodes[ 3 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
216243 pass_along_path ( & nodes, None ) ;
217244}
218245
@@ -239,11 +266,13 @@ fn we_are_intro_node() {
239266 let blinded_path = BlindedPath :: new ( & [ nodes[ 0 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , & * nodes[ 2 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
240267
241268 nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
269+ nodes[ 2 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
242270 pass_along_path ( & nodes, None ) ;
243271
244272 // Try with a two-hop blinded path where we are the introduction node.
245273 let blinded_path = BlindedPath :: new ( & [ nodes[ 0 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) ] , & * nodes[ 1 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
246274 nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
275+ nodes[ 1 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
247276 nodes. remove ( 2 ) ;
248277 pass_along_path ( & nodes, None ) ;
249278}
@@ -271,28 +300,31 @@ fn invalid_blinded_path_error() {
271300
272301#[ test]
273302fn reply_path ( ) {
274- let nodes = create_nodes ( 4 ) ;
275- let test_msg = TestCustomMessage :: Response ;
303+ let mut nodes = create_nodes ( 4 ) ;
304+ let test_msg = TestCustomMessage :: Request ;
276305 let secp_ctx = Secp256k1 :: new ( ) ;
277306
278307 // Destination::Node
279308 let reply_path = BlindedPath :: new ( & [ nodes[ 2 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 0 ] . get_node_pk ( ) ] , & * nodes[ 0 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
280309 nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 3 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , Some ( reply_path) ) . unwrap ( ) ;
310+ nodes[ 3 ] . custom_handler . expect_message ( TestCustomMessage :: Request ) ;
281311 pass_along_path ( & nodes, None ) ;
282312 // Make sure the last node successfully decoded the reply path.
283- nodes[ 3 ] . logger . assert_log_contains (
284- "lightning::onion_message::messenger" . to_string ( ) ,
285- format ! ( "Received an onion message with path_id None and a reply_path" ) . to_string ( ) , 1 ) ;
313+ nodes[ 0 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
314+ nodes . reverse ( ) ;
315+ pass_along_path ( & nodes , None ) ;
286316
287317 // Destination::BlindedPath
288318 let blinded_path = BlindedPath :: new ( & [ nodes[ 1 ] . get_node_pk ( ) , nodes[ 2 ] . get_node_pk ( ) , nodes[ 3 ] . get_node_pk ( ) ] , & * nodes[ 3 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
289319 let reply_path = BlindedPath :: new ( & [ nodes[ 2 ] . get_node_pk ( ) , nodes[ 1 ] . get_node_pk ( ) , nodes[ 0 ] . get_node_pk ( ) ] , & * nodes[ 0 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
290320
291321 nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: BlindedPath ( blinded_path) , OnionMessageContents :: Custom ( test_msg) , Some ( reply_path) ) . unwrap ( ) ;
322+ nodes[ 3 ] . custom_handler . expect_message ( TestCustomMessage :: Request ) ;
323+ pass_along_path ( & nodes, None ) ;
324+ // Make sure the last node successfully decoded the reply path.
325+ nodes[ 0 ] . custom_handler . expect_message ( TestCustomMessage :: Response ) ;
326+ nodes. reverse ( ) ;
292327 pass_along_path ( & nodes, None ) ;
293- nodes[ 3 ] . logger . assert_log_contains (
294- "lightning::onion_message::messenger" . to_string ( ) ,
295- format ! ( "Received an onion message with path_id None and a reply_path" ) . to_string ( ) , 2 ) ;
296328}
297329
298330#[ test]
0 commit comments