Skip to content

Commit

Permalink
bad
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 20, 2024
1 parent e2d3cf0 commit e602c3e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
24 changes: 19 additions & 5 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cosmrs::{tx::Msg, Gas};
use cvm_runtime::shared::CvmProgram;
use cw_mantis_order::{CrossChainPart, OrderItem, OrderSolution, Ratio, SolutionSubMsg};
use mantis_node::{
error::MantisError,
mantis::{
args::*,
autopilot, blackbox,
Expand Down Expand Up @@ -103,6 +104,7 @@ async fn solve_orders(solver_args: &SolverArgs) {
.await;
let all_orders = get_active_orders(&args.order_contract, &mut wasm_read_client, &tip).await;
if !all_orders.is_empty() {
log::debug!(target: "mantis::solver::", "there are {:?} to try solve", all_orders.len());
let main_chain = CosmosChainInfo {
rpc: args.rpc_centauri.clone(),
chain_id: args.main_chain_id.clone(),
Expand Down Expand Up @@ -210,9 +212,21 @@ async fn get_data_and_solve(
)
.await;

let mut errors = vec![];
for msg in msgs {
send_solution(msg, tip, signing_key, order_contract, rpc, gas).await;
tip.account.sequence += 1;
match send_solution(msg.clone(), tip, signing_key, order_contract, rpc, gas).await {
Ok(ok) => {
log::info!("solution sent for msg {:?}", msg);
tip.account.sequence += 1;
}
Err(err) => {
log::error!("solution failed: {:?}", err);
errors.push(err);
}
};
}
if errors.len() > 0 {
panic!("errors: {:?}", errors)
}
}

Expand All @@ -223,7 +237,7 @@ async fn send_solution(
order_contract: &String,
rpc: &CosmosChainInfo,
gas: Gas,
) {
) -> Result<(), MantisError> {
log::info!("========================= settle =========================");
let auth_info = simulate_and_set_fee(signing_key, &tip.account, gas).await;
let msg = to_exec_signed(signing_key, order_contract.clone(), msg);
Expand All @@ -234,14 +248,14 @@ async fn send_solution(
signing_key,
tip,
)
.await;
.await?;
match &result.tx_result.code {
cosmrs::tendermint::abci::Code::Err(err) => {
log::error!("solution result: {:?}", result);
panic!("Error: {:?}", err)
}
cosmrs::tendermint::abci::Code::Ok => {
log::trace!("ok: {:?}", result);
}
}
Ok(())
}
4 changes: 4 additions & 0 deletions mantis/node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub enum MantisError {
CowFillBadlyFound { order_id: Uint128, reason: String },
/// Blackbox error: `{reason}`
BlackboxError { reason: String },
/// `{source}` Failed to broadcast tx
FailedToBroadcastTx { source: String },
/// `{source}` Failed to execute tx
FailedToExecuteTx { source: String },
}

impl std::error::Error for MantisError {}
5 changes: 3 additions & 2 deletions mantis/node/src/mantis/autopilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn cleanup(
tip: &Tip,
gas: Gas,
) {
log::info!("========================= cleanup =========================");
log::info!(target: "mantis::autopilot", " cleanup of old orders");
let auth_info = simulate_and_set_fee(signing_key, &tip.account, gas).await;
let msg = cw_mantis_order::ExecMsg::Timeout {
orders: vec![],
Expand All @@ -33,7 +33,8 @@ pub async fn cleanup(
signing_key,
tip,
)
.await;
.await
.expect("cleaned");
match &result.tx_result.code {
cosmrs::tendermint::abci::Code::Err(err) => {
log::error!("clean result: {:?}", result);
Expand Down
27 changes: 17 additions & 10 deletions mantis/node/src/mantis/cosmos/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{error::MantisError, prelude::*};

use cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount;
use cosmrs::{
Expand Down Expand Up @@ -132,17 +132,24 @@ pub async fn sign_and_tx_tendermint(
rpc: &str,
sign_doc: SignDoc,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
) -> cosmrs::rpc::endpoint::broadcast::tx_commit::Response {
) -> Result<cosmrs::rpc::endpoint::broadcast::tx_commit::Response, MantisError> {
let rpc_client: cosmrs::rpc::HttpClient = cosmrs::rpc::HttpClient::new(rpc).unwrap();
let tx_raw = sign_doc.sign(signing_key).expect("signed");
let result = tx_raw
.broadcast_commit(&rpc_client)
.await
.expect("broadcasted");

let result = tx_raw.broadcast_commit(&rpc_client).await.map_err(|x| {
MantisError::FailedToBroadcastTx {
source: x.to_string(),
}
})?;

if result.check_tx.code.is_err() || result.tx_result.code.is_err() {
log::error!("tx error: {:?}", result);
return Err(MantisError::FailedToExecuteTx {
source: format!("{:?}", result),
});
}
log::trace!("result: {:?}", result);
assert!(!result.check_tx.code.is_err(), "err");
assert!(!result.tx_result.code.is_err(), "err");
result
Ok(result)
}

#[derive(Debug, Clone)]
Expand All @@ -157,7 +164,7 @@ pub async fn tx_broadcast_single_signed_msg(
rpc: &CosmosChainInfo,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
tip: &Tip,
) -> cosmrs::rpc::endpoint::broadcast::tx_commit::Response {
) -> Result<cosmrs::rpc::endpoint::broadcast::tx_commit::Response, MantisError> {
let tx_body = tx::Body::new(vec![msg], "", Height::try_from(tip.timeout(100)).unwrap());

let sign_doc = SignDoc::new(
Expand Down
5 changes: 3 additions & 2 deletions mantis/node/src/mantis/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ pub async fn simulate_order(
signing_key,
&tip,
)
.await;
.await
.expect("simulated");

println!("simulated tx {:?}", result.height)
log::trace!("simulated tx {:?}", result.height)
}

0 comments on commit e602c3e

Please sign in to comment.