diff --git a/alarms-dispatcher/src/config.rs b/alarms-dispatcher/src/config.rs index 42edd35..9761f93 100644 --- a/alarms-dispatcher/src/config.rs +++ b/alarms-dispatcher/src/config.rs @@ -33,8 +33,8 @@ impl Contract { pub struct Config { poll_period_seconds: u64, node: Node, - time_alarms: Contract, - market_price_oracle: Contract, + time_alarms: Vec, + market_price_oracle: Vec, } impl Config { @@ -47,11 +47,11 @@ impl Config { &self.node } - pub const fn time_alarms(&self) -> &Contract { + pub fn time_alarms(&self) -> &[Contract] { &self.time_alarms } - pub const fn market_price_oracle(&self) -> &Contract { + pub fn market_price_oracle(&self) -> &[Contract] { &self.market_price_oracle } } diff --git a/alarms-dispatcher/src/main.rs b/alarms-dispatcher/src/main.rs index 9b9dfba..6c0f1c9 100644 --- a/alarms-dispatcher/src/main.rs +++ b/alarms-dispatcher/src/main.rs @@ -5,7 +5,7 @@ use semver::{ Prerelease as SemVerPrerelease, Version, }; use serde::Deserialize; -use tokio::time::sleep; +use tokio::time::{sleep, sleep_until, Instant}; use tracing::{debug, error, info, info_span, span::EnteredSpan}; use tracing_appender::{ non_blocking::{self, NonBlocking}, @@ -105,18 +105,19 @@ async fn check_comparibility(rpc_setup: &RpcSetup) -> AppResult<()> { patch: u64, } - for (contract, name, compatible) in [ - ( - rpc_setup.config.time_alarms(), - "timealarms", - TIME_ALARMS_COMPATIBLE_VERSION, - ), - ( - rpc_setup.config.market_price_oracle(), - "oracle", - ORACLE_COMPATIBLE_VERSION, - ), - ] { + for (contract, name, compatible) in rpc_setup + .config + .time_alarms() + .iter() + .map(|contract: &Contract| (contract, "timealarms", TIME_ALARMS_COMPATIBLE_VERSION)) + .chain( + rpc_setup + .config + .market_price_oracle() + .iter() + .map(|contract: &Contract| (contract, "oracle", ORACLE_COMPATIBLE_VERSION)), + ) + { let version: JsonVersion = rpc_setup .nolus_node .with_grpc(|rpc: TonicChannel| { @@ -162,33 +163,32 @@ async fn dispatch_alarms( .. }: RpcSetup, ) -> Result<(), error::DispatchAlarms> { - type Contracts<'r> = [( - &'r Contract, - &'static str, - fn(error::DispatchAlarm) -> error::DispatchAlarms, - ); 2]; - let poll_period: Duration = Duration::from_secs(config.poll_period_seconds()); let mut fallback_gas_limit: Option = None; - let contracts: Contracts<'_> = [ - ( - config.market_price_oracle(), - "market price", - error::DispatchAlarms::DispatchPriceAlarm - as fn(error::DispatchAlarm) -> error::DispatchAlarms, - ), - ( - config.time_alarms(), - "time", - error::DispatchAlarms::DispatchTimeAlarm - as fn(error::DispatchAlarm) -> error::DispatchAlarms, - ), - ]; + let contracts = config + .market_price_oracle() + .iter() + .map(|contract: &Contract| { + ( + contract, + "market price", + error::DispatchAlarms::DispatchPriceAlarm as _, + ) + }) + .chain(config.time_alarms().iter().map(|contract: &Contract| { + ( + contract, + "time", + error::DispatchAlarms::DispatchPriceAlarm as _, + ) + })); loop { - for contract in contracts { + let next_iteration: Option = Instant::now().checked_add(poll_period); + + for contract in contracts.clone() { let mut is_retry: bool = false; 'dispatch_alarm: loop { @@ -214,7 +214,13 @@ async fn dispatch_alarms( } } - sleep(poll_period).await; + if let Some(instant) = next_iteration { + if Instant::now() < instant { + sleep_until(instant).await + } + } else { + sleep(poll_period).await; + } } } diff --git a/configurations/alarms-dispatcher.dev.toml b/configurations/alarms-dispatcher.dev.toml index 7b7e1f6..e460f06 100644 --- a/configurations/alarms-dispatcher.dev.toml +++ b/configurations/alarms-dispatcher.dev.toml @@ -11,13 +11,13 @@ fee_denom = "unls" gas_price_numerator = 1 gas_price_denominator = 390 -[time_alarms] +[[time_alarms]] address = "nolus1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqmx7chl" max_alarms_group = 16 #gas_limit_per_alarm = 87_500 gas_limit_per_alarm = 1_500_000 -[market_price_oracle] +[[market_price_oracle]] address = "nolus1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgsv3wzl4" max_alarms_group = 16 gas_limit_per_alarm = 1_500_000 diff --git a/configurations/alarms-dispatcher.main.toml b/configurations/alarms-dispatcher.main.toml index 52766fe..8a913eb 100644 --- a/configurations/alarms-dispatcher.main.toml +++ b/configurations/alarms-dispatcher.main.toml @@ -9,12 +9,12 @@ fee_denom = "unls" gas_price_numerator = 1 gas_price_denominator = 390 -[time_alarms] +[[time_alarms]] address = "nolus1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqmx7chl" max_alarms_group = 16 gas_limit_per_alarm = 5_000_000 -[market_price_oracle] +[[market_price_oracle]] address = "nolus1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgsv3wzl4" max_alarms_group = 16 gas_limit_per_alarm = 5_000_000 diff --git a/configurations/alarms-dispatcher.test.toml b/configurations/alarms-dispatcher.test.toml index f757b86..75dd927 100644 --- a/configurations/alarms-dispatcher.test.toml +++ b/configurations/alarms-dispatcher.test.toml @@ -11,13 +11,13 @@ fee_denom = "unls" gas_price_numerator = 1 gas_price_denominator = 390 -[time_alarms] +[[time_alarms]] address = "nolus1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqmx7chl" max_alarms_group = 16 #gas_limit_per_alarm = 175_000 gas_limit_per_alarm = 5_000_000 -[market_price_oracle] +[[market_price_oracle]] address = "nolus1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgsv3wzl4" max_alarms_group = 16 gas_limit_per_alarm = 5_000_000