-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vbank): separate smoothing and distribution fraction from epoch
- Loading branch information
1 parent
82eeadb
commit 9116ede
Showing
9 changed files
with
438 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package keeper | ||
|
||
import ( | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// minCoins returns the minimum of each denomination. | ||
// The input coins should be sorted. | ||
func minCoins(a, b sdk.Coins) sdk.Coins { | ||
min := make([]sdk.Coin, 0) | ||
for indexA, indexB := 0, 0; indexA < len(a) && indexB < len(b); { | ||
coinA, coinB := a[indexA], b[indexB] | ||
switch strings.Compare(coinA.Denom, coinB.Denom) { | ||
case -1: // A < B | ||
indexA++ | ||
case 0: // A == B | ||
minCoin := coinA | ||
if coinB.IsLT(minCoin) { | ||
minCoin = coinB | ||
} | ||
if !minCoin.IsZero() { | ||
min = append(min, minCoin) | ||
} | ||
indexA++ | ||
indexB++ | ||
case 1: // A > B | ||
indexB++ | ||
} | ||
} | ||
return sdk.NewCoins(min...) | ||
} | ||
|
||
func mulCoins(a sdk.Coins, b sdk.Dec) sdk.Coins { | ||
coins := make([]sdk.Coin, 0, len(a)) | ||
for _, coin := range a { | ||
amount := b.MulInt(coin.Amount).TruncateInt() | ||
if amount.IsPositive() { | ||
coins = append(coins, sdk.NewCoin(coin.Denom, amount)) | ||
} | ||
} | ||
return sdk.NewCoins(coins...) | ||
} | ||
|
||
// DistributeRewards drives the rewards state machine. | ||
func (k Keeper) DistributeRewards(ctx sdk.Context) error { | ||
// Distribute rewards. | ||
state := k.GetState(ctx) | ||
params := k.GetParams(ctx) | ||
|
||
smoothingBlocks := params.GetSmoothingBlocks() | ||
thisBlock := ctx.BlockHeight() | ||
cycleIndex := thisBlock - state.LastRewardDistributionBlock | ||
|
||
// Check if we're at the end of the last cycle. | ||
if cycleIndex >= params.RewardEpochDurationBlocks { | ||
// Get more rewards to distribute. | ||
toDistribute := mulCoins(state.RewardPool, params.PerEpochRewardFraction) | ||
state.LastRewardDistributionBlock = thisBlock | ||
state.RewardBlockAmount = params.RewardRate(toDistribute, smoothingBlocks) | ||
k.SetState(ctx, state) | ||
} | ||
|
||
if cycleIndex >= smoothingBlocks { | ||
// No more distribution to do until the next cycle. | ||
return nil | ||
} | ||
|
||
// We're currently within the smoothing period, send the amount to distribute. | ||
xfer := minCoins(state.RewardBlockAmount, state.RewardPool) | ||
if !xfer.IsZero() { | ||
if err := k.SendCoinsToRewardDistributor(ctx, xfer); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
state.RewardPool = state.RewardPool.Sub(xfer) | ||
k.SetState(ctx, state) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.