From 1c831ac87d2437f01c9381c9c3ceb4e1eaa9c099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Thu, 7 Nov 2024 12:43:06 +0100 Subject: [PATCH] feat(c-bridge): report metrics on minutes o'clock --- bridges/centralized-ethereum/Cargo.toml | 1 + .../src/actors/watch_dog.rs | 34 +++++++++++++------ bridges/centralized-ethereum/src/config.rs | 8 ++--- witnet_centralized_ethereum_bridge.toml | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/bridges/centralized-ethereum/Cargo.toml b/bridges/centralized-ethereum/Cargo.toml index f42e852dc..e271fef85 100644 --- a/bridges/centralized-ethereum/Cargo.toml +++ b/bridges/centralized-ethereum/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] actix = { version = "0.13.0", default-features = false } async-jsonrpc-client = { git = "https://github.com/witnet/async-jsonrpc-client", features = ["tcp"], branch = "fix-tcp-leak" } +chrono = "0.4.38" ctrlc = "3.1.3" env_logger = "0.9.0" envy = "0.4" diff --git a/bridges/centralized-ethereum/src/actors/watch_dog.rs b/bridges/centralized-ethereum/src/actors/watch_dog.rs index a24666d69..6f78593c0 100644 --- a/bridges/centralized-ethereum/src/actors/watch_dog.rs +++ b/bridges/centralized-ethereum/src/actors/watch_dog.rs @@ -1,3 +1,4 @@ +use chrono::{Timelike, Utc, NaiveTime}; use crate::{ actors::dr_database::{CountDrsPerState, DrDatabase}, config::Config, @@ -33,7 +34,7 @@ pub struct WatchDog { /// WitOracle bridge contract pub eth_contract: Option>>, /// Polling period for global status - pub polling_rate_ms: u64, + pub polling_rate_minutes: u64, /// Instant at which the actor is created pub start_ts: Option, /// Eth balance upon first metric report: @@ -65,7 +66,7 @@ impl Actor for WatchDog { None, None, ctx, - Duration::from_millis(self.polling_rate_ms), + Duration::from_secs(self.polling_rate_minutes * 60), ); } } @@ -90,7 +91,7 @@ enum WatchDogStatus { impl fmt::Display for WatchDogStatus { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - WatchDogStatus::EvmBalanceLeak => write!(f, "evm-balanace-leak"), + WatchDogStatus::EvmBalanceLeak => write!(f, "evm-balance-leak"), WatchDogStatus::EvmDisconnect => write!(f, "evm-disconnect"), WatchDogStatus::EvmErrors => write!(f, "evm-errors"), WatchDogStatus::EvmSyncing => write!(f, "evm-syncing"), @@ -122,7 +123,7 @@ impl WatchDog { eth_account: config.eth_from, eth_contract: Some(eth_contract), eth_jsonrpc_url: config.eth_jsonrpc_url.clone(), - polling_rate_ms: config.watch_dog_polling_rate_ms, + polling_rate_minutes: config.watch_dog_polling_rate_minutes, start_ts: Some(Instant::now()), start_eth_balance: None, start_wit_balance: None, @@ -349,9 +350,27 @@ impl WatchDog { ctx.spawn(fut.into_actor(self).then( move |(eth_balance, wit_balance, drs_history), _act, ctx| { + let time_now = Utc::now().time(); + let period_minutes = period.as_secs_f32().div_euclid(60.0) as u32; + let time_next_minute= period_minutes * (time_now.minute().div_euclid(period_minutes) + 1); + let time_next = if time_next_minute >= 60 { + NaiveTime::from_hms_opt(time_now.hour() + 1, time_next_minute - 60, 0) + } else { + NaiveTime::from_hms_opt(time_now.hour() , time_next_minute, 0) + }; + let dur = if let Some(time_next) = time_next { + let num_nanosecs = (time_next - time_now).num_nanoseconds(); + if let Some(num_nanosecs) = num_nanosecs { + Duration::from_nanos(num_nanosecs.abs() as u64 - 1_000_000_000) + } else { + period + } + } else { + period + }; // Schedule next iteration only when finished, // as to avoid multiple tasks running in parallel - ctx.run_later(period, move |act, ctx| { + ctx.run_later(dur, move |act, ctx| { act.watch_global_status(eth_balance, wit_balance, drs_history, ctx, period); }); actix::fut::ready(()) @@ -390,11 +409,6 @@ async fn check_eth_account_balance( } } -// async fn check_eth_contract_version( -// eth_jsonrpc_url: &str, -// eth_contract_address: H160, -// ) -> - async fn check_wit_connection_status( wit_client: &Addr, ) -> Result<(), WatchDogStatus> { diff --git a/bridges/centralized-ethereum/src/config.rs b/bridges/centralized-ethereum/src/config.rs index bc895a1e8..694a62772 100644 --- a/bridges/centralized-ethereum/src/config.rs +++ b/bridges/centralized-ethereum/src/config.rs @@ -40,8 +40,8 @@ pub struct Config { /// Let the dog out? pub watch_dog_enabled: bool, /// Watch dog polling rate - #[serde(default = "default_watch_dog_polling_rate_ms")] - pub watch_dog_polling_rate_ms: u64, + #[serde(default = "default_watch_dog_polling_rate_minutes")] + pub watch_dog_polling_rate_minutes: u64, /// Minimum collateral required on data requests read from the WitnetOracle contract pub witnet_dr_min_collateral_nanowits: u64, @@ -77,8 +77,8 @@ fn default_max_batch_size() -> u16 { 256 } -fn default_watch_dog_polling_rate_ms() -> u64 { - 900_000 +fn default_watch_dog_polling_rate_minutes() -> u64 { + 15 } /// Gas limits for some methods. If missing, let the client estimate diff --git a/witnet_centralized_ethereum_bridge.toml b/witnet_centralized_ethereum_bridge.toml index 1f3909e31..bce7b8976 100644 --- a/witnet_centralized_ethereum_bridge.toml +++ b/witnet_centralized_ethereum_bridge.toml @@ -29,7 +29,7 @@ eth_witnet_oracle = "0x77703aE126B971c9946d562F41Dd47071dA00777" watch_dog_enabled = true # Polling period for checking and tracing global status -watch_dog_polling_rate_ms = 5_000 +watch_dog_polling_rate_minutes = 1 # Minimum collateral required on data requests read from the WitnetOracle contract witnet_dr_min_collateral_nanowits = 20_000_000_000