2018-05-28 14:24:58 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
2019-01-22 09:28:39 -08:00
|
|
|
"fmt"
|
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
|
|
|
)
|
|
|
|
|
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")
|
2018-08-31 02:03:43 -07:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// Params - used for initializing default parameter for slashing at genesis
|
|
|
|
type Params struct {
|
2019-02-05 16:36:10 -08:00
|
|
|
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"`
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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,
|
2018-08-31 02:03:43 -07:00
|
|
|
|
|
|
|
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-12-14 11:09:39 -08:00
|
|
|
// Downtime slashing threshold - 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
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
|
|
|
}
|
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, ¶ms)
|
|
|
|
return params
|
|
|
|
}
|