Skip to content

Commit

Permalink
refactor: Support multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilMihaylov committed Nov 15, 2023
1 parent 5103404 commit f7bdb8f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
8 changes: 4 additions & 4 deletions alarms-dispatcher/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Contract>,
market_price_oracle: Vec<Contract>,
}

impl Config {
Expand All @@ -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
}
}
Expand Down
76 changes: 41 additions & 35 deletions alarms-dispatcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -105,18 +105,19 @@ async fn check_comparibility(rpc_setup: &RpcSetup<Config>) -> 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| {
Expand Down Expand Up @@ -162,33 +163,32 @@ async fn dispatch_alarms(
..
}: RpcSetup<Config>,
) -> 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<u64> = 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> = Instant::now().checked_add(poll_period);

for contract in contracts.clone() {
let mut is_retry: bool = false;

'dispatch_alarm: loop {
Expand All @@ -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;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions configurations/alarms-dispatcher.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions configurations/alarms-dispatcher.main.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions configurations/alarms-dispatcher.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f7bdb8f

Please sign in to comment.