Skip to content

Commit

Permalink
feat: root call mod with new CallError (#535)
Browse files Browse the repository at this point in the history
* duplicate call.rs in crate root

* keep only the new call API in root call.rs

* CallResult<R> = Result<R, CallError>;

* RejectCode & CallPerformErrorCode

* remove CleanupExecuted

* try add CandidEncodeFailed error type

* handle CandidEncodeFailed error properly

* copy api/call.rs from main branch and fix for new ic0

* test all Call struct variants and rename ArgDecoderConfig to DecoderConfig

* specify CallPerformErrorCode variants
  • Loading branch information
lwshang authored Dec 11, 2024
1 parent 605abca commit 9f9c0f7
Show file tree
Hide file tree
Showing 9 changed files with 834 additions and 350 deletions.
77 changes: 69 additions & 8 deletions e2e-tests/src/bin/call_struct.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,78 @@
use candid::Principal;
use ic_cdk::api::management_canister::main::{CanisterIdRecord, CreateCanisterArgument};
use ic_cdk::prelude::*;
use candid::Encode;
use ic_cdk::{call::DecoderConfig, prelude::*};

/// This endpoint is to be called by the following Call struct invocation.
#[update]
async fn create_canister_via_struct() -> Principal {
let res: (CanisterIdRecord,) = Call::new(Principal::management_canister(), "create_canister")
.with_args((CreateCanisterArgument::default(),))
.with_cycles(1_000_000_000_000)
async fn echo(arg: u32) -> u32 {
arg
}

#[update]
async fn call_struct() {
let num = 1u32;
let bytes: Vec<u8> = Encode!(&num).unwrap();

// 1. Various ways to call*
// 1.1 call()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 1.2 call_raw()
let res = Call::new(id(), "echo")
.with_args((num,))
.call_raw()
.await
.unwrap();
assert_eq!(res, bytes);
// 1.3 call_with_decoder_config()
let config = DecoderConfig::default();
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.call_with_decoder_config(&config)
.await
.unwrap();
assert_eq!(res.0, num);
// 1.4 call_raw_with_decoder_config()
Call::new(id(), "echo")
.with_args((num,))
.call_and_forget()
.unwrap();

// 2. Various ways to config the call
// 2.1 with_raw_args()
let res: (u32,) = Call::new(id(), "echo")
.with_raw_args(&bytes)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 2.2 with_guaranteed_response()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.with_guaranteed_response()
.call()
.await
.unwrap();
res.0.canister_id
assert_eq!(res.0, num);
// 2.3 change_timeout()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.change_timeout(5)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 2.4 with_cycles()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.with_cycles(100_000)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
}

fn main() {}
14 changes: 8 additions & 6 deletions e2e-tests/tests/call_struct.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use candid::Principal;
use ic_cdk_e2e_tests::cargo_build_canister;
use pocket_ic::common::rest::RawEffectivePrincipal;
use pocket_ic::{call_candid, PocketIc};
use pocket_ic::{call_candid, PocketIcBuilder};

#[test]
fn call_struct() {
let pic = PocketIc::new();
let pic = PocketIcBuilder::new()
.with_application_subnet()
.with_nonmainnet_features(true)
.build();
let wasm = cargo_build_canister("call_struct");
let canister_id = pic.create_canister();
pic.add_cycles(canister_id, 100_000_000_000_000);
pic.install_canister(canister_id, wasm, vec![], None);
let _: (Principal,) = call_candid(
let _: () = call_candid(
&pic,
canister_id,
RawEffectivePrincipal::None,
"create_canister_via_struct",
"call_struct",
(),
)
.expect("Error calling create_canister_via_struct");
.expect("Error calling call_struct");
}
1 change: 0 additions & 1 deletion ic-cdk-timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ extern "C" fn global_timer() {
| RejectionCode::DestinationInvalid
| RejectionCode::CanisterReject
| RejectionCode::CanisterError
| RejectionCode::SysUnknown
| RejectionCode::Unknown => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions ic-cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ic-cdk-macros = { path = "../ic-cdk-macros", version = "=0.18.0-alpha.1" }
serde.workspace = true
serde_bytes.workspace = true
slotmap = { workspace = true, optional = true }
thiserror = "2.0"

[dev-dependencies]
anyhow = "1"
Expand Down
Loading

0 comments on commit 9f9c0f7

Please sign in to comment.