Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Nov 28, 2024
1 parent f984597 commit 724abf6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/dfx-core/src/canister/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ YOU WILL LOSE ALL DATA IN THE CANISTER.
.map_err(CanisterInstallError::InstallWasmError)
}
CallSender::Impersonate(_) => {
unreachable!("Impersonating sender is not supported for canister installation.")
unreachable!("Impersonating sender when installing canisters is not supported.")
}
CallSender::Wallet(wallet_id) => {
let wallet = build_wallet_canister(*wallet_id, agent).await?;
Expand Down
86 changes: 25 additions & 61 deletions src/dfx/src/commands/canister/request_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use backoff::ExponentialBackoff;
use candid::Principal;
use clap::Parser;
use ic_agent::agent::RequestStatusResponse;
use ic_agent::agent::{RejectCode, RejectResponse};
use ic_agent::{AgentError, RequestId};
use pocket_ic::common::rest::{RawEffectivePrincipal, RawMessageId};
use pocket_ic::WasmResult;
use std::str::FromStr;

/// Requests the status of a call from a canister.
Expand Down Expand Up @@ -53,66 +50,33 @@ pub async fn exec(env: &dyn Environment, opts: RequestStatusOpts) -> DfxResult {
let blob = async {
let mut request_accepted = false;
loop {
if let Some(pocketic_handle) = env.get_pocketic() {
let msg_id = RawMessageId {
effective_principal: RawEffectivePrincipal::CanisterId(
canister_id.as_slice().to_vec(),
),
message_id: request_id.as_slice().to_vec(),
};
if let Some(status) = pocketic_handle.ingress_status(msg_id).await {
match status {
Ok(WasmResult::Reply(data)) => return Ok(data),
Ok(WasmResult::Reject(reject_message)) => {
// https://github.com/dfinity/ic/blob/2845f3a1e81eb9eddc0e5541c5565bf6cff898d0/rs/canonical_state/src/lazy_tree_conversion.rs#L509-L513
let reject = RejectResponse {
reject_code: RejectCode::CanisterReject,
reject_message,
error_code: Some("IC0406".to_string()), // https://github.com/dfinity/ic/blob/2845f3a1e81eb9eddc0e5541c5565bf6cff898d0/rs/types/error_types/src/lib.rs#L204
};
return Err(DfxError::new(AgentError::CertifiedReject(reject)));
}
Err(user_error) => {
let error_code_as_u64 = user_error.code as u64;
let derived_reject_code = error_code_as_u64 / 100;
let reject = RejectResponse {
reject_code: derived_reject_code.try_into().unwrap(),
reject_message: user_error.description,
error_code: Some(user_error.code.to_string()),
};
return Err(DfxError::new(AgentError::CertifiedReject(reject)));
}
}
match agent
.request_status_raw(&request_id, canister_id)
.await
.context("Failed to fetch request status.")?
{
RequestStatusResponse::Replied(reply) => return Ok(reply.arg),
RequestStatusResponse::Rejected(response) => {
return Err(DfxError::new(AgentError::CertifiedReject(response)))
}
} else {
match agent
.request_status_raw(&request_id, canister_id)
.await
.context("Failed to fetch request status.")?
{
RequestStatusResponse::Replied(reply) => return Ok(reply.arg),
RequestStatusResponse::Rejected(response) => {
return Err(DfxError::new(AgentError::CertifiedReject(response)))
}
RequestStatusResponse::Unknown => (),
RequestStatusResponse::Received | RequestStatusResponse::Processing => {
// The system will return Unknown until the request is accepted
// and we generally cannot know how long that will take.
// State transitions between Received and Processing may be
// instantaneous. Therefore, once we know the request is accepted,
// we restart the waiter so the request does not time out.
if !request_accepted {
retry_policy.reset();
request_accepted = true;
}
}
RequestStatusResponse::Done => {
return Err(DfxError::new(AgentError::RequestStatusDoneNoReply(
String::from(request_id),
)))
RequestStatusResponse::Unknown => (),
RequestStatusResponse::Received | RequestStatusResponse::Processing => {
// The system will return Unknown until the request is accepted
// and we generally cannot know how long that will take.
// State transitions between Received and Processing may be
// instantaneous. Therefore, once we know the request is accepted,
// we restart the waiter so the request does not time out.
if !request_accepted {
retry_policy.reset();
request_accepted = true;
}
};
}
}
RequestStatusResponse::Done => {
return Err(DfxError::new(AgentError::RequestStatusDoneNoReply(
String::from(request_id),
)))
}
};

let interval = retry_policy
.next_backoff()
Expand Down
2 changes: 1 addition & 1 deletion src/dfx/src/lib/operations/canister/create_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ The command line value will be used.",
}
}
CallSender::Impersonate(_) => {
unreachable!("Impersonating sender when creating canisters in not supported.")
unreachable!("Impersonating sender when creating canisters is not supported.")
}
CallSender::Wallet(wallet_id) => {
create_with_wallet(agent, &wallet_id, with_cycles, settings, subnet_selection).await
Expand Down
2 changes: 1 addition & 1 deletion src/dfx/src/lib/operations/canister/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
}
CallSender::Impersonate(_) => {
unreachable!(
"Impersonating sender in management canister query calls in not supported."
"Impersonating sender in management canister query calls is not supported."
)
}
CallSender::Wallet(wallet_id) => {
Expand Down

0 comments on commit 724abf6

Please sign in to comment.