diff --git a/Cargo.lock b/Cargo.lock index 5ad0711..35b87a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2708,7 +2708,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "mining-cli" -version = "1.1.6" +version = "1.1.7" dependencies = [ "aes-gcm", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 63e8199..0d41cb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mining-cli" -version = "1.1.6" +version = "1.1.7" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/external_api/contracts/events.rs b/src/external_api/contracts/events.rs index be78aee..1b7823f 100644 --- a/src/external_api/contracts/events.rs +++ b/src/external_api/contracts/events.rs @@ -5,7 +5,10 @@ use intmax2_zkp::{ }; use log::info; -use crate::{external_api::contracts::utils::get_latest_block_number, utils::retry::with_retry}; +use crate::{ + external_api::contracts::utils::{get_block, get_latest_block_number}, + utils::retry::with_retry, +}; use super::{ error::BlockchainError, @@ -159,7 +162,6 @@ pub async fn get_latest_deposit_timestamp(sender: Address) -> Result let int1 = get_int1_contract().await?; let mut to_block = get_latest_block_number().await?; - let mut latest_event_block_number = None; let int1_deployed_block = crate::utils::config::Settings::load() .unwrap() .blockchain @@ -180,17 +182,15 @@ pub async fn get_latest_deposit_timestamp(sender: Address) -> Result .into_iter() .map(|(_, meta)| meta.block_number.as_u64()) .max(); - if max_block_number.is_some() { - latest_event_block_number = max_block_number; - break; + if let Some(max_block_number) = max_block_number { + let block = get_block(max_block_number).await?; + return Ok(Some(block.unwrap().timestamp.as_u64())); } to_block = to_block.saturating_sub(EVENT_BLOCK_RANGE); if to_block < int1_deployed_block { - break; + return Ok(None); } } - - Ok(latest_event_block_number) } pub async fn get_latest_withdrawal_timestamp( @@ -200,7 +200,6 @@ pub async fn get_latest_withdrawal_timestamp( let int1 = get_int1_contract().await?; let mut to_block = get_latest_block_number().await?; - let mut latest_event_block_number = None; let int1_deployed_block = crate::utils::config::Settings::load() .unwrap() .blockchain @@ -211,7 +210,7 @@ pub async fn get_latest_withdrawal_timestamp( .from_block(to_block.saturating_sub(EVENT_BLOCK_RANGE)) .to_block(to_block) .address(int1.address().into()) - .topic2(recipient) + .topic1(recipient) .query_with_meta() .await }) @@ -221,16 +220,15 @@ pub async fn get_latest_withdrawal_timestamp( .into_iter() .map(|(_, meta)| meta.block_number.as_u64()) .max(); - if max_block_number.is_some() { - latest_event_block_number = max_block_number; - break; + if let Some(max_block_number) = max_block_number { + let block = get_block(max_block_number).await?; + return Ok(Some(block.unwrap().timestamp.as_u64())); } to_block = to_block.saturating_sub(EVENT_BLOCK_RANGE); if to_block < int1_deployed_block { - break; + return Ok(None); } } - Ok(latest_event_block_number) } #[cfg(test)] diff --git a/src/external_api/intmax/availability.rs b/src/external_api/intmax/availability.rs index 2c55808..39380f7 100644 --- a/src/external_api/intmax/availability.rs +++ b/src/external_api/intmax/availability.rs @@ -1,7 +1,10 @@ use log::info; use serde::{Deserialize, Serialize}; -use crate::utils::{config::Settings, retry::with_retry}; +use crate::{ + external_api::intmax::header::VersionHeader as _, + utils::{config::Settings, retry::with_retry}, +}; use super::error::{IntmaxError, IntmaxErrorResponse}; @@ -24,11 +27,14 @@ pub async fn get_availability() -> Result Result Result { info!("Getting gas price"); let settings = Settings::load().unwrap(); - let response = with_retry(|| async { reqwest::get(settings.api.gas_server_url.clone()).await }) - .await - .map_err(|_| { - IntmaxError::NetworkError("failed to request circulation server".to_string()) - })?; + let response = with_retry(|| async { + reqwest::Client::new() + .get(settings.api.gas_server_url.clone()) + .with_version_header() + .send() + .await + }) + .await + .map_err(|_| IntmaxError::NetworkError("failed to request circulation server".to_string()))?; let response_json: GasPriceResponse = response.json().await.map_err(|e| { IntmaxError::SerializeError(format!("failed to parse response: {}", e.to_string())) })?; diff --git a/src/external_api/intmax/gnark.rs b/src/external_api/intmax/gnark.rs index 060bfb2..079553e 100644 --- a/src/external_api/intmax/gnark.rs +++ b/src/external_api/intmax/gnark.rs @@ -8,10 +8,13 @@ use plonky2::{ }; use serde::{Deserialize, Serialize}; -use crate::utils::{ - config::Settings, - retry::with_retry, - time::{sleep_for, sleep_until}, +use crate::{ + external_api::intmax::header::VersionHeader as _, + utils::{ + config::Settings, + retry::with_retry, + time::{sleep_for, sleep_until}, + }, }; use super::error::{IntmaxError, IntmaxErrorResponse}; @@ -88,6 +91,7 @@ pub async fn gnark_start_prove( reqwest::Client::new() .post(format!("{}/start-proof", base_url)) .json(&input) + .with_version_header() .send() .await }) @@ -110,6 +114,7 @@ pub async fn gnark_get_proof( let response = with_retry(|| async { reqwest::Client::new() .get(format!("{}/get-proof?jobId={}", base_url, job_id)) + .with_version_header() .send() .await }) diff --git a/src/external_api/intmax/header.rs b/src/external_api/intmax/header.rs new file mode 100644 index 0000000..1b9ebe5 --- /dev/null +++ b/src/external_api/intmax/header.rs @@ -0,0 +1,11 @@ +use reqwest::RequestBuilder; + +pub trait VersionHeader { + fn with_version_header(self) -> Self; +} + +impl VersionHeader for RequestBuilder { + fn with_version_header(self) -> Self { + self.header("X-Version", env!("CARGO_PKG_VERSION")) + } +} diff --git a/src/external_api/intmax/mod.rs b/src/external_api/intmax/mod.rs index 9abad65..bf9c54d 100644 --- a/src/external_api/intmax/mod.rs +++ b/src/external_api/intmax/mod.rs @@ -4,3 +4,4 @@ pub mod error; pub mod gas_estimation; pub mod gnark; pub mod withdrawal; +pub mod header; \ No newline at end of file diff --git a/src/external_api/intmax/withdrawal.rs b/src/external_api/intmax/withdrawal.rs index b5e6e7e..420344c 100644 --- a/src/external_api/intmax/withdrawal.rs +++ b/src/external_api/intmax/withdrawal.rs @@ -10,7 +10,10 @@ use serde_json::Value; use std::str::FromStr; use crate::{ - external_api::contracts::int1::{get_int1_contract_with_signer, int_1}, + external_api::{ + contracts::int1::{get_int1_contract_with_signer, int_1}, + intmax::header::VersionHeader as _, + }, utils::{ config::Settings, network::{get_network, Network}, @@ -70,6 +73,7 @@ async fn start_withdrawal( reqwest::Client::new() .post(url.clone()) .json(&input) + .with_version_header() .send() .await }) @@ -98,9 +102,15 @@ async fn query_withdrawal(withdrawal_id: &str) -> Result anyhow::Result<()> { + let version = env!("CARGO_PKG_VERSION"); + println!("Mining CLI {}", version); if is_interactive { // select network if in interactive mode let network = select_network()?; @@ -79,7 +81,11 @@ async fn set_up(is_interactive: bool) -> anyhow::Result<()> { create_config_files()?; // check loading test - Settings::load()?; + let settings = Settings::load()?; + log::info!( + "Settings loaded: {}", + serde_json::to_string_pretty(&settings)? + ); check_avaliability().await?; Ok(()) diff --git a/src/services/mining/deterministic_sleep.rs b/src/services/mining/deterministic_sleep.rs index b12427f..4f870d1 100644 --- a/src/services/mining/deterministic_sleep.rs +++ b/src/services/mining/deterministic_sleep.rs @@ -40,9 +40,14 @@ pub async fn sleep_before_withdrawal(deposit_address: Address) -> anyhow::Result } async fn sleep_if_needed(target_time: u64, is_deposit: bool) { + log::info!( + "sleep_if_needed: target_time: {}, is_deposit: {}", + target_time, + is_deposit + ); let now = chrono::Utc::now().timestamp() as u64; if now >= target_time { - info!("No need to sleep"); + info!("No need to sleep: now={}, target_time={}", now, target_time); return; // no need to sleep } let sleep_from_now = target_time - now; diff --git a/src/services/mod.rs b/src/services/mod.rs index c7fbc13..d69e733 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -29,7 +29,6 @@ pub async fn mining_loop( mining_unit: U256, mining_times: u64, ) -> anyhow::Result<()> { - check_avaliability().await?; let key = Key::new(withdrawal_private_key, 0); print_log(format!( "Processing mining for deposit address {:?}", @@ -44,6 +43,7 @@ pub async fn mining_loop( ) .await?; loop { + check_avaliability().await?; let assets_status = state.sync_and_fetch_assets(&key).await?; let is_qualified = !get_circulation(key.deposit_address).await?.is_excluded; let will_deposit = assets_status.effective_deposit_times() < mining_times as usize @@ -90,10 +90,10 @@ pub async fn mining_loop( } pub async fn exit_loop(state: &mut State, withdrawal_private_key: H256) -> anyhow::Result<()> { - check_avaliability().await?; let key = Key::new(withdrawal_private_key, 0); print_log(format!("Exit for deposit address{:?}", key.deposit_address)); loop { + check_avaliability().await?; let assets_status = state.sync_and_fetch_assets(&key).await?; if assets_status.pending_indices.is_empty() && assets_status.rejected_indices.is_empty() diff --git a/src/utils/config.rs b/src/utils/config.rs index 8742772..9c353c3 100644 --- a/src/utils/config.rs +++ b/src/utils/config.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use config::{Config, File}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::utils::network::get_network; @@ -17,7 +17,7 @@ fn config_path(network: Network) -> PathBuf { .join(format!("config.{}.toml", network)) } -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Settings { pub api: Api, pub blockchain: Blockchain, @@ -25,7 +25,7 @@ pub struct Settings { pub env: Env, } -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Api { pub availability_server_url: String, pub withdrawal_gnark_prover_url: String, @@ -40,7 +40,7 @@ pub struct Api { pub withdrawal_server_url: String, } -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Blockchain { pub chain_id: u64, pub int1_address: String, @@ -51,14 +51,14 @@ pub struct Blockchain { pub single_claim_gas: u64, } -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Env { pub default_max_gas_price: String, pub default_mining_unit: String, pub default_mining_times: u64, } -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Service { pub repository_url: String, pub mining_min_cooldown_in_sec: u64,