cosmos-sdk/x/slashing/genesis.go
Marie Gauthier 90e9370bd8
Replace tmcrypto.PubKey by our own cryptotypes.PubKey (#7419)
* WIP on removing tm pub/privkey

* Fix part of crypto tests

* Add PrivKeyLedgerSecp256K1 proto type

* Use BasePrivKey for ledger priv key type

* Refacto continued

* First round

* x/staking

* Continue porting

* x/* done

* Make build pass

* More conversion

* Remove IntoTmPubKey

* Fix test

* Remove crypto.PubKey in some other places

* Revert ledger changes

* Fix comment

* Remove useless function

* Add To/FromTmPublicKey

* Add migrate tests

* Fix test

* Fix another test

* Rename tm conversion functions

* Less code

* Rename BasePrivKey to LedgerPrivKey

* Add changelog

* Rename functions

Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-11-09 16:01:43 +00:00

71 lines
2.1 KiB
Go

package slashing
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, stakingKeeper types.StakingKeeper, data *types.GenesisState) {
stakingKeeper.IterateValidators(ctx,
func(index int64, validator stakingtypes.ValidatorI) bool {
consPk, err := validator.ConsPubKey()
if err != nil {
panic(err)
}
keeper.AddPubkey(ctx, consPk)
return false
},
)
for _, info := range data.SigningInfos {
address, err := sdk.ConsAddressFromBech32(info.Address)
if err != nil {
panic(err)
}
keeper.SetValidatorSigningInfo(ctx, address, info.ValidatorSigningInfo)
}
for _, array := range data.MissedBlocks {
address, err := sdk.ConsAddressFromBech32(array.Address)
if err != nil {
panic(err)
}
for _, missed := range array.MissedBlocks {
keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
}
}
keeper.SetParams(ctx, data.Params)
}
// ExportGenesis writes the current store values
// to a genesis file, which can be imported again
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data *types.GenesisState) {
params := keeper.GetParams(ctx)
signingInfos := make([]types.SigningInfo, 0)
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
bechAddr := address.String()
signingInfos = append(signingInfos, types.SigningInfo{
Address: bechAddr,
ValidatorSigningInfo: info,
})
localMissedBlocks := keeper.GetValidatorMissedBlocks(ctx, address)
missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
Address: bechAddr,
MissedBlocks: localMissedBlocks,
})
return false
})
return types.NewGenesisState(params, signingInfos, missedBlocks)
}