Skip to content

Commit

Permalink
Merge branch 'master' into vincent/SDK-1844
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-dfinity authored Nov 12, 2024
2 parents 47d62d7 + 6af08b9 commit 3dedb9d
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 19 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# UNRELEASED

### chore: update agent version in frontend templates, and include `resolve.dedupe` in Vite config

### chore: improve error message when trying to use the local replica when it is not running

### Frontend canister
Expand All @@ -16,6 +18,16 @@ Allow setting permissions lists in init arguments just like in upgrade arguments
- Module hash: f45db224b40fac516c877e3108dc809d4b22fa42d05ee8dfa5002536a3a3daed
- Bump agent-js to fix error code

### chore!: improve the messages for the subcommands of `dfx cycles`.

If users run subcommands of `dfx cycles` without the `--ic` flag, show below messages to indicate what to do next.
```
Error explanation:
Cycles ledger with canister ID 'um5iw-rqaaa-aaaaq-qaaba-cai' is not installed.
How to resolve the error:
Run the command with '--ic' flag if you want to manage the cycles on the mainnet.
```

### chore: improve `dfx start` messages.

For `dfx start`, show below messages to users to indicate what to do next.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions e2e/tests-dfx/cycles-ledger.bash
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ current_time_nanoseconds() {
assert_eq "2.900 TC (trillion cycles)."
}

@test "balance without cycles ledger fails as expected" {
dfx_start

assert_command_fail dfx cycles balance
assert_contains "Cycles ledger with canister ID 'um5iw-rqaaa-aaaaq-qaaba-cai' is not installed."
}

@test "transfer" {
start_and_install_nns

Expand Down
19 changes: 19 additions & 0 deletions src/dfx-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.1] - 2024-11-08

### Added

- get_version_from_cache_path function to get the version from the cache path
- DfxInterfaceBuilder: with_extension_manager and with_extension_manager_from_cache_path methods

## [0.1.0] - 2024-09-01

Initial Version
2 changes: 1 addition & 1 deletion src/dfx-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dfx-core"
version = "0.1.0"
version = "0.1.1"
authors.workspace = true
edition.workspace = true
repository.workspace = true
Expand Down
19 changes: 17 additions & 2 deletions src/dfx-core/src/config/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use crate::config::directories::project_dirs;
use crate::error::cache::{
DeleteCacheError, EnsureCacheVersionsDirError, GetBinaryCommandPathError, GetCacheRootError,
IsCacheInstalledError, ListCacheVersionsError,
GetVersionFromCachePathError, IsCacheInstalledError, ListCacheVersionsError,
};
#[cfg(not(windows))]
use crate::foundation::get_user_home;
use crate::fs::composite::ensure_dir_exists;
use semver::Version;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

pub trait Cache {
fn version_str(&self) -> String;
Expand Down Expand Up @@ -50,6 +50,21 @@ pub fn get_cache_path_for_version(v: &str) -> Result<PathBuf, GetCacheRootError>
Ok(p)
}

pub fn get_version_from_cache_path(
cache_path: &Path,
) -> Result<Version, GetVersionFromCachePathError> {
let version = cache_path
.file_name()
.ok_or(GetVersionFromCachePathError::NoCachePathFilename(
cache_path.to_path_buf(),
))?
.to_str()
.ok_or(GetVersionFromCachePathError::CachePathFilenameNotUtf8(
cache_path.to_path_buf(),
))?;
Ok(Version::parse(version)?)
}

/// Return the binary cache root. It constructs it if not present
/// already.
pub fn ensure_cache_versions_dir() -> Result<PathBuf, EnsureCacheVersionsDirError> {
Expand Down
13 changes: 13 additions & 0 deletions src/dfx-core/src/error/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::fs::{
};
use crate::error::get_current_exe::GetCurrentExeError;
use crate::error::get_user_home::GetUserHomeError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -34,6 +35,18 @@ pub enum EnsureCacheVersionsDirError {
GetCacheRoot(#[from] GetCacheRootError),
}

#[derive(Error, Debug)]
pub enum GetVersionFromCachePathError {
#[error("no filename in cache path '{0}'")]
NoCachePathFilename(PathBuf),

#[error("filename in cache path '{0}' is not valid UTF-8")]
CachePathFilenameNotUtf8(PathBuf),

#[error("cannot parse version from cache path filename")]
ParseVersion(#[from] semver::Error),
}

#[derive(Error, Debug)]
pub enum GetCacheRootError {
#[error(transparent)]
Expand Down
12 changes: 12 additions & 0 deletions src/dfx-core/src/error/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::error::cache::GetVersionFromCachePathError;
use crate::error::extension::NewExtensionManagerError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum NewExtensionManagerFromCachePathError {
#[error(transparent)]
GetVersionFromCachePath(#[from] GetVersionFromCachePathError),

#[error(transparent)]
NewExtensionManager(#[from] NewExtensionManagerError),
}
1 change: 1 addition & 0 deletions src/dfx-core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod fs;
pub mod get_current_exe;
pub mod get_user_home;
pub mod identity;
pub mod interface;
pub mod keyring;
pub mod load_dfx_config;
pub mod load_networks_config;
Expand Down
33 changes: 31 additions & 2 deletions src/dfx-core/src/interface/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::{
config::cache::get_version_from_cache_path,
config::model::{
dfinity::{Config, NetworksConfig},
network_descriptor::NetworkDescriptor,
},
error::{
builder::{BuildAgentError, BuildDfxInterfaceError, BuildIdentityError},
extension::NewExtensionManagerError,
interface::NewExtensionManagerFromCachePathError,
network_config::NetworkConfigError,
},
extension::manager::ExtensionManager,
identity::{identity_manager::InitializeIdentity, IdentityManager},
network::{
provider::{create_network_descriptor, LocalBindDetermination},
Expand All @@ -16,6 +20,8 @@ use crate::{
};
use ic_agent::{agent::route_provider::RoundRobinRouteProvider, Agent, Identity};
use reqwest::Client;
use semver::Version;
use std::path::Path;
use std::sync::Arc;

#[derive(PartialEq)]
Expand All @@ -42,14 +48,17 @@ pub struct DfxInterfaceBuilder {
/// There is no need to set this for the local network, where the root key is fetched by default.
/// This would typically be set for a testnet, or an alias for the local network.
force_fetch_root_key_insecure_non_mainnet_only: bool,

extension_manager: Option<ExtensionManager>,
}

impl DfxInterfaceBuilder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
identity: IdentityPicker::Selected,
network: NetworkPicker::Local,
force_fetch_root_key_insecure_non_mainnet_only: false,
extension_manager: None,
}
}

Expand Down Expand Up @@ -77,6 +86,26 @@ impl DfxInterfaceBuilder {
self.with_network(NetworkPicker::Named(name.to_string()))
}

pub fn with_extension_manager(
self,
version: Version,
) -> Result<Self, NewExtensionManagerError> {
let extension_manager = Some(ExtensionManager::new(&version)?);
Ok(Self {
extension_manager,
..self
})
}

pub fn with_extension_manager_from_cache_path(
self,
cache_path: &Path,
) -> Result<Self, NewExtensionManagerFromCachePathError> {
let version = get_version_from_cache_path(cache_path)?;

Ok(self.with_extension_manager(version)?)
}

pub fn with_force_fetch_root_key_insecure_non_mainnet_only(self) -> Self {
Self {
force_fetch_root_key_insecure_non_mainnet_only: true,
Expand All @@ -88,7 +117,7 @@ impl DfxInterfaceBuilder {
let fetch_root_key = self.network == NetworkPicker::Local
|| self.force_fetch_root_key_insecure_non_mainnet_only;
let networks_config = NetworksConfig::new()?;
let config = Config::from_current_dir(None)?.map(Arc::new);
let config = Config::from_current_dir(self.extension_manager.as_ref())?.map(Arc::new);
let network_descriptor = self.build_network_descriptor(config.clone(), &networks_config)?;
let identity = self.build_identity()?;
let agent = self.build_agent(identity.clone(), &network_descriptor)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0"
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3"
},
"devDependencies": {
"@types/react": "^18.2.14",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ export default defineConfig({
),
},
],
dedupe: ['@dfinity/agent'],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"format": "prettier --write \"src/**/*.{json,js,jsx,ts,tsx,css,scss}\""
},
"dependencies": {
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0"
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3"
},
"devDependencies": {
"@sveltejs/adapter-static": "^3.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ export default defineConfig({
),
},
],
dedupe: ['@dfinity/agent'],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"vitest": "^2.0.5"
},
"dependencies": {
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0",
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3",
"lit-html": "^2.8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export default defineConfig({
),
},
],
dedupe: ['@dfinity/agent'],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"dependencies": {
"pinia": "^2.1.6",
"vue": "^3.3.4",
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0"
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default defineConfig({
alias: [
{ find: 'declarations', replacement: fileURLToPath(new URL('../declarations', import.meta.url)) },
{ find: '@', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
]
],
dedupe: ['@dfinity/agent'],
}
});
17 changes: 17 additions & 0 deletions src/dfx/src/lib/diagnosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub fn diagnose(err: &AnyhowError) -> Diagnosis {
} else if *agent_err == AgentError::CertificateNotAuthorized() {
return subnet_not_authorized();
}
if cycles_ledger_not_found(err) {
return diagnose_cycles_ledger_not_found();
}
}

if local_replica_not_running(err) {
Expand Down Expand Up @@ -229,3 +232,17 @@ If you're using a local replica and configuring a wallet was a mistake, you can
recreate the replica with `dfx stop && dfx start --clean` to start over.";
(Some(explanation.to_string()), Some(suggestion.to_string()))
}

fn cycles_ledger_not_found(err: &AnyhowError) -> bool {
err.to_string()
.contains("Canister um5iw-rqaaa-aaaaq-qaaba-cai not found")
}

fn diagnose_cycles_ledger_not_found() -> Diagnosis {
let explanation =
"Cycles ledger with canister ID 'um5iw-rqaaa-aaaaq-qaaba-cai' is not installed.";
let suggestion =
"Run the command with '--ic' flag if you want to manage the cycles on the mainnet.";

(Some(explanation.to_string()), Some(suggestion.to_string()))
}

0 comments on commit 3dedb9d

Please sign in to comment.