Skip to content

Commit 8540983

Browse files
committed
fix type migrate issues
1 parent 5bbbd2e commit 8540983

File tree

19 files changed

+1332
-1977
lines changed

19 files changed

+1332
-1977
lines changed

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 119 additions & 1078 deletions
Large diffs are not rendered by default.

crates/fiber-lib/src/fiber/graph.rs

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::fiber::history::SentNode;
2121
use crate::fiber::key::KeyPair;
2222
use crate::fiber::path::NodeHeapElement;
2323
use crate::fiber::payment::{Attempt, PaymentSession, PaymentStatus};
24-
use crate::fiber::serde_utils::EntityHex;
2524
use crate::fiber::serde_utils::{U128Hex, U64Hex};
2625
use crate::fiber::types::PaymentHopData;
2726
use crate::fiber::types::Privkey;
@@ -453,6 +452,53 @@ impl GraphChannelStat {
453452
}
454453
}
455454

455+
/// A wrapper that combines `SendPaymentData` (the user's payment request) with
456+
/// `GraphChannelStat` (runtime channel usage state for path-finding).
457+
///
458+
/// Graph methods accept `&SendPaymentState` instead of `&SendPaymentData`.
459+
/// Thanks to `Deref<Target = SendPaymentData>`, all payment data fields
460+
/// are accessible directly (e.g., `state.target_pubkey`), while the
461+
/// runtime-only `channel_stats` field lives on this wrapper.
462+
#[derive(Clone, Debug)]
463+
pub struct SendPaymentState {
464+
/// The underlying payment request data.
465+
pub request: SendPaymentData,
466+
/// Runtime channel usage statistics for path-finding (overlay on global stats).
467+
pub channel_stats: GraphChannelStat,
468+
}
469+
470+
impl SendPaymentState {
471+
/// Create a new `SendPaymentState` with default (empty) channel stats.
472+
pub fn new(request: SendPaymentData) -> Self {
473+
Self {
474+
request,
475+
channel_stats: Default::default(),
476+
}
477+
}
478+
479+
/// Create a new `SendPaymentState` with specific channel stats.
480+
pub fn with_channel_stats(request: SendPaymentData, channel_stats: GraphChannelStat) -> Self {
481+
Self {
482+
request,
483+
channel_stats,
484+
}
485+
}
486+
}
487+
488+
impl std::ops::Deref for SendPaymentState {
489+
type Target = SendPaymentData;
490+
491+
fn deref(&self) -> &Self::Target {
492+
&self.request
493+
}
494+
}
495+
496+
impl From<SendPaymentData> for SendPaymentState {
497+
fn from(request: SendPaymentData) -> Self {
498+
Self::new(request)
499+
}
500+
}
501+
456502
#[derive(Clone, Debug)]
457503
pub struct NetworkGraph<S> {
458504
// Whether to always process gossip messages for our own channels.
@@ -517,27 +563,8 @@ pub enum PathFindError {
517563
Other(String),
518564
}
519565

520-
/// A router hop information for a payment, a paymenter router is an array of RouterHop,
521-
/// a router hop generally implies hop `target` will receive `amount_received` with `channel_outpoint` of channel.
522-
/// Improper hop hint may make payment fail, for example the specified channel do not have enough capacity.
523-
#[serde_as]
524-
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
525-
pub struct RouterHop {
526-
/// The node that is sending the TLC to the next node.
527-
pub(crate) target: Pubkey,
528-
/// The channel of this hop used to receive TLC
529-
#[serde_as(as = "EntityHex")]
530-
pub(crate) channel_outpoint: OutPoint,
531-
/// The amount that the source node will transfer to the target node.
532-
/// We have already added up all the fees along the path, so this amount can be used directly for the TLC.
533-
#[serde_as(as = "U128Hex")]
534-
pub(crate) amount_received: u128,
535-
/// The expiry for the TLC that the source node sends to the target node.
536-
/// We have already added up all the expiry deltas along the path,
537-
/// the only thing missing is current time. So the expiry is the current time plus the expiry delta.
538-
#[serde_as(as = "U64Hex")]
539-
pub(crate) incoming_tlc_expiry: u64,
540-
}
566+
// RouterHop is defined in fiber-types and re-exported here for backward compatibility
567+
pub use fiber_types::RouterHop;
541568

542569
#[derive(Debug)]
543570
struct ResolvedRoute {
@@ -1245,7 +1272,7 @@ where
12451272
amount: u128,
12461273
amount_low_bound: Option<u128>,
12471274
max_fee_amount: Option<u128>,
1248-
payment_data: &SendPaymentData,
1275+
payment_data: &SendPaymentState,
12491276
) -> Result<Vec<PaymentHopData>, PathFindError> {
12501277
info!(
12511278
"entered build_route: amount: {}, amount_low_bound: {:?}, max_fee_amount: {:?}",
@@ -1325,7 +1352,7 @@ where
13251352
amount: u128,
13261353
amount_low_bound: Option<u128>,
13271354
max_fee_amount: Option<u128>,
1328-
payment_data: &SendPaymentData,
1355+
payment_data: &SendPaymentState,
13291356
) -> Result<ResolvedRoute, PathFindError> {
13301357
if !payment_data.router.is_empty() {
13311358
// If a router is explicitly provided, use it.
@@ -1383,7 +1410,7 @@ where
13831410
source: Pubkey,
13841411
final_amount: u128,
13851412
max_fee_amount: Option<u128>,
1386-
payment_data: &SendPaymentData,
1413+
payment_data: &SendPaymentState,
13871414
) -> Result<ResolvedRoute, PathFindError> {
13881415
let target = payment_data.target_pubkey;
13891416
let max_fee_amount = max_fee_amount.or(payment_data.max_fee_amount);
@@ -1598,7 +1625,7 @@ where
15981625
source: Pubkey,
15991626
amount: u128,
16001627
max_fee_amount: Option<u128>,
1601-
payment_data: &SendPaymentData,
1628+
payment_data: &SendPaymentState,
16021629
) -> Result<Vec<RouterHop>, PathFindError> {
16031630
#[cfg(any(feature = "metrics", test))]
16041631
{
@@ -1623,7 +1650,7 @@ where
16231650

16241651
pub fn find_path_max_capacity(
16251652
&self,
1626-
payment_data: &SendPaymentData,
1653+
payment_data: &SendPaymentState,
16271654
) -> Result<u128, PathFindError> {
16281655
#[cfg(any(feature = "metrics", test))]
16291656
{
@@ -1657,7 +1684,7 @@ where
16571684
amount: u128,
16581685
amount_low_bound: u128,
16591686
max_fee_amount: Option<u128>,
1660-
payment_data: &SendPaymentData,
1687+
payment_data: &SendPaymentState,
16611688
) -> Result<(Vec<RouterHop>, u128), PathFindError> {
16621689
debug!(
16631690
"find_path_in_range (max capacity) for amount: {} low_bound: {} max_fee_amount: {:?}",
@@ -1741,7 +1768,7 @@ where
17411768
source: Pubkey,
17421769
lower_bound: u128,
17431770
upper_bound: u128,
1744-
payment_data: &SendPaymentData,
1771+
payment_data: &SendPaymentState,
17451772
) -> (Vec<RouterHop>, u128) {
17461773
if let Ok(res) = self.probe_max_capacity_for_route(source, upper_bound, route, payment_data)
17471774
{
@@ -1772,7 +1799,7 @@ where
17721799
source: Pubkey,
17731800
amount: u128,
17741801
route: &[RouterHop],
1775-
payment_data: &SendPaymentData,
1802+
payment_data: &SendPaymentState,
17761803
) -> Result<std::vec::Vec<RouterHop>, PathFindError> {
17771804
self.inner_find_path(
17781805
source,
@@ -1794,7 +1821,7 @@ where
17941821
&self,
17951822
route: &Vec<RouterHop>,
17961823
amount: u128,
1797-
payment_data: &SendPaymentData,
1824+
payment_data: &SendPaymentState,
17981825
trampoline_payload: Option<Vec<u8>>,
17991826
final_hop_expiry_delta_override: Option<u64>,
18001827
) -> Vec<PaymentHopData> {

crates/fiber-lib/src/fiber/network.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,6 +3372,7 @@ where
33723372
tlc_expiry_delta,
33733373
tlc_fee_proportional_millionths
33743374
.unwrap_or(self.tlc_fee_proportional_millionths),
3375+
now_timestamp_as_millis_u64(),
33753376
),
33763377
public_channel_info: public.then_some(PublicChannelInfo::new()),
33773378
is_one_way: one_way,
@@ -3464,6 +3465,7 @@ where
34643465
tlc_expiry_delta.unwrap_or(self.tlc_expiry_delta),
34653466
tlc_fee_proportional_millionths
34663467
.unwrap_or(self.tlc_fee_proportional_millionths),
3468+
now_timestamp_as_millis_u64(),
34673469
),
34683470
public_channel_info: open_channel
34693471
.is_public()

crates/fiber-lib/src/fiber/payment.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::fiber::fee::{calculate_fee_with_base, calculate_tlc_forward_fee};
1212
use crate::fiber::gossip::GossipMessageStore;
1313
use crate::fiber::graph::{
1414
GraphChannelStat, NetworkGraph, NetworkGraphStateStore, PathFindError, RouterHop,
15+
SendPaymentState,
1516
};
1617
use crate::fiber::hash_algorithm::HashAlgorithm;
1718
use crate::fiber::network::{
@@ -84,8 +85,6 @@ pub struct SendPaymentData {
8485
pub trampoline_hops: Option<Vec<Pubkey>>,
8586
#[serde(default)]
8687
pub trampoline_context: Option<TrampolineContext>,
87-
#[serde(skip)]
88-
pub channel_stats: GraphChannelStat,
8988
}
9089

9190
#[derive(Clone, Debug, Default)]
@@ -110,7 +109,6 @@ pub struct SendPaymentDataBuilder {
110109
dry_run: bool,
111110
trampoline_hops: Option<Vec<Pubkey>>,
112111
trampoline_context: Option<TrampolineContext>,
113-
channel_stats: GraphChannelStat,
114112
}
115113

116114
impl SendPaymentDataBuilder {
@@ -226,11 +224,6 @@ impl SendPaymentDataBuilder {
226224
self
227225
}
228226

229-
pub fn channel_stats(mut self, channel_stats: GraphChannelStat) -> Self {
230-
self.channel_stats = channel_stats;
231-
self
232-
}
233-
234227
fn validate(&self, target_pubkey: Pubkey, amount: u128) -> Result<(), String> {
235228
if amount == 0 {
236229
return Err("amount must be greater than 0".to_string());
@@ -383,7 +376,6 @@ impl SendPaymentDataBuilder {
383376
allow_mpp: self.allow_mpp,
384377
dry_run: self.dry_run,
385378
trampoline_hops: self.trampoline_hops,
386-
channel_stats: self.channel_stats,
387379
trampoline_context: self.trampoline_context,
388380
})
389381
}
@@ -546,7 +538,6 @@ impl SendPaymentData {
546538
.allow_mpp(allow_mpp)
547539
.dry_run(command.dry_run)
548540
.trampoline_hops(command.trampoline_hops)
549-
.channel_stats(Default::default())
550541
.build()
551542
}
552543

@@ -1200,7 +1191,7 @@ where
12001191
amount: u128,
12011192
amount_low_bound: Option<u128>,
12021193
max_fee_amount: Option<u128>,
1203-
request: SendPaymentData,
1194+
request: SendPaymentState,
12041195
) -> Result<Vec<PaymentHopData>, PathFindError> {
12051196
let network_graph = self.network_graph.clone();
12061197
tokio::task::spawn_blocking(move || {
@@ -1218,7 +1209,7 @@ where
12181209
amount: u128,
12191210
amount_low_bound: Option<u128>,
12201211
max_fee_amount: Option<u128>,
1221-
request: SendPaymentData,
1212+
request: SendPaymentState,
12221213
) -> Result<Vec<PaymentHopData>, PathFindError> {
12231214
let network_graph = self.network_graph.clone();
12241215
let graph = network_graph.read().await;
@@ -1412,18 +1403,22 @@ where
14121403
graph.channel_stats()
14131404
};
14141405

1415-
session.request.channel_stats = GraphChannelStat::new(Some(channel_stats));
1406+
let channel_stats = GraphChannelStat::new(Some(channel_stats));
1407+
let payment_state = SendPaymentState::with_channel_stats(
1408+
session.request.clone(),
1409+
channel_stats,
1410+
);
14161411
#[cfg(not(target_arch = "wasm32"))]
14171412
let hops = self
1418-
.build_route_in_spawn_task(amount, None, max_fee, session.request.clone())
1413+
.build_route_in_spawn_task(amount, None, max_fee, payment_state)
14191414
.await
14201415
.map_err(|e| {
14211416
Error::BuildPaymentRouteError(format!("Failed to build route, {}", e))
14221417
})?;
14231418

14241419
#[cfg(target_arch = "wasm32")]
14251420
let hops = self
1426-
.build_route(amount, None, max_fee, session.request.clone())
1421+
.build_route(amount, None, max_fee, payment_state)
14271422
.await
14281423
.map_err(|e| {
14291424
Error::BuildPaymentRouteError(format!("Failed to build route, {}", e))
@@ -1455,7 +1450,7 @@ where
14551450
return Err(Error::SendPaymentError(error));
14561451
}
14571452

1458-
session.request.channel_stats = GraphChannelStat::new(Some(channel_stats));
1453+
let mut channel_stats = GraphChannelStat::new(Some(channel_stats));
14591454
let amount_low_bound = Some(1);
14601455
let mut attempt_id = session.attempts_count() as u64;
14611456
let mut target_amount = remain_amount;
@@ -1465,7 +1460,10 @@ where
14651460
if session.max_parts() > 1 && !is_self_pay {
14661461
let path_max = {
14671462
let graph = self.network_graph.read().await;
1468-
graph.find_path_max_capacity(&session.request)?
1463+
graph.find_path_max_capacity(&SendPaymentState::with_channel_stats(
1464+
session.request.clone(),
1465+
channel_stats.clone(),
1466+
))?
14691467
};
14701468
if path_max * (session.max_parts() as u128) < remain_amount {
14711469
let error = "Failed to build enough routes for MPP payment".to_string();
@@ -1486,13 +1484,17 @@ where
14861484
session.max_parts(),
14871485
max_fee,
14881486
);
1487+
let payment_state = SendPaymentState::with_channel_stats(
1488+
session.request.clone(),
1489+
channel_stats.clone(),
1490+
);
14891491
#[cfg(not(target_arch = "wasm32"))]
14901492
let build_route_result = self
14911493
.build_route_in_spawn_task(
14921494
target_amount,
14931495
amount_low_bound,
14941496
max_fee,
1495-
session.request.clone(),
1497+
payment_state.clone(),
14961498
)
14971499
.await;
14981500
#[cfg(target_arch = "wasm32")]
@@ -1501,7 +1503,7 @@ where
15011503
target_amount,
15021504
amount_low_bound,
15031505
max_fee,
1504-
session.request.clone(),
1506+
payment_state,
15051507
)
15061508
.await;
15071509

@@ -1554,7 +1556,7 @@ where
15541556
if let Some(sent_node) =
15551557
graph.get_channel_sent_node(channel_outpoint, *from)
15561558
{
1557-
session.request.channel_stats.add_channel(
1559+
channel_stats.add_channel(
15581560
channel_outpoint,
15591561
sent_node,
15601562
*amount,

0 commit comments

Comments
 (0)