cosmos-sdk/x/ibc/core/02-client/genesis.go

70 lines
2.2 KiB
Go

package client
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/keeper"
"github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
)
// InitGenesis initializes the ibc client submodule's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
k.SetParams(ctx, gs.Params)
// Set all client metadata first. This will allow client keeper to overwrite client and consensus state keys
// if clients accidentally write to ClientKeeper reserved keys.
if len(gs.ClientsMetadata) != 0 {
k.SetAllClientMetadata(ctx, gs.ClientsMetadata)
}
for _, client := range gs.Clients {
cs, ok := client.ClientState.GetCachedValue().(exported.ClientState)
if !ok {
panic("invalid client state")
}
if !gs.Params.IsAllowedClient(cs.ClientType()) {
panic(fmt.Sprintf("client state type %s is not registered on the allowlist", cs.ClientType()))
}
k.SetClientState(ctx, client.ClientId, cs)
}
for _, cs := range gs.ClientsConsensus {
for _, consState := range cs.ConsensusStates {
consensusState, ok := consState.ConsensusState.GetCachedValue().(exported.ConsensusState)
if !ok {
panic(fmt.Sprintf("invalid consensus state with client ID %s at height %s", cs.ClientId, consState.Height))
}
k.SetClientConsensusState(ctx, cs.ClientId, consState.Height, consensusState)
}
}
k.SetNextClientSequence(ctx, gs.NextClientSequence)
// NOTE: localhost creation is specifically disallowed for the time being.
// Issue: https://github.com/cosmos/cosmos-sdk/issues/7871
}
// ExportGenesis returns the ibc client submodule's exported genesis.
// NOTE: CreateLocalhost should always be false on export since a
// created localhost will be included in the exported clients.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
genClients := k.GetAllGenesisClients(ctx)
clientsMetadata, err := k.GetAllClientMetadata(ctx, genClients)
if err != nil {
panic(err)
}
return types.GenesisState{
Clients: genClients,
ClientsMetadata: clientsMetadata,
ClientsConsensus: k.GetAllConsensusStates(ctx),
Params: k.GetParams(ctx),
CreateLocalhost: false,
}
}