@@ -336,10 +336,11 @@ pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b,
336336}
337337
338338#[ macro_export]
339+ /// Gets an RAA and CS which were sent in response to a commitment update
339340macro_rules! get_revoke_commit_msgs {
340341 ( $node: expr, $node_id: expr) => {
341342 {
342- use util:: events:: MessageSendEvent ;
343+ use $crate :: util:: events:: MessageSendEvent ;
343344 let events = $node. node. get_and_clear_pending_msg_events( ) ;
344345 assert_eq!( events. len( ) , 2 ) ;
345346 ( match events[ 0 ] {
@@ -400,8 +401,8 @@ macro_rules! get_event {
400401 }
401402}
402403
403- #[ cfg( test) ]
404404#[ macro_export]
405+ /// Gets an UpdateHTLCs MessageSendEvent
405406macro_rules! get_htlc_update_msgs {
406407 ( $node: expr, $node_id: expr) => {
407408 {
@@ -933,6 +934,9 @@ impl SendEvent {
933934 }
934935}
935936
937+ #[ macro_export]
938+ /// Performs the "commitment signed dance" - the series of message exchanges which occur after a
939+ /// commitment update.
936940macro_rules! commitment_signed_dance {
937941 ( $node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */ ) => {
938942 {
@@ -999,7 +1003,7 @@ macro_rules! commitment_signed_dance {
9991003 {
10001004 commitment_signed_dance!( $node_a, $node_b, $commitment_signed, $fail_backwards, true ) ;
10011005 if $fail_backwards {
1002- expect_pending_htlcs_forwardable!( $node_a) ;
1006+ $crate :: expect_pending_htlcs_forwardable!( $node_a) ;
10031007 check_added_monitors!( $node_a, 1 ) ;
10041008
10051009 let channel_state = $node_a. node. channel_state. lock( ) . unwrap( ) ;
@@ -1057,6 +1061,8 @@ macro_rules! get_route_and_payment_hash {
10571061 } }
10581062}
10591063
1064+ #[ macro_export]
1065+ /// Clears (and ignores) a PendingHTLCsForwardable event
10601066macro_rules! expect_pending_htlcs_forwardable_ignore {
10611067 ( $node: expr) => { {
10621068 let events = $node. node. get_and_clear_pending_events( ) ;
@@ -1068,9 +1074,11 @@ macro_rules! expect_pending_htlcs_forwardable_ignore {
10681074 } }
10691075}
10701076
1077+ #[ macro_export]
1078+ /// Handles a PendingHTLCsForwardable event
10711079macro_rules! expect_pending_htlcs_forwardable {
10721080 ( $node: expr) => { {
1073- expect_pending_htlcs_forwardable_ignore!( $node) ;
1081+ $crate :: expect_pending_htlcs_forwardable_ignore!( $node) ;
10741082 $node. node. process_pending_htlc_forwards( ) ;
10751083
10761084 // Ensure process_pending_htlc_forwards is idempotent.
@@ -1199,60 +1207,95 @@ macro_rules! expect_payment_forwarded {
11991207 }
12001208}
12011209
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+
12021238#[ cfg( test) ]
12031239macro_rules! expect_payment_failed_with_update {
12041240 ( $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) ) ;
12311243 }
12321244}
12331245
12341246#[ cfg( test) ]
12351247macro_rules! expect_payment_failed {
12361248 ( $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) => {
12371260 let events = $node. node. get_and_clear_pending_events( ) ;
12381261 assert_eq!( events. len( ) , 1 ) ;
12391262 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 , .. } => {
12411264 assert_eq!( * payment_hash, $expected_payment_hash, "unexpected payment_hash" ) ;
12421265 assert_eq!( rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value" ) ;
12431266 assert!( retry. is_some( ) , "expected retry.is_some()" ) ;
12441267 assert_eq!( retry. as_ref( ) . unwrap( ) . final_value_msat, path. last( ) . unwrap( ) . fee_msat, "Retry amount should match last hop in path" ) ;
12451268 assert_eq!( retry. as_ref( ) . unwrap( ) . payee. pubkey, path. last( ) . unwrap( ) . pubkey, "Retry payee node_id should match last hop in path" ) ;
1269+
12461270 assert!( error_code. is_some( ) , "expected error_code.is_some() = true" ) ;
12471271 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+ }
12521295 } ,
12531296 _ => panic!( "Unexpected event" ) ,
1254- }
1255- }
1297+ } ;
1298+ } ;
12561299}
12571300
12581301pub 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