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

Addresses #27

Merged
merged 3 commits into from
Feb 6, 2024
Merged

Addresses #27

merged 3 commits into from
Feb 6, 2024

Conversation

joneskm
Copy link
Member

@joneskm joneskm commented Jan 22, 2024

No description provided.

proto-types/build.rs Show resolved Hide resolved
pub fn into_inner(self) -> Vec<u8> {
self.0
}
pub type AccAddress = BaseAddress<0>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newtype pattern instead of simple type alias?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefits do we get from using the Newtype pattern?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub struct BaseAddress<const PREFIX: u8>(Vec<u8>);  

pub struct  AccAddress(  BaseAddress<0> );

impl BaseAddress
{
pub fn from_bech32(address: &str, prefix : u32 ) -> Result<Self, AddressError> {
        let (hrp, data, variant) = bech32::decode(address)?;

// This code could be removed
 //let prefix = if PREFIX == 0 {
//            BECH_32_PREFIX_ACC_ADDR
//        } else {
//            BECH_32_PREFIX_VAL_ADDR
//        };

     if hrp != prefix {
            return Err(AddressError::InvalidPrefix {
                expected: prefix.into(),
                found: hrp,
            });
        };
 // I omitted other code
}


impl AccAddress
{
 pub fn from_bech32(address: &str, ) -> Result<Self, AddressError> {
  Self ( BaseAddress::from_bech32( address, BECH_32_PREFIX_ACC_ADDR )
 }
}

Unsure about other parts of code, but this one method may be simpler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to do the same for the ValAddress and for the other public methods of AccAddress.

Once more complex const parameter types arrive in Rust, we'll be able to replace u8 with &'static str, see rust-lang/rust#95174. This will allow for much cleaner code, something like:

pub type AccAddress = BaseAddress<BECH_32_PREFIX_ACC_ADDR>;
pub type ValAddress = BaseAddress<BECH_32_PREFIX_VAL_ADDR>;

#[derive(Debug, PartialEq, Clone)]
pub struct BaseAddress<const PREFIX: &'static str>(Vec<u8>);

impl<const PREFIX: &'static str> BaseAddress<PREFIX> {
    pub fn from_bech32(address: &str) -> Result<Self, AddressError> {
        let (hrp, data, variant) = bech32::decode(address)?;

        // This could be removed
        // let prefix = if PREFIX == 0 {
        //     BECH_32_PREFIX_ACC_ADDR
        // } else {
        //     BECH_32_PREFIX_VAL_ADDR
        // };


        if hrp != PREFIX {
            return Err(AddressError::InvalidPrefix {
                expected: PREFIX.into(),
                found: hrp,
            });
        };

I guess there's two questions:

  1. Do we prefer the future const generic method using stror the newtype method?
  2. In the meantime which method should we use?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub type AccAddress = BaseAddress<BECH_32_PREFIX_ACC_ADDR>;
pub type ValAddress = BaseAddress<BECH_32_PREFIX_VAL_ADDR>;

Looks much better. I didn't think about it.

  1. Depends on how quick this feature will arrive and MSV policy.
  2. Is it really important to use str over [u8]? If yes, then newtype

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should use const generics even though we are forced to use a u8

@joneskm joneskm merged commit 211c9ab into main Feb 6, 2024
@joneskm joneskm deleted the addresses branch February 28, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants