Skip to content

Commit 788cc7c

Browse files
authored
refactor: rename IterateClients to IterateClientStates, add a prefix (#2856)
feat(api)!: rename `IterateClients` to `IterateClientStates`. The function now takes a prefix argument which may be used for prefix iteration over the client store. feat: add `PrefixedClientStorePath` and `PrefixedClientStoreKey` functions to 24-host
1 parent b28cc47 commit 788cc7c

File tree

6 files changed

+90
-8
lines changed

6 files changed

+90
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242

4343
### API Breaking
4444

45+
* (core/02-client) [\#2856](https://github.com/cosmos/ibc-go/pull/2856) Rename `IterateClients` to `IterateClientStates`. The function now takes a prefix argument which may be used for prefix iteration over the client store.
4546
* (apps/27-interchain-accounts) [\#2607](https://github.com/cosmos/ibc-go/pull/2607) `SerializeCosmosTx` now takes in a `[]proto.Message` instead of `[]sdk.Msg`.
4647
* (apps/transfer) [\#2446](https://github.com/cosmos/ibc-go/pull/2446) Remove `SendTransfer` function in favor of a private `sendTransfer` function. All IBC transfers must be initiated with `MsgTransfer`.
4748
* (apps/29-fee) [\#2395](https://github.com/cosmos/ibc-go/pull/2395) Remove param space from ics29 NewKeeper function. The field was unused.
@@ -115,6 +116,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
115116

116117
### Features
117118

119+
* (core/24-host) [\#2856](https://github.com/cosmos/ibc-go/pull/2856) Add `PrefixedClientStorePath` and `PrefixedClientStoreKey` functions to 24-host
118120
* (core/02-client) [\#2819](https://github.com/cosmos/ibc-go/pull/2819) Add automatic in-place store migrations to remove the localhost client and migrate existing solo machine definitions.
119121
* (light-clients/06-solomachine) [\#2826](https://github.com/cosmos/ibc-go/pull/2826) Add `AppModuleBasic` for the 06-solomachine client and remove solo machine type registration from core IBC. Chains must register the `AppModuleBasic` of light clients.
120122
* (light-clients/07-tendermint) [\#2825](https://github.com/cosmos/ibc-go/pull/2825) Add `AppModuleBasic` for the 07-tendermint client and remove tendermint type registration from core IBC. Chains must register the `AppModuleBasic` of light clients.

modules/core/02-client/keeper/keeper.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string,
147147
// GetAllGenesisClients returns all the clients in state with their client ids returned as IdentifiedClientState
148148
func (k Keeper) GetAllGenesisClients(ctx sdk.Context) types.IdentifiedClientStates {
149149
var genClients types.IdentifiedClientStates
150-
k.IterateClients(ctx, func(clientID string, cs exported.ClientState) bool {
150+
k.IterateClientStates(ctx, nil, func(clientID string, cs exported.ClientState) bool {
151151
genClients = append(genClients, types.NewIdentifiedClientState(clientID, cs))
152152
return false
153153
})
@@ -350,12 +350,12 @@ func (k Keeper) SetUpgradedConsensusState(ctx sdk.Context, planHeight int64, bz
350350
return k.upgradeKeeper.SetUpgradedConsensusState(ctx, planHeight, bz)
351351
}
352352

353-
// IterateClients provides an iterator over all stored light client State
353+
// IterateClientStates provides an iterator over all stored light client State
354354
// objects. For each State object, cb will be called. If the cb returns true,
355355
// the iterator will close and stop.
356-
func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs exported.ClientState) bool) {
356+
func (k Keeper) IterateClientStates(ctx sdk.Context, prefix []byte, cb func(clientID string, cs exported.ClientState) bool) {
357357
store := ctx.KVStore(k.storeKey)
358-
iterator := sdk.KVStorePrefixIterator(store, host.KeyClientStorePrefix)
358+
iterator := sdk.KVStorePrefixIterator(store, host.PrefixedClientStoreKey(prefix))
359359

360360
defer iterator.Close()
361361
for ; iterator.Valid(); iterator.Next() {
@@ -375,11 +375,13 @@ func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs expo
375375
}
376376

377377
// GetAllClients returns all stored light client State objects.
378-
func (k Keeper) GetAllClients(ctx sdk.Context) (states []exported.ClientState) {
379-
k.IterateClients(ctx, func(_ string, state exported.ClientState) bool {
378+
func (k Keeper) GetAllClients(ctx sdk.Context) []exported.ClientState {
379+
var states []exported.ClientState
380+
k.IterateClientStates(ctx, nil, func(_ string, state exported.ClientState) bool {
380381
states = append(states, state)
381382
return false
382383
})
384+
383385
return states
384386
}
385387

modules/core/02-client/keeper/keeper_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,67 @@ func (suite KeeperTestSuite) TestGetAllConsensusStates() {
386386
consStates := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetAllConsensusStates(suite.chainA.GetContext())
387387
suite.Require().Equal(expConsensusStates, consStates, "%s \n\n%s", expConsensusStates, consStates)
388388
}
389+
390+
func (suite KeeperTestSuite) TestIterateClientStates() {
391+
paths := []*ibctesting.Path{
392+
ibctesting.NewPath(suite.chainA, suite.chainB),
393+
ibctesting.NewPath(suite.chainA, suite.chainB),
394+
ibctesting.NewPath(suite.chainA, suite.chainB),
395+
}
396+
397+
solomachines := []*ibctesting.Solomachine{
398+
ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-0", "testing", 1),
399+
ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "06-solomachine-1", "testing", 4),
400+
}
401+
402+
var (
403+
expTMClientIDs = make([]string, len(paths))
404+
expSMClientIDs = make([]string, len(solomachines))
405+
)
406+
407+
// create tendermint clients
408+
for i, path := range paths {
409+
suite.coordinator.SetupClients(path)
410+
expTMClientIDs[i] = path.EndpointA.ClientID
411+
}
412+
413+
// create solomachine clients
414+
for i, sm := range solomachines {
415+
expSMClientIDs[i] = sm.CreateClient(suite.chainA)
416+
}
417+
418+
testCases := []struct {
419+
name string
420+
prefix []byte
421+
expClientIDs []string
422+
}{
423+
{
424+
"all clientIDs",
425+
nil,
426+
append(expSMClientIDs, expTMClientIDs...),
427+
},
428+
{
429+
"tendermint clientIDs",
430+
[]byte(exported.Tendermint),
431+
expTMClientIDs,
432+
},
433+
{
434+
"solo machine clientIDs",
435+
[]byte(exported.Solomachine),
436+
expSMClientIDs,
437+
},
438+
}
439+
440+
for _, tc := range testCases {
441+
tc := tc
442+
suite.Run(tc.name, func() {
443+
var clientIDs []string
444+
suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.IterateClientStates(suite.chainA.GetContext(), tc.prefix, func(clientID string, _ exported.ClientState) bool {
445+
clientIDs = append(clientIDs, clientID)
446+
return false
447+
})
448+
449+
suite.Require().Equal(tc.expClientIDs, clientIDs)
450+
})
451+
}
452+
}

modules/core/03-connection/keeper/keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (k Keeper) SetNextConnectionSequence(ctx sdk.Context, sequence uint64) {
146146
// no paths are stored.
147147
func (k Keeper) GetAllClientConnectionPaths(ctx sdk.Context) []types.ConnectionPaths {
148148
var allConnectionPaths []types.ConnectionPaths
149-
k.clientKeeper.IterateClients(ctx, func(clientID string, cs exported.ClientState) bool {
149+
k.clientKeeper.IterateClientStates(ctx, nil, func(clientID string, cs exported.ClientState) bool {
150150
paths, found := k.GetClientConnectionPaths(ctx, clientID)
151151
if !found {
152152
// continue when connection handshake is not initialized

modules/core/03-connection/types/expected_keepers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ type ClientKeeper interface {
1212
GetClientConsensusState(ctx sdk.Context, clientID string, height exported.Height) (exported.ConsensusState, bool)
1313
GetSelfConsensusState(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error)
1414
ValidateSelfClient(ctx sdk.Context, clientState exported.ClientState) error
15-
IterateClients(ctx sdk.Context, cb func(string, exported.ClientState) bool)
15+
IterateClientStates(ctx sdk.Context, prefix []byte, cb func(string, exported.ClientState) bool)
1616
ClientStore(ctx sdk.Context, clientID string) sdk.KVStore
1717
}

modules/core/24-host/keys.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ func FullClientKey(clientID string, path []byte) []byte {
5555
return []byte(FullClientPath(clientID, string(path)))
5656
}
5757

58+
// PrefixedClientStorePath returns a key path which can be used for prefixed
59+
// key store iteration. The prefix may be a clientType, clientID, or any
60+
// valid key prefix which may be concatenated with the client store constant.
61+
func PrefixedClientStorePath(prefix []byte) string {
62+
return fmt.Sprintf("%s/%s", KeyClientStorePrefix, prefix)
63+
}
64+
65+
// PrefixedClientStoreKey returns a key which can be used for prefixed
66+
// key store iteration. The prefix may be a clientType, clientID, or any
67+
// valid key prefix which may be concatenated with the client store constant.
68+
func PrefixedClientStoreKey(prefix []byte) []byte {
69+
return []byte(PrefixedClientStorePath(prefix))
70+
}
71+
5872
// ICS02
5973
// 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
6074

0 commit comments

Comments
 (0)