Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define an Address as a constant? #702

Open
PaulRBerg opened this issue Jan 1, 2023 · 2 comments
Open

How to define an Address as a constant? #702

PaulRBerg opened this issue Jan 1, 2023 · 2 comments

Comments

@PaulRBerg
Copy link

PaulRBerg commented Jan 1, 2023

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.

@ordian
Copy link
Member

ordian commented Feb 1, 2023

As you've noticed, Address is a type alias for H160, which is a type constructed by the fixed-hash crate. This crate was written in the pre-const times, where the amount of computation one could do in a const context was limited.

We still can't make H160::from and H160::from_str const, because the respective traits from std lib are not const.
We can however, add a separate const method to do a const construction.

A similar issue for uint types is #585.

@dinhani
Copy link

dinhani commented Jun 9, 2023

Using hex-literal crate.

H160(hex_literal::hex!("0298F4332e3857631385b39766325058a93e249f")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants