Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 13, 2024
1 parent 53b4d38 commit 8cfb17f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 43 deletions.
6 changes: 2 additions & 4 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ where
// note we can not use get_received_tlc_by_id here, because this new add_tlc may be
// trying to add a duplicate tlc, so we use tlc count to make sure no new tlc was added
// and only send RemoveTlc message to peer if the TLC is not in our state
assert!(tlc_count == state.tlcs.len());
error!("Error handling AddTlc message: {:?}", e);
let error_detail = self.get_tlc_detail_error(state, &e).await;
assert!(tlc_count == state.tlcs.len());
if state.get_received_tlc(tlc_id).is_none() {
self.network
.send_message(NetworkActorMessage::new_command(
Expand Down Expand Up @@ -893,9 +893,7 @@ where
command: ChannelCommand::RemoveTlc(
RemoveTlcCommand {
id: added_tlc_id,
reason: RemoveTlcReason::RemoveTlcFail(RemoveTlcFail {
onion_packet: vec![],
}),
reason: RemoveTlcReason::RemoveTlcFail(res),
},
port,
),
Expand Down
8 changes: 2 additions & 6 deletions src/fiber/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,11 @@ where
next_hop: None,
incoming_cltv_height: 0,
});
loop {
nodes_visited += 1;
let Some(cur_hop) = nodes_heap.pop() else {
break;
};

while let Some(cur_hop) = nodes_heap.pop() {
if cur_hop.node_id == source {
break;
}
nodes_visited += 1;

for (from, channel_info, channel_update) in self.get_node_inbounds(cur_hop.node_id) {
edges_expanded += 1;
Expand Down
43 changes: 22 additions & 21 deletions src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ where

// TODO: we should check the OnionPacket is valid or not, only the current node can decrypt it.
NetworkActorCommand::SendPaymentOnionPacket(command, reply) => {
self.handle_onion_packet_send_command(state, command, reply)
self.handle_send_onion_packet_command(state, command, reply)
.await;
}
NetworkActorCommand::PeelPaymentOnionPacket(onion_packet, payment_hash, reply) => {
Expand Down Expand Up @@ -1965,7 +1965,7 @@ where
}
}

async fn handle_onion_packet_send_command(
async fn handle_send_onion_packet_command(
&self,
state: &mut NetworkActorState<S>,
command: SendOnionPacketCommand,
Expand Down Expand Up @@ -2138,17 +2138,19 @@ where
mut payment_session: PaymentSession,
) -> Result<PaymentSession, Error> {
let payment_data = payment_session.request.clone();
let payment_hash = payment_data.payment_hash;
let mut error = None;
while payment_session.can_retry() {
payment_session.retried_times += 1;
let graph = self.network_graph.read().await;
let hops_infos = match graph.build_route(payment_data.clone()) {
let hops_infos = match self
.network_graph
.read()
.await
.build_route(payment_data.clone())
{
Err(e) => {
error!("Failed to build route: {:?}", e);
error = Some(format!(
"Failed to build route: {:?}",
payment_data.payment_hash
));
error = Some(format!("Failed to build route: {:?}", payment_hash));
break;
}
Ok(onion_path) => onion_path,
Expand All @@ -2173,22 +2175,21 @@ where
packet: peeled_packet.serialize(),
previous_tlc: None,
};
self.handle_onion_packet_send_command(state, command, rpc_reply)
self.handle_send_onion_packet_command(state, command, rpc_reply)
.await;
match recv.await.expect("msg recv error") {
Err(e) => {
let error_detail = e.decode().expect("decoded error");
error!("Failed to send onion packet with error: {:?}", e);
// This is the error implies we send payment request to the first hop failed
let err = format!(
"Failed to send onion packet: {:?} with error: {:?}",
payment_data.payment_hash,
error_detail.error_code_as_str()
);
error = Some(err);
// drop the graph lock so that `update_with_tcl_fail` can acquire it
drop(graph);
self.update_with_tcl_fail(&error_detail).await;
if let Some(error_detail) = e.decode() {
error!("Failed to send onion packet with error: {:?}", e);
// This is the error implies we send payment request to the first hop failed
let err = format!(
"Failed to send onion packet: {:?} with error {:?}",
payment_hash,
error_detail.error_code_as_str()
);
error = Some(err);
self.update_with_tcl_fail(&error_detail).await;
}
continue;
}
Ok(tlc_id) => {
Expand Down
11 changes: 5 additions & 6 deletions src/fiber/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,14 +1342,13 @@ impl TlcErr {
}
}

// This is the onion packet we need to encode and send back to the sender
// currently it's the raw TlcErr serialized data from the TlcErr struct
// sender should decode it and get the TlcErr, then decide what to do
// This is the onion packet we need to encode and send back to the sender,
// currently it's the raw TlcErr serialized data from the TlcErr struct,
// sender should decode it and get the TlcErr, then decide what to do with the error
// Note: this supposed to be only accessible by the sender, and it's not reliable since it
// is not placed on-chain due to the possibility of hop failure.
// is not placed on-chain due to the possibility of hop failure.
//
// FIXME: a better name like `TlcFailOnionPacket`?, since it's not only caused by removing tlc now,
// maybe we will replace it later.
// FIXME: a better name like `TlcFailOnionPacket`?, since it's not only caused by removing tlc now
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoveTlcFail {
// TODO: replace this with the real onion packet
Expand Down
8 changes: 2 additions & 6 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::fiber::{
hash_algorithm::HashAlgorithm,
network::{AcceptChannelCommand, OpenChannelCommand, SendPaymentCommand},
serde_utils::{U128Hex, U16Hex, U64Hex},
types::{Hash256, LockTime, Pubkey, RemoveTlcFail, RemoveTlcFulfill},
types::{Hash256, LockTime, Pubkey, RemoveTlcFulfill},
NetworkActorCommand, NetworkActorMessage,
};
use crate::{handle_actor_call, handle_actor_cast, log_and_error};
Expand Down Expand Up @@ -438,11 +438,7 @@ where
}
RemoveTlcReason::RemoveTlcFail { error_code: _ } => {
// TODO: maybe we should remove this PRC or move add_tlc and remove_tlc to `test` module?
crate::fiber::types::RemoveTlcReason::RemoveTlcFail(
RemoveTlcFail {
onion_packet: vec![],
},
)
unimplemented!("RemoveTlcFail is only for internal use");
}
},
},
Expand Down

0 comments on commit 8cfb17f

Please sign in to comment.