Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 24, 2024
1 parent 1e6b3b8 commit c3a4ac7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/fiber/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ fn current_time() -> u128 {
.as_millis()
}

pub(crate) fn output_direction(node1: Pubkey, node2: Pubkey) -> (Direction, Direction) {
if node1 < node2 {
(Direction::Forward, Direction::Backward)
} else {
(Direction::Backward, Direction::Forward)
}
}

impl InternalResult {
pub fn add(
&mut self,
Expand All @@ -73,11 +81,7 @@ impl InternalResult {
time,
amount,
};
let direction = if node_1 < node_2 {
Direction::Forward
} else {
Direction::Backward
};
let (direction, _) = output_direction(node_1, node_2);
self.pairs.insert((channel, direction), pair);
}

Expand Down Expand Up @@ -412,11 +416,7 @@ where
) -> f64 {
let mut success_amount = 0;
let mut fail_amount = capacity;
let direction = if from < target {
Direction::Forward
} else {
Direction::Backward
};
let (direction, _) = output_direction(from, target);
if let Some(result) = self.get_result(channel, direction) {
if result.fail_time != 0 {
fail_amount = self.cannot_send(result.fail_amount, result.fail_time, capacity);
Expand Down
10 changes: 1 addition & 9 deletions src/fiber/tests/history.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::fiber::graph::SessionRouteNode;
use crate::fiber::history::output_direction;
use crate::fiber::history::{Direction, DEFAULT_BIMODAL_DECAY_TIME};
use crate::fiber::history::{InternalPairResult, InternalResult};
use crate::fiber::history::{PaymentHistory, TimedResult};
use crate::fiber::tests::test_utils::{generate_pubkey, MemoryStore};
use crate::fiber::types::Pubkey;
use crate::store::Store;
use ckb_types::packed::OutPoint;
use molecule::prelude::Entity;
Expand All @@ -24,14 +24,6 @@ fn gen_rand_outpoint() -> OutPoint {
OutPoint::from_slice(&rand_slice).unwrap()
}

fn output_direction(node1: Pubkey, node2: Pubkey) -> (Direction, Direction) {
if node1 < node2 {
(Direction::Forward, Direction::Backward)
} else {
(Direction::Backward, Direction::Forward)
}
}

#[test]
fn test_history() {
let mut history = PaymentHistory::new(generate_pubkey().into(), None, MemoryStore::default());
Expand Down

0 comments on commit c3a4ac7

Please sign in to comment.