Skip to content

Commit

Permalink
refactor: Moved logic behind time-oracle package's Alarms API
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilMihaylov authored and Gancho Manev committed May 16, 2023
1 parent d39d2e6 commit b3d5397
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
24 changes: 14 additions & 10 deletions contracts/timealarms/src/alarms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use platform::{
message::Response as MessageResponse,
};
use sdk::cosmwasm_std::{Addr, Env, QuerierWrapper, Storage, Timestamp};
use time_oracle::{Alarms, InDelivery};
use time_oracle::Alarms;

use crate::{
msg::{AlarmsCount, AlarmsStatusResponse, ExecuteAlarmMsg},
Expand All @@ -30,6 +30,7 @@ impl TimeAlarms {

pub fn remove(&self, storage: &mut dyn Storage, addr: Addr) -> Result<(), ContractError> {
self.time_alarms.remove(storage, addr)?;

Ok(())
}

Expand Down Expand Up @@ -68,12 +69,11 @@ impl TimeAlarms {
.into_iter()
.try_fold(
AlarmsDispatcher::new(ExecuteAlarmMsg::TimeAlarm {}, Self::EVENT_TYPE),
|mut dispatcher, (addr, time)| -> ContractResult<_> {
dispatcher = dispatcher.send_to(&addr, Self::REPLY_ID)?;

self.time_alarms.remove(storage, addr.clone())?;
|mut dispatcher, (subscriber, time)| -> ContractResult<_> {
dispatcher = dispatcher.send_to(&subscriber, Self::REPLY_ID)?;

InDelivery::new(storage).add(addr, time)?;
self.time_alarms
.out_for_delivery(storage, subscriber, time)?;

Ok(dispatcher)
},
Expand All @@ -96,10 +96,14 @@ impl TimeAlarms {
Ok(AlarmsStatusResponse { remaining_alarms })
}

pub fn failed(&mut self, in_delivery: &mut InDelivery<'_>) -> ContractResult<()> {
in_delivery
.failed(&mut self.time_alarms)
.map_err(Into::into)
#[inline]
pub fn last_delivered(&self, storage: &mut dyn Storage) -> ContractResult<()> {
self.time_alarms.last_delivered(storage).map_err(Into::into)
}

#[inline]
pub fn last_failed(&self, storage: &mut dyn Storage) -> ContractResult<()> {
self.time_alarms.last_failed(storage).map_err(Into::into)
}
}

Expand Down
7 changes: 3 additions & 4 deletions contracts/timealarms/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use sdk::{
cosmwasm_ext::Response as CwResponse,
cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, SubMsgResult},
};
use time_oracle::InDelivery;
use versioning::{package_version, version, VersionSegment};

use crate::{
Expand Down Expand Up @@ -85,16 +84,16 @@ pub fn reply(deps: DepsMut<'_>, _env: Env, msg: Reply) -> ContractResult<CwRespo

let emitter: Emitter = Emitter::of_type(EVENT_TYPE);

let mut in_delivery: InDelivery<'_> = InDelivery::new(deps.storage);
let time_alarms: TimeAlarms = TimeAlarms::new();

Ok(response::response_only_messages(match msg.result {
SubMsgResult::Ok(_) => {
in_delivery.delivered()?;
time_alarms.last_delivered(deps.storage)?;

emitter.emit(KEY_DELIVERED, "success")
}
SubMsgResult::Err(err) => {
TimeAlarms::new().failed(&mut in_delivery)?;
time_alarms.last_failed(deps.storage)?;

emitter.emit(KEY_DELIVERED, "error").emit(KEY_DETAILS, err)
}
Expand Down
51 changes: 42 additions & 9 deletions packages/time-oracle/src/alarms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ impl<'a> Alarms<'a> {
Self { alarms }
}

#[inline]
pub fn add(
&self,
storage: &mut dyn Storage,
subscriber: Addr,
time: Timestamp,
) -> Result<(), AlarmError> {
self.alarms.save(storage, subscriber, &as_seconds(time))?;

Ok(())
self.add_internal(storage, subscriber, as_seconds(time))
}

pub fn remove(&self, storage: &mut dyn Storage, subscriber: Addr) -> Result<(), AlarmError> {
Expand All @@ -57,6 +56,27 @@ impl<'a> Alarms<'a> {
Ok(())
}

pub fn out_for_delivery(
&self,
storage: &mut dyn Storage,
subscriber: Addr,
time: TimeSeconds,
) -> Result<(), AlarmError> {
self.alarms.remove(storage, subscriber.clone())?;

InDelivery::new(storage).add(subscriber, time)
}

#[inline]
pub fn last_delivered(&self, storage: &mut dyn Storage) -> Result<(), AlarmError> {
InDelivery::new(storage).delivered()
}

#[inline]
pub fn last_failed(&self, storage: &mut dyn Storage) -> Result<(), AlarmError> {
InDelivery::new(storage).failed(self)
}

pub fn alarms_selection<'b>(
&self,
storage: &'b dyn Storage,
Expand All @@ -76,41 +96,54 @@ impl<'a> Alarms<'a> {
)
.map(|res| res.map_err(AlarmError::from))
}

fn add_internal(
&self,
storage: &mut dyn Storage,
subscriber: Addr,
time: TimeSeconds,
) -> Result<(), AlarmError> {
self.alarms
.save(storage, subscriber, &time)
.map_err(Into::into)
}
}

pub struct InDelivery<'r> {
#[must_use]
struct InDelivery<'r> {
storage: &'r mut dyn Storage,
}

impl<'r> InDelivery<'r> {
const ALARMS: Deque<'_, TimeAlarm> = Deque::new("in_delivery");

pub fn new(storage: &'r mut dyn Storage) -> Self {
#[inline]
fn new(storage: &'r mut dyn Storage) -> Self {
Self { storage }
}

pub fn add(&mut self, subscriber: Addr, time: TimeSeconds) -> Result<(), AlarmError> {
fn add(&mut self, subscriber: Addr, time: TimeSeconds) -> Result<(), AlarmError> {
Self::ALARMS
.push_back(self.storage, &TimeAlarm { subscriber, time })
.map_err(Into::into)
}

pub fn delivered(&mut self) -> Result<(), AlarmError> {
fn delivered(&mut self) -> Result<(), AlarmError> {
Self::ALARMS
.pop_front(self.storage)
.map(|maybe_alarm: Option<TimeAlarm>| debug_assert!(maybe_alarm.is_some()))
.map_err(Into::into)
}

pub fn failed(&mut self, alarms: &mut Alarms<'_>) -> Result<(), AlarmError> {
fn failed(&mut self, alarms: &Alarms<'_>) -> Result<(), AlarmError> {
Self::ALARMS
.pop_front(self.storage)
.map_err(Into::into)
.and_then(|maybe_alarm: Option<TimeAlarm>| {
maybe_alarm.ok_or(AlarmError::ReplyOnEmptyAlarmQueue)
})
.and_then(|TimeAlarm { subscriber, time }| {
alarms.add(self.storage, subscriber, Timestamp::from_seconds(time))
alarms.add_internal(self.storage, subscriber, time)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/time-oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use thiserror::Error;

use sdk::cosmwasm_std::StdError;

pub use crate::alarms::{Alarms, InDelivery};
pub use crate::alarms::Alarms;

mod alarms;
pub mod migrate_v1;
Expand Down

0 comments on commit b3d5397

Please sign in to comment.