github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/02-client/genesis.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/keeper"
     8  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types"
     9  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported"
    10  )
    11  
    12  // InitGenesis initializes the ibc client submodule's state from a provided genesis
    13  // state.
    14  func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
    15  	k.SetParams(ctx, gs.Params)
    16  
    17  	// Set all client metadata first. This will allow client keeper to overwrite client and consensus state keys
    18  	// if clients accidentally write to ClientKeeper reserved keys.
    19  	if len(gs.ClientsMetadata) != 0 {
    20  		k.SetAllClientMetadata(ctx, gs.ClientsMetadata)
    21  	}
    22  
    23  	for _, client := range gs.Clients {
    24  		cs, ok := client.ClientState.GetCachedValue().(exported.ClientState)
    25  		if !ok {
    26  			panic("invalid client state")
    27  		}
    28  
    29  		if !gs.Params.IsAllowedClient(cs.ClientType()) {
    30  			panic(fmt.Sprintf("client state type %s is not registered on the allowlist", cs.ClientType()))
    31  		}
    32  
    33  		k.SetClientState(ctx, client.ClientId, cs)
    34  	}
    35  
    36  	for _, cs := range gs.ClientsConsensus {
    37  		for _, consState := range cs.ConsensusStates {
    38  			consensusState, ok := consState.ConsensusState.GetCachedValue().(exported.ConsensusState)
    39  			if !ok {
    40  				panic(fmt.Sprintf("invalid consensus state with client ID %s at height %s", cs.ClientId, consState.Height))
    41  			}
    42  
    43  			k.SetClientConsensusState(ctx, cs.ClientId, consState.Height, consensusState)
    44  		}
    45  	}
    46  
    47  	k.SetNextClientSequence(ctx, gs.NextClientSequence)
    48  
    49  	// NOTE: localhost creation is specifically disallowed for the time being.
    50  	// Issue: https://github.com/cosmos/cosmos-sdk/issues/7871
    51  }
    52  
    53  // ExportGenesis returns the ibc client submodule's exported genesis.
    54  // NOTE: CreateLocalhost should always be false on export since a
    55  // created localhost will be included in the exported clients.
    56  func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
    57  	genClients := k.GetAllGenesisClients(ctx)
    58  	clientsMetadata, err := k.GetAllClientMetadata(ctx, genClients)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	return types.GenesisState{
    63  		Clients:            genClients,
    64  		ClientsMetadata:    clientsMetadata,
    65  		ClientsConsensus:   k.GetAllConsensusStates(ctx),
    66  		Params:             k.GetParams(ctx),
    67  		CreateLocalhost:    false,
    68  		NextClientSequence: k.GetNextClientSequence(ctx),
    69  	}
    70  }