Skip to content

Commit

Permalink
(floating) figuring out errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Mar 11, 2022
1 parent d7092d8 commit b913b1c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
25 changes: 14 additions & 11 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ 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 @@ -165,10 +171,7 @@ impl<R: Runtime> Dispatcher<R> {
}
}

pub fn decode_in_msg<C: Context>(
ctx: &mut C,
in_msg: &roothash::IncomingMessage,
) -> Result<types::in_msg::IncomingMessageData, modules::core::Error> {
pub fn decode_in_msg<C: Context>(in_msg: &roothash::IncomingMessage) -> Result<types::in_msg::IncomingMessageData, modules::core::Error> {
let data: types::in_msg::IncomingMessageData = cbor::from_slice(&in_msg.data)
.map_err(|e| modules::core::Error::MalformedIncomingMessageData(in_msg.id, e.into()))?;
data.validate_basic()?;
Expand Down Expand Up @@ -339,18 +342,18 @@ impl<R: Runtime> Dispatcher<R> {
in_msg: &roothash::IncomingMessage,
data: &IncomingMessageData,
tx: &Option<Transaction>,
) -> Result<(), Error> {
R::Modules::execute_in_msg(ctx, in_msg, data, tx);
) -> Result<(), RuntimeError> {
R::Modules::execute_in_msg(ctx, in_msg, data, tx)?;
if let Some(tx) = tx {
let tx_size = match data.ut.expect(fmt!("incoming message {} has tx but no ut", in_msg.id)).len().try_into() {
let tx_size = match data.ut.expect(&format!("incoming message {} has tx but no ut", in_msg.id)).len().try_into() {
Ok(tx_size) => tx_size,
Err(err) => {
warn!(ctx.get_logger("dispatcher"), "incoming message transaction too large"; "id" => in_msg.id, "err" => ?err);
return Ok(());
}
};
// Use the ID as index.
let index = in_msg.id.into();
let index = in_msg.id.try_into().unwrap();
Self::execute_tx(ctx, tx_size, tx.clone(), index)?;
}
Ok(())
Expand Down Expand Up @@ -537,7 +540,7 @@ impl<R: Runtime + Send + Sync> transaction::dispatcher::Dispatcher for Dispatche
let mut prefixes: BTreeSet<Prefix> = BTreeSet::new();
let mut in_msgs_parsed = Vec::with_capacity(in_msgs.len());
for in_msg in in_msgs {
let data = cbor::from_slice(&in_msg.data).unwrap_or_else(|err| {
let data = Self::decode_in_msg(in_msg).unwrap_or_else(|err| {
warn!(ctx.get_logger("dispatcher"), "incoming message data malformed"; "id" => in_msg.id, "err" => ?err);
IncomingMessageData::noop()
});
Expand Down Expand Up @@ -603,7 +606,7 @@ impl<R: Runtime + Send + Sync> transaction::dispatcher::Dispatcher for Dispatche
|ctx| -> Result<Vec<ExecuteTxResult>, RuntimeError> {
// Execute incoming messages.
for in_msg in in_msgs {
let data = cbor::from_slice(&in_msg.data).unwrap_or_else(|err| {
let data = Self::decode_in_msg(in_msg).unwrap_or_else(|err| {
warn!(ctx.get_logger("dispatcher"), "incoming message data malformed"; "id" => in_msg.id, "err" => ?err);
IncomingMessageData::noop()
});
Expand Down
4 changes: 2 additions & 2 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,11 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
_tx: &Option<Transaction>,
) -> Result<(), CoreError> {
if !in_msg.fee.is_zero() {
Accounts::mint_into_fee_accumulator(ctx, &token::BaseUnits(Consensus::amount_from_consensus(in_msg.fee.try_into()?)?, Consensus::consensus_denomination(ctx)?))?;
Accounts::mint_into_fee_accumulator(ctx, &token::BaseUnits(Consensus::amount_from_consensus(ctx, in_msg.fee.try_into().unwrap()).unwrap(), Consensus::consensus_denomination(ctx).unwrap())).unwrap();
// TODO: Emit event that fee has been paid.
}
if !in_msg.tokens.is_zero() {
Accounts::mint(ctx, in_msg.caller.into(), &token::BaseUnits(Consensus::amount_from_consensus(in_msg.tokens.into())?, Consensus::consensus_denomination(ctx)?))?;
Accounts::mint(ctx, in_msg.caller.into(), &token::BaseUnits(Consensus::amount_from_consensus(ctx, in_msg.tokens.try_into().unwrap()).unwrap(), Consensus::consensus_denomination(ctx).unwrap())).unwrap();
// TODO: Emit event.
}
Ok(())
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 @@ -646,6 +646,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
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

0 comments on commit b913b1c

Please sign in to comment.