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"
|
2020-10-12 06:56:02 -07:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
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-08-11 07:22:30 -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,
|
2020-10-12 06:56:02 -07:00
|
|
|
func(index int64, validator stakingtypes.ValidatorI) bool {
|
2020-11-09 08:01:43 -08:00
|
|
|
consPk, err := validator.ConsPubKey()
|
2020-10-23 05:07:52 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
keeper.AddPubkey(ctx, consPk)
|
2019-05-16 08:25:32 -07:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2020-08-04 05:58:36 -07:00
|
|
|
for _, info := range data.SigningInfos {
|
|
|
|
address, err := sdk.ConsAddressFromBech32(info.Address)
|
2018-11-08 16:28:28 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-04 05:58:36 -07:00
|
|
|
keeper.SetValidatorSigningInfo(ctx, address, info.ValidatorSigningInfo)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
|
2020-08-04 05:58:36 -07:00
|
|
|
for _, array := range data.MissedBlocks {
|
|
|
|
address, err := sdk.ConsAddressFromBech32(array.Address)
|
2018-11-08 16:28:28 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-04 05:58:36 -07:00
|
|
|
for _, missed := range array.MissedBlocks {
|
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-08-11 07:22:30 -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)
|
2020-08-04 05:58:36 -07:00
|
|
|
signingInfos := make([]types.SigningInfo, 0)
|
|
|
|
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
|
2019-06-05 16:26:17 -07:00
|
|
|
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
|
2018-11-08 16:28:28 -08:00
|
|
|
bechAddr := address.String()
|
2020-08-04 05:58:36 -07:00
|
|
|
signingInfos = append(signingInfos, types.SigningInfo{
|
|
|
|
Address: bechAddr,
|
|
|
|
ValidatorSigningInfo: info,
|
|
|
|
})
|
2018-11-08 16:28:28 -08:00
|
|
|
|
2020-08-04 05:58:36 -07:00
|
|
|
localMissedBlocks := keeper.GetValidatorMissedBlocks(ctx, address)
|
|
|
|
|
|
|
|
missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
|
|
|
|
Address: bechAddr,
|
|
|
|
MissedBlocks: 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
|
|
|
}
|