From 097e2514c7ca82ac26e00f5d1db16c61c5bae9ea Mon Sep 17 00:00:00 2001 From: Warren He Date: Fri, 11 Mar 2022 11:22:35 -0800 Subject: [PATCH] module: add incoming message handler --- runtime-sdk/modules/contracts/src/lib.rs | 1 + runtime-sdk/modules/evm/src/lib.rs | 2 + runtime-sdk/src/dispatcher.rs | 10 ++-- runtime-sdk/src/module.rs | 51 +++++++++++++++++++ runtime-sdk/src/modules/accounts/mod.rs | 26 ++++++++++ runtime-sdk/src/modules/consensus/mod.rs | 2 + .../src/modules/consensus_accounts/mod.rs | 11 +++- runtime-sdk/src/modules/core/mod.rs | 2 + runtime-sdk/src/modules/core/test.rs | 1 + runtime-sdk/src/modules/rewards/mod.rs | 2 + runtime-sdk/src/runtime.rs | 5 +- runtime-sdk/src/types/address.rs | 6 +++ .../runtimes/benchmarking/src/runtime/mod.rs | 2 + .../runtimes/simple-keyvalue/src/keyvalue.rs | 1 + 14 files changed, 114 insertions(+), 8 deletions(-) diff --git a/runtime-sdk/modules/contracts/src/lib.rs b/runtime-sdk/modules/contracts/src/lib.rs index f0c858c088..396996d990 100644 --- a/runtime-sdk/modules/contracts/src/lib.rs +++ b/runtime-sdk/modules/contracts/src/lib.rs @@ -715,5 +715,6 @@ 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 {} diff --git a/runtime-sdk/modules/evm/src/lib.rs b/runtime-sdk/modules/evm/src/lib.rs index 2cdb532b82..0fcc3e2491 100644 --- a/runtime-sdk/modules/evm/src/lib.rs +++ b/runtime-sdk/modules/evm/src/lib.rs @@ -683,6 +683,8 @@ impl module::TransactionHandler for Module { } } +impl module::IncomingMessageHandler for Module {} + impl module::BlockHandler for Module { fn end_block(ctx: &mut C) { // Update the list of historic block hashes. diff --git a/runtime-sdk/src/dispatcher.rs b/runtime-sdk/src/dispatcher.rs index 22ae9e645b..93dd357724 100644 --- a/runtime-sdk/src/dispatcher.rs +++ b/runtime-sdk/src/dispatcher.rs @@ -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, @@ -338,10 +338,11 @@ impl Dispatcher { /// transaction if there is one. pub fn execute_in_msg( ctx: &mut C, - _in_msg: &roothash::IncomingMessage, + in_msg: &roothash::IncomingMessage, data: &IncomingMessageData, tx: &Option, ) -> 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 Dispatcher { /// prefixes for the embedded transaction if there is one. pub fn prefetch_in_msg( prefixes: &mut BTreeSet, - _in_msg: &roothash::IncomingMessage, - _data: &IncomingMessageData, + in_msg: &roothash::IncomingMessage, + data: &IncomingMessageData, tx: &Option, ) -> Result<(), RuntimeError> { + R::Modules::prefetch_in_msg(prefixes, in_msg, data, tx)?; if let Some(tx) = tx { Self::prefetch_tx(prefixes, tx.clone())?; } diff --git a/runtime-sdk/src/module.rs b/runtime-sdk/src/module.rs index fe5623ae47..d2d07f9647 100644 --- a/runtime-sdk/src/module.rs +++ b/runtime-sdk/src/module.rs @@ -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, + _in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> 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( + _ctx: &mut C, + _in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> Result<(), error::RuntimeError> { + Ok(()) + } +} + +#[impl_for_tuples(30)] +impl IncomingMessageHandler for Tuple { + fn prefetch_in_msg( + prefixes: &mut BTreeSet, + in_msg: &roothash::IncomingMessage, + data: &IncomingMessageData, + tx: &Option, + ) -> Result<(), error::RuntimeError> { + for_tuples!( #( Tuple::prefetch_in_msg(prefixes, in_msg, data, tx)?; )* ); + Ok(()) + } + + fn execute_in_msg( + ctx: &mut C, + in_msg: &roothash::IncomingMessage, + data: &IncomingMessageData, + tx: &Option, + ) -> Result<(), error::RuntimeError> { + for_tuples!( #( Tuple::execute_in_msg(ctx, in_msg, data, tx)?; )* ); + Ok(()) + } +} + /// Migration handler. pub trait MigrationHandler { /// Genesis state type. diff --git a/runtime-sdk/src/modules/accounts/mod.rs b/runtime-sdk/src/modules/accounts/mod.rs index bda154829b..4c77bd4b48 100644 --- a/runtime-sdk/src/modules/accounts/mod.rs +++ b/runtime-sdk/src/modules/accounts/mod.rs @@ -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( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error>; + /// Move amount from fee accumulator into address. fn move_from_fee_accumulator( ctx: &mut C, @@ -587,6 +593,24 @@ impl API for Module { Ok(()) } + fn mint_into_fee_accumulator( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error> { + if ctx.is_simulation() { + return Ok(()); + } + + ctx.value::(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( ctx: &mut C, to: Address, @@ -858,6 +882,8 @@ impl module::TransactionHandler for Module { } } +impl module::IncomingMessageHandler for Module {} + impl module::BlockHandler for Module { fn end_block(ctx: &mut C) { // Determine the fees that are available for disbursement from the last block. diff --git a/runtime-sdk/src/modules/consensus/mod.rs b/runtime-sdk/src/modules/consensus/mod.rs index eff9b3d0f3..3d57aa88df 100644 --- a/runtime-sdk/src/modules/consensus/mod.rs +++ b/runtime-sdk/src/modules/consensus/mod.rs @@ -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 {} diff --git a/runtime-sdk/src/modules/consensus_accounts/mod.rs b/runtime-sdk/src/modules/consensus_accounts/mod.rs index f16ce0c711..97522598df 100644 --- a/runtime-sdk/src/modules/consensus_accounts/mod.rs +++ b/runtime-sdk/src/modules/consensus_accounts/mod.rs @@ -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::{ @@ -20,9 +21,10 @@ use crate::{ storage::Prefix, types::{ address::Address, + in_msg::IncomingMessageData, message::{MessageEvent, MessageEventHookInvocation}, token, - transaction::AuthInfo, + transaction::{AuthInfo, Transaction}, }, }; @@ -416,6 +418,11 @@ impl { } +impl + module::IncomingMessageHandler for Module +{ +} + impl module::BlockHandler for Module { diff --git a/runtime-sdk/src/modules/core/mod.rs b/runtime-sdk/src/modules/core/mod.rs index 0895771415..28cadacae3 100644 --- a/runtime-sdk/src/modules/core/mod.rs +++ b/runtime-sdk/src/modules/core/mod.rs @@ -645,6 +645,8 @@ impl module::TransactionHandler for Module { } } +impl module::IncomingMessageHandler for Module {} + impl module::MigrationHandler for Module { type Genesis = Genesis; diff --git a/runtime-sdk/src/modules/core/test.rs b/runtime-sdk/src/modules/core/test.rs index 552229729c..abfbd7bb7b 100644 --- a/runtime-sdk/src/modules/core/test.rs +++ b/runtime-sdk/src/modules/core/test.rs @@ -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 { diff --git a/runtime-sdk/src/modules/rewards/mod.rs b/runtime-sdk/src/modules/rewards/mod.rs index 2f6a07e3fd..d1b0dc72bd 100644 --- a/runtime-sdk/src/modules/rewards/mod.rs +++ b/runtime-sdk/src/modules/rewards/mod.rs @@ -138,6 +138,8 @@ impl module::MigrationHandler for Module module::TransactionHandler for Module {} +impl module::IncomingMessageHandler for Module {} + impl module::BlockHandler for Module { fn end_block(ctx: &mut C) { let epoch = ctx.epoch(); diff --git a/runtime-sdk/src/runtime.rs b/runtime-sdk/src/runtime.rs index 2f828c5708..b6e0fce011 100644 --- a/runtime-sdk/src/runtime.rs +++ b/runtime-sdk/src/runtime.rs @@ -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; diff --git a/runtime-sdk/src/types/address.rs b/runtime-sdk/src/types/address.rs index d4ff0a340f..09e6be284b 100644 --- a/runtime-sdk/src/types/address.rs +++ b/runtime-sdk/src/types/address.rs @@ -250,6 +250,12 @@ impl From
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::*; diff --git a/tests/runtimes/benchmarking/src/runtime/mod.rs b/tests/runtimes/benchmarking/src/runtime/mod.rs index a0cc649f07..76fe530e9b 100644 --- a/tests/runtimes/benchmarking/src/runtime/mod.rs +++ b/tests/runtimes/benchmarking/src/runtime/mod.rs @@ -203,6 +203,8 @@ impl module::MigrationHandler for Module module::IncomingMessageHandler for Module {} + impl module::TransactionHandler for Module {} impl module::BlockHandler for Module {} diff --git a/tests/runtimes/simple-keyvalue/src/keyvalue.rs b/tests/runtimes/simple-keyvalue/src/keyvalue.rs index 50a4c8079a..6fea08b5f9 100644 --- a/tests/runtimes/simple-keyvalue/src/keyvalue.rs +++ b/tests/runtimes/simple-keyvalue/src/keyvalue.rs @@ -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 {}