-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wasm on the Web #1990
Wasm on the Web #1990
Changes from all commits
1f2c3e6
af4d2b0
0a97b7e
ee07852
e230fac
5172b58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ use linera_witty::{ | |
}; | ||
use tokio::sync::Mutex; | ||
use wasm_instrument::{gas_metering, parity_wasm}; | ||
use wasmer::{sys::EngineBuilder, Cranelift, Engine, Module, Singlepass, Store}; | ||
use wasmer::{Engine, Module, Store}; | ||
|
||
use super::{ | ||
module_cache::ModuleCache, | ||
|
@@ -28,8 +28,15 @@ use crate::{ | |
|
||
/// An [`Engine`] instance configured to run application services. | ||
static SERVICE_ENGINE: Lazy<Engine> = Lazy::new(|| { | ||
let compiler_config = Cranelift::new(); | ||
EngineBuilder::new(compiler_config).into() | ||
#[cfg(web)] | ||
{ | ||
wasmer::Engine::default() | ||
} | ||
|
||
#[cfg(not(web))] | ||
{ | ||
wasmer::sys::EngineBuilder::new(wasmer::Cranelift::new()).into() | ||
} | ||
}); | ||
|
||
/// A cache of compiled contract modules, with their respective [`Engine`] instances. | ||
|
@@ -65,7 +72,7 @@ impl WasmContractModule { | |
|
||
impl<Runtime> WasmerContractInstance<Runtime> | ||
where | ||
Runtime: ContractRuntime + WriteBatch + Clone + Send + Sync + Unpin + 'static, | ||
Runtime: ContractRuntime + WriteBatch + Clone + Unpin + 'static, | ||
{ | ||
/// Prepares a runtime instance to call into the Wasm contract. | ||
pub fn prepare( | ||
|
@@ -100,7 +107,7 @@ impl WasmServiceModule { | |
|
||
impl<Runtime> WasmerServiceInstance<Runtime> | ||
where | ||
Runtime: ServiceRuntime + WriteBatch + Clone + Send + Sync + Unpin + 'static, | ||
Runtime: ServiceRuntime + WriteBatch + Clone + Unpin + 'static, | ||
{ | ||
/// Prepares a runtime instance to call into the Wasm service. | ||
pub fn prepare(service_module: &Module, runtime: Runtime) -> Result<Self, WasmExecutionError> { | ||
|
@@ -118,7 +125,7 @@ where | |
|
||
impl<Runtime> crate::UserContract for WasmerContractInstance<Runtime> | ||
where | ||
Runtime: ContractRuntime + Send + Unpin + 'static, | ||
Runtime: ContractRuntime + Unpin + 'static, | ||
{ | ||
fn instantiate( | ||
&mut self, | ||
|
@@ -160,10 +167,7 @@ where | |
} | ||
} | ||
|
||
impl<Runtime> crate::UserService for WasmerServiceInstance<Runtime> | ||
where | ||
Runtime: Send + 'static, | ||
{ | ||
impl<Runtime: 'static> crate::UserService for WasmerServiceInstance<Runtime> { | ||
fn handle_query( | ||
&mut self, | ||
_context: QueryContext, | ||
|
@@ -253,17 +257,21 @@ impl CachedContractModule { | |
|
||
/// Creates a new [`Engine`] to compile a contract bytecode. | ||
fn create_compilation_engine() -> Engine { | ||
let mut compiler_config = Singlepass::default(); | ||
compiler_config.canonicalize_nans(true); | ||
#[cfg(not(web))] | ||
{ | ||
let mut compiler_config = wasmer::Singlepass::default(); | ||
compiler_config.canonicalize_nans(true); | ||
|
||
wasmer::sys::EngineBuilder::new(compiler_config).into() | ||
} | ||
|
||
EngineBuilder::new(compiler_config).into() | ||
#[cfg(web)] | ||
wasmer::Engine::default() | ||
} | ||
|
||
/// Creates a [`Module`] from a compiled contract using a headless [`Engine`]. | ||
pub fn create_execution_instance(&self) -> Result<(Engine, Module), anyhow::Error> { | ||
use wasmer::NativeEngineExt; | ||
|
||
let engine = Engine::headless(); | ||
let engine = Engine::default(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe @jvff to confirm this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed on call, this should be an improbable edge case that doesn't affect consensus, only the possibility of a client proposing a block. We should fix it eventually, but it's not a blocker. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an issue tracking this future work: #2103. |
||
let store = Store::new(engine.clone()); | ||
let module = unsafe { Module::deserialize(&store, &*self.compiled_bytecode) }?; | ||
Ok((engine, module)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove newline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the newline is important here to visually separate the setup from the use of
compiler_config
.(Ideally it would be a builder! And then the whole thing would be one expression.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this is all doing one thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's small enough that I'm not particularly bothered by it. I think if we decide to make changes elsewhere I'll include it in the next push — otherwise I'd like to merge it without waiting another hour for CI so I can point
linera-web
to it 😅