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

sdk: Add functions for type-safe conversions from integers to ChainID #4271

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

johnsaigle
Copy link
Contributor

@johnsaigle johnsaigle commented Feb 20, 2025

In many places in the Guardian, the following is done:

// equivalent to `cid := uint16(x)`
cid := vaa.ChainID(x)

As ChainID() is just a custom type around uint16, this is equivalent to doing an unchecked type-conversion to uint16, which could result in edge-case truncation/overflow/sign-change issues.

The motivation for this change is to provide a clear way to go from any integer type for a ChainID in a type-safe way. With this PR, someone could instead write the following:

// just type-safe
cid, err := vaa.ChainIDFromNumber(x)
// type-safe + corresponds to a real chain
cid, err := vaa.KnownChainIDFromNumber(x)

If the general idea seems good, I will merge this work with #4239 in the future so that the function introduced in that PR will use KnownChainIDFromNumber in the numeric string context.

@johnsaigle johnsaigle added the enhancement New feature or request label Feb 20, 2025
@johnsaigle johnsaigle marked this pull request as ready for review February 20, 2025 17:07
@johnsaigle johnsaigle marked this pull request as draft February 21, 2025 17:18
@johnsaigle johnsaigle marked this pull request as ready for review February 21, 2025 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant