Skip to content

Commit

Permalink
Merge pull request #74 from terra-money/chore/small-refactor
Browse files Browse the repository at this point in the history
chore: refactor and added comments
  • Loading branch information
javiersuweijie authored Jan 26, 2023
2 parents 6249c75 + 7347af8 commit 846d9a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion x/alliance/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (k Keeper) RebalanceBondTokenWeights(ctx sdk.Context, assets []*types.Allia
expectedBondAmount := sdk.ZeroDec()
for _, asset := range assets {
// Ignores assets that were recently added to prevent a small set of stakers from owning too much of the
// voting power
// voting power at the start. Uses the asset.RewardStartTime to determine when an asset is activated
if ctx.BlockTime().Before(asset.RewardStartTime) {
// Queue a rebalancing event so that we keep checking if the asset rewards has started in the next block
k.QueueAssetRebalanceEvent(ctx)
Expand Down
16 changes: 0 additions & 16 deletions x/alliance/keeper/legacy_querier.go

This file was deleted.

10 changes: 8 additions & 2 deletions x/alliance/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import (
"github.com/terra-money/alliance/x/alliance/types"
)

// SlashValidator works by reducing the amount of validator shares for all alliance assets by a `fraction`
// This effectively reallocates tokens from slashed validators to good validators
// On top of slashing currently bonded delegations, we also slash re-delegations and un-delegations
// that are still in the progress of unbonding
func (k Keeper) SlashValidator(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error {
val, err := k.GetAllianceValidator(ctx, valAddr)
if err != nil {
return err
}
// slashedValidatorShares accumulates the final validator shares after slashing
slashedValidatorShares := sdk.NewDecCoins()
for _, share := range val.ValidatorShares {
sharesToSlash := share.Amount.Mul(fraction)
slashedValidatorShares = slashedValidatorShares.Add(sdk.NewDecCoinFromDec(share.Denom, share.Amount.Sub(sharesToSlash)))
sharesAfterSlashing := sdk.NewDecCoinFromDec(share.Denom, share.Amount.Sub(sharesToSlash))
slashedValidatorShares = slashedValidatorShares.Add(sharesAfterSlashing)
asset, found := k.GetAssetByDenom(ctx, share.Denom)
if !found {
return types.ErrUnknownAsset
Expand Down Expand Up @@ -88,7 +94,7 @@ func (k Keeper) SlashRedelegations(ctx sdk.Context, valAddr sdk.ValAddress, frac
if err != nil {
return err
}
dstVal.TotalDelegatorShares = sdk.NewDecCoins(dstVal.TotalDelegatorShares...).Sub(sdk.NewDecCoins(sdk.NewDecCoinFromDec(asset.Denom, sharesToSlash)))
dstVal.TotalDelegatorShares = sdk.DecCoins(dstVal.TotalDelegatorShares).Sub(sdk.NewDecCoins(sdk.NewDecCoinFromDec(asset.Denom, sharesToSlash)))
k.SetValidator(ctx, dstVal)

delegation.Shares = delegation.Shares.Sub(sharesToSlash)
Expand Down

0 comments on commit 846d9a5

Please sign in to comment.