forked from dfinity/ic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(nervous-system-agent): Add pocket-ic support to ic-nervous-s…
…ystem-agent (dfinity#1590) This will allow us to use ic-nervous-system-agent helper functions from within tests built on pocket-ic. This PR synergizes with dfinity#1589, which starts to use ic-nervous-system-agent types from within ic-nervous-system-integration-tests
- Loading branch information
1 parent
1b4cee6
commit 0663326
Showing
6 changed files
with
111 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use candid::Principal; | ||
use ic_agent::Agent; | ||
use ic_nervous_system_clients::Request; | ||
use thiserror::Error; | ||
|
||
use crate::CallCanisters; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum AgentCallError { | ||
#[error("agent error: {0}")] | ||
Agent(#[from] ic_agent::AgentError), | ||
#[error("canister request could not be encoded: {0}")] | ||
CandidEncode(candid::Error), | ||
#[error("canister did not respond with the expected response type: {0}")] | ||
CandidDecode(candid::Error), | ||
} | ||
|
||
impl CallCanisters for Agent { | ||
type Error = AgentCallError; | ||
async fn call<R: Request>( | ||
&self, | ||
canister_id: impl Into<Principal> + Send, | ||
request: R, | ||
) -> Result<R::Response, Self::Error> { | ||
let canister_id = canister_id.into(); | ||
let request_bytes = candid::encode_one(&request).map_err(AgentCallError::CandidEncode)?; | ||
let response = if R::UPDATE { | ||
let request = self | ||
.update(&canister_id, R::METHOD) | ||
.with_arg(request_bytes) | ||
.call() | ||
.await?; | ||
match request { | ||
ic_agent::agent::CallResponse::Response(response) => response, | ||
ic_agent::agent::CallResponse::Poll(request_id) => { | ||
self.wait(&request_id, canister_id).await? | ||
} | ||
} | ||
} else { | ||
self.query(&canister_id, R::METHOD) | ||
.with_arg(request_bytes) | ||
.call() | ||
.await? | ||
}; | ||
|
||
let response = | ||
candid::decode_one(response.as_slice()).map_err(AgentCallError::CandidDecode)?; | ||
Ok(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use candid::Principal; | ||
use ic_nervous_system_clients::Request; | ||
use pocket_ic::PocketIc; | ||
use thiserror::Error; | ||
|
||
use crate::CallCanisters; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum PocketIcCallError { | ||
#[error("pocket_ic error: {0}")] | ||
PocketIc(pocket_ic::UserError), | ||
#[error("canister rejected the request: {0}")] | ||
Reject(String), | ||
#[error("canister request could not be encoded: {0}")] | ||
CandidEncode(candid::Error), | ||
#[error("canister did not respond with the expected response type: {0}")] | ||
CandidDecode(candid::Error), | ||
} | ||
|
||
impl CallCanisters for PocketIc { | ||
type Error = PocketIcCallError; | ||
async fn call<R: Request>( | ||
&self, | ||
canister_id: impl Into<Principal> + Send, | ||
request: R, | ||
) -> Result<R::Response, Self::Error> { | ||
let canister_id = canister_id.into(); | ||
let request_bytes = | ||
candid::encode_one(&request).map_err(PocketIcCallError::CandidEncode)?; | ||
let response = if R::UPDATE { | ||
self.update_call( | ||
canister_id, | ||
Principal::anonymous(), | ||
R::METHOD, | ||
request_bytes, | ||
) | ||
} else { | ||
self.query_call( | ||
canister_id, | ||
Principal::anonymous(), | ||
R::METHOD, | ||
request_bytes, | ||
) | ||
} | ||
.map_err(PocketIcCallError::PocketIc)?; | ||
|
||
match response { | ||
pocket_ic::WasmResult::Reply(reply) => { | ||
let response = candid::decode_one(reply.as_slice()) | ||
.map_err(PocketIcCallError::CandidDecode)?; | ||
Ok(response) | ||
} | ||
pocket_ic::WasmResult::Reject(reject) => Err(PocketIcCallError::Reject(reject)), | ||
} | ||
} | ||
} |