From 065c0d1b0dd76cee7247254a16a699d42fe59f74 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 12:06:24 +0200 Subject: [PATCH 01/11] add HermesReactor --- hermes/bin/src/event_queue/event.rs | 2 +- hermes/bin/src/lib.rs | 2 ++ hermes/bin/src/reactor.rs | 42 +++++++++++++++++++++++++++++ hermes/bin/src/wasm/module.rs | 2 +- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 hermes/bin/src/reactor.rs diff --git a/hermes/bin/src/event_queue/event.rs b/hermes/bin/src/event_queue/event.rs index 18dcb6758..554391781 100644 --- a/hermes/bin/src/event_queue/event.rs +++ b/hermes/bin/src/event_queue/event.rs @@ -3,7 +3,7 @@ use crate::wasm::module::ModuleInstance; /// A trait for defining the behavior of a Hermes event. -pub trait HermesEventPayload { +pub trait HermesEventPayload: Send { /// Returns the name of the event associated with the payload. fn event_name(&self) -> &str; diff --git a/hermes/bin/src/lib.rs b/hermes/bin/src/lib.rs index 802f8a952..379e10035 100644 --- a/hermes/bin/src/lib.rs +++ b/hermes/bin/src/lib.rs @@ -2,6 +2,8 @@ //! This file exists, so that doc tests can be used inside binary crates. mod event_queue; +#[allow(missing_docs, clippy::missing_docs_in_private_items, dead_code)] +mod reactor; mod runtime; mod state; mod wasm; diff --git a/hermes/bin/src/reactor.rs b/hermes/bin/src/reactor.rs new file mode 100644 index 000000000..d956bd1ea --- /dev/null +++ b/hermes/bin/src/reactor.rs @@ -0,0 +1,42 @@ +use std::{ + sync::mpsc::{channel, Receiver, Sender}, + thread, +}; + +use crate::{event_queue::event::HermesEventPayload, wasm::module::Module}; + +pub(crate) struct HermesReactor { + wasm_module: Module, + + event_sender: Sender>, + event_receiver: Receiver>, +} + +impl HermesReactor { + fn event_execution_loop( + mut wasm_module: Module, event_receiver: Receiver>, + ) -> anyhow::Result<()> { + for event in event_receiver { + wasm_module.execute_event(event.as_ref())?; + } + Ok(()) + } + + pub(crate) fn new(app_name: String, module_bytes: &[u8]) -> anyhow::Result { + let wasm_module = Module::new(app_name, module_bytes)?; + let (event_sender, event_receiver) = channel(); + + Ok(Self { + wasm_module, + event_sender, + event_receiver, + }) + } + + pub(crate) fn run(self) -> anyhow::Result<()> { + let events_thread = thread::spawn(|| { + Self::event_execution_loop(self.wasm_module, self.event_receiver).unwrap(); + }); + Ok(()) + } +} diff --git a/hermes/bin/src/wasm/module.rs b/hermes/bin/src/wasm/module.rs index f354694bc..c104b4ec4 100644 --- a/hermes/bin/src/wasm/module.rs +++ b/hermes/bin/src/wasm/module.rs @@ -94,7 +94,7 @@ impl Module { /// # Errors /// - `BadModuleError` #[allow(dead_code)] - pub(crate) fn execute_event(&mut self, event: &impl HermesEventPayload) -> anyhow::Result<()> { + pub(crate) fn execute_event(&mut self, event: &dyn HermesEventPayload) -> anyhow::Result<()> { self.context.use_for(event.event_name().to_string()); let state = HermesState::new(&self.context); From 198ab21f06095e7ac475de6a95df798aa02931f7 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 12:08:31 +0200 Subject: [PATCH 02/11] rename mod --- hermes/bin/src/lib.rs | 2 +- hermes/bin/src/main.rs | 2 +- .../src/{runtime => runtime_extensions}/extensions/bindings.rs | 0 .../bin/src/{runtime => runtime_extensions}/extensions/mod.rs | 2 +- .../bin/src/{runtime => runtime_extensions}/extensions/state.rs | 0 .../{runtime => runtime_extensions}/host/hermes/binary/mod.rs | 2 +- .../{runtime => runtime_extensions}/host/hermes/cardano/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/hermes/cbor/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/hermes/cron/mod.rs | 2 +- .../{runtime => runtime_extensions}/host/hermes/crypto/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/hermes/hash/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/hermes/init/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/hermes/json/mod.rs | 2 +- .../{runtime => runtime_extensions}/host/hermes/kv_store/mod.rs | 2 +- .../host/hermes/localtime/mod.rs | 2 +- .../{runtime => runtime_extensions}/host/hermes/logging/mod.rs | 2 +- .../bin/src/{runtime => runtime_extensions}/host/hermes/mod.rs | 2 +- hermes/bin/src/{runtime => runtime_extensions}/host/mod.rs | 0 .../src/{runtime => runtime_extensions}/host/wasi/cli/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/wasi/clocks/mod.rs | 2 +- .../host/wasi/clocks/monotonic.rs | 2 +- .../{runtime => runtime_extensions}/host/wasi/clocks/wall.rs | 2 +- .../{runtime => runtime_extensions}/host/wasi/filesystem/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/wasi/http/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/wasi/io/error.rs | 2 +- .../bin/src/{runtime => runtime_extensions}/host/wasi/io/mod.rs | 2 +- .../src/{runtime => runtime_extensions}/host/wasi/io/streams.rs | 2 +- hermes/bin/src/{runtime => runtime_extensions}/host/wasi/mod.rs | 2 +- .../host/wasi/random/insecure.rs | 2 +- .../host/wasi/random/insecure_seed.rs | 2 +- .../src/{runtime => runtime_extensions}/host/wasi/random/mod.rs | 2 +- .../{runtime => runtime_extensions}/host/wasi/random/secure.rs | 2 +- hermes/bin/src/{runtime => runtime_extensions}/mod.rs | 0 hermes/bin/src/state.rs | 2 +- hermes/bin/src/wasm/module.rs | 2 +- 35 files changed, 31 insertions(+), 31 deletions(-) rename hermes/bin/src/{runtime => runtime_extensions}/extensions/bindings.rs (100%) rename hermes/bin/src/{runtime => runtime_extensions}/extensions/mod.rs (97%) rename hermes/bin/src/{runtime => runtime_extensions}/extensions/state.rs (100%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/binary/mod.rs (89%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/cardano/mod.rs (99%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/cbor/mod.rs (89%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/cron/mod.rs (99%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/crypto/mod.rs (98%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/hash/mod.rs (96%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/init/mod.rs (69%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/json/mod.rs (89%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/kv_store/mod.rs (98%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/localtime/mod.rs (98%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/logging/mod.rs (98%) rename hermes/bin/src/{runtime => runtime_extensions}/host/hermes/mod.rs (95%) rename hermes/bin/src/{runtime => runtime_extensions}/host/mod.rs (100%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/cli/mod.rs (97%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/clocks/mod.rs (84%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/clocks/monotonic.rs (95%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/clocks/wall.rs (97%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/filesystem/mod.rs (99%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/http/mod.rs (99%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/io/error.rs (96%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/io/mod.rs (85%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/io/streams.rs (99%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/mod.rs (92%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/random/insecure.rs (96%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/random/insecure_seed.rs (97%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/random/mod.rs (89%) rename hermes/bin/src/{runtime => runtime_extensions}/host/wasi/random/secure.rs (97%) rename hermes/bin/src/{runtime => runtime_extensions}/mod.rs (100%) diff --git a/hermes/bin/src/lib.rs b/hermes/bin/src/lib.rs index 379e10035..512867736 100644 --- a/hermes/bin/src/lib.rs +++ b/hermes/bin/src/lib.rs @@ -4,6 +4,6 @@ mod event_queue; #[allow(missing_docs, clippy::missing_docs_in_private_items, dead_code)] mod reactor; -mod runtime; +mod runtime_extensions; mod state; mod wasm; diff --git a/hermes/bin/src/main.rs b/hermes/bin/src/main.rs index 117bdf2fe..69b7bc9a8 100644 --- a/hermes/bin/src/main.rs +++ b/hermes/bin/src/main.rs @@ -1,7 +1,7 @@ //! The Hermes Node. mod event_queue; -mod runtime; +mod runtime_extensions; mod state; mod wasm; diff --git a/hermes/bin/src/runtime/extensions/bindings.rs b/hermes/bin/src/runtime_extensions/extensions/bindings.rs similarity index 100% rename from hermes/bin/src/runtime/extensions/bindings.rs rename to hermes/bin/src/runtime_extensions/extensions/bindings.rs diff --git a/hermes/bin/src/runtime/extensions/mod.rs b/hermes/bin/src/runtime_extensions/extensions/mod.rs similarity index 97% rename from hermes/bin/src/runtime/extensions/mod.rs rename to hermes/bin/src/runtime_extensions/extensions/mod.rs index 52aff0da5..6525cf619 100644 --- a/hermes/bin/src/runtime/extensions/mod.rs +++ b/hermes/bin/src/runtime_extensions/extensions/mod.rs @@ -20,7 +20,7 @@ pub(crate) mod state; /// Example of how to call the autogenerated entry points #[allow(dead_code)] fn example() -> anyhow::Result<()> { - use crate::runtime::extensions::state::{Context, Stateful}; + use crate::runtime_extensions::extensions::state::{Context, Stateful}; // Configure an `Engine` and compile the `Component` that is being run for // the application. diff --git a/hermes/bin/src/runtime/extensions/state.rs b/hermes/bin/src/runtime_extensions/extensions/state.rs similarity index 100% rename from hermes/bin/src/runtime/extensions/state.rs rename to hermes/bin/src/runtime_extensions/extensions/state.rs diff --git a/hermes/bin/src/runtime/host/hermes/binary/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs similarity index 89% rename from hermes/bin/src/runtime/host/hermes/binary/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs index 763e74690..98d1889ed 100644 --- a/hermes/bin/src/runtime/host/hermes/binary/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs @@ -1,7 +1,7 @@ //! Host - CBOR implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::binary::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/hermes/cardano/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs similarity index 99% rename from hermes/bin/src/runtime/host/hermes/cardano/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs index 8ab3dda37..9b4a344dd 100644 --- a/hermes/bin/src/runtime/host/hermes/cardano/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs @@ -1,7 +1,7 @@ //! Host - Cardano Blockchain implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::cardano::api::{ CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError, UnsubscribeOptions, diff --git a/hermes/bin/src/runtime/host/hermes/cbor/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs similarity index 89% rename from hermes/bin/src/runtime/host/hermes/cbor/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs index 685fded80..b65b2a036 100644 --- a/hermes/bin/src/runtime/host/hermes/cbor/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs @@ -1,7 +1,7 @@ //! Host - CBOR implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::cbor::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/hermes/cron/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs similarity index 99% rename from hermes/bin/src/runtime/host/hermes/cron/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs index 463e2a9e1..85c500173 100644 --- a/hermes/bin/src/runtime/host/hermes/cron/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs @@ -1,7 +1,7 @@ //! Host - Cron implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::{ hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host}, wasi::clocks::monotonic_clock::Instant, diff --git a/hermes/bin/src/runtime/host/hermes/crypto/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs similarity index 98% rename from hermes/bin/src/runtime/host/hermes/crypto/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs index 2b06252de..cf1c220f2 100644 --- a/hermes/bin/src/runtime/host/hermes/crypto/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs @@ -1,7 +1,7 @@ //! Host - Crypto implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::{ binary::api::Bstr, crypto::api::{ diff --git a/hermes/bin/src/runtime/host/hermes/hash/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs similarity index 96% rename from hermes/bin/src/runtime/host/hermes/hash/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs index 9f1a642a2..c3ccedf66 100644 --- a/hermes/bin/src/runtime/host/hermes/hash/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs @@ -1,7 +1,7 @@ //! Host - Hash implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::{ binary::api::Bstr, hash::api::{Errno, Host}, diff --git a/hermes/bin/src/runtime/host/hermes/init/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs similarity index 69% rename from hermes/bin/src/runtime/host/hermes/init/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs index b796452e0..245d1e68e 100644 --- a/hermes/bin/src/runtime/host/hermes/init/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs @@ -1,6 +1,6 @@ //! Host - Init implementations -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; /// State pub(crate) struct State {} diff --git a/hermes/bin/src/runtime/host/hermes/json/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs similarity index 89% rename from hermes/bin/src/runtime/host/hermes/json/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs index 9b360f379..df23a9589 100644 --- a/hermes/bin/src/runtime/host/hermes/json/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs @@ -1,7 +1,7 @@ //! Host - JSON implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::json::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/hermes/kv_store/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs similarity index 98% rename from hermes/bin/src/runtime/host/hermes/kv_store/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs index 36d79c21b..e4d3a672a 100644 --- a/hermes/bin/src/runtime/host/hermes/kv_store/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs @@ -1,7 +1,7 @@ //! Host - KV-Store implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::kv_store::api::{Host, KvValues}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/hermes/localtime/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs similarity index 98% rename from hermes/bin/src/runtime/host/hermes/localtime/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs index 8ee18ae29..9db5d2bab 100644 --- a/hermes/bin/src/runtime/host/hermes/localtime/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs @@ -1,7 +1,7 @@ //! Host - Localtime implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::{ hermes::localtime::api::{Errno, Host, Localtime, Timezone}, wasi::clocks::wall_clock::Datetime, diff --git a/hermes/bin/src/runtime/host/hermes/logging/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs similarity index 98% rename from hermes/bin/src/runtime/host/hermes/logging/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs index d872929c4..ba172f398 100644 --- a/hermes/bin/src/runtime/host/hermes/logging/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs @@ -1,7 +1,7 @@ //! Host - Logging implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::hermes::{ json::api::Json, logging::api::{Host, Level}, diff --git a/hermes/bin/src/runtime/host/hermes/mod.rs b/hermes/bin/src/runtime_extensions/host/hermes/mod.rs similarity index 95% rename from hermes/bin/src/runtime/host/hermes/mod.rs rename to hermes/bin/src/runtime_extensions/host/hermes/mod.rs index ffe732a92..216ebd92d 100644 --- a/hermes/bin/src/runtime/host/hermes/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/hermes/mod.rs @@ -1,6 +1,6 @@ //! Runtime modules - extensions - HERMES custom extensions -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; pub(crate) mod binary; pub(crate) mod cardano; diff --git a/hermes/bin/src/runtime/host/mod.rs b/hermes/bin/src/runtime_extensions/host/mod.rs similarity index 100% rename from hermes/bin/src/runtime/host/mod.rs rename to hermes/bin/src/runtime_extensions/host/mod.rs diff --git a/hermes/bin/src/runtime/host/wasi/cli/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs similarity index 97% rename from hermes/bin/src/runtime/host/wasi/cli/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs index d1237cda5..ea947763c 100644 --- a/hermes/bin/src/runtime/host/wasi/cli/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - CLI implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::{ cli, io::streams::{InputStream, OutputStream}, diff --git a/hermes/bin/src/runtime/host/wasi/clocks/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs similarity index 84% rename from hermes/bin/src/runtime/host/wasi/clocks/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs index 674319ca3..95b016c21 100644 --- a/hermes/bin/src/runtime/host/wasi/clocks/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI - Clock implementations -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; mod monotonic; mod wall; diff --git a/hermes/bin/src/runtime/host/wasi/clocks/monotonic.rs b/hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs similarity index 95% rename from hermes/bin/src/runtime/host/wasi/clocks/monotonic.rs rename to hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs index e3e77bbe7..5bb7bf8b2 100644 --- a/hermes/bin/src/runtime/host/wasi/clocks/monotonic.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs @@ -1,7 +1,7 @@ //! Host - WASI - monotonic clock implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::clocks::monotonic_clock::{Duration, Host, Instant}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/wasi/clocks/wall.rs b/hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs similarity index 97% rename from hermes/bin/src/runtime/host/wasi/clocks/wall.rs rename to hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs index 2a25685d7..86d555fdb 100644 --- a/hermes/bin/src/runtime/host/wasi/clocks/wall.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs @@ -1,7 +1,7 @@ //! Host - WASI - Wall Clock implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::clocks::wall_clock::{Datetime, Host}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/wasi/filesystem/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs similarity index 99% rename from hermes/bin/src/runtime/host/wasi/filesystem/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs index 60462a1f0..2b9fd1d55 100644 --- a/hermes/bin/src/runtime/host/wasi/filesystem/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - Filesystem implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::{ filesystem::{ self, diff --git a/hermes/bin/src/runtime/host/wasi/http/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs similarity index 99% rename from hermes/bin/src/runtime/host/wasi/http/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs index edc03203f..8dd54c4ad 100644 --- a/hermes/bin/src/runtime/host/wasi/http/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - HTTP implementations use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::{ http::{ self, diff --git a/hermes/bin/src/runtime/host/wasi/io/error.rs b/hermes/bin/src/runtime_extensions/host/wasi/io/error.rs similarity index 96% rename from hermes/bin/src/runtime/host/wasi/io/error.rs rename to hermes/bin/src/runtime_extensions/host/wasi/io/error.rs index 2b0aa8828..16cc423ca 100644 --- a/hermes/bin/src/runtime/host/wasi/io/error.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/io/error.rs @@ -1,7 +1,7 @@ //! WASI IO Error use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::io::error::{Error, Host, HostError}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/wasi/io/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs similarity index 85% rename from hermes/bin/src/runtime/host/wasi/io/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs index 6994734de..274d331ed 100644 --- a/hermes/bin/src/runtime/host/wasi/io/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI IO Implementation -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; pub(crate) mod error; pub(crate) mod streams; diff --git a/hermes/bin/src/runtime/host/wasi/io/streams.rs b/hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs similarity index 99% rename from hermes/bin/src/runtime/host/wasi/io/streams.rs rename to hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs index c96645894..d979ae58a 100644 --- a/hermes/bin/src/runtime/host/wasi/io/streams.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs @@ -1,7 +1,7 @@ //! WASI IO Streams use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::io::streams::{ Host, HostInputStream, HostOutputStream, InputStream, OutputStream, StreamError, }, diff --git a/hermes/bin/src/runtime/host/wasi/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/mod.rs similarity index 92% rename from hermes/bin/src/runtime/host/wasi/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/mod.rs index f8b5f0a08..31359e2b2 100644 --- a/hermes/bin/src/runtime/host/wasi/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/mod.rs @@ -1,6 +1,6 @@ //! Runtime modules - extensions - WASI standard extensions -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; pub(crate) mod cli; pub(crate) mod clocks; diff --git a/hermes/bin/src/runtime/host/wasi/random/insecure.rs b/hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs similarity index 96% rename from hermes/bin/src/runtime/host/wasi/random/insecure.rs rename to hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs index 6fe934459..d56103615 100644 --- a/hermes/bin/src/runtime/host/wasi/random/insecure.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs @@ -1,7 +1,7 @@ //! Insecure RNG use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::random::insecure::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/wasi/random/insecure_seed.rs b/hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs similarity index 97% rename from hermes/bin/src/runtime/host/wasi/random/insecure_seed.rs rename to hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs index 7f414d6b0..6495a9d67 100644 --- a/hermes/bin/src/runtime/host/wasi/random/insecure_seed.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs @@ -1,7 +1,7 @@ //! Insecure RNG use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::random::insecure_seed::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/host/wasi/random/mod.rs b/hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs similarity index 89% rename from hermes/bin/src/runtime/host/wasi/random/mod.rs rename to hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs index 416a6e6b6..bf9d950f7 100644 --- a/hermes/bin/src/runtime/host/wasi/random/mod.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI - Random implementations -use crate::runtime::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::extensions::state::{Context, Stateful}; pub(crate) mod insecure; pub(crate) mod insecure_seed; diff --git a/hermes/bin/src/runtime/host/wasi/random/secure.rs b/hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs similarity index 97% rename from hermes/bin/src/runtime/host/wasi/random/secure.rs rename to hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs index 878ff8dd2..11a199490 100644 --- a/hermes/bin/src/runtime/host/wasi/random/secure.rs +++ b/hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs @@ -1,7 +1,7 @@ //! Random RNG use crate::{ - runtime::extensions::{ + runtime_extensions::extensions::{ bindings::wasi::random::random::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime/mod.rs b/hermes/bin/src/runtime_extensions/mod.rs similarity index 100% rename from hermes/bin/src/runtime/mod.rs rename to hermes/bin/src/runtime_extensions/mod.rs diff --git a/hermes/bin/src/state.rs b/hermes/bin/src/state.rs index e5b1d9401..765682206 100644 --- a/hermes/bin/src/state.rs +++ b/hermes/bin/src/state.rs @@ -1,6 +1,6 @@ //! Hermes state implementation. -use crate::runtime::{ +use crate::runtime_extensions::{ extensions::state::{Context, Stateful}, host::{hermes, wasi}, }; diff --git a/hermes/bin/src/wasm/module.rs b/hermes/bin/src/wasm/module.rs index c104b4ec4..ba135dff3 100644 --- a/hermes/bin/src/wasm/module.rs +++ b/hermes/bin/src/wasm/module.rs @@ -11,7 +11,7 @@ use wasmtime::{ use crate::{ event_queue::event::HermesEventPayload, - runtime::extensions::{ + runtime_extensions::extensions::{ bindings, state::{Context, Stateful}, }, From 326685840580b5430f08b317837fbd3220ad8550 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 12:18:35 +0200 Subject: [PATCH 03/11] refactor --- hermes/bin/src/reactor.rs | 2 +- .../{extensions => }/bindings.rs | 0 .../src/runtime_extensions/extensions/mod.rs | 107 ----------------- .../{host => }/hermes/binary/mod.rs | 2 +- .../{host => }/hermes/cardano/mod.rs | 2 +- .../{host => }/hermes/cbor/mod.rs | 2 +- .../{host => }/hermes/cron/mod.rs | 2 +- .../{host => }/hermes/crypto/mod.rs | 2 +- .../{host => }/hermes/hash/mod.rs | 2 +- .../{host => }/hermes/init/mod.rs | 2 +- .../{host => }/hermes/json/mod.rs | 2 +- .../{host => }/hermes/kv_store/mod.rs | 2 +- .../{host => }/hermes/localtime/mod.rs | 2 +- .../{host => }/hermes/logging/mod.rs | 2 +- .../{host => }/hermes/mod.rs | 4 +- hermes/bin/src/runtime_extensions/host/mod.rs | 4 - hermes/bin/src/runtime_extensions/mod.rs | 111 +++++++++++++++++- .../{extensions => }/state.rs | 0 .../{host => }/wasi/cli/mod.rs | 2 +- .../{host => }/wasi/clocks/mod.rs | 2 +- .../{host => }/wasi/clocks/monotonic.rs | 2 +- .../{host => }/wasi/clocks/wall.rs | 2 +- .../{host => }/wasi/filesystem/mod.rs | 2 +- .../{host => }/wasi/http/mod.rs | 2 +- .../{host => }/wasi/io/error.rs | 2 +- .../{host => }/wasi/io/mod.rs | 2 +- .../{host => }/wasi/io/streams.rs | 2 +- .../runtime_extensions/{host => }/wasi/mod.rs | 4 +- .../{host => }/wasi/random/insecure.rs | 2 +- .../{host => }/wasi/random/insecure_seed.rs | 2 +- .../{host => }/wasi/random/mod.rs | 2 +- .../{host => }/wasi/random/secure.rs | 2 +- hermes/bin/src/state.rs | 5 +- hermes/bin/src/wasm/module.rs | 2 +- 34 files changed, 141 insertions(+), 146 deletions(-) rename hermes/bin/src/runtime_extensions/{extensions => }/bindings.rs (100%) delete mode 100644 hermes/bin/src/runtime_extensions/extensions/mod.rs rename hermes/bin/src/runtime_extensions/{host => }/hermes/binary/mod.rs (89%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/cardano/mod.rs (99%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/cbor/mod.rs (89%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/cron/mod.rs (99%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/crypto/mod.rs (98%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/hash/mod.rs (96%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/init/mod.rs (69%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/json/mod.rs (89%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/kv_store/mod.rs (98%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/localtime/mod.rs (98%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/logging/mod.rs (98%) rename hermes/bin/src/runtime_extensions/{host => }/hermes/mod.rs (92%) delete mode 100644 hermes/bin/src/runtime_extensions/host/mod.rs rename hermes/bin/src/runtime_extensions/{extensions => }/state.rs (100%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/cli/mod.rs (97%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/clocks/mod.rs (84%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/clocks/monotonic.rs (95%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/clocks/wall.rs (97%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/filesystem/mod.rs (99%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/http/mod.rs (99%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/io/error.rs (96%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/io/mod.rs (85%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/io/streams.rs (99%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/mod.rs (86%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/random/insecure.rs (96%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/random/insecure_seed.rs (97%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/random/mod.rs (89%) rename hermes/bin/src/runtime_extensions/{host => }/wasi/random/secure.rs (97%) diff --git a/hermes/bin/src/reactor.rs b/hermes/bin/src/reactor.rs index d956bd1ea..00187eb28 100644 --- a/hermes/bin/src/reactor.rs +++ b/hermes/bin/src/reactor.rs @@ -34,7 +34,7 @@ impl HermesReactor { } pub(crate) fn run(self) -> anyhow::Result<()> { - let events_thread = thread::spawn(|| { + let _events_thread = thread::spawn(|| { Self::event_execution_loop(self.wasm_module, self.event_receiver).unwrap(); }); Ok(()) diff --git a/hermes/bin/src/runtime_extensions/extensions/bindings.rs b/hermes/bin/src/runtime_extensions/bindings.rs similarity index 100% rename from hermes/bin/src/runtime_extensions/extensions/bindings.rs rename to hermes/bin/src/runtime_extensions/bindings.rs diff --git a/hermes/bin/src/runtime_extensions/extensions/mod.rs b/hermes/bin/src/runtime_extensions/extensions/mod.rs deleted file mode 100644 index 6525cf619..000000000 --- a/hermes/bin/src/runtime_extensions/extensions/mod.rs +++ /dev/null @@ -1,107 +0,0 @@ -//! Hermes runtime extensions - -use wasmtime::{ - component::{Component, Linker}, - Config, Engine, Store, -}; - -use self::bindings::{ - exports::hermes::cardano::event_on_block::BlockSrc, - hermes::{ - cardano::api::{CardanoBlock, CardanoBlockchainId, CardanoTxn}, - cron::api::CronTagged, - }, -}; -use crate::state::HermesState; - -pub(crate) mod bindings; -pub(crate) mod state; - -/// Example of how to call the autogenerated entry points -#[allow(dead_code)] -fn example() -> anyhow::Result<()> { - use crate::runtime_extensions::extensions::state::{Context, Stateful}; - - // Configure an `Engine` and compile the `Component` that is being run for - // the application. - let mut config = Config::new(); - config.wasm_component_model(true); - let engine = Engine::new(&config)?; - let component = Component::from_file(&engine, "./your-component.wasm")?; - - // Instantiation of bindings always happens through a `Linker`. - // - // Note that the closure provided here is a projection from `T` in - // `Store` to `&mut U` where `U` implements the `HelloWorldImports` - // trait. In this case the `T`, `MyState`, is stored directly in the - // structure so no projection is necessary here. - let mut linker = Linker::new(&engine); - bindings::Hermes::add_to_linker(&mut linker, |state: &mut HermesState| state)?; - - let instance_pre = linker.instantiate_pre(&component)?; - - // As with the core wasm API of Wasmtime instantiation occurs within a - // `Store`. The bindings structure contains an `instantiate` method which - // takes the store, component, and linker. This returns the `bindings` - // structure which is an instance of `HelloWorld` and supports typed access - // to the exports of the component. - let mut store = Store::new( - &engine, - HermesState::new(&Context::new("my-app".to_string())), - ); - - // - let (bindings, _) = bindings::Hermes::instantiate_pre(&mut store, &instance_pre)?; - - // Show how we call the events in our API. - let _result = bindings.hermes_init_event().call_init(&mut store)?; - - // HTTP API to be rewritten, but this is how its called. - // let arg1 = ??; - // let arg2 = ??; - // let result = bindings.interface0.call_handle(&mut store, arg1, arg2)?; - - // Example of calling on_block. - let arg0 = CardanoBlockchainId::Mainnet; - let arg1 = CardanoBlock::new(); - let arg2 = BlockSrc::TIP; - bindings - .hermes_cardano_event_on_block() - .call_on_cardano_block(&mut store, arg0, &arg1, arg2)?; - - // Example of calling on_txn. - let arg0 = CardanoBlockchainId::Mainnet; - let arg1: u64 = 123_456; - let arg2: u32 = 12; - let arg3 = CardanoTxn::new(); - - bindings - .hermes_cardano_event_on_txn() - .call_on_cardano_txn(&mut store, arg0, arg1, arg2, &arg3)?; - - // Example of calling on_rollback. - let arg0 = CardanoBlockchainId::Mainnet; - let arg1: u64 = 123_456; - bindings - .hermes_cardano_event_on_rollback() - .call_on_cardano_rollback(&mut store, arg0, arg1)?; - - // Example of calling on_cron. - let arg0 = CronTagged { - when: "* * * * *".to_string(), - tag: "tag".to_string(), - }; - let arg1 = false; - let _result = bindings - .hermes_cron_event() - .call_on_cron(&mut store, &arg0, arg1)?; - - // Example of calling kv_update - let arg0 = "key"; - let arg1 = bindings::hermes::kv_store::api::KvValues::KvString("value".to_string()); - bindings - .hermes_kv_store_event() - .call_kv_update(&mut store, arg0, &arg1)?; - - Ok(()) -} diff --git a/hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs b/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs similarity index 89% rename from hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/binary/mod.rs index 98d1889ed..2758ac237 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/binary/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs @@ -1,7 +1,7 @@ //! Host - CBOR implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::binary::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs similarity index 99% rename from hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs index 9b4a344dd..d91aad324 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/cardano/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs @@ -1,7 +1,7 @@ //! Host - Cardano Blockchain implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::cardano::api::{ CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError, UnsubscribeOptions, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs similarity index 89% rename from hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs index b65b2a036..c265338d0 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/cbor/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs @@ -1,7 +1,7 @@ //! Host - CBOR implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::cbor::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs similarity index 99% rename from hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/cron/mod.rs index 85c500173..436885de4 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/cron/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs @@ -1,7 +1,7 @@ //! Host - Cron implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::{ hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host}, wasi::clocks::monotonic_clock::Instant, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs b/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs similarity index 98% rename from hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs index cf1c220f2..4d44c5ef5 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/crypto/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs @@ -1,7 +1,7 @@ //! Host - Crypto implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::{ binary::api::Bstr, crypto::api::{ diff --git a/hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs b/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs similarity index 96% rename from hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/hash/mod.rs index c3ccedf66..4903a7875 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/hash/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs @@ -1,7 +1,7 @@ //! Host - Hash implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::{ binary::api::Bstr, hash::api::{Errno, Host}, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs b/hermes/bin/src/runtime_extensions/hermes/init/mod.rs similarity index 69% rename from hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/init/mod.rs index 245d1e68e..c2c837459 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/init/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/init/mod.rs @@ -1,6 +1,6 @@ //! Host - Init implementations -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; /// State pub(crate) struct State {} diff --git a/hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs b/hermes/bin/src/runtime_extensions/hermes/json/mod.rs similarity index 89% rename from hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/json/mod.rs index df23a9589..835f51262 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/json/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/json/mod.rs @@ -1,7 +1,7 @@ //! Host - JSON implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::json::api::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs similarity index 98% rename from hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs index e4d3a672a..5cbdbbcc6 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/kv_store/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs @@ -1,7 +1,7 @@ //! Host - KV-Store implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::kv_store::api::{Host, KvValues}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs b/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs similarity index 98% rename from hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs index 9db5d2bab..11be28678 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/localtime/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs @@ -1,7 +1,7 @@ //! Host - Localtime implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::{ hermes::localtime::api::{Errno, Host, Localtime, Timezone}, wasi::clocks::wall_clock::Datetime, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs b/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs similarity index 98% rename from hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/logging/mod.rs index ba172f398..dff4d2358 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/logging/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs @@ -1,7 +1,7 @@ //! Host - Logging implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::hermes::{ json::api::Json, logging::api::{Host, Level}, diff --git a/hermes/bin/src/runtime_extensions/host/hermes/mod.rs b/hermes/bin/src/runtime_extensions/hermes/mod.rs similarity index 92% rename from hermes/bin/src/runtime_extensions/host/hermes/mod.rs rename to hermes/bin/src/runtime_extensions/hermes/mod.rs index 216ebd92d..8111063b9 100644 --- a/hermes/bin/src/runtime_extensions/host/hermes/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/mod.rs @@ -1,6 +1,6 @@ -//! Runtime modules - extensions - HERMES custom extensions +//! Hermes runtime extensions implmentations - HERMES custom extensions -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; pub(crate) mod binary; pub(crate) mod cardano; diff --git a/hermes/bin/src/runtime_extensions/host/mod.rs b/hermes/bin/src/runtime_extensions/host/mod.rs deleted file mode 100644 index 6e3721d85..000000000 --- a/hermes/bin/src/runtime_extensions/host/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Runtime runtime extensions - Host implementations - -pub(crate) mod hermes; -pub(crate) mod wasi; diff --git a/hermes/bin/src/runtime_extensions/mod.rs b/hermes/bin/src/runtime_extensions/mod.rs index 6591d1e6e..da720dfce 100644 --- a/hermes/bin/src/runtime_extensions/mod.rs +++ b/hermes/bin/src/runtime_extensions/mod.rs @@ -1,4 +1,109 @@ -//! Runtime modules +//! Hermes runtime extensions -pub(crate) mod extensions; -pub(crate) mod host; +use wasmtime::{ + component::{Component, Linker}, + Config, Engine, Store, +}; + +use self::bindings::{ + exports::hermes::cardano::event_on_block::BlockSrc, + hermes::{ + cardano::api::{CardanoBlock, CardanoBlockchainId, CardanoTxn}, + cron::api::CronTagged, + }, +}; +use crate::state::HermesState; + +pub(crate) mod bindings; +pub(crate) mod hermes; +pub(crate) mod state; +pub(crate) mod wasi; + +/// Example of how to call the autogenerated entry points +#[allow(dead_code)] +fn example() -> anyhow::Result<()> { + use crate::runtime_extensions::state::{Context, Stateful}; + + // Configure an `Engine` and compile the `Component` that is being run for + // the application. + let mut config = Config::new(); + config.wasm_component_model(true); + let engine = Engine::new(&config)?; + let component = Component::from_file(&engine, "./your-component.wasm")?; + + // Instantiation of bindings always happens through a `Linker`. + // + // Note that the closure provided here is a projection from `T` in + // `Store` to `&mut U` where `U` implements the `HelloWorldImports` + // trait. In this case the `T`, `MyState`, is stored directly in the + // structure so no projection is necessary here. + let mut linker = Linker::new(&engine); + bindings::Hermes::add_to_linker(&mut linker, |state: &mut HermesState| state)?; + + let instance_pre = linker.instantiate_pre(&component)?; + + // As with the core wasm API of Wasmtime instantiation occurs within a + // `Store`. The bindings structure contains an `instantiate` method which + // takes the store, component, and linker. This returns the `bindings` + // structure which is an instance of `HelloWorld` and supports typed access + // to the exports of the component. + let mut store = Store::new( + &engine, + HermesState::new(&Context::new("my-app".to_string())), + ); + + // + let (bindings, _) = bindings::Hermes::instantiate_pre(&mut store, &instance_pre)?; + + // Show how we call the events in our API. + let _result = bindings.hermes_init_event().call_init(&mut store)?; + + // HTTP API to be rewritten, but this is how its called. + // let arg1 = ??; + // let arg2 = ??; + // let result = bindings.interface0.call_handle(&mut store, arg1, arg2)?; + + // Example of calling on_block. + let arg0 = CardanoBlockchainId::Mainnet; + let arg1 = CardanoBlock::new(); + let arg2 = BlockSrc::TIP; + bindings + .hermes_cardano_event_on_block() + .call_on_cardano_block(&mut store, arg0, &arg1, arg2)?; + + // Example of calling on_txn. + let arg0 = CardanoBlockchainId::Mainnet; + let arg1: u64 = 123_456; + let arg2: u32 = 12; + let arg3 = CardanoTxn::new(); + + bindings + .hermes_cardano_event_on_txn() + .call_on_cardano_txn(&mut store, arg0, arg1, arg2, &arg3)?; + + // Example of calling on_rollback. + let arg0 = CardanoBlockchainId::Mainnet; + let arg1: u64 = 123_456; + bindings + .hermes_cardano_event_on_rollback() + .call_on_cardano_rollback(&mut store, arg0, arg1)?; + + // Example of calling on_cron. + let arg0 = CronTagged { + when: "* * * * *".to_string(), + tag: "tag".to_string(), + }; + let arg1 = false; + let _result = bindings + .hermes_cron_event() + .call_on_cron(&mut store, &arg0, arg1)?; + + // Example of calling kv_update + let arg0 = "key"; + let arg1 = bindings::hermes::kv_store::api::KvValues::KvString("value".to_string()); + bindings + .hermes_kv_store_event() + .call_kv_update(&mut store, arg0, &arg1)?; + + Ok(()) +} diff --git a/hermes/bin/src/runtime_extensions/extensions/state.rs b/hermes/bin/src/runtime_extensions/state.rs similarity index 100% rename from hermes/bin/src/runtime_extensions/extensions/state.rs rename to hermes/bin/src/runtime_extensions/state.rs diff --git a/hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs b/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs similarity index 97% rename from hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/cli/mod.rs index ea947763c..0e136e805 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/cli/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - CLI implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::{ cli, io::streams::{InputStream, OutputStream}, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/mod.rs similarity index 84% rename from hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/clocks/mod.rs index 95b016c21..f7b982f12 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/clocks/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI - Clock implementations -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; mod monotonic; mod wall; diff --git a/hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs similarity index 95% rename from hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs rename to hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs index 5bb7bf8b2..a3abcd6cc 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/clocks/monotonic.rs +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs @@ -1,7 +1,7 @@ //! Host - WASI - monotonic clock implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::clocks::monotonic_clock::{Duration, Host, Instant}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs similarity index 97% rename from hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs rename to hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs index 86d555fdb..6c2b4949f 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/clocks/wall.rs +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs @@ -1,7 +1,7 @@ //! Host - WASI - Wall Clock implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::clocks::wall_clock::{Datetime, Host}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs b/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs similarity index 99% rename from hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs index 2b9fd1d55..fd4771edb 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/filesystem/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - Filesystem implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::{ filesystem::{ self, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs b/hermes/bin/src/runtime_extensions/wasi/http/mod.rs similarity index 99% rename from hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/http/mod.rs index 8dd54c4ad..68dedc475 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/http/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/http/mod.rs @@ -1,7 +1,7 @@ //! Host - WASI - HTTP implementations use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::{ http::{ self, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/io/error.rs b/hermes/bin/src/runtime_extensions/wasi/io/error.rs similarity index 96% rename from hermes/bin/src/runtime_extensions/host/wasi/io/error.rs rename to hermes/bin/src/runtime_extensions/wasi/io/error.rs index 16cc423ca..3e4f0bea9 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/io/error.rs +++ b/hermes/bin/src/runtime_extensions/wasi/io/error.rs @@ -1,7 +1,7 @@ //! WASI IO Error use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::io::error::{Error, Host, HostError}, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs b/hermes/bin/src/runtime_extensions/wasi/io/mod.rs similarity index 85% rename from hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/io/mod.rs index 274d331ed..ba46e0ec6 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/io/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/io/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI IO Implementation -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; pub(crate) mod error; pub(crate) mod streams; diff --git a/hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs b/hermes/bin/src/runtime_extensions/wasi/io/streams.rs similarity index 99% rename from hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs rename to hermes/bin/src/runtime_extensions/wasi/io/streams.rs index d979ae58a..26385b746 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/io/streams.rs +++ b/hermes/bin/src/runtime_extensions/wasi/io/streams.rs @@ -1,7 +1,7 @@ //! WASI IO Streams use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::io::streams::{ Host, HostInputStream, HostOutputStream, InputStream, OutputStream, StreamError, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/mod.rs b/hermes/bin/src/runtime_extensions/wasi/mod.rs similarity index 86% rename from hermes/bin/src/runtime_extensions/host/wasi/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/mod.rs index 31359e2b2..3e7afb002 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/mod.rs @@ -1,6 +1,6 @@ -//! Runtime modules - extensions - WASI standard extensions +//! Hermes runtime extensions implementations - WASI standard extensions -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; pub(crate) mod cli; pub(crate) mod clocks; diff --git a/hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure.rs similarity index 96% rename from hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs rename to hermes/bin/src/runtime_extensions/wasi/random/insecure.rs index d56103615..d6573e1cd 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/random/insecure.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure.rs @@ -1,7 +1,7 @@ //! Insecure RNG use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::random::insecure::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs similarity index 97% rename from hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs rename to hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs index 6495a9d67..22d7d2987 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/random/insecure_seed.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs @@ -1,7 +1,7 @@ //! Insecure RNG use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::random::insecure_seed::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs b/hermes/bin/src/runtime_extensions/wasi/random/mod.rs similarity index 89% rename from hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs rename to hermes/bin/src/runtime_extensions/wasi/random/mod.rs index bf9d950f7..a1e32999e 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/random/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/mod.rs @@ -1,6 +1,6 @@ //! Host - WASI - Random implementations -use crate::runtime_extensions::extensions::state::{Context, Stateful}; +use crate::runtime_extensions::state::{Context, Stateful}; pub(crate) mod insecure; pub(crate) mod insecure_seed; diff --git a/hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs b/hermes/bin/src/runtime_extensions/wasi/random/secure.rs similarity index 97% rename from hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs rename to hermes/bin/src/runtime_extensions/wasi/random/secure.rs index 11a199490..9334edbc7 100644 --- a/hermes/bin/src/runtime_extensions/host/wasi/random/secure.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/secure.rs @@ -1,7 +1,7 @@ //! Random RNG use crate::{ - runtime_extensions::extensions::{ + runtime_extensions::{ bindings::wasi::random::random::Host, state::{Context, Stateful}, }, diff --git a/hermes/bin/src/state.rs b/hermes/bin/src/state.rs index 765682206..131116755 100644 --- a/hermes/bin/src/state.rs +++ b/hermes/bin/src/state.rs @@ -1,8 +1,9 @@ //! Hermes state implementation. use crate::runtime_extensions::{ - extensions::state::{Context, Stateful}, - host::{hermes, wasi}, + hermes, + state::{Context, Stateful}, + wasi, }; #[allow(dead_code)] diff --git a/hermes/bin/src/wasm/module.rs b/hermes/bin/src/wasm/module.rs index ba135dff3..ea5713bee 100644 --- a/hermes/bin/src/wasm/module.rs +++ b/hermes/bin/src/wasm/module.rs @@ -11,7 +11,7 @@ use wasmtime::{ use crate::{ event_queue::event::HermesEventPayload, - runtime_extensions::extensions::{ + runtime_extensions::{ bindings, state::{Context, Stateful}, }, From 5dc58597fdc00dfb33b79dfd044dcad03f1708c2 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 12:42:30 +0200 Subject: [PATCH 04/11] move hermes runtime extensions host impl to the separate module --- .../runtime_extensions/hermes/binary/host.rs | 5 + .../runtime_extensions/hermes/binary/mod.rs | 14 +- .../runtime_extensions/hermes/cardano/host.rs | 165 +++++++++++++++++ .../runtime_extensions/hermes/cardano/mod.rs | 171 +----------------- .../runtime_extensions/hermes/cbor/host.rs | 5 + .../src/runtime_extensions/hermes/cbor/mod.rs | 14 +- .../runtime_extensions/hermes/cron/host.rs | 137 ++++++++++++++ .../src/runtime_extensions/hermes/cron/mod.rs | 143 +-------------- .../runtime_extensions/hermes/crypto/host.rs | 84 +++++++++ .../runtime_extensions/hermes/crypto/mod.rs | 90 +-------- .../runtime_extensions/hermes/hash/host.rs | 32 ++++ .../src/runtime_extensions/hermes/hash/mod.rs | 38 +--- .../src/runtime_extensions/hermes/init/mod.rs | 2 +- .../runtime_extensions/hermes/json/host.rs | 5 + .../src/runtime_extensions/hermes/json/mod.rs | 14 +- .../hermes/kv_store/host.rs | 86 +++++++++ .../runtime_extensions/hermes/kv_store/mod.rs | 92 +--------- .../hermes/localtime/host.rs | 62 +++++++ .../hermes/localtime/mod.rs | 68 +------ .../runtime_extensions/hermes/logging/host.rs | 63 +++++++ .../runtime_extensions/hermes/logging/mod.rs | 69 +------ 21 files changed, 685 insertions(+), 674 deletions(-) create mode 100644 hermes/bin/src/runtime_extensions/hermes/binary/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/cardano/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/cbor/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/cron/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/crypto/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/hash/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/json/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/kv_store/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/localtime/host.rs create mode 100644 hermes/bin/src/runtime_extensions/hermes/logging/host.rs diff --git a/hermes/bin/src/runtime_extensions/hermes/binary/host.rs b/hermes/bin/src/runtime_extensions/hermes/binary/host.rs new file mode 100644 index 000000000..7eddd020f --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/binary/host.rs @@ -0,0 +1,5 @@ +//! Binary host implementation for WASM runtime. + +use crate::{runtime_extensions::bindings::hermes::binary::api::Host, state::HermesState}; + +impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs b/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs index 2758ac237..69a2afd33 100644 --- a/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/binary/mod.rs @@ -1,12 +1,8 @@ -//! Host - CBOR implementations +//! Binary runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::binary::api::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -16,5 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/cardano/host.rs b/hermes/bin/src/runtime_extensions/hermes/cardano/host.rs new file mode 100644 index 000000000..1949e263e --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/cardano/host.rs @@ -0,0 +1,165 @@ +//! Cardano Blockchain host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::hermes::cardano::api::{ + CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError, + UnsubscribeOptions, + }, + state::HermesState, +}; + +impl Host for HermesState { + /// Subscribe to the Blockchain block data. + /// + /// **Parameters** + /// + /// - `net` : The blockchain network to fetch block from, and subscribe to. + /// - `whence`: Where to start fetching blocks from. + /// + /// **Returns** + /// + /// - `ok(u64)` : The slot we are synching from now. + /// - `error(fetch-error)` : If an error occurred. + /// + /// **Notes** + /// + /// If the blockchain is not yet syncing, it will start, from the requested slot. + /// If the blockchain is not yet syncing, and `whence` == `continue` then the + /// blockchain will + /// not be synced from, the calling module will only be subscribed for block events. + /// + /// If the blockchain is already syncing, the sync will stop and restart, unless + /// `whence` == `continue`. + /// When `whence` == `continue` the blockchain will keep syncing from where it is at, + /// and this module + /// will be subscribed to block updates. + /// + /// `whence` == `stop` will prevent the blockchain syncing, and the caller will be + /// unsubscribed. + fn subscribe_blocks( + &mut self, _net: CardanoBlockchainId, _whence: Slot, + ) -> wasmtime::Result> { + todo!() + } + + /// Unsubscribe from the blockchain events listed. + /// + /// **Parameters** + /// + /// - `events` : The events to unsubscribe from (and optionally stop the blockchain + /// follower). + /// + /// **Notes** + /// + /// This only unsubscribes from the events. + /// The option `stop` MUST be set to actually stop fetching data from the blockchain + /// once started. + /// + /// `stop` can be set without unsubscribing, and this will interrupt the flow of + /// blockchain data. + /// After `stop`, `subscribe-blocks(?, continue)` would cause blockchain sync to + /// continue from + /// the last block received. This would result in the last block being sent as an + /// event twice, + /// once before the `stop` and once after the `continue`. + fn unsubscribe(&mut self, _events: UnsubscribeOptions) -> wasmtime::Result<()> { + todo!() + } + + /// Subscribe to transaction data events, does not alter the blockchain sync in + /// anyway. + /// + /// **Parameters** + /// + /// - `net` : The blockchain network to subscribe to txn events from. + fn subscribe_txn(&mut self, _net: CardanoBlockchainId) -> wasmtime::Result<()> { + todo!() + } + + /// Subscribe to blockchain rollback events, does not alter the blockchain sync in + /// anyway. + /// + /// **Parameters** + /// + /// - `net` : The blockchain network to subscribe to txn events from. + /// + /// **Notes** + /// + /// After a rollback event, the blockchain sync will AUTOMATICALLY start sending block + /// data from the rollback point. No action is required to actually follow the + /// rollback, unless the + /// default behavior is not desired. + fn subscribe_rollback(&mut self, _net: CardanoBlockchainId) -> wasmtime::Result<()> { + todo!() + } + + /// Fetch a block from the requested blockchain at the requested slot. + /// + /// **Parameters** + /// + /// - `net` : The blockchain network to get a block from. + /// - `whence` : Which block to get. + /// + /// **Returns** + /// + /// - `cardano-block` : The block requested. + /// - `fetch-error` : An error if the block can not be fetched. + /// + /// **Notes** + /// + /// Fetching a block does not require the blockchain to be subscribed, or for blocks + /// to be + /// being followed and generating events. + /// It also will not alter the automatic fetching of blocks in any way, and happens in + /// parallel + /// to automated block fetch. + fn fetch_block( + &mut self, _net: CardanoBlockchainId, _whence: Slot, + ) -> wasmtime::Result> { + todo!() + } + + /// Get transactions from a block. + /// + /// This can be used to easily extract all transactions from a complete block. + /// + /// **Parameters** + /// + /// - `block` : The blockchain data to extract transactions from. + /// + /// **Returns** + /// + /// - a list of all transactions in the block, in the order they appear in the block. + /// + /// **Notes** + /// + /// This function exists to support `fetch-block`. + /// Transactions from subscribed block events, should be processed as transaction + /// events. + fn get_txns(&mut self, _block: CardanoBlock) -> wasmtime::Result> { + todo!() + } + + /// Post a transactions to the blockchain. + /// + /// This can be used to post a pre-formed transaction to the required blockchain. + /// + /// **Parameters** + /// + /// - `net` : The blockchain to post the transaction to. + /// - `txn` : The transaction data, ready to submit. + /// + /// **Returns** + /// + /// - An error if the transaction can not be posted. + /// + /// **Notes** + /// + /// This is proposed functionality, and is not yet active. + /// All calls to this function will return `post-txn-not-allowed` error. + fn post_txn( + &mut self, _net: CardanoBlockchainId, _txn: CardanoTxn, + ) -> wasmtime::Result> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs index d91aad324..543f86be9 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs @@ -1,15 +1,8 @@ -//! Host - Cardano Blockchain implementations +//! Cardano Blockchain runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::cardano::api::{ - CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError, - UnsubscribeOptions, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -19,159 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// Subscribe to the Blockchain block data. - /// - /// **Parameters** - /// - /// - `net` : The blockchain network to fetch block from, and subscribe to. - /// - `whence`: Where to start fetching blocks from. - /// - /// **Returns** - /// - /// - `ok(u64)` : The slot we are synching from now. - /// - `error(fetch-error)` : If an error occurred. - /// - /// **Notes** - /// - /// If the blockchain is not yet syncing, it will start, from the requested slot. - /// If the blockchain is not yet syncing, and `whence` == `continue` then the - /// blockchain will - /// not be synced from, the calling module will only be subscribed for block events. - /// - /// If the blockchain is already syncing, the sync will stop and restart, unless - /// `whence` == `continue`. - /// When `whence` == `continue` the blockchain will keep syncing from where it is at, - /// and this module - /// will be subscribed to block updates. - /// - /// `whence` == `stop` will prevent the blockchain syncing, and the caller will be - /// unsubscribed. - fn subscribe_blocks( - &mut self, _net: CardanoBlockchainId, _whence: Slot, - ) -> wasmtime::Result> { - todo!() - } - - /// Unsubscribe from the blockchain events listed. - /// - /// **Parameters** - /// - /// - `events` : The events to unsubscribe from (and optionally stop the blockchain - /// follower). - /// - /// **Notes** - /// - /// This only unsubscribes from the events. - /// The option `stop` MUST be set to actually stop fetching data from the blockchain - /// once started. - /// - /// `stop` can be set without unsubscribing, and this will interrupt the flow of - /// blockchain data. - /// After `stop`, `subscribe-blocks(?, continue)` would cause blockchain sync to - /// continue from - /// the last block received. This would result in the last block being sent as an - /// event twice, - /// once before the `stop` and once after the `continue`. - fn unsubscribe(&mut self, _events: UnsubscribeOptions) -> wasmtime::Result<()> { - todo!() - } - - /// Subscribe to transaction data events, does not alter the blockchain sync in - /// anyway. - /// - /// **Parameters** - /// - /// - `net` : The blockchain network to subscribe to txn events from. - fn subscribe_txn(&mut self, _net: CardanoBlockchainId) -> wasmtime::Result<()> { - todo!() - } - - /// Subscribe to blockchain rollback events, does not alter the blockchain sync in - /// anyway. - /// - /// **Parameters** - /// - /// - `net` : The blockchain network to subscribe to txn events from. - /// - /// **Notes** - /// - /// After a rollback event, the blockchain sync will AUTOMATICALLY start sending block - /// data from the rollback point. No action is required to actually follow the - /// rollback, unless the - /// default behavior is not desired. - fn subscribe_rollback(&mut self, _net: CardanoBlockchainId) -> wasmtime::Result<()> { - todo!() - } - - /// Fetch a block from the requested blockchain at the requested slot. - /// - /// **Parameters** - /// - /// - `net` : The blockchain network to get a block from. - /// - `whence` : Which block to get. - /// - /// **Returns** - /// - /// - `cardano-block` : The block requested. - /// - `fetch-error` : An error if the block can not be fetched. - /// - /// **Notes** - /// - /// Fetching a block does not require the blockchain to be subscribed, or for blocks - /// to be - /// being followed and generating events. - /// It also will not alter the automatic fetching of blocks in any way, and happens in - /// parallel - /// to automated block fetch. - fn fetch_block( - &mut self, _net: CardanoBlockchainId, _whence: Slot, - ) -> wasmtime::Result> { - todo!() - } - - /// Get transactions from a block. - /// - /// This can be used to easily extract all transactions from a complete block. - /// - /// **Parameters** - /// - /// - `block` : The blockchain data to extract transactions from. - /// - /// **Returns** - /// - /// - a list of all transactions in the block, in the order they appear in the block. - /// - /// **Notes** - /// - /// This function exists to support `fetch-block`. - /// Transactions from subscribed block events, should be processed as transaction - /// events. - fn get_txns(&mut self, _block: CardanoBlock) -> wasmtime::Result> { - todo!() - } - - /// Post a transactions to the blockchain. - /// - /// This can be used to post a pre-formed transaction to the required blockchain. - /// - /// **Parameters** - /// - /// - `net` : The blockchain to post the transaction to. - /// - `txn` : The transaction data, ready to submit. - /// - /// **Returns** - /// - /// - An error if the transaction can not be posted. - /// - /// **Notes** - /// - /// This is proposed functionality, and is not yet active. - /// All calls to this function will return `post-txn-not-allowed` error. - fn post_txn( - &mut self, _net: CardanoBlockchainId, _txn: CardanoTxn, - ) -> wasmtime::Result> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/hermes/cbor/host.rs b/hermes/bin/src/runtime_extensions/hermes/cbor/host.rs new file mode 100644 index 000000000..ccd135018 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/cbor/host.rs @@ -0,0 +1,5 @@ +//! CBOR host implementation for WASM runtime. + +use crate::{runtime_extensions::bindings::hermes::cbor::api::Host, state::HermesState}; + +impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs index c265338d0..24972f85c 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs @@ -1,12 +1,8 @@ -//! Host - CBOR implementations +//! CBOR runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::cbor::api::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -16,5 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/host.rs b/hermes/bin/src/runtime_extensions/hermes/cron/host.rs new file mode 100644 index 000000000..75260877b --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/cron/host.rs @@ -0,0 +1,137 @@ +//! Cron host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::{ + hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host}, + wasi::clocks::monotonic_clock::Instant, + }, + state::HermesState, +}; + +impl Host for HermesState { + /// # Schedule Recurrent CRON event + /// + /// Cron events will be delivered to the `on-cron` event handler. + /// + /// ## Parameters + /// + /// - `entry`: The crontab entry to add. + /// - `when`: When the event triggers. Standard crontab format. + /// - `tag`: A tag which will accompany the triggered event. + /// - `retrigger`: + /// - `true`: The event will re-trigger every time the crontab entry matches until + /// cancelled. + /// - `false`: The event will automatically cancel after it is generated once. + /// + /// ## Returns + /// + /// - `true`: Crontab added successfully. (Or the crontab event already exists) + /// - `false`: Crontab failed to be added. + /// + /// ## Note: + /// + /// If the crontab entry already exists, the retrigger flag can be changed by calling + /// this function. This could be useful where a retriggering crontab event is desired + /// to be stopped, but ONLY after it has triggered once more. + fn add(&mut self, _entry: CronTagged, _retrigger: bool) -> wasmtime::Result { + todo!() + } + + /// # Schedule A Single cron event after a fixed delay. + /// + /// Allows for easy timed wait events to be delivered without + /// requiring datetime calculations or formatting cron entries. + /// + /// ## Parameters + /// + /// - `duration`: How many nanoseconds to delay. The delay will be AT LEAST this + /// long. + /// - `tag`: A tag which will accompany the triggered event. + /// + /// ## Returns + /// + /// - `true`: Crontab added successfully. + /// - `false`: Crontab failed to be added. + /// + /// ## Note: + /// + /// This is a convenience function which will automatically calculate the crontab + /// entry needed to trigger the event after the requested `duration`. + /// It is added as a non-retriggering event. + /// Listing the crontabs after this call will list the delay in addition to all other + /// crontab entries. + fn delay(&mut self, _duration: Instant, _tag: CronEventTag) -> wasmtime::Result { + todo!() + } + + /// # List currently active cron schedule. + /// + /// Allows for management of scheduled cron events. + /// + /// ## Parameters + /// + /// - `tag`: Optional, the tag to limit the list to. If `none` then all crons listed. + /// + /// ## Returns + /// + /// - A list of tuples containing the scheduled crontabs and their tags, along with + /// the current retrigger flag. + /// The list is sorted from most crontab that will trigger soonest to latest. + /// Crontabs are only listed once, in the case where a crontab may be scheduled + /// may times before a later one. + /// - `0` - `cron-tagged` - The Tagged crontab event. + /// - `1` - `bool` - The state of the retrigger flag. + fn ls(&mut self, _tag: Option) -> wasmtime::Result> { + todo!() + } + + /// # Remove the requested crontab. + /// + /// Allows for management of scheduled cron events. + /// + /// ## Parameters + /// + /// - `when`: The crontab entry to add. Standard crontab format. + /// - `tag`: A tag which will accompany the triggered event. + /// + /// ## Returns + /// + /// - `true`: The requested crontab was deleted and will not trigger. + /// - `false`: The requested crontab does not exist. + fn rm(&mut self, _entry: CronTagged) -> wasmtime::Result { + todo!() + } + + /// # Make a crontab entry from individual time values. + /// + /// Crates the properly formatted cron entry + /// from numeric cron time components. + /// Convenience function to make building cron strings simpler when they are + /// calculated from data. + /// + /// ## Parameters + /// + /// - `dow` - `DayOfWeek` (0-7, 0 or 7 = Sunday) + /// - `month` - Month of the year (1-12, 1 = January) + /// - `day` - Day in the month (1-31) + /// - `hour` - Hour in the day (0-23) + /// - `minute` - Minute in the hour (0-59) + /// + /// ## Returns + /// + /// - A matching `cron-sched` ready for use in the cron functions above. + /// + /// ## Note: + /// No checking is done to determine if the requested date is valid. + /// If a particular component is out of its allowable range it will be silently + /// clamped within the allowable range of each parameter. + /// Redundant entries will be removed. + /// - For example specifying a `month` as `3` and `2-4` will + /// remove the individual month and only produce the range. + fn mkcron( + &mut self, _dow: CronTime, _month: CronTime, _day: CronTime, _hour: CronTime, + _minute: CronTime, + ) -> wasmtime::Result { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs index 436885de4..d40a42c5e 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs @@ -1,15 +1,8 @@ -//! Host - Cron implementations +//! Cron runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::{ - hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host}, - wasi::clocks::monotonic_clock::Instant, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -19,131 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// # Schedule Recurrent CRON event - /// - /// Cron events will be delivered to the `on-cron` event handler. - /// - /// ## Parameters - /// - /// - `entry`: The crontab entry to add. - /// - `when`: When the event triggers. Standard crontab format. - /// - `tag`: A tag which will accompany the triggered event. - /// - `retrigger`: - /// - `true`: The event will re-trigger every time the crontab entry matches until - /// cancelled. - /// - `false`: The event will automatically cancel after it is generated once. - /// - /// ## Returns - /// - /// - `true`: Crontab added successfully. (Or the crontab event already exists) - /// - `false`: Crontab failed to be added. - /// - /// ## Note: - /// - /// If the crontab entry already exists, the retrigger flag can be changed by calling - /// this function. This could be useful where a retriggering crontab event is desired - /// to be stopped, but ONLY after it has triggered once more. - fn add(&mut self, _entry: CronTagged, _retrigger: bool) -> wasmtime::Result { - todo!() - } - - /// # Schedule A Single cron event after a fixed delay. - /// - /// Allows for easy timed wait events to be delivered without - /// requiring datetime calculations or formatting cron entries. - /// - /// ## Parameters - /// - /// - `duration`: How many nanoseconds to delay. The delay will be AT LEAST this - /// long. - /// - `tag`: A tag which will accompany the triggered event. - /// - /// ## Returns - /// - /// - `true`: Crontab added successfully. - /// - `false`: Crontab failed to be added. - /// - /// ## Note: - /// - /// This is a convenience function which will automatically calculate the crontab - /// entry needed to trigger the event after the requested `duration`. - /// It is added as a non-retriggering event. - /// Listing the crontabs after this call will list the delay in addition to all other - /// crontab entries. - fn delay(&mut self, _duration: Instant, _tag: CronEventTag) -> wasmtime::Result { - todo!() - } - - /// # List currently active cron schedule. - /// - /// Allows for management of scheduled cron events. - /// - /// ## Parameters - /// - /// - `tag`: Optional, the tag to limit the list to. If `none` then all crons listed. - /// - /// ## Returns - /// - /// - A list of tuples containing the scheduled crontabs and their tags, along with - /// the current retrigger flag. - /// The list is sorted from most crontab that will trigger soonest to latest. - /// Crontabs are only listed once, in the case where a crontab may be scheduled - /// may times before a later one. - /// - `0` - `cron-tagged` - The Tagged crontab event. - /// - `1` - `bool` - The state of the retrigger flag. - fn ls(&mut self, _tag: Option) -> wasmtime::Result> { - todo!() - } - - /// # Remove the requested crontab. - /// - /// Allows for management of scheduled cron events. - /// - /// ## Parameters - /// - /// - `when`: The crontab entry to add. Standard crontab format. - /// - `tag`: A tag which will accompany the triggered event. - /// - /// ## Returns - /// - /// - `true`: The requested crontab was deleted and will not trigger. - /// - `false`: The requested crontab does not exist. - fn rm(&mut self, _entry: CronTagged) -> wasmtime::Result { - todo!() - } - - /// # Make a crontab entry from individual time values. - /// - /// Crates the properly formatted cron entry - /// from numeric cron time components. - /// Convenience function to make building cron strings simpler when they are - /// calculated from data. - /// - /// ## Parameters - /// - /// - `dow` - `DayOfWeek` (0-7, 0 or 7 = Sunday) - /// - `month` - Month of the year (1-12, 1 = January) - /// - `day` - Day in the month (1-31) - /// - `hour` - Hour in the day (0-23) - /// - `minute` - Minute in the hour (0-59) - /// - /// ## Returns - /// - /// - A matching `cron-sched` ready for use in the cron functions above. - /// - /// ## Note: - /// No checking is done to determine if the requested date is valid. - /// If a particular component is out of its allowable range it will be silently - /// clamped within the allowable range of each parameter. - /// Redundant entries will be removed. - /// - For example specifying a `month` as `3` and `2-4` will - /// remove the individual month and only produce the range. - fn mkcron( - &mut self, _dow: CronTime, _month: CronTime, _day: CronTime, _hour: CronTime, - _minute: CronTime, - ) -> wasmtime::Result { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/hermes/crypto/host.rs b/hermes/bin/src/runtime_extensions/hermes/crypto/host.rs new file mode 100644 index 000000000..c014e60be --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/crypto/host.rs @@ -0,0 +1,84 @@ +//! Crypto host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::hermes::{ + binary::api::Bstr, + crypto::api::{ + Ed25519Bip32, Ed25519Bip32PrivateKey, Ed25519Bip32PublicKey, Ed25519Bip32Signature, + Host, HostEd25519Bip32, + }, + }, + state::HermesState, +}; + +impl HostEd25519Bip32 for HermesState { + /// Create a new ED25519-BIP32 Crypto resource + /// + /// **Parameters** + /// + /// - `private_key` : The key to use, if not supplied one is RANDOMLY generated. + fn new( + &mut self, _private_key: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the public key for this private key. + fn public_key( + &mut self, _resource: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Sign data with the Private key, and return it. + /// + /// **Parameters** + /// + /// - `data` : The data to sign. + fn sign_data( + &mut self, _resource: wasmtime::component::Resource, _data: Bstr, + ) -> wasmtime::Result { + todo!() + } + + /// Check a signature on a set of data. + /// + /// **Parameters** + /// + /// - `data` : The data to check. + /// - `sig` : The signature to check. + /// + /// **Returns** + /// + /// - `true` : Signature checked OK. + /// - `false` : Signature check failed. + fn check_sig( + &mut self, _resource: wasmtime::component::Resource, _data: Bstr, + _sig: Ed25519Bip32Signature, + ) -> wasmtime::Result { + todo!() + } + + /// Derive a new private key from the current private key. + /// + /// Note: uses BIP32 HD key derivation. + fn derive( + &mut self, _resource: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Create a new RANDOM private key. + /// + /// Note, this does not need to be used, as the constructor will do this + /// automatically. + fn gen_private_key(&mut self) -> wasmtime::Result { + todo!() + } + + fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { + todo!() + } +} + +impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs b/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs index 4d44c5ef5..6cbb6a69d 100644 --- a/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/crypto/mod.rs @@ -1,18 +1,8 @@ -//! Host - Crypto implementations +//! Crypto runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::{ - binary::api::Bstr, - crypto::api::{ - Ed25519Bip32, Ed25519Bip32PrivateKey, Ed25519Bip32PublicKey, Ed25519Bip32Signature, - Host, HostEd25519Bip32, - }, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -22,75 +12,3 @@ impl Stateful for State { State {} } } - -impl HostEd25519Bip32 for HermesState { - /// Create a new ED25519-BIP32 Crypto resource - /// - /// **Parameters** - /// - /// - `private_key` : The key to use, if not supplied one is RANDOMLY generated. - fn new( - &mut self, _private_key: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the public key for this private key. - fn public_key( - &mut self, _resource: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Sign data with the Private key, and return it. - /// - /// **Parameters** - /// - /// - `data` : The data to sign. - fn sign_data( - &mut self, _resource: wasmtime::component::Resource, _data: Bstr, - ) -> wasmtime::Result { - todo!() - } - - /// Check a signature on a set of data. - /// - /// **Parameters** - /// - /// - `data` : The data to check. - /// - `sig` : The signature to check. - /// - /// **Returns** - /// - /// - `true` : Signature checked OK. - /// - `false` : Signature check failed. - fn check_sig( - &mut self, _resource: wasmtime::component::Resource, _data: Bstr, - _sig: Ed25519Bip32Signature, - ) -> wasmtime::Result { - todo!() - } - - /// Derive a new private key from the current private key. - /// - /// Note: uses BIP32 HD key derivation. - fn derive( - &mut self, _resource: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Create a new RANDOM private key. - /// - /// Note, this does not need to be used, as the constructor will do this - /// automatically. - fn gen_private_key(&mut self) -> wasmtime::Result { - todo!() - } - - fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - todo!() - } -} - -impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/hash/host.rs b/hermes/bin/src/runtime_extensions/hermes/hash/host.rs new file mode 100644 index 000000000..1e196eb89 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/hash/host.rs @@ -0,0 +1,32 @@ +//! Hash host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::hermes::{ + binary::api::Bstr, + hash::api::{Errno, Host}, + }, + state::HermesState, +}; + +impl Host for HermesState { + /// Hash a binary buffer with BLAKE2s + fn blake2s( + &mut self, _buf: Bstr, _outlen: Option, _key: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Hash a binary buffer with `BLAKE2b` + fn blake2b( + &mut self, _buf: Bstr, _outlen: Option, _key: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Hash a binary buffer with BLAKE3 + fn blake3( + &mut self, _buf: Bstr, _outlen: Option, _key: Option, + ) -> wasmtime::Result> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs b/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs index 4903a7875..db70a87d0 100644 --- a/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/hash/mod.rs @@ -1,15 +1,8 @@ -//! Host - Hash implementations +//! Hash runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::{ - binary::api::Bstr, - hash::api::{Errno, Host}, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -19,26 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// Hash a binary buffer with BLAKE2s - fn blake2s( - &mut self, _buf: Bstr, _outlen: Option, _key: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Hash a binary buffer with `BLAKE2b` - fn blake2b( - &mut self, _buf: Bstr, _outlen: Option, _key: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Hash a binary buffer with BLAKE3 - fn blake3( - &mut self, _buf: Bstr, _outlen: Option, _key: Option, - ) -> wasmtime::Result> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/hermes/init/mod.rs b/hermes/bin/src/runtime_extensions/hermes/init/mod.rs index c2c837459..0eee10a7b 100644 --- a/hermes/bin/src/runtime_extensions/hermes/init/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/init/mod.rs @@ -1,4 +1,4 @@ -//! Host - Init implementations +//! Init runtime extension implementation. use crate::runtime_extensions::state::{Context, Stateful}; diff --git a/hermes/bin/src/runtime_extensions/hermes/json/host.rs b/hermes/bin/src/runtime_extensions/hermes/json/host.rs new file mode 100644 index 000000000..00e7e46c8 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/json/host.rs @@ -0,0 +1,5 @@ +//! JSON host implementation for WASM runtime. + +use crate::{runtime_extensions::bindings::hermes::json::api::Host, state::HermesState}; + +impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/json/mod.rs b/hermes/bin/src/runtime_extensions/hermes/json/mod.rs index 835f51262..68389f3f6 100644 --- a/hermes/bin/src/runtime_extensions/hermes/json/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/json/mod.rs @@ -1,12 +1,8 @@ -//! Host - JSON implementations +//! JSON runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::json::api::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -16,5 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState {} diff --git a/hermes/bin/src/runtime_extensions/hermes/kv_store/host.rs b/hermes/bin/src/runtime_extensions/hermes/kv_store/host.rs new file mode 100644 index 000000000..eb9647e55 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/kv_store/host.rs @@ -0,0 +1,86 @@ +//! KV-Store host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::hermes::kv_store::api::{Host, KvValues}, + state::HermesState, +}; + +impl Host for HermesState { + /// Set a value in the local key-value store + /// Setting None will cause the Key to be deleted from the KV store. + fn kv_set(&mut self, _key: String, _value: Option) -> wasmtime::Result<()> { + todo!() + } + + /// Get a value from the local key-value store + /// Returns the default if not set. + fn kv_get_default( + &mut self, _key: String, _default: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get a value from the local key-value store + /// Returns None if the Key does not exist in the KV Store. + /// This is a convenience function, and is equivalent to `kv-get-default(key, none)` + fn kv_get(&mut self, _key: String) -> wasmtime::Result> { + todo!() + } + + /// Get a value, and then set it (Atomic) + /// Setting None will cause the Key to be deleted from the KV store. + fn kv_get_set( + &mut self, _key: String, _value: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get a value, and then add to it (Atomic) + /// Adding to a string will concatenate the string. + /// String concatenation will only occur up to the maximum possible size of a string + /// value.\\ + /// Concatenation beyond the maximum size will result in truncation. + /// Adding to a numeric will have the expected behavior (rounded to nearest if + /// necessary). + /// The original type does not change, so: `float64 + u64 = float64`. `s64 + float64 + /// = s64` + /// If the value overflows or under-flows it will saturate at the limit. + /// This behavior allows us to decrement values by using the signed version, so + /// `u64(10) + s64(-5) = u64(5))` + /// If a string is added to a numeric, nothing happens. + /// If a numeric is added to a string, it is converted to a string first, and then + /// concatenated + /// Note: There will be no spaces added. So "My string" + u32(77) = "My string77" + fn kv_add( + &mut self, _key: String, _value: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Check if the Key equals a test value (exact match) and if it does, store the new + /// value. + /// In all cases, the current value is returned. + /// If the types are NOT the same, the comparison will fail, even if the values are + /// equivalent. + /// For example: `u64(7) != s64(7)`, `float64(-1) != s64(-1)`. + fn kv_cas( + &mut self, _key: String, _test: Option, _value: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Subscribe to any updates made to a particular Key. + /// After this call, this module will receive Key Update events when a key is written. + /// It returns the current value of the Key and None if it is not set. + fn kv_subscribe(&mut self, _key: String) -> wasmtime::Result> { + todo!() + } + + /// Unsubscribe to any updates made to a particular Key. + /// After this call, this module will no longer receive Key Update events when a key + /// is written. + /// It returns the current value of the Key and None if it is not set. + fn kv_unsubscribe(&mut self, _key: String) -> wasmtime::Result> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs index 5cbdbbcc6..66dbea735 100644 --- a/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs @@ -1,12 +1,8 @@ -//! Host - KV-Store implementations +//! KV-Store runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::kv_store::api::{Host, KvValues}, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -16,83 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// Set a value in the local key-value store - /// Setting None will cause the Key to be deleted from the KV store. - fn kv_set(&mut self, _key: String, _value: Option) -> wasmtime::Result<()> { - todo!() - } - - /// Get a value from the local key-value store - /// Returns the default if not set. - fn kv_get_default( - &mut self, _key: String, _default: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get a value from the local key-value store - /// Returns None if the Key does not exist in the KV Store. - /// This is a convenience function, and is equivalent to `kv-get-default(key, none)` - fn kv_get(&mut self, _key: String) -> wasmtime::Result> { - todo!() - } - - /// Get a value, and then set it (Atomic) - /// Setting None will cause the Key to be deleted from the KV store. - fn kv_get_set( - &mut self, _key: String, _value: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get a value, and then add to it (Atomic) - /// Adding to a string will concatenate the string. - /// String concatenation will only occur up to the maximum possible size of a string - /// value.\\ - /// Concatenation beyond the maximum size will result in truncation. - /// Adding to a numeric will have the expected behavior (rounded to nearest if - /// necessary). - /// The original type does not change, so: `float64 + u64 = float64`. `s64 + float64 - /// = s64` - /// If the value overflows or under-flows it will saturate at the limit. - /// This behavior allows us to decrement values by using the signed version, so - /// `u64(10) + s64(-5) = u64(5))` - /// If a string is added to a numeric, nothing happens. - /// If a numeric is added to a string, it is converted to a string first, and then - /// concatenated - /// Note: There will be no spaces added. So "My string" + u32(77) = "My string77" - fn kv_add( - &mut self, _key: String, _value: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Check if the Key equals a test value (exact match) and if it does, store the new - /// value. - /// In all cases, the current value is returned. - /// If the types are NOT the same, the comparison will fail, even if the values are - /// equivalent. - /// For example: `u64(7) != s64(7)`, `float64(-1) != s64(-1)`. - fn kv_cas( - &mut self, _key: String, _test: Option, _value: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Subscribe to any updates made to a particular Key. - /// After this call, this module will receive Key Update events when a key is written. - /// It returns the current value of the Key and None if it is not set. - fn kv_subscribe(&mut self, _key: String) -> wasmtime::Result> { - todo!() - } - - /// Unsubscribe to any updates made to a particular Key. - /// After this call, this module will no longer receive Key Update events when a key - /// is written. - /// It returns the current value of the Key and None if it is not set. - fn kv_unsubscribe(&mut self, _key: String) -> wasmtime::Result> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/hermes/localtime/host.rs b/hermes/bin/src/runtime_extensions/hermes/localtime/host.rs new file mode 100644 index 000000000..67af83d8b --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/localtime/host.rs @@ -0,0 +1,62 @@ +//! Localtime host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::{ + hermes::localtime::api::{Errno, Host, Localtime, Timezone}, + wasi::clocks::wall_clock::Datetime, + }, + state::HermesState, +}; + +impl Host for HermesState { + /// Get localtime from a datetime or now. + /// + /// **Parameters** + /// + /// `when` : The datetime we want to convert (Optional, if not set it will convert + /// `now`). + /// `tz` : The timezone to use. (Optional, if not set uses the local machines + /// configured local timezone.) + /// + /// **Returns** + /// + /// `localtime` : the converted time. + /// `errno` : An error indicating why conversion failed. + fn get_localtime( + &mut self, _when: Option, _tz: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get a new localtime from a localtime, by recalculating time for a new timezone. + /// + /// **Parameters** + /// + /// `time` : The localtime to convert. + /// `tz` : The timezone to use. (Optional, if not set uses the local machines + /// configured local timezone.) + /// + /// **Returns** + /// + /// `localtime` : the converted time. + /// `errno` : An error indicating why conversion failed. + fn alt_localtime( + &mut self, _time: Localtime, _tz: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get a datetime from a localtime. + /// + /// **Parameters** + /// + /// `time` : The localtime to convert. + /// + /// **Returns** + /// + /// `datetime` : the converted time. + /// `errno` : An error indicating why conversion failed. + fn get_datetime(&mut self, _time: Localtime) -> wasmtime::Result> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs b/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs index 11be28678..9472a28b9 100644 --- a/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/localtime/mod.rs @@ -1,15 +1,8 @@ -//! Host - Localtime implementations +//! Localtime runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::{ - hermes::localtime::api::{Errno, Host, Localtime, Timezone}, - wasi::clocks::wall_clock::Datetime, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -19,56 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// Get localtime from a datetime or now. - /// - /// **Parameters** - /// - /// `when` : The datetime we want to convert (Optional, if not set it will convert - /// `now`). - /// `tz` : The timezone to use. (Optional, if not set uses the local machines - /// configured local timezone.) - /// - /// **Returns** - /// - /// `localtime` : the converted time. - /// `errno` : An error indicating why conversion failed. - fn get_localtime( - &mut self, _when: Option, _tz: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get a new localtime from a localtime, by recalculating time for a new timezone. - /// - /// **Parameters** - /// - /// `time` : The localtime to convert. - /// `tz` : The timezone to use. (Optional, if not set uses the local machines - /// configured local timezone.) - /// - /// **Returns** - /// - /// `localtime` : the converted time. - /// `errno` : An error indicating why conversion failed. - fn alt_localtime( - &mut self, _time: Localtime, _tz: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get a datetime from a localtime. - /// - /// **Parameters** - /// - /// `time` : The localtime to convert. - /// - /// **Returns** - /// - /// `datetime` : the converted time. - /// `errno` : An error indicating why conversion failed. - fn get_datetime(&mut self, _time: Localtime) -> wasmtime::Result> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/hermes/logging/host.rs b/hermes/bin/src/runtime_extensions/hermes/logging/host.rs new file mode 100644 index 000000000..dd3962815 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/logging/host.rs @@ -0,0 +1,63 @@ +//! Logging host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::hermes::{ + json::api::Json, + logging::api::{Host, Level}, + }, + state::HermesState, +}; + +impl Host for HermesState { + /// Generate a Log + /// + /// The Hermes API will add extra information to the log, such as the instance of the + /// webasm + /// module being logged. + /// The Webasm module does not need to concern itself with this kind of information, + /// and should + /// log as if it is the only instance. + /// It also should not log any webasm shared context, except where it is relevant to + /// the log message itself. + /// + /// **Parameters** + /// + /// - `level` : The log level this message is for. + /// - `file` : The name of the src file being logged from. (Optional) + /// - `fn` : The function within the file being logged from. (Optional) + /// - `line` : The line of code the log was generated from. (Optional) + /// - `col` : The column of code the log was generated from. (Optional) + /// - `ctx` : The logging context. (Should have no newlines or formatting). + /// - `msg` : A Single line message to be logged. (Should have no newlines or + /// formatting). + /// - `data` : A Free form json payload that will be logged with the msg. This must + /// be valid JSON. + /// + /// *Notes* + /// + /// The `data` parameter may contain a record of the format: + /// ```json + /// { + /// "bt" : [ , ] + /// } + /// ``` + /// The logger will interpret this as a backtrace where each entry in the array is one + /// line of the backtrace. + /// The format of the backtrace lines is up to the webasm module generating the log. + /// The individual backtrace entries may contain line breaks if the backtrace entry is + /// multiline. + /// * Multiline backtrace entries should be de-dented, relative to the first line. + /// * This is to allow the display to properly format multiline entries. + /// This format is designed to keep the broadest flexibility for multiple languages + /// capabilities. + /// The backtrace must be sorted with most recent lines of the backtrace occurring + /// first in the array. + /// Backtrace must be contained in a single `log` call. Multiple log calls will be + /// considered independent logs. + fn log( + &mut self, _level: Level, _file: Option, _fn_: Option, _line: Option, + _col: Option, _ctx: Option, _msg: String, _data: Option, + ) -> wasmtime::Result<()> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs b/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs index dff4d2358..dc7d340eb 100644 --- a/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/logging/mod.rs @@ -1,15 +1,8 @@ -//! Host - Logging implementations +//! Logging runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::hermes::{ - json::api::Json, - logging::api::{Host, Level}, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// State pub(crate) struct State {} @@ -19,57 +12,3 @@ impl Stateful for State { State {} } } - -impl Host for HermesState { - /// Generate a Log - /// - /// The Hermes API will add extra information to the log, such as the instance of the - /// webasm - /// module being logged. - /// The Webasm module does not need to concern itself with this kind of information, - /// and should - /// log as if it is the only instance. - /// It also should not log any webasm shared context, except where it is relevant to - /// the log message itself. - /// - /// **Parameters** - /// - /// - `level` : The log level this message is for. - /// - `file` : The name of the src file being logged from. (Optional) - /// - `fn` : The function within the file being logged from. (Optional) - /// - `line` : The line of code the log was generated from. (Optional) - /// - `col` : The column of code the log was generated from. (Optional) - /// - `ctx` : The logging context. (Should have no newlines or formatting). - /// - `msg` : A Single line message to be logged. (Should have no newlines or - /// formatting). - /// - `data` : A Free form json payload that will be logged with the msg. This must - /// be valid JSON. - /// - /// *Notes* - /// - /// The `data` parameter may contain a record of the format: - /// ```json - /// { - /// "bt" : [ , ] - /// } - /// ``` - /// The logger will interpret this as a backtrace where each entry in the array is one - /// line of the backtrace. - /// The format of the backtrace lines is up to the webasm module generating the log. - /// The individual backtrace entries may contain line breaks if the backtrace entry is - /// multiline. - /// * Multiline backtrace entries should be de-dented, relative to the first line. - /// * This is to allow the display to properly format multiline entries. - /// This format is designed to keep the broadest flexibility for multiple languages - /// capabilities. - /// The backtrace must be sorted with most recent lines of the backtrace occurring - /// first in the array. - /// Backtrace must be contained in a single `log` call. Multiple log calls will be - /// considered independent logs. - fn log( - &mut self, _level: Level, _file: Option, _fn_: Option, _line: Option, - _col: Option, _ctx: Option, _msg: String, _data: Option, - ) -> wasmtime::Result<()> { - todo!() - } -} From 68681e9667a14c168eca0e4b571ae0f31e5306c0 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 13:24:16 +0200 Subject: [PATCH 05/11] move wasi runtime extensions host impl to the separate module --- .../src/runtime_extensions/wasi/cli/host.rs | 52 ++ .../src/runtime_extensions/wasi/cli/mod.rs | 58 +- .../{monotonic.rs => monotonic/host.rs} | 16 +- .../wasi/clocks/monotonic/mod.rs | 14 + .../wasi/clocks/{wall.rs => wall/host.rs} | 16 +- .../wasi/clocks/wall/mod.rs | 14 + .../wasi/filesystem/host.rs | 429 ++++++++++++ .../runtime_extensions/wasi/filesystem/mod.rs | 435 +----------- .../src/runtime_extensions/wasi/http/host.rs | 657 +++++++++++++++++ .../src/runtime_extensions/wasi/http/mod.rs | 663 +----------------- .../wasi/io/{error.rs => error/host.rs} | 16 +- .../runtime_extensions/wasi/io/error/mod.rs | 14 + .../wasi/io/{streams.rs => streams/host.rs} | 18 +- .../runtime_extensions/wasi/io/streams/mod.rs | 14 + .../random/{insecure.rs => insecure/host.rs} | 19 +- .../wasi/random/insecure/mod.rs | 14 + .../host.rs} | 18 +- .../wasi/random/insecure_seed/mod.rs | 14 + .../wasi/random/{secure.rs => secure/host.rs} | 19 +- .../wasi/random/secure/mod.rs | 14 + 20 files changed, 1263 insertions(+), 1251 deletions(-) create mode 100644 hermes/bin/src/runtime_extensions/wasi/cli/host.rs rename hermes/bin/src/runtime_extensions/wasi/clocks/{monotonic.rs => monotonic/host.rs} (61%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/mod.rs rename hermes/bin/src/runtime_extensions/wasi/clocks/{wall.rs => wall/host.rs} (77%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/clocks/wall/mod.rs create mode 100644 hermes/bin/src/runtime_extensions/wasi/filesystem/host.rs create mode 100644 hermes/bin/src/runtime_extensions/wasi/http/host.rs rename hermes/bin/src/runtime_extensions/wasi/io/{error.rs => error/host.rs} (71%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/io/error/mod.rs rename hermes/bin/src/runtime_extensions/wasi/io/{streams.rs => streams/host.rs} (96%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/io/streams/mod.rs rename hermes/bin/src/runtime_extensions/wasi/random/{insecure.rs => insecure/host.rs} (71%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/random/insecure/mod.rs rename hermes/bin/src/runtime_extensions/wasi/random/{insecure_seed.rs => insecure_seed/host.rs} (76%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/mod.rs rename hermes/bin/src/runtime_extensions/wasi/random/{secure.rs => secure/host.rs} (79%) create mode 100644 hermes/bin/src/runtime_extensions/wasi/random/secure/mod.rs diff --git a/hermes/bin/src/runtime_extensions/wasi/cli/host.rs b/hermes/bin/src/runtime_extensions/wasi/cli/host.rs new file mode 100644 index 000000000..68f22faec --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/cli/host.rs @@ -0,0 +1,52 @@ +//! CLI host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::wasi::{ + cli, + io::streams::{InputStream, OutputStream}, + }, + state::HermesState, +}; + +impl cli::environment::Host for HermesState { + /// Get the POSIX-style environment variables. + /// + /// Each environment variable is provided as a pair of string variable names + /// and string value. + /// + /// Morally, these are a value import, but until value imports are available + /// in the component model, this import function should return the same + /// values each time it is called. + fn get_environment(&mut self) -> wasmtime::Result> { + todo!() + } + + /// Get the POSIX-style arguments to the program. + fn get_arguments(&mut self) -> wasmtime::Result> { + todo!() + } + + /// Return a path that programs should use as their initial current working + /// directory, interpreting `.` as shorthand for this. + fn initial_cwd(&mut self) -> wasmtime::Result> { + todo!() + } +} + +impl cli::stdin::Host for HermesState { + fn get_stdin(&mut self) -> wasmtime::Result> { + todo!() + } +} + +impl cli::stdout::Host for HermesState { + fn get_stdout(&mut self) -> wasmtime::Result> { + todo!() + } +} + +impl cli::stderr::Host for HermesState { + fn get_stderr(&mut self) -> wasmtime::Result> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs b/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs index 0e136e805..8aebdf8a8 100644 --- a/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/cli/mod.rs @@ -1,15 +1,8 @@ -//! Host - WASI - CLI implementations +//! CLI runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::wasi::{ - cli, - io::streams::{InputStream, OutputStream}, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// WASI State pub(crate) struct State {} @@ -19,46 +12,3 @@ impl Stateful for State { Self {} } } - -impl cli::environment::Host for HermesState { - /// Get the POSIX-style environment variables. - /// - /// Each environment variable is provided as a pair of string variable names - /// and string value. - /// - /// Morally, these are a value import, but until value imports are available - /// in the component model, this import function should return the same - /// values each time it is called. - fn get_environment(&mut self) -> wasmtime::Result> { - todo!() - } - - /// Get the POSIX-style arguments to the program. - fn get_arguments(&mut self) -> wasmtime::Result> { - todo!() - } - - /// Return a path that programs should use as their initial current working - /// directory, interpreting `.` as shorthand for this. - fn initial_cwd(&mut self) -> wasmtime::Result> { - todo!() - } -} - -impl cli::stdin::Host for HermesState { - fn get_stdin(&mut self) -> wasmtime::Result> { - todo!() - } -} - -impl cli::stdout::Host for HermesState { - fn get_stdout(&mut self) -> wasmtime::Result> { - todo!() - } -} - -impl cli::stderr::Host for HermesState { - fn get_stderr(&mut self) -> wasmtime::Result> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/host.rs similarity index 61% rename from hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs rename to hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/host.rs index a3abcd6cc..77eb45e4e 100644 --- a/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic.rs +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/host.rs @@ -1,22 +1,10 @@ -//! Host - WASI - monotonic clock implementations +//! Monotonic clock host implementation for WASM runtime. use crate::{ - runtime_extensions::{ - bindings::wasi::clocks::monotonic_clock::{Duration, Host, Instant}, - state::{Context, Stateful}, - }, + runtime_extensions::bindings::wasi::clocks::monotonic_clock::{Duration, Host, Instant}, state::HermesState, }; -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} - impl Host for HermesState { /// Read the current value of the clock. /// diff --git a/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/mod.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/mod.rs new file mode 100644 index 000000000..23cf470f0 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/monotonic/mod.rs @@ -0,0 +1,14 @@ +//! Monotonic clock runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/wall/host.rs similarity index 77% rename from hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs rename to hermes/bin/src/runtime_extensions/wasi/clocks/wall/host.rs index 6c2b4949f..e6d043eee 100644 --- a/hermes/bin/src/runtime_extensions/wasi/clocks/wall.rs +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/wall/host.rs @@ -1,22 +1,10 @@ -//! Host - WASI - Wall Clock implementations +//! Wall clock host implementation for WASM runtime. use crate::{ - runtime_extensions::{ - bindings::wasi::clocks::wall_clock::{Datetime, Host}, - state::{Context, Stateful}, - }, + runtime_extensions::bindings::wasi::clocks::wall_clock::{Datetime, Host}, state::HermesState, }; -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} - impl Host for HermesState { /// Read the current value of the clock. /// diff --git a/hermes/bin/src/runtime_extensions/wasi/clocks/wall/mod.rs b/hermes/bin/src/runtime_extensions/wasi/clocks/wall/mod.rs new file mode 100644 index 000000000..cfdcad95d --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/clocks/wall/mod.rs @@ -0,0 +1,14 @@ +//! Wall clock runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/filesystem/host.rs b/hermes/bin/src/runtime_extensions/wasi/filesystem/host.rs new file mode 100644 index 000000000..a61460e3b --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/filesystem/host.rs @@ -0,0 +1,429 @@ +//! Filesystem host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::wasi::{ + filesystem::{ + self, + types::{ + Advice, Descriptor, DescriptorFlags, DescriptorStat, DescriptorType, + DirectoryEntry, DirectoryEntryStream, Error, ErrorCode, Filesize, + MetadataHashValue, NewTimestamp, OpenFlags, PathFlags, + }, + }, + io::streams::{InputStream, OutputStream}, + }, + state::HermesState, +}; + +impl filesystem::types::HostDescriptor for HermesState { + /// Return a stream for reading from a file, if available. + /// + /// May fail with an error-code describing why the file cannot be read. + /// + /// Multiple read, write, and append streams may be active on the same open + /// file and they do not interfere with each other. + /// + /// Note: This allows using `read-stream`, which is similar to `read` in POSIX. + fn read_via_stream( + &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, + ) -> wasmtime::Result, ErrorCode>> { + todo!() + } + + /// Return a stream for writing to a file, if available. + /// + /// May fail with an error-code describing why the file cannot be written. + /// + /// Note: This allows using `write-stream`, which is similar to `write` in + /// POSIX. + fn write_via_stream( + &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, + ) -> wasmtime::Result, ErrorCode>> { + todo!() + } + + /// Return a stream for appending to a file, if available. + /// + /// May fail with an error-code describing why the file cannot be appended. + /// + /// Note: This allows using `write-stream`, which is similar to `write` with + /// `O_APPEND` in in POSIX. + fn append_via_stream( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result, ErrorCode>> { + todo!() + } + + /// Provide file advisory information on a descriptor. + /// + /// This is similar to `posix_fadvise` in POSIX. + fn advise( + &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, + _length: Filesize, _advice: Advice, + ) -> wasmtime::Result> { + todo!() + } + + /// Synchronize the data of a file to disk. + /// + /// This function succeeds with no effect if the file descriptor is not + /// opened for writing. + /// + /// Note: This is similar to `fdatasync` in POSIX. + fn sync_data( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Get flags associated with a descriptor. + /// + /// Note: This returns similar flags to `fcntl(fd, F_GETFL)` in POSIX. + /// + /// Note: This returns the value that was the `fs_flags` value returned + /// from `fdstat_get` in earlier versions of WASI. + fn get_flags( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the dynamic type of a descriptor. + /// + /// Note: This returns the same value as the `type` field of the `fd-stat` + /// returned by `stat`, `stat-at` and similar. + /// + /// Note: This returns similar flags to the `st_mode & S_IFMT` value provided + /// by `fstat` in POSIX. + /// + /// Note: This returns the value that was the `fs_filetype` value returned + /// from `fdstat_get` in earlier versions of WASI. + fn get_type( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Adjust the size of an open file. If this increases the file\'s size, the + /// extra bytes are filled with zeros. + /// + /// Note: This was called `fd_filestat_set_size` in earlier versions of WASI. + fn set_size( + &mut self, _descriptor: wasmtime::component::Resource, _size: Filesize, + ) -> wasmtime::Result> { + todo!() + } + + /// Adjust the timestamps of an open file or directory. + /// + /// Note: This is similar to `futimens` in POSIX. + /// + /// Note: This was called `fd_filestat_set_times` in earlier versions of WASI. + fn set_times( + &mut self, _descriptor: wasmtime::component::Resource, + _data_access_timestamp: NewTimestamp, _data_modification_timestamp: NewTimestamp, + ) -> wasmtime::Result> { + todo!() + } + + /// Read from a descriptor, without using and updating the descriptor\'s offset. + /// + /// This function returns a list of bytes containing the data that was + /// read, along with a bool which, when true, indicates that the end of the + /// file was reached. The returned list will contain up to `length` bytes; it + /// may return fewer than requested, if the end of the file is reached or + /// if the I/O operation is interrupted. + /// + /// In the future, this may change to return a `stream`. + /// + /// Note: This is similar to `pread` in POSIX. + fn read( + &mut self, _descriptor: wasmtime::component::Resource, _length: Filesize, + _offset: Filesize, + ) -> wasmtime::Result, bool), ErrorCode>> { + todo!() + } + + /// Write to a descriptor, without using and updating the descriptor\'s offset. + /// + /// It is valid to write past the end of a file; the file is extended to the + /// extent of the write, with bytes between the previous end and the start of + /// the write set to zero. + /// + /// In the future, this may change to take a `stream`. + /// + /// Note: This is similar to `pwrite` in POSIX. + fn write( + &mut self, _descriptor: wasmtime::component::Resource, _buffer: Vec, + _offset: Filesize, + ) -> wasmtime::Result> { + todo!() + } + + /// Read directory entries from a directory. + /// + /// On filesystems where directories contain entries referring to themselves + /// and their parents, often named `.` and `..` respectively, these entries + /// are omitted. + /// + /// This always returns a new stream which starts at the beginning of the + /// directory. Multiple streams may be active on the same directory, and they + /// do not interfere with each other. + fn read_directory( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result, ErrorCode>> + { + todo!() + } + + /// Synchronize the data and metadata of a file to disk. + /// + /// This function succeeds with no effect if the file descriptor is not + /// opened for writing. + /// + /// Note: This is similar to `fsync` in POSIX. + fn sync( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Create a directory. + /// + /// Note: This is similar to `mkdirat` in POSIX. + fn create_directory_at( + &mut self, _descriptor: wasmtime::component::Resource, _path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Return the attributes of an open file or directory. + /// + /// Note: This is similar to `fstat` in POSIX, except that it does not return + /// device and inode information. For testing whether two descriptors refer to + /// the same underlying filesystem object, use `is-same-object`. To obtain + /// additional data that can be used do determine whether a file has been + /// modified, use `metadata-hash`. + /// + /// Note: This was called `fd_filestat_get` in earlier versions of WASI. + fn stat( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Return the attributes of a file or directory. + /// + /// Note: This is similar to `fstatat` in POSIX, except that it does not + /// return device and inode information. See the `stat` description for a + /// discussion of alternatives. + /// + /// Note: This was called `path_filestat_get` in earlier versions of WASI. + fn stat_at( + &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, + _path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Adjust the timestamps of a file or directory. + /// + /// Note: This is similar to `utimensat` in POSIX. + /// + /// Note: This was called `path_filestat_set_times` in earlier versions of + /// WASI. + fn set_times_at( + &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, + _path: String, _data_access_timestamp: NewTimestamp, + _data_modification_timestamp: NewTimestamp, + ) -> wasmtime::Result> { + todo!() + } + + /// Create a hard link. + /// + /// Note: This is similar to `linkat` in POSIX. + fn link_at( + &mut self, _descriptor: wasmtime::component::Resource, + _old_path_flags: PathFlags, _old_path: String, + _new_descriptor: wasmtime::component::Resource, _new_path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Open a file or directory. + /// + /// The returned descriptor is not guaranteed to be the lowest-numbered + /// descriptor not currently open/ it is randomized to prevent applications + /// from depending on making assumptions about indexes, since this is + /// error-prone in multi-threaded contexts. The returned descriptor is + /// guaranteed to be less than 2**31. + /// + /// If `flags` contains `descriptor-flags::mutate-directory`, and the base + /// descriptor doesn't have `descriptor-flags::mutate-directory` set, + /// `open-at` fails with `error-code::read-only`. + /// + /// If `flags` contains `write` or `mutate-directory`, or `open-flags` + /// contains `truncate` or `create`, and the base descriptor doesn't have + /// `descriptor-flags::mutate-directory` set, `open-at` fails with + /// `error-code::read-only`. + /// + /// Note: This is similar to `openat` in POSIX. + fn open_at( + &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, + _path: String, _open_flags: OpenFlags, _flags: DescriptorFlags, + ) -> wasmtime::Result, ErrorCode>> { + todo!() + } + + /// Read the contents of a symbolic link. + /// + /// If the contents contain an absolute or rooted path in the underlying + /// filesystem, this function fails with `error-code::not-permitted`. + /// + /// Note: This is similar to `readlinkat` in POSIX. + fn readlink_at( + &mut self, _descriptor: wasmtime::component::Resource, _path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Remove a directory. + /// + /// Return `error-code::not-empty` if the directory is not empty. + /// + /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. + fn remove_directory_at( + &mut self, _descriptor: wasmtime::component::Resource, _path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Rename a filesystem object. + /// + /// Note: This is similar to `renameat` in POSIX. + fn rename_at( + &mut self, _old_descriptor: wasmtime::component::Resource, _old_path: String, + _new_descriptor: wasmtime::component::Resource, _new_path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Create a symbolic link (also known as a "symlink"). + /// + /// If `old-path` starts with `/`, the function fails with + /// `error-code::not-permitted`. + /// + /// Note: This is similar to `symlinkat` in POSIX. + fn symlink_at( + &mut self, _old_descriptor: wasmtime::component::Resource, _old_path: String, + _new_path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Unlink a filesystem object that is not a directory. + /// + /// Return `error-code::is-directory` if the path refers to a directory. + /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. + fn unlink_file_at( + &mut self, _descriptor: wasmtime::component::Resource, _path: String, + ) -> wasmtime::Result> { + todo!() + } + + /// Test whether two descriptors refer to the same filesystem object. + /// + /// In POSIX, this corresponds to testing whether the two descriptors have the + /// same device (`st_dev`) and inode (`st_ino` or `d_ino`) numbers. + /// wasi-filesystem does not expose device and inode numbers, so this function + /// may be used instead. + fn is_same_object( + &mut self, _descriptor: wasmtime::component::Resource, + _other: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Return a hash of the metadata associated with a filesystem object referred + /// to by a descriptor. + /// + /// This returns a hash of the last-modification timestamp and file size, and + /// may also include the inode number, device number, birth timestamp, and + /// other metadata fields that may change when the file is modified or + /// replaced. It may also include a secret value chosen by the + /// implementation and not otherwise exposed. + /// + /// Implementations are encouraged to provide the following properties: + /// + /// - If the file is not modified or replaced, the computed hash value should + /// usually not change. + /// - If the object is modified or replaced, the computed hash value should + /// usually change. + /// - The inputs to the hash should not be easily computable from the + /// computed hash. + /// + /// However, none of these is required. + fn metadata_hash( + &mut self, _descriptor: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Return a hash of the metadata associated with a filesystem object referred + /// to by a directory descriptor and a relative path. + /// + /// This performs the same hash computation as `metadata-hash`. + fn metadata_hash_at( + &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, + _path: String, + ) -> wasmtime::Result> { + todo!() + } + + fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { + todo!() + } +} + +impl filesystem::types::HostDirectoryEntryStream for HermesState { + /// Read a single directory entry from a `directory-entry-stream`. + fn read_directory_entry( + &mut self, _dir: wasmtime::component::Resource, + ) -> wasmtime::Result, ErrorCode>> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl filesystem::types::Host for HermesState { + /// Attempts to extract a filesystem-related `error-code` from the stream + /// `error` provided. + /// + /// Stream operations which return `stream-error::last-operation-failed` + /// have a payload with more information about the operation that failed. + /// This payload can be passed through to this function to see if there\'s + /// filesystem-related information about the error to return. + /// + /// Note that this function is fallible because not all stream-related + /// errors are filesystem-related errors. + fn filesystem_error_code( + &mut self, _err: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } +} + +impl filesystem::preopens::Host for HermesState { + /// Return the set of preopened directories, and their path. + fn get_directories( + &mut self, + ) -> wasmtime::Result, String)>> { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs b/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs index fd4771edb..aa77b84c2 100644 --- a/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/filesystem/mod.rs @@ -1,22 +1,8 @@ -//! Host - WASI - Filesystem implementations +//! Filesystem runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::wasi::{ - filesystem::{ - self, - types::{ - Advice, Descriptor, DescriptorFlags, DescriptorStat, DescriptorType, - DirectoryEntry, DirectoryEntryStream, Error, ErrorCode, Filesize, - MetadataHashValue, NewTimestamp, OpenFlags, PathFlags, - }, - }, - io::streams::{InputStream, OutputStream}, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// WASI State pub(crate) struct State {} @@ -26,416 +12,3 @@ impl Stateful for State { Self {} } } - -impl filesystem::types::HostDescriptor for HermesState { - /// Return a stream for reading from a file, if available. - /// - /// May fail with an error-code describing why the file cannot be read. - /// - /// Multiple read, write, and append streams may be active on the same open - /// file and they do not interfere with each other. - /// - /// Note: This allows using `read-stream`, which is similar to `read` in POSIX. - fn read_via_stream( - &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, - ) -> wasmtime::Result, ErrorCode>> { - todo!() - } - - /// Return a stream for writing to a file, if available. - /// - /// May fail with an error-code describing why the file cannot be written. - /// - /// Note: This allows using `write-stream`, which is similar to `write` in - /// POSIX. - fn write_via_stream( - &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, - ) -> wasmtime::Result, ErrorCode>> { - todo!() - } - - /// Return a stream for appending to a file, if available. - /// - /// May fail with an error-code describing why the file cannot be appended. - /// - /// Note: This allows using `write-stream`, which is similar to `write` with - /// `O_APPEND` in in POSIX. - fn append_via_stream( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result, ErrorCode>> { - todo!() - } - - /// Provide file advisory information on a descriptor. - /// - /// This is similar to `posix_fadvise` in POSIX. - fn advise( - &mut self, _descriptor: wasmtime::component::Resource, _offset: Filesize, - _length: Filesize, _advice: Advice, - ) -> wasmtime::Result> { - todo!() - } - - /// Synchronize the data of a file to disk. - /// - /// This function succeeds with no effect if the file descriptor is not - /// opened for writing. - /// - /// Note: This is similar to `fdatasync` in POSIX. - fn sync_data( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Get flags associated with a descriptor. - /// - /// Note: This returns similar flags to `fcntl(fd, F_GETFL)` in POSIX. - /// - /// Note: This returns the value that was the `fs_flags` value returned - /// from `fdstat_get` in earlier versions of WASI. - fn get_flags( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the dynamic type of a descriptor. - /// - /// Note: This returns the same value as the `type` field of the `fd-stat` - /// returned by `stat`, `stat-at` and similar. - /// - /// Note: This returns similar flags to the `st_mode & S_IFMT` value provided - /// by `fstat` in POSIX. - /// - /// Note: This returns the value that was the `fs_filetype` value returned - /// from `fdstat_get` in earlier versions of WASI. - fn get_type( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Adjust the size of an open file. If this increases the file\'s size, the - /// extra bytes are filled with zeros. - /// - /// Note: This was called `fd_filestat_set_size` in earlier versions of WASI. - fn set_size( - &mut self, _descriptor: wasmtime::component::Resource, _size: Filesize, - ) -> wasmtime::Result> { - todo!() - } - - /// Adjust the timestamps of an open file or directory. - /// - /// Note: This is similar to `futimens` in POSIX. - /// - /// Note: This was called `fd_filestat_set_times` in earlier versions of WASI. - fn set_times( - &mut self, _descriptor: wasmtime::component::Resource, - _data_access_timestamp: NewTimestamp, _data_modification_timestamp: NewTimestamp, - ) -> wasmtime::Result> { - todo!() - } - - /// Read from a descriptor, without using and updating the descriptor\'s offset. - /// - /// This function returns a list of bytes containing the data that was - /// read, along with a bool which, when true, indicates that the end of the - /// file was reached. The returned list will contain up to `length` bytes; it - /// may return fewer than requested, if the end of the file is reached or - /// if the I/O operation is interrupted. - /// - /// In the future, this may change to return a `stream`. - /// - /// Note: This is similar to `pread` in POSIX. - fn read( - &mut self, _descriptor: wasmtime::component::Resource, _length: Filesize, - _offset: Filesize, - ) -> wasmtime::Result, bool), ErrorCode>> { - todo!() - } - - /// Write to a descriptor, without using and updating the descriptor\'s offset. - /// - /// It is valid to write past the end of a file; the file is extended to the - /// extent of the write, with bytes between the previous end and the start of - /// the write set to zero. - /// - /// In the future, this may change to take a `stream`. - /// - /// Note: This is similar to `pwrite` in POSIX. - fn write( - &mut self, _descriptor: wasmtime::component::Resource, _buffer: Vec, - _offset: Filesize, - ) -> wasmtime::Result> { - todo!() - } - - /// Read directory entries from a directory. - /// - /// On filesystems where directories contain entries referring to themselves - /// and their parents, often named `.` and `..` respectively, these entries - /// are omitted. - /// - /// This always returns a new stream which starts at the beginning of the - /// directory. Multiple streams may be active on the same directory, and they - /// do not interfere with each other. - fn read_directory( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result, ErrorCode>> - { - todo!() - } - - /// Synchronize the data and metadata of a file to disk. - /// - /// This function succeeds with no effect if the file descriptor is not - /// opened for writing. - /// - /// Note: This is similar to `fsync` in POSIX. - fn sync( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Create a directory. - /// - /// Note: This is similar to `mkdirat` in POSIX. - fn create_directory_at( - &mut self, _descriptor: wasmtime::component::Resource, _path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Return the attributes of an open file or directory. - /// - /// Note: This is similar to `fstat` in POSIX, except that it does not return - /// device and inode information. For testing whether two descriptors refer to - /// the same underlying filesystem object, use `is-same-object`. To obtain - /// additional data that can be used do determine whether a file has been - /// modified, use `metadata-hash`. - /// - /// Note: This was called `fd_filestat_get` in earlier versions of WASI. - fn stat( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Return the attributes of a file or directory. - /// - /// Note: This is similar to `fstatat` in POSIX, except that it does not - /// return device and inode information. See the `stat` description for a - /// discussion of alternatives. - /// - /// Note: This was called `path_filestat_get` in earlier versions of WASI. - fn stat_at( - &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, - _path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Adjust the timestamps of a file or directory. - /// - /// Note: This is similar to `utimensat` in POSIX. - /// - /// Note: This was called `path_filestat_set_times` in earlier versions of - /// WASI. - fn set_times_at( - &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, - _path: String, _data_access_timestamp: NewTimestamp, - _data_modification_timestamp: NewTimestamp, - ) -> wasmtime::Result> { - todo!() - } - - /// Create a hard link. - /// - /// Note: This is similar to `linkat` in POSIX. - fn link_at( - &mut self, _descriptor: wasmtime::component::Resource, - _old_path_flags: PathFlags, _old_path: String, - _new_descriptor: wasmtime::component::Resource, _new_path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Open a file or directory. - /// - /// The returned descriptor is not guaranteed to be the lowest-numbered - /// descriptor not currently open/ it is randomized to prevent applications - /// from depending on making assumptions about indexes, since this is - /// error-prone in multi-threaded contexts. The returned descriptor is - /// guaranteed to be less than 2**31. - /// - /// If `flags` contains `descriptor-flags::mutate-directory`, and the base - /// descriptor doesn't have `descriptor-flags::mutate-directory` set, - /// `open-at` fails with `error-code::read-only`. - /// - /// If `flags` contains `write` or `mutate-directory`, or `open-flags` - /// contains `truncate` or `create`, and the base descriptor doesn't have - /// `descriptor-flags::mutate-directory` set, `open-at` fails with - /// `error-code::read-only`. - /// - /// Note: This is similar to `openat` in POSIX. - fn open_at( - &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, - _path: String, _open_flags: OpenFlags, _flags: DescriptorFlags, - ) -> wasmtime::Result, ErrorCode>> { - todo!() - } - - /// Read the contents of a symbolic link. - /// - /// If the contents contain an absolute or rooted path in the underlying - /// filesystem, this function fails with `error-code::not-permitted`. - /// - /// Note: This is similar to `readlinkat` in POSIX. - fn readlink_at( - &mut self, _descriptor: wasmtime::component::Resource, _path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Remove a directory. - /// - /// Return `error-code::not-empty` if the directory is not empty. - /// - /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. - fn remove_directory_at( - &mut self, _descriptor: wasmtime::component::Resource, _path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Rename a filesystem object. - /// - /// Note: This is similar to `renameat` in POSIX. - fn rename_at( - &mut self, _old_descriptor: wasmtime::component::Resource, _old_path: String, - _new_descriptor: wasmtime::component::Resource, _new_path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Create a symbolic link (also known as a "symlink"). - /// - /// If `old-path` starts with `/`, the function fails with - /// `error-code::not-permitted`. - /// - /// Note: This is similar to `symlinkat` in POSIX. - fn symlink_at( - &mut self, _old_descriptor: wasmtime::component::Resource, _old_path: String, - _new_path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Unlink a filesystem object that is not a directory. - /// - /// Return `error-code::is-directory` if the path refers to a directory. - /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. - fn unlink_file_at( - &mut self, _descriptor: wasmtime::component::Resource, _path: String, - ) -> wasmtime::Result> { - todo!() - } - - /// Test whether two descriptors refer to the same filesystem object. - /// - /// In POSIX, this corresponds to testing whether the two descriptors have the - /// same device (`st_dev`) and inode (`st_ino` or `d_ino`) numbers. - /// wasi-filesystem does not expose device and inode numbers, so this function - /// may be used instead. - fn is_same_object( - &mut self, _descriptor: wasmtime::component::Resource, - _other: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Return a hash of the metadata associated with a filesystem object referred - /// to by a descriptor. - /// - /// This returns a hash of the last-modification timestamp and file size, and - /// may also include the inode number, device number, birth timestamp, and - /// other metadata fields that may change when the file is modified or - /// replaced. It may also include a secret value chosen by the - /// implementation and not otherwise exposed. - /// - /// Implementations are encouraged to provide the following properties: - /// - /// - If the file is not modified or replaced, the computed hash value should - /// usually not change. - /// - If the object is modified or replaced, the computed hash value should - /// usually change. - /// - The inputs to the hash should not be easily computable from the - /// computed hash. - /// - /// However, none of these is required. - fn metadata_hash( - &mut self, _descriptor: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Return a hash of the metadata associated with a filesystem object referred - /// to by a directory descriptor and a relative path. - /// - /// This performs the same hash computation as `metadata-hash`. - fn metadata_hash_at( - &mut self, _descriptor: wasmtime::component::Resource, _path_flags: PathFlags, - _path: String, - ) -> wasmtime::Result> { - todo!() - } - - fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - todo!() - } -} - -impl filesystem::types::HostDirectoryEntryStream for HermesState { - /// Read a single directory entry from a `directory-entry-stream`. - fn read_directory_entry( - &mut self, _dir: wasmtime::component::Resource, - ) -> wasmtime::Result, ErrorCode>> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl filesystem::types::Host for HermesState { - /// Attempts to extract a filesystem-related `error-code` from the stream - /// `error` provided. - /// - /// Stream operations which return `stream-error::last-operation-failed` - /// have a payload with more information about the operation that failed. - /// This payload can be passed through to this function to see if there\'s - /// filesystem-related information about the error to return. - /// - /// Note that this function is fallible because not all stream-related - /// errors are filesystem-related errors. - fn filesystem_error_code( - &mut self, _err: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } -} - -impl filesystem::preopens::Host for HermesState { - /// Return the set of preopened directories, and their path. - fn get_directories( - &mut self, - ) -> wasmtime::Result, String)>> { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/wasi/http/host.rs b/hermes/bin/src/runtime_extensions/wasi/http/host.rs new file mode 100644 index 000000000..817a025ce --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/http/host.rs @@ -0,0 +1,657 @@ +//! HTTP host implementation for WASM runtime. + +use crate::{ + runtime_extensions::bindings::wasi::{ + http::{ + self, + outgoing_handler::{ + ErrorCode, FutureIncomingResponse, OutgoingRequest, RequestOptions, + }, + types::{ + Duration, FieldKey, FieldValue, Fields, FutureTrailers, HeaderError, Headers, + HostIncomingResponse, HostOutgoingResponse, IncomingBody, IncomingRequest, + IncomingResponse, IoError, Method, OutgoingBody, OutgoingResponse, + ResponseOutparam, Scheme, StatusCode, Trailers, + }, + }, + io::streams::{InputStream, OutputStream}, + }, + state::HermesState, +}; + +impl http::types::HostFutureIncomingResponse for HermesState { + /// Returns the incoming HTTP Response, or an error, once one is ready. + /// + /// The outer `option` represents future readiness. Users can wait on this + /// `option` to become `some` using the `subscribe` method. + /// + /// The outer `result` is used to retrieve the response or error at most + /// once. It will be success on the first call in which the outer option + /// is `some`, and error on subsequent calls. + /// + /// The inner `result` represents that either the incoming HTTP Response + /// status and headers have received successfully, or that an error + /// occurred. Errors may also occur while consuming the response body, + /// but those will be reported by the `incoming-body` and its + /// `output-stream` child. + fn get( + &mut self, _res: wasmtime::component::Resource, + ) -> wasmtime::Result< + Option, ErrorCode>, ()>>, + > { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostFields for HermesState { + /// Construct an empty HTTP Fields. + /// + /// The resulting `fields` is mutable. + fn new(&mut self) -> wasmtime::Result> { + todo!() + } + + /// Construct an HTTP Fields. + /// + /// The resulting `fields` is mutable. + /// + /// The list represents each key-value pair in the Fields. Keys + /// which have multiple values are represented by multiple entries in this + /// list with the same key. + /// + /// The tuple is a pair of the field key, represented as a string, and + /// Value, represented as a list of bytes. In a valid Fields, all keys + /// and values are valid UTF-8 strings. However, values are not always + /// well-formed, so they are represented as a raw list of bytes. + /// + /// An error result will be returned if any header or value was + /// syntactically invalid, or if a header was forbidden. + fn from_list( + &mut self, _entries: Vec<(FieldKey, FieldValue)>, + ) -> wasmtime::Result, HeaderError>> { + todo!() + } + + /// Get all of the values corresponding to a key. If the key is not present + /// in this `fields`, an empty list is returned. However, if the key is + /// present but empty, this is represented by a list with one or more + /// empty field-values present. + fn get( + &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns `true` when the key is present in this `fields`. If the key is + /// syntactically invalid, `false` is returned. + fn has( + &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, + ) -> wasmtime::Result { + todo!() + } + + /// Set all of the values for a key. Clears any existing values for that + /// key, if they have been set. + /// + /// Fails with `header-error.immutable` if the `fields` are immutable. + fn set( + &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, + _value: Vec, + ) -> wasmtime::Result> { + todo!() + } + + /// Delete all values for a key. Does nothing if no values for the key + /// exist. + /// + /// Fails with `header-error.immutable` if the `fields` are immutable. + fn delete( + &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, + ) -> wasmtime::Result> { + todo!() + } + + /// Append a value for a key. Does not change or delete any existing + /// values for that key. + /// + /// Fails with `header-error.immutable` if the `fields` are immutable. + fn append( + &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, + _value: FieldValue, + ) -> wasmtime::Result> { + todo!() + } + + /// Retrieve the full set of keys and values in the Fields. Like the + /// constructor, the list represents each key-value pair. + /// + /// The outer list represents each key-value pair in the Fields. Keys + /// which have multiple values are represented by multiple entries in this + /// list with the same key. + fn entries( + &mut self, _fields: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Make a deep copy of the Fields. Equivalent in behavior to calling the + /// `fields` constructor on the return value of `entries`. The resulting + /// `fields` is mutable. + fn clone( + &mut self, _fields: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostFutureTrailers for HermesState { + /// Returns the contents of the trailers, or an error which occurred, + /// once the future is ready. + /// + /// The outer `option` represents future readiness. Users can wait on this + /// `option` to become `some` using the `subscribe` method. + /// + /// The outer `result` is used to retrieve the trailers or error at most + /// once. It will be success on the first call in which the outer option + /// is `some`, and error on subsequent calls. + /// + /// The inner `result` represents that either the HTTP Request or Response + /// body, as well as any trailers, were received successfully, or that an + /// error occurred receiving them. The optional `trailers` indicates whether + /// or not trailers were present in the body. + /// + /// When some `trailers` are returned by this method, the `trailers` + /// resource is immutable, and a child. Use of the `set`, `append`, or + /// `delete` methods will return an error, and the resource must be + /// dropped before the parent `future-trailers` is dropped. + fn get( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result< + Option>, ErrorCode>, ()>>, + > { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostOutgoingBody for HermesState { + /// Returns a stream for writing the body contents. + /// + /// The returned `output-stream` is a child resource: it must be dropped + /// before the parent `outgoing-body` resource is dropped (or finished), + /// otherwise the `outgoing-body` drop or `finish` will trap. + /// + /// Returns success on the first call: the `output-stream` resource for + /// this `outgoing-body` may be retrieved at most once. Subsequent calls + /// will return error. + fn write( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + /// Finalize an outgoing body, optionally providing trailers. This must be + /// called to signal that the response is complete. If the `outgoing-body` + /// is dropped without calling `outgoing-body.finalize`, the implementation + /// should treat the body as corrupted. + /// + /// Fails if the body\'s `outgoing-request` or `outgoing-response` was + /// constructed with a Content-Length header, and the contents written + /// to the body (via `write`) does not match the value given in the + /// Content-Length. + fn finish( + &mut self, _this: wasmtime::component::Resource, + _trailers: Option>, + ) -> wasmtime::Result> { + todo!() + } + + fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { + todo!() + } +} + +impl HostOutgoingResponse for HermesState { + /// Construct an `outgoing-response`, with a default `status-code` of `200`. + /// If a different `status-code` is needed, it must be set via the + /// `set-status-code` method. + /// + /// * `headers` is the HTTP Headers for the Response. + fn new( + &mut self, _headers: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the HTTP Status Code for the Response. + fn status_code( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Set the HTTP Status Code for the Response. Fails if the status-code + /// given is not a valid http status code. + fn set_status_code( + &mut self, _rep: wasmtime::component::Resource, _status_code: StatusCode, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the headers associated with the Request. + /// + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. + /// + /// This headers resource is a child: it must be dropped before the parent + /// `outgoing-request` is dropped, or its ownership is transferred to + /// another component by e.g. `outgoing-handler.handle`. + fn headers( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns the resource corresponding to the outgoing Body for this Response. + /// + /// Returns success on the first call: the `outgoing-body` resource for + /// this `outgoing-response` can be retrieved at most once. Subsequent + /// calls will return error. + fn body( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostIncomingBody for HermesState { + /// Returns the contents of the body, as a stream of bytes. + /// + /// Returns success on first call: the stream representing the contents + /// can be retrieved at most once. Subsequent calls will return error. + /// + /// The returned `input-stream` resource is a child: it must be dropped + /// before the parent `incoming-body` is dropped, or consumed by + /// `incoming-body.finish`. + /// + /// This invariant ensures that the implementation can determine whether + /// the user is consuming the contents of the body, waiting on the + /// `future-trailers` to be ready, or neither. This allows for network + /// backpressure is to be applied when the user is consuming the body, + /// and for that backpressure to not inhibit delivery of the trailers if + /// the user does not read the entire body. + fn stream( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + /// Takes ownership of `incoming-body`, and returns a `future-trailers`. + /// This function will trap if the `input-stream` child is still alive. + fn finish( + &mut self, _this: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { + todo!() + } +} + +impl HostIncomingResponse for HermesState { + /// Returns the status code from the incoming response. + fn status( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Returns the headers from the incoming response. + /// + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. + /// + /// This headers resource is a child: it must be dropped before the parent + /// `incoming-response` is dropped. + fn headers( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns the incoming body. May be called at most once. Returns error + /// if called additional times. + fn consume( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostResponseOutparam for HermesState { + /// Set the value of the `response-outparam` to either send a response, + /// or indicate an error. + /// + /// This method consumes the `response-outparam` to ensure that it is + /// called at most once. If it is never called, the implementation + /// will respond with an error. + /// + /// The user may provide an `error` to `response` to allow the + /// implementation determine how to respond with an HTTP error response. + fn set( + &mut self, _param: wasmtime::component::Resource, + _response: Result, ErrorCode>, + ) -> wasmtime::Result<()> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostRequestOptions for HermesState { + /// Construct a default `request-options` value. + fn new(&mut self) -> wasmtime::Result> { + todo!() + } + + /// The timeout for the initial connect to the HTTP Server. + fn connect_timeout( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the timeout for the initial connect to the HTTP Server. An error + /// return value indicates that this timeout is not supported. + fn set_connect_timeout( + &mut self, _rep: wasmtime::component::Resource, _duration: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// The timeout for receiving the first byte of the Response body. + fn first_byte_timeout( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the timeout for receiving the first byte of the Response body. An + /// error return value indicates that this timeout is not supported. + fn set_first_byte_timeout( + &mut self, _rep: wasmtime::component::Resource, _duration: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// The timeout for receiving subsequent chunks of bytes in the Response + /// body stream. + fn between_bytes_timeout( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the timeout for receiving subsequent chunks of bytes in the Response + /// body stream. An error return value indicates that this timeout is not + /// supported. + fn set_between_bytes_timeout( + &mut self, _rep: wasmtime::component::Resource, _duration: Option, + ) -> wasmtime::Result> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostOutgoingRequest for HermesState { + /// Construct a new `outgoing-request` with a default `method` of `GET`, and + /// `none` values for `path-with-query`, `scheme`, and `authority`. + /// + /// * `headers` is the HTTP Headers for the Request. + /// + /// It is possible to construct, or manipulate with the accessor functions + /// below, an `outgoing-request` with an invalid combination of `scheme` + /// and `authority`, or `headers` which are not permitted to be sent. + /// It is the obligation of the `outgoing-handler.handle` implementation + /// to reject invalid constructions of `outgoing-request`. + fn new( + &mut self, _headers: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns the resource corresponding to the outgoing Body for this + /// Request. + /// + /// Returns success on the first call: the `outgoing-body` resource for + /// this `outgoing-request` can be retrieved at most once. Subsequent + /// calls will return error. + fn body( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + /// Get the Method for the Request. + fn method( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Set the Method for the Request. Fails if the string present in a + /// `method.other` argument is not a syntactically valid method. + fn set_method( + &mut self, _rep: wasmtime::component::Resource, _method: Method, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the combination of the HTTP Path and Query for the Request. + /// When `none`, this represents an empty Path and empty Query. + fn path_with_query( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the combination of the HTTP Path and Query for the Request. + /// When `none`, this represents an empty Path and empty Query. Fails is the + /// string given is not a syntactically valid path and query uri component. + fn set_path_with_query( + &mut self, _rep: wasmtime::component::Resource, + _path_with_query: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the HTTP Related Scheme for the Request. When `none`, the + /// implementation may choose an appropriate default scheme. + fn scheme( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the HTTP Related Scheme for the Request. When `none`, the + /// implementation may choose an appropriate default scheme. Fails if the + /// string given is not a syntactically valid uri scheme. + fn set_scheme( + &mut self, _rep: wasmtime::component::Resource, _scheme: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the HTTP Authority for the Request. A value of `none` may be used + /// with Related Schemes which do not require an Authority. The HTTP and + /// HTTPS schemes always require an authority. + fn authority( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Set the HTTP Authority for the Request. A value of `none` may be used + /// with Related Schemes which do not require an Authority. The HTTP and + /// HTTPS schemes always require an authority. Fails if the string given is + /// not a syntactically valid uri authority. + fn set_authority( + &mut self, _rep: wasmtime::component::Resource, _authority: Option, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the headers associated with the Request. + /// + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. + /// + /// This headers resource is a child: it must be dropped before the parent + /// `outgoing-request` is dropped, or its ownership is transferred to + /// another component by e.g. `outgoing-handler.handle`. + fn headers( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::HostIncomingRequest for HermesState { + /// Returns the method of the incoming request. + fn method( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result { + todo!() + } + + /// Returns the path with query parameters from the request, as a string. + fn path_with_query( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns the protocol scheme from the request. + fn scheme( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Returns the authority from the request, if it was present. + fn authority( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Get the `headers` associated with the request. + /// + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. + /// + /// The `headers` returned are a child resource: it must be dropped before + /// the parent `incoming-request` is dropped. Dropping this + /// `incoming-request` before all children are dropped will trap. + fn headers( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } + + /// Gives the `incoming-body` associated with this request. Will only + /// return success at most once, and subsequent calls will return error. + fn consume( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result, ()>> { + todo!() + } + + fn drop( + &mut self, _rep: wasmtime::component::Resource, + ) -> wasmtime::Result<()> { + todo!() + } +} + +impl http::types::Host for HermesState { + /// Attempts to extract a http-related `error` from the wasi:io `error` + /// provided. + /// + /// Stream operations which return + /// `wasi:io/stream/stream-error::last-operation-failed` have a payload of + /// type `wasi:io/error/error` with more information about the operation + /// that failed. This payload can be passed through to this function to see + /// if there\'s http-related information about the error to return. + /// + /// Note that this function is fallible because not all io-errors are + /// http-related errors. + fn http_error_code( + &mut self, _err: wasmtime::component::Resource, + ) -> wasmtime::Result> { + todo!() + } +} + +impl http::outgoing_handler::Host for HermesState { + /// This function is invoked with an outgoing HTTP Request, and it returns + /// a resource `future-incoming-response` which represents an HTTP Response + /// which may arrive in the future. + /// + /// The `options` argument accepts optional parameters for the HTTP + /// protocol\'s transport layer. + /// + /// This function may return an error if the `outgoing-request` is invalid + /// or not allowed to be made. Otherwise, protocol errors are reported + /// through the `future-incoming-response`. + fn handle( + &mut self, _request: wasmtime::component::Resource, + _options: Option>, + ) -> wasmtime::Result, ErrorCode>> + { + todo!() + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/http/mod.rs b/hermes/bin/src/runtime_extensions/wasi/http/mod.rs index 68dedc475..a585b9268 100644 --- a/hermes/bin/src/runtime_extensions/wasi/http/mod.rs +++ b/hermes/bin/src/runtime_extensions/wasi/http/mod.rs @@ -1,26 +1,8 @@ -//! Host - WASI - HTTP implementations +//! HTTP runtime extension implementation. -use crate::{ - runtime_extensions::{ - bindings::wasi::{ - http::{ - self, - outgoing_handler::{ - ErrorCode, FutureIncomingResponse, OutgoingRequest, RequestOptions, - }, - types::{ - Duration, FieldKey, FieldValue, Fields, FutureTrailers, HeaderError, Headers, - HostIncomingResponse, HostOutgoingResponse, IncomingBody, IncomingRequest, - IncomingResponse, IoError, Method, OutgoingBody, OutgoingResponse, - ResponseOutparam, Scheme, StatusCode, Trailers, - }, - }, - io::streams::{InputStream, OutputStream}, - }, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; /// WASI State pub(crate) struct State {} @@ -30,640 +12,3 @@ impl Stateful for State { Self {} } } - -impl http::types::HostFutureIncomingResponse for HermesState { - /// Returns the incoming HTTP Response, or an error, once one is ready. - /// - /// The outer `option` represents future readiness. Users can wait on this - /// `option` to become `some` using the `subscribe` method. - /// - /// The outer `result` is used to retrieve the response or error at most - /// once. It will be success on the first call in which the outer option - /// is `some`, and error on subsequent calls. - /// - /// The inner `result` represents that either the incoming HTTP Response - /// status and headers have received successfully, or that an error - /// occurred. Errors may also occur while consuming the response body, - /// but those will be reported by the `incoming-body` and its - /// `output-stream` child. - fn get( - &mut self, _res: wasmtime::component::Resource, - ) -> wasmtime::Result< - Option, ErrorCode>, ()>>, - > { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostFields for HermesState { - /// Construct an empty HTTP Fields. - /// - /// The resulting `fields` is mutable. - fn new(&mut self) -> wasmtime::Result> { - todo!() - } - - /// Construct an HTTP Fields. - /// - /// The resulting `fields` is mutable. - /// - /// The list represents each key-value pair in the Fields. Keys - /// which have multiple values are represented by multiple entries in this - /// list with the same key. - /// - /// The tuple is a pair of the field key, represented as a string, and - /// Value, represented as a list of bytes. In a valid Fields, all keys - /// and values are valid UTF-8 strings. However, values are not always - /// well-formed, so they are represented as a raw list of bytes. - /// - /// An error result will be returned if any header or value was - /// syntactically invalid, or if a header was forbidden. - fn from_list( - &mut self, _entries: Vec<(FieldKey, FieldValue)>, - ) -> wasmtime::Result, HeaderError>> { - todo!() - } - - /// Get all of the values corresponding to a key. If the key is not present - /// in this `fields`, an empty list is returned. However, if the key is - /// present but empty, this is represented by a list with one or more - /// empty field-values present. - fn get( - &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns `true` when the key is present in this `fields`. If the key is - /// syntactically invalid, `false` is returned. - fn has( - &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, - ) -> wasmtime::Result { - todo!() - } - - /// Set all of the values for a key. Clears any existing values for that - /// key, if they have been set. - /// - /// Fails with `header-error.immutable` if the `fields` are immutable. - fn set( - &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, - _value: Vec, - ) -> wasmtime::Result> { - todo!() - } - - /// Delete all values for a key. Does nothing if no values for the key - /// exist. - /// - /// Fails with `header-error.immutable` if the `fields` are immutable. - fn delete( - &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, - ) -> wasmtime::Result> { - todo!() - } - - /// Append a value for a key. Does not change or delete any existing - /// values for that key. - /// - /// Fails with `header-error.immutable` if the `fields` are immutable. - fn append( - &mut self, _fields: wasmtime::component::Resource, _name: FieldKey, - _value: FieldValue, - ) -> wasmtime::Result> { - todo!() - } - - /// Retrieve the full set of keys and values in the Fields. Like the - /// constructor, the list represents each key-value pair. - /// - /// The outer list represents each key-value pair in the Fields. Keys - /// which have multiple values are represented by multiple entries in this - /// list with the same key. - fn entries( - &mut self, _fields: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Make a deep copy of the Fields. Equivalent in behavior to calling the - /// `fields` constructor on the return value of `entries`. The resulting - /// `fields` is mutable. - fn clone( - &mut self, _fields: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostFutureTrailers for HermesState { - /// Returns the contents of the trailers, or an error which occurred, - /// once the future is ready. - /// - /// The outer `option` represents future readiness. Users can wait on this - /// `option` to become `some` using the `subscribe` method. - /// - /// The outer `result` is used to retrieve the trailers or error at most - /// once. It will be success on the first call in which the outer option - /// is `some`, and error on subsequent calls. - /// - /// The inner `result` represents that either the HTTP Request or Response - /// body, as well as any trailers, were received successfully, or that an - /// error occurred receiving them. The optional `trailers` indicates whether - /// or not trailers were present in the body. - /// - /// When some `trailers` are returned by this method, the `trailers` - /// resource is immutable, and a child. Use of the `set`, `append`, or - /// `delete` methods will return an error, and the resource must be - /// dropped before the parent `future-trailers` is dropped. - fn get( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result< - Option>, ErrorCode>, ()>>, - > { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostOutgoingBody for HermesState { - /// Returns a stream for writing the body contents. - /// - /// The returned `output-stream` is a child resource: it must be dropped - /// before the parent `outgoing-body` resource is dropped (or finished), - /// otherwise the `outgoing-body` drop or `finish` will trap. - /// - /// Returns success on the first call: the `output-stream` resource for - /// this `outgoing-body` may be retrieved at most once. Subsequent calls - /// will return error. - fn write( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - /// Finalize an outgoing body, optionally providing trailers. This must be - /// called to signal that the response is complete. If the `outgoing-body` - /// is dropped without calling `outgoing-body.finalize`, the implementation - /// should treat the body as corrupted. - /// - /// Fails if the body\'s `outgoing-request` or `outgoing-response` was - /// constructed with a Content-Length header, and the contents written - /// to the body (via `write`) does not match the value given in the - /// Content-Length. - fn finish( - &mut self, _this: wasmtime::component::Resource, - _trailers: Option>, - ) -> wasmtime::Result> { - todo!() - } - - fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - todo!() - } -} - -impl HostOutgoingResponse for HermesState { - /// Construct an `outgoing-response`, with a default `status-code` of `200`. - /// If a different `status-code` is needed, it must be set via the - /// `set-status-code` method. - /// - /// * `headers` is the HTTP Headers for the Response. - fn new( - &mut self, _headers: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the HTTP Status Code for the Response. - fn status_code( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Set the HTTP Status Code for the Response. Fails if the status-code - /// given is not a valid http status code. - fn set_status_code( - &mut self, _rep: wasmtime::component::Resource, _status_code: StatusCode, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the headers associated with the Request. - /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. - /// - /// This headers resource is a child: it must be dropped before the parent - /// `outgoing-request` is dropped, or its ownership is transferred to - /// another component by e.g. `outgoing-handler.handle`. - fn headers( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns the resource corresponding to the outgoing Body for this Response. - /// - /// Returns success on the first call: the `outgoing-body` resource for - /// this `outgoing-response` can be retrieved at most once. Subsequent - /// calls will return error. - fn body( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostIncomingBody for HermesState { - /// Returns the contents of the body, as a stream of bytes. - /// - /// Returns success on first call: the stream representing the contents - /// can be retrieved at most once. Subsequent calls will return error. - /// - /// The returned `input-stream` resource is a child: it must be dropped - /// before the parent `incoming-body` is dropped, or consumed by - /// `incoming-body.finish`. - /// - /// This invariant ensures that the implementation can determine whether - /// the user is consuming the contents of the body, waiting on the - /// `future-trailers` to be ready, or neither. This allows for network - /// backpressure is to be applied when the user is consuming the body, - /// and for that backpressure to not inhibit delivery of the trailers if - /// the user does not read the entire body. - fn stream( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - /// Takes ownership of `incoming-body`, and returns a `future-trailers`. - /// This function will trap if the `input-stream` child is still alive. - fn finish( - &mut self, _this: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - fn drop(&mut self, _rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - todo!() - } -} - -impl HostIncomingResponse for HermesState { - /// Returns the status code from the incoming response. - fn status( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Returns the headers from the incoming response. - /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. - /// - /// This headers resource is a child: it must be dropped before the parent - /// `incoming-response` is dropped. - fn headers( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns the incoming body. May be called at most once. Returns error - /// if called additional times. - fn consume( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostResponseOutparam for HermesState { - /// Set the value of the `response-outparam` to either send a response, - /// or indicate an error. - /// - /// This method consumes the `response-outparam` to ensure that it is - /// called at most once. If it is never called, the implementation - /// will respond with an error. - /// - /// The user may provide an `error` to `response` to allow the - /// implementation determine how to respond with an HTTP error response. - fn set( - &mut self, _param: wasmtime::component::Resource, - _response: Result, ErrorCode>, - ) -> wasmtime::Result<()> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostRequestOptions for HermesState { - /// Construct a default `request-options` value. - fn new(&mut self) -> wasmtime::Result> { - todo!() - } - - /// The timeout for the initial connect to the HTTP Server. - fn connect_timeout( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the timeout for the initial connect to the HTTP Server. An error - /// return value indicates that this timeout is not supported. - fn set_connect_timeout( - &mut self, _rep: wasmtime::component::Resource, _duration: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// The timeout for receiving the first byte of the Response body. - fn first_byte_timeout( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the timeout for receiving the first byte of the Response body. An - /// error return value indicates that this timeout is not supported. - fn set_first_byte_timeout( - &mut self, _rep: wasmtime::component::Resource, _duration: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// The timeout for receiving subsequent chunks of bytes in the Response - /// body stream. - fn between_bytes_timeout( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the timeout for receiving subsequent chunks of bytes in the Response - /// body stream. An error return value indicates that this timeout is not - /// supported. - fn set_between_bytes_timeout( - &mut self, _rep: wasmtime::component::Resource, _duration: Option, - ) -> wasmtime::Result> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostOutgoingRequest for HermesState { - /// Construct a new `outgoing-request` with a default `method` of `GET`, and - /// `none` values for `path-with-query`, `scheme`, and `authority`. - /// - /// * `headers` is the HTTP Headers for the Request. - /// - /// It is possible to construct, or manipulate with the accessor functions - /// below, an `outgoing-request` with an invalid combination of `scheme` - /// and `authority`, or `headers` which are not permitted to be sent. - /// It is the obligation of the `outgoing-handler.handle` implementation - /// to reject invalid constructions of `outgoing-request`. - fn new( - &mut self, _headers: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns the resource corresponding to the outgoing Body for this - /// Request. - /// - /// Returns success on the first call: the `outgoing-body` resource for - /// this `outgoing-request` can be retrieved at most once. Subsequent - /// calls will return error. - fn body( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - /// Get the Method for the Request. - fn method( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Set the Method for the Request. Fails if the string present in a - /// `method.other` argument is not a syntactically valid method. - fn set_method( - &mut self, _rep: wasmtime::component::Resource, _method: Method, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the combination of the HTTP Path and Query for the Request. - /// When `none`, this represents an empty Path and empty Query. - fn path_with_query( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the combination of the HTTP Path and Query for the Request. - /// When `none`, this represents an empty Path and empty Query. Fails is the - /// string given is not a syntactically valid path and query uri component. - fn set_path_with_query( - &mut self, _rep: wasmtime::component::Resource, - _path_with_query: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the HTTP Related Scheme for the Request. When `none`, the - /// implementation may choose an appropriate default scheme. - fn scheme( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the HTTP Related Scheme for the Request. When `none`, the - /// implementation may choose an appropriate default scheme. Fails if the - /// string given is not a syntactically valid uri scheme. - fn set_scheme( - &mut self, _rep: wasmtime::component::Resource, _scheme: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the HTTP Authority for the Request. A value of `none` may be used - /// with Related Schemes which do not require an Authority. The HTTP and - /// HTTPS schemes always require an authority. - fn authority( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Set the HTTP Authority for the Request. A value of `none` may be used - /// with Related Schemes which do not require an Authority. The HTTP and - /// HTTPS schemes always require an authority. Fails if the string given is - /// not a syntactically valid uri authority. - fn set_authority( - &mut self, _rep: wasmtime::component::Resource, _authority: Option, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the headers associated with the Request. - /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. - /// - /// This headers resource is a child: it must be dropped before the parent - /// `outgoing-request` is dropped, or its ownership is transferred to - /// another component by e.g. `outgoing-handler.handle`. - fn headers( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::HostIncomingRequest for HermesState { - /// Returns the method of the incoming request. - fn method( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result { - todo!() - } - - /// Returns the path with query parameters from the request, as a string. - fn path_with_query( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns the protocol scheme from the request. - fn scheme( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Returns the authority from the request, if it was present. - fn authority( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Get the `headers` associated with the request. - /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. - /// - /// The `headers` returned are a child resource: it must be dropped before - /// the parent `incoming-request` is dropped. Dropping this - /// `incoming-request` before all children are dropped will trap. - fn headers( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } - - /// Gives the `incoming-body` associated with this request. Will only - /// return success at most once, and subsequent calls will return error. - fn consume( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result, ()>> { - todo!() - } - - fn drop( - &mut self, _rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - todo!() - } -} - -impl http::types::Host for HermesState { - /// Attempts to extract a http-related `error` from the wasi:io `error` - /// provided. - /// - /// Stream operations which return - /// `wasi:io/stream/stream-error::last-operation-failed` have a payload of - /// type `wasi:io/error/error` with more information about the operation - /// that failed. This payload can be passed through to this function to see - /// if there\'s http-related information about the error to return. - /// - /// Note that this function is fallible because not all io-errors are - /// http-related errors. - fn http_error_code( - &mut self, _err: wasmtime::component::Resource, - ) -> wasmtime::Result> { - todo!() - } -} - -impl http::outgoing_handler::Host for HermesState { - /// This function is invoked with an outgoing HTTP Request, and it returns - /// a resource `future-incoming-response` which represents an HTTP Response - /// which may arrive in the future. - /// - /// The `options` argument accepts optional parameters for the HTTP - /// protocol\'s transport layer. - /// - /// This function may return an error if the `outgoing-request` is invalid - /// or not allowed to be made. Otherwise, protocol errors are reported - /// through the `future-incoming-response`. - fn handle( - &mut self, _request: wasmtime::component::Resource, - _options: Option>, - ) -> wasmtime::Result, ErrorCode>> - { - todo!() - } -} diff --git a/hermes/bin/src/runtime_extensions/wasi/io/error.rs b/hermes/bin/src/runtime_extensions/wasi/io/error/host.rs similarity index 71% rename from hermes/bin/src/runtime_extensions/wasi/io/error.rs rename to hermes/bin/src/runtime_extensions/wasi/io/error/host.rs index 3e4f0bea9..c83020e96 100644 --- a/hermes/bin/src/runtime_extensions/wasi/io/error.rs +++ b/hermes/bin/src/runtime_extensions/wasi/io/error/host.rs @@ -1,22 +1,10 @@ -//! WASI IO Error +//! IO Error host implementation for WASM runtime. use crate::{ - runtime_extensions::{ - bindings::wasi::io::error::{Error, Host, HostError}, - state::{Context, Stateful}, - }, + runtime_extensions::bindings::wasi::io::error::{Error, Host, HostError}, state::HermesState, }; -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} - impl HostError for HermesState { /// Returns a string that is suitable to assist humans in debugging /// this error. diff --git a/hermes/bin/src/runtime_extensions/wasi/io/error/mod.rs b/hermes/bin/src/runtime_extensions/wasi/io/error/mod.rs new file mode 100644 index 000000000..2c8a5f9a6 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/io/error/mod.rs @@ -0,0 +1,14 @@ +//! IO Error runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/io/streams.rs b/hermes/bin/src/runtime_extensions/wasi/io/streams/host.rs similarity index 96% rename from hermes/bin/src/runtime_extensions/wasi/io/streams.rs rename to hermes/bin/src/runtime_extensions/wasi/io/streams/host.rs index 26385b746..945924104 100644 --- a/hermes/bin/src/runtime_extensions/wasi/io/streams.rs +++ b/hermes/bin/src/runtime_extensions/wasi/io/streams/host.rs @@ -1,24 +1,12 @@ -//! WASI IO Streams +//! IO Streams host implementation for WASM runtime. use crate::{ - runtime_extensions::{ - bindings::wasi::io::streams::{ - Host, HostInputStream, HostOutputStream, InputStream, OutputStream, StreamError, - }, - state::{Context, Stateful}, + runtime_extensions::bindings::wasi::io::streams::{ + Host, HostInputStream, HostOutputStream, InputStream, OutputStream, StreamError, }, state::HermesState, }; -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} - impl HostInputStream for HermesState { /// Perform a non-blocking read from the stream. /// diff --git a/hermes/bin/src/runtime_extensions/wasi/io/streams/mod.rs b/hermes/bin/src/runtime_extensions/wasi/io/streams/mod.rs new file mode 100644 index 000000000..68d0b1cb6 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/io/streams/mod.rs @@ -0,0 +1,14 @@ +//! IO Streams runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/random/insecure.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure/host.rs similarity index 71% rename from hermes/bin/src/runtime_extensions/wasi/random/insecure.rs rename to hermes/bin/src/runtime_extensions/wasi/random/insecure/host.rs index d6573e1cd..05efa3e72 100644 --- a/hermes/bin/src/runtime_extensions/wasi/random/insecure.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure/host.rs @@ -1,21 +1,6 @@ -//! Insecure RNG +//! Insecure RNG host implementation for WASM runtime. -use crate::{ - runtime_extensions::{ - bindings::wasi::random::insecure::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; - -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} +use crate::{runtime_extensions::bindings::wasi::random::insecure::Host, state::HermesState}; impl Host for HermesState { /// Return `len` insecure pseudo-random bytes. diff --git a/hermes/bin/src/runtime_extensions/wasi/random/insecure/mod.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure/mod.rs new file mode 100644 index 000000000..5167521d1 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure/mod.rs @@ -0,0 +1,14 @@ +//! Insecure RNG runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/host.rs similarity index 76% rename from hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs rename to hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/host.rs index 22d7d2987..2037d52b7 100644 --- a/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/host.rs @@ -1,21 +1,7 @@ -//! Insecure RNG +//! Insecure RNG seed host implementation for WASM runtime. -use crate::{ - runtime_extensions::{ - bindings::wasi::random::insecure_seed::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; +use crate::{runtime_extensions::bindings::wasi::random::insecure_seed::Host, state::HermesState}; -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} impl Host for HermesState { /// Return a 128-bit value that may contain a pseudo-random value. /// diff --git a/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/mod.rs b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/mod.rs new file mode 100644 index 000000000..c18a06d7c --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/random/insecure_seed/mod.rs @@ -0,0 +1,14 @@ +//! Insecure RNG seed runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} diff --git a/hermes/bin/src/runtime_extensions/wasi/random/secure.rs b/hermes/bin/src/runtime_extensions/wasi/random/secure/host.rs similarity index 79% rename from hermes/bin/src/runtime_extensions/wasi/random/secure.rs rename to hermes/bin/src/runtime_extensions/wasi/random/secure/host.rs index 9334edbc7..93f0f9c1e 100644 --- a/hermes/bin/src/runtime_extensions/wasi/random/secure.rs +++ b/hermes/bin/src/runtime_extensions/wasi/random/secure/host.rs @@ -1,21 +1,6 @@ -//! Random RNG +//! Random RNG host implementation for WASM runtime. -use crate::{ - runtime_extensions::{ - bindings::wasi::random::random::Host, - state::{Context, Stateful}, - }, - state::HermesState, -}; - -/// WASI State -pub(crate) struct State {} - -impl Stateful for State { - fn new(_ctx: &Context) -> Self { - Self {} - } -} +use crate::{runtime_extensions::bindings::wasi::random::random::Host, state::HermesState}; impl Host for HermesState { /// Return `len` cryptographically-secure random or pseudo-random bytes. diff --git a/hermes/bin/src/runtime_extensions/wasi/random/secure/mod.rs b/hermes/bin/src/runtime_extensions/wasi/random/secure/mod.rs new file mode 100644 index 000000000..f98cb3bb0 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/wasi/random/secure/mod.rs @@ -0,0 +1,14 @@ +//! Random RNG runtime extension implementation. + +use crate::runtime_extensions::state::{Context, Stateful}; + +mod host; + +/// WASI State +pub(crate) struct State {} + +impl Stateful for State { + fn new(_ctx: &Context) -> Self { + Self {} + } +} From 24ae67275602dde8878b32db1968cfcd5678b4cc Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 14:32:38 +0200 Subject: [PATCH 06/11] add on cron event impl --- hermes/bin/src/event_queue/event.rs | 2 +- .../runtime_extensions/hermes/cron/event.rs | 29 +++++++++++++++++++ .../src/runtime_extensions/hermes/cron/mod.rs | 1 + hermes/bin/src/wasm/module.rs | 9 ++---- 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 hermes/bin/src/runtime_extensions/hermes/cron/event.rs diff --git a/hermes/bin/src/event_queue/event.rs b/hermes/bin/src/event_queue/event.rs index 554391781..b357ffa14 100644 --- a/hermes/bin/src/event_queue/event.rs +++ b/hermes/bin/src/event_queue/event.rs @@ -16,5 +16,5 @@ pub trait HermesEventPayload: Send { /// # Returns /// /// An `anyhow::Result` indicating the success or failure of the payload execution. - fn execute(&self, executor: &mut ModuleInstance) -> anyhow::Result<()>; + fn execute(&self, module: &mut ModuleInstance) -> anyhow::Result<()>; } diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/event.rs b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs new file mode 100644 index 000000000..64d7228e1 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs @@ -0,0 +1,29 @@ +//! Cron runtime extension event handler implementation. + +use crate::{ + event_queue::event::HermesEventPayload, + runtime_extensions::bindings::hermes::cron::api::CronTagged, +}; + +/// +struct OnCronEvent { + /// + tag: CronTagged, + /// + last: bool, +} + +impl HermesEventPayload for OnCronEvent { + fn event_name(&self) -> &str { + "on-cron" + } + + fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { + module.instance.hermes_cron_event().call_on_cron( + &mut module.store, + &self.tag, + self.last, + )?; + Ok(()) + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs index d40a42c5e..27b6ab0b7 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cron/mod.rs @@ -2,6 +2,7 @@ use crate::runtime_extensions::state::{Context, Stateful}; +mod event; mod host; /// State diff --git a/hermes/bin/src/wasm/module.rs b/hermes/bin/src/wasm/module.rs index ea5713bee..febd36a31 100644 --- a/hermes/bin/src/wasm/module.rs +++ b/hermes/bin/src/wasm/module.rs @@ -29,9 +29,9 @@ struct BadWASMModuleError(String); /// It is used to interact with the WASM module. pub(crate) struct ModuleInstance { /// `wasmtime::Store` entity - pub(crate) _store: WasmStore, + pub(crate) store: WasmStore, /// `Instance` entity - pub(crate) _instance: bindings::Hermes, + pub(crate) instance: bindings::Hermes, } /// Structure defines an abstraction over the WASM module @@ -102,10 +102,7 @@ impl Module { let (instance, _) = bindings::Hermes::instantiate_pre(&mut store, &self.pre_instance) .map_err(|e| BadWASMModuleError(e.to_string()))?; - event.execute(&mut ModuleInstance { - _instance: instance, - _store: store, - })?; + event.execute(&mut ModuleInstance { store, instance })?; Ok(()) } } From 656598863372324cf623b5006c5475ac75bbade3 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 14:47:31 +0200 Subject: [PATCH 07/11] add cardano events --- .../hermes/cardano/event.rs | 87 +++++++++++++++++++ .../runtime_extensions/hermes/cardano/mod.rs | 1 + .../runtime_extensions/hermes/cron/event.rs | 6 +- wasm/wasi/wit/deps/hermes-cardano/api.wit | 6 ++ wasm/wasi/wit/deps/hermes-cardano/event.wit | 14 +-- 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 hermes/bin/src/runtime_extensions/hermes/cardano/event.rs diff --git a/hermes/bin/src/runtime_extensions/hermes/cardano/event.rs b/hermes/bin/src/runtime_extensions/hermes/cardano/event.rs new file mode 100644 index 000000000..af952b5ae --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/cardano/event.rs @@ -0,0 +1,87 @@ +//! Cardano Blockchain runtime extension event handler implementation. + +use crate::{ + event_queue::event::HermesEventPayload, + runtime_extensions::bindings::hermes::cardano::api::{ + BlockSrc, CardanoBlock, CardanoBlockchainId, CardanoTxn, + }, +}; + +/// On Cardano block event +struct OnCardanoBlockEvent { + /// The blockchain id the block originated from. + blockchain: CardanoBlockchainId, + /// This raw CBOR block data. + block: CardanoBlock, + /// Source information about where the block came from, and if we are at tip or not. + source: BlockSrc, +} + +impl HermesEventPayload for OnCardanoBlockEvent { + fn event_name(&self) -> &str { + "on-cardano-block" + } + + fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { + module + .instance + .hermes_cardano_event_on_block() + .call_on_cardano_block(&mut module.store, self.blockchain, &self.block, self.source)?; + Ok(()) + } +} + +/// On Cardano txn event +struct OnCardanoTxnEvent { + /// The blockchain id the block originated from. + blockchain: CardanoBlockchainId, + /// The slot the transaction is in. + slot: u64, + /// The offset in the block this transaction is at. + txn_index: u32, + /// The raw transaction data itself. + txn: CardanoTxn, +} + +impl HermesEventPayload for OnCardanoTxnEvent { + fn event_name(&self) -> &str { + "on-cardano-txn" + } + + fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { + module + .instance + .hermes_cardano_event_on_txn() + .call_on_cardano_txn( + &mut module.store, + self.blockchain, + self.slot, + self.txn_index, + &self.txn, + )?; + + Ok(()) + } +} + +/// On Cardano rollback event +struct OnCardanoRollback { + /// The blockchain id the block originated from. + blockchain: CardanoBlockchainId, + /// The slot the transaction is in. + slot: u64, +} + +impl HermesEventPayload for OnCardanoRollback { + fn event_name(&self) -> &str { + "on-cardano-rollback" + } + + fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { + module + .instance + .hermes_cardano_event_on_rollback() + .call_on_cardano_rollback(&mut module.store, self.blockchain, self.slot)?; + Ok(()) + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs index 543f86be9..d6c134dda 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs @@ -2,6 +2,7 @@ use crate::runtime_extensions::state::{Context, Stateful}; +mod event; mod host; /// State diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/event.rs b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs index 64d7228e1..df6eeedde 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cron/event.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs @@ -5,11 +5,11 @@ use crate::{ runtime_extensions::bindings::hermes::cron::api::CronTagged, }; -/// +/// On cron event struct OnCronEvent { - /// + /// The tagged cron event that was triggered. tag: CronTagged, - /// + /// This cron event will not retrigger. last: bool, } diff --git a/wasm/wasi/wit/deps/hermes-cardano/api.wit b/wasm/wasi/wit/deps/hermes-cardano/api.wit index 77a419a25..e46902891 100644 --- a/wasm/wasi/wit/deps/hermes-cardano/api.wit +++ b/wasm/wasi/wit/deps/hermes-cardano/api.wit @@ -24,6 +24,12 @@ interface api { local-test-blockchain // A local isolated test blockchain. } + flags block-src { + tip, + node, + mithril + } + /// The Slot number to interact with variant slot { genesis, // The very start of the blockchain. diff --git a/wasm/wasi/wit/deps/hermes-cardano/event.wit b/wasm/wasi/wit/deps/hermes-cardano/event.wit index 780e06deb..5cbee8775 100644 --- a/wasm/wasi/wit/deps/hermes-cardano/event.wit +++ b/wasm/wasi/wit/deps/hermes-cardano/event.wit @@ -22,13 +22,7 @@ /// Cardano API Interface - Export ONLY interface event-on-block { - use api.{cardano-blockchain-id, cardano-block}; - - flags block-src { - tip, - node, - mithril - } + use api.{cardano-blockchain-id, cardano-block, block-src}; /// Triggered when a cardano block event fires. /// @@ -43,7 +37,7 @@ interface event-on-block { /// Returns: /// Nothing. /// - on-cardano-block: func(blockchain: cardano-blockchain-id, block: cardano-block, source:block-src); + on-cardano-block: func(blockchain: cardano-blockchain-id, block: cardano-block, source: block-src); } /// Cardano API Interface - Export ONLY @@ -64,7 +58,7 @@ interface event-on-txn { /// Returns: /// Nothing. /// - on-cardano-txn: func(blockchain: cardano-blockchain-id, slot:u64, txn-index: u32, txn: cardano-txn); + on-cardano-txn: func(blockchain: cardano-blockchain-id, slot: u64, txn-index: u32, txn: cardano-txn); } /// Cardano API Interface - Export ONLY @@ -83,7 +77,7 @@ interface event-on-rollback { /// Returns: /// Nothing. /// - on-cardano-rollback: func(blockchain: cardano-blockchain-id, slot:u64); + on-cardano-rollback: func(blockchain: cardano-blockchain-id, slot: u64); } From 8a175205aa76ba25e8c2631b116d06e5be28c148 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 16:14:13 +0200 Subject: [PATCH 08/11] add kv-update event --- .../runtime_extensions/hermes/cron/event.rs | 3 +- .../hermes/kv_store/event.rs | 29 +++++++++++++++++++ .../runtime_extensions/hermes/kv_store/mod.rs | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 hermes/bin/src/runtime_extensions/hermes/kv_store/event.rs diff --git a/hermes/bin/src/runtime_extensions/hermes/cron/event.rs b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs index df6eeedde..20b1b36b7 100644 --- a/hermes/bin/src/runtime_extensions/hermes/cron/event.rs +++ b/hermes/bin/src/runtime_extensions/hermes/cron/event.rs @@ -19,7 +19,8 @@ impl HermesEventPayload for OnCronEvent { } fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { - module.instance.hermes_cron_event().call_on_cron( + // TODO (@stevenj): https://github.com/input-output-hk/hermes/issues/93 + let _res: bool = module.instance.hermes_cron_event().call_on_cron( &mut module.store, &self.tag, self.last, diff --git a/hermes/bin/src/runtime_extensions/hermes/kv_store/event.rs b/hermes/bin/src/runtime_extensions/hermes/kv_store/event.rs new file mode 100644 index 000000000..42394b865 --- /dev/null +++ b/hermes/bin/src/runtime_extensions/hermes/kv_store/event.rs @@ -0,0 +1,29 @@ +//! KV-Store runtime extension event handler implementation. + +use crate::{ + event_queue::event::HermesEventPayload, + runtime_extensions::bindings::hermes::kv_store::api::KvValues, +}; + +/// KV update event +struct KVUpdateEvent { + /// Key. + key: String, + /// Value. + value: KvValues, +} + +impl HermesEventPayload for KVUpdateEvent { + fn event_name(&self) -> &str { + "kv-update" + } + + fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> { + module.instance.hermes_kv_store_event().call_kv_update( + &mut module.store, + &self.key, + &self.value, + )?; + Ok(()) + } +} diff --git a/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs index 66dbea735..21fa3eb22 100644 --- a/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/kv_store/mod.rs @@ -2,6 +2,7 @@ use crate::runtime_extensions::state::{Context, Stateful}; +mod event; mod host; /// State From d73d4b129c2c6168703aeb365d86e51a8ffb1b49 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 16:16:55 +0200 Subject: [PATCH 09/11] remove reactor --- hermes/bin/src/lib.rs | 2 -- hermes/bin/src/reactor.rs | 42 --------------------------------------- 2 files changed, 44 deletions(-) delete mode 100644 hermes/bin/src/reactor.rs diff --git a/hermes/bin/src/lib.rs b/hermes/bin/src/lib.rs index 512867736..78d6a0016 100644 --- a/hermes/bin/src/lib.rs +++ b/hermes/bin/src/lib.rs @@ -2,8 +2,6 @@ //! This file exists, so that doc tests can be used inside binary crates. mod event_queue; -#[allow(missing_docs, clippy::missing_docs_in_private_items, dead_code)] -mod reactor; mod runtime_extensions; mod state; mod wasm; diff --git a/hermes/bin/src/reactor.rs b/hermes/bin/src/reactor.rs deleted file mode 100644 index 00187eb28..000000000 --- a/hermes/bin/src/reactor.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::{ - sync::mpsc::{channel, Receiver, Sender}, - thread, -}; - -use crate::{event_queue::event::HermesEventPayload, wasm::module::Module}; - -pub(crate) struct HermesReactor { - wasm_module: Module, - - event_sender: Sender>, - event_receiver: Receiver>, -} - -impl HermesReactor { - fn event_execution_loop( - mut wasm_module: Module, event_receiver: Receiver>, - ) -> anyhow::Result<()> { - for event in event_receiver { - wasm_module.execute_event(event.as_ref())?; - } - Ok(()) - } - - pub(crate) fn new(app_name: String, module_bytes: &[u8]) -> anyhow::Result { - let wasm_module = Module::new(app_name, module_bytes)?; - let (event_sender, event_receiver) = channel(); - - Ok(Self { - wasm_module, - event_sender, - event_receiver, - }) - } - - pub(crate) fn run(self) -> anyhow::Result<()> { - let _events_thread = thread::spawn(|| { - Self::event_execution_loop(self.wasm_module, self.event_receiver).unwrap(); - }); - Ok(()) - } -} From c467aa318d4c0ed2b0c859678895eb9d11de636d Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 18:35:57 +0200 Subject: [PATCH 10/11] fix --- hermes/bin/src/runtime_extensions/hermes/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/bin/src/runtime_extensions/hermes/mod.rs b/hermes/bin/src/runtime_extensions/hermes/mod.rs index 8111063b9..3e01852ed 100644 --- a/hermes/bin/src/runtime_extensions/hermes/mod.rs +++ b/hermes/bin/src/runtime_extensions/hermes/mod.rs @@ -1,4 +1,4 @@ -//! Hermes runtime extensions implmentations - HERMES custom extensions +//! Hermes runtime extensions implementations - HERMES custom extensions use crate::runtime_extensions::state::{Context, Stateful}; From f5ddf86982a5fa8227f82b0dd6bc036c2aa0742b Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Wed, 14 Feb 2024 22:30:23 +0200 Subject: [PATCH 11/11] add docs --- wasm/wasi/wit/deps/hermes-cardano/api.wit | 1 + 1 file changed, 1 insertion(+) diff --git a/wasm/wasi/wit/deps/hermes-cardano/api.wit b/wasm/wasi/wit/deps/hermes-cardano/api.wit index e46902891..525e53ccf 100644 --- a/wasm/wasi/wit/deps/hermes-cardano/api.wit +++ b/wasm/wasi/wit/deps/hermes-cardano/api.wit @@ -24,6 +24,7 @@ interface api { local-test-blockchain // A local isolated test blockchain. } + /// Source information about where the block came from, and if we are at tip or not. flags block-src { tip, node,