Skip to content

Commit

Permalink
add router in send payment result for better debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jan 9, 2025
1 parent f34882a commit 9098d2d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/fiber/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,8 @@ impl From<PaymentSession> for SendPaymentResponse {
created_at: session.created_at,
last_updated_at: session.last_updated_at,
fee,
#[cfg(debug_assertions)]
router: session.route,
}
}
}
2 changes: 2 additions & 0 deletions src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub struct SendPaymentResponse {
pub last_updated_at: u64,
pub failed_error: Option<String>,
pub fee: u128,
#[cfg(debug_assertions)]
pub router: SessionRoute,
}

/// What kind of local information should be broadcasted to the network.
Expand Down
7 changes: 4 additions & 3 deletions src/fiber/tests/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ async fn test_send_payment_max_value_in_flight_in_first_hop() {

// if we build a nother channel with higher max_value_in_flight
// we can send payment with amount 100000000 + 1 with this new channel
let (_channel_id, _funding_tx) = {
let (_channel_id, funding_tx) = {
establish_channel_between_nodes(
&mut node_0,
&mut node_1,
Expand All @@ -1528,9 +1528,10 @@ async fn test_send_payment_max_value_in_flight_in_first_hop() {
.send_payment_keysend(&node_1, 100000000 + 1, false)
.await
.unwrap();
eprintln!("res: {:?}", res);
assert_eq!(res.fee, 0);

let payment_hash = res.payment_hash;
node_0.wait_until_success(payment_hash).await;
node_0
.expect_payment_used_channel(&payment_hash, &funding_tx)
.await;
}
16 changes: 16 additions & 0 deletions src/fiber/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,22 @@ impl NetworkNode {
.unwrap()
}

pub async fn expect_payment_used_channel(
&self,
payment_hash: &Hash256,
funding_tx: &TransactionView,
) {
let payment_result = self.get_payment_result(*payment_hash).await;
let used_channes = payment_result
.router
.nodes
.iter()
.map(|r| r.channel_outpoint.clone())
.collect::<Vec<_>>();
let funding_channel_outpoint = OutPoint::new(funding_tx.hash(), 0);
assert!(used_channes.contains(&funding_channel_outpoint));
}

pub async fn wait_until_success(&self, payment_hash: Hash256) {
loop {
let status = self.get_payment_status(payment_hash).await;
Expand Down
48 changes: 48 additions & 0 deletions src/rpc/payment.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::fiber::graph::SessionRoute as InnerSessionRoute;
use crate::fiber::serde_utils::EntityHex;
use crate::fiber::{
channel::ChannelActorStateStore,
graph::PaymentSessionStatus as InnerPaymentSessionStatus,
Expand All @@ -8,6 +10,7 @@ use crate::fiber::{
};
use crate::{handle_actor_call, log_and_error};
use ckb_jsonrpc_types::Script;
use ckb_types::packed::OutPoint;
use jsonrpsee::{
core::async_trait,
proc_macros::rpc,
Expand Down Expand Up @@ -48,6 +51,43 @@ impl From<InnerPaymentSessionStatus> for PaymentSessionStatus {
}
}

/// The node and channel information in a payment route hop
#[cfg(debug_assertions)]
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SessionRouteNode {
/// the public key of the node
pub pubkey: Pubkey,
/// the amount for this hop
pub amount: u128,
/// the channel outpoint for this hop
#[serde_as(as = "EntityHex")]
pub channel_outpoint: OutPoint,
}

/// The router information for a payment route, used for debugging
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct SessionRoute {
/// the nodes in the route
pub nodes: Vec<SessionRouteNode>,
}

impl From<InnerSessionRoute> for SessionRoute {
fn from(route: InnerSessionRoute) -> Self {
SessionRoute {
nodes: route
.nodes
.into_iter()
.map(|node| SessionRouteNode {
pubkey: node.pubkey,
amount: node.amount,
channel_outpoint: node.channel_outpoint,
})
.collect(),
}
}
}

#[serde_as]
#[derive(Serialize, Deserialize, Clone)]
pub struct GetPaymentCommandResult {
Expand All @@ -66,6 +106,10 @@ pub struct GetPaymentCommandResult {
/// fee paid for the payment
#[serde_as(as = "U128Hex")]
pub fee: u128,

#[cfg(debug_assertions)]
/// The route information for the payment
router: SessionRoute,
}

#[serde_as]
Expand Down Expand Up @@ -220,6 +264,8 @@ where
last_updated_at: response.last_updated_at,
failed_error: response.failed_error,
fee: response.fee,
#[cfg(debug_assertions)]
router: response.router.into(),
})
}

Expand All @@ -240,6 +286,8 @@ where
created_at: response.created_at,
failed_error: response.failed_error,
fee: response.fee,
#[cfg(debug_assertions)]
router: response.router.into(),
})
}
}

0 comments on commit 9098d2d

Please sign in to comment.