Skip to content

Commit

Permalink
refactor: Added dyn Storage as part of Alarms(Mut) and removed …
Browse files Browse the repository at this point in the history
…it from API callings
  • Loading branch information
KirilMihaylov committed May 15, 2023
1 parent e3cb160 commit 4cf53be
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 149 deletions.
96 changes: 48 additions & 48 deletions contracts/timealarms/src/alarms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,62 @@ use platform::{
message::Response as MessageResponse,
};
use sdk::cosmwasm_std::{Addr, Env, QuerierWrapper, Storage, Timestamp};
use time_oracle::Alarms;
use time_oracle::{Alarms, AlarmsMut};

use crate::{
msg::{AlarmsCount, AlarmsStatusResponse, ExecuteAlarmMsg},
result::ContractResult,
ContractError,
};

pub(super) struct TimeAlarms {
time_alarms: Alarms<'static>,
const ALARMS_NAMESPACE: &'static str = "alarms";
const ALARMS_IDX_NAMESPACE: &'static str = "alarms_idx";
const REPLY_ID: Id = 0;
const EVENT_TYPE: &'static str = "timealarm";

pub(super) struct TimeAlarms<'r> {
time_alarms: Alarms<'r, 'static, 'static>,
}

pub(super) struct TimeAlarmsMut<'r> {
time_alarms: AlarmsMut<'r, 'static>,
}

impl TimeAlarms {
const ALARMS_NAMESPACE: &'static str = "alarms";
const ALARMS_IDX_NAMESPACE: &'static str = "alarms_idx";
const REPLY_ID: Id = 0;
const EVENT_TYPE: &'static str = "timealarm";
impl<'r> TimeAlarms<'r> {
pub fn new(storage: &'r dyn Storage) -> Self {
Self {
time_alarms: Alarms::new(storage, ALARMS_NAMESPACE, ALARMS_IDX_NAMESPACE),
}
}

pub fn try_any_alarm(&self, ctime: Timestamp) -> Result<AlarmsStatusResponse, ContractError> {
let remaining_alarms = self
.time_alarms
.alarms_selection(ctime)
.next()
.transpose()?
.is_some();

Ok(AlarmsStatusResponse { remaining_alarms })
}
}

pub fn new() -> Self {
impl<'r> TimeAlarmsMut<'r> {
pub fn new(storage: &'r mut dyn Storage) -> Self {
Self {
time_alarms: Alarms::new(Self::ALARMS_NAMESPACE, Self::ALARMS_IDX_NAMESPACE),
time_alarms: AlarmsMut::new(storage, ALARMS_NAMESPACE, ALARMS_IDX_NAMESPACE),
}
}

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

Ok(())
}

pub fn try_add(
&self,
&mut self,
querier: &QuerierWrapper<'_>,
storage: &mut dyn Storage,
env: &Env,
subscriber: Addr,
time: Timestamp,
Expand All @@ -48,62 +70,40 @@ impl TimeAlarms {

contract::validate_addr(querier, &subscriber)
.map_err(ContractError::from)
.and_then(|()| {
self.time_alarms
.add(storage, subscriber, time)
.map_err(Into::into)
})
.and_then(|()| self.time_alarms.add(subscriber, time).map_err(Into::into))
.map(|()| Default::default())
}

pub fn try_notify(
&self,
storage: &mut dyn Storage,
&mut self,
ctime: Timestamp,
max_count: AlarmsCount,
) -> ContractResult<(AlarmsCount, MessageResponse)> {
self.time_alarms
.alarms_selection(storage, ctime)
.as_alarms()
.alarms_selection(ctime)
.take(max_count.try_into()?)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.try_fold(
AlarmsDispatcher::new(ExecuteAlarmMsg::TimeAlarm {}, Self::EVENT_TYPE),
|mut dispatcher, (subscriber, time)| -> ContractResult<_> {
dispatcher = dispatcher.send_to(&subscriber, Self::REPLY_ID)?;
AlarmsDispatcher::new(ExecuteAlarmMsg::TimeAlarm {}, EVENT_TYPE),
|mut dispatcher, (subscriber, _)| -> ContractResult<_> {
dispatcher = dispatcher.send_to(&subscriber, REPLY_ID)?;

self.time_alarms
.out_for_delivery(storage, subscriber, time)?;
self.time_alarms.out_for_delivery(subscriber)?;

Ok(dispatcher)
},
)
.map(|dispatcher| (dispatcher.nb_sent(), dispatcher.into()))
}

pub fn try_any_alarm(
&self,
storage: &dyn Storage,
ctime: Timestamp,
) -> Result<AlarmsStatusResponse, ContractError> {
let remaining_alarms = self
.time_alarms
.alarms_selection(storage, ctime)
.next()
.transpose()?
.is_some();

Ok(AlarmsStatusResponse { remaining_alarms })
}

#[inline]
pub fn last_delivered(&self, storage: &mut dyn Storage) -> ContractResult<()> {
self.time_alarms.last_delivered(storage).map_err(Into::into)
pub fn last_delivered(&mut self) -> ContractResult<()> {
self.time_alarms.last_delivered().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)
pub fn last_failed(&mut self, now: Timestamp) -> ContractResult<()> {
self.time_alarms.last_failed(now).map_err(Into::into)
}
}

Expand Down
22 changes: 11 additions & 11 deletions contracts/timealarms/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ pub fn execute(
msg: ExecuteMsg,
) -> ContractResult<CwResponse> {
match msg {
ExecuteMsg::AddAlarm { time } => TimeAlarms::new()
.try_add(&deps.querier, deps.storage, &env, info.sender, time)
ExecuteMsg::AddAlarm { time } => TimeAlarmsMut::new(deps.storage)
.try_add(&deps.querier, &env, info.sender, time)
.map(response::response_only_messages),
ExecuteMsg::DispatchAlarms { max_count } => TimeAlarms::new()
.try_notify(deps.storage, env.block.time, max_count)
ExecuteMsg::DispatchAlarms { max_count } => TimeAlarmsMut::new(deps.storage)
.try_notify(env.block.time, max_count)
.and_then(|(total, resp)| {
response::response_with_messages(&DispatchAlarmsResponse(total), resp)
}),
Expand All @@ -60,8 +60,8 @@ pub fn execute(
#[cfg_attr(feature = "contract-with-bindings", entry_point)]
pub fn sudo(deps: DepsMut<'_>, _env: Env, msg: SudoMsg) -> ContractResult<CwResponse> {
match msg {
SudoMsg::RemoveTimeAlarm { receiver } => TimeAlarms::new()
.remove(deps.storage, receiver)
SudoMsg::RemoveTimeAlarm { receiver } => TimeAlarmsMut::new(deps.storage)
.remove(receiver)
.map(|()| Default::default()),
}
}
Expand All @@ -71,29 +71,29 @@ pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> ContractResult<Binary>
match msg {
QueryMsg::ContractVersion {} => Ok(to_binary(&package_version!())?),
QueryMsg::AlarmsStatus {} => Ok(to_binary(
&TimeAlarms::new().try_any_alarm(deps.storage, env.block.time)?,
&TimeAlarms::new(deps.storage).try_any_alarm(env.block.time)?,
)?),
}
}

#[cfg_attr(feature = "contract-with-bindings", entry_point)]
pub fn reply(deps: DepsMut<'_>, _env: Env, msg: Reply) -> ContractResult<CwResponse> {
pub fn reply(deps: DepsMut<'_>, env: Env, msg: Reply) -> ContractResult<CwResponse> {
const EVENT_TYPE: &str = "time-alarm";
const KEY_DELIVERED: &str = "delivered";
const KEY_DETAILS: &str = "details";

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

let time_alarms: TimeAlarms = TimeAlarms::new();
let mut time_alarms: TimeAlarmsMut = TimeAlarmsMut::new(deps.storage);

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

emitter.emit(KEY_DELIVERED, "success")
}
SubMsgResult::Err(err) => {
time_alarms.last_failed(deps.storage)?;
time_alarms.last_failed(env.block.time)?;

emitter.emit(KEY_DELIVERED, "error").emit(KEY_DETAILS, err)
}
Expand Down
Loading

0 comments on commit 4cf53be

Please sign in to comment.