Skip to content

Commit

Permalink
Merge branch 'main' into mraszyk/unknown-is-agent-error
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk authored Aug 21, 2023
2 parents ac13cb6 + 6a2c4d6 commit fead1b3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust: [ '1.65.0' ]
os: [ ubuntu-latest ]
rust: ["1.65.0"]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ version = "0.25.0"
authors = ["DFINITY Stiftung <[email protected]>"]
edition = "2021"
repository = "https://github.com/dfinity/agent-rs"
# MSRV
# Avoid updating this field unless we use new Rust features
# Sync rust-version in following CI files:
rust-version = "1.65.0"
license = "Apache-2.0"

Expand Down
2 changes: 1 addition & 1 deletion ic-agent/src/identity/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl BasicIdentity {
.collect::<Result<Vec<u8>, std::io::Error>>()?;

Ok(BasicIdentity::from_key_pair(Ed25519KeyPair::from_pkcs8(
pem::parse(&bytes)?.contents(),
pem::parse(bytes)?.contents(),
)?))
}

Expand Down
2 changes: 1 addition & 1 deletion ic-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include = ["src", "Cargo.toml", "../LICENSE", "README.md"]

[dependencies]
async-trait = "0.1.68"
candid = { workspace = true, features = ["arc_type"] }
candid = { workspace = true }
ic-agent = { workspace = true, default-features = false }
serde = { workspace = true }
serde_bytes = { workspace = true }
Expand Down
23 changes: 15 additions & 8 deletions ic-utils/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ use ic_agent::{agent::UpdateBuilder, export::Principal, Agent, AgentError, Reque
use serde::de::DeserializeOwned;
use std::fmt;
use std::future::Future;
use std::pin::Pin;

mod expiry;
pub use expiry::Expiry;

#[cfg(target_family = "wasm")]
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
#[cfg(not(target_family = "wasm"))]
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;

/// A type that implements synchronous calls (ie. 'query' calls).
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
Expand Down Expand Up @@ -96,14 +102,15 @@ where
/// .create_canister()
/// .as_provisional_create_with_amount(None)
/// .with_effective_canister_id(effective_id)
/// .and_then(|(canister_id,)| async move {
/// management_canister
/// .install_code(&canister_id, canister_wasm)
/// .build()
/// .unwrap()
/// .call_and_wait()
/// .await?;
/// Ok((canister_id,))
/// .and_then(|(canister_id,)| {
/// let call = management_canister
/// .install_code(&canister_id, canister_wasm)
/// .build()
/// .unwrap();
/// async move {
/// call.call_and_wait().await?;
/// Ok((canister_id,))
/// }
/// })
/// .call_and_wait()
/// .await?;
Expand Down
9 changes: 2 additions & 7 deletions ic-utils/src/call/expiry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use time::OffsetDateTime;

/// An expiry value. Either not specified (the default), a delay relative to the time the
/// call is made, or a specific date time.
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Default)]
pub enum Expiry {
/// Unspecified. Will not try to override the Agent's value, which might itself have
/// its own default value.
#[default]
Unspecified,

/// A duration that will be added to the system time when the call is made.
Expand Down Expand Up @@ -65,9 +66,3 @@ impl From<OffsetDateTime> for Expiry {
Self::DateTime(dt)
}
}

impl Default for Expiry {
fn default() -> Self {
Expiry::Unspecified
}
}
24 changes: 16 additions & 8 deletions ic-utils/src/interfaces/management_canister/builders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Builder interfaces for some method calls of the management canister.

use crate::{
call::AsyncCall, canister::Argument, interfaces::management_canister::MgmtMethod, Canister,
call::{AsyncCall, BoxFuture},
canister::Argument,
interfaces::management_canister::MgmtMethod,
Canister,
};
use async_trait::async_trait;
use candid::{CandidType, Deserialize, Nat};
Expand Down Expand Up @@ -437,15 +440,20 @@ impl<'agent, 'canister: 'agent> InstallCodeBuilder<'agent, 'canister> {
}
}

#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
impl<'agent, 'canister: 'agent> AsyncCall<()> for InstallCodeBuilder<'agent, 'canister> {
async fn call(self) -> Result<RequestId, AgentError> {
self.build()?.call().await
fn call<'async_trait>(self) -> BoxFuture<'async_trait, Result<RequestId, AgentError>>
where
Self: 'async_trait,
{
let call_res = self.build();
Box::pin(async move { call_res?.call().await })
}

async fn call_and_wait(self) -> Result<(), AgentError> {
self.build()?.call_and_wait().await
fn call_and_wait<'async_trait>(self) -> BoxFuture<'async_trait, Result<(), AgentError>>
where
Self: 'async_trait,
{
let call_res = self.build();
Box::pin(async move { call_res?.call_and_wait().await })
}
}

Expand Down
31 changes: 18 additions & 13 deletions ic-utils/src/interfaces/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
//!
//! [cycles wallet]: https://github.com/dfinity/cycles-wallet

use std::ops::Deref;
use std::{future::Future, ops::Deref};

use crate::{
call::{AsyncCall, SyncCall},
call::{AsyncCall, BoxFuture, SyncCall},
canister::Argument,
interfaces::management_canister::{
attributes::{ComputeAllocation, FreezingThreshold, MemoryAllocation},
builders::CanisterSettings,
},
Canister,
};
use async_trait::async_trait;
use candid::{decode_args, utils::ArgumentDecoder, CandidType, Deserialize, Nat};
use ic_agent::{agent::RejectCode, export::Principal, Agent, AgentError, RequestId};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -111,28 +110,34 @@ where
}

/// Calls the forwarded canister call on the wallet canister. Equivalent to `.build().call()`.
pub async fn call(self) -> Result<RequestId, AgentError> {
self.build()?.call().await
pub fn call(self) -> impl Future<Output = Result<RequestId, AgentError>> + 'agent {
let call_res = self.build();
async move { call_res?.call().await }
}

/// Calls the forwarded canister call on the wallet canister, and waits for the result. Equivalent to `.build().call_and_wait()`.
pub async fn call_and_wait(self) -> Result<Out, AgentError> {
self.build()?.call_and_wait().await
pub fn call_and_wait(self) -> impl Future<Output = Result<Out, AgentError>> + 'agent {
let call_res = self.build();
async move { call_res?.call_and_wait().await }
}
}

#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
impl<'agent, 'canister: 'agent, Out> AsyncCall<Out> for CallForwarder<'agent, 'canister, Out>
where
Out: for<'de> ArgumentDecoder<'de> + Send + Sync,
{
async fn call(self) -> Result<RequestId, AgentError> {
self.call().await
fn call<'async_trait>(self) -> BoxFuture<'async_trait, Result<RequestId, AgentError>>
where
Self: 'async_trait,
{
Box::pin(self.call())
}

async fn call_and_wait(self) -> Result<Out, AgentError> {
self.call_and_wait().await
fn call_and_wait<'async_trait>(self) -> BoxFuture<'async_trait, Result<Out, AgentError>>
where
Self: 'async_trait,
{
Box::pin(self.call_and_wait())
}
}

Expand Down
6 changes: 5 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[toolchain]
channel = "1.65.0"
channel = "1.71.1"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]

# The version only influences the local develop environment.
# No need to sync with Cargo.toml and CI.

0 comments on commit fead1b3

Please sign in to comment.