@@ -336,10 +336,11 @@ pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b,
336
336
}
337
337
338
338
#[ macro_export]
339
+ /// Gets an RAA and CS which were sent in response to a commitment update
339
340
macro_rules! get_revoke_commit_msgs {
340
341
( $node: expr, $node_id: expr) => {
341
342
{
342
- use util:: events:: MessageSendEvent ;
343
+ use $crate :: util:: events:: MessageSendEvent ;
343
344
let events = $node. node. get_and_clear_pending_msg_events( ) ;
344
345
assert_eq!( events. len( ) , 2 ) ;
345
346
( match events[ 0 ] {
@@ -400,8 +401,8 @@ macro_rules! get_event {
400
401
}
401
402
}
402
403
403
- #[ cfg( test) ]
404
404
#[ macro_export]
405
+ /// Gets an UpdateHTLCs MessageSendEvent
405
406
macro_rules! get_htlc_update_msgs {
406
407
( $node: expr, $node_id: expr) => {
407
408
{
@@ -933,6 +934,9 @@ impl SendEvent {
933
934
}
934
935
}
935
936
937
+ #[ macro_export]
938
+ /// Performs the "commitment signed dance" - the series of message exchanges which occur after a
939
+ /// commitment update.
936
940
macro_rules! commitment_signed_dance {
937
941
( $node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */ ) => {
938
942
{
@@ -999,7 +1003,7 @@ macro_rules! commitment_signed_dance {
999
1003
{
1000
1004
commitment_signed_dance!( $node_a, $node_b, $commitment_signed, $fail_backwards, true ) ;
1001
1005
if $fail_backwards {
1002
- expect_pending_htlcs_forwardable!( $node_a) ;
1006
+ $crate :: expect_pending_htlcs_forwardable!( $node_a) ;
1003
1007
check_added_monitors!( $node_a, 1 ) ;
1004
1008
1005
1009
let channel_state = $node_a. node. channel_state. lock( ) . unwrap( ) ;
@@ -1057,6 +1061,8 @@ macro_rules! get_route_and_payment_hash {
1057
1061
} }
1058
1062
}
1059
1063
1064
+ #[ macro_export]
1065
+ /// Clears (and ignores) a PendingHTLCsForwardable event
1060
1066
macro_rules! expect_pending_htlcs_forwardable_ignore {
1061
1067
( $node: expr) => { {
1062
1068
let events = $node. node. get_and_clear_pending_events( ) ;
@@ -1068,9 +1074,11 @@ macro_rules! expect_pending_htlcs_forwardable_ignore {
1068
1074
} }
1069
1075
}
1070
1076
1077
+ #[ macro_export]
1078
+ /// Handles a PendingHTLCsForwardable event
1071
1079
macro_rules! expect_pending_htlcs_forwardable {
1072
1080
( $node: expr) => { {
1073
- expect_pending_htlcs_forwardable_ignore!( $node) ;
1081
+ $crate :: expect_pending_htlcs_forwardable_ignore!( $node) ;
1074
1082
$node. node. process_pending_htlc_forwards( ) ;
1075
1083
1076
1084
// Ensure process_pending_htlc_forwards is idempotent.
@@ -1199,60 +1207,95 @@ macro_rules! expect_payment_forwarded {
1199
1207
}
1200
1208
}
1201
1209
1210
+ pub struct PaymentFailedConditions < ' a > {
1211
+ pub ( crate ) expected_htlc_error_data : Option < ( u16 , & ' a [ u8 ] ) > ,
1212
+ pub ( crate ) expected_blamed_scid : Option < u64 > ,
1213
+ pub ( crate ) expected_blamed_chan_closed : Option < bool > ,
1214
+ }
1215
+
1216
+ impl < ' a > PaymentFailedConditions < ' a > {
1217
+ pub fn new ( ) -> Self {
1218
+ Self {
1219
+ expected_htlc_error_data : None ,
1220
+ expected_blamed_scid : None ,
1221
+ expected_blamed_chan_closed : None ,
1222
+ }
1223
+ }
1224
+ pub fn blamed_scid ( mut self , scid : u64 ) -> Self {
1225
+ self . expected_blamed_scid = Some ( scid) ;
1226
+ self
1227
+ }
1228
+ pub fn blamed_chan_closed ( mut self , closed : bool ) -> Self {
1229
+ self . expected_blamed_chan_closed = Some ( closed) ;
1230
+ self
1231
+ }
1232
+ pub fn expected_htlc_error_data ( mut self , code : u16 , data : & ' a [ u8 ] ) -> Self {
1233
+ self . expected_htlc_error_data = Some ( ( code, data) ) ;
1234
+ self
1235
+ }
1236
+ }
1237
+
1202
1238
#[ cfg( test) ]
1203
1239
macro_rules! expect_payment_failed_with_update {
1204
1240
( $node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $scid: expr, $chan_closed: expr) => {
1205
- let events = $node. node. get_and_clear_pending_events( ) ;
1206
- assert_eq!( events. len( ) , 1 ) ;
1207
- match events[ 0 ] {
1208
- Event :: PaymentPathFailed { ref payment_hash, rejected_by_dest, ref network_update, ref error_code, ref error_data, ref path, ref retry, .. } => {
1209
- assert_eq!( * payment_hash, $expected_payment_hash, "unexpected payment_hash" ) ;
1210
- assert_eq!( rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value" ) ;
1211
- assert!( retry. is_some( ) , "expected retry.is_some()" ) ;
1212
- assert_eq!( retry. as_ref( ) . unwrap( ) . final_value_msat, path. last( ) . unwrap( ) . fee_msat, "Retry amount should match last hop in path" ) ;
1213
- assert_eq!( retry. as_ref( ) . unwrap( ) . payee. pubkey, path. last( ) . unwrap( ) . pubkey, "Retry payee node_id should match last hop in path" ) ;
1214
- assert!( error_code. is_some( ) , "expected error_code.is_some() = true" ) ;
1215
- assert!( error_data. is_some( ) , "expected error_data.is_some() = true" ) ;
1216
- match network_update {
1217
- & Some ( NetworkUpdate :: ChannelUpdateMessage { ref msg } ) if !$chan_closed => {
1218
- assert_eq!( msg. contents. short_channel_id, $scid) ;
1219
- assert_eq!( msg. contents. flags & 2 , 0 ) ;
1220
- } ,
1221
- & Some ( NetworkUpdate :: ChannelClosed { short_channel_id, is_permanent } ) if $chan_closed => {
1222
- assert_eq!( short_channel_id, $scid) ;
1223
- assert!( is_permanent) ;
1224
- } ,
1225
- Some ( _) => panic!( "Unexpected update type" ) ,
1226
- None => panic!( "Expected update" ) ,
1227
- }
1228
- } ,
1229
- _ => panic!( "Unexpected event" ) ,
1230
- }
1241
+ expect_payment_failed_conditions!( $node, $expected_payment_hash, $rejected_by_dest,
1242
+ $crate:: ln:: functional_test_utils:: PaymentFailedConditions :: new( ) . blamed_scid( $scid) . blamed_chan_closed( $chan_closed) ) ;
1231
1243
}
1232
1244
}
1233
1245
1234
1246
#[ cfg( test) ]
1235
1247
macro_rules! expect_payment_failed {
1236
1248
( $node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $( , $expected_error_code: expr, $expected_error_data: expr) * ) => {
1249
+ let mut conditions = $crate:: ln:: functional_test_utils:: PaymentFailedConditions :: new( ) ;
1250
+ $(
1251
+ conditions = conditions. expected_htlc_error_data( $expected_error_code, & $expected_error_data) ;
1252
+ ) *
1253
+ expect_payment_failed_conditions!( $node, $expected_payment_hash, $rejected_by_dest, conditions) ;
1254
+ } ;
1255
+ }
1256
+
1257
+ #[ cfg( test) ]
1258
+ macro_rules! expect_payment_failed_conditions {
1259
+ ( $node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $conditions: expr) => {
1237
1260
let events = $node. node. get_and_clear_pending_events( ) ;
1238
1261
assert_eq!( events. len( ) , 1 ) ;
1239
1262
match events[ 0 ] {
1240
- Event :: PaymentPathFailed { ref payment_hash, rejected_by_dest, network_update : _ , ref error_code, ref error_data, ref path, ref retry, .. } => {
1263
+ Event :: PaymentPathFailed { ref payment_hash, rejected_by_dest, ref error_code, ref error_data, ref path, ref retry, ref network_update , .. } => {
1241
1264
assert_eq!( * payment_hash, $expected_payment_hash, "unexpected payment_hash" ) ;
1242
1265
assert_eq!( rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value" ) ;
1243
1266
assert!( retry. is_some( ) , "expected retry.is_some()" ) ;
1244
1267
assert_eq!( retry. as_ref( ) . unwrap( ) . final_value_msat, path. last( ) . unwrap( ) . fee_msat, "Retry amount should match last hop in path" ) ;
1245
1268
assert_eq!( retry. as_ref( ) . unwrap( ) . payee. pubkey, path. last( ) . unwrap( ) . pubkey, "Retry payee node_id should match last hop in path" ) ;
1269
+
1246
1270
assert!( error_code. is_some( ) , "expected error_code.is_some() = true" ) ;
1247
1271
assert!( error_data. is_some( ) , "expected error_data.is_some() = true" ) ;
1248
- $(
1249
- assert_eq!( error_code. unwrap( ) , $expected_error_code, "unexpected error code" ) ;
1250
- assert_eq!( & error_data. as_ref( ) . unwrap( ) [ ..] , $expected_error_data, "unexpected error data" ) ;
1251
- ) *
1272
+ if let Some ( ( code, data) ) = $conditions. expected_htlc_error_data {
1273
+ assert_eq!( error_code. unwrap( ) , code, "unexpected error code" ) ;
1274
+ assert_eq!( & error_data. as_ref( ) . unwrap( ) [ ..] , data, "unexpected error data" ) ;
1275
+ }
1276
+
1277
+ if let Some ( chan_closed) = $conditions. expected_blamed_chan_closed {
1278
+ match network_update {
1279
+ & Some ( $crate:: routing:: network_graph:: NetworkUpdate :: ChannelUpdateMessage { ref msg } ) if !chan_closed => {
1280
+ if let Some ( scid) = $conditions. expected_blamed_scid {
1281
+ assert_eq!( msg. contents. short_channel_id, scid) ;
1282
+ }
1283
+ assert_eq!( msg. contents. flags & 2 , 0 ) ;
1284
+ } ,
1285
+ & Some ( $crate:: routing:: network_graph:: NetworkUpdate :: ChannelClosed { short_channel_id, is_permanent } ) if chan_closed => {
1286
+ if let Some ( scid) = $conditions. expected_blamed_scid {
1287
+ assert_eq!( short_channel_id, scid) ;
1288
+ }
1289
+ assert!( is_permanent) ;
1290
+ } ,
1291
+ Some ( _) => panic!( "Unexpected update type" ) ,
1292
+ None => panic!( "Expected update" ) ,
1293
+ }
1294
+ }
1252
1295
} ,
1253
1296
_ => panic!( "Unexpected event" ) ,
1254
- }
1255
- }
1297
+ } ;
1298
+ } ;
1256
1299
}
1257
1300
1258
1301
pub fn send_along_route_with_secret < ' a , ' b , ' c > ( origin_node : & Node < ' a , ' b , ' c > , route : Route , expected_paths : & [ & [ & Node < ' a , ' b , ' c > ] ] , recv_value : u64 , our_payment_hash : PaymentHash , our_payment_secret : PaymentSecret ) -> PaymentId {
0 commit comments