Skip to content

Commit

Permalink
feat(barq-common): implement Default for Router
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Jul 8, 2024
1 parent d421032 commit 828a06c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
16 changes: 15 additions & 1 deletion barq-common/src/strategy.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Box<dyn Strategy>>) -> Self {
Expand Down
15 changes: 2 additions & 13 deletions barq-plugin/src/methods/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(
Expand Down
16 changes: 3 additions & 13 deletions barq-plugin/src/methods/route_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -37,16 +34,9 @@ pub struct BarqRouteInfoResponse {
pub fn barq_route_info(plugin: &mut Plugin<State>, request: Value) -> Result<Value, PluginError> {
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!({}))
Expand Down

0 comments on commit 828a06c

Please sign in to comment.