1

I've copied Kulupu's difficulty pallet and tried to build my node after connecting the pallet. The decl_storage! macro in that pallet looks like this:

decl_storage! {
trait Store for Module<T: Config> as Difficulty {
    /// Past difficulties and timestamps, from earliest to latest.
    // TODO Bug here
    PastDifficultiesAndTimestamps: [Option<DifficultyAndTimestamp<T::Moment>>; 60] = [None; DIFFICULTY_ADJUST_WINDOW as usize];
    /// Current difficulty.
    pub CurrentDifficulty get(fn difficulty) build(|config: &GenesisConfig| {
        config.initial_difficulty
    }): Difficulty;
    /// Initial difficulty.
    pub InitialDifficulty config(initial_difficulty): Difficulty;
}

}

And the error I get is:

error[E0277]: the trait bound `[std::option::Option<DifficultyAndTimestamp<<T as pallet_timestamp::Config>::Moment>>; 60]: sp_core::Encode` is not satisfied
  --> pallets/difficulty/src/lib.rs:40:1
   |
40 | / decl_storage! {
41 | |     trait Store for Module<T: Config> as Difficulty {
42 | |         /// Past difficulties and timestamps, from earliest to latest.
43 | |         // TODO Implement sp_core::Encode, Decode for
...  |
51 | |     }
52 | | }
   | |_^ the trait `sp_core::Encode` is not implemented for `[std::option::Option<DifficultyAndTimestamp<<T as pallet_timestamp::Config>::Moment>>; 60]`
   |
   = help: the following implementations were found:
             <[T; N] as sp_core::Encode>
             <[T] as sp_core::Encode>
note: required by a bound in `hidden_include::storage::generator::StorageValue`
  --> /home/creestl/.cargo/git/checkouts/substrate-7e08433d4c370a21/1b57cff/frame/support/src/storage/generator/value.rs:30:27
   |
30 | pub trait StorageValue<T: FullCodec> {
   |                           ^^^^^^^^^ required by this bound in `hidden_include::storage::generator::StorageValue`
   = note: this error originates in the macro `decl_storage` (in Nightly builds, run with -Z macro-backtrace for more info)

It says that sp_core:Encode (and Decode and some others...) is not implemented for Option<DifficultyAndTimestamp<...>> even though there is such Derive in difficulty pallet:

#[derive(Encode, Decode, TypeInfo, Clone, Copy, Eq, PartialEq, Debug)]
pub struct DifficultyAndTimestamp<M> {
    pub difficulty: Difficulty,
    pub timestamp: M,
}

ALSO if I just clone whole Kulupu's repo and cd to difficulty crate and run cargo build --release it builds no problem. But if I do the same in my crate (with the same code) it won't build.

How do I fix this?
Should I provide a whole error massage?

1 Answer 1

0

This kind of error is usually misleading.

It is clear that Encode is being derived in the code snippet above, but as you see the compiler is complaining that it isn't being derived.

This usually happens when there are mismatched dependencies. For example, one part of your code uses sp-core = v1.0.0 and the other uses sp-core = v2.0.0.

In this case, if the derive is generating Encode for v1.0.0, but then some v2.0.0 code checks this, it will say that Encode is not derived because of the version mismatch.

I don't have a specific solution for you since I don't know your exact project structure, but I would check that all versions of crates you are using, especially the versions of Substrate, are in agreement.

1
  • That solved the issue. Thanks!
    – CREESTL
    Apr 29, 2022 at 12:13

Your Answer

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

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