Skip to content

Commit

Permalink
fix(providers): proxy request for a certain provider
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Jan 23, 2024
1 parent d093f95 commit d9f7377
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static HANDLER_TASK_METRICS: TaskMetrics = TaskMetrics::new("handler_task");
pub struct RpcQueryParams {
pub chain_id: String,
pub project_id: String,
pub provider_id: Option<String>,
}

#[derive(Serialize)]
Expand Down
15 changes: 11 additions & 4 deletions src/handlers/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ async fn handler_internal(
query_params.chain_id.to_lowercase()
};

let provider = state
.providers
.get_provider_for_chain_id(&chain_id)
.ok_or(RpcError::UnsupportedChain(chain_id.clone()))?;
let provider = if let Some(provider_id) = query_params.clone().provider_id {
match state.providers.get_provider_by_provider_id(&provider_id) {
None => return Err(RpcError::UnsupportedProvider(provider_id)),
Some(provider) => provider,
}
} else {
state
.providers
.get_provider_for_chain_id(&chain_id)
.ok_or(RpcError::UnsupportedChain(chain_id.clone()))?
};

Span::current().record("provider", &provider.provider_kind().to_string());

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod json_rpc;
mod metrics;
pub mod profiler;
mod project;
mod providers;
pub mod providers;
mod state;
mod storage;
mod utils;
Expand Down
8 changes: 8 additions & 0 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ impl ProviderRepository {
}
}
}

#[tracing::instrument(skip(self), level = "debug")]
pub fn get_provider_by_provider_id(&self, provider_id: &str) -> Option<Arc<dyn RpcProvider>> {
let provider = ProviderKind::from_str(provider_id)?;

self.providers.get(&provider).cloned()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -302,6 +309,7 @@ impl Display for ProviderKind {
}
}

#[allow(clippy::should_implement_trait)]
impl ProviderKind {
pub fn from_str(s: &str) -> Option<Self> {
match s {
Expand Down
11 changes: 9 additions & 2 deletions tests/functional/http/aurora.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::check_if_rpc_is_responding_correctly_for_supported_chain,
crate::context::ServerContext,
rpc_proxy::providers::ProviderKind,
test_context::test_context,
};

Expand All @@ -11,12 +12,18 @@ async fn aurora_provider(ctx: &mut ServerContext) {
// Aurora Mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Aurora,
"eip155:1313161554",
"0x4e454152",
)
.await;

// Aurora Testnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:1313161555", "0x4e454153")
.await
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Aurora,
"eip155:1313161555",
"0x4e454153",
)
.await
}
17 changes: 15 additions & 2 deletions tests/functional/http/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::check_if_rpc_is_responding_correctly_for_supported_chain,
crate::context::ServerContext,
rpc_proxy::providers::ProviderKind,
test_context::test_context,
};

Expand All @@ -9,8 +10,20 @@ use {
#[ignore]
async fn base_provider_eip155_8453_and_84531(ctx: &mut ServerContext) {
// Base mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:8453", "0x2105").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Base,
"eip155:8453",
"0x2105",
)
.await;

// Base Goerli
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:84531", "0x14a33").await
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Base,
"eip155:84531",
"0x14a33",
)
.await
}
17 changes: 15 additions & 2 deletions tests/functional/http/binance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::check_if_rpc_is_responding_correctly_for_supported_chain,
crate::context::ServerContext,
rpc_proxy::providers::ProviderKind,
test_context::test_context,
};

Expand All @@ -9,8 +10,20 @@ use {
#[ignore]
async fn binance_provider_eip155_56_and_97(ctx: &mut ServerContext) {
// Binance mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:56", "0x38").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Binance,
"eip155:56",
"0x38",
)
.await;

// Binance testnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:97", "0x61").await
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Binance,
"eip155:97",
"0x61",
)
.await
}
90 changes: 78 additions & 12 deletions tests/functional/http/infura.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::check_if_rpc_is_responding_correctly_for_supported_chain,
crate::context::ServerContext,
rpc_proxy::providers::ProviderKind,
test_context::test_context,
};

Expand All @@ -9,36 +10,101 @@ use {
#[ignore]
async fn infura_provider(ctx: &mut ServerContext) {
// Ethereum mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:1", "0x1").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:1",
"0x1",
)
.await;

// Ethereum Goerli
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:5", "0x5").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:5",
"0x5",
)
.await;

// Ethereum Sepolia
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:11155111", "0xaa36a7")
.await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:11155111",
"0xaa36a7",
)
.await;

// Polgyon mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:137", "0x89").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:137",
"0x89",
)
.await;

// Polygon mumbai
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:80001", "0x13881").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:80001",
"0x13881",
)
.await;

// Optimism mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:10", "0xa").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:10",
"0xa",
)
.await;

// Optimism goerli
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:420", "0x1A4").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:420",
"0x1A4",
)
.await;

// Arbitrum mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:42161", "0xa4b1").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:42161",
"0xa4b1",
)
.await;

// Arbitrum goerli
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:421613", "0x66eed").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:421613",
"0x66eed",
)
.await;

// Celo
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:42220", "0xa4ec").await;
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:42220",
"0xa4ec",
)
.await;

// Base Goerli
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:84531", "0x14a33").await
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Infura,
"eip155:84531",
"0x14a33",
)
.await
}
7 changes: 4 additions & 3 deletions tests/functional/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{context::ServerContext, utils::send_jsonrpc_request, JSONRPC_VERSION},
hyper::{Body, Client, Method, Request, StatusCode},
hyper_tls::HttpsConnector,
rpc_proxy::handlers::history::HistoryResponseBody,
rpc_proxy::{handlers::history::HistoryResponseBody, providers::ProviderKind},
test_context::test_context,
};

Expand All @@ -16,12 +16,13 @@ pub(crate) mod zora;

async fn check_if_rpc_is_responding_correctly_for_supported_chain(
ctx: &ServerContext,
provider_id: &ProviderKind,
chaind_id: &str,
expected_id: &str,
) {
let addr = format!(
"{}/v1/?projectId={}&chainId=",
ctx.server.public_addr, ctx.server.project_id
"{}/v1/?projectId={}&providerId={}&chainId=",
ctx.server.public_addr, ctx.server.project_id, provider_id
);

let client = Client::builder().build::<_, hyper::Body>(HttpsConnector::new());
Expand Down
Loading

0 comments on commit d9f7377

Please sign in to comment.