paritytech / paritytech/parity-common

Issues wrapping ethereum types and deriving scale-codec methods

Open
#679 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Z1-question
Dominant language
Rust
Stars
311
Forks
245
PR merge metrics
No merged PRs in 30d

Description

I'm trying to implement wrapper types for ethereum_types types in order to provide other traits necessary for making my code compatible with https://github.com/sigp/lighthouse dependencies and Substrate. The following snippet demonstrates my intention.

macro_rules! uint_declare_wrapper_and_serde {
    ($name: ident, $len: expr) => {
        #[derive(
            Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Add, Sub, Mul, Div, Rem,
            AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, Display, From, Into,
            Encode, EncodeLike, Decode, MaxEncodedLen, TypeInfo,
        )]
        // #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
        pub struct $name(pub ethereum_types::$name);

        impl Encodable for $name {
            fn rlp_append(&self, s: &mut RlpStream) {
                <ethereum_types::$name>::rlp_append(&self.0, s);
            }
        }

        impl Decodable for $name {
            fn decode(rlp: &Rlp) -> Result<Self, RlpDecoderError> {
                Ok($name(<ethereum_types::$name>::rlp_derive(rlp)?))
            }
        }
    };
}

uint_declare_wrapper_and_serde!(U64, 1);
uint_declare_wrapper_and_serde!(U128, 2);
uint_declare_wrapper_and_serde!(U256, 4);

Unfortunately, I keep fighting with the compiler who doesn't think that these types can implement Encode/Decode even though I am importing the package with those features. My TOML is below:

ethereum-types = { version = "0.12.1", features = ["codec", "rlp", "num-traits"], default-features = false }
Errors
error[E0599]: no function or associated item named `max_encoded_len` found for struct `ethereum_types::U64` in the current scope
  --> src/lib.rs:62:30
   |
62 |         pub struct $name(pub ethereum_types::$name);
   |                              ^^^^^^^^^^^^^^ function or associated item not found in `ethereum_types::U64`
...
78 | uint_declare_wrapper_and_serde!(U64, 1);
   | --------------------------------------- in this macro invocation
   |
   = help: items from traits can only be used if the trait is in scope
   = note: this error originates in the macro `uint_declare_wrapper_and_serde` (in Nightly builds, run with -Z macro-backtrace for more info)
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
4  | use parity_scale_codec::max_encoded_len::MaxEncodedLen;
   |

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the wrapper macro in src/lib.rs and the ethereum-types dependency features shown in Cargo.toml. Reproduce the compiler errors and inspect how the requested scale-codec traits are exposed for these types. Done means the intended wrappers compile with the required Encode, Decode, and related derives.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.