github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/genesis.go (about)

     1  package slashing
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	"github.com/Finschia/finschia-sdk/x/slashing/keeper"
     6  	"github.com/Finschia/finschia-sdk/x/slashing/types"
     7  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
     8  )
     9  
    10  // InitGenesis initialize default parameters
    11  // and the keeper's address to pubkey map
    12  func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.StakingKeeper, data *types.GenesisState) {
    13  	stakingKeeper.IterateValidators(ctx,
    14  		func(index int64, validator stakingtypes.ValidatorI) bool {
    15  			consPk, err := validator.ConsPubKey()
    16  			if err != nil {
    17  				panic(err)
    18  			}
    19  			if err = keeper.AddPubkey(ctx, consPk); err != nil {
    20  				panic(err)
    21  			}
    22  			return false
    23  		},
    24  	)
    25  
    26  	for _, info := range data.SigningInfos {
    27  		address, err := sdk.ConsAddressFromBech32(info.Address)
    28  		if err != nil {
    29  			panic(err)
    30  		}
    31  		keeper.SetValidatorSigningInfo(ctx, address, info.ValidatorSigningInfo)
    32  	}
    33  
    34  	for _, array := range data.MissedBlocks {
    35  		address, err := sdk.ConsAddressFromBech32(array.Address)
    36  		if err != nil {
    37  			panic(err)
    38  		}
    39  		for _, missed := range array.MissedBlocks {
    40  			keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
    41  		}
    42  	}
    43  
    44  	keeper.SetParams(ctx, data.Params)
    45  }
    46  
    47  // ExportGenesis writes the current store values
    48  // to a genesis file, which can be imported again
    49  // with InitGenesis
    50  func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data *types.GenesisState) {
    51  	params := keeper.GetParams(ctx)
    52  	signingInfos := make([]types.SigningInfo, 0)
    53  	missedBlocks := make([]types.ValidatorMissedBlocks, 0)
    54  	keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
    55  		bechAddr := address.String()
    56  		signingInfos = append(signingInfos, types.SigningInfo{
    57  			Address:              bechAddr,
    58  			ValidatorSigningInfo: info,
    59  		})
    60  
    61  		localMissedBlocks := keeper.GetValidatorMissedBlocks(ctx, address)
    62  
    63  		missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
    64  			Address:      bechAddr,
    65  			MissedBlocks: localMissedBlocks,
    66  		})
    67  
    68  		return false
    69  	})
    70  
    71  	return types.NewGenesisState(params, signingInfos, missedBlocks)
    72  }