github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/slashing/genesis.go (about) 1 package slashing 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 "github.com/fibonacci-chain/fbc/x/slashing/internal/types" 6 "github.com/fibonacci-chain/fbc/x/staking/exported" 7 ) 8 9 // InitGenesis initialize default parameters 10 // and the keeper's address to pubkey map 11 func InitGenesis(ctx sdk.Context, keeper Keeper, stakingKeeper types.StakingKeeper, data types.GenesisState) { 12 stakingKeeper.IterateValidators(ctx, 13 func(index int64, validator exported.ValidatorI) bool { 14 keeper.AddPubkey(ctx, validator.GetConsPubKey()) 15 return false 16 }, 17 ) 18 19 for addr, info := range data.SigningInfos { 20 address, err := sdk.ConsAddressFromBech32(addr) 21 if err != nil { 22 panic(err) 23 } 24 keeper.SetValidatorSigningInfo(ctx, address, info) 25 } 26 27 for addr, array := range data.MissedBlocks { 28 address, err := sdk.ConsAddressFromBech32(addr) 29 if err != nil { 30 panic(err) 31 } 32 for _, missed := range array { 33 keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed) 34 } 35 } 36 37 keeper.SetParams(ctx, data.Params) 38 } 39 40 // ExportGenesis writes the current store values 41 // to a genesis file, which can be imported again 42 // with InitGenesis 43 func ExportGenesis(ctx sdk.Context, keeper Keeper) (data types.GenesisState) { 44 params := keeper.GetParams(ctx) 45 signingInfos := make(map[string]types.ValidatorSigningInfo) 46 missedBlocks := make(map[string][]types.MissedBlock) 47 keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) { 48 bechAddr := address.String() 49 signingInfos[bechAddr] = info 50 localMissedBlocks := []types.MissedBlock{} 51 52 keeper.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) { 53 localMissedBlocks = append(localMissedBlocks, types.NewMissedBlock(index, missed)) 54 return false 55 }) 56 missedBlocks[bechAddr] = localMissedBlocks 57 58 return false 59 }) 60 61 return types.NewGenesisState(params, signingInfos, missedBlocks) 62 }