Skip to content

Commit

Permalink
introduced basic parsing of 'msg_responses' field inside 'TxMsgData'
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Jun 12, 2024
1 parent ced74a7 commit c41f4e9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
66 changes: 66 additions & 0 deletions cosmrs/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cosmrs/src/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod msg_data;

pub use self::{
gas_info::GasInfo,
msg_data::{MsgData, TxMsgData},
msg_data::{MsgData, TxMsgData, MsgResponse},
};

/// Transaction data.
Expand Down
61 changes: 54 additions & 7 deletions cosmrs/src/abci/msg_data.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use super::Data;
use crate::{
proto::{self, traits::Message},
proto::{self, traits::Message, Any},
tx::Msg,
ErrorReport, Result,
};
use eyre::eyre;
use serde::{Deserialize, Serialize};

/// MsgData defines the data returned in a Result object during message execution.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MsgData {
/// Incoming message type that emitted this result data, for example `"/cosmos.bank.v1beta1.MsgSend"`.
pub msg_type: String,
Expand Down Expand Up @@ -50,13 +51,52 @@ impl From<MsgData> for proto::cosmos::base::abci::v1beta1::MsgData {
}
}

/// The messages responses of the TxMsgData. Corresponds to `Any`
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MsgResponse {
/// Response message type that emitted this result data, for example `"/cosmwasm.wasm.v1.MsgExecuteContractResponse"`.
pub type_url: String,

/// Binary data emitted by this response.
pub value: Vec<u8>,
}

impl MsgResponse {
/// Attempts to decode the `data` field of this result into the specified `Msg` type.
pub fn try_decode_as<M: Msg>(&self) -> Result<M> {
M::Proto::decode(&*self.value)?.try_into()
}
}

impl From<Any> for MsgResponse {
fn from(any: Any) -> Self {
MsgResponse {
type_url: any.type_url,
value: any.value,
}
}
}

impl From<MsgResponse> for Any {
fn from(msg_response: MsgResponse) -> Self {
Any {
type_url: msg_response.type_url,
value: msg_response.value,
}
}
}

/// TxMsgData defines a list of MsgData. A transaction will have a MsgData object for each message.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TxMsgData {
/// Data emitted by the messages in a particular transaction.
// Note: this field will be deprecated and not populated as of cosmos-sdk 0.46.
// It will be superseded by `msg_responses` field of type Vec<Any>
pub data: Vec<MsgData>,

/// This field contains the Msg handler responses packed into Anys.
// Note: this field is an empty vec for chains running cosmos-sdk < 0.46.
pub msg_responses: Vec<MsgResponse>,
}

impl TryFrom<Data> for TxMsgData {
Expand All @@ -76,9 +116,11 @@ impl TryFrom<proto::cosmos::base::abci::v1beta1::TxMsgData> for TxMsgData {

#[allow(deprecated)]
fn try_from(proto: proto::cosmos::base::abci::v1beta1::TxMsgData) -> Result<TxMsgData> {
// TODO(tarcieri): parse `msg_responses`
if !proto.msg_responses.is_empty() {
return Err(eyre!("TxMsgData::msg_responses unsupported"));
// this case should be impossible as with the switch in cosmos-sdk 0.46 only one of those should contain any data
if !proto.msg_responses.is_empty() && !proto.data.is_empty() {
return Err(eyre!(
"TxMsgData: both msg_responses and data fields are populated"
));
}

Ok(TxMsgData {
Expand All @@ -87,6 +129,7 @@ impl TryFrom<proto::cosmos::base::abci::v1beta1::TxMsgData> for TxMsgData {
.into_iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
msg_responses: proto.msg_responses.into_iter().map(Into::into).collect(),
})
}
}
Expand All @@ -96,7 +139,11 @@ impl From<TxMsgData> for proto::cosmos::base::abci::v1beta1::TxMsgData {
fn from(tx_msg_data: TxMsgData) -> Self {
proto::cosmos::base::abci::v1beta1::TxMsgData {
data: tx_msg_data.data.into_iter().map(Into::into).collect(),
msg_responses: vec![], // TODO(tarcieri): serialize responses
msg_responses: tx_msg_data
.msg_responses
.into_iter()
.map(Into::into)
.collect(),
}
}
}

0 comments on commit c41f4e9

Please sign in to comment.