-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
validator slash event stored by period and height #4654
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#4654 validator slash event stored by period and height |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
|
@@ -86,33 +88,20 @@ func (k Keeper) decrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, | |
} | ||
|
||
func (k Keeper) updateValidatorSlashFraction(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) { | ||
if fraction.GT(sdk.OneDec()) { | ||
panic("fraction greater than one") | ||
if fraction.GT(sdk.OneDec()) || fraction.LT(sdk.ZeroDec()) { | ||
panic(fmt.Sprintf("fraction must be >=0 and <=1, current fraction: %v", fraction)) | ||
} | ||
height := uint64(ctx.BlockHeight()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason why all this logic was removed? Was calculating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I think this design decision was overlooked at inception seeing as the revised code eliminates the need for this complexity. By introducing the period into the validator-slash-record-key the fraction no longer needs to be "flattened" with the other fractions in that height, hence we remove all this flattening logic in this function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is better and simpler. Mea culpa. I think originally we were concerned about iteration costs. |
||
currentFraction := sdk.ZeroDec() | ||
endedPeriod := k.GetValidatorCurrentRewards(ctx, valAddr).Period - 1 | ||
current, found := k.GetValidatorSlashEvent(ctx, valAddr, height) | ||
if found { | ||
// there has already been a slash event this height, | ||
// and we don't need to store more than one, | ||
// so just update the current slash fraction | ||
currentFraction = current.Fraction | ||
} else { | ||
val := k.stakingKeeper.Validator(ctx, valAddr) | ||
// increment current period | ||
endedPeriod = k.incrementValidatorPeriod(ctx, val) | ||
// increment reference count on period we need to track | ||
k.incrementReferenceCount(ctx, valAddr, endedPeriod) | ||
} | ||
currentMultiplicand := sdk.OneDec().Sub(currentFraction) | ||
newMultiplicand := sdk.OneDec().Sub(fraction) | ||
|
||
// using MulTruncate here conservatively increases the slashing amount | ||
updatedFraction := sdk.OneDec().Sub(currentMultiplicand.MulTruncate(newMultiplicand)) | ||
val := k.stakingKeeper.Validator(ctx, valAddr) | ||
|
||
if updatedFraction.LT(sdk.ZeroDec()) { | ||
panic("negative slash fraction") | ||
} | ||
k.SetValidatorSlashEvent(ctx, valAddr, height, types.NewValidatorSlashEvent(endedPeriod, updatedFraction)) | ||
// increment current period | ||
newPeriod := k.incrementValidatorPeriod(ctx, val) | ||
|
||
// increment reference count on period we need to track | ||
k.incrementReferenceCount(ctx, valAddr, newPeriod) | ||
|
||
slashEvent := types.NewValidatorSlashEvent(newPeriod, fraction) | ||
height := uint64(ctx.BlockHeight()) | ||
|
||
k.SetValidatorSlashEvent(ctx, valAddr, height, newPeriod, slashEvent) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @sabau