From a7f62b1e1db8a12ab8492a5671f0cab3befc61c7 Mon Sep 17 00:00:00 2001 From: Dimitris Sarlis Date: Tue, 28 Apr 2020 14:20:33 +0200 Subject: [PATCH 1/2] Use None instead of 0 if no compute allocation is specified --- src/agent/rust/src/agent/mod.rs | 2 +- src/agent/rust/src/agent/replica_api.rs | 3 ++- .../rust/src/types/canister_attributes.rs | 4 ++-- src/dfx/src/commands/canister/install.rs | 19 +++++++++---------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/agent/rust/src/agent/mod.rs b/src/agent/rust/src/agent/mod.rs index 20c1bf0acc..80691ba7b8 100644 --- a/src/agent/rust/src/agent/mod.rs +++ b/src/agent/rust/src/agent/mod.rs @@ -221,7 +221,7 @@ impl Agent { module, arg, nonce: &self.nonce_factory.generate(), - compute_allocation: attributes.compute_allocation.0, + compute_allocation: attributes.compute_allocation.map(|x| x.into()), }) .await } diff --git a/src/agent/rust/src/agent/replica_api.rs b/src/agent/rust/src/agent/replica_api.rs index 01e81d3870..5ace9449eb 100644 --- a/src/agent/rust/src/agent/replica_api.rs +++ b/src/agent/rust/src/agent/replica_api.rs @@ -47,7 +47,8 @@ pub enum SubmitRequest<'a> { module: &'a Blob, arg: &'a Blob, nonce: &'a Option, - compute_allocation: u8, + #[serde(skip_serializing_if = "Option::is_none")] + compute_allocation: Option, }, Call { canister_id: &'a CanisterId, diff --git a/src/agent/rust/src/types/canister_attributes.rs b/src/agent/rust/src/types/canister_attributes.rs index eccc62cf16..5ee1fbe3d0 100644 --- a/src/agent/rust/src/types/canister_attributes.rs +++ b/src/agent/rust/src/types/canister_attributes.rs @@ -47,13 +47,13 @@ impl std::convert::TryFrom for ComputeAllocation { } pub struct CanisterAttributes { - pub compute_allocation: ComputeAllocation, + pub compute_allocation: Option, } impl Default for CanisterAttributes { fn default() -> Self { CanisterAttributes { - compute_allocation: ComputeAllocation(0), + compute_allocation: None, } } } diff --git a/src/dfx/src/commands/canister/install.rs b/src/dfx/src/commands/canister/install.rs index 3e7b55e110..b065426b4b 100644 --- a/src/dfx/src/commands/canister/install.rs +++ b/src/dfx/src/commands/canister/install.rs @@ -7,7 +7,7 @@ use crate::lib::message::UserMessage; use clap::{App, Arg, ArgMatches, SubCommand}; use ic_agent::{Agent, Blob, CanisterAttributes, ComputeAllocation, RequestId}; use slog::info; -use std::convert::TryInto; +use std::convert::TryFrom; use tokio::runtime::Runtime; pub fn construct() -> App<'static, 'static> { @@ -39,7 +39,6 @@ pub fn construct() -> App<'static, 'static> { .long("compute-allocation") .short("c") .takes_value(true) - .default_value("0") .validator(compute_allocation_validator), ) } @@ -48,7 +47,7 @@ async fn install_canister( env: &dyn Environment, agent: &Agent, canister_info: &CanisterInfo, - compute_allocation: ComputeAllocation, + compute_allocation: Option, ) -> DfxResult { let log = env.get_logger(); let canister_id = canister_info.get_canister_id().ok_or_else(|| { @@ -93,13 +92,13 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult { let agent = env .get_agent() .ok_or(DfxError::CommandMustBeRunInAProject)?; - let compute_allocation: ComputeAllocation = args - .value_of("compute-allocation") - .unwrap_or("0") - .parse::() - .unwrap() - .try_into() - .expect("Compute Allocation must be a percentage."); + let compute_allocation = match args.value_of("compute-allocation") { + Some(compute_allocation_arg) => Some( + ComputeAllocation::try_from(compute_allocation_arg.parse::().unwrap()) + .expect("Compute Allocation must be a percentage."), + ), + None => None, + }; let mut runtime = Runtime::new().expect("Unable to create a runtime"); From 415490043ace13862d2ec83911b8a1b9d9f5c68b Mon Sep 17 00:00:00 2001 From: Dimitris Sarlis Date: Tue, 28 Apr 2020 14:31:49 +0200 Subject: [PATCH 2/2] Use map instead of match --- src/dfx/src/commands/canister/install.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/dfx/src/commands/canister/install.rs b/src/dfx/src/commands/canister/install.rs index b065426b4b..daea360e20 100644 --- a/src/dfx/src/commands/canister/install.rs +++ b/src/dfx/src/commands/canister/install.rs @@ -92,13 +92,10 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult { let agent = env .get_agent() .ok_or(DfxError::CommandMustBeRunInAProject)?; - let compute_allocation = match args.value_of("compute-allocation") { - Some(compute_allocation_arg) => Some( - ComputeAllocation::try_from(compute_allocation_arg.parse::().unwrap()) - .expect("Compute Allocation must be a percentage."), - ), - None => None, - }; + let compute_allocation = args.value_of("compute-allocation").map(|arg| { + ComputeAllocation::try_from(arg.parse::().unwrap()) + .expect("Compute Allocation must be a percentage.") + }); let mut runtime = Runtime::new().expect("Unable to create a runtime");