-
Notifications
You must be signed in to change notification settings - Fork 642
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
refactor: rename IterateClients to IterateClientStates, add a prefix #2856
Changes from all commits
7b3fa48
e391ec9
fd09362
c94f1be
89770cd
f8a946c
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 |
---|---|---|
|
@@ -147,7 +147,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string, | |
// GetAllGenesisClients returns all the clients in state with their client ids returned as IdentifiedClientState | ||
func (k Keeper) GetAllGenesisClients(ctx sdk.Context) types.IdentifiedClientStates { | ||
var genClients types.IdentifiedClientStates | ||
k.IterateClients(ctx, func(clientID string, cs exported.ClientState) bool { | ||
k.IterateClientStates(ctx, nil, func(clientID string, cs exported.ClientState) bool { | ||
genClients = append(genClients, types.NewIdentifiedClientState(clientID, cs)) | ||
return false | ||
}) | ||
|
@@ -350,12 +350,12 @@ func (k Keeper) SetUpgradedConsensusState(ctx sdk.Context, planHeight int64, bz | |
return k.upgradeKeeper.SetUpgradedConsensusState(ctx, planHeight, bz) | ||
} | ||
|
||
// IterateClients provides an iterator over all stored light client State | ||
// IterateClientStates provides an iterator over all stored light client State | ||
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. should we add a section which says that Unless we move to sentinel value in which case we can outline that. |
||
// objects. For each State object, cb will be called. If the cb returns true, | ||
// the iterator will close and stop. | ||
func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs exported.ClientState) bool) { | ||
func (k Keeper) IterateClientStates(ctx sdk.Context, prefix []byte, cb func(clientID string, cs exported.ClientState) bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, host.KeyClientStorePrefix) | ||
iterator := sdk.KVStorePrefixIterator(store, host.PrefixedClientStoreKey(prefix)) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
|
@@ -375,11 +375,13 @@ func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs expo | |
} | ||
|
||
// GetAllClients returns all stored light client State objects. | ||
func (k Keeper) GetAllClients(ctx sdk.Context) (states []exported.ClientState) { | ||
k.IterateClients(ctx, func(_ string, state exported.ClientState) bool { | ||
func (k Keeper) GetAllClients(ctx sdk.Context) []exported.ClientState { | ||
var states []exported.ClientState | ||
k.IterateClientStates(ctx, nil, func(_ string, state exported.ClientState) bool { | ||
states = append(states, state) | ||
return false | ||
}) | ||
|
||
return states | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,20 @@ func FullClientKey(clientID string, path []byte) []byte { | |
return []byte(FullClientPath(clientID, string(path))) | ||
} | ||
|
||
// PrefixedClientStorePath returns a key path which can be used for prefixed | ||
// key store iteration. The prefix may be a clientType, clientID, or any | ||
// valid key prefix which may be concatenated with the client store constant. | ||
func PrefixedClientStorePath(prefix []byte) string { | ||
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. This ends up returning Or we could be explicit and do a nil check and return 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. ^^ this was written before the suggestion for a sentinel value. 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. Would you want any differing behaviour if we used a sentinel value? |
||
return fmt.Sprintf("%s/%s", KeyClientStorePrefix, prefix) | ||
} | ||
|
||
// PrefixedClientStoreKey returns a key which can be used for prefixed | ||
// key store iteration. The prefix may be a clientType, clientID, or any | ||
// valid key prefix which may be concatenated with the client store constant. | ||
func PrefixedClientStoreKey(prefix []byte) []byte { | ||
return []byte(PrefixedClientStorePath(prefix)) | ||
} | ||
|
||
// ICS02 | ||
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#path-space | ||
|
||
|
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.
I'm not a huge fan of method signatures where explicitly passing
nil
an intended/regular workflow. Maybe this is overkill, but I think I prefer the idea of an explicit sentinel value likeAnd use this rather than
nil
. A value of nil could mean a lot of different things, with a value like this it is quite obvious what we are doing.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.
That sounds fine to me. I'm impartial to this decision. The SDK already makes use of nil with iterators so it doesn't feel out of the normal for me
Where would you place the
EmptyPrefix
, 24-host?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.
Ah I see, if it's already an existing pattern to pass nil to iterators then it probably is better to just stay consistent.
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.
Yup. I'm still happy to go with a sentinel value if we think that is cleaner. I guess we can use nil for now an update in the future?
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.
sounds good to me 👍