Skip to content

Commit

Permalink
feat: support chain aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Dec 6, 2023
1 parent 3b50a68 commit 0115852
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions graph-gateway/src/chains/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl super::Client for Client {
type Config = config::Chain;

fn chain_name(config: &Self::Config) -> &str {
&config.name
&config.names[0]
}

fn poll_interval() -> Duration {
Expand All @@ -34,7 +34,7 @@ impl super::Client for Client {
chain: config::Chain,
notify: mpsc::UnboundedSender<ClientMsg>,
) -> mpsc::UnboundedSender<UnresolvedBlock> {
let _trace = tracing::info_span!("Ethereum Client Actor", chain = %chain.name).entered();
let _trace = tracing::info_span!("ethereum_client", chain = %chain.names[0]).entered();
let (unresolved_tx, mut unresolved_rx) = mpsc::unbounded_channel();
let mut client = Self {
chain,
Expand Down Expand Up @@ -66,7 +66,7 @@ impl super::Client for Client {
impl Client {
async fn spawn_block_fetch(&mut self, unresolved: Option<UnresolvedBlock>) {
let client = self.http_client.clone();
let chain = self.chain.name.clone();
let chain = self.chain.names[0].clone();
let rpc = self.chain.rpc.clone();
let notify = self.notify.clone();
tokio::spawn(async move {
Expand Down
9 changes: 4 additions & 5 deletions graph-gateway/src/client_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct Context {
pub budgeter: &'static Budgeter,
pub indexer_selection_retry_limit: usize,
pub l2_gateway: Option<Url>,
pub block_caches: &'static HashMap<String, BlockCache>,
pub block_caches: &'static HashMap<String, &'static BlockCache>,
pub network: GraphNetwork,
pub indexing_statuses: Eventual<Ptr<HashMap<Indexing, IndexingStatus>>>,
pub attestation_domain: &'static Eip712Domain,
Expand Down Expand Up @@ -444,10 +444,9 @@ async fn handle_client_query_inner(
})
.collect();

let block_cache = ctx
let block_cache = *ctx
.block_caches
.get(&subgraph_chain)
.cloned()
.ok_or_else(|| Error::SubgraphChainNotSupported(subgraph_chain))?;

let block_constraints = block_constraints(&context)
Expand Down Expand Up @@ -612,7 +611,7 @@ async fn handle_client_query_inner(
};

let latest_query_block = pick_latest_query_block(
&block_cache,
block_cache,
latest_block.number.saturating_sub(selection.blocks_behind),
blocks_per_minute,
)
Expand Down Expand Up @@ -642,7 +641,7 @@ async fn handle_client_query_inner(
&latest_block,
&latest_query_block,
&utility_params.requirements,
&block_cache,
block_cache,
&selection,
)
.await;
Expand Down
3 changes: 2 additions & 1 deletion graph-gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub struct AttestationConfig {
#[serde_as]
#[derive(Clone, Debug, Deserialize)]
pub struct Chain {
pub name: String,
/// The first name is used in logs, the others are aliases also supported in subgraph manifests.
pub names: Vec<String>,
#[serde_as(as = "DisplayFromStr")]
pub rpc: Url,
}
Expand Down
15 changes: 8 additions & 7 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ async fn main() {
.filter(|_| !config.geoip_blocked_countries.is_empty())
.map(|db| GeoIP::new(db, config.geoip_blocked_countries).unwrap());

let block_caches = config
let block_caches: HashMap<String, &'static BlockCache> = config
.chains
.into_iter()
.map(|chain| {
let network = chain.name.clone();
let cache = BlockCache::new::<ethereum::Client>(chain);
(network, cache)
.flat_map(|chain| {
let cache: &'static BlockCache =
Box::leak(Box::new(BlockCache::new::<ethereum::Client>(chain.clone())));
chain.names.into_iter().map(move |alias| (alias, cache))
})
.collect::<HashMap<String, BlockCache>>();
let block_caches: &'static HashMap<String, BlockCache> = Box::leak(Box::new(block_caches));
.collect();
let block_caches: &'static HashMap<String, &'static BlockCache> =
Box::leak(Box::new(block_caches));

let http_client = reqwest::Client::builder()
.timeout(Duration::from_secs(20))
Expand Down

0 comments on commit 0115852

Please sign in to comment.