2018-08-12 00:33:48 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-12 07:25:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
2020-02-19 16:04:13 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-06-05 10:42:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/exported"
|
2018-08-12 00:33:48 -07:00
|
|
|
)
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// InitGenesis initialize default parameters
|
|
|
|
// and the keeper's address to pubkey map
|
2020-06-12 07:25:12 -07:00
|
|
|
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.StakingKeeper, data types.GenesisState) {
|
2019-05-16 08:25:32 -07:00
|
|
|
stakingKeeper.IterateValidators(ctx,
|
2019-06-05 10:42:25 -07:00
|
|
|
func(index int64, validator exported.ValidatorI) bool {
|
2019-07-19 06:43:38 -07:00
|
|
|
keeper.AddPubkey(ctx, validator.GetConsPubKey())
|
2019-05-16 08:25:32 -07:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
for addr, info := range data.SigningInfos {
|
|
|
|
address, err := sdk.ConsAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-11-26 04:21:23 -08:00
|
|
|
keeper.SetValidatorSigningInfo(ctx, address, info)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for addr, array := range data.MissedBlocks {
|
|
|
|
address, err := sdk.ConsAddressFromBech32(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, missed := range array {
|
2019-07-19 06:43:38 -07:00
|
|
|
keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
keeper.SetParams(ctx, data.Params)
|
2018-08-12 00:33:48 -07:00
|
|
|
}
|
2018-11-08 16:28:28 -08:00
|
|
|
|
|
|
|
// ExportGenesis writes the current store values
|
|
|
|
// to a genesis file, which can be imported again
|
|
|
|
// with InitGenesis
|
2020-06-12 07:25:12 -07:00
|
|
|
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data types.GenesisState) {
|
2019-07-19 06:43:38 -07:00
|
|
|
params := keeper.GetParams(ctx)
|
2019-06-05 16:26:17 -07:00
|
|
|
signingInfos := make(map[string]types.ValidatorSigningInfo)
|
|
|
|
missedBlocks := make(map[string][]types.MissedBlock)
|
|
|
|
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
|
2018-11-08 16:28:28 -08:00
|
|
|
bechAddr := address.String()
|
|
|
|
signingInfos[bechAddr] = info
|
2019-06-05 16:26:17 -07:00
|
|
|
localMissedBlocks := []types.MissedBlock{}
|
2018-11-08 16:28:28 -08:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
keeper.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) {
|
2019-07-19 06:43:38 -07:00
|
|
|
localMissedBlocks = append(localMissedBlocks, types.NewMissedBlock(index, missed))
|
2018-11-08 16:28:28 -08:00
|
|
|
return false
|
|
|
|
})
|
2018-11-09 07:26:08 -08:00
|
|
|
missedBlocks[bechAddr] = localMissedBlocks
|
2018-11-08 16:28:28 -08:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
return types.NewGenesisState(params, signingInfos, missedBlocks)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|