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 17, 2022
1 parent 696037a commit 097e251
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 8 deletions.
1 change: 1 addition & 0 deletions runtime-sdk/modules/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -338,10 +338,11 @@ impl<R: Runtime> Dispatcher<R> {
/// transaction if there is one.
pub fn execute_in_msg<C: BatchContext>(
ctx: &mut C,
_in_msg: &roothash::IncomingMessage,
in_msg: &roothash::IncomingMessage,
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
Expand All @@ -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())?;
}
Expand Down
51 changes: 51 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 @@ -16,6 +18,7 @@ use crate::{
storage,
storage::{Prefix, Store},
types::{
in_msg::IncomingMessageData,
message::MessageResult,
transaction::{
self, AuthInfo, Call, Transaction, TransactionWeight, UnverifiedTransaction,
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ pub trait API {
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error>;

/// Mint new tokens, directly crediting the fee accumulator.
fn mint_into_fee_accumulator<C: Context>(
ctx: &mut C,
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error>;

/// Move amount from fee accumulator into address.
fn move_from_fee_accumulator<C: Context>(
ctx: &mut C,
Expand Down Expand Up @@ -587,6 +593,24 @@ impl API for Module {
Ok(())
}

fn mint_into_fee_accumulator<C: Context>(
ctx: &mut C,
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error> {
if ctx.is_simulation() {
return Ok(());
}

ctx.value::<FeeAccumulator>(CONTEXT_KEY_FEE_ACCUMULATOR)
.or_default()
.add(amount);

// Increase total supply.
Self::inc_total_supply(ctx.runtime_state(), amount)?;

Ok(())
}

fn move_from_fee_accumulator<C: Context>(
ctx: &mut C,
to: Address,
Expand Down Expand Up @@ -858,6 +882,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.
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
11 changes: 9 additions & 2 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
//! while keeping track of amount deposited per account.
use std::{collections::BTreeSet, convert::TryInto};

use num_traits::Zero;
use once_cell::sync::Lazy;
use thiserror::Error;

use oasis_core_runtime::consensus::staking::Account as ConsensusAccount;
use oasis_core_runtime::consensus::{roothash, staking::Account as ConsensusAccount};
use oasis_runtime_sdk_macros::{handler, sdk_derive};

use crate::{
Expand All @@ -20,9 +21,10 @@ use crate::{
storage::Prefix,
types::{
address::Address,
in_msg::IncomingMessageData,
message::{MessageEvent, MessageEventHookInvocation},
token,
transaction::AuthInfo,
transaction::{AuthInfo, Transaction},
},
};

Expand Down Expand Up @@ -416,6 +418,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>
{
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/rewards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions runtime-sdk/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -43,6 +43,7 @@ pub trait Runtime {
type Modules: TransactionHandler
+ MigrationHandler
+ MethodHandler
+ IncomingMessageHandler
+ BlockHandler
+ InvariantHandler
+ ModuleInfoHandler;
Expand Down
6 changes: 6 additions & 0 deletions runtime-sdk/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
2 changes: 2 additions & 0 deletions tests/runtimes/benchmarking/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {}
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-keyvalue/src/keyvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down

0 comments on commit 097e251

Please sign in to comment.