package slashing import ( "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/params" ) // Default parameter namespace const ( DefaultParamspace = ModuleName DefaultMaxEvidenceAge time.Duration = 60 * 2 * time.Second DefaultSignedBlocksWindow int64 = 100 DefaultDowntimeJailDuration time.Duration = 60 * 10 * time.Second ) // The Double Sign Jail period ends at Max Time supported by Amino (Dec 31, 9999 - 23:59:59 GMT) var ( DoubleSignJailEndTime = time.Unix(253402300799, 0) DefaultMinSignedPerWindow = sdk.NewDecWithPrec(5, 1) DefaultSlashFractionDoubleSign = sdk.NewDec(1).Quo(sdk.NewDec(20)) DefaultSlashFractionDowntime = sdk.NewDec(1).Quo(sdk.NewDec(100)) ) // Parameter store keys var ( KeyMaxEvidenceAge = []byte("MaxEvidenceAge") KeySignedBlocksWindow = []byte("SignedBlocksWindow") KeyMinSignedPerWindow = []byte("MinSignedPerWindow") KeyDowntimeJailDuration = []byte("DowntimeJailDuration") KeySlashFractionDoubleSign = []byte("SlashFractionDoubleSign") KeySlashFractionDowntime = []byte("SlashFractionDowntime") ) // ParamKeyTable for slashing module func ParamKeyTable() params.KeyTable { return params.NewKeyTable().RegisterParamSet(&Params{}) } // Params - used for initializing default parameter for slashing at genesis type Params struct { MaxEvidenceAge time.Duration `json:"max_evidence_age"` SignedBlocksWindow int64 `json:"signed_blocks_window"` MinSignedPerWindow sdk.Dec `json:"min_signed_per_window"` DowntimeJailDuration time.Duration `json:"downtime_jail_duration"` SlashFractionDoubleSign sdk.Dec `json:"slash_fraction_double_sign"` SlashFractionDowntime sdk.Dec `json:"slash_fraction_downtime"` } func (p Params) String() string { return fmt.Sprintf(`Slashing Params: MaxEvidenceAge: %s SignedBlocksWindow: %d MinSignedPerWindow: %s DowntimeJailDuration: %s SlashFractionDoubleSign: %d SlashFractionDowntime: %d`, p.MaxEvidenceAge, p.SignedBlocksWindow, p.MinSignedPerWindow, p.DowntimeJailDuration, p.SlashFractionDoubleSign, p.SlashFractionDowntime) } // Implements params.ParamSet func (p *Params) ParamSetPairs() params.ParamSetPairs { return params.ParamSetPairs{ {KeyMaxEvidenceAge, &p.MaxEvidenceAge}, {KeySignedBlocksWindow, &p.SignedBlocksWindow}, {KeyMinSignedPerWindow, &p.MinSignedPerWindow}, {KeyDowntimeJailDuration, &p.DowntimeJailDuration}, {KeySlashFractionDoubleSign, &p.SlashFractionDoubleSign}, {KeySlashFractionDowntime, &p.SlashFractionDowntime}, } } // Default parameters for this module func DefaultParams() Params { return Params{ MaxEvidenceAge: DefaultMaxEvidenceAge, SignedBlocksWindow: DefaultSignedBlocksWindow, MinSignedPerWindow: DefaultMinSignedPerWindow, DowntimeJailDuration: DefaultDowntimeJailDuration, SlashFractionDoubleSign: DefaultSlashFractionDoubleSign, SlashFractionDowntime: DefaultSlashFractionDowntime, } } // MaxEvidenceAge - max age for evidence func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) { k.paramspace.Get(ctx, KeyMaxEvidenceAge, &res) return } // SignedBlocksWindow - sliding window for downtime slashing func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) { k.paramspace.Get(ctx, KeySignedBlocksWindow, &res) return } // Downtime slashing threshold func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 { var minSignedPerWindow sdk.Dec k.paramspace.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow) signedBlocksWindow := k.SignedBlocksWindow(ctx) // NOTE: RoundInt64 will never panic as minSignedPerWindow is // less than 1. return minSignedPerWindow.MulInt64(signedBlocksWindow).RoundInt64() } // Downtime unbond duration func (k Keeper) DowntimeJailDuration(ctx sdk.Context) (res time.Duration) { k.paramspace.Get(ctx, KeyDowntimeJailDuration, &res) return } // SlashFractionDoubleSign func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) { k.paramspace.Get(ctx, KeySlashFractionDoubleSign, &res) return } // SlashFractionDowntime func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) { k.paramspace.Get(ctx, KeySlashFractionDowntime, &res) return } // GetParams returns the total set of slashing parameters. func (k Keeper) GetParams(ctx sdk.Context) (params Params) { k.paramspace.GetParamSet(ctx, ¶ms) return params }