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 StringToKnownChainID function for VAAs #4239

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

Conversation

johnsaigle
Copy link
Contributor

@johnsaigle johnsaigle commented Jan 23, 2025

Adds a function to the SDK's vaa structs that parses known ChainIDs from string representations of ints or chain names.

I've found myself wishing that this function exists in the context of working with ChainIDs. It's nice to have this parsing and validation provided by the SDK.

@johnsaigle johnsaigle added enhancement New feature or request sdk labels Jan 23, 2025
@johnsaigle johnsaigle changed the title sdk: add Atoi function for ChainID sdk: add Atoi function for ChainID (parsing ChainID from string representations of integers) Jan 23, 2025
@johnsaigle johnsaigle force-pushed the vaa-atoi branch 3 times, most recently from 09cb5b4 to dc21be2 Compare January 23, 2025 23:17
@johnsaigle johnsaigle marked this pull request as draft January 23, 2025 23:18
@johnsaigle johnsaigle marked this pull request as ready for review January 24, 2025 14:52
bruce-riley
bruce-riley previously approved these changes Jan 24, 2025
}
}
if !found {
err = fmt.Errorf("value %s is not a valid chainId", s)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth doing a special check for id == 0 and returning ChainIDUnset?

Copy link
Contributor Author

@johnsaigle johnsaigle Jan 24, 2025

Choose a reason for hiding this comment

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

Curious about your thoughts on that actually. I originally had the function set up to do just that, but then since I saw that GetAllNetworkIDs() seemed to specifically omit ChainIDUnset, I figured I'd follow what the existing code was doing. Maybe it's not a problem if they have different behaviours though?

Copy link
Contributor

@evan-gray evan-gray Jan 27, 2025

Choose a reason for hiding this comment

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

It depends on the use case. chain ID 0 / unset is used for governance. so if you're using this to parse something that comes from the payload of real VAAs, then it's possible (though I'm not sure why you would have that as a string). If this is more for splitting a VAA ID or something, there should never be a VAA emitted with chain ID 0 as far as I am aware, which is similarly why 0 is not a "Network ID" for GetAllNetworkIDs. I guess I'm not sure of the use case that led to wishing for this 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Follow up - what is the case where you have a chain ID (for some reason) but should be told it is invalid? Any uint16 is technically valid as a chain ID, so anything that passes the first line shouldn't necessarily trigger an error. Otherwise the caller would need to update their code any time there is a new chain added, which quickly becomes a nightmare. Maybe that's why I would lean on the side of continuing to not have this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @evan-gray thanks for the review and for the questions. Essentially my motivation here is to get more use out of the ChainID type and wanted a way to go from a string to a valid, registered ChainID within Wormhole. Based on your questions I'm realizing that I was thinking of this code as a "wormhole SDK" rather than a "vaa SDK" and that's perhaps stretching the purpose of the code.

I think you're right that any uint16 is a valid ChainID in a sense (even if it doesn't correspond to any actual instantiated chain); for that reason Atoi is not a good name here because that sort of generic conversion should accept any uint16, I feel.

Would you be open to including this function if renamed/redocumented to something like "ValidChainIDFromString"? I would personally find this function useful but if in your view this is outside of the scope of this file then I'll defer to your judgement and close the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps “known” would be a better adjective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about KnownNetworkIDFromString()? That way we surface that it's using the NetworkID list under the hood and it becomes more clear that 0/ChainIDUnset is not a valid use of the function. In this way also it should have the same maintenance requirements/constraints that GetAllNetworkIDs has.

Copy link
Contributor

Choose a reason for hiding this comment

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

I’d be okay with that as long as it has a doc string describing what (not) to use it for. Not 100% why they’re called NetworkID and not just ChainID there though. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK awesome, happy to add that documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've renamed the function, added documentation, and changed the name of this PR and its description.

@johnsaigle johnsaigle self-assigned this Feb 13, 2025
@johnsaigle johnsaigle marked this pull request as draft February 13, 2025 21:46
@johnsaigle johnsaigle changed the title sdk: add Atoi function for ChainID (parsing ChainID from string representations of integers) sdk: add StringToKnownChainID function for VAAs Feb 20, 2025
@johnsaigle johnsaigle marked this pull request as ready for review February 20, 2025 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sdk
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants