Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
update: event docs
Browse files Browse the repository at this point in the history
  • Loading branch information
borispovod committed Oct 4, 2021
1 parent 4b9fb56 commit 973b7bf
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pallets/sp-mvm/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file is part of Pontem Network.
// Apache 2.0

//! Implement Move VM kind of events and adopt it for Substrate based events.
//! Implement support of Move VM events inside Substrate.
use core::convert::TryInto;
use move_vm::io::traits::EventHandler;
use sp_std::prelude::*;
Expand All @@ -19,17 +19,23 @@ pub trait DepositMoveEvent {
fn deposit_move_event(e: MoveEventArguments);
}

/// Basic event writer.
pub struct EventWriter<F>(F);

/// Move VM event struct.
pub struct MoveEventArguments {
/// Event GUID.
pub guid: Vec<u8>,
/// Move VM type stored into event.
pub ty_tag: TypeTag,
/// Event message.
pub message: Vec<u8>,
}

impl<T: Config> TryInto<Event<T>> for MoveEventArguments {
type Error = parity_scale_codec::Error;

/// Convert Move VM event into pallet one.
fn try_into(self) -> Result<Event<T>, Self::Error> {
let ty_tag_enc = format!("{}", self.ty_tag).as_bytes().to_vec();
Ok(Event::Event(self.guid, ty_tag_enc, self.message))
Expand All @@ -38,6 +44,7 @@ impl<T: Config> TryInto<Event<T>> for MoveEventArguments {

impl<F: Fn(MoveEventArguments)> EventHandler for EventWriter<F> {
#[inline]
/// Catch new events and pass them to Even Writer function.
fn on_event(&self, guid: Vec<u8>, _seq_num: u64, ty_tag: TypeTag, message: Vec<u8>) {
self.0(MoveEventArguments {
guid,
Expand All @@ -48,6 +55,7 @@ impl<F: Fn(MoveEventArguments)> EventHandler for EventWriter<F> {
}

impl<F> EventWriter<F> {
/// New Event Writer.
pub fn new(f: F) -> Self {
Self(f)
}
Expand All @@ -70,6 +78,7 @@ impl<T: DepositMoveEvent + 'static> GetDepositMoveEventFn<T> for T {}
pub trait CreateMoveEventHandler<T> {
type EventHandler: EventHandler;

/// Create a new event handler for Move VM.
fn create_move_event_handler() -> Self::EventHandler;
}

Expand All @@ -80,6 +89,9 @@ where
{
type EventHandler = DefaultEventHandler;

/// Create a new event handler for Move VM.
///
/// Event Handler will be used inside the VM to convert Move VM events to Substrate one once a new event happens.
fn create_move_event_handler() -> Self::EventHandler {
EventWriter::new(T::get_deposit_move_event_fn())
}
Expand Down

0 comments on commit 973b7bf

Please sign in to comment.