Skip to content

Commit

Permalink
fixup! fix(tests): rename amount_received_msat to amount_msat
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Jul 29, 2024
1 parent 86cf51b commit 5dea9a2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
11 changes: 6 additions & 5 deletions barq-common/src/algorithms/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pub mod direct;
pub mod probabilistic;

use crate::strategy::StrategyKind;

use super::strategy::Strategy;

pub fn get_algorithm(name: &str) -> Option<Box<dyn Strategy>> {
match name {
"direct" => Some(Box::new(direct::Direct::new())),
pub fn get_algorithm(strategy: &StrategyKind) -> Option<Box<dyn Strategy>> {
match strategy {
StrategyKind::Direct => Some(Box::new(direct::Direct::new())),
#[allow(clippy::box_default)]
"probabilistic" => Some(Box::new(probabilistic::LDKRoutingStrategy::default())),
_ => None,
StrategyKind::Probabilistic => Some(Box::new(probabilistic::LDKRoutingStrategy::default())),
}
}
10 changes: 5 additions & 5 deletions barq-common/src/algorithms/probabilistic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ where
/// required for LDK routing.
fn can_apply(&self, input: &RouteInput) -> Result<bool> {
if input.graph.has_p2p_info() {
Ok(true)
} else {
Err(anyhow::anyhow!(
"The network graph does not have peer-to-peer information required for LDK routing"
))
return Ok(true);
}
log::warn!(
"The network graph does not have peer-to-peer information required for LDK routing"
);
Ok(false)
}

fn route(&self, input: &RouteInput) -> Result<RouteOutput> {
Expand Down
32 changes: 10 additions & 22 deletions barq-common/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::algorithms::direct::Direct;
// FIXME: this should be a builder pattern.
use crate::algorithms::get_algorithm;
use crate::graph::NetworkGraph;

Expand Down Expand Up @@ -76,10 +76,7 @@ pub struct RouteInput {
/// The network graph used for routing
pub graph: Box<dyn NetworkGraph>,
/// Strategy to use for routing
///
/// Note: This field is optional. If not provided, the router will select
/// the best strategy based on the input and context.
pub strategy: Option<String>,
pub strategy: StrategyKind,
}

/// Represents the output of a routing strategy
Expand Down Expand Up @@ -112,8 +109,9 @@ impl Default for Router {
/// - [`crate::algorithms::direct::Direct`]
fn default() -> Self {
// SAFETY: We can safely unwrap here because we know that the algorithm exists
let direct = get_algorithm("direct").expect("Failed to get direct algorithm");
let strategies = vec![direct];
let direct = get_algorithm(&StrategyKind::Direct).unwrap();
let probabilistic = get_algorithm(&StrategyKind::Probabilistic).unwrap();
let strategies = vec![direct, probabilistic];

Router { strategies }
}
Expand All @@ -128,29 +126,19 @@ impl Router {
}

/// Execute the routing process using the best strategy based on input
///
/// If the user specifies a strategy in the input, the router will use that
/// strategy. Otherwise, it will select the best strategy based on the input
/// and context.
pub fn execute(&self, input: &RouteInput) -> Result<RouteOutput> {
// Either the user specifies a strategy or we select the best one
let best_strategy = match input.strategy.clone() {
Some(strategy) => get_algorithm(&strategy)
.ok_or_else(|| anyhow::anyhow!("Failed to get strategy: {}", strategy))?,
None => self
.select_best_strategy(input)?
.ok_or_else(|| anyhow::anyhow!("No strategy found for the given input"))?,
};

let best_strategy = get_algorithm(&input.strategy)
.ok_or_else(|| anyhow::anyhow!("No strategy found for the given input"))?;
best_strategy.route(input)
}

/// Select the best strategy based on input
fn select_best_strategy(&self, _input: &RouteInput) -> Result<Option<Box<dyn Strategy>>> {
// FIXME: we di not overdesign, would be cool to have this functionality tho
fn select_best_strategy(&self, _input: &RouteInput) -> anyhow::Result<Option<StrategyKind>> {
// TODO: Implement logic to select the best strategy based on input
// and whether the strategy can be applied to the input

// For now, we will just use the direct strategy
Ok(Some(Box::new(Direct::new())))
Ok(Some(StrategyKind::Direct))
}
}
2 changes: 1 addition & 1 deletion barq-plugin/src/methods/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn barq_pay(
amount_msat: b11.amount_msat,
cltv: b11.min_final_cltv_expiry,
graph: network_graph,
strategy: request.strategy,
strategy: request.strategy().map_err(|e| error!("{e}"))?,
};

// Execute the routing process
Expand Down
2 changes: 1 addition & 1 deletion barq-plugin/src/methods/route_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn barq_route_info(plugin: &mut Plugin<State>, request: Value) -> Result<Val
amount_msat: request.amount_msat,
cltv: request.cltv,
graph: network_graph,
strategy: request.strategy,
strategy: request.strategy().map_err(|e| error!("{e}"))?,
};

let output = router.execute(&input);
Expand Down

0 comments on commit 5dea9a2

Please sign in to comment.