-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor replica and fix minor stuff (#22)
- Loading branch information
Showing
11 changed files
with
291 additions
and
190 deletions.
There are no files selected for viewing
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 +1 @@ | ||
service : { add_counter : (principal) -> (); increment : () -> () } | ||
service : { deploy_counter : () -> (principal) } |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
//! canister. | ||
pub mod canister; | ||
pub use canister::MultiCounterCanister; | ||
pub use canister::FactoryCounterCanister; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "ic-kit-management" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ic-kit = {path="../ic-kit", version="0.5.0-alpha.4"} | ||
candid="0.7" | ||
serde="1.0" |
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,16 @@ | ||
// TODO(qti3e Implement management interface and types. | ||
|
||
use ic_kit::prelude::*; | ||
|
||
#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)] | ||
pub struct CreateCanisterArgument { | ||
pub settings: Option<CanisterSettings>, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone, PartialOrd, PartialEq, CandidType)] | ||
pub struct CanisterSettings { | ||
pub controllers: Option<Vec<Principal>>, | ||
pub compute_allocation: Option<Nat>, | ||
pub memory_allocation: Option<Nat>, | ||
pub freezing_threshold: Option<Nat>, | ||
} |
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,79 @@ | ||
use crate::call::{CallBuilder, CallReply}; | ||
use crate::types::{Env, Message, RequestId}; | ||
use crate::Replica; | ||
use ic_types::Principal; | ||
use std::panic::{RefUnwindSafe, UnwindSafe}; | ||
use tokio::sync::oneshot; | ||
|
||
pub struct CanisterHandle<'a> { | ||
pub(crate) replica: &'a Replica, | ||
pub(crate) canister_id: Principal, | ||
} | ||
|
||
impl<'a> CanisterHandle<'a> { | ||
/// Create a new call builder to call this canister. | ||
pub fn new_call<S: Into<String>>(&self, method_name: S) -> CallBuilder { | ||
CallBuilder::new(self.replica, self.canister_id, method_name.into()) | ||
} | ||
|
||
/// Run the given custom function in the execution thread of the canister. | ||
pub async fn custom<F: FnOnce() + Send + RefUnwindSafe + UnwindSafe + 'static>( | ||
&self, | ||
f: F, | ||
env: Env, | ||
) -> CallReply { | ||
let (tx, rx) = oneshot::channel(); | ||
|
||
self.replica.enqueue_request( | ||
self.canister_id, | ||
Message::CustomTask { | ||
request_id: RequestId::new(), | ||
task: Box::new(f), | ||
env, | ||
}, | ||
Some(tx), | ||
); | ||
|
||
rx.await.unwrap() | ||
} | ||
|
||
/// Run the given raw message in the canister's execution thread. | ||
pub async fn run_env(&self, env: Env) -> CallReply { | ||
let (tx, rx) = oneshot::channel(); | ||
|
||
self.replica.enqueue_request( | ||
self.canister_id, | ||
Message::Request { | ||
request_id: RequestId::new(), | ||
env, | ||
}, | ||
Some(tx), | ||
); | ||
|
||
rx.await.unwrap() | ||
} | ||
|
||
/// Runs the init hook of the canister. For more customization use [`CanisterHandle::run_env`] | ||
/// with [`Env::init()`]. | ||
pub async fn init(&self) -> CallReply { | ||
self.run_env(Env::init()).await | ||
} | ||
|
||
/// Runs the pre_upgrade hook of the canister. For more customization use | ||
/// [`CanisterHandle::run_env`] with [`Env::pre_upgrade()`]. | ||
pub async fn pre_upgrade(&self) -> CallReply { | ||
self.run_env(Env::pre_upgrade()).await | ||
} | ||
|
||
/// Runs the post_upgrade hook of the canister. For more customization use | ||
/// [`CanisterHandle::run_env`] with [`Env::post_upgrade()`]. | ||
pub async fn post_upgrade(&self) -> CallReply { | ||
self.run_env(Env::post_upgrade()).await | ||
} | ||
|
||
/// Runs the post_upgrade hook of the canister. For more customization use | ||
/// [`CanisterHandle::run_env`] with [`Env::heartbeat()`]. | ||
pub async fn heartbeat(&self) -> CallReply { | ||
self.run_env(Env::heartbeat()).await | ||
} | ||
} |
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.