cosmos-sdk/x/slashing/genesis.go

139 lines
4.2 KiB
Go
Raw Normal View History

package slashing
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
)
// GenesisState - all slashing state that must be provided at genesis
type GenesisState struct {
2019-01-10 17:22:49 -08:00
Params Params `json:"params"`
SigningInfos map[string]ValidatorSigningInfo `json:"signing_infos"`
MissedBlocks map[string][]MissedBlock `json:"missed_blocks"`
}
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewGenesisState creates a new GenesisState object
func NewGenesisState(params Params, signingInfos map[string]ValidatorSigningInfo,
missedBlocks map[string][]MissedBlock) GenesisState {
return GenesisState{
Params: params,
SigningInfos: signingInfos,
MissedBlocks: missedBlocks,
}
}
// MissedBlock
type MissedBlock struct {
Index int64 `json:"index"`
Missed bool `json:"missed"`
}
// DefaultGenesisState - default GenesisState used by Cosmos Hub
2018-09-10 04:59:05 -07:00
func DefaultGenesisState() GenesisState {
return GenesisState{
2019-01-10 17:22:49 -08:00
Params: DefaultParams(),
SigningInfos: make(map[string]ValidatorSigningInfo),
MissedBlocks: make(map[string][]MissedBlock),
}
}
// ValidateGenesis validates the slashing genesis parameters
func ValidateGenesis(data GenesisState) error {
downtime := data.Params.SlashFractionDowntime
if downtime.IsNegative() || downtime.GT(sdk.OneDec()) {
return fmt.Errorf("Slashing fraction downtime should be less than or equal to one and greater than zero, is %s", downtime.String())
}
dblSign := data.Params.SlashFractionDoubleSign
if dblSign.IsNegative() || dblSign.GT(sdk.OneDec()) {
return fmt.Errorf("Slashing fraction double sign should be less than or equal to one and greater than zero, is %s", dblSign.String())
}
minSign := data.Params.MinSignedPerWindow
if minSign.IsNegative() || minSign.GT(sdk.OneDec()) {
return fmt.Errorf("Min signed per window should be less than or equal to one and greater than zero, is %s", minSign.String())
}
maxEvidence := data.Params.MaxEvidenceAge
if maxEvidence < 1*time.Minute {
return fmt.Errorf("Max evidence age must be at least 1 minute, is %s", maxEvidence.String())
}
2019-01-10 17:22:49 -08:00
downtimeJail := data.Params.DowntimeJailDuration
if downtimeJail < 1*time.Minute {
return fmt.Errorf("Downtime unblond duration must be at least 1 minute, is %s", downtimeJail.String())
}
signedWindow := data.Params.SignedBlocksWindow
if signedWindow < 10 {
return fmt.Errorf("Signed blocks window must be at least 10, is %d", signedWindow)
}
return nil
}
// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
func InitGenesis(ctx sdk.Context, keeper Keeper, stakingKeeper StakingKeeper, data GenesisState) {
stakingKeeper.IterateValidators(ctx,
func(index int64, validator exported.ValidatorI) bool {
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
keeper.addPubkey(ctx, validator.GetConsPubKey())
return false
},
)
for addr, info := range data.SigningInfos {
address, err := sdk.ConsAddressFromBech32(addr)
if err != nil {
panic(err)
}
keeper.SetValidatorSigningInfo(ctx, address, info)
}
for addr, array := range data.MissedBlocks {
address, err := sdk.ConsAddressFromBech32(addr)
if err != nil {
panic(err)
}
for _, missed := range array {
keeper.setValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
}
}
keeper.paramspace.SetParamSet(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) (data GenesisState) {
var params Params
keeper.paramspace.GetParamSet(ctx, &params)
signingInfos := make(map[string]ValidatorSigningInfo)
missedBlocks := make(map[string][]MissedBlock)
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info ValidatorSigningInfo) (stop bool) {
bechAddr := address.String()
signingInfos[bechAddr] = info
2018-11-09 07:26:08 -08:00
localMissedBlocks := []MissedBlock{}
keeper.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) {
2018-11-09 07:26:08 -08:00
localMissedBlocks = append(localMissedBlocks, MissedBlock{index, missed})
return false
})
2018-11-09 07:26:08 -08:00
missedBlocks[bechAddr] = localMissedBlocks
return false
})
return GenesisState{
2019-01-10 17:22:49 -08:00
Params: params,
SigningInfos: signingInfos,
MissedBlocks: missedBlocks,
}
}