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

Collin/chain name enum #18

Merged
merged 8 commits into from
Mar 22, 2022
Merged

Collin/chain name enum #18

merged 8 commits into from
Mar 22, 2022

Conversation

cbrit
Copy link
Member

@cbrit cbrit commented Mar 8, 2022

No description provided.

@cbrit cbrit requested a review from philipjames44 March 8, 2022 22:24
@cbrit cbrit self-assigned this Mar 8, 2022
@cbrit cbrit force-pushed the collin/chain-name-enum branch from 758c7ae to e20c132 Compare March 9, 2022 19:55
Copy link
Contributor

@philipjames44 philipjames44 left a comment

Choose a reason for hiding this comment

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

Not necessarily requesting changes, just some points to discuss potentially.

ocular/src/chain/client.rs Outdated Show resolved Hide resolved
ocular/src/chain.rs Outdated Show resolved Hide resolved
@tony-iqlusion
Copy link

tony-iqlusion commented Mar 9, 2022

A slightly different way to approach this would be to have a type which provides a const fn constructor which validates the chain ID, which would allow you to have a single type where you provide a number of constants for "well known" chains, but anyone else could bind their own constants for chains not in the registry. Something like this:

impl ChainId {
    const AGORIC: ChainId = ChainId::new("agoric");
    const AKASH: ChainId = ChainId::new("akash");
    const ARKH: ChainId = ChainId::new("arkh");
    [...]
}

Using inherent constants the syntax is nearly identical to an enum: ChainId::AGORIC vs Chains::Agoric in this PR. It gives you convenience of a registry but in a way that's extensible so anyone else can define their own chain ID. And if any chain IDs are added to the registry later, the value is compatible with what a user might define themselves.

https://github.com/iqlusioninc/crates/blob/main/bip32/src/prefix.rs

I'd otherwise recommend using tendermint::chain::Id for this, but it lacks the sort of const fn support I just described which would allow you to create a registry of chain ID constants.

I can open a PR upstream for that if this sounds interesting.

@philipjames44
Copy link
Contributor

philipjames44 commented Mar 9, 2022

A slightly different way to approach this would be to have a type which provides a const fn constructor which validates the chain ID, which would allow you to have a single type where you provide a number of constants for "well known" chains, but anyone else could bind their own constants for chains not in the registry. Something like this:

impl ChainId {
    const AGORIC: ChainId = ChainId::new("agoric");
    const AKASH: ChainId = ChainId::new("akash");
    const ARKH: ChainId = ChainId::new("arkh");
    [...]
}

Using inherent constants the syntax is nearly identical to an enum: ChainId::AGORIC vs Chains::Agoric in this PR. It gives you convenience of a registry but in a way that's extensible so anyone else can define their own chain ID. And if any chain IDs are added to the registry later, the value is compatible with what a user might define themselves.

https://github.com/iqlusioninc/crates/blob/main/bip32/src/prefix.rs

I'd otherwise recommend using tendermint::chain::Id for this, but it lacks the sort of const fn support I just described which would allow you to create a registry of chain ID constants.

I can open a PR upstream for that if this sounds interesting.

'+ 1' on the const fn's

I think that sounds pretty good.

@philipjames44 philipjames44 mentioned this pull request Mar 16, 2022
tony-iqlusion added a commit to informalsystems/tendermint-rs that referenced this pull request Mar 16, 2022
In PeggyJV/ocular#18 we are trying to create a registry of "well-known"
chain IDs. I proposed using `tendermint::chain::Id` for this, but to do
so we'd need to have a const initializer support.

This commit changes the internal representation of `chain::Id` to
`Cow<'static, str>`. This permits a `const fn` initializer, but avoids
adding a lifetime to `chain::Id` itself.

It also adds `chain::Id::new` which is defined as a `const fn`, which
can be used to define chain ID constants.
@tony-iqlusion
Copy link

I opened a PR to add a const fn constructor to tendermint::chain::Id: informalsystems/tendermint-rs#1105

tony-iqlusion added a commit to informalsystems/tendermint-rs that referenced this pull request Mar 16, 2022
In PeggyJV/ocular#18 we are trying to create a registry of "well-known"
chain IDs. I proposed using `tendermint::chain::Id` for this, but to do
so we'd need to have a const initializer support.

This commit changes the internal representation of `chain::Id` to
`Cow<'static, str>`. This permits a `const fn` initializer, but avoids
adding a lifetime to `chain::Id` itself.

It also adds `chain::Id::new` which is defined as a `const fn`, which
can be used to define chain ID constants.
tony-iqlusion added a commit to informalsystems/tendermint-rs that referenced this pull request Mar 17, 2022
In PeggyJV/ocular#18 we are trying to create a registry of "well-known"
chain IDs. I proposed using `tendermint::chain::Id` for this, but to do
so we'd need to have a const initializer support.

This commit changes the internal representation of `chain::Id` to
`Cow<'static, str>`. This permits a `const fn` initializer, but avoids
adding a lifetime to `chain::Id` itself.

It also adds `chain::Id::new` which is defined as a `const fn`, which
can be used to define chain ID constants.
@adizere
Copy link
Collaborator

adizere commented Mar 17, 2022

A slightly different way to approach this would be to have a type which provides a const fn constructor which validates the chain ID, which would allow you to have a single type where you provide a number of constants for "well known" chains, but anyone else could bind their own constants for chains not in the registry.

+1, neat solution. I'd expect chains to change relatively fast, cadence of say ~1/month, so I can imagine binding new constants while 99% of time using the well-known definitions.

If we can rely on tendermint::chain::Id that would be even better.

tony-iqlusion added a commit to informalsystems/tendermint-rs that referenced this pull request Mar 17, 2022
In PeggyJV/ocular#18 we are trying to create a registry of "well-known"
chain IDs. I proposed using `tendermint::chain::Id` for this, but to do
so we'd need to have a const initializer support.

This commit changes the internal representation of `chain::Id` to
`Cow<'static, str>`. This permits a `const fn` initializer, but avoids
adding a lifetime to `chain::Id` itself.

It also adds `chain::Id::new` which is defined as a `const fn`, which
can be used to define chain ID constants.
thanethomson pushed a commit to informalsystems/tendermint-rs that referenced this pull request Mar 17, 2022
In PeggyJV/ocular#18 we are trying to create a registry of "well-known"
chain IDs. I proposed using `tendermint::chain::Id` for this, but to do
so we'd need to have a const initializer support.

This commit changes the internal representation of `chain::Id` to
`Cow<'static, str>`. This permits a `const fn` initializer, but avoids
adding a lifetime to `chain::Id` itself.

It also adds `chain::Id::new` which is defined as a `const fn`, which
can be used to define chain ID constants.
@@ -2,3 +2,58 @@ pub mod client;
pub mod config;
pub mod info;
pub mod registry;

pub type ChainName = tendermint::chain::Id;

Choose a reason for hiding this comment

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

Why not just call this ChainId to match the tendermint name?

Also note that prefixing a type with the module it's contained in is non-idiomatic:

https://rust-lang.github.io/rfcs/0356-no-module-prefixes.html

I would suggest just re-exporting tendermint::chain::Id here:

Suggested change
pub type ChainName = tendermint::chain::Id;
pub use tendermint::chain::Id;

Copy link
Member Author

@cbrit cbrit Mar 19, 2022

Choose a reason for hiding this comment

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

My concern is that referring to the reference name as an ID will be conflating the chain-id in the registry with the chain name, thus the type alias and rename to make it match the field in the registry chains.json, rather than to prefix it with the module name. That's just a coincidence. Does that change your opinion?

@cbrit cbrit merged commit d428669 into main Mar 22, 2022
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.

4 participants