From b913b1ce9140e16457a253e173d647e3b7afc012 Mon Sep 17 00:00:00 2001 From: Warren He Date: Fri, 11 Mar 2022 15:00:41 -0800 Subject: [PATCH] (floating) figuring out errors --- runtime-sdk/src/dispatcher.rs | 25 +++++++++++-------- .../src/modules/consensus_accounts/mod.rs | 4 +-- runtime-sdk/src/modules/core/mod.rs | 2 ++ runtime-sdk/src/types/address.rs | 6 +++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/runtime-sdk/src/dispatcher.rs b/runtime-sdk/src/dispatcher.rs index b13523e7a8..c81ea7c150 100644 --- a/runtime-sdk/src/dispatcher.rs +++ b/runtime-sdk/src/dispatcher.rs @@ -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, @@ -165,10 +171,7 @@ impl Dispatcher { } } - pub fn decode_in_msg( - ctx: &mut C, - in_msg: &roothash::IncomingMessage, - ) -> Result { + pub fn decode_in_msg(in_msg: &roothash::IncomingMessage) -> Result { 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()?; @@ -339,10 +342,10 @@ impl Dispatcher { in_msg: &roothash::IncomingMessage, data: &IncomingMessageData, tx: &Option, - ) -> 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); @@ -350,7 +353,7 @@ impl Dispatcher { } }; // 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(()) @@ -537,7 +540,7 @@ impl transaction::dispatcher::Dispatcher for Dispatche let mut prefixes: BTreeSet = 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() }); @@ -603,7 +606,7 @@ impl transaction::dispatcher::Dispatcher for Dispatche |ctx| -> Result, 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() }); diff --git a/runtime-sdk/src/modules/consensus_accounts/mod.rs b/runtime-sdk/src/modules/consensus_accounts/mod.rs index 4d133dc51b..789de74622 100644 --- a/runtime-sdk/src/modules/consensus_accounts/mod.rs +++ b/runtime-sdk/src/modules/consensus_accounts/mod.rs @@ -443,11 +443,11 @@ impl _tx: &Option, ) -> 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(()) diff --git a/runtime-sdk/src/modules/core/mod.rs b/runtime-sdk/src/modules/core/mod.rs index 1158a1ad16..07382d92cf 100644 --- a/runtime-sdk/src/modules/core/mod.rs +++ b/runtime-sdk/src/modules/core/mod.rs @@ -646,6 +646,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/types/address.rs b/runtime-sdk/src/types/address.rs index d4ff0a340f..349f961e9f 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 for Address { + fn from(addr: ConsensusAddress) -> Address { + Address::from_bytes(addr.as_ref()).unwrap() + } +} + #[cfg(test)] mod test { use super::*;