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 47fb526
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 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
42 changes: 27 additions & 15 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 @@ -64,8 +65,7 @@ impl Actor for WatchDog {
None,
None,
None,
ctx,
Duration::from_millis(self.polling_rate_ms),
ctx
);
}
}
Expand All @@ -90,7 +90,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 +122,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 All @@ -135,8 +135,7 @@ impl WatchDog {
eth_balance: Option<f64>,
wit_balance: Option<f64>,
drs_history: Option<(u64, u64, u64)>,
ctx: &mut Context<Self>,
period: Duration,
ctx: &mut Context<Self>
) {
if self.start_eth_balance.is_none() && eth_balance.is_some() {
self.start_eth_balance = eth_balance;
Expand Down Expand Up @@ -348,11 +347,29 @@ impl WatchDog {
};

ctx.spawn(fut.into_actor(self).then(
move |(eth_balance, wit_balance, drs_history), _act, ctx| {
move |(eth_balance, wit_balance, drs_history), act, ctx| {
let time_now = Utc::now().time();
let period_minutes = act.polling_rate_minutes 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 {
Duration::from_secs((period_minutes * 60) as u64)
}
} else {
Duration::from_secs((period_minutes * 60) as u64)
};
// Schedule next iteration only when finished,
// as to avoid multiple tasks running in parallel
ctx.run_later(period, move |act, ctx| {
act.watch_global_status(eth_balance, wit_balance, drs_history, ctx, period);
ctx.run_later(dur, move |act, ctx| {
act.watch_global_status(eth_balance, wit_balance, drs_history, ctx);
});
actix::fut::ready(())
},
Expand Down Expand Up @@ -390,11 +407,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 47fb526

Please sign in to comment.