Skip to content

Commit

Permalink
refactor replica and fix minor stuff (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf authored Sep 1, 2022
2 parents 4b7e64e + bfddf47 commit 7f8a47c
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 190 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ members = [
"examples/naming_system",
"ic-kit",
"ic-kit-certified",
"ic-kit-stable",
"ic-kit-sys",
"ic-kit-macros",
"ic-kit-management",
"ic-kit-runtime",
"ic-kit-stable",
"ic-kit-sys",
]

[profile.canister-release]
Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
This directory contains some example canister's implemented using the IC Kit. Each example tries
to be as simple as possible to demonstrate one aspect of the IC Kit and a possible design pattern
you can also use to develop canisters.

Simple State Manipulation:
- Counter
2 changes: 1 addition & 1 deletion examples/factory_counter/candid.did
Original file line number Diff line number Diff line change
@@ -1 +1 @@
service : { add_counter : (principal) -> (); increment : () -> () }
service : { deploy_counter : () -> (principal) }
5 changes: 5 additions & 0 deletions examples/factory_counter/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ use ic_kit_example_counter::CounterCanister;
async fn deploy_counter() -> Principal {
// TODO(qti3e) we should make it possible to deploy an instance of CounterCanister.
// It should work in the IC and the ic-kit-runtime environment.
// CounterCanister::install_code(CANISTER_ID);
todo!()
}

#[derive(KitCanister)]
#[candid_path("candid.did")]
pub struct FactoryCounterCanister;
2 changes: 1 addition & 1 deletion examples/factory_counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
//! canister.
pub mod canister;
pub use canister::MultiCounterCanister;
pub use canister::FactoryCounterCanister;
10 changes: 5 additions & 5 deletions examples/multi_counter/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ fn increment(counters: &MultiCounter) {
for &canister_id in counters.canister_ids.iter() {
println!("Increment on {}", canister_id);

spawn(async move {
CallBuilder::new(canister_id, "increment")
.perform_one_way()
.expect("Expected the one way call to succeed.");
});
CallBuilder::new(canister_id, "increment")
.perform_one_way()
.expect("Expected the one way call to succeed.");
}
}

Expand Down Expand Up @@ -82,6 +80,8 @@ mod tests {
// Do a proxy increment call.
let x = canister.new_call("increment").perform().await;

// TODO(qti3e) replica.idle.await

println!("{:#?}", x);
}
}
11 changes: 11 additions & 0 deletions ic-kit-management/Cargo.toml
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"
16 changes: 16 additions & 0 deletions ic-kit-management/src/lib.rs
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>,
}
79 changes: 79 additions & 0 deletions ic-kit-runtime/src/handle.rs
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
}
}
1 change: 1 addition & 0 deletions ic-kit-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cfg_if::cfg_if! {
pub mod stable;
pub mod types;
pub mod users;
pub mod handle;

pub use canister::{Canister, CanisterMethod};
pub use replica::Replica;
Expand Down
Loading

0 comments on commit 7f8a47c

Please sign in to comment.