From 00dc8dd46971c1470974ce52af9e4a4141f1bf07 Mon Sep 17 00:00:00 2001 From: Lior Bondarevski Date: Wed, 28 Dec 2022 14:55:35 +0200 Subject: [PATCH 1/8] Fix errors parsing in querier --- packages/std/src/results/contract_result.rs | 4 ++-- packages/std/src/traits.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/std/src/results/contract_result.rs b/packages/std/src/results/contract_result.rs index a54e71f6d6..364725e743 100644 --- a/packages/std/src/results/contract_result.rs +++ b/packages/std/src/results/contract_result.rs @@ -30,11 +30,11 @@ use std::fmt; /// assert_eq!(to_vec(&result).unwrap(), br#"{"error":"Something went wrong"}"#); /// ``` #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -pub enum ContractResult { +pub enum ContractResult { Ok(S), /// An error type that every custom error created by contract developers can be converted to. /// This could potientially have more structure, but String is the easiest. - Err(String), + Err(E), } // Implementations here mimic the Result API and should be implemented via a conversion to Result diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 3a157f16df..2a31c37c6d 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -1,4 +1,4 @@ -use serde::{de::DeserializeOwned, Serialize}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::marker::PhantomData; use std::ops::Deref; @@ -208,8 +208,17 @@ pub trait Api { fn ed25519_sign(&self, message: &[u8], private_key: &[u8]) -> Result, SigningError>; } +#[derive(Debug, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] + +pub enum LegacyQueryResult { + /// Whenever there is no specific error type available + GenericErr { msg: String }, +} + /// A short-hand alias for the two-level query result (1. accessing the contract, 2. executing query in the contract) -pub type QuerierResult = SystemResult>; +pub type QuerierResult = SystemResult>; pub trait Querier { /// raw_query is all that must be implemented for the Querier. @@ -265,7 +274,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { system_err ))), SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err( - format!("Querier contract error: {}", contract_err), + format!("Querier contract error: {:?}", contract_err), )), SystemResult::Ok(ContractResult::Ok(value)) => from_binary(&value), } From 403aaf6072eeaf25d722e1e855460c238fa173fc Mon Sep 17 00:00:00 2001 From: Itzik Grossman Date: Sat, 7 Jan 2023 02:01:26 +0200 Subject: [PATCH 2/8] Add random feature and type to env --- packages/std/Cargo.toml | 3 +++ packages/std/src/types.rs | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index 974794d14d..6f42617234 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -34,6 +34,9 @@ stargate = [] # that require these types. Without this, they get the smaller ibc-v1 API. ibc3 = ["stargate"] +# Add random seed to env in inits and executes +random = [] + [dependencies] base64 = "0.13.0" cosmwasm-derive = { path = "../derive", version = "1.0.0" } diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index 7c88b6cf62..bcfef3326b 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -2,10 +2,11 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::addresses::Addr; +use crate::Binary; use crate::coins::Coin; use crate::timestamp::Timestamp; -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct Env { pub block: BlockInfo, /// Information on the transaction this message was executed in. @@ -15,7 +16,7 @@ pub struct Env { pub contract: ContractInfo, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct TransactionInfo { /// The position of this transaction in the block. The first /// transaction has index 0. @@ -26,7 +27,7 @@ pub struct TransactionInfo { pub index: u32, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct BlockInfo { /// The height of a block is the number of blocks preceding it in the blockchain. pub height: u64, @@ -40,20 +41,24 @@ pub struct BlockInfo { /// Using chrono: /// /// ``` + /// # use secret_cosmwasm_std as cosmwasm_std; /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; /// # let env = Env { /// # block: BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), + /// # random: Binary::default(), /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { /// # address: Addr::unchecked("contract"), + /// # code_hash: "".to_string() /// # }, /// # }; /// # extern crate chrono; /// use chrono::NaiveDateTime; + /// use secret_cosmwasm_std::Binary; /// let seconds = env.block.time.seconds(); /// let nsecs = env.block.time.subsec_nanos(); /// let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32); @@ -62,22 +67,28 @@ pub struct BlockInfo { /// Creating a simple millisecond-precision timestamp (as used in JavaScript): /// /// ``` + /// # use secret_cosmwasm_std as cosmwasm_std; + /// # use secret_cosmwasm_std::Binary; /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; /// # let env = Env { /// # block: BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), + /// # random: Binary::default(), /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { /// # address: Addr::unchecked("contract"), - /// # }, + /// # code_hash: "".to_string() + /// }, /// # }; /// let millis = env.block.time.nanos() / 1_000_000; /// ``` pub time: Timestamp, pub chain_id: String, + #[cfg(feature = "random")] + pub random: Binary, } /// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed @@ -105,7 +116,7 @@ pub struct MessageInfo { pub funds: Vec, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct ContractInfo { pub address: Addr, #[serde(default)] From c4dc3b1d326276917fc6432adfefb8ce78ecc912 Mon Sep 17 00:00:00 2001 From: Cashmaney Date: Thu, 12 Jan 2023 14:30:07 +0200 Subject: [PATCH 3/8] Moved to message_info --- packages/std/src/types.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index bcfef3326b..ab839254e0 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -2,9 +2,9 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::addresses::Addr; -use crate::Binary; use crate::coins::Coin; use crate::timestamp::Timestamp; +use crate::Binary; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct Env { @@ -48,7 +48,6 @@ pub struct BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), - /// # random: Binary::default(), /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { @@ -75,7 +74,6 @@ pub struct BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), - /// # random: Binary::default(), /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { @@ -87,8 +85,6 @@ pub struct BlockInfo { /// ``` pub time: Timestamp, pub chain_id: String, - #[cfg(feature = "random")] - pub random: Binary, } /// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed @@ -114,6 +110,8 @@ pub struct MessageInfo { /// or `MsgExecuteContract`. The transfer is processed in bank before the contract /// is executed such that the new balance is visible during contract execution. pub funds: Vec, + #[cfg(feature = "random")] + pub random: Binary, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] From 99326f7a0a5ff022c2a4ebfa359d69e8c44493f1 Mon Sep 17 00:00:00 2001 From: Itzik Grossman Date: Tue, 14 Mar 2023 22:52:28 +0200 Subject: [PATCH 4/8] Added random as a feature to cw --- packages/std/Cargo.toml | 2 +- packages/std/src/exports.rs | 4 ++++ packages/std/src/types.rs | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index 6f42617234..3f2e43c728 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -9,7 +9,7 @@ license = "Apache-2.0" readme = "README.md" [package.metadata.docs.rs] -features = ["stargate", "staking", "ibc3"] +features = ["stargate", "staking", "ibc3", "random"] [features] default = [] diff --git a/packages/std/src/exports.rs b/packages/std/src/exports.rs index 90f1880da4..2dc2c3f57e 100644 --- a/packages/std/src/exports.rs +++ b/packages/std/src/exports.rs @@ -29,6 +29,10 @@ use crate::serde::{from_slice, to_vec}; use crate::types::Env; use crate::{CustomMsg, Deps, DepsMut, MessageInfo}; +#[cfg(feature = "random")] +#[no_mangle] +extern "C" fn requires_random() -> () {} + #[cfg(feature = "iterator")] #[no_mangle] extern "C" fn requires_iterator() -> () {} diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index ab839254e0..e54d345573 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -111,7 +111,8 @@ pub struct MessageInfo { /// is executed such that the new balance is visible during contract execution. pub funds: Vec, #[cfg(feature = "random")] - pub random: Binary, + #[serde(skip_serializing_if = "Option::is_none")] + pub random: Option, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] From 8ee395ba033c392d7170c971df97f085edaed2d9 Mon Sep 17 00:00:00 2001 From: Itzik Grossman Date: Tue, 21 Mar 2023 21:37:56 +0200 Subject: [PATCH 5/8] Move random to env (to support ibc entry points) --- packages/std/src/types.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index e54d345573..d8ea7cb6df 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -85,6 +85,9 @@ pub struct BlockInfo { /// ``` pub time: Timestamp, pub chain_id: String, + #[cfg(feature = "random")] + #[serde(skip_serializing_if = "Option::is_none")] + pub random: Option, } /// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed @@ -110,9 +113,6 @@ pub struct MessageInfo { /// or `MsgExecuteContract`. The transfer is processed in bank before the contract /// is executed such that the new balance is visible during contract execution. pub funds: Vec, - #[cfg(feature = "random")] - #[serde(skip_serializing_if = "Option::is_none")] - pub random: Option, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] From 45a5118acbc494ab57b95dd0719ba533cdf84cbe Mon Sep 17 00:00:00 2001 From: Itzik Grossman Date: Sat, 1 Apr 2023 15:20:54 +0300 Subject: [PATCH 6/8] Merge with our master. Fix some errors of mock/testing types --- packages/std/src/testing/mock.rs | 1 + packages/std/src/traits.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index feca7fa89a..3a74d51928 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -248,6 +248,7 @@ pub fn mock_env() -> Env { height: 12_345, time: Timestamp::from_nanos(1_571_797_419_879_305_533), chain_id: "cosmos-testnet-14002".to_string(), + random: Some(Binary::from_base64("wLsKdf/sYqvSMI0G0aWRjob25mrIB0VQVjTjDXnDafk=").unwrap()) }, transaction: Some(TransactionInfo { index: 3 }), contract: ContractInfo { diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 7fc02f59b6..089050cbd3 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -236,7 +236,7 @@ pub enum LegacyQueryResult { } /// A short-hand alias for the two-level query result (1. accessing the contract, 2. executing query in the contract) -pub type QuerierResult = SystemResult>; +pub type QuerierResult = SystemResult>; pub trait Querier { /// raw_query is all that must be implemented for the Querier. From 209714201dc1ade3b32952d70c490d8316139317 Mon Sep 17 00:00:00 2001 From: Cashmaney Date: Thu, 13 Apr 2023 12:20:16 +0300 Subject: [PATCH 7/8] compiles properly now --- packages/std/src/testing/mock.rs | 5 ++++- packages/std/src/traits.rs | 17 ++++++----------- packages/std/src/types.rs | 2 ++ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index 3a74d51928..2aa611d2d6 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -248,7 +248,10 @@ pub fn mock_env() -> Env { height: 12_345, time: Timestamp::from_nanos(1_571_797_419_879_305_533), chain_id: "cosmos-testnet-14002".to_string(), - random: Some(Binary::from_base64("wLsKdf/sYqvSMI0G0aWRjob25mrIB0VQVjTjDXnDafk=").unwrap()) + #[cfg(feature = "random")] + random: Some( + Binary::from_base64("wLsKdf/sYqvSMI0G0aWRjob25mrIB0VQVjTjDXnDafk=").unwrap(), + ), }, transaction: Some(TransactionInfo { index: 3 }), contract: ContractInfo { diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 089050cbd3..4af1b54894 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -1,4 +1,8 @@ -use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use serde::{de::DeserializeOwned, Serialize}; + +#[cfg(feature = "random")] +use serde::Deserialize; + use std::marker::PhantomData; use std::ops::Deref; @@ -226,15 +230,6 @@ pub trait Api { fn gas_evaporate(&self, evaporate: u32) -> StdResult<()>; } -#[derive(Debug, Serialize, Deserialize, PartialEq)] -#[serde(rename_all = "snake_case")] -#[non_exhaustive] - -pub enum LegacyQueryResult { - /// Whenever there is no specific error type available - GenericErr { msg: String }, -} - /// A short-hand alias for the two-level query result (1. accessing the contract, 2. executing query in the contract) pub type QuerierResult = SystemResult>; @@ -292,7 +287,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { system_err ))), SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err( - format!("Querier contract error: {:?}", contract_err), + format!("Querier contract error: {}", contract_err), )), SystemResult::Ok(ContractResult::Ok(value)) => from_binary(&value), } diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index adda223a52..4c33bd2fa9 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use crate::addresses::Addr; use crate::coin::Coin; use crate::timestamp::Timestamp; + +#[cfg(feature = "random")] use crate::Binary; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] From a66daeb2c1b6f3c57f739214bffeb6b4113e9447 Mon Sep 17 00:00:00 2001 From: Cashmaney Date: Thu, 13 Apr 2023 13:41:31 +0300 Subject: [PATCH 8/8] Fixed tests --- packages/std/src/types.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index 4c33bd2fa9..936e84d85c 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -43,14 +43,13 @@ pub struct BlockInfo { /// Using chrono: /// /// ``` - /// # use secret_cosmwasm_std as cosmwasm_std; /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; /// # let env = Env { /// # block: BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), - /// # }, + /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { /// # address: Addr::unchecked("contract"), @@ -59,7 +58,7 @@ pub struct BlockInfo { /// # }; /// # extern crate chrono; /// use chrono::NaiveDateTime; - /// use secret_cosmwasm_std::Binary; + /// use cosmwasm_std::Binary; /// let seconds = env.block.time.seconds(); /// let nsecs = env.block.time.subsec_nanos(); /// let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32); @@ -68,8 +67,7 @@ pub struct BlockInfo { /// Creating a simple millisecond-precision timestamp (as used in JavaScript): /// /// ``` - /// # use secret_cosmwasm_std as cosmwasm_std; - /// # use secret_cosmwasm_std::Binary; + /// # use cosmwasm_std::Binary; /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; /// # let env = Env { /// # block: BlockInfo {