1

I'm working on a custom pallet related to the zero-knowledge proof system and I've run into an issue regarding the trait implementations. I have defined a fixed-size hash type H256 using a custom macro construct_fixed_hash! provided by the fixed_hash crate:

construct_fixed_hash!{ pub struct H256(32); }

This type is used extensively across my module for cryptographic purposes. For a specific feature, I need to treat this type differently; hence, I introduced a new type alias GEpoch for H256 to represent a distinct conceptual entity:

pub type GEpoch = H256;

I've implemented the Encode and Decode traits for the GEpoch type, however, on defining it as a storage item inside a pallet like this:

#[pallet::storage]
#[pallet::getter(fn g_epoch)]
pub(crate) type LastGEpoch<T: Config> = StorageValue<_, GEpoch>;

I'm getting this error:

error[E0277]: the trait bound `g_epoch::H256: EncodeLike` is not satisfied
  --> pallets/zk-system/src/lib.rs:50:15
   |
50 |       #[pallet::pallet]
   |  _______________^
51 | |     pub struct Pallet<T>(core::marker::PhantomData<T>);
52 | |
53 | |     #[pallet::event]
...  |
70 | |     #[pallet::getter(fn g_epoch)]
71 | |     pub(crate) type LastGEpoch<T: Config> = StorageValue<_, GEpoch>;
   | |______________________________^ the trait `EncodeLike` is not implemented for `g_epoch::H256`
   |
   = help: the following other types implement trait `EncodeLike<T>`:
             <bool as EncodeLike>
             <i8 as EncodeLike>
             <i16 as EncodeLike>
             <i32 as EncodeLike>
             <i64 as EncodeLike>
             <i128 as EncodeLike>
             <u8 as EncodeLike>
             <u16 as EncodeLike>
           and 333 others
   = note: required for `g_epoch::H256` to implement `FullEncode`
   = note: required for `g_epoch::H256` to implement `FullCodec`
   = note: required for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageLastGEpoch<T>, g_epoch::H256>` to implement `StorageInfoTrait`

As I understand it, the implementation of Encode/Decode traits automatically implements the EncodeLike trait, but I'm still getting this issue.

1 Answer 1

2

sp_core::hash already provides a H256 type that we normally use for storage. You can still wrap it, but would be better to wrap it in a Rust newtype. Implementing traits on type aliases is normally going to produce conflicting implementations, since it is just a rename - not a new type.

You can probably debug it by putting first the H256 into storage and ensuring that it works and then a wrapped GEpoch type.

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.