Skip to content

Commit

Permalink
feat(c-bridge): report metrics on minutes o'clock
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Nov 7, 2024
1 parent 55b51c8 commit 1c831ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions bridges/centralized-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 24 additions & 10 deletions bridges/centralized-ethereum/src/actors/watch_dog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{Timelike, Utc, NaiveTime};
use crate::{
actors::dr_database::{CountDrsPerState, DrDatabase},
config::Config,
Expand Down Expand Up @@ -33,7 +34,7 @@ pub struct WatchDog {
/// WitOracle bridge contract
pub eth_contract: Option<Arc<Contract<web3::transports::Http>>>,
/// 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<Instant>,
/// Eth balance upon first metric report:
Expand Down Expand Up @@ -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),
);
}
}
Expand All @@ -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"),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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<JsonRpcClient>,
) -> Result<(), WatchDogStatus> {
Expand Down
8 changes: 4 additions & 4 deletions bridges/centralized-ethereum/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion witnet_centralized_ethereum_bridge.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c831ac

Please sign in to comment.