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