From e602c3e4f07621f6b26fa7db68cfb969aa6e6c27 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Sat, 20 Apr 2024 02:16:29 +0100 Subject: [PATCH] bad --- mantis/node/src/bin/mantis.rs | 24 +++++++++++++++++----- mantis/node/src/error.rs | 4 ++++ mantis/node/src/mantis/autopilot.rs | 5 +++-- mantis/node/src/mantis/cosmos/client.rs | 27 ++++++++++++++++--------- mantis/node/src/mantis/simulate.rs | 5 +++-- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/mantis/node/src/bin/mantis.rs b/mantis/node/src/bin/mantis.rs index 34d454f..4f1f453 100644 --- a/mantis/node/src/bin/mantis.rs +++ b/mantis/node/src/bin/mantis.rs @@ -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, @@ -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(), @@ -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) } } @@ -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); @@ -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(()) } diff --git a/mantis/node/src/error.rs b/mantis/node/src/error.rs index 7a0d641..24013db 100644 --- a/mantis/node/src/error.rs +++ b/mantis/node/src/error.rs @@ -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 {} diff --git a/mantis/node/src/mantis/autopilot.rs b/mantis/node/src/mantis/autopilot.rs index 24c1ae6..b528177 100644 --- a/mantis/node/src/mantis/autopilot.rs +++ b/mantis/node/src/mantis/autopilot.rs @@ -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![], @@ -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); diff --git a/mantis/node/src/mantis/cosmos/client.rs b/mantis/node/src/mantis/cosmos/client.rs index 8052b51..bf9c0bd 100644 --- a/mantis/node/src/mantis/cosmos/client.rs +++ b/mantis/node/src/mantis/cosmos/client.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::{error::MantisError, prelude::*}; use cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount; use cosmrs::{ @@ -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 { 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)] @@ -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 { let tx_body = tx::Body::new(vec![msg], "", Height::try_from(tip.timeout(100)).unwrap()); let sign_doc = SignDoc::new( diff --git a/mantis/node/src/mantis/simulate.rs b/mantis/node/src/mantis/simulate.rs index b1244a5..c1bfb32 100644 --- a/mantis/node/src/mantis/simulate.rs +++ b/mantis/node/src/mantis/simulate.rs @@ -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) }