Skip to content

Commit dab5acf

Browse files
committed
runtime: Disable ipfs.cat by default
1 parent d1ed689 commit dab5acf

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

runtime/wasm/src/host.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ use crate::mapping::{MappingContext, MappingRequest, MappingTrigger};
3030

3131
pub(crate) const TIMEOUT_ENV_VAR: &str = "GRAPH_MAPPING_HANDLER_TIMEOUT";
3232

33+
lazy_static! {
34+
pub static ref TIMEOUT: Option<Duration> = std::env::var(TIMEOUT_ENV_VAR)
35+
.ok()
36+
.map(|s| u64::from_str(&s).expect("Invalid value for GRAPH_MAPPING_HANDLER_TIMEOUT"))
37+
.map(Duration::from_secs);
38+
static ref ALLOW_NON_DETERMINISTIC_IPFS: bool =
39+
std::env::var("GRAPH_ALLOW_NON_DETERMINISTIC_IPFS").is_ok();
40+
}
41+
3342
struct RuntimeHostConfig {
3443
subgraph_id: SubgraphDeploymentId,
3544
mapping: Mapping,
@@ -97,18 +106,14 @@ where
97106
subgraph_id: SubgraphDeploymentId,
98107
metrics: Arc<HostMetrics>,
99108
) -> Result<Sender<Self::Req>, anyhow::Error> {
100-
let timeout = std::env::var(TIMEOUT_ENV_VAR)
101-
.ok()
102-
.and_then(|s| u64::from_str(&s).ok())
103-
.map(Duration::from_secs);
104-
105109
crate::mapping::spawn_module(
106110
raw_module,
107111
logger,
108112
subgraph_id,
109113
metrics,
110114
tokio::runtime::Handle::current(),
111-
timeout,
115+
*TIMEOUT,
116+
*ALLOW_NON_DETERMINISTIC_IPFS,
112117
)
113118
}
114119

runtime/wasm/src/host_exports.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ impl HostExports {
413413
ctx.derive_with_empty_block_state(),
414414
host_metrics.clone(),
415415
module.timeout,
416+
module.allow_non_determinstic_ipfs,
416417
)?;
417418
let result = module.handle_json_callback(&callback, &sv.value, &user_data)?;
418419
// Log progress every 15s

runtime/wasm/src/mapping.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn spawn_module(
2020
host_metrics: Arc<HostMetrics>,
2121
runtime: tokio::runtime::Handle,
2222
timeout: Option<Duration>,
23+
allow_non_deterministic_ipfs: bool,
2324
) -> Result<mpsc::Sender<MappingRequest>, anyhow::Error> {
2425
let valid_module = Arc::new(ValidModule::new(&raw_module)?);
2526

@@ -54,6 +55,7 @@ pub fn spawn_module(
5455
ctx,
5556
host_metrics.clone(),
5657
timeout,
58+
allow_non_deterministic_ipfs,
5759
)?;
5860
section.end();
5961

runtime/wasm/src/module/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ pub(crate) struct WasmInstanceContext {
246246

247247
// A trap ocurred due to a possible reorg detection.
248248
possible_reorg: bool,
249+
250+
pub(crate) allow_non_determinstic_ipfs: bool,
249251
}
250252

251253
impl WasmInstance {
@@ -255,6 +257,7 @@ impl WasmInstance {
255257
ctx: MappingContext,
256258
host_metrics: Arc<HostMetrics>,
257259
timeout: Option<Duration>,
260+
allow_non_determinstic_ipfs: bool,
258261
) -> Result<WasmInstance, anyhow::Error> {
259262
let mut linker = wasmtime::Linker::new(&wasmtime::Store::new(valid_module.module.engine()));
260263

@@ -322,6 +325,7 @@ impl WasmInstance {
322325
host_metrics.cheap_clone(),
323326
timeout,
324327
timeout_stopwatch.cheap_clone(),
328+
allow_non_determinstic_ipfs
325329
).unwrap())
326330
}
327331

@@ -366,6 +370,7 @@ impl WasmInstance {
366370
host_metrics.cheap_clone(),
367371
timeout,
368372
timeout_stopwatch.cheap_clone(),
373+
allow_non_determinstic_ipfs,
369374
)
370375
.unwrap(),
371376
)
@@ -482,6 +487,7 @@ impl WasmInstance {
482487
host_metrics,
483488
timeout,
484489
timeout_stopwatch,
490+
allow_non_determinstic_ipfs,
485491
)?);
486492
}
487493

@@ -548,6 +554,7 @@ impl WasmInstanceContext {
548554
host_metrics: Arc<HostMetrics>,
549555
timeout: Option<Duration>,
550556
timeout_stopwatch: Arc<std::sync::Mutex<TimeoutStopwatch>>,
557+
allow_non_determinstic_ipfs: bool,
551558
) -> Result<Self, anyhow::Error> {
552559
// Provide access to the WASM runtime linear memory
553560
let memory = instance
@@ -570,6 +577,7 @@ impl WasmInstanceContext {
570577
arena_free_size: 0,
571578
arena_start_ptr: 0,
572579
possible_reorg: false,
580+
allow_non_determinstic_ipfs,
573581
})
574582
}
575583

@@ -580,6 +588,7 @@ impl WasmInstanceContext {
580588
host_metrics: Arc<HostMetrics>,
581589
timeout: Option<Duration>,
582590
timeout_stopwatch: Arc<std::sync::Mutex<TimeoutStopwatch>>,
591+
allow_non_determinstic_ipfs: bool,
583592
) -> Result<Self, anyhow::Error> {
584593
let memory = caller
585594
.get_export("memory")
@@ -605,6 +614,7 @@ impl WasmInstanceContext {
605614
arena_free_size: 0,
606615
arena_start_ptr: 0,
607616
possible_reorg: false,
617+
allow_non_determinstic_ipfs,
608618
})
609619
}
610620
}
@@ -805,6 +815,13 @@ impl WasmInstanceContext {
805815

806816
/// function ipfs.cat(link: String): Bytes
807817
fn ipfs_cat(&mut self, link_ptr: AscPtr<AscString>) -> Result<AscPtr<Uint8Array>, Trap> {
818+
if !self.allow_non_determinstic_ipfs {
819+
return Err(anyhow::anyhow!(
820+
"`ipfs.cat` is deprecated. Improved support for IPFS will be added in the future"
821+
)
822+
.into());
823+
}
824+
808825
let link = self.asc_get(link_ptr);
809826
let ipfs_res = self.ctx.host_exports.ipfs_cat(&self.ctx.logger, link);
810827
match ipfs_res {
@@ -831,6 +848,13 @@ impl WasmInstanceContext {
831848
user_data: AscPtr<AscEnum<StoreValueKind>>,
832849
flags: AscPtr<Array<AscPtr<AscString>>>,
833850
) -> Result<(), Trap> {
851+
if !self.allow_non_determinstic_ipfs {
852+
return Err(anyhow::anyhow!(
853+
"`ipfs.map` is deprecated. Improved support for IPFS will be added in the future"
854+
)
855+
.into());
856+
}
857+
834858
let link: String = self.asc_get(link_ptr);
835859
let callback: String = self.asc_get(callback);
836860
let user_data: store::Value = self.try_asc_get(user_data)?;

runtime/wasm/src/module/test.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ fn test_valid_module_and_store(
5959
Arc::new(ValidModule::new(data_source.mapping.runtime.as_ref()).unwrap()),
6060
mock_context(deployment_id, data_source, store.clone()),
6161
host_metrics,
62-
std::env::var(crate::host::TIMEOUT_ENV_VAR)
63-
.ok()
64-
.and_then(|s| u64::from_str(&s).ok())
65-
.map(std::time::Duration::from_secs),
62+
*crate::host::TIMEOUT,
63+
true,
6664
)
6765
.unwrap();
6866

0 commit comments

Comments
 (0)