Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions integration_tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use ic_pocket_canister_runtime::{
use pocket_ic::{common::rest::CanisterHttpResponse, ErrorCode, RejectResponse};
use serde::de::DeserializeOwned;
use serde_json::{json, Value};
use sol_rpc_client::{RequestBuilder, SolRpcEndpoint};
use sol_rpc_client::{SolRpcClient, SolRpcConfig};
use sol_rpc_client::{
DefaultRequestCycles, RequestBuilder, SolRpcClient, SolRpcConfig, SolRpcEndpoint,
};
use sol_rpc_int_tests::{Setup, DEFAULT_CALLER_TEST_ID};
use sol_rpc_types::{
CommitmentLevel, ConfirmedTransactionStatusWithSignature, ConsensusStrategy,
Expand All @@ -33,7 +34,7 @@ use solana_transaction_status_client_types::{
EncodedTransaction, EncodedTransactionWithStatusMeta, TransactionBinaryEncoding,
TransactionConfirmationStatus, TransactionStatus, UiLoadedAddresses, UiTransactionStatusMeta,
};
use std::{fmt::Debug, iter::zip, num::NonZeroU8, str::FromStr};
use std::{fmt::Debug, iter::zip, num::NonZeroU8, ops::RangeFrom, str::FromStr};
use strum::IntoEnumIterator;

const USDC_PUBLIC_KEY: solana_pubkey::Pubkey =
Expand Down Expand Up @@ -469,18 +470,14 @@ mod generic_request_tests {

#[tokio::test]
async fn should_require_base_http_outcall_fee() {
async fn check<Config, Params, CandidOutput, Output>(
request: RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'_>>,
Config,
Params,
CandidOutput,
Output,
>,
async fn check<Runtime, Config, Params, CandidOutput, Output>(
request: RequestBuilder<Runtime, Config, Params, CandidOutput, Output>,
) where
Runtime: ic_canister_runtime::Runtime,
Config: CandidType + Clone + Send,
Params: CandidType + Clone + Send,
CandidOutput: Into<Output> + CandidType + DeserializeOwned,
RequestBuilder<Runtime, Config, Params, CandidOutput, Output>: DefaultRequestCycles,
{
let result = request
.with_cycles(HTTP_OUTCALL_BASE_FEE - 1)
Expand Down Expand Up @@ -535,18 +532,14 @@ mod generic_request_tests {

#[tokio::test]
async fn should_not_require_cycles_in_demo_mode() {
async fn check<Config, Params, CandidOutput, Output>(
request: RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'_>>,
Config,
Params,
CandidOutput,
Output,
>,
async fn check<Runtime, Config, Params, CandidOutput, Output>(
request: RequestBuilder<Runtime, Config, Params, CandidOutput, Output>,
) where
Runtime: ic_canister_runtime::Runtime,
Config: CandidType + Clone + Send,
Params: CandidType + Clone + Send,
CandidOutput: Into<Output> + CandidType + DeserializeOwned,
RequestBuilder<Runtime, Config, Params, CandidOutput, Output>: DefaultRequestCycles,
{
let result = request.with_cycles(0).try_send().await;
assert!(result.is_ok());
Expand Down Expand Up @@ -841,7 +834,6 @@ fn rpc_sources() -> Vec<RpcSources> {

mod cycles_cost_tests {
use super::*;
use std::ops::RangeFrom;

#[tokio::test]
async fn should_be_idempotent() {
Expand Down Expand Up @@ -977,22 +969,30 @@ mod cycles_cost_tests {

#[tokio::test]
async fn should_get_exact_cycles_cost() {
async fn check<Config, Params, CandidOutput, Output>(
async fn check<Runtime, Config, Params, CandidOutput, Output>(
setup: &Setup,
request: RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'_>>,
Runtime,
Config,
Params,
MultiRpcResult<CandidOutput>,
MultiRpcResult<Output>,
>,
expected_cycles_cost: u128,
) where
Runtime: ic_canister_runtime::Runtime,
Config: CandidType + Clone + Send,
Params: CandidType + Clone + Send,
CandidOutput: CandidType + DeserializeOwned,
Output: Debug,
MultiRpcResult<CandidOutput>: Into<MultiRpcResult<Output>>,
RequestBuilder<
Runtime,
Config,
Params,
MultiRpcResult<CandidOutput>,
MultiRpcResult<Output>,
>: DefaultRequestCycles,
{
let five_percents = 5_u8;

Expand Down Expand Up @@ -1177,12 +1177,13 @@ mod cycles_cost_tests {

mod rpc_config_tests {
use super::*;
use sol_rpc_client::DefaultRequestCycles;

#[tokio::test]
async fn should_respect_response_size_estimate() {
async fn check<Config, Params, CandidOutput, Output>(
async fn check<'a, Config, Params, CandidOutput, Output>(
request: RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'_>>,
CyclesWalletRuntime<PocketIcRuntime<'a>>,
Config,
Params,
MultiRpcResult<CandidOutput>,
Expand All @@ -1194,6 +1195,13 @@ mod rpc_config_tests {
CandidOutput: CandidType + DeserializeOwned,
Output: Debug + PartialEq,
MultiRpcResult<CandidOutput>: Into<MultiRpcResult<Output>>,
RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'a>>,
Config,
Params,
MultiRpcResult<CandidOutput>,
MultiRpcResult<Output>,
>: DefaultRequestCycles,
{
let result = request
.with_response_size_estimate(1_999_999)
Expand Down Expand Up @@ -1255,8 +1263,8 @@ mod rpc_config_tests {

#[tokio::test]
async fn should_respect_response_strategy() {
async fn check<F, Config, Params, CandidOutput, Output>(
setup: &Setup,
async fn check<'a, F, Config, Params, CandidOutput, Output>(
setup: &'a Setup,
request: F,
offset: &mut u64,
json_rpc_request: JsonRpcRequestMatcher,
Expand All @@ -1276,6 +1284,13 @@ mod rpc_config_tests {
CandidOutput: CandidType + DeserializeOwned,
Output: Debug + PartialEq,
MultiRpcResult<CandidOutput>: Into<MultiRpcResult<Output>>,
RequestBuilder<
CyclesWalletRuntime<PocketIcRuntime<'a>>,
Config,
Params,
MultiRpcResult<CandidOutput>,
MultiRpcResult<Output>,
>: DefaultRequestCycles,
{
let mocks = MockHttpOutcallsBuilder::new()
.given(json_rpc_request.clone().with_id(*offset))
Expand Down
56 changes: 10 additions & 46 deletions libs/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ use crate::request::{
use candid::{CandidType, Principal};
use ic_canister_runtime::{IcError, IcRuntime, Runtime};
pub use request::{
EstimateBlockhashRequestBuilder, EstimateRecentBlockhashError, Request, RequestBuilder,
SolRpcConfig, SolRpcEndpoint, SolRpcRequest,
DefaultRequestCycles, EstimateBlockhashRequestBuilder, EstimateRecentBlockhashError, Request,
RequestBuilder, SolRpcConfig, SolRpcEndpoint, SolRpcRequest,
};
use serde::de::DeserializeOwned;
use sol_rpc_types::{
Expand Down Expand Up @@ -324,11 +324,7 @@ impl<R> SolRpcClient<R> {
&self,
params: impl Into<GetAccountInfoParams>,
) -> GetAccountInfoRequestBuilder<R> {
RequestBuilder::new(
self.clone(),
GetAccountInfoRequest::new(params.into()),
10_000_000_000,
)
RequestBuilder::new(self.clone(), GetAccountInfoRequest::new(params.into()))
}

/// Call `getBalance` on the SOL RPC canister.
Expand Down Expand Up @@ -359,11 +355,7 @@ impl<R> SolRpcClient<R> {
/// # }
/// ```
pub fn get_balance(&self, params: impl Into<GetBalanceParams>) -> GetBalanceRequestBuilder<R> {
RequestBuilder::new(
self.clone(),
GetBalanceRequest::new(params.into()),
10_000_000_000,
)
RequestBuilder::new(self.clone(), GetBalanceRequest::new(params.into()))
}

/// Call `getBlock` on the SOL RPC canister.
Expand Down Expand Up @@ -433,7 +425,7 @@ impl<R> SolRpcClient<R> {
/// # }
/// ```
pub fn get_block(&self, params: impl Into<GetBlockParams>) -> GetBlockRequestBuilder<R> {
RequestBuilder::new(self.clone(), GetBlockRequest::new(params.into()), 0).update_cycles()
RequestBuilder::new(self.clone(), GetBlockRequest::new(params.into()))
}

/// Call `getTokenAccountBalance` on the SOL RPC canister.
Expand Down Expand Up @@ -481,7 +473,6 @@ impl<R> SolRpcClient<R> {
RequestBuilder::new(
self.clone(),
GetTokenAccountBalanceRequest::new(params.into()),
10_000_000_000,
)
}

Expand Down Expand Up @@ -559,7 +550,6 @@ impl<R> SolRpcClient<R> {
Ok(RequestBuilder::new(
self.clone(),
GetRecentPrioritizationFeesRequest::from(params),
10_000_000_000,
))
}

Expand Down Expand Up @@ -638,7 +628,6 @@ impl<R> SolRpcClient<R> {
RequestBuilder::new(
self.clone(),
GetSignaturesForAddressRequest::from(params.into()),
2_000_000_000, // TODO XC-338: Check heuristic
)
}

Expand Down Expand Up @@ -744,11 +733,9 @@ impl<R> SolRpcClient<R> {
I: IntoIterator<Item = &'a solana_signature::Signature>,
{
let signatures = signatures.into_iter().collect::<Vec<_>>();
let num_signatures = signatures.len();
Ok(RequestBuilder::new(
self.clone(),
GetSignatureStatusesRequest::from(GetSignatureStatusesParams::try_from(signatures)?),
2_000_000_000 + num_signatures as u128 * 1_000_000, // TODO XC-338: Check heuristic
))
}

Expand Down Expand Up @@ -782,7 +769,7 @@ impl<R> SolRpcClient<R> {
/// # }
/// ```
pub fn get_slot(&self) -> GetSlotRequestBuilder<R> {
RequestBuilder::new(self.clone(), GetSlotRequest::default(), 10_000_000_000)
RequestBuilder::new(self.clone(), GetSlotRequest::default())
}

/// Call `getTransaction` on the SOL RPC canister.
Expand Down Expand Up @@ -861,11 +848,7 @@ impl<R> SolRpcClient<R> {
&self,
params: impl Into<GetTransactionParams>,
) -> GetTransactionRequestBuilder<R> {
RequestBuilder::new(
self.clone(),
GetTransactionRequest::new(params.into()),
10_000_000_000,
)
RequestBuilder::new(self.clone(), GetTransactionRequest::new(params.into()))
}

/// Call `sendTransaction` on the SOL RPC canister.
Expand Down Expand Up @@ -910,11 +893,7 @@ impl<R> SolRpcClient<R> {
let params = params
.try_into()
.expect("Unable to build request parameters");
RequestBuilder::new(
self.clone(),
SendTransactionRequest::new(params),
10_000_000_000,
)
RequestBuilder::new(self.clone(), SendTransactionRequest::new(params))
}

/// Call `jsonRequest` on the SOL RPC canister.
Expand Down Expand Up @@ -976,7 +955,6 @@ impl<R> SolRpcClient<R> {
RequestBuilder::new(
self.clone(),
JsonRequest::try_from(json_request).expect("Client error: invalid JSON request"),
10_000_000_000,
)
}
}
Expand Down Expand Up @@ -1086,24 +1064,10 @@ impl<R: Runtime> SolRpcClient<R> {
EstimateBlockhashRequestBuilder::new(self.clone())
}

async fn execute_request<Config, Params, CandidOutput, Output>(
&self,
request: Request<Config, Params, CandidOutput, Output>,
) -> Output
where
Config: CandidType + Send,
Params: CandidType + Send,
CandidOutput: Into<Output> + CandidType + DeserializeOwned,
{
let rpc_method = request.endpoint.rpc_method();
self.try_execute_request(request)
.await
.unwrap_or_else(|e| panic!("Client error: failed to call `{}`: {e:?}", rpc_method))
}

async fn try_execute_request<Config, Params, CandidOutput, Output>(
&self,
request: Request<Config, Params, CandidOutput, Output>,
cycles: u128,
) -> Result<Output, IcError>
where
Config: CandidType + Send,
Expand All @@ -1116,7 +1080,7 @@ impl<R: Runtime> SolRpcClient<R> {
self.config.sol_rpc_canister,
request.endpoint.rpc_method(),
(request.rpc_sources, request.rpc_config, request.params),
request.cycles,
cycles,
)
.await
.map(Into::into)
Expand Down
Loading