-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support notifying a chosen canister when trade is completed (#5167)
- Loading branch information
Showing
17 changed files
with
258 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
use crate::RuntimeState; | ||
|
||
pub mod make_pending_payments; | ||
pub mod notify_status_change; | ||
|
||
pub(crate) fn start(state: &RuntimeState) { | ||
make_pending_payments::start_job_if_required(state); | ||
notify_status_change::start_job_if_required(state); | ||
} |
63 changes: 63 additions & 0 deletions
63
backend/canisters/escrow/impl/src/jobs/notify_status_change.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::{mutate_state, RuntimeState}; | ||
use canister_client::make_c2c_call; | ||
use ic_cdk_timers::TimerId; | ||
use std::cell::Cell; | ||
use std::time::Duration; | ||
use tracing::trace; | ||
use types::{CanisterId, OfferStatus, OfferStatusChange}; | ||
|
||
thread_local! { | ||
static TIMER_ID: Cell<Option<TimerId>> = Cell::default(); | ||
} | ||
|
||
pub(crate) fn start_job_if_required(state: &RuntimeState) -> bool { | ||
if TIMER_ID.get().is_none() && !state.data.notify_status_change_queue.is_empty() { | ||
let timer_id = ic_cdk_timers::set_timer_interval(Duration::ZERO, run); | ||
TIMER_ID.set(Some(timer_id)); | ||
trace!("'notify_status_change' job started"); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub fn run() { | ||
if let Some((canister_id, offer_id, status)) = mutate_state(get_next) { | ||
ic_cdk::spawn(notify_offer_status(canister_id, offer_id, status)); | ||
} else if let Some(timer_id) = TIMER_ID.take() { | ||
ic_cdk_timers::clear_timer(timer_id); | ||
trace!("'notify_status_change' job stopped"); | ||
} | ||
} | ||
|
||
fn get_next(state: &mut RuntimeState) -> Option<(CanisterId, u32, OfferStatus)> { | ||
while let Some(id) = state.data.notify_status_change_queue.pop() { | ||
if let Some((canister_id, offer_id, status)) = state | ||
.data | ||
.offers | ||
.get(id) | ||
.and_then(|o| o.canister_to_notify.map(|c| (c, o.id, o.status(state.env.now())))) | ||
{ | ||
return Some((canister_id, offer_id, status)); | ||
} | ||
} | ||
None | ||
} | ||
|
||
async fn notify_offer_status(canister_id: CanisterId, offer_id: u32, status: OfferStatus) { | ||
if make_c2c_call( | ||
canister_id, | ||
"c2c_notify_p2p_offer_status_change_msgpack", | ||
OfferStatusChange { offer_id, status }, | ||
msgpack::serialize, | ||
|r| msgpack::deserialize::<()>(r), | ||
) | ||
.await | ||
.is_err() | ||
{ | ||
mutate_state(|state| { | ||
state.data.notify_status_change_queue.push(offer_id); | ||
start_job_if_required(state); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod notify_status_change_queue; | ||
pub mod offers; | ||
pub mod pending_payments_queue; |
21 changes: 21 additions & 0 deletions
21
backend/canisters/escrow/impl/src/model/notify_status_change_queue.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::VecDeque; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct NotifyStatusChangeQueue { | ||
offers: VecDeque<u32>, | ||
} | ||
|
||
impl NotifyStatusChangeQueue { | ||
pub fn push(&mut self, offer_id: u32) { | ||
self.offers.push_back(offer_id); | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<u32> { | ||
self.offers.pop_front() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.offers.is_empty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.