Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
351 changes: 121 additions & 230 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ members = [
[patch.crates-io.ic-agent]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[patch.crates-io.ic-identity-hsm]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[patch.crates-io.ic-types]
version = "0.1.2"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[patch.crates-io.ic-utils]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"
22 changes: 11 additions & 11 deletions e2e/bats/controller.bash → e2e/bats/update_settings.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ teardown() {
rm -rf $(pwd)/home-for-test
}

@test "set controller" {
@test "update controller" {
# Create two identities and get their Principals
assert_command dfx identity new jose
assert_command dfx identity new juana
Expand All @@ -34,8 +34,8 @@ teardown() {
ID=$(dfx canister id hello)

# Set controller using canister name and identity name
assert_command dfx canister set-controller hello juana
assert_match "Set \"juana\" as controller of \"hello\"."
assert_command dfx canister update-settings hello --controller juana
assert_match "Updated \"juana\" as controller of \"hello\"."

# Juana is controller, Jose cannot reinstall
assert_command_fail dfx canister install hello -m reinstall
Expand All @@ -51,22 +51,22 @@ teardown() {

assert_command dfx identity use juana
# Set controller using canister id and principal
assert_command dfx canister set-controller ${ID} ${JOSE_PRINCIPAL}
assert_match "Set \"${JOSE_PRINCIPAL}\" as controller of \"${ID}\"."
assert_command dfx canister update-settings ${ID} --controller ${JOSE_PRINCIPAL}
assert_match "Updated \"${JOSE_PRINCIPAL}\" as controller of \"${ID}\"."
assert_command_fail dfx canister install hello -m reinstall

# Set controller using combination of name/id and identity/principal
assert_command dfx --identity jose canister set-controller hello ${JUANA_PRINCIPAL}
assert_match "Set \"${JUANA_PRINCIPAL}\" as controller of \"hello\"."
assert_command dfx --identity jose canister update-settings hello --controller ${JUANA_PRINCIPAL}
assert_match "Updated \"${JUANA_PRINCIPAL}\" as controller of \"hello\"."

assert_command dfx --identity juana canister set-controller ${ID} jose
assert_match "Set \"jose\" as controller of \"${ID}\"."
assert_command dfx --identity juana canister update-settings ${ID} --controller jose
assert_match "Updated \"jose\" as controller of \"${ID}\"."

# Set controller using invalid principal/identity fails
assert_command_fail dfx --identity jose canister set-controller hello bob
assert_command_fail dfx --identity jose canister update-settings hello --controller bob
assert_match "Identity bob does not exist"

# Set controller using invalid canister name/id fails
assert_command_fail dfx --identity jose canister set-controller hello_assets juana
assert_command_fail dfx --identity jose canister update-settings hello_assets --controller juana
assert_match "Cannot find canister id. Please issue 'dfx canister create hello_assets'."
}
4 changes: 2 additions & 2 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
},
"ic-ref": {
"repo": "ssh://git@github.com/dfinity-lab/ic-ref",
"rev": "4f4e3a151b621b902e44e18a860c20493698abc4",
"tag": "0.14.0",
"rev": "54e08f4e65c49f59caf7b18bd8874275436874e8",
"branch": "joachim/impl-settings",
"type": "git"
},
"motoko": {
Expand Down
16 changes: 8 additions & 8 deletions src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,26 @@ wasmparser = "0.45.0"
[dependencies.ic-agent]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[dependencies.ic-identity-hsm]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[dependencies.ic-types]
version = "0.1.2"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[dependencies.ic-utils]
version = "0.1.0"
git = "https://github.com/dfinity/agent-rs.git"
branch = "next"
rev = "5d6ed55236641c136c941707c751b020822cee16"
branch = "pshahi/update-create-settings"
rev = "7e098b2bb4877c98f31588acc7715c19eb50cc9a"

[dev-dependencies]
env_logger = "0.6"
Expand Down
104 changes: 100 additions & 4 deletions src/dfx/src/commands/canister/create.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::config::dfinity::ConfigInterface;
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::identity::identity_manager::IdentityManager;
use crate::lib::operations::canister::create_canister;
use crate::lib::root_key::fetch_root_key_if_needed;
use crate::util::clap::validators::{compute_allocation_validator, memory_allocation_validator};
use crate::util::expiry_duration;

use anyhow::bail;
use anyhow::{anyhow, bail};
use clap::Clap;
use humanize_rs::bytes::Bytes;
use ic_types::principal::Principal as CanisterId;
use ic_utils::interfaces::management_canister::attributes::{ComputeAllocation, MemoryAllocation};
use std::convert::TryFrom;

/// Creates an empty canister on the Internet Computer and
/// associates the Internet Computer assigned Canister ID to the canister name.
Expand All @@ -17,21 +24,110 @@ pub struct CanisterCreateOpts {
/// Creates all canisters configured in dfx.json.
#[clap(long, required_unless_present("canister-name"))]
all: bool,

/// Specifies the identity name or the principal of the new controller.
controller: Option<String>,

/// Specifies the canister's compute allocation. This should be a percent in the range [0..100]
#[clap(long, short('c'), validator(compute_allocation_validator))]
compute_allocation: Option<String>,

/// Specifies how much memory the canister is allowed to use in total.
/// This should be a value in the range [0..256 TB]
#[clap(long, validator(memory_allocation_validator))]
memory_allocation: Option<String>,
}

fn get_compute_allocation(
compute_allocation: Option<String>,
config_interface: &ConfigInterface,
canister_name: &str,
) -> DfxResult<Option<ComputeAllocation>> {
Ok(compute_allocation
.or(config_interface.get_compute_allocation(canister_name)?)
.map(|arg| {
ComputeAllocation::try_from(arg.parse::<u64>().unwrap())
.expect("Compute Allocation must be a percentage.")
}))
}

fn get_memory_allocation(
memory_allocation: Option<String>,
config_interface: &ConfigInterface,
canister_name: &str,
) -> DfxResult<Option<MemoryAllocation>> {
Ok(memory_allocation
.or(config_interface.get_memory_allocation(canister_name)?)
.map(|arg| {
MemoryAllocation::try_from(u64::try_from(arg.parse::<Bytes>().unwrap().size()).unwrap())
.expect("Memory allocation must be between 0 and 2^48 (i.e 256TB), inclusively.")
}))
}

pub async fn exec(env: &dyn Environment, opts: CanisterCreateOpts) -> DfxResult {
let config = env.get_config_or_anyhow()?;
let timeout = expiry_duration();

fetch_root_key_if_needed(env).await?;
let config_interface = config.get_config();

let controller = if let Some(controller) = opts.controller {
match CanisterId::from_text(controller.clone()) {
Ok(principal) => Some(principal),
Err(_) => Some(
IdentityManager::new(env)?
.instantiate_identity_from_name(controller.as_str())?
.sender()
.map_err(|err| anyhow!(err))?,
),
}
} else {
None
};

if let Some(canister_name) = opts.canister_name.clone() {
create_canister(env, canister_name.as_str(), timeout).await
if let Some(canister_name) = opts.canister_name.as_deref() {
let compute_allocation = get_compute_allocation(
opts.compute_allocation.clone(),
config_interface,
canister_name,
)?;
let memory_allocation = get_memory_allocation(
opts.memory_allocation.clone(),
config_interface,
canister_name,
)?;
create_canister(
env,
canister_name,
timeout,
controller,
compute_allocation,
memory_allocation,
)
.await
} else if opts.all {
// Create all canisters.
if let Some(canisters) = &config.get_config().canisters {
for canister_name in canisters.keys() {
create_canister(env, canister_name, timeout).await?;
let compute_allocation = get_compute_allocation(
opts.compute_allocation.clone(),
config_interface,
canister_name,
)?;
let memory_allocation = get_memory_allocation(
opts.memory_allocation.clone(),
config_interface,
canister_name,
)?;
create_canister(
env,
canister_name,
timeout,
controller.clone(),
compute_allocation,
memory_allocation,
)
.await?;
}
}
Ok(())
Expand Down
88 changes: 3 additions & 85 deletions src/dfx/src/commands/canister/install.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::config::dfinity::ConfigInterface;
use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::operations::canister::install_canister;
use crate::lib::root_key::fetch_root_key_if_needed;
use crate::util::clap::validators::{compute_allocation_validator, memory_allocation_validator};
use crate::util::{blob_from_arguments, expiry_duration, get_candid_init_type};

use anyhow::{anyhow, bail};
use clap::Clap;
use humanize_rs::bytes::Bytes;
use ic_utils::interfaces::management_canister::{ComputeAllocation, InstallMode, MemoryAllocation};
use std::convert::TryFrom;
use ic_utils::interfaces::management_canister::builders::InstallMode;
use std::str::FromStr;

/// Deploys compiled code as a canister on the Internet Computer.
Expand Down Expand Up @@ -41,41 +37,6 @@ pub struct CanisterInstallOpts {
/// Specifies the data type for the argument when making the call using an argument.
#[clap(long, requires("argument"), possible_values(&["idl", "raw"]))]
argument_type: Option<String>,

/// Specifies the canister's compute allocation. This should be a percent in the range [0..100]
#[clap(long, short('c'), validator(compute_allocation_validator))]
compute_allocation: Option<String>,

/// Specifies how much memory the canister is allowed to use in total.
/// This should be a value in the range [0..256 TB]
#[clap(long, validator(memory_allocation_validator))]
memory_allocation: Option<String>,
}

fn get_compute_allocation(
compute_allocation: Option<String>,
config_interface: &ConfigInterface,
canister_name: &str,
) -> DfxResult<Option<ComputeAllocation>> {
Ok(compute_allocation
.or(config_interface.get_compute_allocation(canister_name)?)
.map(|arg| {
ComputeAllocation::try_from(arg.parse::<u64>().unwrap())
.expect("Compute Allocation must be a percentage.")
}))
}

fn get_memory_allocation(
memory_allocation: Option<String>,
config_interface: &ConfigInterface,
canister_name: &str,
) -> DfxResult<Option<MemoryAllocation>> {
Ok(memory_allocation
.or(config_interface.get_memory_allocation(canister_name)?)
.map(|arg| {
MemoryAllocation::try_from(u64::try_from(arg.parse::<Bytes>().unwrap().size()).unwrap())
.expect("Memory allocation must be between 0 and 2^48 (i.e 256TB), inclusively.")
}))
}

pub async fn exec(env: &dyn Environment, opts: CanisterInstallOpts) -> DfxResult {
Expand All @@ -87,7 +48,6 @@ pub async fn exec(env: &dyn Environment, opts: CanisterInstallOpts) -> DfxResult

fetch_root_key_if_needed(env).await?;

let config_interface = config.get_config();
let mode = InstallMode::from_str(opts.mode.as_str()).map_err(|err| anyhow!(err))?;
let canister_id_store = CanisterIdStore::for_env(env)?;

Expand All @@ -101,28 +61,7 @@ pub async fn exec(env: &dyn Environment, opts: CanisterInstallOpts) -> DfxResult
let arg_type = opts.argument_type.as_deref();
let install_args = blob_from_arguments(arguments, arg_type, &init_type)?;

let compute_allocation = get_compute_allocation(
opts.compute_allocation.clone(),
config_interface,
canister_name,
)?;
let memory_allocation = get_memory_allocation(
opts.memory_allocation.clone(),
config_interface,
canister_name,
)?;

install_canister(
env,
&agent,
&canister_info,
&install_args,
compute_allocation,
mode,
memory_allocation,
timeout,
)
.await
install_canister(env, &agent, &canister_info, &install_args, mode, timeout).await
} else if opts.all {
// Install all canisters.
if let Some(canisters) = &config.get_config().canisters {
Expand All @@ -132,28 +71,7 @@ pub async fn exec(env: &dyn Environment, opts: CanisterInstallOpts) -> DfxResult

let install_args = [];

let compute_allocation = get_compute_allocation(
opts.compute_allocation.clone(),
config_interface,
canister_name,
)?;
let memory_allocation = get_memory_allocation(
opts.memory_allocation.clone(),
config_interface,
canister_name,
)?;

install_canister(
env,
&agent,
&canister_info,
&install_args,
compute_allocation,
mode,
memory_allocation,
timeout,
)
.await?;
install_canister(env, &agent, &canister_info, &install_args, mode, timeout).await?;
}
}
Ok(())
Expand Down
Loading