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

xdr: get address from AccountId without panic #1999

Merged
merged 2 commits into from
Dec 3, 2019

Conversation

debnil
Copy link
Contributor

@debnil debnil commented Dec 3, 2019

What

This PR adds a method to the xdr.AccountId struct that returns the address with an error. The existing Address method is now a thin wrapper around this method.

Why

In long-running, data-intensive applications, like the Hubble project, we want to avoid panics wherever possible. A panic on a single malformed input can abort the entire pipeline. A panic-free method to get the address of an xdr.AccountId struct helps reduce such possible failures.

Known limitations

N/A

@cla-bot cla-bot bot added the cla: yes label Dec 3, 2019
Copy link
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

It looks like the new GetAddress function will still panic in some cases, and if we're making the new function an implementation that we can assume won't panic, we should return those errors too.

if aid == nil {
return ""
return "", nil
}

switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
ed := aid.MustEd25519()
Copy link
Member

@leighmcculloch leighmcculloch Dec 3, 2019

Choose a reason for hiding this comment

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

This line could still panic if the key isn't actually an Ed25519 key or is corrupted in such a way that this fails. If you want to eradicate all panics here you can replace this with GetEd25519 which will return the same result plus an ok bool.

}

switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
ed := aid.MustEd25519()
raw := make([]byte, 32)
copy(raw, ed[:])
return strkey.MustEncode(strkey.VersionByteAccountID, raw)
return strkey.MustEncode(strkey.VersionByteAccountID, raw), nil
Copy link
Member

Choose a reason for hiding this comment

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

The same with this line. strkey.Encode will get you the same behavior without a panic.

Copy link
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

👍 Looks good. It's great to offer an alternative to panics here. Thanks for taking the initiative to improve this.

I mentioned @ire-and-curses as an FYI because I feel like he might have an opinion on the naming, but I think the naming is making the best of an imperfect situation.


// GetAddress returns the strkey encoded form of this AccountId, and an error
// if the AccountId is backed by a public key of an unknown type.
func (aid *AccountId) GetAddress() (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

@ire-and-curses I know you like to think about this stuff: Ideally this would be Address and the existing function would be MustAddress, but there's no way we can do this in a backwards compatible way. Calling this GetAddress looks like the best approach here. FYI in case you're thinking about a holistic clean up of panic/non-panic function names in a future major release.

@debnil debnil merged commit d9ab7e7 into stellar:master Dec 3, 2019
@debnil debnil deleted the no-panic-address branch December 3, 2019 22:45
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