Skip to content

Commit 4baec90

Browse files
authored
make DS Runtime Optional (#3766)
1 parent 7707967 commit 4baec90

File tree

13 files changed

+58
-35
lines changed

13 files changed

+58
-35
lines changed

chain/arweave/src/data_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ impl blockchain::DataSource<Chain> for DataSource {
162162
self.mapping.api_version.clone()
163163
}
164164

165-
fn runtime(&self) -> &[u8] {
166-
self.mapping.runtime.as_ref()
165+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
166+
Some(self.mapping.runtime.cheap_clone())
167167
}
168168
}
169169

@@ -289,8 +289,8 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
289289
self.mapping.api_version.clone()
290290
}
291291

292-
fn runtime(&self) -> &[u8] {
293-
self.mapping.runtime.as_ref()
292+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
293+
Some(self.mapping.runtime.cheap_clone())
294294
}
295295
}
296296

chain/cosmos/src/data_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ impl blockchain::DataSource<Chain> for DataSource {
197197
self.mapping.api_version.clone()
198198
}
199199

200-
fn runtime(&self) -> &[u8] {
201-
self.mapping.runtime.as_ref()
200+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
201+
Some(self.mapping.runtime.cheap_clone())
202202
}
203203
}
204204

@@ -339,7 +339,7 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
339339
unimplemented!("{}", TEMPLATE_ERROR);
340340
}
341341

342-
fn runtime(&self) -> &[u8] {
342+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
343343
unimplemented!("{}", TEMPLATE_ERROR);
344344
}
345345
}

chain/ethereum/src/data_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl blockchain::DataSource<Chain> for DataSource {
219219
self.mapping.api_version.clone()
220220
}
221221

222-
fn runtime(&self) -> &[u8] {
223-
self.mapping.runtime.as_ref()
222+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
223+
Some(self.mapping.runtime.cheap_clone())
224224
}
225225
}
226226

@@ -836,8 +836,8 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
836836
self.mapping.api_version.clone()
837837
}
838838

839-
fn runtime(&self) -> &[u8] {
840-
self.mapping.runtime.as_ref()
839+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
840+
Some(self.mapping.runtime.cheap_clone())
841841
}
842842
}
843843

chain/near/src/data_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl blockchain::DataSource<Chain> for DataSource {
219219
self.mapping.api_version.clone()
220220
}
221221

222-
fn runtime(&self) -> &[u8] {
223-
self.mapping.runtime.as_ref()
222+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
223+
Some(self.mapping.runtime.cheap_clone())
224224
}
225225
}
226226

@@ -374,8 +374,8 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
374374
self.mapping.api_version.clone()
375375
}
376376

377-
fn runtime(&self) -> &[u8] {
378-
self.mapping.runtime.as_ref()
377+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
378+
Some(self.mapping.runtime.cheap_clone())
379379
}
380380
}
381381

chain/substreams/src/data_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl blockchain::DataSource<Chain> for DataSource {
5858
todo!()
5959
}
6060

61-
fn runtime(&self) -> &[u8] {
61+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
6262
todo!()
6363
}
6464

@@ -145,7 +145,7 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
145145
todo!()
146146
}
147147

148-
fn runtime(&self) -> &[u8] {
148+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
149149
todo!()
150150
}
151151

core/src/subgraph/instance.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,16 @@ where
7272
// we use the same order here as in the subgraph manifest to make the
7373
// event processing behavior predictable
7474
for ds in manifest.data_sources {
75+
let runtime = ds.runtime();
76+
let module_bytes = match runtime {
77+
None => continue,
78+
Some(ref module_bytes) => module_bytes,
79+
};
80+
7581
let host = this.new_host(
7682
logger.cheap_clone(),
7783
ds,
84+
module_bytes,
7885
templates.cheap_clone(),
7986
host_metrics.cheap_clone(),
8087
)?;
@@ -84,21 +91,23 @@ where
8491
Ok(this)
8592
}
8693

94+
// module_bytes is the same as data_source.runtime().unwrap(), this is to ensure that this
95+
// function is only called for data_sources for which data_source.runtime().is_some() is true.
8796
fn new_host(
8897
&mut self,
8998
logger: Logger,
9099
data_source: C::DataSource,
100+
module_bytes: &Arc<Vec<u8>>,
91101
templates: Arc<Vec<C::DataSourceTemplate>>,
92102
host_metrics: Arc<HostMetrics>,
93103
) -> Result<T::Host, Error> {
94104
let mapping_request_sender = {
95-
let module_bytes = data_source.runtime();
96-
let module_hash = tiny_keccak::keccak256(module_bytes);
105+
let module_hash = tiny_keccak::keccak256(module_bytes.as_ref());
97106
if let Some(sender) = self.module_cache.get(&module_hash) {
98107
sender.clone()
99108
} else {
100109
let sender = T::spawn_mapping(
101-
module_bytes.to_owned(),
110+
module_bytes.as_ref(),
102111
logger,
103112
self.subgraph_id.clone(),
104113
host_metrics.clone(),
@@ -167,7 +176,18 @@ where
167176
<= data_source.creation_block()
168177
);
169178

170-
let host = Arc::new(self.new_host(logger.clone(), data_source, templates, metrics)?);
179+
let module_bytes = match &data_source.runtime() {
180+
None => return Ok(None),
181+
Some(ref module_bytes) => module_bytes.cheap_clone(),
182+
};
183+
184+
let host = Arc::new(self.new_host(
185+
logger.clone(),
186+
data_source,
187+
&module_bytes,
188+
templates,
189+
metrics,
190+
)?);
171191

172192
Ok(if self.hosts.contains(&host) {
173193
None

graph/src/blockchain/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<C: Blockchain> DataSource<C> for MockDataSource {
8787
todo!()
8888
}
8989

90-
fn runtime(&self) -> &[u8] {
90+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
9191
todo!()
9292
}
9393

@@ -142,7 +142,7 @@ impl<C: Blockchain> DataSourceTemplate<C> for MockDataSourceTemplate {
142142
todo!()
143143
}
144144

145-
fn runtime(&self) -> &[u8] {
145+
fn runtime(&self) -> Option<Arc<Vec<u8>>> {
146146
todo!()
147147
}
148148

graph/src/blockchain/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub trait DataSource<C: Blockchain>:
199199
fn context(&self) -> Arc<Option<DataSourceContext>>;
200200
fn creation_block(&self) -> Option<BlockNumber>;
201201
fn api_version(&self) -> semver::Version;
202-
fn runtime(&self) -> &[u8];
202+
fn runtime(&self) -> Option<Arc<Vec<u8>>>;
203203

204204
/// Checks if `trigger` matches this data source, and if so decodes it into a `MappingTrigger`.
205205
/// A return of `Ok(None)` mean the trigger does not match.
@@ -245,7 +245,7 @@ pub trait UnresolvedDataSourceTemplate<C: Blockchain>:
245245

246246
pub trait DataSourceTemplate<C: Blockchain>: Send + Sync + Debug {
247247
fn api_version(&self) -> semver::Version;
248-
fn runtime(&self) -> &[u8];
248+
fn runtime(&self) -> Option<Arc<Vec<u8>>>;
249249
fn name(&self) -> &str;
250250
}
251251

graph/src/components/subgraph/host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub trait RuntimeHostBuilder<C: Blockchain>: Clone + Send + Sync + 'static {
160160
/// Spawn a mapping and return a channel for mapping requests. The sender should be able to be
161161
/// cached and shared among mappings that use the same wasm file.
162162
fn spawn_mapping(
163-
raw_module: Vec<u8>,
163+
raw_module: &[u8],
164164
logger: Logger,
165165
subgraph_id: DeploymentHash,
166166
metrics: Arc<HostMetrics>,

graph/src/data/subgraph/features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn detect_ipfs_on_ethereum_contracts<C: Blockchain>(
135135
) -> Result<Option<SubgraphFeature>, InvalidMapping> {
136136
for runtime in manifest.runtimes() {
137137
for function_name in IPFS_ON_ETHEREUM_CONTRACTS_FUNCTION_NAMES {
138-
if calls_host_fn(runtime, function_name).map_err(|_| InvalidMapping)? {
138+
if calls_host_fn(&runtime, function_name).map_err(|_| InvalidMapping)? {
139139
return Ok(Some(SubgraphFeature::IpfsOnEthereumContracts));
140140
}
141141
}

0 commit comments

Comments
 (0)