-
Notifications
You must be signed in to change notification settings - Fork 625
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
feat: add helper function for parsing clientID from client state path #2820
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,40 @@ func ParseIdentifier(identifier, prefix string) (uint64, error) { | |
return sequence, nil | ||
} | ||
|
||
// MustParseClientStatePath returns the client ID from a client state path. It panics | ||
// if the provided path is invalid or if the clientID is empty. | ||
func MustParseClientStatePath(path string) string { | ||
clientID, err := parseClientStatePath(path) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return clientID | ||
} | ||
|
||
// parseClientStatePath returns the client ID from a client state path. It returns | ||
// an error if the provided path is invalid. | ||
func parseClientStatePath(path string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I debated whether to make this public or not. I opted for only making |
||
split := strings.Split(path, "/") | ||
if len(split) != 3 { | ||
return "", sdkerrors.Wrapf(ErrInvalidPath, "cannot parse client state path %s", path) | ||
} | ||
|
||
if split[0] != string(KeyClientStorePrefix) { | ||
return "", sdkerrors.Wrapf(ErrInvalidPath, "path does not begin with client store prefix: expected %s, got %s", KeyClientStorePrefix, split[0]) | ||
} | ||
|
||
if split[2] != KeyClientState { | ||
return "", sdkerrors.Wrapf(ErrInvalidPath, "path does not end with client state key: expected %s, got %s", KeyClientState, split[2]) | ||
} | ||
|
||
if strings.TrimSpace(split[1]) == "" { | ||
return "", sdkerrors.Wrap(ErrInvalidPath, "clientID is empty") | ||
} | ||
|
||
return split[1], nil | ||
} | ||
|
||
// ParseConnectionPath returns the connection ID from a full path. It returns | ||
// an error if the provided path is invalid. | ||
func ParseConnectionPath(path string) (string, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this ever be hit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for consensus state keys and metadata keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since client state key value is at the end of the key, you cannot prefix iterate on them