From ea50688d3462290e559c3422ddb09b6e501cc924 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Sat, 20 Apr 2024 02:50:43 +0100 Subject: [PATCH] clean --- contracts/cosmwasm/order/src/lib.rs | 12 ++++++++++++ mantis/node/src/bin/mantis.rs | 30 ++++------------------------- mantis/node/src/mantis/indexer.rs | 9 +++++++++ 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/contracts/cosmwasm/order/src/lib.rs b/contracts/cosmwasm/order/src/lib.rs index 8afe6f4..fe1987f 100644 --- a/contracts/cosmwasm/order/src/lib.rs +++ b/contracts/cosmwasm/order/src/lib.rs @@ -120,6 +120,17 @@ impl OrderContract<'_> { Ok(Response::default().add_event(order_created)) } + #[msg(query)] + pub fn has_stale(&self, ctx: QueryCtx) -> StdResult { + Ok(self + .orders + .range(ctx.deps.storage, None, None, Order::Ascending) + .any(|x| { + let (_id, order) = x.as_ref().unwrap(); + order.msg.timeout < ctx.env.block.height + })) + } + /// Hook/crank for cleanup. /// Caller receives small reward for doing so. /// This is to prevent spamming of old orders. @@ -143,6 +154,7 @@ impl OrderContract<'_> { let (_id, order) = x.as_ref().unwrap(); order.msg.timeout < ctx.env.block.height }) + .take(30) // in batches .collect(); let mut msgs = vec![]; for order in orders? { diff --git a/mantis/node/src/bin/mantis.rs b/mantis/node/src/bin/mantis.rs index b591572..a212f3f 100644 --- a/mantis/node/src/bin/mantis.rs +++ b/mantis/node/src/bin/mantis.rs @@ -13,7 +13,7 @@ use mantis_node::{ args::*, autopilot, blackbox, cosmos::{client::*, cosmwasm::to_exec_signed, *}, - indexer::{get_active_orders, get_cvm_glt}, + indexer::{get_active_orders, get_cvm_glt, has_stale_orders}, simulate, solve::PairSolution, }, @@ -80,7 +80,9 @@ async fn solve_orders(solver_args: &SolverArgs) { get_latest_block_and_account_by_key(&args.rpc_centauri, &args.grpc_centauri, &signer) .await; - if rand::random::() > 150 { + if rand::random::() > 200 + || has_stale_orders(&args.order_contract, &mut wasm_read_client, &tip).await + { autopilot::cleanup( &mut write_client, &mut cosmos_query_client, @@ -92,30 +94,6 @@ async fn solve_orders(solver_args: &SolverArgs) { ) .await; tip.account.sequence += 1; - } else { - let stale_orders = mantis_node::mantis::indexer::get_stale_orders( - &args.order_contract, - &mut wasm_read_client, - &tip, - ) - .await; - if stale_orders.len() > 0 { - // need to have function to check count, cause this timeouts - if stale_orders.len() > 0 { - log::warn!(target: "mantis::autopilot", "timedouted orders"); - } - autopilot::cleanup( - &mut write_client, - &mut cosmos_query_client, - args.order_contract.clone(), - &signer, - &cosmos_chain_info, - &tip, - gas, - ) - .await; - tip.account.sequence += 1; - } } let mut tip = diff --git a/mantis/node/src/mantis/indexer.rs b/mantis/node/src/mantis/indexer.rs index 7fb492e..66a1009 100644 --- a/mantis/node/src/mantis/indexer.rs +++ b/mantis/node/src/mantis/indexer.rs @@ -31,6 +31,15 @@ pub async fn get_stale_orders( .collect::>() } +pub async fn has_stale_orders( + order_contract: &String, + cosmos_query_client: &mut CosmWasmReadClient, + tip: &Tip, +) -> bool { + let query = cw_mantis_order::QueryMsg::HasStale {}; + smart_query::<_, bool>(order_contract, query, cosmos_query_client).await +} + pub async fn get_cvm_glt( contract: &String, cosmos_query_client: &mut CosmWasmReadClient,