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

incoming message e2e tests #852

Draft
wants to merge 14 commits into
base: pro-wh/feature/inmsgs
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
module: add incoming message handler
pro-wh committed Mar 30, 2022
commit c3391f78b266903b19b0b10a83c186f73f9d8a1f
1 change: 1 addition & 0 deletions runtime-sdk/modules/contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -715,5 +715,6 @@ impl<Cfg: Config> module::MigrationHandler for Module<Cfg> {
}

impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {}
impl<Cfg: Config> module::IncomingMessageHandler for Module<Cfg> {}
impl<Cfg: Config> module::BlockHandler for Module<Cfg> {}
impl<Cfg: Config> module::InvariantHandler for Module<Cfg> {}
2 changes: 2 additions & 0 deletions runtime-sdk/modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -683,6 +683,8 @@ impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
}
}

impl<Cfg: Config> module::IncomingMessageHandler for Module<Cfg> {}

impl<Cfg: Config> module::BlockHandler for Module<Cfg> {
fn end_block<C: Context>(ctx: &mut C) {
// Update the list of historic block hashes.
8 changes: 5 additions & 3 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ use crate::{
error::{Error as _, RuntimeError},
event::IntoTags,
keymanager::{KeyManagerClient, KeyManagerError},
module::{self, BlockHandler, MethodHandler, TransactionHandler},
module::{self, BlockHandler, IncomingMessageHandler, MethodHandler, TransactionHandler},
modules,
modules::core::API as _,
runtime::Runtime,
@@ -342,6 +342,7 @@ impl<R: Runtime> Dispatcher<R> {
data: &IncomingMessageData,
tx: &Option<Transaction>,
) -> Result<(), RuntimeError> {
R::Modules::execute_in_msg(ctx, in_msg, data, tx)?;
if let Some(tx) = tx {
let tx_size = match data
.ut
@@ -367,10 +368,11 @@ impl<R: Runtime> Dispatcher<R> {
/// prefixes for the embedded transaction if there is one.
pub fn prefetch_in_msg(
prefixes: &mut BTreeSet<Prefix>,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
in_msg: &roothash::IncomingMessage,
data: &IncomingMessageData,
tx: &Option<Transaction>,
) -> Result<(), RuntimeError> {
R::Modules::prefetch_in_msg(prefixes, in_msg, data, tx)?;
if let Some(tx) = tx {
Self::prefetch_tx(prefixes, tx.clone())?;
}
51 changes: 51 additions & 0 deletions runtime-sdk/src/module.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -16,6 +18,7 @@ use crate::{
storage,
storage::{Prefix, Store},
types::{
in_msg::IncomingMessageData,
message::MessageResult,
transaction::{
self, AuthInfo, Call, Transaction, TransactionWeight, UnverifiedTransaction,
@@ -370,6 +373,54 @@ impl TransactionHandler for Tuple {
}
}

/// Roothash incoming message handler.
pub trait IncomingMessageHandler {
/// Add storage prefixes to prefetch, except for the prefixes for the embedded transaction. The
/// dispatcher will invoke the method handler for the embedded transaction separately.
fn prefetch_in_msg(
_prefixes: &mut BTreeSet<Prefix>,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), error::RuntimeError> {
Ok(())
}

/// Execute an incoming message, except for the embedded transaction. The dispatcher will
/// invoke the transaction and method handlers for the embedded transaction separately.
fn execute_in_msg<C: Context>(
_ctx: &mut C,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), error::RuntimeError> {
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::RuntimeError> {
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::RuntimeError> {
for_tuples!( #( Tuple::execute_in_msg(ctx, in_msg, data, tx)?; )* );
Ok(())
}
}

/// Migration handler.
pub trait MigrationHandler {
/// Genesis state type.
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -858,6 +858,8 @@ impl module::TransactionHandler for Module {
}
}

impl module::IncomingMessageHandler for Module {}

impl module::BlockHandler for Module {
fn end_block<C: Context>(ctx: &mut C) {
// Determine the fees that are available for disbursement from the last block.
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/consensus/mod.rs
Original file line number Diff line number Diff line change
@@ -372,6 +372,8 @@ impl module::MigrationHandler for Module {

impl module::TransactionHandler for Module {}

impl module::IncomingMessageHandler for Module {}

impl module::BlockHandler for Module {}

impl module::InvariantHandler for Module {}
5 changes: 5 additions & 0 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -416,6 +416,11 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
{
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
module::IncomingMessageHandler for Module<Accounts, Consensus>
{
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> module::BlockHandler
for Module<Accounts, Consensus>
{
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
@@ -645,6 +645,8 @@ impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
}
}

impl<Cfg: Config> module::IncomingMessageHandler for Module<Cfg> {}

impl<Cfg: Config> module::MigrationHandler for Module<Cfg> {
type Genesis = Genesis;

1 change: 1 addition & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
@@ -211,6 +211,7 @@ impl crate::module::MethodHandler for GasWasterModule {
}
}

impl module::IncomingMessageHandler for GasWasterModule {}
impl module::BlockHandler for GasWasterModule {}
impl module::TransactionHandler for GasWasterModule {}
impl module::MigrationHandler for GasWasterModule {
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/rewards/mod.rs
Original file line number Diff line number Diff line change
@@ -138,6 +138,8 @@ impl<Accounts: modules::accounts::API> module::MigrationHandler for Module<Accou

impl<Accounts: modules::accounts::API> module::TransactionHandler for Module<Accounts> {}

impl<Accounts: modules::accounts::API> module::IncomingMessageHandler for Module<Accounts> {}

impl<Accounts: modules::accounts::API> module::BlockHandler for Module<Accounts> {
fn end_block<C: Context>(ctx: &mut C) {
let epoch = ctx.epoch();
5 changes: 3 additions & 2 deletions runtime-sdk/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ use crate::{
crypto, dispatcher,
keymanager::{KeyManagerClient, TrustedPolicySigners},
module::{
BlockHandler, InvariantHandler, MethodHandler, MigrationHandler, ModuleInfoHandler,
TransactionHandler,
BlockHandler, IncomingMessageHandler, InvariantHandler, MethodHandler, MigrationHandler,
ModuleInfoHandler, TransactionHandler,
},
modules, storage,
};
@@ -43,6 +43,7 @@ pub trait Runtime {
type Modules: TransactionHandler
+ MigrationHandler
+ MethodHandler
+ IncomingMessageHandler
+ BlockHandler
+ InvariantHandler
+ ModuleInfoHandler;
6 changes: 6 additions & 0 deletions runtime-sdk/src/types/address.rs
Original file line number Diff line number Diff line change
@@ -250,6 +250,12 @@ impl From<Address> for ConsensusAddress {
}
}

impl From<&ConsensusAddress> for Address {
fn from(addr: &ConsensusAddress) -> Address {
Address::from_bytes(addr.as_ref()).unwrap()
}
}

#[cfg(test)]
mod test {
use super::*;
2 changes: 2 additions & 0 deletions tests/runtimes/benchmarking/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -203,6 +203,8 @@ impl<Accounts: modules::accounts::API> module::MigrationHandler for Module<Accou
}
}

impl<Accounts: modules::accounts::API> module::IncomingMessageHandler for Module<Accounts> {}

impl<Accounts: modules::accounts::API> module::TransactionHandler for Module<Accounts> {}

impl<Accounts: modules::accounts::API> module::BlockHandler for Module<Accounts> {}
1 change: 1 addition & 0 deletions tests/runtimes/simple-keyvalue/src/keyvalue.rs
Original file line number Diff line number Diff line change
@@ -153,6 +153,7 @@ impl sdk::module::TransactionHandler for Module {
}
}

impl sdk::module::IncomingMessageHandler for Module {}
impl sdk::module::BlockHandler for Module {}
impl sdk::module::InvariantHandler for Module {}