/
migrations.go
46 lines (36 loc) · 1.71 KB
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v7/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
}