2018-05-28 14:24:58 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
|
|
|
|
2018-05-28 14:24:58 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-17 08:52:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-05-28 14:24:58 -07:00
|
|
|
)
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// Default parameter namespace
|
2018-07-13 17:12:23 -07:00
|
|
|
const (
|
2018-09-18 04:16:20 -07:00
|
|
|
DefaultParamspace = "slashing"
|
2018-07-13 17:12:23 -07:00
|
|
|
)
|
|
|
|
|
2018-09-17 08:28:13 -07:00
|
|
|
// Parameter store key
|
2018-09-27 11:52:29 -07:00
|
|
|
var (
|
|
|
|
KeyMaxEvidenceAge = []byte("MaxEvidenceAge")
|
|
|
|
KeySignedBlocksWindow = []byte("SignedBlocksWindow")
|
|
|
|
KeyMinSignedPerWindow = []byte("MinSignedPerWindow")
|
|
|
|
KeyDoubleSignUnbondDuration = []byte("DoubleSignUnbondDuration")
|
|
|
|
KeyDowntimeUnbondDuration = []byte("DowntimeUnbondDuration")
|
|
|
|
KeySlashFractionDoubleSign = []byte("SlashFractionDoubleSign")
|
|
|
|
KeySlashFractionDowntime = []byte("SlashFractionDowntime")
|
2018-08-31 02:03:43 -07:00
|
|
|
)
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
// ParamTypeTable for slashing module
|
|
|
|
func ParamTypeTable() params.TypeTable {
|
|
|
|
return params.NewTypeTable().RegisterParamSet(&Params{})
|
2018-10-06 08:32:41 -07:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:03:43 -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"`
|
2018-10-06 06:50:58 -07:00
|
|
|
DoubleSignUnbondDuration time.Duration `json:"double-sign-unbond-duration"`
|
2018-08-31 02:03:43 -07:00
|
|
|
DowntimeUnbondDuration time.Duration `json:"downtime-unbond-duration"`
|
2018-10-06 06:50:58 -07:00
|
|
|
SlashFractionDoubleSign sdk.Dec `json:"slash-fraction-double-sign"`
|
2018-08-31 02:03:43 -07:00
|
|
|
SlashFractionDowntime sdk.Dec `json:"slash-fraction-downtime"`
|
|
|
|
}
|
|
|
|
|
2018-09-18 02:57:24 -07:00
|
|
|
// Implements params.ParamStruct
|
2018-10-06 06:50:58 -07:00
|
|
|
func (p *Params) KeyValuePairs() params.KeyValuePairs {
|
|
|
|
return params.KeyValuePairs{
|
2018-09-17 08:52:03 -07:00
|
|
|
{KeyMaxEvidenceAge, &p.MaxEvidenceAge},
|
|
|
|
{KeySignedBlocksWindow, &p.SignedBlocksWindow},
|
|
|
|
{KeyMinSignedPerWindow, &p.MinSignedPerWindow},
|
|
|
|
{KeyDoubleSignUnbondDuration, &p.DoubleSignUnbondDuration},
|
|
|
|
{KeyDowntimeUnbondDuration, &p.DowntimeUnbondDuration},
|
|
|
|
{KeySlashFractionDoubleSign, &p.SlashFractionDoubleSign},
|
|
|
|
{KeySlashFractionDowntime, &p.SlashFractionDowntime},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// Default parameters used by Cosmos Hub
|
2018-09-10 04:59:05 -07:00
|
|
|
func DefaultParams() Params {
|
2018-08-31 02:03:43 -07:00
|
|
|
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 five minutes for testnets
|
|
|
|
DoubleSignUnbondDuration: 60 * 5 * time.Second,
|
|
|
|
|
|
|
|
// TODO Temporarily set to 100 blocks for testnets
|
|
|
|
SignedBlocksWindow: 100,
|
|
|
|
|
|
|
|
// TODO Temporarily set to 10 minutes for testnets
|
|
|
|
DowntimeUnbondDuration: 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)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:12:23 -07:00
|
|
|
// MaxEvidenceAge - Max age for evidence - 21 days (3 weeks)
|
|
|
|
// MaxEvidenceAge = 60 * 60 * 24 * 7 * 3
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeyMaxEvidenceAge, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignedBlocksWindow - sliding window for downtime slashing
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeySignedBlocksWindow, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// Downtime slashing thershold - default 50% of the SignedBlocksWindow
|
2018-07-13 17:12:23 -07:00
|
|
|
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
|
2018-08-31 02:03:43 -07:00
|
|
|
var minSignedPerWindow sdk.Dec
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
|
2018-07-13 17:12:23 -07:00
|
|
|
signedBlocksWindow := k.SignedBlocksWindow(ctx)
|
2018-08-14 17:15:02 -07:00
|
|
|
return sdk.NewDec(signedBlocksWindow).Mul(minSignedPerWindow).RoundInt64()
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Double-sign unbond duration
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) DoubleSignUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeyDoubleSignUnbondDuration, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Downtime unbond duration
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) DowntimeUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeyDowntimeUnbondDuration, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SlashFractionDoubleSign - currently default 5%
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeySlashFractionDoubleSign, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SlashFractionDowntime - currently default 1%
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
|
2018-10-10 13:01:30 -07:00
|
|
|
k.paramspace.Get(ctx, KeySlashFractionDowntime, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|