Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(slashing): disable slashing during the singularity #17

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 79 additions & 19 deletions api/cosmos/slashing/v1beta1/slashing.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/cosmos/slashing/v1beta1/slashing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ message Params {
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
// the block height until singularity is activated
uint64 singularity_height = 6;
}
8 changes: 7 additions & 1 deletion x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func TestBeginBlocker(t *testing.T) {

ctx := app.BaseApp.NewContext(false)

params, err := slashingKeeper.GetParams(ctx)
require.NoError(t, err)

params.SingularityHeight = 1
require.NoError(t, slashingKeeper.SetParams(ctx, params))

pks := simtestutil.CreateTestPubKeys(1)
simtestutil.AddTestAddrsFromPubKeys(bankKeeper, stakingKeeper, ctx, pks, stakingKeeper.TokensFromConsensusPower(ctx, 200))
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
Expand Down Expand Up @@ -84,7 +90,7 @@ func TestBeginBlocker(t *testing.T) {
require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil)
require.Equal(t, int64(0), info.MissedBlocksCounter)

height := int64(0)
height := int64(2)

signedBlocksWindow, err := slashingKeeper.SignedBlocksWindow(ctx)
require.NoError(t, err)
Expand Down
12 changes: 11 additions & 1 deletion x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
minHeight := signInfo.StartHeight + signedBlocksWindow
maxMissed := signedBlocksWindow - minSignedPerWindow

params, err := k.GetParams(ctx)
if err != nil {
return errors.Wrap(err, "failed to get params")
}

// if we are past the minimum height and the validator has missed too many blocks, punish them
if height > minHeight && signInfo.MissedBlocksCounter > maxMissed {
if height > int64(params.SingularityHeight) && height > minHeight && signInfo.MissedBlocksCounter > maxMissed {
validator, err := k.sk.ValidatorByConsAddr(ctx, consAddr)
if err != nil {
return err
Expand Down Expand Up @@ -170,6 +175,11 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
"slashed", slashFractionDowntime.String(),
"jailed_until", signInfo.JailedUntil,
)
} else if height == int64(params.SingularityHeight) {
// reset the counter & bitmap so that the validator won't be
// immediately slashed after singularity.
signInfo.MissedBlocksCounter = 0
signInfo.IndexOffset = 0
} else {
// validator was (a) not found or (b) already jailed so we do not slash
logger.Info(
Expand Down
1 change: 1 addition & 0 deletions x/slashing/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (s *KeeperTestSuite) TestUpdateParams() {
DowntimeJailDuration: time.Duration(34800000000000),
SlashFractionDoubleSign: slashFractionDoubleSign,
SlashFractionDowntime: slashFractionDowntime,
SingularityHeight: uint64(1),
},
},
expectErr: false,
Expand Down
10 changes: 9 additions & 1 deletion x/slashing/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DowntimeJailDuration = "downtime_jail_duration"
SlashFractionDoubleSign = "slash_fraction_double_sign"
SlashFractionDowntime = "slash_fraction_downtime"
SingularityHeight = "singularity_height"
)

// GenSignedBlocksWindow randomized SignedBlocksWindow
Expand Down Expand Up @@ -47,6 +48,10 @@ func GenSlashFractionDowntime(r *rand.Rand) math.LegacyDec {
return math.LegacyNewDec(1).Quo(math.LegacyNewDec(int64(r.Intn(200) + 1)))
}

func GenSingularityHeight(r *rand.Rand) uint64 {
return uint64(simulation.RandIntBetween(r, 10, 1000))
}

// RandomizedGenState generates a random GenesisState for slashing
func RandomizedGenState(simState *module.SimulationState) {
var signedBlocksWindow int64
Expand All @@ -64,9 +69,12 @@ func RandomizedGenState(simState *module.SimulationState) {
var slashFractionDowntime math.LegacyDec
simState.AppParams.GetOrGenerate(SlashFractionDowntime, &slashFractionDowntime, simState.Rand, func(r *rand.Rand) { slashFractionDowntime = GenSlashFractionDowntime(r) })

var singularityHeight uint64
simState.AppParams.GetOrGenerate(SingularityHeight, &singularityHeight, simState.Rand, func(r *rand.Rand) { singularityHeight = GenSingularityHeight(r) })

params := types.NewParams(
signedBlocksWindow, minSignedPerWindow, downtimeJailDuration,
slashFractionDoubleSign, slashFractionDowntime,
slashFractionDoubleSign, slashFractionDowntime, singularityHeight,
)

slashingGenesis := types.NewGenesisState(params, []types.SigningInfo{}, []types.ValidatorMissedBlocks{})
Expand Down
5 changes: 5 additions & 0 deletions x/slashing/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ func ValidateGenesis(data GenesisState) error {
return fmt.Errorf("signed blocks window must be at least 10, is %d", signedWindow)
}

singularityHeight := data.Params.SingularityHeight
if singularityHeight == 0 {
return fmt.Errorf("singularity height must be positive, is %d", singularityHeight)
}

return nil
}
21 changes: 20 additions & 1 deletion x/slashing/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
DefaultSignedBlocksWindow = int64(100)
DefaultDowntimeJailDuration = 60 * 10 * time.Second
DefaultSingularityHeight = 1209600 // 42 days with 3 seconds block time
)

var (
Expand All @@ -22,14 +23,15 @@ var (
// NewParams creates a new Params object
func NewParams(
signedBlocksWindow int64, minSignedPerWindow math.LegacyDec, downtimeJailDuration time.Duration,
slashFractionDoubleSign, slashFractionDowntime math.LegacyDec,
slashFractionDoubleSign, slashFractionDowntime math.LegacyDec, singularityHeight uint64,
) Params {
return Params{
SignedBlocksWindow: signedBlocksWindow,
MinSignedPerWindow: minSignedPerWindow,
DowntimeJailDuration: downtimeJailDuration,
SlashFractionDoubleSign: slashFractionDoubleSign,
SlashFractionDowntime: slashFractionDowntime,
SingularityHeight: singularityHeight,
}
}

Expand All @@ -41,6 +43,7 @@ func DefaultParams() Params {
DefaultDowntimeJailDuration,
DefaultSlashFractionDoubleSign,
DefaultSlashFractionDowntime,
DefaultSingularityHeight,
)
}

Expand All @@ -61,6 +64,9 @@ func (p Params) Validate() error {
if err := validateSlashFractionDowntime(p.SlashFractionDowntime); err != nil {
return err
}
if err := validateSingularityHeight(p.SingularityHeight); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -146,3 +152,16 @@ func validateSlashFractionDowntime(i interface{}) error {

return nil
}

func validateSingularityHeight(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v == 0 {
return fmt.Errorf("singularity height must be positive: %d", v)
}

return nil
}
Loading