paritytech / paritytech/parity-common

How to define an `Address` as a constant?

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

Nobody has claimed this yet.

F8-enhancement Q2-easy
Dominant language
Rust
Stars
311
Forks
245
PR merge metrics
No merged PRs in 30d

Description

Hello! I want to define a global constant of type Address, but I just don't know how to do it, and I couldn't figure out a solution from the docs. I tried to do the following:

use ethereum_types::Address;
use std::str::FromStr;

pub const MY_ADDRESS: Address = Address::from_slice(0x0298F4332e3857631385b39766325058a93e249f);

But the compilation failed with this error:

error: integer literal is too large

I also tried to use the from_str function:

use ethereum_types::Address;
use std::str::FromStr;

pub const MY_ADDRESS: Address = Address::from_str("0x0298F4332e3857631385b39766325058a93e249f").unwrap();

But this didn't work either:

error: cannot call non-const fn <H160 as FromStr>::from_str in constants
note: calls in constants are limited to constant functions, tuple structs and tuple variants
cannot call non-const fn <H160 as FromStr>::from_str in constants
calls in constants are limited to constant functions, tuple structs and tuple variants

Is it even possible to do this, or must Addresses always be defined as non-constants?

I saw in your tests that you are hard-coding some addresses like this:

H160::from([
    0xEF, 0x2D, 0x6D, 0x19, 0x40, 0x84, 0xC2, 0xDE, 0x36, 0xE0,
    0xDA, 0xBF, 0xCE, 0x45, 0xD0, 0x46, 0xB3, 0x7D, 0x11, 0x06,
]);

But I wonder if there is a less verbose way to do it, which doesn't involve passing an array of bytes to the from function? Ideally I would pass the address in full, as shown in the code snippets above.

Side note: it might be worth it to enable discussions in this repo, so that users like me who don't want to report bugs or make feature requests can avoid spamming the issues channel with questions.

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 Address implementation and the hard-coded examples in fixed-hash/src/tests.rs, then check the crate documentation for const constructors and parsing APIs. Document whether a full hexadecimal address can be used in a constant and show the supported pattern; done means the documentation answers the question without requiring users to infer it from tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.