2

Trying to generate random 256 bytes hash-like data. But compiler tells

  error[E0599]: no function or associated item named `random` found for struct `H256` in the current scope
     --> /home/poh/pro/work/aratar/substrate-dev/trait/app-agents/src/lib.rs:262:23
      |
  262 |                 seed: hash::H256::random(),
      |                                   ^^^^^^ function or associated item not found in `H256`

But, if I change random to zero, then everything compiles successfully. What did I miss?

Using substrate v0.9.25.

Update

How to reproduce:

  • Clone 0.9.25 git clone --recursive [email protected]:paritytech/substrate.git -b polkadot-v0.9.25
  • Apply this changes:
diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml
index 9e98d4e15a..40421d3a8c 100644
--- a/frame/assets/Cargo.toml
+++ b/frame/assets/Cargo.toml
@@ -23,9 +23,9 @@ frame-support = { version = "4.0.0-dev", default-features = false, path = "../su
 # `system` module provides us with all sorts of useful stuff and macros depend on it being around.
 frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
 frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
+sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
 
 [dev-dependencies]
-sp-core = { version = "6.0.0", path = "../../primitives/core" }
 sp-std = { version = "4.0.0", path = "../../primitives/std" }
 sp-io = { version = "6.0.0", path = "../../primitives/io" }
 pallet-balances = { version = "4.0.0-dev", path = "../balances" }
diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs
index 3f9146c5d2..fad1825812 100644
--- a/frame/assets/src/lib.rs
+++ b/frame/assets/src/lib.rs
@@ -160,6 +160,7 @@ use frame_support::{
        },
 };
 use frame_system::Config as SystemConfig;
+use sp_core::hash;
 
 pub use pallet::*;
 pub use weights::WeightInfo;
@@ -504,6 +505,7 @@ pub mod pallet {
                        admin: <T::Lookup as StaticLookup>::Source,
                        min_balance: T::Balance,
                ) -> DispatchResult {
+                       let x = hash::H256::random();
                        let owner = ensure_signed(origin)?;
                        let admin = T::Lookup::lookup(admin)?;

  • cargo check

My setup is

$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home:  /home/poh/.rustup

installed toolchains
--------------------

stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu

installed targets for active toolchain
--------------------------------------

wasm32-unknown-unknown
x86_64-unknown-linux-gnu

active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.62.1 (e092d0b6b 2022-07-16)
2
  • Can you provide a minimal test that shows this behavior? I was able to get this to work using a very simple unit test Commented Aug 26, 2022 at 18:35
  • 1
    @ShannonWells, I updated the OP post with reproduction steps.
    – broomrider
    Commented Aug 26, 2022 at 21:38

1 Answer 1

1

If you're using sp_core in your tests, then you can have it like this:

use sp_core::H256;
H256::random()

Make sure to include rand crate as a dev dependency.

Looks like the H256::random() function implementation is feature guarded via rand crate:

// Implementation for disabled rand crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rand` crate feature in
// a user crate.
#[cfg(not(feature = "rand"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rand_for_fixed_hash {
    ( $name:ident ) => {};
}

// Implementation for enabled rand crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rand` crate feature in
// a user crate.
#[cfg(feature = "rand")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rand_for_fixed_hash {
    ( $name:ident ) => {
        impl $crate::rand::distributions::Distribution<$name> for $crate::rand::distributions::Standard {
            fn sample<R: $crate::rand::Rng + ?Sized>(&self, rng: &mut R) -> $name {
                let mut ret = $name::zero();
                for byte in ret.as_bytes_mut().iter_mut() {
                    *byte = rng.gen();
                }
                ret
            }
        }

        /// Utilities using the `rand` crate.
        impl $name {
            <!--snip-->
            /// Create a new hash with cryptographically random content.
            pub fn random() -> Self {
                let mut hash = Self::zero();
                hash.randomize();
                hash
            }
        }
    };
}
3
  • No, I'm trying to use it in pallet. I updated the OP post with reproduction steps.
    – broomrider
    Commented Aug 26, 2022 at 21:39
  • 1
    You will notice that sp_core has a dev dependency of the rand crate.
    – Bruno - R0GUE
    Commented Aug 26, 2022 at 21:55
  • 1
    I think the problem is that I'm trying to use random in runtime (which is wasm compiled). This article helps me to do proper (but weak) random in runtime docs.substrate.io/reference/how-to-guides/pallet-design/…
    – broomrider
    Commented Aug 26, 2022 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.