diff --git a/barq-common/src/strategy.rs b/barq-common/src/strategy.rs index 087d6ec..83ee8bf 100644 --- a/barq-common/src/strategy.rs +++ b/barq-common/src/strategy.rs @@ -1,7 +1,7 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; -use crate::graph::NetworkGraph; +use crate::{algorithms::get_algorithm, graph::NetworkGraph}; /// The `Strategy` trait defines an interface for routing strategies used within /// Barq. @@ -72,6 +72,20 @@ pub struct Router { // FIXME: Should we have a database here to store routing information? } +impl Default for Router { + /// Create a new `Router` instance with the default strategies + /// + /// Default strategies: + /// - [`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]; + + Router { strategies } + } +} + impl Router { /// Create a new `Router` instance with the provided strategies pub fn new(strategies: Vec>) -> Self { diff --git a/barq-plugin/src/methods/pay.rs b/barq-plugin/src/methods/pay.rs index e37ac10..1c9675a 100644 --- a/barq-plugin/src/methods/pay.rs +++ b/barq-plugin/src/methods/pay.rs @@ -4,10 +4,7 @@ use serde_json as json; use clightningrpc_gossip_map::GossipMap; use clightningrpc_plugin::{error, errors::PluginError, plugin::Plugin}; -use barq_common::{ - algorithms::get_algorithm, - strategy::{RouteInput, Router}, -}; +use barq_common::strategy::{RouteInput, Router}; use crate::{methods::utils::graph::build_network_graph, plugin::State}; @@ -84,15 +81,7 @@ pub fn barq_pay( let request: BarqPayRequest = json::from_value(request).map_err(|err| error!("{err}"))?; let state = &plugin.state; - - // Get the routing strategies - // 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 probabilistic = get_algorithm("probabilistic").expect("Failed to get - // probabilistic algorithm"); - let strategies = vec![direct]; - // Create a new router with the strategies - let router = Router::new(strategies); + let router = Router::default(); let b11: Bolt11 = state .call( diff --git a/barq-plugin/src/methods/route_info.rs b/barq-plugin/src/methods/route_info.rs index f75d145..168fe69 100644 --- a/barq-plugin/src/methods/route_info.rs +++ b/barq-plugin/src/methods/route_info.rs @@ -5,10 +5,7 @@ use serde_json::Value; use clightningrpc_gossip_map::GossipMap; use clightningrpc_plugin::{error, errors::PluginError, plugin::Plugin}; -use barq_common::{ - algorithms::get_algorithm, - strategy::{RouteHop, RouteInput, Router}, -}; +use barq_common::strategy::{RouteHop, RouteInput, Router}; use crate::{ methods::{pay::NodeInfo, utils::graph::build_network_graph}, @@ -37,16 +34,9 @@ pub struct BarqRouteInfoResponse { pub fn barq_route_info(plugin: &mut Plugin, request: Value) -> Result { log::info!("barqrouteinfo called with request: {}", request); let request: BarqRouteInfoRequest = json::from_value(request).map_err(|err| error!("{err}"))?; - let state = &plugin.state; - // Get the routing strategies - // 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 probabilistic = get_algorithm("probabilistic").expect("Failed to get - // probabilistic algorithm"); - let strategies = vec![direct]; - // Create a new router with the strategies - let router = Router::new(strategies); + let state = &plugin.state; + let router = Router::default(); let node_info: NodeInfo = state .call("getinfo", serde_json::json!({}))