Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Hermes IPFS WASI WIT definitions #259

Merged
merged 19 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ IFMT
Intellij
ioerr
iohk
ipfs
jetbrains
jsonschema
jorm
Expand Down
29 changes: 29 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/ipfs/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Hermes IPFS runtime extension event handler implementation.
use crate::{
event::HermesEventPayload,
runtime_extensions::bindings::hermes::ipfs::api::{PubsubMessage, PubsubTopic},
};

/// Event handler for the `on-topic` event.
#[allow(dead_code)]
pub(crate) struct OnTopicEvent {
/// Topic
pub(crate) topic: PubsubTopic,
/// Message
pub(crate) message: PubsubMessage,
}

impl HermesEventPayload for OnTopicEvent {
fn event_name(&self) -> &str {
"on-topic"
}

fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> {
let _res: bool = module
.instance
.hermes_ipfs_event()
.call_on_topic(&mut module.store, &self.message)?;
saibatizoku marked this conversation as resolved.
Show resolved Hide resolved
// TODO(@saibatizoku): WIP: add message handling
Ok(())
}
}
40 changes: 40 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/ipfs/host.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! IPFS host implementation for WASM runtime.

use crate::{
runtime_context::HermesRuntimeContext,
runtime_extensions::bindings::hermes::ipfs::api::{
DhtKey, DhtValue, Errno, Host, IpfsContent, IpfsPath, PeerId, PubsubTopic,
},
};

impl Host for HermesRuntimeContext {
fn file_add(&mut self, _contents: IpfsContent) -> wasmtime::Result<Result<IpfsPath, Errno>> {
todo!();
}

fn file_get(&mut self, _path: IpfsPath) -> wasmtime::Result<Result<IpfsContent, Errno>> {
todo!();
}

fn file_pin(&mut self, _ipfs_path: IpfsPath) -> wasmtime::Result<Result<bool, Errno>> {
todo!();
}

fn dht_put(
&mut self, _key: DhtKey, _contents: IpfsContent,
) -> wasmtime::Result<Result<bool, Errno>> {
todo!();
}

fn dht_get(&mut self, _key: DhtKey) -> wasmtime::Result<Result<DhtValue, Errno>> {
todo!();
}

fn pubsub_subscribe(&mut self, _topic: PubsubTopic) -> wasmtime::Result<Result<bool, Errno>> {
todo!();
}

fn peer_evict(&mut self, _peer: PeerId) -> wasmtime::Result<Result<bool, Errno>> {
todo!();
}
}
6 changes: 6 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/ipfs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Hermes IPFS runtime extension.
mod event;
mod host;

/// Advise Runtime Extensions of a new context
pub(crate) fn new_context(_ctx: &crate::runtime_context::HermesRuntimeContext) {}
2 changes: 2 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) mod crypto;
pub(crate) mod hash;
pub(crate) mod init;
pub mod integration_test;
pub(crate) mod ipfs;
pub(crate) mod json;
pub(crate) mod kv_store;
pub(crate) mod localtime;
Expand All @@ -25,6 +26,7 @@ pub(crate) fn new_context(ctx: &HermesRuntimeContext) {
crypto::new_context(ctx);
hash::new_context(ctx);
init::new_context(ctx);
ipfs::new_context(ctx);
json::new_context(ctx);
kv_store::new_context(ctx);
localtime::new_context(ctx);
Expand Down
1 change: 1 addition & 0 deletions wasm/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"inval",
"Iovec",
"iovs",
"ipfs",
"IPPROTO",
"ISDIR",
"isdir",
Expand Down
7 changes: 7 additions & 0 deletions wasm/integration-test/cardano/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hermes::{
api::{BlockSrc, CardanoBlock, CardanoBlockchainId, CardanoTxn, Slot},
},
cron::api::CronTagged,
ipfs::api::PubsubMessage,
kv_store::api::KvValues,
},
wasi::http::types::{IncomingRequest, ResponseOutparam},
Expand Down Expand Up @@ -89,6 +90,12 @@ impl hermes::exports::hermes::init::event::Guest for TestComponent {
}
}

impl hermes::exports::hermes::ipfs::event::Guest for TestComponent {
fn on_topic(message: PubsubMessage) -> bool {
false
}
}

impl hermes::exports::hermes::kv_store::event::Guest for TestComponent {
fn kv_update(_key: String, _value: KvValues) {}
}
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/clocks/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/cron/cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/hashing/hashing.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/localtime/localtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/logger/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
5 changes: 5 additions & 0 deletions wasm/integration-test/smoke-test/smoke-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ bool exports_hermes_init_event_init(void)
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value)
{
Expand Down
7 changes: 7 additions & 0 deletions wasm/integration-test/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hermes::{
hermes::{
cardano::api::{BlockSrc, CardanoBlock, CardanoBlockchainId, CardanoTxn},
cron::api::CronTagged,
ipfs::api::PubsubMessage,
kv_store::api::KvValues,
sqlite,
},
Expand Down Expand Up @@ -79,6 +80,12 @@ impl hermes::exports::hermes::init::event::Guest for TestComponent {
}
}

impl hermes::exports::hermes::ipfs::event::Guest for TestComponent {
fn on_topic(message: PubsubMessage) -> bool {
false
}
}

impl hermes::exports::hermes::kv_store::event::Guest for TestComponent {
fn kv_update(_key: String, _value: KvValues) {}
}
Expand Down
5 changes: 5 additions & 0 deletions wasm/stub-module/stub-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ bool exports_hermes_init_event_init(void) {
return false;
}

// Exported Functions from `hermes:ipfs/event`
bool exports_hermes_ipfs_event_on_topic(exports_hermes_ipfs_event_pubsub_message_t *message) {
return false;
}

// Exported Functions from `hermes:kv-store/event`
void exports_hermes_kv_store_event_kv_update(hermes_string_t *key, exports_hermes_kv_store_event_kv_values_t *value) {

Expand Down
69 changes: 69 additions & 0 deletions wasm/wasi/wit/deps/hermes-ipfs/api.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// Interface to local `IPFS` instance.
interface api {
/// A DHT key.
type dht-key = list<u8>;
/// A DHT value.
type dht-value = list<u8>;
/// The binary contents of an IPFS file.
type ipfs-content = list<u8>;
/// A path to an IPFS file.
type ipfs-path = string;
/// The ID of a peer.
type peer-id = string;
/// A PubSub topic.
type pubsub-topic = string;
/// A PubSub message from a topic subscription.
record pubsub-message {
/// The topic that the message was received on.
topic: pubsub-topic,
/// The contents of the message.
message: string,
/// Peer ID that sent the message.
peer: peer-id,
}

/// Errors that occur in IPFS networking.
enum errno {
/// Unable to get DHT value.
dht-get-error,
/// Unable to put DHT value.
dht-put-error,
/// Unable to publish file to IPFS.
file-add-error,
/// Unable to get file from IPFS.
file-get-error,
/// Unable to pin file.
file-pin-error,
/// Unable to parse a valid IPFS path.
invalid-ipfs-path,
/// Invalid CID.
invalid-cid,
/// Invalid Peer ID.
invalid-peer-id,
/// Unable to evict peer.
peer-eviction-error,
/// Unable to publish to IPFS topic.
pubsub-publish-error,
/// Unable to subscribe to IPFS topic.
pubsub-subscribe-error,
}

/// Uploads a file to IPFS.
file-add: func(contents: ipfs-content) -> result<ipfs-path, errno>;
/// Retrieves a file from IPFS.
file-get: func(path: ipfs-path) -> result<ipfs-content, errno>;
/// Pins a file by path to IPFS.
file-pin: func(path: ipfs-path) -> result<bool, errno>;
/// Puts a DHT key-value into IPFS.
dht-put: func(key: dht-key, contents: ipfs-content) -> result<bool, errno>;
/// Gets a DHT key-value from IPFS.
dht-get: func(key: dht-key) -> result<dht-value, errno>;
/// Subscribes to a PubSub topic.
pubsub-subscribe: func(topic: pubsub-topic) -> result<bool, errno>;
/// Evict peer from network.
peer-evict: func(peer: peer-id) -> result<bool, errno>;
}

world ipfs-api {
export api;
}
19 changes: 19 additions & 0 deletions wasm/wasi/wit/deps/hermes-ipfs/event.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// # IPFS API
///
/// Event triggered on receiving a message on a PubSub topic.
///
/// ## Permissions
///
/// This API is ALWAYS available.

/// IPFS API Interface - Export ONLY
interface event {
use api.{pubsub-message};

/// Triggers when a message is received on a topic.
on-topic: func(message: pubsub-message) -> bool;
}

world ipfs-event {
export event;
}
6 changes: 6 additions & 0 deletions wasm/wasi/wit/deps/hermes-ipfs/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hermes:ipfs;

world all {
import api;
export event;
}
1 change: 1 addition & 0 deletions wasm/wasi/wit/hermes.wit
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ world hermes {
include hermes:crypto/all;
include hermes:hash/all;
include hermes:init/all;
include hermes:ipfs/all;
include hermes:json/all;
include hermes:kv-store/all;
include hermes:localtime/all;
Expand Down
Loading