diff --git a/Cargo.lock b/Cargo.lock index 07ca333aee..e0ef5aaffd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4535,6 +4535,7 @@ dependencies = [ "base58", "bitvec", "blake2", + "cfg-if", "derivative", "derive_more", "frame-metadata 16.0.0", diff --git a/Cargo.toml b/Cargo.toml index 3a5ec3bfbf..167604b0d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,7 +75,7 @@ impl-serde = { version = "0.4.0" } indoc = "2" jsonrpsee = { version = "0.21" } pretty_assertions = "1.4.0" -primitive-types = { version = "0.12.2", default-features = false, features = ["codec", "scale-info", "serde"] } +primitive-types = { version = "0.12.2", default-features = false, features = ["codec", "scale-info", "serde_no_std"] } proc-macro-error = "1.0.4" proc-macro2 = "1.0.78" quote = "1.0.35" diff --git a/core/Cargo.toml b/core/Cargo.toml index 8dbb4bf5f9..dab5f2bd6e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -15,21 +15,21 @@ description = "Sign extrinsics to be submitted by Subxt" keywords = ["parity", "subxt", "extrinsic", "no-std"] [features] -default = ["std"] +# default = ["std"] std = [] substrate-compat = ["sp-core", "sp-runtime"] [dependencies] codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] } -scale-info = { workspace = true } -scale-value = { workspace = true } -scale-bits = { workspace = true } -scale-decode = { workspace = true } -scale-encode = { workspace = true } +scale-info = { workspace = true, default-features = false, features = ["bitvec"] } +scale-value = { workspace = true, default-features = false } +scale-bits = { workspace = true, default-features = false } +scale-decode = { workspace = true, default-features = false } +scale-encode = { workspace = true, default-features = false } frame-metadata = { workspace = true } subxt-metadata = { workspace = true } -derivative = { workspace = true } +derivative = { workspace = true, features = ["use_core"] } derive_more = { workspace = true } hex = { workspace = true } serde = { workspace = true, features = ["derive"] } @@ -41,7 +41,7 @@ blake2 = { workspace = true } # Provides some deserialization, types like U256/H256 and hashing impls like twox/blake256: impl-serde = { workspace = true } -primitive-types = { workspace = true } +primitive-types = { workspace = true, default-features = false, features = ["codec", "serde_no_std", "scale-info"] } sp-core-hashing = { workspace = true } # For parsing urls to disallow insecure schemes @@ -50,6 +50,7 @@ url = { workspace = true } # Included if the "substrate-compat" feature is enabled. sp-core = { workspace = true, optional = true } sp-runtime = { workspace = true, optional = true } +cfg-if = "1.0.0" [dev-dependencies] diff --git a/core/no_std_tests/Cargo.lock b/core/no_std_tests/Cargo.lock index d7ffa7cfd3..8cbcad4291 100644 --- a/core/no_std_tests/Cargo.lock +++ b/core/no_std_tests/Cargo.lock @@ -204,9 +204,6 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ - "byteorder", - "rand", - "rustc-hex", "static_assertions", ] @@ -264,17 +261,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "getrandom" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "hashbrown" version = "0.14.3" @@ -413,12 +399,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "ppv-lite86" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" - [[package]] name = "primitive-types" version = "0.12.2" @@ -476,42 +456,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rustc-hex" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" - [[package]] name = "rustc_version" version = "0.4.0" @@ -742,9 +686,11 @@ version = "0.34.0" dependencies = [ "base58", "blake2", + "cfg-if", "derivative", "derive_more", "frame-metadata 16.0.0", + "hex", "impl-serde", "parity-scale-codec", "primitive-types", @@ -756,6 +702,7 @@ dependencies = [ "serde", "serde_json", "sp-core-hashing", + "subxt-metadata", "url", ] @@ -769,6 +716,17 @@ dependencies = [ "subxt-core", ] +[[package]] +name = "subxt-metadata" +version = "0.34.0" +dependencies = [ + "frame-metadata 16.0.0", + "parity-scale-codec", + "scale-info", + "sp-core-hashing", + "thiserror", +] + [[package]] name = "syn" version = "1.0.109" @@ -797,6 +755,26 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -907,12 +885,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - [[package]] name = "winnow" version = "0.5.34" diff --git a/core/no_std_tests/src/main.rs b/core/no_std_tests/src/main.rs index c5e37f1d02..757e8a7da1 100644 --- a/core/no_std_tests/src/main.rs +++ b/core/no_std_tests/src/main.rs @@ -47,6 +47,8 @@ static ALLOCATOR: LibcAlloc = LibcAlloc; fn main() { let metadata_bytes: &[u8] = include_bytes!("../../../artifacts/polkadot_metadata_small.scale"); + let e = core::marker::PhantomData::; + // let genesis_hash = { // let h = "91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3"; // let bytes = hex::decode(h).unwrap(); diff --git a/core/src/client/mod.rs b/core/src/client/mod.rs index 96b397d300..919154b836 100644 --- a/core/src/client/mod.rs +++ b/core/src/client/mod.rs @@ -13,7 +13,7 @@ pub struct MinimalClient { pub genesis_hash: C::Hash, pub runtime_version: RuntimeVersion, pub metadata: Metadata, - marker: std::marker::PhantomData, + marker: core::marker::PhantomData, } impl MinimalClient { diff --git a/core/src/config/extrinsic_params.rs b/core/src/config/extrinsic_params.rs index 378376b450..8cf659aeaa 100644 --- a/core/src/config/extrinsic_params.rs +++ b/core/src/config/extrinsic_params.rs @@ -11,6 +11,9 @@ use crate::client::MinimalClient; use super::Config; use core::fmt::Debug; + +use crate::prelude::*; + use derive_more::Display; /// An error that can be emitted when trying to construct an instance of [`ExtrinsicParams`], @@ -34,17 +37,21 @@ pub enum ExtrinsicParamsError { UnknownSignedExtension(String), /// Some custom error. #[display(fmt = "Error constructing extrinsic parameters: {_0}")] + #[cfg(feature = "std")] Custom(CustomExtrinsicParamsError), } /// A custom error. +#[cfg(feature = "std")] pub type CustomExtrinsicParamsError = Box; -impl From for ExtrinsicParamsError { - fn from(value: std::convert::Infallible) -> Self { +impl From for ExtrinsicParamsError { + fn from(value: core::convert::Infallible) -> Self { match value {} } } + +#[cfg(feature = "std")] impl From for ExtrinsicParamsError { fn from(value: CustomExtrinsicParamsError) -> Self { ExtrinsicParamsError::Custom(value) diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index ddfa2466c7..f14849df6e 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -75,7 +75,7 @@ pub trait BlockHash: + Encode + PartialEq + Eq - + std::hash::Hash + + core::hash::Hash { } impl BlockHash for T where @@ -90,7 +90,7 @@ impl BlockHash for T where + Encode + PartialEq + Eq - + std::hash::Hash + + core::hash::Hash { } diff --git a/core/src/config/signed_extensions.rs b/core/src/config/signed_extensions.rs index 6ba0284c40..a93f305aef 100644 --- a/core/src/config/signed_extensions.rs +++ b/core/src/config/signed_extensions.rs @@ -10,15 +10,15 @@ use super::extrinsic_params::{ExtrinsicParams, ExtrinsicParamsEncoder, ExtrinsicParamsError}; use super::Config; use crate::client::MinimalClient; +use crate::prelude::*; use crate::utils::Era; use codec::{Compact, Encode}; +use collections::BTreeMap; use core::fmt::Debug; use derivative::Derivative; use scale_decode::DecodeAsType; use scale_info::PortableRegistry; -use std::collections::HashMap; - /// A single [`SignedExtension`] has a unique name, but is otherwise the /// same as [`ExtrinsicParams`] in describing how to encode the extra and /// additional data. @@ -368,7 +368,7 @@ impl SignedExtension for ChargeTransactionPayment { /// is a sensible default, and allows for a single configuration to work across multiple chains. pub struct AnyOf { params: Vec>, - _marker: std::marker::PhantomData<(T, Params)>, + _marker: core::marker::PhantomData<(T, Params)>, } macro_rules! impl_tuples { @@ -393,7 +393,7 @@ macro_rules! impl_tuples { // For each signed extension in the tuple, find the matching index in the metadata, if // there is one, and add it to a map with that index as the key. - let mut exts_by_index = HashMap::new(); + let mut exts_by_index = BTreeMap::new(); $({ for (idx, e) in metadata.extrinsic().signed_extensions().iter().enumerate() { // Skip over any exts that have a match already: @@ -425,7 +425,7 @@ macro_rules! impl_tuples { Ok(AnyOf { params, - _marker: std::marker::PhantomData + _marker: core::marker::PhantomData }) } } diff --git a/core/src/config/substrate.rs b/core/src/config/substrate.rs index a2eaaf4f63..1c9904d427 100644 --- a/core/src/config/substrate.rs +++ b/core/src/config/substrate.rs @@ -8,6 +8,7 @@ use super::{Config, DefaultExtrinsicParams, DefaultExtrinsicParamsBuilder, Hashe use codec::{Decode, Encode}; use serde::{Deserialize, Serialize}; +use crate::prelude::*; pub use crate::utils::{AccountId32, MultiAddress, MultiSignature}; pub use primitive_types::{H256, U256}; diff --git a/core/src/lib.rs b/core/src/lib.rs index d1106c42bc..abbe9a4fad 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -6,10 +6,13 @@ //! //! `#[no_std]` compatible core crate for subxt. +#![no_std] + pub mod client; pub mod config; pub mod dynamic; pub mod metadata; +pub mod prelude; pub mod tx; pub mod utils; diff --git a/core/src/metadata/decode_encode_traits.rs b/core/src/metadata/decode_encode_traits.rs index a1f5d019fb..daadadddcd 100644 --- a/core/src/metadata/decode_encode_traits.rs +++ b/core/src/metadata/decode_encode_traits.rs @@ -3,6 +3,7 @@ // see LICENSE for license details. use super::Metadata; +use crate::prelude::*; /// This trait is implemented for all types that also implement [`scale_decode::DecodeAsType`]. pub trait DecodeWithMetadata: Sized { diff --git a/core/src/metadata/metadata_type.rs b/core/src/metadata/metadata_type.rs index 99f930e54a..4fde0bc4d3 100644 --- a/core/src/metadata/metadata_type.rs +++ b/core/src/metadata/metadata_type.rs @@ -2,8 +2,9 @@ // This file is dual-licensed as Apache-2.0 or GPL-3.0. // see LICENSE for license details. +use crate::prelude::*; use derive_more::Display; -use std::sync::Arc; +use sync::Arc; /// A cheaply clone-able representation of the runtime metadata received from a node. #[derive(Clone, Debug)] @@ -11,7 +12,7 @@ pub struct Metadata { inner: Arc, } -impl std::ops::Deref for Metadata { +impl core::ops::Deref for Metadata { type Target = subxt_metadata::Metadata; fn deref(&self) -> &Self::Target { &self.inner diff --git a/core/src/prelude.rs b/core/src/prelude.rs new file mode 100644 index 0000000000..2c031fdae8 --- /dev/null +++ b/core/src/prelude.rs @@ -0,0 +1,41 @@ +#[cfg(not(feature = "std"))] +extern crate alloc; + +use cfg_if::cfg_if; + +cfg_if! { + if #[cfg(feature = "std")] { + + + + pub use std::{ + borrow, + borrow::ToOwned, + boxed::{Box}, + collections, + format, + format::format, + string::{String}, + str, + sync, + vec, + rc, + }; + pub use std::vec::Vec; + } else { + pub use alloc::{ + borrow, + borrow::ToOwned, + boxed::{Box}, + collections, + format, + string::{String}, + str, + sync, + vec, + rc, + }; + pub use alloc::vec::Vec; + + } +} diff --git a/core/src/tx/tx_payload.rs b/core/src/tx/tx_payload.rs index 902420aca0..6b65d8e434 100644 --- a/core/src/tx/tx_payload.rs +++ b/core/src/tx/tx_payload.rs @@ -13,7 +13,7 @@ // use codec::Encode; // use scale_encode::EncodeAsFields; // use scale_value::{Composite, ValueDef, Variant}; -// use std::{borrow::Cow, sync::Arc}; +// use core::{borrow::Cow, sync::Arc}; // /// This represents a transaction payload that can be submitted // /// to a node. diff --git a/core/src/utils/account_id.rs b/core/src/utils/account_id.rs index b9ee3dda21..13add87256 100644 --- a/core/src/utils/account_id.rs +++ b/core/src/utils/account_id.rs @@ -6,6 +6,7 @@ //! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_core::AccountId32` //! for instance, to gain functionality without forcing a dependency on Substrate crates here. +use crate::prelude::*; use codec::{Decode, Encode}; use derive_more::Display; use serde::{Deserialize, Serialize}; @@ -146,13 +147,13 @@ impl<'de> Deserialize<'de> for AccountId32 { } } -impl std::fmt::Display for AccountId32 { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for AccountId32 { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "{}", self.to_ss58check()) } } -impl std::str::FromStr for AccountId32 { +impl core::str::FromStr for AccountId32 { type Err = FromSs58Error; fn from_str(s: &str) -> Result { AccountId32::from_ss58check(s) diff --git a/core/src/utils/bits.rs b/core/src/utils/bits.rs index ed830a0dea..5ab6e0b0ab 100644 --- a/core/src/utils/bits.rs +++ b/core/src/utils/bits.rs @@ -4,13 +4,14 @@ //! Generic `scale_bits` over `bitvec`-like `BitOrder` and `BitFormat` types. +use crate::prelude::*; use codec::{Compact, Input}; +use core::marker::PhantomData; use scale_bits::{ scale::format::{Format, OrderFormat, StoreFormat}, Bits, }; use scale_decode::IntoVisitor; -use std::marker::PhantomData; /// Associates `bitvec::store::BitStore` trait with corresponding, type-erased `scale_bits::StoreFormat` enum. /// @@ -144,7 +145,7 @@ impl codec::Encode for DecodedBits(std::marker::PhantomData<(S, O)>); +pub struct DecodedBitsVisitor(core::marker::PhantomData<(S, O)>); impl scale_decode::Visitor for DecodedBitsVisitor { type Value<'scale, 'info> = DecodedBits; type Error = scale_decode::Error; diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index df58f9d27a..07a22aa4b2 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -64,7 +64,9 @@ pub fn url_is_secure(url: &str) -> Result { Ok(secure_scheme || is_localhost) } -/// A version of [`std::marker::PhantomData`] that is also Send and Sync (which is fine +use crate::prelude::*; + +/// A version of [`core::marker::PhantomData`] that is also Send and Sync (which is fine /// because regardless of the generic param, it is always possible to Send + Sync this /// 0 size type). #[derive(Derivative, Encode, Decode, scale_info::TypeInfo)] diff --git a/core/src/utils/multi_address.rs b/core/src/utils/multi_address.rs index 9b1e556fa9..c056d04e0f 100644 --- a/core/src/utils/multi_address.rs +++ b/core/src/utils/multi_address.rs @@ -6,6 +6,7 @@ //! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_runtime::MultiAddress` //! for instance, to gain functionality without forcing a dependency on Substrate crates here. +use crate::prelude::*; use codec::{Decode, Encode}; /// A multi-format address wrapper for on-chain accounts. This is a simplified version of Substrate's diff --git a/core/src/utils/static_type.rs b/core/src/utils/static_type.rs index 2d13e61eba..4457372bc6 100644 --- a/core/src/utils/static_type.rs +++ b/core/src/utils/static_type.rs @@ -2,6 +2,7 @@ // This file is dual-licensed as Apache-2.0 or GPL-3.0. // see LICENSE for license details. +use crate::prelude::*; use codec::{Decode, Encode}; use scale_decode::{visitor::DecodeAsTypeResult, IntoVisitor, Visitor}; use scale_encode::EncodeAsType; @@ -29,7 +30,7 @@ impl EncodeAsType for Static { } } -pub struct StaticDecodeAsTypeVisitor(std::marker::PhantomData); +pub struct StaticDecodeAsTypeVisitor(core::marker::PhantomData); impl Visitor for StaticDecodeAsTypeVisitor { type Value<'scale, 'info> = Static; @@ -52,7 +53,7 @@ impl Visitor for StaticDecodeAsTypeVisitor { impl IntoVisitor for Static { type Visitor = StaticDecodeAsTypeVisitor; fn into_visitor() -> Self::Visitor { - StaticDecodeAsTypeVisitor(std::marker::PhantomData) + StaticDecodeAsTypeVisitor(core::marker::PhantomData) } } @@ -64,14 +65,14 @@ impl From for Static { } // Static is just a marker type and should be as transparent as possible: -impl std::ops::Deref for Static { +impl core::ops::Deref for Static { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } -impl std::ops::DerefMut for Static { +impl core::ops::DerefMut for Static { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/core/src/utils/unchecked_extrinsic.rs b/core/src/utils/unchecked_extrinsic.rs index 882b490bed..f74ea74eb0 100644 --- a/core/src/utils/unchecked_extrinsic.rs +++ b/core/src/utils/unchecked_extrinsic.rs @@ -9,9 +9,9 @@ //! runtime APIs. Deriving `EncodeAsType` would lead to the inner //! bytes to be re-encoded (length prefixed). -use std::marker::PhantomData; - +use crate::prelude::*; use codec::{Decode, Encode}; +use core::marker::PhantomData; use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType, IntoVisitor, Visitor}; use super::{Encoded, Static}; diff --git a/core/src/utils/wrapper_opaque.rs b/core/src/utils/wrapper_opaque.rs index 9257405715..f3f8700529 100644 --- a/core/src/utils/wrapper_opaque.rs +++ b/core/src/utils/wrapper_opaque.rs @@ -3,6 +3,7 @@ // see LICENSE for license details. use super::PhantomDataSendSync; +use crate::prelude::*; use codec::{Compact, Decode, DecodeAll, Encode}; use derivative::Derivative; use scale_decode::{IntoVisitor, Visitor}; @@ -108,7 +109,7 @@ impl EncodeAsType for WrapperKeepOpaque { } } -pub struct WrapperKeepOpaqueVisitor(std::marker::PhantomData); +pub struct WrapperKeepOpaqueVisitor(core::marker::PhantomData); impl Visitor for WrapperKeepOpaqueVisitor { type Value<'scale, 'info> = WrapperKeepOpaque; type Error = scale_decode::Error; @@ -153,7 +154,7 @@ impl Visitor for WrapperKeepOpaqueVisitor { impl IntoVisitor for WrapperKeepOpaque { type Visitor = WrapperKeepOpaqueVisitor; fn into_visitor() -> Self::Visitor { - WrapperKeepOpaqueVisitor(std::marker::PhantomData) + WrapperKeepOpaqueVisitor(core::marker::PhantomData) } } @@ -198,7 +199,7 @@ mod test { + Encode + Decode + PartialEq - + std::fmt::Debug + + core::fmt::Debug + scale_info::TypeInfo + 'static, {