-
Notifications
You must be signed in to change notification settings - Fork 639
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: use IterateClientStates in migration code #2857
Changes from all commits
f638802
682457c
cce6636
227759b
2b26ecf
6529b13
de3389b
e27a3a0
3794d3a
f4b1fb1
6896367
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package v7 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/cosmos/ibc-go/v6/modules/core/exported" | ||
) | ||
|
||
// ClientKeeper expected IBC client keeper | ||
type ClientKeeper interface { | ||
GetClientState(ctx sdk.Context, clientID string) (exported.ClientState, bool) | ||
SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState) | ||
IterateClientStates(ctx sdk.Context, prefix []byte, cb func(string, exported.ClientState) bool) | ||
ClientStore(ctx sdk.Context, clientID string) sdk.KVStore | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tendermint | ||
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 file is only for migrations.go. I wonder if this warrants making a subdirectory |
||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
|
||
"github.com/cosmos/ibc-go/v6/modules/core/exported" | ||
) | ||
|
||
// ClientKeeper expected account IBC client keeper | ||
type ClientKeeper interface { | ||
GetClientState(ctx sdk.Context, clientID string) (exported.ClientState, bool) | ||
IterateClientStates(ctx sdk.Context, prefix []byte, cb func(string, exported.ClientState) bool) | ||
ClientStore(ctx sdk.Context, clientID string) sdk.KVStore | ||
Logger(ctx sdk.Context) log.Logger | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,33 @@ | ||
package tendermint | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" | ||
host "github.com/cosmos/ibc-go/v6/modules/core/24-host" | ||
"github.com/cosmos/ibc-go/v6/modules/core/exported" | ||
) | ||
|
||
// PruneTendermintConsensusStates prunes all expired tendermint consensus states. This function | ||
// may optionally be called during in-place store migrations. The ibc store key must be provided. | ||
func PruneTendermintConsensusStates(ctx sdk.Context, cdc codec.BinaryCodec, storeKey storetypes.StoreKey) error { | ||
store := ctx.KVStore(storeKey) | ||
|
||
// iterate over ibc store with prefix: clients/07-tendermint, | ||
tendermintClientPrefix := []byte(fmt.Sprintf("%s/%s", host.KeyClientStorePrefix, exported.Tendermint)) | ||
iterator := sdk.KVStorePrefixIterator(store, tendermintClientPrefix) | ||
|
||
func PruneTendermintConsensusStates(ctx sdk.Context, cdc codec.BinaryCodec, clientKeeper ClientKeeper) error { | ||
var clientIDs []string | ||
|
||
// collect all clients to avoid performing store state changes during iteration | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
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. :) |
||
path := string(iterator.Key()) | ||
if !strings.Contains(path, host.KeyClientState) { | ||
// skip non client state keys | ||
continue | ||
} | ||
|
||
clientID := host.MustParseClientStatePath(path) | ||
clientKeeper.IterateClientStates(ctx, []byte(exported.Tendermint), func(clientID string, _ exported.ClientState) bool { | ||
clientIDs = append(clientIDs, clientID) | ||
} | ||
return false | ||
}) | ||
|
||
// keep track of the total consensus states pruned so chains can | ||
// understand how much space is saved when the migration is run | ||
var totalPruned int | ||
|
||
for _, clientID := range clientIDs { | ||
clientPrefix := []byte(fmt.Sprintf("%s/%s/", host.KeyClientStorePrefix, clientID)) | ||
clientStore := prefix.NewStore(ctx.KVStore(storeKey), clientPrefix) | ||
clientStore := clientKeeper.ClientStore(ctx, clientID) | ||
|
||
bz := clientStore.Get(host.ClientStateKey()) | ||
if bz == nil { | ||
return clienttypes.ErrClientNotFound | ||
} | ||
|
||
var clientState exported.ClientState | ||
if err := cdc.UnmarshalInterface(bz, &clientState); err != nil { | ||
return sdkerrors.Wrap(err, "failed to unmarshal client state bytes into tendermint client state") | ||
clientState, ok := clientKeeper.GetClientState(ctx, clientID) | ||
if !ok { | ||
return sdkerrors.Wrapf(clienttypes.ErrClientNotFound, "clientID %s", clientID) | ||
} | ||
|
||
tmClientState, ok := clientState.(*ClientState) | ||
|
@@ -65,7 +38,7 @@ func PruneTendermintConsensusStates(ctx sdk.Context, cdc codec.BinaryCodec, stor | |
totalPruned += PruneAllExpiredConsensusStates(ctx, clientStore, cdc, tmClientState) | ||
} | ||
|
||
clientLogger := ctx.Logger().With("module", "x/"+host.ModuleName+"/"+clienttypes.SubModuleName) | ||
clientLogger := clientKeeper.Logger(ctx) | ||
clientLogger.Info("pruned expired tendermint consensus states", "total", totalPruned) | ||
|
||
return nil | ||
|
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.
This function can be improved if we add a prefix to the
IterateConsensusStates
function