Skip to content

Commit 9a186ad

Browse files
authored
feat: Implement plan-based request restrictions (#868)
2 parents 4d7842d + c8c54de commit 9a186ad

File tree

10 files changed

+1481
-965
lines changed

10 files changed

+1481
-965
lines changed

tycho-indexer/src/cli.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clap::{Args, Parser, Subcommand};
22
use tycho_common::{models::Chain, Bytes};
33
use tycho_ethereum::rpc::{config::RPCRetryConfig, EthereumRpcClient};
44

5-
use crate::{extractor::ExtractionError, services::ServerRpcConfig};
5+
use crate::extractor::ExtractionError;
66

77
/// Tycho Indexer using Substreams
88
///
@@ -79,10 +79,6 @@ pub struct GlobalArgs {
7979
/// RPC configuration (URL and retry settings)
8080
#[command(flatten)]
8181
pub rpc: RPCArgs,
82-
83-
/// Tycho RPC server configuration (minimum filtering thresholds)
84-
#[command(flatten)]
85-
pub server: ServerArgs,
8682
}
8783

8884
/// RPC configuration arguments (url, retry settings, and potentially others, such as batching)
@@ -105,38 +101,6 @@ pub struct RPCArgs {
105101
pub max_backoff_ms: u64,
106102
}
107103

108-
/// Tycho RPC server configuration (minimum filtering thresholds)
109-
#[derive(Args, Debug, Clone, PartialEq)]
110-
pub struct ServerArgs {
111-
/// Minimum TVL threshold for RPC responses (in chain's native token)
112-
/// Components requests with TVL below this value or with no TVL will be rejected
113-
#[clap(long = "rpc-min-tvl", env = "RPC_MIN_TVL")]
114-
pub min_tvl: Option<f64>,
115-
116-
/// Minimum token quality threshold for RPC responses
117-
/// Tokens requests with quality below this value will be rejected
118-
#[clap(long = "rpc-min-token-quality", env = "RPC_MIN_TOKEN_QUALITY")]
119-
pub min_token_quality: Option<i32>,
120-
121-
/// Maximum traded_n_days_ago threshold for RPC responses
122-
/// Tokens requests with traded_n_days_ago above this value will be rejected
123-
#[clap(
124-
long = "rpc-max-traded-n-days-ago",
125-
env = "RPC_MAX_TRADED_N_DAYS_AGO",
126-
alias = "rpc-min-traded-n-days-ago" // to ensure backward compatibility, TODO: remove after next prod release
127-
)]
128-
pub max_traded_n_days_ago: Option<u64>,
129-
}
130-
131-
impl From<ServerArgs> for ServerRpcConfig {
132-
fn from(args: ServerArgs) -> Self {
133-
Self::new()
134-
.with_min_tvl(args.min_tvl)
135-
.with_min_quality(args.min_token_quality)
136-
.with_max_traded_n_days_ago(args.max_traded_n_days_ago)
137-
}
138-
}
139-
140104
impl RPCArgs {
141105
pub fn build_client(&self) -> Result<EthereumRpcClient, ExtractionError> {
142106
let retry_config =
@@ -314,11 +278,6 @@ mod cli_tests {
314278
initial_backoff_ms: 150,
315279
max_backoff_ms: 5000,
316280
},
317-
server: ServerArgs {
318-
min_tvl: None,
319-
min_token_quality: None,
320-
max_traded_n_days_ago: None,
321-
},
322281
},
323282
command: Command::Run(RunSpkgArgs {
324283
chain: "ethereum".to_string(),
@@ -381,11 +340,6 @@ mod cli_tests {
381340
initial_backoff_ms: 200,
382341
max_backoff_ms: 10000,
383342
},
384-
server: ServerArgs {
385-
min_tvl: None,
386-
min_token_quality: None,
387-
max_traded_n_days_ago: None,
388-
},
389343
},
390344
command: Command::Index(IndexArgs {
391345
substreams_args: SubstreamsArgs {

tycho-indexer/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use tycho_indexer::{
5858
token_analysis_cron::analyze_tokens,
5959
ExtractionError,
6060
},
61-
services::ServicesBuilder,
61+
services::{PlansConfig, ServicesBuilder},
6262
};
6363
use tycho_storage::postgres::{builder::GatewayBuilder, cache::CachedGateway};
6464

@@ -337,12 +337,14 @@ async fn run_rpc(global_args: GlobalArgs) -> Result<(), ExtractionError> {
337337
ExtractionError::Setup("AUTH_API_KEY environment variable is not set".to_string())
338338
})?;
339339

340+
let plans_config = PlansConfig::from_yaml("./plans.yaml").map_err(ExtractionError::Setup)?;
341+
340342
let (server_handle, server_task) =
341343
ServicesBuilder::new(direct_gw.clone(), rpc_client.clone(), api_key)
342344
.prefix(&global_args.server_version_prefix)
343345
.bind(&global_args.server_ip)
344346
.port(global_args.server_port)
345-
.server_rpc_config(global_args.server.into())
347+
.plans_config(plans_config)
346348
.run()?;
347349
info!(server_url, "Http and Ws server started");
348350
let shutdown_task = tokio::spawn(shutdown_handler(server_handle, vec![], None));
@@ -406,12 +408,14 @@ async fn create_indexing_tasks(
406408
let api_key = env::var("AUTH_API_KEY").map_err(|_| {
407409
ExtractionError::Setup("AUTH_API_KEY environment variable is not set".to_string())
408410
})?;
411+
let plans_config = PlansConfig::from_yaml("./plans.yaml").map_err(ExtractionError::Setup)?;
412+
409413
let (server_handle, server_task) =
410414
ServicesBuilder::new(cached_gw.clone(), rpc_client.clone(), api_key)
411415
.prefix(&global_args.server_version_prefix)
412416
.bind(&global_args.server_ip)
413417
.port(global_args.server_port)
414-
.server_rpc_config(global_args.server.clone().into())
418+
.plans_config(plans_config)
415419
.dci_protocols(dci_protocols)
416420
.protocol_systems(protocol_systems)
417421
.register_extractors(extractor_handles.clone())

0 commit comments

Comments
 (0)