Skip to content

Add std feature #1971

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

Merged
merged 8 commits into from
Dec 19, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ and this project adheres to
- cosmwasm-vm: Replace `MockApi` with bech32 implementation. ([#1914])
- cosmwasm-std: Make `IbcReceiveResponse::acknowledgement` optional and add
`IbcReceiveResponse::without_ack` constructor. ([#1892])
- cosmwasm-std: Add `std` feature and make it a default feature. ([#1971])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
Expand All @@ -86,6 +87,7 @@ and this project adheres to
[#1944]: https://github.com/CosmWasm/cosmwasm/pull/1944
[#1949]: https://github.com/CosmWasm/cosmwasm/pull/1949
[#1967]: https://github.com/CosmWasm/cosmwasm/pull/1967
[#1971]: https://github.com/CosmWasm/cosmwasm/pull/1971

### Removed

Expand Down
16 changes: 13 additions & 3 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ major releases of `cosmwasm`. Note that you can also view the

If you were using cosmwasm-std's `ibc3` feature, you can remove it, as it is
the default now. Depending on your usage, you might have to enable the
`stargate` feature instead, since it was previously implied by `ibc3`. Also
remove any uses of the `backtraces` feature. You can use a `RUST_BACKTRACE=1`
env variable for this now.
`stargate` feature instead, since it was previously implied by `ibc3`.

Also remove any uses of the `backtraces` feature. You can use a
`RUST_BACKTRACE=1` env variable for this now.

If you were using `cosmwasm-std` with `default-features = false`, you probably
want to enable the `std` feature now, as we might move certain existing
functionality to that feature in the future to support no_std environments:

```diff
-cosmwasm-std = { version = "2.0.0", default-features = false, features = [...] }
+cosmwasm-std = { version = "2.0.0", default-features = false, features = ["std", ...] }
```

- `ContractInfoResponse::new` now takes all fields of the response as
parameters:
Expand Down
6 changes: 4 additions & 2 deletions docs/USING_COSMWASM_STD.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ there it becomes impossible to use the contract in chains with lower CosmWasm
versions. If you add `abort`, it becomes impossible for the contract developer
to opt out of the abort feature due to your library. Since this affects the
default features `abort` and `iterator`, you should always disable default
features.
features. However, you should make sure to keep the `std` feature enabled, as we
might move certain existing functionality to that feature in the future.

Also libraries should define a loose version range that allows the contract
developer to control which cosmwasm-std version they want to use in the final
Expand All @@ -85,5 +86,6 @@ A typical dependency then looks like this:

```toml
# We really need `stargate` here as this is an IBC related library. `abort` and `iterator` are not needed.
cosmwasm-std = { version = "1.0.1", default-features = false, features = ["stargate"] }
# `std` should always stay enabled.
cosmwasm-std = { version = "1.0.1", default-features = false, features = ["std", "stargate"] }
```
11 changes: 8 additions & 3 deletions packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ readme = "README.md"
features = ["abort", "stargate", "staking", "cosmwasm_1_4"]

[features]
default = ["iterator", "abort"]
default = ["iterator", "abort", "std"]
abort = []
# This feature can be used in the future to provide support for no_std environments,
# but disabling it will not provide any benefit at the moment. Even with this feature disabled,
# we currently require an std environment.
# You probably want to keep this enabled for now to avoid possible breaking changes later.
std = ["serde/std", "serde-json-wasm/std"]
# iterator allows us to iterate over all DB items in a given range
# optional as some merkle stores (like tries) don't support this
# given Ethereum 1.0, 2.0, Substrate, and other major projects use Tries
Expand Down Expand Up @@ -45,13 +50,13 @@ cosmwasm_1_4 = ["cosmwasm_1_3"]
[dependencies]
base64 = "0.21.0"
cosmwasm-derive = { path = "../derive", version = "1.5.0" }
derivative = "2"
derivative = { version = "2", features = ["use_core"] }
forward_ref = "1"
hex = "0.4"
schemars = "0.8.3"
sha2 = "0.10.3"
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }
serde-json-wasm = { version = "1.0.0" }
serde-json-wasm = { version = "1.0.0", default-features = false }
thiserror = "1.0.26"
bnum = "0.8.0"
static_assertions = "1.1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use sha2::{
};
use thiserror::Error;

use crate::prelude::*;
use crate::{binary::Binary, forward_ref_partial_eq, HexBinary};

/// A human readable address.
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::errors::{StdError, StdResult};
use crate::prelude::*;

/// Binary is a wrapper around Vec<u8> to add base64 de/serialization
/// with serde. It also adds some helper methods to help encode inline.
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::prelude::*;
use crate::{StdError, StdResult};

/// A SHA-256 checksum of a Wasm blob, used to identify a Wasm code.
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::{fmt, str::FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;
use crate::{errors::CoinFromStrError, math::Uint128};

#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, JsonSchema)]
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::collections::BTreeMap;
use core::fmt;
use core::str::FromStr;

use crate::prelude::*;
use crate::{
errors::CoinsError, Coin, OverflowError, OverflowOperation, StdError, StdResult, Uint128,
};
Expand Down
32 changes: 28 additions & 4 deletions packages/std/src/errors/backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::{Debug, Display, Formatter, Result};

use crate::prelude::*;

/// This wraps an actual backtrace to achieve two things:
/// - being able to fill this with a stub implementation in `no_std` environments
/// - being able to use this in conjunction with [`thiserror::Error`]
Expand All @@ -9,10 +11,17 @@ impl BT {
#[track_caller]
pub fn capture() -> Self {
// in case of no_std, we can fill with a stub here
#[cfg(target_arch = "wasm32")]
return BT(Box::new(std::backtrace::Backtrace::disabled()));
#[cfg(not(target_arch = "wasm32"))]
return BT(Box::new(std::backtrace::Backtrace::capture()));
#[cfg(feature = "std")]
{
#[cfg(target_arch = "wasm32")]
return BT(Box::new(std::backtrace::Backtrace::disabled()));
#[cfg(not(target_arch = "wasm32"))]
return BT(Box::new(std::backtrace::Backtrace::capture()));
}
#[cfg(not(feature = "std"))]
{
BT(Box::new(Stub))
}
}
}

Expand All @@ -31,6 +40,21 @@ impl Display for BT {
}
}

#[allow(unused)]
struct Stub;

impl Debug for Stub {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "<disabled>")
}
}

impl Display for Stub {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "<disabled>")
}
}

/// This macro implements `From` for a given error type to a given error type where
/// the target error has a `backtrace` field.
/// This is meant as a replacement for `thiserror`'s `#[from]` attribute, which does not
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/errors/std_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{impl_from_err, BT};
use thiserror::Error;

use crate::errors::{RecoverPubkeyError, VerificationError};
use crate::prelude::*;

/// Structured error type for init, execute and query.
///
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/errors/system_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;
use crate::Binary;

/// SystemError is used for errors inside the VM and is API friendly (i.e. serializable).
Expand Down Expand Up @@ -39,6 +40,7 @@ pub enum SystemError {
},
}

#[cfg(feature = "std")]
impl std::error::Error for SystemError {}

impl core::fmt::Display for SystemError {
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/hex_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::ops::Deref;
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::prelude::*;
use crate::{Binary, StdError, StdResult};

/// This is a wrapper around Vec<u8> to add hex de/serialization
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::addresses::Addr;
use crate::binary::Binary;
use crate::coin::Coin;
use crate::errors::StdResult;
use crate::prelude::*;
use crate::results::{Attribute, CosmosMsg, Empty, Event, SubMsg};
use crate::serde::to_json_binary;
use crate::timestamp::Timestamp;
Expand Down
3 changes: 3 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mod timestamp;
mod traits;
mod types;

/// This module is to simplify no_std imports
pub(crate) mod prelude;

/// This modules is very advanced and will not be used directly by the vast majority of users.
/// We want to offer it to ensure a stable storage key composition system but don't encourage
/// contract devs to use it directly.
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundUpOverflowError, StdError,
};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Decimal256, SignedDecimal, SignedDecimal256};

use super::Fraction;
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundUpOverflowError, StdError,
};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Decimal, SignedDecimal, SignedDecimal256, Uint512};

use super::Fraction;
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, CheckedMultiplyRatioError, Int256, Int512, Int64, Uint128, Uint256,
Uint512, Uint64,
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/int256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, CheckedMultiplyRatioError, Int128, Int512, Int64, Uint128, Uint256,
Uint512, Uint64,
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/int512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Int128, Int256, Int64, Uint128, Uint256, Uint512, Uint64};

/// Used internally - we don't want to leak this type since we might change
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, CheckedMultiplyRatioError, Int128, Int256, Int512, Uint128, Uint256,
Uint512, Uint64,
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/signed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int256, SignedDecimal256};

use super::Fraction;
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/signed_decimal_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::errors::{
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int512, SignedDecimal};

use super::Fraction;
Expand Down
8 changes: 4 additions & 4 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use core::fmt::{self};
use core::fmt;
use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
ShrAssign, Sub, SubAssign,
};
use core::str::FromStr;
use std::ops::Not;

use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
Expand All @@ -14,6 +13,7 @@ use crate::errors::{
CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, StdError,
};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint256,
Uint64,
Expand Down
6 changes: 3 additions & 3 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use core::fmt;
use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
ShrAssign, Sub, SubAssign,
};
use core::str::FromStr;
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::ops::Not;

use crate::errors::{
CheckedMultiplyFractionError, CheckedMultiplyRatioError, ConversionOverflowError,
DivideByZeroError, OverflowError, OverflowOperation, StdError,
};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint128,
Uint512, Uint64,
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
use crate::errors::{
ConversionOverflowError, DivideByZeroError, OverflowError, OverflowOperation, StdError,
};
use crate::prelude::*;
use crate::{forward_ref_partial_eq, Int128, Int256, Int512, Int64, Uint128, Uint256, Uint64};

/// Used internally - we don't want to leak this type since we might change
Expand Down
8 changes: 4 additions & 4 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use core::fmt::{self};
use core::fmt;
use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
ShrAssign, Sub, SubAssign,
};
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::ops::Not;

use crate::errors::{
CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
OverflowOperation, StdError,
};
use crate::prelude::*;
use crate::{
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint128,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;

/// Replicates the cosmos-sdk bank module Metadata type
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
pub struct DenomMetadata {
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/pagination.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;
use crate::Binary;

/// Simplified version of the PageRequest type for pagination from the cosmos-sdk
Expand Down
6 changes: 6 additions & 0 deletions packages/std/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use alloc::boxed::Box;
pub use alloc::format;
pub use alloc::string::{String, ToString};
pub use alloc::vec;
pub use alloc::vec::Vec;
pub use core::option::Option::{self, None, Some};
1 change: 1 addition & 0 deletions packages/std/src/query/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};

use crate::Coin;

use crate::prelude::*;
#[cfg(feature = "cosmwasm_1_3")]
use crate::PageRequest;
use crate::{Binary, DenomMetadata};
Expand Down
Loading