@@ -423,15 +423,6 @@ const CHECK_CLTV_EXPIRY_SANITY: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_P
423
423
#[ allow( dead_code) ]
424
424
const CHECK_CLTV_EXPIRY_SANITY_2 : u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2 * CLTV_CLAIM_BUFFER ;
425
425
426
- macro_rules! secp_call {
427
- ( $res: expr, $err: expr ) => {
428
- match $res {
429
- Ok ( key) => key,
430
- Err ( _) => return Err ( $err) ,
431
- }
432
- } ;
433
- }
434
-
435
426
/// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
436
427
pub struct ChannelDetails {
437
428
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
@@ -469,6 +460,32 @@ pub struct ChannelDetails {
469
460
pub is_live : bool ,
470
461
}
471
462
463
+ /// If a payment fails to send, it can be in one of several states. This enum is returned as the
464
+ /// Err() type describing which state the payment is in, see the description of individual enum
465
+ /// states for more.
466
+ #[ derive( Debug ) ]
467
+ pub enum PaymentSendFailure {
468
+ /// A parameter which was passed to send_payment was invalid, preventing us from attempting to
469
+ /// send the payment at all. No channel state has been changed or messages sent to peers, and
470
+ /// once you've changed the parameter at error, you can freely retry the payment in full.
471
+ ParameterError ( APIError ) ,
472
+ /// All paths which were attempted failed to send, with no channel state change taking place.
473
+ /// You can freely retry the payment in full (though you probably want to do so over different
474
+ /// paths than the ones selected).
475
+ AllFailedRetrySafe ( Vec < APIError > ) ,
476
+ /// Some paths which were attempted failed to send, though possibly not all. At least some
477
+ /// paths have irrevocably committed to the HTLC and retrying the payment in full would result
478
+ /// in over-/re-payment.
479
+ ///
480
+ /// The results here are ordered the same as the paths in the route object which was passed to
481
+ /// send_payment, and any Errs which are not APIError::MonitorUpdateFailed can be safely
482
+ /// retried.
483
+ ///
484
+ /// Any entries which contain Err(APIError::MonitorUpdateFailed) or Ok(()) MUST NOT be retried
485
+ /// as they will result in over-/re-payment.
486
+ PartialFailure ( Vec < Result < ( ) , APIError > > ) ,
487
+ }
488
+
472
489
macro_rules! handle_error {
473
490
( $self: ident, $internal: expr, $their_node_id: expr, $locked_channel_state: expr) => {
474
491
match $internal {
@@ -1184,109 +1201,154 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1184
1201
/// payment_preimage tracking (which you should already be doing as they represent "proof of
1185
1202
/// payment") and prevent double-sends yourself.
1186
1203
///
1187
- /// May generate a SendHTLCs message event on success, which should be relayed.
1204
+ /// May generate SendHTLCs message(s) event on success, which should be relayed.
1205
+ ///
1206
+ /// Each path may have a different return value, and PaymentSendValue may return a Vec with
1207
+ /// each entry matching the corresponding-index entry in the route paths.
1188
1208
///
1189
- /// Raises APIError::RoutError when invalid route or forward parameter
1190
- /// (cltv_delta, fee, node public key) is specified.
1191
- /// Raises APIError::ChannelUnavailable if the next-hop channel is not available for updates
1192
- /// (including due to previous monitor update failure or new permanent monitor update failure).
1193
- /// Raised APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
1194
- /// relevant updates.
1209
+ /// In general, a path may raise:
1210
+ /// * APIError::RouteError when an invalid route or forwarding parameter (cltv_delta, fee,
1211
+ /// node public key) is specified.
1212
+ /// * APIError::ChannelUnavailable if the next-hop channel is not available for updates
1213
+ /// (including due to previous monitor update failure or new permanent monitor update
1214
+ /// failure).
1215
+ /// * APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
1216
+ /// relevant updates.
1195
1217
///
1196
- /// In case of APIError::RouteError/APIError::ChannelUnavailable, the payment send has failed
1197
- /// and you may wish to retry via a different route immediately.
1198
- /// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1199
- /// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1200
- /// the payment via a different route unless you intend to pay twice!
1218
+ /// Note that depending on the type of the PaymentSendFailure the HTLC may have been
1219
+ /// irrevocably committed to on our end. In such a case, do NOT retry the payment with a
1220
+ /// different route unless you intend to pay twice!
1201
1221
///
1202
1222
/// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1203
1223
/// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1204
1224
/// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1205
1225
/// must not contain multiple paths as otherwise the multipath data cannot be sent.
1206
1226
/// If a payment_secret *is* provided, we assume that the invoice had the basic_mpp feature bit
1207
1227
/// set (either as required or as available).
1208
- pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , APIError > {
1209
- if route. paths . len ( ) < 1 || route . paths . len ( ) > 1 {
1210
- return Err ( APIError :: RouteError { err : "We currently don't support MPP, and we need at least one path" } ) ;
1228
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , PaymentSendFailure > {
1229
+ if route. paths . len ( ) < 1 {
1230
+ return Err ( PaymentSendFailure :: ParameterError ( APIError :: RouteError { err : "There must be at least one path to send over" } ) ) ;
1211
1231
}
1212
- if route. paths [ 0 ] . len ( ) < 1 || route . paths [ 0 ] . len ( ) > 20 {
1213
- return Err ( APIError :: RouteError { err : "Path didn't go anywhere/had bogus size" } ) ;
1232
+ if route. paths . len ( ) > 10 {
1233
+ return Err ( PaymentSendFailure :: ParameterError ( APIError :: RouteError { err : "Sending over more than 10 paths is not currently supported" } ) ) ;
1214
1234
}
1235
+ let mut total_value = 0 ;
1215
1236
let our_node_id = self . get_our_node_id ( ) ;
1216
- for ( idx, hop) in route. paths [ 0 ] . iter ( ) . enumerate ( ) {
1217
- if idx != route. paths [ 0 ] . len ( ) - 1 && hop. pubkey == our_node_id {
1218
- return Err ( APIError :: RouteError { err : "Path went through us but wasn't a simple rebalance loop to us" } ) ;
1237
+ for path in route. paths . iter ( ) {
1238
+ if path. len ( ) < 1 || path. len ( ) > 20 {
1239
+ return Err ( PaymentSendFailure :: ParameterError ( APIError :: RouteError { err : "Path didn't go anywhere/had bogus size" } ) ) ;
1240
+ }
1241
+ for ( idx, hop) in path. iter ( ) . enumerate ( ) {
1242
+ if idx != path. len ( ) - 1 && hop. pubkey == our_node_id {
1243
+ return Err ( PaymentSendFailure :: ParameterError ( APIError :: RouteError { err : "Path went through us but wasn't a simple rebalance loop to us" } ) ) ;
1244
+ }
1219
1245
}
1246
+ total_value += path. last ( ) . unwrap ( ) . fee_msat ;
1220
1247
}
1221
-
1222
- let ( session_priv, prng_seed) = self . keys_manager . get_onion_rand ( ) ;
1223
-
1224
1248
let cur_height = self . latest_block_height . load ( Ordering :: Acquire ) as u32 + 1 ;
1249
+ let mut results = Vec :: new ( ) ;
1250
+ ' path_loop: for path in route. paths . iter ( ) {
1251
+ macro_rules! check_res_push {
1252
+ ( $res: expr) => { match $res {
1253
+ Ok ( r) => r,
1254
+ Err ( e) => {
1255
+ results. push( Err ( e) ) ;
1256
+ continue ' path_loop;
1257
+ } ,
1258
+ }
1259
+ }
1260
+ }
1225
1261
1226
- let onion_keys = secp_call ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & route. paths[ 0 ] , & session_priv) ,
1227
- APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ;
1228
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route. paths [ 0 ] , payment_secret, cur_height) ?;
1229
- if onion_utils:: route_size_insane ( & onion_payloads) {
1230
- return Err ( APIError :: RouteError { err : "Route had too large size once" } ) ;
1231
- }
1232
- let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, & payment_hash) ;
1262
+ log_trace ! ( self , "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
1263
+ let ( session_priv, prng_seed) = self . keys_manager . get_onion_rand ( ) ;
1233
1264
1234
- let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1265
+ let onion_keys = check_res_push ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & path, & session_priv)
1266
+ . map_err( |_| APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ) ;
1267
+ let ( onion_payloads, htlc_msat, htlc_cltv) = check_res_push ! ( onion_utils:: build_onion_payloads( & path, total_value, payment_secret, cur_height) ) ;
1268
+ if onion_utils:: route_size_insane ( & onion_payloads) {
1269
+ check_res_push ! ( Err ( APIError :: RouteError { err: "Route had too large size once" } ) ) ;
1270
+ }
1271
+ let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, & payment_hash) ;
1235
1272
1236
- let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1237
- let err: Result < ( ) , _ > = loop {
1273
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1238
1274
1239
- let id = match channel_lock. short_to_id . get ( & route. paths [ 0 ] . first ( ) . unwrap ( ) . short_channel_id ) {
1240
- None => return Err ( APIError :: ChannelUnavailable { err : "No channel available with first hop!" } ) ,
1241
- Some ( id) => id. clone ( ) ,
1242
- } ;
1275
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1276
+ let err: Result < ( ) , _ > = loop {
1277
+ let id = match channel_lock. short_to_id . get ( & path. first ( ) . unwrap ( ) . short_channel_id ) {
1278
+ None => check_res_push ! ( Err ( APIError :: ChannelUnavailable { err: "No channel available with first hop!" } ) ) ,
1279
+ Some ( id) => id. clone ( ) ,
1280
+ } ;
1243
1281
1244
- let channel_state = channel_lock. borrow_parts ( ) ;
1245
- if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( id) {
1246
- match {
1247
- if chan. get ( ) . get_their_node_id ( ) != route. paths [ 0 ] . first ( ) . unwrap ( ) . pubkey {
1248
- return Err ( APIError :: RouteError { err : "Node ID mismatch on first hop!" } ) ;
1249
- }
1250
- if !chan. get ( ) . is_live ( ) {
1251
- return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected/pending monitor update!" } ) ;
1252
- }
1253
- break_chan_entry ! ( self , chan. get_mut( ) . send_htlc_and_commit( htlc_msat, payment_hash. clone( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1254
- path: route. paths[ 0 ] . clone( ) ,
1255
- session_priv: session_priv. clone( ) ,
1256
- first_hop_htlc_msat: htlc_msat,
1257
- } , onion_packet) , channel_state, chan)
1258
- } {
1259
- Some ( ( update_add, commitment_signed, chan_monitor) ) => {
1260
- if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1261
- maybe_break_monitor_err ! ( self , e, channel_state, chan, RAACommitmentOrder :: CommitmentFirst , false , true ) ;
1262
- // Note that MonitorUpdateFailed here indicates (per function docs)
1263
- // that we will resent the commitment update once we unfree monitor
1264
- // updating, so we have to take special care that we don't return
1265
- // something else in case we will resend later!
1266
- return Err ( APIError :: MonitorUpdateFailed ) ;
1282
+ let channel_state = channel_lock. borrow_parts ( ) ;
1283
+ if let hash_map:: Entry :: Occupied ( mut chan) = channel_state. by_id . entry ( id) {
1284
+ match {
1285
+ if chan. get ( ) . get_their_node_id ( ) != path. first ( ) . unwrap ( ) . pubkey {
1286
+ check_res_push ! ( Err ( APIError :: RouteError { err: "Node ID mismatch on first hop!" } ) ) ;
1267
1287
}
1288
+ if !chan. get ( ) . is_live ( ) {
1289
+ check_res_push ! ( Err ( APIError :: ChannelUnavailable { err: "Peer for first hop currently disconnected/pending monitor update!" } ) ) ;
1290
+ }
1291
+ break_chan_entry ! ( self , chan. get_mut( ) . send_htlc_and_commit( htlc_msat, payment_hash. clone( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1292
+ path: path. clone( ) ,
1293
+ session_priv: session_priv. clone( ) ,
1294
+ first_hop_htlc_msat: htlc_msat,
1295
+ } , onion_packet) , channel_state, chan)
1296
+ } {
1297
+ Some ( ( update_add, commitment_signed, chan_monitor) ) => {
1298
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1299
+ maybe_break_monitor_err ! ( self , e, channel_state, chan, RAACommitmentOrder :: CommitmentFirst , false , true ) ;
1300
+ // Note that MonitorUpdateFailed here indicates (per function docs)
1301
+ // that we will resent the commitment update once we unfree monitor
1302
+ // updating, so we have to take special care that we don't return
1303
+ // something else in case we will resend later!
1304
+ check_res_push ! ( Err ( APIError :: MonitorUpdateFailed ) ) ;
1305
+ }
1268
1306
1269
- channel_state. pending_msg_events . push ( events:: MessageSendEvent :: UpdateHTLCs {
1270
- node_id : route. paths [ 0 ] . first ( ) . unwrap ( ) . pubkey ,
1271
- updates : msgs:: CommitmentUpdate {
1272
- update_add_htlcs : vec ! [ update_add] ,
1273
- update_fulfill_htlcs : Vec :: new ( ) ,
1274
- update_fail_htlcs : Vec :: new ( ) ,
1275
- update_fail_malformed_htlcs : Vec :: new ( ) ,
1276
- update_fee : None ,
1277
- commitment_signed,
1278
- } ,
1279
- } ) ;
1280
- } ,
1281
- None => { } ,
1282
- }
1283
- } else { unreachable ! ( ) ; }
1284
- return Ok ( ( ) ) ;
1285
- } ;
1307
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: UpdateHTLCs {
1308
+ node_id : path. first ( ) . unwrap ( ) . pubkey ,
1309
+ updates : msgs:: CommitmentUpdate {
1310
+ update_add_htlcs : vec ! [ update_add] ,
1311
+ update_fulfill_htlcs : Vec :: new ( ) ,
1312
+ update_fail_htlcs : Vec :: new ( ) ,
1313
+ update_fail_malformed_htlcs : Vec :: new ( ) ,
1314
+ update_fee : None ,
1315
+ commitment_signed,
1316
+ } ,
1317
+ } ) ;
1318
+ } ,
1319
+ None => { } ,
1320
+ }
1321
+ } else { unreachable ! ( ) ; }
1322
+ results. push ( Ok ( ( ) ) ) ;
1323
+ continue ' path_loop;
1324
+ } ;
1286
1325
1287
- match handle_error ! ( self , err, route. paths[ 0 ] . first( ) . unwrap( ) . pubkey, channel_lock) {
1288
- Ok ( _) => unreachable ! ( ) ,
1289
- Err ( e) => { Err ( APIError :: ChannelUnavailable { err : e. err } ) }
1326
+ match handle_error ! ( self , err, path. first( ) . unwrap( ) . pubkey, channel_lock) {
1327
+ Ok ( _) => unreachable ! ( ) ,
1328
+ Err ( e) => {
1329
+ check_res_push ! ( Err ( APIError :: ChannelUnavailable { err: e. err } ) ) ;
1330
+ } ,
1331
+ }
1332
+ }
1333
+ let mut has_ok = false ;
1334
+ let mut has_err = false ;
1335
+ for res in results. iter ( ) {
1336
+ if res. is_ok ( ) { has_ok = true ; }
1337
+ if res. is_err ( ) { has_err = true ; }
1338
+ if let & Err ( APIError :: MonitorUpdateFailed ) = res {
1339
+ // MonitorUpdateFailed is inherently unsafe to retry, so we call it a
1340
+ // PartialFailure.
1341
+ has_err = true ;
1342
+ has_ok = true ;
1343
+ break ;
1344
+ }
1345
+ }
1346
+ if has_err && has_ok {
1347
+ Err ( PaymentSendFailure :: PartialFailure ( results) )
1348
+ } else if has_err {
1349
+ Err ( PaymentSendFailure :: AllFailedRetrySafe ( results. drain ( ..) . map ( |r| r. unwrap_err ( ) ) . collect ( ) ) )
1350
+ } else {
1351
+ Ok ( ( ) )
1290
1352
}
1291
1353
}
1292
1354
0 commit comments