Skip to content

Commit

Permalink
module: add incoming message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Mar 11, 2022
1 parent 1315066 commit 3e5a88d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions runtime-sdk/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
use cbor::Encode as _;
use impl_trait_for_tuples::impl_for_tuples;

use oasis_core_runtime::consensus::roothash;

use crate::{
context::{Context, TxContext},
dispatcher, error,
Expand All @@ -22,6 +24,8 @@ use crate::{
},
},
};
use crate::modules::core::Error;
use crate::types::in_msg::IncomingMessageData;

/// Result of invoking the method handler.
pub enum DispatchResult<B, R> {
Expand Down Expand Up @@ -370,6 +374,48 @@ impl TransactionHandler for Tuple {
}
}

pub trait IncomingMessageHandler {
fn prefetch_in_msg(
_prefixes: &mut BTreeSet<Prefix>,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), modules::core::Error> {
Ok(())
}
fn execute_in_msg<C: Context>(
_ctx: &mut C,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), modules::core::Error> {
Ok(())
}
}

#[impl_for_tuples(30)]
impl IncomingMessageHandler for Tuple {
fn prefetch_in_msg(
prefixes: &mut BTreeSet<Prefix>,
in_msg: &roothash::IncomingMessage,
data: &IncomingMessageData,
tx: &Option<Transaction>,
) -> Result<(), Error> {
for_tuples!( #( Tuple::prefetch_in_msg(prefixes, in_msg, data, tx)?; )* );
Ok(())
}

fn execute_in_msg<C: Context>(
ctx: &mut C,
in_msg: &roothash::IncomingMessage,
data: &IncomingMessageData,
tx: &Option<Transaction>,
) -> Result<(), Error> {
for_tuples!( #( Tuple::execute_in_msg(ctx, in_msg, data, tx)?; )* );
Ok(())
}
}

/// Migration handler.
pub trait MigrationHandler {
/// Genesis state type.
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
},
modules, storage,
};
use crate::module::IncomingMessageHandler;

/// A runtime.
pub trait Runtime {
Expand All @@ -43,6 +44,7 @@ pub trait Runtime {
type Modules: TransactionHandler
+ MigrationHandler
+ MethodHandler
+ IncomingMessageHandler
+ BlockHandler
+ InvariantHandler
+ ModuleInfoHandler;
Expand Down

0 comments on commit 3e5a88d

Please sign in to comment.