Skip to content

Commit 904c440

Browse files
Revert "refactor: always fetch did file from canister when making canister calls (#2931)" (#3555)
This reverts commit 935ffc7.
1 parent 32717b8 commit 904c440

File tree

14 files changed

+77
-91
lines changed

14 files changed

+77
-91
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ When using `dfx canister status`, the output now includes the new query statisti
1212

1313
Fix the bug that when parsing `vec {1;2;3}` with `blob` type, dfx silently ignores the numbers.
1414

15-
### fix!: always fetch did file from canister when making canister calls
16-
17-
`dfx canister call` will always fetch did file from the canister metadata. This is especially helpful for calling remote canisters. It's a breaking change in the sense that if the canister doesn't have the `candid:service` metadata, we will not read the local did file from build artifact, and dfx will issue a warning in this case to encourage canister developers to put the did file into canister metadata.
18-
1915
### fix: support `import` for local did file
2016

2117
If the local did file contains `import` or init args, dfx will rewrite the did file when storing in canister metadata.

e2e/tests-dfx/assetscanister.bash

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ check_permission_failure() {
266266
FE_CANISTER_ID="$(dfx canister id e2e_project_frontend)"
267267
rm .dfx/local/canister_ids.json
268268
assert_command_fail dfx canister call "$FE_CANISTER_ID" validate_revoke_permission "(record { of_principal=principal \"$PREPARE_PRINCIPAL\"; permission = variant { FlyBeFree }; })"
269-
assert_contains "FlyBeFree not found"
269+
assert_contains "trapped"
270270
}
271271

272272
@test "access control - fine-grained" {
@@ -905,29 +905,24 @@ check_permission_failure() {
905905
dfx canister install e2e_project_frontend
906906

907907
assert_command dfx canister call --query e2e_project_frontend retrieve '("/binary/noise.txt")' --output idl
908-
# shellcheck disable=SC2154
909-
assert_eq '(blob "\b8\01\20\80\0a\77\31\32\20\00\78\79\0a\4b\4c\0b\0a\6a\6b")' "$stdout"
908+
assert_eq '(blob "\b8\01\20\80\0a\77\31\32\20\00\78\79\0a\4b\4c\0b\0a\6a\6b")'
910909

911910
assert_command dfx canister call --query e2e_project_frontend retrieve '("/text-with-newlines.txt")' --output idl
912-
# shellcheck disable=SC2154
913-
assert_eq '(blob "cherries\0ait\27s cherry season\0aCHERRIES")' "$stdout"
911+
assert_eq '(blob "cherries\0ait\27s cherry season\0aCHERRIES")'
914912

915913
assert_command dfx canister call --update e2e_project_frontend store '(record{key="AA"; content_type="text/plain"; content_encoding="identity"; content=blob "hello, world!"})'
916914
assert_eq '()'
917915
assert_command dfx canister call --update e2e_project_frontend store '(record{key="B"; content_type="application/octet-stream"; content_encoding="identity"; content=blob"XWV"})'
918916
assert_eq '()'
919917

920918
assert_command dfx canister call --query e2e_project_frontend retrieve '("B")' --output idl
921-
# shellcheck disable=SC2154
922-
assert_eq '(blob "XWV")' "$stdout"
919+
assert_eq '(blob "XWV")'
923920

924921
assert_command dfx canister call --query e2e_project_frontend retrieve '("AA")' --output idl
925-
# shellcheck disable=SC2154
926-
assert_eq '(blob "hello, world!")' "$stdout"
922+
assert_eq '(blob "hello, world!")'
927923

928924
assert_command dfx canister call --query e2e_project_frontend retrieve '("B")' --output idl
929-
# shellcheck disable=SC2154
930-
assert_eq '(blob "XWV")' "$stdout"
925+
assert_eq '(blob "XWV")'
931926

932927
assert_command_fail dfx canister call --query e2e_project_frontend retrieve '("C")'
933928
}

e2e/tests-dfx/build_granular.bash

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ teardown() {
6767
dfx canister install e2e_project_frontend
6868

6969
assert_command dfx canister call --query e2e_project_frontend retrieve '("/binary/noise.txt")' --output idl
70-
# shellcheck disable=SC2154
71-
assert_eq '(blob "\b8\01\20\80\0a\77\31\32\20\00\78\79\0a\4b\4c\0b\0a\6a\6b")' "$stdout"
70+
assert_eq '(blob "\b8\01\20\80\0a\77\31\32\20\00\78\79\0a\4b\4c\0b\0a\6a\6b")'
7271

7372
assert_command dfx canister call --query e2e_project_frontend retrieve '("/text-with-newlines.txt")' --output idl
74-
# shellcheck disable=SC2154
75-
assert_eq '(blob "cherries\0ait\27s cherry season\0aCHERRIES")' "$stdout"
73+
assert_eq '(blob "cherries\0ait\27s cherry season\0aCHERRIES")'
7674
}
7775

7876
@test "cyclic dependencies are detected" {

e2e/tests-dfx/call.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ teardown() {
2626
CANISTER_ID=$(dfx canister id hello_backend)
2727
rm .dfx/local/canister_ids.json
2828

29-
# given a canister id, fetch the did file from metadata
29+
# if no candid file known, then no field names
3030
assert_command dfx canister call "$CANISTER_ID" make_struct '("A", "B")'
31-
assert_eq '(record { c = "A"; d = "B" })'
31+
assert_eq '(record { 99 = "A"; 100 = "B" })'
3232

3333
# if passing the candid file, field names available
3434
assert_command dfx canister call --candid .dfx/local/canisters/hello_backend/hello_backend.did "$CANISTER_ID" make_struct '("A", "B")'

e2e/tests-dfx/identity.bash

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ teardown() {
155155
assert_command dfx canister call e2e_project_frontend store '(record{key="B"; content_type="application/octet-stream"; content_encoding="identity"; content=blob"XWV"})' --identity alice
156156
assert_eq '()'
157157
assert_command dfx canister call --output idl e2e_project_frontend retrieve '("B")'
158-
# shellcheck disable=SC2154
159-
assert_eq '(blob "XWV")' "$stdout"
158+
assert_eq '(blob "XWV")'
160159
}
161160

162161
@test "after renaming an identity, the renamed identity is still initializer" {
@@ -182,8 +181,7 @@ teardown() {
182181
assert_command dfx canister call e2e_project_frontend store '(record{key="B"; content_type="application/octet-stream"; content_encoding="identity"; content=blob "hello"})' --identity bob
183182
assert_eq '()'
184183
assert_command dfx canister call --output idl e2e_project_frontend retrieve '("B")'
185-
# shellcheck disable=SC2154
186-
assert_eq '(blob "hello")' "$stdout"
184+
assert_eq '(blob "hello")'
187185
}
188186

189187
@test "using an unencrypted identity on mainnet provokes a warning" {

e2e/tests-dfx/wallet.bash

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ teardown() {
153153

154154
assert_command dfx canister call "$WALLET" wallet_call \
155155
"(record { canister = principal \"$(dfx canister id e2e_project_backend)\"; method_name = \"amInitializer\"; args = blob \"DIDL\00\00\"; cycles = (0:nat64)})"
156-
# shellcheck disable=SC2154
157-
assert_eq '(variant { 17_724 = record { 153_986_224 = blob "\44\49\44\4c\00\01\7e\01" } })' "$stdout" # True in DIDL.
156+
assert_eq '(variant { 17_724 = record { 153_986_224 = blob "\44\49\44\4c\00\01\7e\01" } })' # True in DIDL.
158157
}
159158

160159
@test "forward user call through wallet: deploy" {
@@ -170,8 +169,7 @@ teardown() {
170169
assert_command dfx canister call e2e_project_backend amInitializer
171170
assert_command dfx canister call "$WALLET" wallet_call \
172171
"(record { canister = principal \"$(dfx canister id e2e_project_backend)\"; method_name = \"amInitializer\"; args = blob \"DIDL\00\00\"; cycles = (0:nat64)})"
173-
# shellcheck disable=SC2154
174-
assert_eq '(variant { 17_724 = record { 153_986_224 = blob "\44\49\44\4c\00\01\7e\01" } })' "$stdout" # True in DIDL.
172+
assert_eq '(variant { 17_724 = record { 153_986_224 = blob "\44\49\44\4c\00\01\7e\01" } })' # True in DIDL.
175173
}
176174

177175
@test "a 64-bit wallet can still be called in the 128-bit context" {

src/dfx/src/commands/canister/call.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ use crate::lib::error::DfxResult;
44
use crate::lib::operations::canister::get_local_cid_and_candid_path;
55
use crate::lib::root_key::fetch_root_key_if_needed;
66
use crate::util::clap::parsers::{cycle_amount_parser, file_or_stdin_parser};
7-
use crate::util::{
8-
arguments_from_file, blob_from_arguments, get_candid_type, print_idl_blob, read_module_metadata,
9-
};
7+
use crate::util::{arguments_from_file, blob_from_arguments, get_candid_type, print_idl_blob};
108
use anyhow::{anyhow, Context};
119
use candid::Principal as CanisterId;
1210
use candid::{CandidType, Decode, Deserialize, Principal};
13-
use candid_parser::utils::CandidSource;
1411
use clap::Parser;
1512
use dfx_core::canister::build_wallet_canister;
1613
use dfx_core::identity::CallSender;
@@ -221,32 +218,29 @@ pub async fn exec(
221218
opts: CanisterCallOpts,
222219
call_sender: &CallSender,
223220
) -> DfxResult {
224-
let agent = env.get_agent();
225-
fetch_root_key_if_needed(env).await?;
226-
227221
let callee_canister = opts.canister_name.as_str();
228222
let method_name = opts.method_name.as_str();
229223
let canister_id_store = env.get_canister_id_store()?;
230224

231-
let canister_id = match CanisterId::from_text(callee_canister) {
232-
Ok(id) => id,
225+
let (canister_id, maybe_candid_path) = match CanisterId::from_text(callee_canister) {
226+
Ok(id) => {
227+
if let Some(canister_name) = canister_id_store.get_name(callee_canister) {
228+
get_local_cid_and_candid_path(env, canister_name, Some(id))?
229+
} else {
230+
// TODO fetch candid file from remote canister
231+
(id, None)
232+
}
233+
}
233234
Err(_) => {
234235
let canister_id = canister_id_store.get(callee_canister)?;
235-
get_local_cid_and_candid_path(env, callee_canister, Some(canister_id))?.0
236+
get_local_cid_and_candid_path(env, callee_canister, Some(canister_id))?
236237
}
237238
};
238-
let method_type = if let Some(path) = opts.candid {
239-
get_candid_type(CandidSource::File(&path), method_name)
240-
} else {
241-
read_module_metadata(agent, canister_id, "candid:service")
242-
.await
243-
.and_then(|did| get_candid_type(CandidSource::Text(&did), method_name))
244-
};
245-
if method_type.is_none() {
246-
eprintln!("Cannot fetch Candid interface from canister metadata, sending arguments with inferred types.");
247-
}
239+
let maybe_candid_path = opts.candid.or(maybe_candid_path);
248240

249241
let is_management_canister = canister_id == CanisterId::management_canister();
242+
243+
let method_type = maybe_candid_path.and_then(|path| get_candid_type(&path, method_name));
250244
let is_query_method = method_type.as_ref().map(|(_, f)| f.is_query());
251245

252246
let arguments_from_file = opts
@@ -281,6 +275,9 @@ pub async fn exec(
281275
// Get the argument, get the type, convert the argument to the type and return
282276
// an error if any of it doesn't work.
283277
let arg_value = blob_from_arguments(arguments, opts.random.as_deref(), arg_type, &method_type)?;
278+
let agent = env.get_agent();
279+
280+
fetch_root_key_if_needed(env).await?;
284281

285282
// amount has been validated by cycle_amount_validator
286283
let cycles = opts.with_cycles.unwrap_or(0);

src/dfx/src/commands/canister/sign.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::util::clap::parsers::file_or_stdin_parser;
88
use crate::util::{arguments_from_file, blob_from_arguments, get_candid_type};
99
use anyhow::{anyhow, bail, Context};
1010
use candid::Principal;
11-
use candid_parser::utils::CandidSource;
1211
use clap::Parser;
1312
use dfx_core::identity::CallSender;
1413
use ic_agent::AgentError;
@@ -87,7 +86,7 @@ pub async fn exec(
8786
if let Some(canister_name) = canister_id_store.get_name(callee_canister) {
8887
get_local_cid_and_candid_path(env, canister_name, Some(id))?
8988
} else {
90-
// Sign works in offline mode, cannot fetch candid file from remote canister
89+
// TODO fetch candid file from remote canister
9190
(id, None)
9291
}
9392
}
@@ -97,8 +96,7 @@ pub async fn exec(
9796
}
9897
};
9998

100-
let method_type =
101-
maybe_candid_path.and_then(|path| get_candid_type(CandidSource::File(&path), method_name));
99+
let method_type = maybe_candid_path.and_then(|path| get_candid_type(&path, method_name));
102100
let is_query_method = method_type.as_ref().map(|(_, f)| f.is_query());
103101

104102
let arguments_from_file = opts

src/dfx/src/commands/deps/init.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::lib::deps::{
55
};
66
use crate::lib::environment::Environment;
77
use crate::lib::error::DfxResult;
8-
use crate::util::fuzzy_parse_argument;
8+
use crate::util::{check_candid_file, fuzzy_parse_argument};
99
use anyhow::{anyhow, bail};
1010
use candid::Principal;
11-
use candid_parser::{types::IDLTypes, typing::ast_to_type, utils::CandidSource};
11+
use candid_parser::{types::IDLTypes, typing::ast_to_type};
1212
use clap::Parser;
1313
use slog::{info, warn, Logger};
1414

@@ -68,7 +68,7 @@ fn set_init(
6868
.ok_or_else(|| anyhow!("Failed to find {canister_id} entry in pulled.json"))?;
6969
let canister_prompt = get_canister_prompt(canister_id, pulled_canister);
7070
let idl_path = get_pulled_service_candid_path(canister_id)?;
71-
let (env, _) = CandidSource::File(&idl_path).load()?;
71+
let (env, _) = check_candid_file(&idl_path)?;
7272
let candid_args = pulled_json.get_candid_args(canister_id)?;
7373
let candid_args_idl_types: IDLTypes = candid_args.parse()?;
7474
let mut types = vec![];

src/dfx/src/commands/remote/generate_binding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::lib::agent::create_agent_environment;
22
use crate::lib::environment::Environment;
33
use crate::lib::error::DfxResult;
44
use crate::lib::models::canister::CanisterPool;
5+
use crate::util::check_candid_file;
56
use anyhow::Context;
6-
use candid_parser::utils::CandidSource;
77
use clap::Parser;
88
use slog::info;
99

@@ -70,7 +70,7 @@ pub fn exec(env: &dyn Environment, opts: GenerateBindingOpts) -> DfxResult {
7070
continue;
7171
}
7272
}
73-
let (type_env, did_types) = CandidSource::File(&candid).load()?;
73+
let (type_env, did_types) = check_candid_file(&candid)?;
7474
let extension = main.extension().unwrap_or_default();
7575
let bindings = if extension == "mo" {
7676
Some(candid_parser::bindings::motoko::compile(

0 commit comments

Comments
 (0)