Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use protobuf de helpers #515

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/cw0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ mod payment;
pub use pagination::{
calc_range_end, calc_range_start, calc_range_start_string, maybe_addr, maybe_canonical,
};
pub use parse_reply::{parse_reply_execute_data, parse_reply_instantiate_data};
pub use parse_reply::{
parse_execute_response_data, parse_instantiate_response_data, parse_reply_execute_data,
parse_reply_instantiate_data,
};
pub use payment::{may_pay, must_pay, nonpayable, one_coin, PaymentError};

pub use crate::balance::NativeBalance;
Expand Down
41 changes: 27 additions & 14 deletions packages/cw0/src/parse_reply.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use cosmwasm_std::{Binary, Reply};
Expand All @@ -7,13 +9,13 @@ const WIRE_TYPE_LENGTH_DELIMITED: u8 = 2;
// Up to 9 bytes of varints as a practical limit (https://github.com/multiformats/unsigned-varint#practical-maximum-of-9-bytes-for-security)
const VARINT_MAX_BYTES: usize = 9;

#[derive(Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we serialize? (As JSON)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be useful.

We need to "binarize" these messages, not serialise them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I prefer no json methods.

We can add method to protobuf serialise in the future. (which could be used in multitest for example).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging now, so I can start linking. feel free to clean up later.

pub struct MsgInstantiateContractResponse {
pub contract_address: String,
pub data: Option<Binary>,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct MsgExecuteContractResponse {
pub data: Option<Binary>,
}
Expand Down Expand Up @@ -112,8 +114,24 @@ pub fn parse_reply_instantiate_data(
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
parse_instantiate_response_data(&data.0)
}

pub fn parse_reply_execute_data(msg: Reply) -> Result<MsgExecuteContractResponse, ParseReplyError> {
let data = msg
.result
.into_result()
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
parse_execute_response_data(&data.0)
}

pub fn parse_instantiate_response_data(
data: &[u8],
) -> Result<MsgInstantiateContractResponse, ParseReplyError> {
// Manual protobuf decoding
let mut data = data.0;
let mut data = data.to_vec();
// Parse contract addr
let contract_addr = parse_protobuf_string(&mut data, 1)?;

Expand All @@ -126,19 +144,14 @@ pub fn parse_reply_instantiate_data(
})
}

pub fn parse_reply_execute_data(msg: Reply) -> Result<MsgExecuteContractResponse, ParseReplyError> {
let data = msg
.result
.into_result()
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
pub fn parse_execute_response_data(
data: &[u8],
) -> Result<MsgExecuteContractResponse, ParseReplyError> {
// Manual protobuf decoding
let mut data = data.0;
// Parse (optional) data
let data = parse_protobuf_bytes(&mut data, 1)?;
let mut data = data.to_vec();
let inner_data = parse_protobuf_bytes(&mut data, 1)?;

Ok(MsgExecuteContractResponse { data })
Ok(MsgExecuteContractResponse { data: inner_data })
}

#[derive(Error, Debug, PartialEq)]
Expand Down
7 changes: 4 additions & 3 deletions packages/multi-test/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::transactions::transactional;
use cosmwasm_std::testing::mock_wasmd_attr;

use anyhow::{anyhow, bail, Result as AnyResult};
use cw0::parse_instantiate_response_data;

// Contract state is kept in Storage, separate from the contracts themselves
const CONTRACTS: Map<&Addr, ContractData> = Map::new("contracts");
Expand Down Expand Up @@ -856,11 +857,11 @@ pub fn parse_contract_addr(data: &Option<Binary>) -> AnyResult<Addr> {
.ok_or_else(|| anyhow!("No data response"))?
.to_vec();
// parse the protobuf struct
let init_data = InstantiateData::decode(bin.as_slice())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is the example I can link to. I guess it works.

if init_data.address.is_empty() {
let init_data = parse_instantiate_response_data(bin.as_slice())?;
if init_data.contract_address.is_empty() {
bail!("no contract address provided");
}
Ok(Addr::unchecked(init_data.address))
Ok(Addr::unchecked(init_data.contract_address))
}

#[cfg(test)]
Expand Down