Skip to content

Commit

Permalink
cosmrs: domain type updates for wasmd 0.29.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-iqlusion committed Mar 16, 2023
1 parent 3afba82 commit 69684c7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
29 changes: 21 additions & 8 deletions cosmrs/src/cosmwasm/access_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct AccessConfig {
/// Access type granted.
pub permission: AccessType,

/// Account address with the associated permission.
pub address: AccountId,
/// Account addresses with the associated permission.
pub addresses: Vec<AccountId>,
}

impl TryFrom<proto::cosmwasm::wasm::v1::AccessConfig> for AccessConfig {
Expand All @@ -23,12 +23,24 @@ impl TryFrom<&proto::cosmwasm::wasm::v1::AccessConfig> for AccessConfig {
type Error = ErrorReport;

fn try_from(proto: &proto::cosmwasm::wasm::v1::AccessConfig) -> Result<AccessConfig> {
let permission = AccessType::from_i32(proto.permission).ok_or(Error::InvalidEnumValue {
name: "permission",
found_value: proto.permission,
})?;

let mut addresses = Vec::with_capacity(proto.addresses.len());

if !proto.address.is_empty() {
addresses.push(proto.address.parse()?);
}

for address in &proto.addresses {
addresses.push(address.parse()?);
}

Ok(AccessConfig {
permission: AccessType::from_i32(proto.permission).ok_or(Error::InvalidEnumValue {
name: "permission",
found_value: proto.permission,
})?,
address: proto.address.parse()?,
permission,
addresses,
})
}
}
Expand All @@ -43,7 +55,8 @@ impl From<&AccessConfig> for proto::cosmwasm::wasm::v1::AccessConfig {
fn from(config: &AccessConfig) -> proto::cosmwasm::wasm::v1::AccessConfig {
proto::cosmwasm::wasm::v1::AccessConfig {
permission: config.permission as i32,
address: config.address.to_string(),
address: "".to_string(),
addresses: config.addresses.iter().map(ToString::to_string).collect(),
}
}
}
10 changes: 9 additions & 1 deletion cosmrs/src/cosmwasm/code_info_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::ContractCodeId;
use super::{AccessConfig, ContractCodeId};
use crate::{proto, AccountId, ErrorReport, Result};

/// CodeInfoResponse contains code meta data from CodeInfo
Expand All @@ -12,6 +12,9 @@ pub struct CodeInfoResponse {

/// sha256 hash of the code stored
pub data_hash: Vec<u8>,

/// Instantiate permission.
pub instantiate_permission: Option<AccessConfig>,
}

impl TryFrom<proto::cosmwasm::wasm::v1::CodeInfoResponse> for CodeInfoResponse {
Expand All @@ -22,6 +25,10 @@ impl TryFrom<proto::cosmwasm::wasm::v1::CodeInfoResponse> for CodeInfoResponse {
code_id: proto.code_id,
creator: proto.creator.parse()?,
data_hash: proto.data_hash,
instantiate_permission: proto
.instantiate_permission
.map(TryInto::try_into)
.transpose()?,
})
}
}
Expand All @@ -32,6 +39,7 @@ impl From<CodeInfoResponse> for proto::cosmwasm::wasm::v1::CodeInfoResponse {
code_id: code_info.code_id,
creator: code_info.creator.to_string(),
data_hash: code_info.data_hash,
instantiate_permission: code_info.instantiate_permission.map(Into::into),
}
}
}
8 changes: 7 additions & 1 deletion cosmrs/src/cosmwasm/msg_store_code.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::AccessConfig;
use crate::{proto, tx::Msg, AccountId, ErrorReport, Result};
use crate::{proto, tx::Msg, AccountId, Error, ErrorReport, Result};
use tendermint::Hash;

/// MsgStoreCode submit Wasm code to the system
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -49,6 +50,9 @@ impl From<MsgStoreCode> for proto::cosmwasm::wasm::v1::MsgStoreCode {
pub struct MsgStoreCodeResponse {
/// CodeID is the reference to the stored WASM code
pub code_id: u64,

/// Checksum is the sha256 hash of the stored code
pub checksum: Hash,
}

impl Msg for MsgStoreCodeResponse {
Expand All @@ -63,6 +67,7 @@ impl TryFrom<proto::cosmwasm::wasm::v1::MsgStoreCodeResponse> for MsgStoreCodeRe
) -> Result<MsgStoreCodeResponse> {
Ok(MsgStoreCodeResponse {
code_id: proto.code_id,
checksum: Hash::Sha256(proto.checksum.try_into().map_err(|_| Error::Crypto)?),
})
}
}
Expand All @@ -71,6 +76,7 @@ impl From<MsgStoreCodeResponse> for proto::cosmwasm::wasm::v1::MsgStoreCodeRespo
fn from(msg: MsgStoreCodeResponse) -> proto::cosmwasm::wasm::v1::MsgStoreCodeResponse {
proto::cosmwasm::wasm::v1::MsgStoreCodeResponse {
code_id: msg.code_id,
checksum: msg.checksum.as_bytes().into()
}
}
}
24 changes: 11 additions & 13 deletions cosmrs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tendermint::Hash;
use thiserror::Error;

/// Kinds of errors.
#[derive(Clone, Debug, Error, PartialEq)]
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum Error {
/// Invalid account.
#[error("invalid account ID: {id:?}")]
Expand All @@ -33,6 +33,16 @@ pub enum Error {
name: String,
},

/// Invalid value for the given field of an enum.
#[error("invalid proto enum value: {name:?}, value: {found_value:?}")]
InvalidEnumValue {
/// Name of the enum field
name: &'static str,

/// Actual value of the field found
found_value: i32,
},

/// Protobuf is missing a field.
#[error("missing proto field: {name:?}")]
MissingField {
Expand All @@ -56,16 +66,4 @@ pub enum Error {
/// Transaction hash that wasn't found.
hash: Hash,
},

/// Invalid value for the given field of an enum.
#[error("invalid proto enum value: {name:?}, value: {found_value:?}")]
InvalidEnumValue {
/// Name of the enum field
name: &'static str,

/// Actual value of the field found
found_value: i32,
},
}

impl Eq for Error {}

0 comments on commit 69684c7

Please sign in to comment.