Skip to content

Commit 6983dc7

Browse files
author
Severin Siffert
authored
Merge pull request #72 from dfinity/severin/cmc-knows-about-cycles-ledger
feat: configure CMC to play nice with cycles ledger
2 parents 1eec50b + e523796 commit 6983dc7

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

extensions-utils/src/dependencies/download_wasms/nns.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ pub const NNS_DAPP: StandardCanister = StandardCanister {
152152
wasm_name: "nns-dapp_local.wasm",
153153
wasm_url: "https://github.com/dfinity/nns-dapp/releases/download/tip/nns-dapp_t2.wasm",
154154
};
155+
/// Canister that can be used instead of cycle wallets
156+
pub const CYCLES_LEDGER: StandardCanister = StandardCanister {
157+
canister_name: "cycles_ledger",
158+
canister_id: "um5iw-rqaaa-aaaaq-qaaba-cai",
159+
wasm_name: "cycles_ledger.wasm.gz",
160+
wasm_url: "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v0.2.8/cycles-ledger.wasm.gz",
161+
};
155162
/// Backend canisters deployed by `ic nns init`.
156163
pub const NNS_CORE: &[&IcNnsInitCanister; 11] = &[
157164
&NNS_REGISTRY, // 0

extensions/nns/src/install_nns.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use dfx_core::canister::install_canister_wasm;
1010
use dfx_core::config::model::dfinity::{NetworksConfig, ReplicaSubnetType};
1111
use dfx_core::config::model::network_descriptor::NetworkDescriptor;
1212
use dfx_core::identity::CallSender;
13+
use dfx_extensions_utils::dependencies::download_wasms::nns::{CYCLES_LEDGER, NNS_CYCLES_MINTING};
1314
use dfx_extensions_utils::{
1415
call_extension_bundled_binary, download_nns_wasms, nns_wasm_dir, IcNnsInitCanister,
1516
SnsCanisterInstallation, StandardCanister, ED25519_TEST_ACCOUNT, NNS_CORE, NNS_FRONTEND,
@@ -19,14 +20,15 @@ use dfx_extensions_utils::{
1920
use anyhow::{anyhow, bail, Context};
2021
use backoff::backoff::Backoff;
2122
use backoff::ExponentialBackoff;
22-
use candid::Encode;
23+
use candid::{CandidType, Encode};
2324
use fn_error_context::context;
2425
use futures_util::future::try_join_all;
2526
use ic_agent::export::Principal;
2627
use ic_agent::Agent;
2728
use ic_utils::interfaces::management_canister::builders::InstallMode;
2829
use ic_utils::interfaces::ManagementCanister;
2930
use reqwest::Url;
31+
use sha2::{Digest, Sha256};
3032
use slog::Logger;
3133
use std::ffi::OsString;
3234
use std::fs;
@@ -115,6 +117,7 @@ pub async fn install_nns(
115117
eprintln!("Configuring the NNS...");
116118
set_xdr_rate(1234567, &nns_url, dfx_cache_path)?;
117119
set_cmc_authorized_subnets(&nns_url, &subnet_id, dfx_cache_path)?;
120+
set_cycles_ledger_canister_id_in_cmc(&nns_url, dfx_cache_path)?;
118121

119122
print_nns_details(provider_url)?;
120123
Ok(())
@@ -394,7 +397,7 @@ pub async fn download(source: &Url, target: &Path) -> anyhow::Result<()> {
394397

395398
/// Arguments for the ic-nns-init command line function.
396399
pub struct IcNnsInitOpts {
397-
/// An URL to accees one or more NNS subnet replicas.
400+
/// An URL to access one or more NNS subnet replicas.
398401
nns_url: String,
399402
/// A directory that needs to be populated will all required wasms before calling ic-nns-init.
400403
wasm_dir: PathBuf,
@@ -483,6 +486,60 @@ pub fn set_cmc_authorized_subnets(
483486
.map_err(|e| anyhow!("Call to propose to set authorized subnets failed: {e}"))
484487
}
485488

489+
/// Sets the cycles ledger canister id in the CMC
490+
#[context("Failed to set the cycles ledger canister id in the CMC")]
491+
pub fn set_cycles_ledger_canister_id_in_cmc(
492+
nns_url: &Url,
493+
dfx_cache_path: &Path,
494+
) -> anyhow::Result<()> {
495+
#[derive(CandidType, Clone, Debug, PartialEq, Eq)]
496+
struct CyclesCanisterInitPayload {
497+
pub cycles_ledger_canister_id: Option<Principal>,
498+
}
499+
500+
let wasm_path = nns_wasm_dir(dfx_cache_path);
501+
let cmc_wasm_path = wasm_path.join(NNS_CYCLES_MINTING.wasm_name);
502+
let cmc_wasm_bytes = dfx_core::fs::read(&cmc_wasm_path)?;
503+
let wasm_hash = Sha256::digest(cmc_wasm_bytes);
504+
let mut upgrade_arg_file = tempfile::NamedTempFile::new()?;
505+
upgrade_arg_file
506+
.write_all(
507+
&Encode!(&(Some(CyclesCanisterInitPayload {
508+
cycles_ledger_canister_id: Some(
509+
Principal::from_text(CYCLES_LEDGER.canister_id).unwrap()
510+
),
511+
}),))
512+
.unwrap(),
513+
)
514+
.context("Failed to write to tempfile.")?;
515+
516+
let cmc_wasm_path_str = cmc_wasm_path.to_string_lossy();
517+
let wasm_hash_str = hex::encode(wasm_hash);
518+
let upgrade_arg_file_str = upgrade_arg_file.path().to_string_lossy();
519+
let args = vec![
520+
"--nns-url",
521+
nns_url.as_str(),
522+
"propose-to-change-nns-canister",
523+
"--test-neuron-proposer",
524+
"--proposal-title",
525+
"Set cycles ledger canister id in Cycles Minting Canister",
526+
"--summary",
527+
"Set cycles ledger canister id in Cycles Minting Canister",
528+
"--mode",
529+
"upgrade",
530+
"--canister-id",
531+
NNS_CYCLES_MINTING.canister_id,
532+
"--wasm-module-path",
533+
&cmc_wasm_path_str,
534+
"--wasm-module-sha256",
535+
&wasm_hash_str,
536+
"--arg",
537+
&upgrade_arg_file_str,
538+
];
539+
call_extension_bundled_binary("ic-admin", args, dfx_cache_path)
540+
.map_err(|e| anyhow!("Call to set the cycles ledger canister id in the CMC: {e}"))
541+
}
542+
486543
/// Uploads wasms to the nns-sns-wasm canister.
487544
#[context("Failed to upload wasm files to the nns-sns-wasm canister; it may not be possible to create an SNS.")]
488545
pub fn upload_nns_sns_wasms_canister_wasms(dfx_cache_path: &Path) -> anyhow::Result<()> {

0 commit comments

Comments
 (0)