forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify optional tendermint pruning migrations (cosmos#2862)
- Loading branch information
1 parent
2728e7d
commit 0dc8d1d
Showing
7 changed files
with
101 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
modules/light-clients/07-tendermint/migrations/expected_keepers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package migrations | ||
|
||
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 | ||
} |
46 changes: 46 additions & 0 deletions
46
modules/light-clients/07-tendermint/migrations/migrations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
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" | ||
"github.com/cosmos/ibc-go/v6/modules/core/exported" | ||
ibctm "github.com/cosmos/ibc-go/v6/modules/light-clients/07-tendermint" | ||
) | ||
|
||
// PruneExpiredConsensusStates 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 PruneExpiredConsensusStates(ctx sdk.Context, cdc codec.BinaryCodec, clientKeeper ClientKeeper) (int, error) { | ||
var clientIDs []string | ||
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 { | ||
clientStore := clientKeeper.ClientStore(ctx, clientID) | ||
|
||
clientState, ok := clientKeeper.GetClientState(ctx, clientID) | ||
if !ok { | ||
return 0, sdkerrors.Wrapf(clienttypes.ErrClientNotFound, "clientID %s", clientID) | ||
} | ||
|
||
tmClientState, ok := clientState.(*ibctm.ClientState) | ||
if !ok { | ||
return 0, sdkerrors.Wrap(clienttypes.ErrInvalidClient, "client state is not tendermint even though client id contains 07-tendermint") | ||
} | ||
|
||
totalPruned += ibctm.PruneAllExpiredConsensusStates(ctx, clientStore, cdc, tmClientState) | ||
} | ||
|
||
clientLogger := clientKeeper.Logger(ctx) | ||
clientLogger.Info("pruned expired tendermint consensus states", "total", totalPruned) | ||
|
||
return totalPruned, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters