Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
348 changes: 207 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,20 @@ members = [
"src/dfx",
]

[patch.crates-io]
ic-types = { git = "ssh://git@github.com/dfinity-lab/agent-rust.git", branch = "next", version = "0.1.1" }
[patch.crates-io.ic-agent]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "191dca1c38cf7eebfb177f242063592223c87976"

[patch.crates-io.ic-types]
version = "0.1.2"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "191dca1c38cf7eebfb177f242063592223c87976"

[patch.crates-io.ic-utils]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "191dca1c38cf7eebfb177f242063592223c87976"
8 changes: 8 additions & 0 deletions dfx.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ let
cargoTestCommands = _: [
''cargo $cargo_options test $cargo_test_options --workspace''
];
override = attrs: {
preConfigure = (attrs.preConfigure or "") + ''
unset SDKROOT
'';
};
};

# set DFX_ASSETS for the builds and shells
Expand Down Expand Up @@ -85,6 +90,9 @@ let

# Set environment variable for debug version.
export DFX_TIMESTAMP_DEBUG_MODE_ONLY=$(date +%s)

# the presence of this var breaks brotli-sys compilation
unset SDKROOT
'';
};
};
Expand Down
17 changes: 11 additions & 6 deletions src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ chrono = "0.4.9"
clap = "2.33.0"
console = "0.7.7"
crossbeam = "0.7.3"
delay = "0.2.0"
delay = "0.3.0"
dialoguer = "0.6.2"
erased-serde = "0.3.10"
flate2 = "1.0.11"
Expand Down Expand Up @@ -68,16 +68,21 @@ wasmparser = "0.45.0"

[dependencies.ic-agent]
version = "0.1.0"
git = "ssh://git@github.com/dfinity-lab/agent-rust.git"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "9d611f08cb55923fc6ff5bf0136e8f4b848dd2cc"
rev = "191dca1c38cf7eebfb177f242063592223c87976"

[dependencies.ic-types]
version = "0.1.1"
git = "ssh://git@github.com/dfinity-lab/agent-rust.git"
version = "0.1.2"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "9d611f08cb55923fc6ff5bf0136e8f4b848dd2cc"
rev = "191dca1c38cf7eebfb177f242063592223c87976"

[dependencies.ic-utils]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "191dca1c38cf7eebfb177f242063592223c87976"

[dev-dependencies]
env_logger = "0.6"
Expand Down
8 changes: 6 additions & 2 deletions src/dfx/src/commands/canister/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
let timeout = expiry_duration();

if is_query {
let blob =
runtime.block_on(agent.query_raw(&canister_id, method_name, &arg_value, None))?;
let blob = runtime.block_on(
agent
.query(&canister_id, method_name)
.with_arg(&arg_value)
.call(),
)?;
print_idl_blob(&blob, output_type, &method_type)
.map_err(|e| DfxError::InvalidData(format!("Invalid IDL blob: {}", e)))?;
} else if args.is_present("async") {
Expand Down
12 changes: 7 additions & 5 deletions src/dfx/src/commands/canister/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::lib::waiter::waiter_with_timeout;
use crate::util::expiry_duration;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::{Agent, ManagementCanister};
use ic_agent::Agent;
use ic_utils::call::AsyncCall;
use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::time::Duration;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -36,7 +38,7 @@ async fn delete_canister(
canister_name: &str,
timeout: Duration,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let mgr = ManagementCanister::create(agent);
let log = env.get_logger();
let mut canister_id_store = CanisterIdStore::for_env(env)?;
let canister_id = canister_id_store.get(canister_name)?;
Expand All @@ -47,9 +49,9 @@ async fn delete_canister(
canister_id.to_text(),
);

mgr.delete_canister(waiter_with_timeout(timeout), &canister_id)
.await
.map_err(DfxError::from)?;
mgr.delete_canister(&canister_id)
.call_and_wait(waiter_with_timeout(timeout))
.await?;

canister_id_store.remove(canister_name)?;

Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/commands/canister/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::util::{blob_from_arguments, expiry_duration, get_candid_init_type};

use clap::{App, Arg, ArgMatches, SubCommand};
use humanize_rs::bytes::{Bytes, Unit};
use ic_agent::{ComputeAllocation, InstallMode, MemoryAllocation};

use ic_utils::interfaces::management_canister::{ComputeAllocation, InstallMode, MemoryAllocation};
use std::convert::TryFrom;
use std::str::FromStr;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
&canister_info,
&install_args,
compute_allocation,
mode.clone(),
mode,
memory_allocation,
timeout,
))?;
Expand Down
3 changes: 2 additions & 1 deletion src/dfx/src/commands/canister/request_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::util::clap::validators;
use crate::util::{expiry_duration, print_idl_blob};
use clap::{App, Arg, ArgMatches, SubCommand};
use delay::Waiter;
use ic_agent::{AgentError, Replied, RequestId, RequestStatusResponse};
use ic_agent::agent::{Replied, RequestStatusResponse};
use ic_agent::{AgentError, RequestId};
use std::str::FromStr;
use tokio::runtime::Runtime;

Expand Down
9 changes: 6 additions & 3 deletions src/dfx/src/commands/canister/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::lib::waiter::waiter_with_timeout;
use crate::util::expiry_duration;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::{Agent, ManagementCanister};
use ic_agent::Agent;
use ic_utils::call::AsyncCall;
use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::time::Duration;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -36,7 +38,7 @@ async fn start_canister(
canister_name: &str,
timeout: Duration,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let mgr = ManagementCanister::create(agent);
let log = env.get_logger();
let canister_id_store = CanisterIdStore::for_env(env)?;
let canister_id = canister_id_store.get(canister_name)?;
Expand All @@ -48,7 +50,8 @@ async fn start_canister(
canister_id.to_text(),
);

mgr.start_canister(waiter_with_timeout(timeout), &canister_id)
mgr.start_canister(&canister_id)
.call_and_wait(waiter_with_timeout(timeout))
.await
.map_err(DfxError::from)?;

Expand Down
11 changes: 7 additions & 4 deletions src/dfx/src/commands/canister/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::lib::waiter::waiter_with_timeout;
use crate::util::expiry_duration;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::{Agent, ManagementCanister};
use ic_agent::Agent;
use ic_utils::call::AsyncCall;
use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::time::Duration;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -36,13 +38,14 @@ async fn canister_status(
canister_name: &str,
timeout: Duration,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let mgr = ManagementCanister::create(agent);
let log = env.get_logger();
let canister_id_store = CanisterIdStore::for_env(env)?;
let canister_id = canister_id_store.get(canister_name)?;

let status = mgr
.canister_status(waiter_with_timeout(timeout), &canister_id)
let (status,) = mgr
.canister_status(&canister_id)
.call_and_wait(waiter_with_timeout(timeout))
.await
.map_err(DfxError::from)?;
Comment thread
hansl marked this conversation as resolved.
Outdated
info!(log, "Canister {}'s status is {}.", canister_name, status);
Expand Down
9 changes: 6 additions & 3 deletions src/dfx/src/commands/canister/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::lib::waiter::waiter_with_timeout;
use crate::util::expiry_duration;

use clap::{App, Arg, ArgMatches, SubCommand};
use ic_agent::{Agent, ManagementCanister};
use ic_agent::Agent;
use ic_utils::call::AsyncCall;
use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::time::Duration;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -36,7 +38,7 @@ async fn stop_canister(
canister_name: &str,
timeout: Duration,
) -> DfxResult {
let mgr = ManagementCanister::new(agent);
let mgr = ManagementCanister::create(agent);
let log = env.get_logger();
let canister_id_store = CanisterIdStore::for_env(env)?;
let canister_id = canister_id_store.get(canister_name)?;
Expand All @@ -48,7 +50,8 @@ async fn stop_canister(
canister_id.to_text(),
);

mgr.stop_canister(waiter_with_timeout(timeout), &canister_id)
mgr.stop_canister(&canister_id)
.call_and_wait(waiter_with_timeout(timeout))
.await
.map_err(DfxError::from)?;
Comment thread
hansl marked this conversation as resolved.
Outdated

Expand Down
3 changes: 2 additions & 1 deletion src/dfx/src/commands/identity/principal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::lib::error::DfxResult;
use crate::lib::identity::identity_manager::IdentityManager;
use crate::lib::message::UserMessage;
use clap::{App, ArgMatches, SubCommand};
use ic_agent::Identity;

pub fn construct() -> App<'static, 'static> {
SubCommand::with_name("get-principal").about(UserMessage::GetPrincipalId.to_str())
}

pub fn exec(env: &dyn Environment, _args: &ArgMatches<'_>) -> DfxResult {
let identity = IdentityManager::new(env)?.instantiate_selected_identity()?;
let principal_id = identity.sender()?;
let principal_id = identity.as_ref().sender()?;
println!("{}", principal_id.to_text());
Ok(())
}
7 changes: 2 additions & 5 deletions src/dfx/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crossbeam::channel::{Receiver, Sender};
use crossbeam::unbounded;
use delay::{Delay, Waiter};
use futures::executor::block_on;
use ic_agent::{Agent, AgentConfig};
use ic_agent::Agent;
use indicatif::{ProgressBar, ProgressDrawTarget};
use std::fs;
use std::io::{Error, ErrorKind};
Expand Down Expand Up @@ -51,10 +51,7 @@ pub fn construct() -> App<'static, 'static> {
fn ping_and_wait(frontend_url: &str) -> DfxResult {
let mut runtime = Runtime::new().expect("Unable to create a runtime");

let agent = Agent::new(AgentConfig {
url: frontend_url.to_string(),
..AgentConfig::default()
})?;
let agent = Agent::builder().with_url(frontend_url).build()?;

// wait for frontend to come up
let mut waiter = Delay::builder()
Expand Down
27 changes: 12 additions & 15 deletions src/dfx/src/lib/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::lib::identity::identity_manager::IdentityManager;
use crate::lib::network::network_descriptor::NetworkDescriptor;
use crate::lib::progress_bar::ProgressBar;

use ic_agent::{Agent, AgentConfig, Identity};
use ic_agent::{Agent, Identity};
use semver::Version;
use slog::{Logger, Record};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -409,22 +409,19 @@ impl ic_agent::PasswordManager for AgentClient {
}
}

fn create_agent(
logger: Logger,
url: &str,
identity: Box<dyn Identity + Send + Sync>,
timeout: Duration,
) -> Option<Agent> {
fn create_agent<I>(logger: Logger, url: &str, identity: Box<I>, timeout: Duration) -> Option<Agent>
where
I: Identity + Send + Sync + 'static,
{
AgentClient::new(logger, url.to_string())
.ok()
.and_then(|executor| {
Agent::new(AgentConfig {
url: url.to_string(),
identity,
password_manager: Some(Box::new(executor)),
ingress_expiry_duration: Some(timeout),
..AgentConfig::default()
})
.ok()
Agent::builder()
.with_url(url)
.with_boxed_identity(identity)
.with_password_manager(executor)
.with_ingress_expiry(Some(timeout))
.build()
.ok()
})
}
2 changes: 1 addition & 1 deletion src/dfx/src/lib/error/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pub enum IdentityErrorKind {
CannotFindUserHomeDirectory(),

#[error("An error occurred while reading {1}: {0}")]
AgentPemError(ic_agent::PemError, PathBuf),
AgentPemError(ic_agent::identity::PemError, PathBuf),
}
14 changes: 6 additions & 8 deletions src/dfx/src/lib/identity/identity_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::lib::config::get_config_dfx_dir_path;
use crate::lib::environment::Environment;
use crate::lib::error::{DfxError, DfxResult, IdentityErrorKind};
use ic_agent::{BasicIdentity, Identity};
use ic_agent::identity::BasicIdentity;
use ic_agent::Identity;
use pem::{encode, Pem};
use ring::{rand, signature};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -63,14 +64,11 @@ impl IdentityManager {
}

/// Create an Identity instance for use with an Agent
pub fn instantiate_selected_identity(&self) -> DfxResult<Box<dyn Identity + Send + Sync>> {
pub fn instantiate_selected_identity(&self) -> DfxResult<Box<impl Identity + Send + Sync>> {
let pem_path = self.get_selected_identity_pem_path();
let basic = BasicIdentity::from_pem_file(&pem_path).map_err(|e| {
DfxError::IdentityError(IdentityErrorKind::AgentPemError(e, pem_path.clone()))
})?;

let b: Box<dyn Identity + Send + Sync> = Box::new(basic);
Ok(b)
Ok(Box::new(BasicIdentity::from_pem_file(&pem_path).map_err(
|e| DfxError::IdentityError(IdentityErrorKind::AgentPemError(e, pem_path.clone())),
)?))
}

/// Create a new identity (name -> generated key)
Expand Down
10 changes: 7 additions & 3 deletions src/dfx/src/lib/operations/canister/create_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::provider::get_network_context;
use crate::lib::waiter::waiter_with_timeout;

use ic_agent::ManagementCanister;
use ic_utils::call::AsyncCall;
use ic_utils::interfaces::ManagementCanister;
use slog::info;
use std::format;
use std::time::Duration;
Expand Down Expand Up @@ -39,13 +40,16 @@ pub fn create_canister(env: &dyn Environment, canister_name: &str, timeout: Dura
Ok(())
}
None => {
let mgr = ManagementCanister::new(
let mgr = ManagementCanister::create(
env.get_agent()
.ok_or(DfxError::CommandMustBeRunInAProject)?,
);

let mut runtime = Runtime::new().expect("Unable to create a runtime");
let cid = runtime.block_on(mgr.create_canister(waiter_with_timeout(timeout)))?;
let (cid,) = runtime.block_on(
mgr.create_canister()
.call_and_wait(waiter_with_timeout(timeout)),
)?;
let canister_id = cid.to_text();
info!(
log,
Expand Down
Loading