@@ -1337,12 +1337,14 @@ impl OutboundPayments {
13371337		} 
13381338		let  mut  has_ok = false ; 
13391339		let  mut  has_err = false ; 
1340- 		let  mut  pending_amt_unsent  = 0 ; 
1340+ 		let  mut  has_unsent  = false ; 
13411341		let  mut  total_ok_fees_msat = 0 ; 
1342+ 		let  mut  total_ok_amt_sent_msat = 0 ; 
13421343		for  ( res,  path)  in  results. iter ( ) . zip ( route. paths . iter ( ) )  { 
13431344			if  res. is_ok ( )  { 
13441345				has_ok = true ; 
13451346				total_ok_fees_msat += path. fee_msat ( ) ; 
1347+ 				total_ok_amt_sent_msat += path. final_value_msat ( ) ; 
13461348			} 
13471349			if  res. is_err ( )  {  has_err = true ;  } 
13481350			if  let  & Err ( APIError :: MonitorUpdateInProgress )  = res { 
@@ -1351,23 +1353,28 @@ impl OutboundPayments {
13511353				has_err = true ; 
13521354				has_ok = true ; 
13531355				total_ok_fees_msat += path. fee_msat ( ) ; 
1356+ 				total_ok_amt_sent_msat += path. final_value_msat ( ) ; 
13541357			}  else  if  res. is_err ( )  { 
1355- 				pending_amt_unsent += path . final_value_msat ( ) ; 
1358+ 				has_unsent =  true ; 
13561359			} 
13571360		} 
13581361		if  has_err && has_ok { 
13591362			Err ( PaymentSendFailure :: PartialFailure  { 
13601363				results, 
13611364				payment_id, 
1362- 				failed_paths_retry :  if  pending_amt_unsent !=  0  { 
1365+ 				failed_paths_retry :  if  has_unsent  { 
13631366					if  let  Some ( route_params)  = & route. route_params  { 
13641367						let  mut  route_params = route_params. clone ( ) ; 
13651368						// We calculate the leftover fee budget we're allowed to spend by 
13661369						// subtracting the used fee from the total fee budget. 
13671370						route_params. max_total_routing_fee_msat  = route_params
13681371							. max_total_routing_fee_msat . map ( |m| m. saturating_sub ( total_ok_fees_msat) ) ; 
1369- 						route_params. final_value_msat  = pending_amt_unsent; 
13701372
1373+ 						// We calculate the remaining target amount by subtracting the succeded 
1374+ 						// path values. 
1375+ 						let  final_value_msat = core:: cmp:: min ( route_params. final_value_msat ,  route. get_total_amount ( ) ) ; 
1376+ 						route_params. final_value_msat  = final_value_msat
1377+ 							. saturating_sub ( total_ok_amt_sent_msat) ; 
13711378						Some ( route_params) 
13721379					}  else  {  None  } 
13731380				}  else  {  None  } , 
0 commit comments