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 (
|
2019-02-07 17:41:23 -08:00
|
|
|
DefaultParamspace = ModuleName
|
|
|
|
DefaultMaxEvidenceAge time.Duration = 60 * 2 * time.Second
|
|
|
|
DefaultSignedBlocksWindow int64 = 100
|
|
|
|
DefaultDowntimeJailDuration time.Duration = 60 * 10 * time.Second
|
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 (
|
2019-02-08 12:44:19 -08:00
|
|
|
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))
|
2019-01-10 17:22:49 -08:00
|
|
|
)
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Parameter store keys
|
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-05-16 08:25:32 -07:00
|
|
|
// NewParams creates a new Params object
|
|
|
|
func NewParams(maxEvidenceAge time.Duration, signedBlocksWindow int64,
|
|
|
|
minSignedPerWindow sdk.Dec, downtimeJailDuration time.Duration,
|
|
|
|
slashFractionDoubleSign sdk.Dec, slashFractionDowntime sdk.Dec) Params {
|
|
|
|
|
|
|
|
return Params{
|
|
|
|
MaxEvidenceAge: maxEvidenceAge,
|
|
|
|
SignedBlocksWindow: signedBlocksWindow,
|
|
|
|
MinSignedPerWindow: minSignedPerWindow,
|
|
|
|
DowntimeJailDuration: downtimeJailDuration,
|
|
|
|
SlashFractionDoubleSign: slashFractionDoubleSign,
|
|
|
|
SlashFractionDowntime: slashFractionDowntime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2019-05-06 09:50:05 -07:00
|
|
|
SlashFractionDoubleSign: %s
|
|
|
|
SlashFractionDowntime: %s`, p.MaxEvidenceAge,
|
2019-01-22 09:28:39 -08:00
|
|
|
p.SignedBlocksWindow, p.MinSignedPerWindow,
|
|
|
|
p.DowntimeJailDuration, p.SlashFractionDoubleSign,
|
|
|
|
p.SlashFractionDowntime)
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Implements params.ParamSet
|
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},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Default parameters for this module
|
2018-09-10 04:59:05 -07:00
|
|
|
func DefaultParams() Params {
|
2018-08-31 02:03:43 -07:00
|
|
|
return Params{
|
2019-02-07 17:41:23 -08:00
|
|
|
MaxEvidenceAge: DefaultMaxEvidenceAge,
|
|
|
|
SignedBlocksWindow: DefaultSignedBlocksWindow,
|
|
|
|
MinSignedPerWindow: DefaultMinSignedPerWindow,
|
|
|
|
DowntimeJailDuration: DefaultDowntimeJailDuration,
|
|
|
|
SlashFractionDoubleSign: DefaultSlashFractionDoubleSign,
|
|
|
|
SlashFractionDowntime: DefaultSlashFractionDowntime,
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// MaxEvidenceAge - max age for evidence
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) {
|
2019-04-05 13:20:58 -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) {
|
2019-04-05 13:20:58 -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
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Downtime slashing threshold
|
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
|
2019-04-05 13:20:58 -07:00
|
|
|
k.paramspace.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
|
2018-07-13 17:12:23 -07:00
|
|
|
signedBlocksWindow := k.SignedBlocksWindow(ctx)
|
2019-02-05 21:30:48 -08:00
|
|
|
|
|
|
|
// NOTE: RoundInt64 will never panic as minSignedPerWindow is
|
|
|
|
// less than 1.
|
|
|
|
return minSignedPerWindow.MulInt64(signedBlocksWindow).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) {
|
2019-04-05 13:20:58 -07:00
|
|
|
k.paramspace.Get(ctx, KeyDowntimeJailDuration, &res)
|
2018-08-31 02:03:43 -07:00
|
|
|
return
|
2018-07-13 17:12:23 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// SlashFractionDoubleSign
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
|
2019-04-05 13:20:58 -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
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// SlashFractionDowntime
|
2018-08-31 02:03:43 -07:00
|
|
|
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
|
2019-04-05 13:20:58 -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
|
|
|
|
}
|