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 events implementation #126

Merged
merged 12 commits into from
Feb 14, 2024
4 changes: 2 additions & 2 deletions hermes/bin/src/event_queue/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,5 +16,5 @@ pub trait HermesEventPayload {
/// # 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<()>;
}
2 changes: 1 addition & 1 deletion hermes/bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! This file exists, so that doc tests can be used inside binary crates.

mod event_queue;
mod runtime;
mod runtime_extensions;
mod state;
mod wasm;
2 changes: 1 addition & 1 deletion hermes/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The Hermes Node.

mod event_queue;
mod runtime;
mod runtime_extensions;
mod state;
mod wasm;

Expand Down
20 changes: 0 additions & 20 deletions hermes/bin/src/runtime/host/hermes/binary/mod.rs

This file was deleted.

20 changes: 0 additions & 20 deletions hermes/bin/src/runtime/host/hermes/cbor/mod.rs

This file was deleted.

20 changes: 0 additions & 20 deletions hermes/bin/src/runtime/host/hermes/json/mod.rs

This file was deleted.

4 changes: 0 additions & 4 deletions hermes/bin/src/runtime/host/mod.rs

This file was deleted.

4 changes: 0 additions & 4 deletions hermes/bin/src/runtime/mod.rs

This file was deleted.

5 changes: 5 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/binary/host.rs
Original file line number Diff line number Diff line change
@@ -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 {}
14 changes: 14 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Binary runtime extension implementation.

use crate::runtime_extensions::state::{Context, Stateful};

mod host;

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}
87 changes: 87 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cardano/event.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
//! Host - Cardano Blockchain implementations
//! Cardano Blockchain host implementation for WASM runtime.

use crate::{
runtime::extensions::{
bindings::hermes::cardano::api::{
CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError,
UnsubscribeOptions,
},
state::{Context, Stateful},
runtime_extensions::bindings::hermes::cardano::api::{
CardanoBlock, CardanoBlockchainId, CardanoTxn, FetchError, Host, Slot, TxnError,
UnsubscribeOptions,
},
state::HermesState,
};

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}

impl Host for HermesState {
/// Subscribe to the Blockchain block data.
///
Expand Down
15 changes: 15 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cardano/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Cardano Blockchain runtime extension implementation.

use crate::runtime_extensions::state::{Context, Stateful};

mod event;
mod host;

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}
5 changes: 5 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cbor/host.rs
Original file line number Diff line number Diff line change
@@ -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 {}
14 changes: 14 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cbor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! CBOR runtime extension implementation.

use crate::runtime_extensions::state::{Context, Stateful};

mod host;

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}
30 changes: 30 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cron/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Cron runtime extension event handler implementation.

use crate::{
event_queue::event::HermesEventPayload,
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,
}

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

fn execute(&self, module: &mut crate::wasm::module::ModuleInstance) -> anyhow::Result<()> {
// 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,
)?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
//! Host - Cron implementations
//! 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::{Context, Stateful},
runtime_extensions::bindings::{
hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host},
wasi::clocks::monotonic_clock::Instant,
},
state::HermesState,
};

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}

impl Host for HermesState {
/// # Schedule Recurrent CRON event
///
Expand Down
15 changes: 15 additions & 0 deletions hermes/bin/src/runtime_extensions/hermes/cron/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Cron runtime extension implementation.

use crate::runtime_extensions::state::{Context, Stateful};

mod event;
mod host;

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
//! Host - Crypto implementations
//! Crypto host implementation for WASM runtime.

use crate::{
runtime::extensions::{
bindings::hermes::{
binary::api::Bstr,
crypto::api::{
Ed25519Bip32, Ed25519Bip32PrivateKey, Ed25519Bip32PublicKey, Ed25519Bip32Signature,
Host, HostEd25519Bip32,
},
runtime_extensions::bindings::hermes::{
binary::api::Bstr,
crypto::api::{
Ed25519Bip32, Ed25519Bip32PrivateKey, Ed25519Bip32PublicKey, Ed25519Bip32Signature,
Host, HostEd25519Bip32,
},
state::{Context, Stateful},
},
state::HermesState,
};

/// State
pub(crate) struct State {}

impl Stateful for State {
fn new(_ctx: &Context) -> Self {
State {}
}
}

impl HostEd25519Bip32 for HermesState {
/// Create a new ED25519-BIP32 Crypto resource
///
Expand Down
Loading
Loading