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
6 changes: 5 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ assert_matches = "1.5.0"
async-trait = "0.1.88"
candid = "0.10.13"
candid_parser = "0.1.4"
canhttp = { git = "https://github.com/dfinity/evm-rpc-canister", rev = "1aeeca3bdcb86ce4493b71e4117f65ab78475396" }
canhttp = { git = "https://github.com/dfinity/evm-rpc-canister", rev = "b976abe57379be7d2649f6a31522d0bb25c5f455" }
ciborium = "0.2.2"
# Transitive dependency of ic-ed25519
# See https://forum.dfinity.org/t/module-imports-function-wbindgen-describe-from-wbindgen-placeholder-that-is-not-exported-by-the-runtime/11545/8
Expand Down
2 changes: 1 addition & 1 deletion canister/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "src/main.rs"
[dependencies]
assert_matches = { workspace = true }
candid = { workspace = true }
canhttp = { workspace = true, features = ["json"] }
canhttp = { workspace = true, features = ["json", "multi"] }
canlog = { path = "../canlog", features = ["derive"] }
ciborium = { workspace = true }
const_format = { workspace = true }
Expand Down
8 changes: 7 additions & 1 deletion canister/sol_rpc_canister.did
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ type GetSlotParams = record {
// Represents the result of a generic RPC request.
type RequestResult = variant { Ok : text; Err : RpcError };

// Represents an aggregated result from multiple RPC calls for a generic RPC request.
type MultiRequestResult = variant {
Consistent : RequestResult;
Inconsistent : vec record { RpcSource; RequestResult };
};

// A string used as a regex pattern.
type Regex = text;

Expand Down Expand Up @@ -210,5 +216,5 @@ service : (InstallArgs,) -> {
getSlot : (RpcSources, opt RpcConfig, opt GetSlotParams) -> (MultiGetSlotResult);

// Make a generic RPC request that sends the given json_rpc_payload.
request : (RpcSource, json_rpc_paylod: text, max_response_bytes: nat64) -> (RequestResult)
request : (RpcSources, opt RpcConfig, json_rpc_paylod: text) -> (MultiRequestResult)
};
28 changes: 19 additions & 9 deletions canister/src/candid_rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::{
rpc_client::{MultiCallError, SolRpcClient},
types::MultiRpcResult,
};
use sol_rpc_types::{GetSlotParams, RpcConfig, RpcResult, RpcSources};
use crate::rpc_client::{ReducedResult, SolRpcClient};
use canhttp::multi::ReductionError;
use serde::Serialize;
use sol_rpc_types::{GetSlotParams, MultiRpcResult, RpcConfig, RpcResult, RpcSources};
use solana_clock::Slot;
use std::fmt::Debug;

fn process_result<T>(result: Result<T, MultiCallError<T>>) -> MultiRpcResult<T> {
fn process_result<T>(result: ReducedResult<T>) -> MultiRpcResult<T> {
match result {
Ok(value) => MultiRpcResult::Consistent(Ok(value)),
Err(err) => match err {
MultiCallError::ConsistentError(err) => MultiRpcResult::Consistent(Err(err)),
MultiCallError::InconsistentResults(multi_call_results) => {
let results = multi_call_results.into_vec();
ReductionError::ConsistentError(err) => MultiRpcResult::Consistent(Err(err)),
ReductionError::InconsistentResults(multi_call_results) => {
let results: Vec<_> = multi_call_results.into_iter().collect();
results.iter().for_each(|(_service, _service_result)| {
// TODO XC-296: Add metrics for inconsistent providers
});
Expand All @@ -36,4 +36,14 @@ impl CandidRpcClient {
pub async fn get_slot(&self, params: GetSlotParams) -> MultiRpcResult<Slot> {
process_result(self.client.get_slot(params).await)
}

pub async fn raw_request<I>(
&self,
request: canhttp::http::json::JsonRpcRequest<I>,
) -> MultiRpcResult<serde_json::Value>
where
I: Serialize + Clone + Debug,
{
process_result(self.client.raw_request(request).await)
}
}
36 changes: 23 additions & 13 deletions canister/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use candid::candid_method;
use canhttp::http::json::JsonRpcRequest;
use canlog::{log, Log, Sort};
use ic_cdk::{api::is_controller, query, update};
use sol_rpc_canister::{
candid_rpc::CandidRpcClient,
http_types, lifecycle,
logs::Priority,
providers::{get_provider, PROVIDERS},
rpc_client,
state::{mutate_state, read_state},
};
use sol_rpc_types::{
GetSlotParams, MultiRpcResult, RpcAccess, RpcConfig, RpcError, RpcResult, RpcSource,
RpcSources, SupportedRpcProvider, SupportedRpcProviderId,
GetSlotParams, MultiRpcResult, RpcAccess, RpcConfig, RpcError, RpcSources,
SupportedRpcProvider, SupportedRpcProviderId,
};
use solana_clock::Slot;
use std::str::FromStr;
Expand Down Expand Up @@ -80,24 +80,34 @@ async fn get_slot(
params: Option<GetSlotParams>,
) -> MultiRpcResult<Slot> {
match CandidRpcClient::new(source, config) {
Ok(client) => client.get_slot(params.unwrap_or_default()).await.into(),
Ok(client) => client.get_slot(params.unwrap_or_default()).await,
Err(err) => Err(err).into(),
}
}

#[update]
#[candid_method]
async fn request(
provider: RpcSource,
source: RpcSources,
config: Option<RpcConfig>,
json_rpc_payload: String,
max_response_bytes: u64,
) -> RpcResult<String> {
let request: canhttp::http::json::JsonRpcRequest<serde_json::Value> =
serde_json::from_str(&json_rpc_payload)
.map_err(|e| RpcError::ValidationError(format!("Invalid JSON RPC request: {e}")))?;
rpc_client::call(&provider, request, max_response_bytes)
.await
.map(|value: serde_json::Value| value.to_string())
) -> MultiRpcResult<String> {
let request: JsonRpcRequest<serde_json::Value> = match serde_json::from_str(&json_rpc_payload) {
Ok(req) => req,
Err(e) => {
return Err(RpcError::ValidationError(format!(
"Invalid JSON RPC request: {e}"
)))
.into()
}
};
match CandidRpcClient::new(source, config) {
Ok(client) => client
.raw_request(request)
.await
.map(|value| value.to_string()),
Err(err) => Err(err).into(),
}
}

#[query(hidden = true)]
Expand Down
Loading