Skip to content
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

Remove usage of sp-std from Substrate #7043

Merged
merged 17 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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
35 changes: 35 additions & 0 deletions prdoc/pr_7043.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
title: Remove usage of `sp-std` from Substrate
doc:
- audience: Runtime Dev
description: |-
# Description

This PR removes usage of deprecated `sp-std` from Substrate. (following PR of #5010)

## Integration

This PR doesn't remove re-exported `sp_std` from any crates yet, so downstream projects using re-exported `sp_std` will not be affected.

## Review Notes

The existing code using `sp-std` is refactored to use `alloc` and `core` directly. The key-value maps are instantiated from an array of tuples directly instead of using `sp_std::map!` macro.

This PR replaces `sp_std::Writer`, a helper type for using `Vec<u8>` with `core::fmt::Write` trait, with `alloc::string::String`.

crates:
- name: pallet-contracts
bump: patch
- name: pallet-revive
bump: patch
- name: sp-runtime
bump: patch
- name: frame-support-procedural
bump: patch
- name: frame-system
bump: patch
- name: pallet-contracts-proc-macro
bump: patch
- name: pallet-revive-proc-macro
bump: patch
- name: frame-support
bump: patch
7 changes: 3 additions & 4 deletions substrate/frame/contracts/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,9 @@ fn expand_functions(def: &EnvDef, expand_mode: ExpandMode) -> TokenStream2 {
let result = #body;
if ::log::log_enabled!(target: "runtime::contracts::strace", ::log::Level::Trace) {
use core::fmt::Write;
let mut w = sp_std::Writer::default();
let _ = core::write!(&mut w, #trace_fmt_str, #( #trace_fmt_args, )* result);
let msg = core::str::from_utf8(&w.inner()).unwrap_or_default();
ctx.ext().append_debug_buffer(msg);
let mut msg = alloc::string::String::default();
let _ = core::write!(&mut msg, #trace_fmt_str, #( #trace_fmt_args, )* result);
ctx.ext().append_debug_buffer(&msg);
}
result
}
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/src/transient_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::{
storage::WriteOutcome,
Config, Error,
};
use alloc::{collections::BTreeMap, vec::Vec};
use codec::Encode;
use core::marker::PhantomData;
use core::{marker::PhantomData, mem};
use frame_support::DefaultNoBound;
use sp_runtime::{DispatchError, DispatchResult, Saturating};
use sp_std::{collections::btree_map::BTreeMap, mem, vec::Vec};

/// Meter entry tracks transaction allocations.
#[derive(Default, Debug)]
Expand Down
7 changes: 3 additions & 4 deletions substrate/frame/revive/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,9 @@ fn expand_functions(def: &EnvDef) -> TokenStream2 {
let result = (|| #body)();
if ::log::log_enabled!(target: "runtime::revive::strace", ::log::Level::Trace) {
use core::fmt::Write;
let mut w = sp_std::Writer::default();
let _ = core::write!(&mut w, #trace_fmt_str, #( #trace_fmt_args, )* result);
let msg = core::str::from_utf8(&w.inner()).unwrap_or_default();
self.ext().append_debug_buffer(msg);
let mut msg = alloc::string::String::default();
let _ = core::write!(&mut msg, #trace_fmt_str, #( #trace_fmt_args, )* result);
self.ext().append_debug_buffer(&msg);
}
result
}
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/revive/src/transient_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::{
storage::WriteOutcome,
Config, Error,
};
use alloc::{collections::BTreeMap, vec::Vec};
use codec::Encode;
use core::marker::PhantomData;
use core::{marker::PhantomData, mem};
use frame_support::DefaultNoBound;
use sp_runtime::{DispatchError, DispatchResult, Saturating};
use sp_std::{collections::btree_map::BTreeMap, mem, vec::Vec};

/// Meter entry tracks transaction allocations.
#[derive(Default, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn expand_config_metadata(def: &Def) -> proc_macro2::TokenStream {
ty: #frame_support::__private::scale_info::meta_type::<
<T as Config #trait_use_gen>::#ident
>(),
docs: #frame_support::__private::sp_std::vec![ #( #doc ),* ],
docs: #frame_support::__private::vec![ #( #doc ),* ],
}
})
});
Expand All @@ -136,9 +136,9 @@ pub fn expand_config_metadata(def: &Def) -> proc_macro2::TokenStream {

#[doc(hidden)]
pub fn pallet_associated_types_metadata()
-> #frame_support::__private::sp_std::vec::Vec<#frame_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR>
-> #frame_support::__private::vec::Vec<#frame_support::__private::metadata_ir::PalletAssociatedTypeMetadataIR>
{
#frame_support::__private::sp_std::vec![ #( #types ),* ]
#frame_support::__private::vec![ #( #types ),* ]
}
}
)
Expand Down
7 changes: 4 additions & 3 deletions substrate/frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod __private {
pub use alloc::{
boxed::Box,
rc::Rc,
string::String,
vec,
vec::{IntoIter, Vec},
};
Expand Down Expand Up @@ -502,9 +503,9 @@ macro_rules! runtime_print {
($($arg:tt)+) => {
{
use core::fmt::Write;
let mut w = $crate::__private::sp_std::Writer::default();
let _ = core::write!(&mut w, $($arg)+);
$crate::__private::sp_io::misc::print_utf8(&w.inner())
let mut msg = $crate::__private::String::default();
let _ = core::write!(&mut msg, $($arg)+);
$crate::__private::sp_io::misc::print_utf8(msg.as_bytes())
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ use sp_runtime::{
},
DispatchError, RuntimeDebug,
};
#[cfg(any(feature = "std", test))]
use sp_std::map;
use sp_version::RuntimeVersion;

use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen};
Expand Down Expand Up @@ -1905,12 +1903,14 @@ impl<T: Config> Pallet<T> {
#[cfg(any(feature = "std", test))]
pub fn externalities() -> TestExternalities {
TestExternalities::new(sp_core::storage::Storage {
top: map![
<BlockHash<T>>::hashed_key_for(BlockNumberFor::<T>::zero()) => [69u8; 32].encode(),
<Number<T>>::hashed_key().to_vec() => BlockNumberFor::<T>::one().encode(),
<ParentHash<T>>::hashed_key().to_vec() => [69u8; 32].encode()
],
children_default: map![],
top: [
(<BlockHash<T>>::hashed_key_for(BlockNumberFor::<T>::zero()), [69u8; 32].encode()),
(<Number<T>>::hashed_key().to_vec(), BlockNumberFor::<T>::one().encode()),
(<ParentHash<T>>::hashed_key().to_vec(), [69u8; 32].encode()),
]
.into_iter()
.collect(),
children_default: Default::default(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod legacy {
Extra: Encode,
{
fn encode(&self) -> Vec<u8> {
let mut tmp = Vec::with_capacity(sp_std::mem::size_of::<Self>());
let mut tmp = Vec::with_capacity(core::mem::size_of::<Self>());

// 1 byte version id.
match self.signature.as_ref() {
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/runtime/src/proving_trie/base16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

use super::{ProofToHashes, ProvingTrie, TrieError};
use crate::{Decode, DispatchError, Encode};
use alloc::vec::Vec;
use codec::MaxEncodedLen;
use sp_std::vec::Vec;
use sp_trie::{
trie_types::{TrieDBBuilder, TrieDBMutBuilderV1},
LayoutV1, MemoryDB, Trie, TrieMut,
Expand Down Expand Up @@ -197,7 +197,7 @@ mod tests {
use super::*;
use crate::traits::BlakeTwo256;
use sp_core::H256;
use sp_std::collections::btree_map::BTreeMap;
use std::collections::BTreeMap;

// A trie which simulates a trie of accounts (u32) and balances (u128).
type BalanceTrie = BasicProvingTrie<BlakeTwo256, u32, u128>;
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/runtime/src/proving_trie/base2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

use super::{ProofToHashes, ProvingTrie, TrieError};
use crate::{Decode, DispatchError, Encode};
use alloc::{collections::BTreeMap, vec::Vec};
use binary_merkle_tree::{merkle_proof, merkle_root, MerkleProof};
use codec::MaxEncodedLen;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

/// A helper structure for building a basic base-2 merkle trie and creating compact proofs for that
/// trie.
Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {
use super::*;
use crate::traits::BlakeTwo256;
use sp_core::H256;
use sp_std::collections::btree_map::BTreeMap;
use std::collections::BTreeMap;

// A trie which simulates a trie of accounts (u32) and balances (u128).
type BalanceTrie = BasicProvingTrie<BlakeTwo256, u32, u128>;
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/runtime/src/proving_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod base2;
use crate::{Decode, DispatchError, Encode, MaxEncodedLen, TypeInfo};
#[cfg(feature = "serde")]
use crate::{Deserialize, Serialize};
use sp_std::vec::Vec;
use alloc::vec::Vec;
use sp_trie::{trie_types::TrieError as SpTrieError, VerifyError};

/// A runtime friendly error type for tries.
Expand Down
6 changes: 3 additions & 3 deletions substrate/primitives/runtime/src/runtime_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ impl log::Log for RuntimeLogger {

fn log(&self, record: &log::Record) {
use core::fmt::Write;
let mut w = sp_std::Writer::default();
let _ = ::core::write!(&mut w, "{}", record.args());
let mut msg = alloc::string::String::default();
let _ = ::core::write!(&mut msg, "{}", record.args());

sp_io::logging::log(record.level().into(), record.target(), w.inner());
sp_io::logging::log(record.level().into(), record.target(), msg.as_bytes());
}

fn flush(&self) {}
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/runtime/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ pub trait SignedExtension:
/// This method provides a default implementation that returns a vec containing a single
/// [`TransactionExtensionMetadata`].
fn metadata() -> Vec<TransactionExtensionMetadata> {
sp_std::vec![TransactionExtensionMetadata {
alloc::vec![TransactionExtensionMetadata {
identifier: Self::IDENTIFIER,
ty: scale_info::meta_type::<Self>(),
implicit: scale_info::meta_type::<Self::AdditionalSigned>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ use crate::{
},
DispatchResult,
};
use alloc::vec::Vec;
use codec::{Codec, Decode, Encode};
use impl_trait_for_tuples::impl_for_tuples;
use core::fmt::Debug;
#[doc(hidden)]
pub use sp_std::marker::PhantomData;
use sp_std::{self, fmt::Debug, prelude::*};
pub use core::marker::PhantomData;
use impl_trait_for_tuples::impl_for_tuples;
use sp_weights::Weight;
use tuplex::{PopFront, PushBack};

Expand Down Expand Up @@ -258,7 +259,7 @@ pub trait TransactionExtension<Call: Dispatchable>:
/// This method provides a default implementation that returns a vec containing a single
/// [`TransactionExtensionMetadata`].
fn metadata() -> Vec<TransactionExtensionMetadata> {
sp_std::vec![TransactionExtensionMetadata {
alloc::vec![TransactionExtensionMetadata {
identifier: Self::IDENTIFIER,
ty: scale_info::meta_type::<Self>(),
implicit: scale_info::meta_type::<Self::Implicit>()
Expand Down Expand Up @@ -668,7 +669,7 @@ impl<Call: Dispatchable> TransactionExtension<Call> for Tuple {
impl<Call: Dispatchable> TransactionExtension<Call> for () {
const IDENTIFIER: &'static str = "UnitTransactionExtension";
type Implicit = ();
fn implicit(&self) -> sp_std::result::Result<Self::Implicit, TransactionValidityError> {
fn implicit(&self) -> core::result::Result<Self::Implicit, TransactionValidityError> {
Ok(())
}
type Val = ();
Expand Down
Loading