Skip to content

Commit

Permalink
fix(bridge): remove http request rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Feb 9, 2024
1 parent 8bc086b commit 717f56f
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 135 deletions.
77 changes: 0 additions & 77 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions ethportal-peertest/src/scenarios/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use portal_bridge::{
api::{consensus::ConsensusApi, execution::ExecutionApi},
bridge::{beacon::BeaconBridge, history::HistoryBridge},
cli::Provider,
constants::PROVIDER_DAILY_REQUEST_LIMIT,
types::mode::BridgeMode,
};
use serde_json::Value;
Expand All @@ -24,10 +23,9 @@ pub async fn test_history_bridge(peertest: &Peertest, target: &HttpClient) {
let portal_clients = vec![target.clone()];
let epoch_acc_path = "validation_assets/epoch_acc.bin".into();
let mode = BridgeMode::Test("./test_assets/portalnet/bridge_data.json".into());
let execution_api =
ExecutionApi::new(Provider::Test, mode.clone(), PROVIDER_DAILY_REQUEST_LIMIT)
.await
.unwrap();
let execution_api = ExecutionApi::new(Provider::Test, mode.clone())
.await
.unwrap();
// Wait for bootnode to start
sleep(Duration::from_secs(1)).await;
let bridge = HistoryBridge::new(
Expand Down
1 change: 0 additions & 1 deletion portal-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ serde_yaml = "0.9"
snap = "1.1.1"
ssz_types = "0.5.4"
surf = { version = "2.3.2", default-features = false, features = ["h1-client-rustls", "middleware-logger", "encoding"] } # we use rustils because OpenSSL cause issues compiling on aarch64
surf-governor = "0.2.0"
tokio = { version = "1.14.0", features = ["full"] }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
Expand Down
12 changes: 2 additions & 10 deletions portal-bridge/src/api/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{fmt::Display, num::NonZeroU32};
use std::fmt::Display;

use anyhow::anyhow;
use surf::{Client, Config};
use surf_governor::GovernorMiddleware;
use tracing::debug;
use url::Url;

Expand All @@ -18,7 +17,7 @@ pub struct ConsensusApi {
}

impl ConsensusApi {
pub async fn new(provider: Provider, daily_request_limit: u64) -> Result<Self, surf::Error> {
pub async fn new(provider: Provider) -> Result<Self, surf::Error> {
let client: Client = match provider {
Provider::PandaOps => {
let base_cl_endpoint = Url::parse(&BASE_CL_ENDPOINT)
Expand All @@ -45,13 +44,6 @@ impl ConsensusApi {
)))
}
};
// Limits the number of requests sent to the CL provider in a day
let hourly_request_limit = NonZeroU32::new(daily_request_limit as u32 / 24_u32).ok_or(
anyhow!("Invalid daily request limit, must be greater than 0"),
)?;
let rate_limit = GovernorMiddleware::per_hour(hourly_request_limit)
.expect("Expect GovernerMiddleware should have received a valid Duration");
let client = client.with(rate_limit);
debug!(
"Starting ConsensusApi with provider at url: {:?}",
client.config().base_url
Expand Down
17 changes: 3 additions & 14 deletions portal-bridge/src/api/execution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{num::NonZeroU32, sync::Arc};
use std::sync::Arc;

use anyhow::{anyhow, bail};
use ethereum_types::H256;
Expand All @@ -8,7 +8,6 @@ use surf::{
middleware::{Middleware, Next},
Body, Client, Config, Request, Response,
};
use surf_governor::GovernorMiddleware;
use tokio::time::{sleep, Duration};
use tracing::{debug, warn};
use url::Url;
Expand Down Expand Up @@ -50,11 +49,7 @@ pub struct ExecutionApi {
}

impl ExecutionApi {
pub async fn new(
provider: Provider,
mode: BridgeMode,
daily_request_limit: u64,
) -> Result<Self, surf::Error> {
pub async fn new(provider: Provider, mode: BridgeMode) -> Result<Self, surf::Error> {
let client: Client = match &provider {
Provider::PandaOps => {
let endpoint = match mode {
Expand All @@ -81,13 +76,7 @@ impl ExecutionApi {
.try_into()?,
Provider::Test => Config::new().try_into()?,
};
// Limits the number of requests sent to the EL provider in a day
let hourly_request_limit = NonZeroU32::new(daily_request_limit as u32 / 24_u32).ok_or(
anyhow!("Invalid daily request limit, must be greater than 0"),
)?;
let rate_limit = GovernorMiddleware::per_hour(hourly_request_limit)
.expect("Expect GovernerMiddleware should have received a valid Duration");
let client = client.with(rate_limit).with(Retry::default());
let client = client.with(Retry::default());
// Only check that provider is connected & available if not using a test provider.
debug!(
"Starting ExecutionApi with provider at url: {:?}",
Expand Down
15 changes: 0 additions & 15 deletions portal-bridge/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use url::Url;

use crate::{
client_handles::{fluffy_handle, trin_handle},
constants::PROVIDER_DAILY_REQUEST_LIMIT,
types::{mode::BridgeMode, network::NetworkKind},
};

Expand Down Expand Up @@ -93,20 +92,6 @@ pub struct BridgeConfig {
help = "Data provider for consensus layer data. (\"pandaops\" / local node url)"
)]
pub cl_provider: Provider,

#[arg(
long = "el-provider-daily-request-limit",
help = "Maximum number of requests execution layer sent to the provider in a day.",
default_value_t = PROVIDER_DAILY_REQUEST_LIMIT,
)]
pub el_provider_daily_request_limit: u64,

#[arg(
long = "cl-provider-daily-request-limit",
help = "Maximum number of requests consensus layer sent to the provider in a day.",
default_value_t = PROVIDER_DAILY_REQUEST_LIMIT,
)]
pub cl_provider_daily_request_limit: u64,
}

fn check_node_count(val: &str) -> Result<u8, String> {
Expand Down
1 change: 0 additions & 1 deletion portal-bridge/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ pub const BEACON_GENESIS_TIME: u64 = 1606824023;
// This is a very conservative timeout if a provider takes longer than even 1 second it is probably
// overloaded and not performing well
pub const HTTP_REQUEST_TIMEOUT: Duration = Duration::from_secs(20);
pub const PROVIDER_DAILY_REQUEST_LIMIT: u64 = 1_000_000;
15 changes: 4 additions & 11 deletions portal-bridge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let portal_clients = portal_clients
.clone()
.expect("Failed to create beacon JSON-RPC clients");
let consensus_api = ConsensusApi::new(
bridge_config.cl_provider,
bridge_config.cl_provider_daily_request_limit,
)
.await?;
let consensus_api = ConsensusApi::new(bridge_config.cl_provider).await?;
let bridge_handle = tokio::spawn(async move {
let beacon_bridge =
BeaconBridge::new(consensus_api, bridge_mode, Arc::new(portal_clients));
Expand Down Expand Up @@ -102,12 +98,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
bridge_tasks.push(bridge_handle);
}
_ => {
let execution_api = ExecutionApi::new(
bridge_config.el_provider,
bridge_config.mode.clone(),
bridge_config.el_provider_daily_request_limit,
)
.await?;
let execution_api =
ExecutionApi::new(bridge_config.el_provider, bridge_config.mode.clone())
.await?;
let bridge_handle = tokio::spawn(async move {
let master_acc = MasterAccumulator::default();
let header_oracle = HeaderOracle::new(master_acc);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/test_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use trin_validation::{
pub async fn main() -> Result<()> {
init_tracing_logger();
let config = ProviderConfig::parse();
let api = ExecutionApi::new(Provider::PandaOps, BridgeMode::Latest, 100000)
let api = ExecutionApi::new(Provider::PandaOps, BridgeMode::Latest)
.await
.unwrap();
let latest_block = api.get_latest_block_number().await?;
Expand Down

0 comments on commit 717f56f

Please sign in to comment.