Skip to content

Make CosmWasm std package no_std capable #1484

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

Open
hussein-aitlahcen opened this issue Nov 13, 2022 · 3 comments
Open

Make CosmWasm std package no_std capable #1484

hussein-aitlahcen opened this issue Nov 13, 2022 · 3 comments

Comments

@hussein-aitlahcen
Copy link
Contributor

hussein-aitlahcen commented Nov 13, 2022

Hey, making the CosmWasm std package no_std capable would allow third parties to use it in more constrained environments. At Composable we run a CosmWasm VM under a Substrate pallet (equivalent of Cosmos module) and this require the dependencies to be no_std (targeting wasm32).

When prototyping the VM, we initially copied parts of the std (as we are only interested in the types for the VM) and we now think that it is the good moment to deprecate this forked std in favor of the official one.

Consequently, having the PR #1483 merged would drastically improve the interface of the alternative VM and also allow us to maintain it by following the CosmWasm upgrades.

@webmaster128
Copy link
Member

Picking up yesterday's thoughts on extracting a minimal interface package shared between cosmwasm-std and cosmwasm-vm (#1483): Creating yet another package comes with significant drawbacks wrt. documentation. A contract developer only should bother about cosmwasm-std. All relevant symbols and their documentation should be in https://docs.rs/cosmwasm-std/latest/cosmwasm_std/. If we extract some parts, it adds more indirection and the contract developer suddenly has to worry about the difference between the packages even if they are exported through cosmwasm_std. I think we should focus on dev UX and rather spend 10 days maintaining the codebase if it save the contract dev 10 minutes navigating. cosmwasm-std is maintained by a handfull of highly skilled devs very familiar with the codebase and committed to it for months. But it is used by hundreds of devs in various levels from absolute beginner/Rust newbie to advanced blockchain dev. So optimizing for cosmwasm-std maintenance convenience feels wrong. Looking at #1483 the diff is not all that scary to me. cosmwasm-std has few dependencies and I think it's worth evaluating their no_std compatibility.

@webmaster128 webmaster128 added this to the 2.0.0 milestone Jan 5, 2023
mina86 added a commit to mina86/cosmwasm that referenced this issue Jul 20, 2023
std::error::Error doesn’t currently have a stable no_std equivalence.
core::error::Error is a thing but it’s unstable and thus requires
nightly compiler.

There’s one hack we can use.  serde has no_std support and exports
std::error::Error as serde::de::StdErr.  This way we hide the need
for nightly compiler to serde.

The two alternatives are:
- require nightly compiler or
- don’t implement std::error::Error in no_std builds.

Issue: CosmWasm#1484
mina86 added a commit to mina86/cosmwasm that referenced this issue Jul 20, 2023
std::error::Error doesn’t currently have a stable no_std equivalence.
core::error::Error is a thing but it’s unstable and thus requires
nightly compiler.

There’s one hack we can use.  serde has no_std support and exports
std::error::Error as serde::de::StdErr.  This way we hide the need
for nightly compiler to serde.

The two alternatives are:
- require nightly compiler or
- don’t implement std::error::Error in no_std builds.

Issue: CosmWasm#1484
mina86 added a commit to mina86/serde-json-wasm that referenced this issue Jul 24, 2023
Eliminate all uses of std crate from non-test code.  For the most part
it is a matter of using core or alloc namespaces instead.

The only exception is std::error::Error trait.  However, it is
re-exported by serde as serde::{de,ser}::StdError so implement that
instead.

Tests use HashMap which requires std crate.  Since test code doesn’t
matter for no_std support, enable std crate when building tests.

no_std support was tested by building for thumbv6m-none-eabi target.

Issue: CosmWasm/cosmwasm#1484
Issue: CosmWasm#40
mina86 added a commit to mina86/forward_ref that referenced this issue Jul 24, 2023
Even though the crate doesn’t use std crate, because it doesn’t
declare `#![no_std]` it cannot be compiled in no_std environments:

    error[E0463]: can't find crate for `std`
      = note: the `thumbv6m-none-eabi` target may not support the standard library
      = note: `std` is required by `forward_ref` because it does not declare `#![no_std]`

Adding the annotation makes it possible to use the crate on no_std
targets.

Issue: CosmWasm/cosmwasm#1484
mina86 added a commit to mina86/serde-json-wasm that referenced this issue Aug 4, 2023
Remove all references to std from the library and introduce std and
unstable features which enable corresponding serde features.

In default build, std is enabled and nothing changes.  However, if std
is disabled, the library becomes no_std.  Downside of lack of std is
that Error enum no longer implements std::error;:Error.  To remedy that,
unstable feature uses core::error::Error but requires a nightly compiler.

For the most part, this commit is mostly just replacing references to
std crate with core and alloc crates.  On exception is std::error::Error
which is replaced by serde’s StdError such that Error enums implement
whatever trait serde uses.  With std or unstable feature enabled it’s
just an alias to {std,core}::error::Error.

Issue: CosmWasm/cosmwasm#1484
Issue: CosmWasm#40
@mina86
Copy link
Contributor

mina86 commented Aug 11, 2023

forward_ref can be copied over if needed

I have a PR for it. The crate pretty much is no_std except that it lacks the necessary annotation. However, the PR is sitting in limbo so I dunno if the project isn’t abandoned.

schemars probably has to become optional

schemars is only used for generating the schema as far as I understand and as such it isn’t cross-compiled to no_std targets. Changing cw_serde macro such that it conditionally derives schemars::JsonSchema should works nicely.

serde
serde-json-wasm

Both have no_std support. serde-json-wasm in the newly released 1.0 version. In both cases no_std support requires nightly compiler (this is due to core::error::Error).

thiserror

May I suggest a bold option: stop supporting std::error::Error with 2.0 release? Since CosmWasm is reliant on serde, one cheeky option is to use serde::ser::StdError instead and for deriving Display we can use derive_more::Display. The only downside here would be no easy way to derive source method.

I’ve sent a PR adding no_std support however dtolnay rejected it on the grounds that it requires nightly compiler. There is thiserror-core fork which does support no_std so that’s also an option. I’ll try to push some preparation PRs to thiserror to make it so that the fork will have smaller diff to upstream.

@webmaster128
Copy link
Member

#1971 will create an std feature that allows to conditionally enable the use of std. This feature is enabled by default but must be enabled manually if default-features = false is used. This makes it breaking and will ship in cosmwasm-std 2.0.

For now a build without std enabled is likely going to fail. Whoever needs no_std support is welcome to improve the situation for such builds. I'll keep this open but remove the full no_std support from the 2.0 milestone.

Thanks everyone

@webmaster128 webmaster128 removed this from the 2.0.0 milestone Dec 18, 2023
aumetra pushed a commit to CosmWasm/serde-json-wasm that referenced this issue Jul 26, 2024
Remove all references to std from the library and introduce std and
unstable features which enable corresponding serde features.

In default build, std is enabled and nothing changes.  However, if std
is disabled, the library becomes no_std.  Downside of lack of std is
that Error enum no longer implements std::error;:Error.  To remedy that,
unstable feature uses core::error::Error but requires a nightly compiler.

For the most part, this commit is mostly just replacing references to
std crate with core and alloc crates.  On exception is std::error::Error
which is replaced by serde’s StdError such that Error enums implement
whatever trait serde uses.  With std or unstable feature enabled it’s
just an alias to {std,core}::error::Error.

Issue: CosmWasm/cosmwasm#1484
Issue: #40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants