From 551ade3402e4fcaf9190c23a7c1a9cf139b97787 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 10:36:20 +0100 Subject: [PATCH 1/6] feat: configure CMC to play nice with cycles ledger --- .../src/dependencies/download_wasms/nns.rs | 7 +++ extensions/nns/src/install_nns.rs | 62 ++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/extensions-utils/src/dependencies/download_wasms/nns.rs b/extensions-utils/src/dependencies/download_wasms/nns.rs index 06db139..6647a6b 100644 --- a/extensions-utils/src/dependencies/download_wasms/nns.rs +++ b/extensions-utils/src/dependencies/download_wasms/nns.rs @@ -152,6 +152,13 @@ pub const NNS_DAPP: StandardCanister = StandardCanister { wasm_name: "nns-dapp_local.wasm", wasm_url: "https://github.com/dfinity/nns-dapp/releases/download/tip/nns-dapp_t2.wasm", }; +/// Canister that can be used instead of cycle wallets +pub const CYCLES_LEDGER: StandardCanister = StandardCanister { + canister_name: "cycles_ledger", + canister_id: "um5iw-rqaaa-aaaaq-qaaba-cai", + wasm_name: "cycles_ledger.wasm.gz", + wasm_url: "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v0.2.8/cycles-ledger.wasm.gz", +}; /// Backend canisters deployed by `ic nns init`. pub const NNS_CORE: &[&IcNnsInitCanister; 11] = &[ &NNS_REGISTRY, // 0 diff --git a/extensions/nns/src/install_nns.rs b/extensions/nns/src/install_nns.rs index 5041141..22ac2a1 100644 --- a/extensions/nns/src/install_nns.rs +++ b/extensions/nns/src/install_nns.rs @@ -10,6 +10,7 @@ use dfx_core::canister::install_canister_wasm; use dfx_core::config::model::dfinity::{NetworksConfig, ReplicaSubnetType}; use dfx_core::config::model::network_descriptor::NetworkDescriptor; use dfx_core::identity::CallSender; +use dfx_extensions_utils::dependencies::download_wasms::nns::{CYCLES_LEDGER, NNS_CYCLES_MINTING}; use dfx_extensions_utils::{ call_extension_bundled_binary, download_nns_wasms, nns_wasm_dir, IcNnsInitCanister, SnsCanisterInstallation, StandardCanister, ED25519_TEST_ACCOUNT, NNS_CORE, NNS_FRONTEND, @@ -19,7 +20,7 @@ use dfx_extensions_utils::{ use anyhow::{anyhow, bail, Context}; use backoff::backoff::Backoff; use backoff::ExponentialBackoff; -use candid::Encode; +use candid::{CandidType, Encode}; use fn_error_context::context; use futures_util::future::try_join_all; use ic_agent::export::Principal; @@ -27,6 +28,7 @@ use ic_agent::Agent; use ic_utils::interfaces::management_canister::builders::InstallMode; use ic_utils::interfaces::ManagementCanister; use reqwest::Url; +use sha2::{Digest, Sha256}; use slog::Logger; use std::ffi::OsString; use std::fs; @@ -115,6 +117,7 @@ pub async fn install_nns( eprintln!("Configuring the NNS..."); set_xdr_rate(1234567, &nns_url, dfx_cache_path)?; set_cmc_authorized_subnets(&nns_url, &subnet_id, dfx_cache_path)?; + set_cycles_ledger_canister_id_in_cmc(&nns_url, dfx_cache_path)?; print_nns_details(provider_url)?; Ok(()) @@ -394,7 +397,7 @@ pub async fn download(source: &Url, target: &Path) -> anyhow::Result<()> { /// Arguments for the ic-nns-init command line function. pub struct IcNnsInitOpts { - /// An URL to accees one or more NNS subnet replicas. + /// An URL to access one or more NNS subnet replicas. nns_url: String, /// A directory that needs to be populated will all required wasms before calling ic-nns-init. wasm_dir: PathBuf, @@ -483,6 +486,61 @@ pub fn set_cmc_authorized_subnets( .map_err(|e| anyhow!("Call to propose to set authorized subnets failed: {e}")) } +/// Sets the cycles ledger canister id in the CMC +#[context("Failed to set the cycles ledger canister id in the CMC")] +pub fn set_cycles_ledger_canister_id_in_cmc( + nns_url: &Url, + dfx_cache_path: &Path, +) -> anyhow::Result<()> { + #[derive(CandidType, Clone, Debug, PartialEq, Eq)] + struct CyclesCanisterInitPayload { + pub cycles_ledger_canister_id: Option, + } + + let wasm_path = nns_wasm_dir(dfx_cache_path); + let ledger_wasm_path = wasm_path.join(NNS_CYCLES_MINTING.wasm_name); + let ledger_wasm_bytes = std::fs::read(&ledger_wasm_path) + .with_context(|| format!("Failed to read {}", ledger_wasm_path.to_string_lossy()))?; + let wasm_hash = Sha256::digest(ledger_wasm_bytes); + let mut upgrade_arg_file = tempfile::NamedTempFile::new()?; + upgrade_arg_file + .write_all( + &Encode!(&(Some(CyclesCanisterInitPayload { + cycles_ledger_canister_id: Some( + Principal::from_text(CYCLES_LEDGER.canister_id).unwrap() + ), + }),)) + .unwrap(), + ) + .unwrap(); + + let ledger_wasm_path_str = ledger_wasm_path.to_string_lossy(); + let wasm_hash_str = hex::encode(wasm_hash); + let upgrade_arg_file_str = upgrade_arg_file.path().to_string_lossy(); + let args = vec![ + "--nns-url", + nns_url.as_str(), + "propose-to-change-nns-canister", + "--test-neuron-proposer", + "--proposal-title", + "Set cycles ledger canister id in Cycles Minting Canister", + "--summary", + "Set cycles ledger canister id in Cycles Minting Canister", + "--mode", + "upgrade", + "--canister-id", + NNS_CYCLES_MINTING.canister_id, + "--wasm-module-path", + &ledger_wasm_path_str, + "--wasm-module-sha256", + &wasm_hash_str, + "--arg", + &upgrade_arg_file_str, + ]; + call_extension_bundled_binary("ic-admin", args, dfx_cache_path) + .map_err(|e| anyhow!("Call to propose to set authorized subnets failed: {e}")) +} + /// Uploads wasms to the nns-sns-wasm canister. #[context("Failed to upload wasm files to the nns-sns-wasm canister; it may not be possible to create an SNS.")] pub fn upload_nns_sns_wasms_canister_wasms(dfx_cache_path: &Path) -> anyhow::Result<()> { From a4dc34c27034b96580c17146a8e916abcde7b071 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 13:17:43 +0100 Subject: [PATCH 2/6] Update extensions/nns/src/install_nns.rs Co-authored-by: Eric Swanson <64809312+ericswanson-dfinity@users.noreply.github.com> --- extensions/nns/src/install_nns.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extensions/nns/src/install_nns.rs b/extensions/nns/src/install_nns.rs index 22ac2a1..afee698 100644 --- a/extensions/nns/src/install_nns.rs +++ b/extensions/nns/src/install_nns.rs @@ -499,8 +499,7 @@ pub fn set_cycles_ledger_canister_id_in_cmc( let wasm_path = nns_wasm_dir(dfx_cache_path); let ledger_wasm_path = wasm_path.join(NNS_CYCLES_MINTING.wasm_name); - let ledger_wasm_bytes = std::fs::read(&ledger_wasm_path) - .with_context(|| format!("Failed to read {}", ledger_wasm_path.to_string_lossy()))?; + let ledger_wasm_bytes = dfx_core::fs::read(&ledger_wasm_path)?; let wasm_hash = Sha256::digest(ledger_wasm_bytes); let mut upgrade_arg_file = tempfile::NamedTempFile::new()?; upgrade_arg_file From 5a66c6f8ea4194849b080a7032f15f2ed85f0970 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 13:46:44 +0100 Subject: [PATCH 3/6] cycles ledger now has a latest url --- extensions-utils/src/dependencies/download_wasms/nns.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions-utils/src/dependencies/download_wasms/nns.rs b/extensions-utils/src/dependencies/download_wasms/nns.rs index 6647a6b..0baf646 100644 --- a/extensions-utils/src/dependencies/download_wasms/nns.rs +++ b/extensions-utils/src/dependencies/download_wasms/nns.rs @@ -157,7 +157,7 @@ pub const CYCLES_LEDGER: StandardCanister = StandardCanister { canister_name: "cycles_ledger", canister_id: "um5iw-rqaaa-aaaaq-qaaba-cai", wasm_name: "cycles_ledger.wasm.gz", - wasm_url: "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v0.2.8/cycles-ledger.wasm.gz", + wasm_url: "https://github.com/dfinity/cycles-ledger/releases/latest/download/cycles-ledger.wasm.gz", }; /// Backend canisters deployed by `ic nns init`. pub const NNS_CORE: &[&IcNnsInitCanister; 11] = &[ From 538bf58610d3c01979643c329f0d3f4a57a63f23 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 17:28:33 +0100 Subject: [PATCH 4/6] Revert "cycles ledger now has a latest url" This reverts commit 5a66c6f8ea4194849b080a7032f15f2ed85f0970. --- extensions-utils/src/dependencies/download_wasms/nns.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions-utils/src/dependencies/download_wasms/nns.rs b/extensions-utils/src/dependencies/download_wasms/nns.rs index 0baf646..6647a6b 100644 --- a/extensions-utils/src/dependencies/download_wasms/nns.rs +++ b/extensions-utils/src/dependencies/download_wasms/nns.rs @@ -157,7 +157,7 @@ pub const CYCLES_LEDGER: StandardCanister = StandardCanister { canister_name: "cycles_ledger", canister_id: "um5iw-rqaaa-aaaaq-qaaba-cai", wasm_name: "cycles_ledger.wasm.gz", - wasm_url: "https://github.com/dfinity/cycles-ledger/releases/latest/download/cycles-ledger.wasm.gz", + wasm_url: "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v0.2.8/cycles-ledger.wasm.gz", }; /// Backend canisters deployed by `ic nns init`. pub const NNS_CORE: &[&IcNnsInitCanister; 11] = &[ From 57b9b9ab6e027a0a3ab6e1fec583f6ed9b7a03b4 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 17:55:03 +0100 Subject: [PATCH 5/6] remove unwrap, fix error message --- extensions/nns/src/install_nns.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/nns/src/install_nns.rs b/extensions/nns/src/install_nns.rs index afee698..ff5bf0a 100644 --- a/extensions/nns/src/install_nns.rs +++ b/extensions/nns/src/install_nns.rs @@ -511,7 +511,7 @@ pub fn set_cycles_ledger_canister_id_in_cmc( }),)) .unwrap(), ) - .unwrap(); + .context("Failed to write to tempfile.")?; let ledger_wasm_path_str = ledger_wasm_path.to_string_lossy(); let wasm_hash_str = hex::encode(wasm_hash); @@ -537,7 +537,7 @@ pub fn set_cycles_ledger_canister_id_in_cmc( &upgrade_arg_file_str, ]; call_extension_bundled_binary("ic-admin", args, dfx_cache_path) - .map_err(|e| anyhow!("Call to propose to set authorized subnets failed: {e}")) + .map_err(|e| anyhow!("Call to set the cycles ledger canister id in the CMC: {e}")) } /// Uploads wasms to the nns-sns-wasm canister. From 38562335bb8e9063d354eb2fa201bcf3053a3d98 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Tue, 6 Feb 2024 17:58:15 +0100 Subject: [PATCH 6/6] ledger -> cmc --- extensions/nns/src/install_nns.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extensions/nns/src/install_nns.rs b/extensions/nns/src/install_nns.rs index ff5bf0a..7a9a282 100644 --- a/extensions/nns/src/install_nns.rs +++ b/extensions/nns/src/install_nns.rs @@ -498,9 +498,9 @@ pub fn set_cycles_ledger_canister_id_in_cmc( } let wasm_path = nns_wasm_dir(dfx_cache_path); - let ledger_wasm_path = wasm_path.join(NNS_CYCLES_MINTING.wasm_name); - let ledger_wasm_bytes = dfx_core::fs::read(&ledger_wasm_path)?; - let wasm_hash = Sha256::digest(ledger_wasm_bytes); + let cmc_wasm_path = wasm_path.join(NNS_CYCLES_MINTING.wasm_name); + let cmc_wasm_bytes = dfx_core::fs::read(&cmc_wasm_path)?; + let wasm_hash = Sha256::digest(cmc_wasm_bytes); let mut upgrade_arg_file = tempfile::NamedTempFile::new()?; upgrade_arg_file .write_all( @@ -513,7 +513,7 @@ pub fn set_cycles_ledger_canister_id_in_cmc( ) .context("Failed to write to tempfile.")?; - let ledger_wasm_path_str = ledger_wasm_path.to_string_lossy(); + let cmc_wasm_path_str = cmc_wasm_path.to_string_lossy(); let wasm_hash_str = hex::encode(wasm_hash); let upgrade_arg_file_str = upgrade_arg_file.path().to_string_lossy(); let args = vec![ @@ -530,7 +530,7 @@ pub fn set_cycles_ledger_canister_id_in_cmc( "--canister-id", NNS_CYCLES_MINTING.canister_id, "--wasm-module-path", - &ledger_wasm_path_str, + &cmc_wasm_path_str, "--wasm-module-sha256", &wasm_hash_str, "--arg",