cosmos-sdk/x/slashing/params.go

136 lines
4.2 KiB
Go
Raw Normal View History

package slashing
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-09-17 08:52:03 -07:00
"github.com/cosmos/cosmos-sdk/x/params"
)
// Default parameter namespace
const (
2018-09-18 04:16:20 -07:00
DefaultParamspace = "slashing"
)
2019-01-10 17:22:49 -08:00
// 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)
)
2018-09-17 08:28:13 -07:00
// Parameter store key
2018-09-27 11:52:29 -07:00
var (
2019-01-10 17:22:49 -08:00
KeyMaxEvidenceAge = []byte("MaxEvidenceAge")
KeySignedBlocksWindow = []byte("SignedBlocksWindow")
KeyMinSignedPerWindow = []byte("MinSignedPerWindow")
KeyDowntimeJailDuration = []byte("DowntimeJailDuration")
KeySlashFractionDoubleSign = []byte("SlashFractionDoubleSign")
KeySlashFractionDowntime = []byte("SlashFractionDowntime")
)
2019-02-04 18:13:04 -08:00
// ParamKeyTable for slashing module
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
2018-10-06 08:32:41 -07:00
}
// 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)
}
2018-09-18 02:57:24 -07:00
// Implements params.ParamStruct
2019-02-04 18:13:04 -08:00
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
2018-09-17 08:52:03 -07:00
{KeyMaxEvidenceAge, &p.MaxEvidenceAge},
{KeySignedBlocksWindow, &p.SignedBlocksWindow},
{KeyMinSignedPerWindow, &p.MinSignedPerWindow},
2019-01-10 17:22:49 -08:00
{KeyDowntimeJailDuration, &p.DowntimeJailDuration},
2018-09-17 08:52:03 -07:00
{KeySlashFractionDoubleSign, &p.SlashFractionDoubleSign},
{KeySlashFractionDowntime, &p.SlashFractionDowntime},
}
}
// Default parameters used by Cosmos Hub
2018-09-10 04:59:05 -07:00
func DefaultParams() Params {
return Params{
// defaultMaxEvidenceAge = 60 * 60 * 24 * 7 * 3
// TODO Temporarily set to 2 minutes for testnets.
MaxEvidenceAge: 60 * 2 * time.Second,
// TODO Temporarily set to 100 blocks for testnets
SignedBlocksWindow: 100,
// TODO Temporarily set to 10 minutes for testnets
2019-01-10 17:22:49 -08:00
DowntimeJailDuration: 60 * 10 * time.Second,
MinSignedPerWindow: sdk.NewDecWithPrec(5, 1),
SlashFractionDoubleSign: sdk.NewDec(1).Quo(sdk.NewDec(20)),
SlashFractionDowntime: sdk.NewDec(1).Quo(sdk.NewDec(100)),
}
}
// MaxEvidenceAge - Max age for evidence - 21 days (3 weeks)
// MaxEvidenceAge = 60 * 60 * 24 * 7 * 3
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
}
2018-12-14 11:09:39 -08:00
// Downtime slashing threshold - default 50% of the SignedBlocksWindow
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
var minSignedPerWindow sdk.Dec
k.paramspace.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
signedBlocksWindow := k.SignedBlocksWindow(ctx)
return sdk.NewDec(signedBlocksWindow).Mul(minSignedPerWindow).RoundInt64()
}
// Downtime unbond duration
2019-01-10 17:22:49 -08:00
func (k Keeper) DowntimeJailDuration(ctx sdk.Context) (res time.Duration) {
k.paramspace.Get(ctx, KeyDowntimeJailDuration, &res)
return
}
// SlashFractionDoubleSign - currently default 5%
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
k.paramspace.Get(ctx, KeySlashFractionDoubleSign, &res)
return
}
// SlashFractionDowntime - currently default 1%
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
k.paramspace.Get(ctx, KeySlashFractionDowntime, &res)
return
}
2018-12-14 11:09:39 -08:00
// GetParams returns the total set of slashing parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params Params) {
k.paramspace.GetParamSet(ctx, &params)
return params
}