Skip to content

Commit

Permalink
making router more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 19, 2024
1 parent 42c6842 commit b2e3b10
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ todo = "warn"
let_unit_value = "allow"

[workspace.dependencies]

displaydoc = { version = "^0.2.0", default-features = false }
cvm = { path = "./crates/cvm", default-features = false }
blackbox_rs = { path = "./node/blackbox_rs", default-features = false }
cvm-route = { path = "./crates/cvm-route", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions mantis/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cw-mantis-order = { workspace = true, features = [
"std",
] }

displaydoc = { workspace = true }
cw-cvm-outpost = { workspace = true, features = ["std"] }
cw-cvm-executor = { workspace = true, features = ["std"] }

Expand Down
13 changes: 13 additions & 0 deletions mantis/node/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cosmwasm_std::Uint128;

#[derive(Debug, displaydoc::Display)]
pub enum MantisError {
/// `{order_id}` Matching order not found
MatchingOrderNotFound { order_id: Uint128 },
/// `{order_id}` Cow fill badly found because `{reason}`
CowFillBadlyFound { order_id: Uint128, reason: String },
/// Blackbox error: `{reason}`
BlackboxError { reason: String },
}

impl std::error::Error for MantisError {}
1 change: 1 addition & 0 deletions mantis/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(let_chains)]
pub mod error;
pub mod mantis;
pub mod prelude;
pub mod solver;
39 changes: 26 additions & 13 deletions mantis/node/src/mantis/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cvm_runtime::{
use cw_cvm_outpost::msg::CvmGlt;
use cw_mantis_order::{CrossChainPart, OrderItem, SolutionSubMsg};

use crate::solver::router::shortest_path;
use crate::{error::MantisError, solver::router::shortest_path};

use super::{
cosmos::client::Tip,
Expand Down Expand Up @@ -88,9 +88,9 @@ pub async fn get_routes(
input: IntentBankInput,
cvm_glt: &GetConfigResponse,
salt: &[u8],
) -> Vec<CvmInstruction> {
) -> Result<Vec<CvmInstruction>, MantisError> {
if route_provider == "shortest_path" {
return shortest_path::route(cvm_glt, input, salt);
Ok(shortest_path::route(cvm_glt, input, salt))
} else {
let blackbox: Client = Client::new(route_provider);
let mut route = blackbox
Expand All @@ -102,13 +102,22 @@ pub async fn get_routes(
input.out_asset_id.to_string().as_str().into(),
)
.await
.expect("route found")
.map_err(|x| MantisError::BlackboxError {
reason: x.to_string(),
})?
.into_inner()
.pop()
.expect("at least one route");
.ok_or(MantisError::BlackboxError {
reason: "no routes".to_string(),
})?;

log::info!("route: {:?}", route);
build_instructions(input.order_accounts, &route.next[0], cvm_glt, salt)
Ok(build_instructions(
input.order_accounts,
&route.next[0],
cvm_glt,
salt,
))
}
}

Expand Down Expand Up @@ -180,9 +189,6 @@ pub async fn solve<Decider: Get<bool>>(
let cows_per_pair = find_cows(&active_orders);
let mut msgs = vec![];

// this we do just to avoid one pair to fail all others, really need to handle all errors gracefully or run solver(thread/process) per pair (i am for second)
let cows_per_pair = cows_per_pair.into_iter().take(1).collect::<Vec<_>>();

for pair_solution in cows_per_pair {
let salt = super::cosmos::cvm::calculate_salt(signing_key, tip, pair_solution.ab.clone());
let cvm_program = if let Some(ref cvm_glt) = cvm_glt {
Expand All @@ -195,6 +201,13 @@ pub async fn solve<Decider: Get<bool>>(
)
.await;

if cvm_program.is_err() {
log::error!("cvm_program error: {:?}", cvm_program);
continue;
}

let cvm_program = cvm_program.expect("qed");

Some(cvm_program)
} else {
None
Expand Down Expand Up @@ -230,21 +243,21 @@ async fn intent_banks_to_cvm_program(
cvm_glt: &cw_cvm_outpost::msg::GetConfigResponse,
router_api: &str,
salt: &Vec<u8>,
) -> CvmProgram {
) -> Result<CvmProgram, MantisError> {
let intents = IntentBankInput::find_intent_amount(
pair_solution.cows.as_ref(),
all_orders,
pair_solution.optimal_price,
cvm_glt,
pair_solution.ab.clone(),
);
)?;

log::info!(target:"mantis::solver::", "intents: {:?}", intents);

let mut instructions = vec![];

for intent in intents.into_iter().filter(|x| x.in_asset_amount.0.gt(&0)) {
let mut cvm_routes = get_routes(router_api, intent, cvm_glt, salt.as_ref()).await;
let mut cvm_routes = get_routes(router_api, intent, cvm_glt, salt.as_ref()).await?;
instructions.append(&mut cvm_routes);
}

Expand All @@ -254,5 +267,5 @@ async fn intent_banks_to_cvm_program(
tag: salt.to_vec(),
instructions,
};
cvm_program
Ok(cvm_program)
}
14 changes: 10 additions & 4 deletions mantis/node/src/mantis/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mantis_cw::{DenomPair, OrderCoinPair};
use num_rational::Ratio;

use crate::{
error::MantisError,
prelude::*,
solver::cows::{orderbook::OrderList, solution::Solution},
};
Expand Down Expand Up @@ -43,7 +44,7 @@ impl IntentBankInput {
optimal_ratio: Ratio<u64>,
cvm_glt: &GetConfigResponse,
pair: DenomPair,
) -> Vec<IntentBankInput> {
) -> Result<Vec<IntentBankInput>, MantisError> {
// native calculations
let mut pair = OrderCoinPair::zero(pair.a, pair.b);
let mut a_to_b = Vec::new();
Expand All @@ -55,11 +56,16 @@ impl IntentBankInput {
let mut order = orders
.iter()
.find(|x| x.order_id == cow.order_id)
.expect("order")
.ok_or(MantisError::MatchingOrderNotFound {
order_id: cow.order_id,
})?
.clone();
order
.fill(cow.cow_out_amount, optimal_ratio.into())
.expect("off chain");
.map_err(|x| MantisError::CowFillBadlyFound {
order_id: cow.order_id,
reason: x.to_string(),
})?;
pair.add(&order.given);

if order.given.denom == pair.a.denom {
Expand Down Expand Up @@ -106,7 +112,7 @@ impl IntentBankInput {
IntentBankInput::new(b_asset, pair.b.amount.into(), a_asset, a_received.collect());
intents.push(intent);
}
intents
Ok(intents)
}
}

Expand Down

0 comments on commit b2e3b10

Please sign in to comment.