Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c634cc

Browse files
committed
core,graph,runtime: Avoid creating duplicate runtime hosts, log warning
1 parent eba8b42 commit 1c634cc

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

core/src/subgraph/instance.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ where
272272
data_source: DataSource,
273273
top_level_templates: Arc<Vec<DataSourceTemplate>>,
274274
metrics: Arc<HostMetrics>,
275-
) -> Result<Arc<T::Host>, anyhow::Error> {
275+
) -> Result<Option<Arc<T::Host>>, anyhow::Error> {
276276
// Protect against creating more than the allowed maximum number of data sources
277277
if let Some(max_data_sources) = *MAX_DATA_SOURCES {
278278
if self.hosts.len() >= max_data_sources {
@@ -289,7 +289,12 @@ where
289289
top_level_templates,
290290
metrics.clone(),
291291
)?);
292-
self.hosts.push(host.clone());
293-
Ok(host)
292+
293+
Ok(if self.hosts.contains(&host) {
294+
None
295+
} else {
296+
self.hosts.push(host.clone());
297+
Some(host)
298+
})
294299
}
295300
}

core/src/subgraph/instance_manager.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,22 @@ where
964964
host_metrics.clone(),
965965
)?;
966966

967-
data_sources.push(data_source);
968-
runtime_hosts.push(host);
967+
match host {
968+
Some(host) => {
969+
data_sources.push(data_source);
970+
runtime_hosts.push(host);
971+
}
972+
None => warn!(
973+
logger,
974+
"no runtime hosted created, there is already a runtime host instantiated for \
975+
this data source";
976+
"name" => &data_source.name,
977+
"address" => &data_source.source.address
978+
.map(|address| address.to_string())
979+
.unwrap_or("none".to_string()),
980+
"abi" => &data_source.source.abi
981+
),
982+
}
969983
}
970984

971985
Ok((data_sources, runtime_hosts))

graph/src/components/subgraph/host.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::PartialEq;
12
use std::fmt;
23
use std::sync::Arc;
34

@@ -145,7 +146,7 @@ impl HostMetrics {
145146
}
146147

147148
pub trait RuntimeHostBuilder: Clone + Send + Sync + 'static {
148-
type Host: RuntimeHost;
149+
type Host: RuntimeHost + PartialEq;
149150
type Req: 'static + Send;
150151

151152
/// Build a new runtime host for a subgraph data source.

graph/src/components/subgraph/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ pub trait SubgraphInstance<H: RuntimeHost> {
6161
data_source: DataSource,
6262
top_level_templates: Arc<Vec<DataSourceTemplate>>,
6363
metrics: Arc<HostMetrics>,
64-
) -> Result<Arc<H>, anyhow::Error>;
64+
) -> Result<Option<Arc<H>>, anyhow::Error>;
6565
}

graph/src/data/subgraph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl From<EthereumContractAbiEntity> for UnresolvedMappingABI {
488488
}
489489
}
490490

491-
#[derive(Clone, Debug)]
491+
#[derive(Clone, Debug, PartialEq)]
492492
pub struct MappingABI {
493493
pub name: String,
494494
pub contract: Contract,

runtime/wasm/src/host.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::PartialEq;
12
use std::collections::HashMap;
23
use std::str::FromStr;
34
use std::time::{Duration, Instant};
@@ -719,3 +720,16 @@ impl RuntimeHostTrait for RuntimeHost {
719720
.await
720721
}
721722
}
723+
724+
impl PartialEq for RuntimeHost {
725+
fn eq(&self, other: &Self) -> bool {
726+
// mapping_request_sender, host_exports and host_metrics are operational structs not needed
727+
// to define uniqueness; each runtime host should be for a unique data source.
728+
self.data_source_name == other.data_source_name
729+
&& self.data_source_contract == other.data_source_contract
730+
&& self.data_source_contract_abi == other.data_source_contract_abi
731+
&& self.data_source_event_handlers == other.data_source_event_handlers
732+
&& self.data_source_call_handlers == other.data_source_call_handlers
733+
&& self.data_source_block_handlers == other.data_source_block_handlers
734+
}
735+
}

0 commit comments

Comments
 (0)