From c3a4ac7b9cab7278c9a1f27d69a99d37c45cfb74 Mon Sep 17 00:00:00 2001 From: yukang Date: Sun, 24 Nov 2024 11:35:43 +0800 Subject: [PATCH] code refactor --- src/fiber/history.rs | 20 ++++++++++---------- src/fiber/tests/history.rs | 10 +--------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/fiber/history.rs b/src/fiber/history.rs index 42932de4..cb16fc2d 100644 --- a/src/fiber/history.rs +++ b/src/fiber/history.rs @@ -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, @@ -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); } @@ -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); diff --git a/src/fiber/tests/history.rs b/src/fiber/tests/history.rs index 28d64934..4c10808a 100644 --- a/src/fiber/tests/history.rs +++ b/src/fiber/tests/history.rs @@ -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; @@ -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());