Skip to content

Commit

Permalink
instantiate externs for version check
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Oct 10, 2023
1 parent 820f7af commit c552b70
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/fuel-indexer-api-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl IntoResponse for ApiError {
(StatusCode::BAD_REQUEST, format!("Invalid asset type: {e}"))
}
ApiError::ToolchainVersionMismatch{fuel_indexer_version, toolchain_version} => {
(StatusCode::METHOD_NOT_ALLOWED, format!("The toolchain version {toolchain_version} that the WASM module was compiled with does not match the fuel-indexer-versiion {fuel_indexer_version}"))
(StatusCode::METHOD_NOT_ALLOWED, format!("WASM module toolchain version `{toolchain_version}` does not match fuel-indexer version `{fuel_indexer_version}`"))
}
_ => (StatusCode::INTERNAL_SERVER_ERROR, generic_details),
};
Expand Down
29 changes: 27 additions & 2 deletions packages/fuel-indexer-api-server/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
use wasmer::{AsStoreMut, Instance, MemoryView, StoreMut, WasmPtr};
use wasmer::{
imports, AsStoreMut, Exports, Function, Instance, MemoryView, StoreMut, WasmPtr,
};

pub(crate) fn check_wasm_toolchain_version(data: Vec<u8>) -> anyhow::Result<String> {
let mut store = wasmer::Store::default();

let module = wasmer::Module::new(&store, data.clone())?;

let imports = wasmer::imports! {};
let mut exports = Exports::new();
exports.insert(
"ff_put_object".to_string(),
Function::new_typed(&mut store, |_: i64, _: i32, _: i32| {}),
);
exports.insert(
"ff_get_object".to_string(),
Function::new_typed(&mut store, |_: i64, _: i32, _: i32| 0i32),
);
exports.insert(
"ff_early_exit".to_string(),
Function::new_typed(&mut store, |_: i32| {}),
);
exports.insert(
"ff_put_many_to_many_record".to_string(),
Function::new_typed(&mut store, |_: i32, _: i32| {}),
);
exports.insert(
"ff_log_data".to_string(),
Function::new_typed(&mut store, |_: i32, _: i32, _: i32| {}),
);

let mut imports = imports! {};
wasmer::Imports::register_namespace(&mut imports, "env", exports);

let instance = wasmer::Instance::new(&mut store, &module, &imports)?;

Expand Down
8 changes: 7 additions & 1 deletion packages/fuel-indexer-api-server/src/uses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,13 @@ async fn parse_register_indexer_multipart(
if asset_type == IndexerAssetType::Wasm {
toolchain_version =
crate::ffi::check_wasm_toolchain_version(data.clone().into())
.unwrap_or("none".to_string());
.map_err(|e| {
tracing::warn!(
"Failed to get WASM module toolchain version: {e}"
);
e
})
.unwrap_or("unknown".to_string());
};
assets.push((asset_type, data.to_vec()));
}
Expand Down

0 comments on commit c552b70

Please sign in to comment.