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

R4R: Implement slashing period #2122

Merged
merged 34 commits into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
cb2fa04
Update PENDING.md
cwgoes Aug 22, 2018
fedb717
SlashingPeriod struct
cwgoes Aug 22, 2018
23f8887
Seperate keys.go, constant prefixes
cwgoes Aug 22, 2018
ddce1cf
Make linter happy
cwgoes Aug 22, 2018
aee492d
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 23, 2018
1a2f1bf
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 23, 2018
8a66088
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 24, 2018
3c59d43
Update Gopkg.lock
cwgoes Aug 24, 2018
bc392cf
Seek slashing period by infraction height
cwgoes Aug 24, 2018
fa8faad
Slashing period hooks
cwgoes Aug 24, 2018
23fbba7
Slashing period unit tests; bugfix
cwgoes Aug 27, 2018
8d2c74b
Add simple hook tests
cwgoes Aug 27, 2018
b111c75
Add sdk.ValidatorHooks interface
cwgoes Aug 27, 2018
dac51aa
No-op hooks
cwgoes Aug 27, 2018
a0f706a
Real hooks
cwgoes Aug 27, 2018
668c1ef
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 27, 2018
35e8990
Fix iteration direction & duplicate key, update Gaia
cwgoes Aug 27, 2018
3534a99
Correctly simulate past validator set signatures
cwgoes Aug 27, 2018
111754a
Tiny rename
cwgoes Aug 27, 2018
5071897
Update dep; 'make format'
cwgoes Aug 27, 2018
d3ba71f
Add quick slashing period functionality test
cwgoes Aug 27, 2018
97cdfd8
Additional unit tests
cwgoes Aug 27, 2018
527bbbd
Use current validators when selected
cwgoes Aug 27, 2018
6ed552a
Panic in the right place
cwgoes Aug 27, 2018
3277257
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 28, 2018
5ba559c
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 28, 2018
14cb956
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 29, 2018
d9ad52f
Address @rigelrozanski comments
cwgoes Aug 29, 2018
225cb34
Fix linter errors
cwgoes Aug 29, 2018
a349376
Address @melekes suggestion
cwgoes Aug 29, 2018
468aad6
Rename hook
cwgoes Aug 31, 2018
54d78b3
Merge branch 'develop' into cwgoes/implement-slashing-period
cwgoes Aug 31, 2018
05ede6c
Update for new bech32 types
cwgoes Aug 31, 2018
c257ac8
'make format'
cwgoes Aug 31, 2018
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
Prev Previous commit
Next Next commit
Slashing period unit tests; bugfix
  • Loading branch information
cwgoes committed Aug 27, 2018
commit 23fbba7701070ca894aacfe7d1014936e6f8fa89
7 changes: 5 additions & 2 deletions x/slashing/slashing_period.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ func (k Keeper) capBySlashingPeriod(ctx sdk.Context, address sdk.ValAddress, fra
// Calculate total amount to be slashed
slashingPeriod := k.getValidatorSlashingPeriodForHeight(ctx, address, infractionHeight)
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
totalToSlash := sdk.MaxDec(slashingPeriod.SlashedSoFar, fraction)

// Calculate remainder
revisedFraction = totalToSlash.Sub(slashingPeriod.SlashedSoFar)

// Update slashing period
slashingPeriod.SlashedSoFar = totalToSlash
k.setValidatorSlashingPeriod(ctx, slashingPeriod)

// Calculate remainder
revisedFraction = slashingPeriod.SlashedSoFar.Sub(totalToSlash)
return

rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
55 changes: 55 additions & 0 deletions x/slashing/slashing_period_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package slashing

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestGetSetValidatorSlashingPeriod(t *testing.T) {
ctx, _, _, _, keeper := createTestInput(t)
addr := sdk.ValAddress(addrs[0])
height := int64(5)
require.Panics(t, func() { keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height) })
newPeriod := ValidatorSlashingPeriod{
ValidatorAddr: addr,
StartHeight: height,
EndHeight: height + 10,
SlashedSoFar: sdk.ZeroDec(),
}
keeper.setValidatorSlashingPeriod(ctx, newPeriod)
// Get at start height
retrieved := keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height)
require.Equal(t, addr, retrieved.ValidatorAddr)
// Get before start height
retrieved = keeper.getValidatorSlashingPeriodForHeight(ctx, addr, int64(0))
require.Equal(t, addr, retrieved.ValidatorAddr)
// Get after start height (panic)
require.Panics(t, func() { keeper.getValidatorSlashingPeriodForHeight(ctx, addr, int64(6)) })
// Get after end height (panic)
newPeriod.EndHeight = int64(4)
keeper.setValidatorSlashingPeriod(ctx, newPeriod)
require.Panics(t, func() { keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height) })
}

func TestValidatorSlashingPeriodCap(t *testing.T) {
ctx, _, _, _, keeper := createTestInput(t)
addr := sdk.ValAddress(addrs[0])
height := int64(5)
newPeriod := ValidatorSlashingPeriod{
ValidatorAddr: addr,
StartHeight: height,
EndHeight: height + 10,
SlashedSoFar: sdk.ZeroDec(),
}
keeper.setValidatorSlashingPeriod(ctx, newPeriod)
half := sdk.NewDec(1).Quo(sdk.NewDec(2))
// First slash should be full
fractionA := keeper.capBySlashingPeriod(ctx, addr, half, height)
require.True(t, fractionA.Equal(half))
// Second slash should be capped
fractionB := keeper.capBySlashingPeriod(ctx, addr, half, height)
require.True(t, fractionB.Equal(sdk.ZeroDec()))
}