Skip to content

Commit

Permalink
fix: modify emissions to follow fixed rewards per block (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Jan 29, 2024
1 parent d1236ba commit e7fd290
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 270 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* [1577](https://github.com/zeta-chain/node/pull/1577) - add chain header tests in E2E tests and fix admin tests

### Features

* [1658](https://github.com/zeta-chain/node/pull/1658) - modify emission distribution to use fixed block rewards
### Fixes
* [1535](https://github.com/zeta-chain/node/issues/1535) - Avoid voting on wrong ballots due to false blockNumber in EVM tx receipt
* [1588](https://github.com/zeta-chain/node/pull/1588) - fix chain params comparison logic
Expand Down
14 changes: 14 additions & 0 deletions common/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"fmt"
"strconv"

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

func GetCoinType(coin string) (CoinType, error) {
Expand All @@ -16,3 +18,15 @@ func GetCoinType(coin string) (CoinType, error) {
// #nosec G701 always in range
return CoinType(coinInt), nil
}

func GetAzetaDecFromAmountInZeta(zetaAmount string) (sdk.Dec, error) {
zetaDec, err := sdk.NewDecFromStr(zetaAmount)
if err != nil {
return sdk.Dec{}, err
}
zetaToAzetaConvertionFactor, err := sdk.NewDecFromStr("1000000000000000000")
if err != nil {
return sdk.Dec{}, err
}
return zetaDec.Mul(zetaToAzetaConvertionFactor), nil
}
65 changes: 65 additions & 0 deletions common/coin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package common_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/zeta-chain/zetacore/common"
)

func Test_GetAzetaDecFromAmountInZeta(t *testing.T) {
tt := []struct {
name string
zetaAmount string
err assert.ErrorAssertionFunc
azetaAmount sdk.Dec
}{
{
name: "valid zeta amount",
zetaAmount: "210000000",
err: assert.NoError,
azetaAmount: sdk.MustNewDecFromStr("210000000000000000000000000"),
},
{
name: "very high zeta amount",
zetaAmount: "21000000000000000000",
err: assert.NoError,
azetaAmount: sdk.MustNewDecFromStr("21000000000000000000000000000000000000"),
},
{
name: "very low zeta amount",
zetaAmount: "1",
err: assert.NoError,
azetaAmount: sdk.MustNewDecFromStr("1000000000000000000"),
},
{
name: "zero zeta amount",
zetaAmount: "0",
err: assert.NoError,
azetaAmount: sdk.MustNewDecFromStr("0"),
},
{
name: "decimal zeta amount",
zetaAmount: "0.1",
err: assert.NoError,
azetaAmount: sdk.MustNewDecFromStr("100000000000000000"),
},
{
name: "invalid zeta amount",
zetaAmount: "%%%%%$#",
err: assert.Error,
azetaAmount: sdk.MustNewDecFromStr("0"),
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
azeta, err := common.GetAzetaDecFromAmountInZeta(tc.zetaAmount)
tc.err(t, err)
if err == nil {
assert.Equal(t, tc.azetaAmount, azeta)
}
})
}

}
35 changes: 23 additions & 12 deletions x/emissions/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissions

import (
"fmt"
"sort"

sdkmath "cosmossdk.io/math"
Expand All @@ -11,29 +12,38 @@ import (
)

func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
emissionPoolBalance := keeper.GetReservesFactor(ctx)
blockRewards := types.BlockReward

reservesFactor, bondFactor, durationFactor := keeper.GetBlockRewardComponents(ctx)
blockRewards := reservesFactor.Mul(bondFactor).Mul(durationFactor)
if blockRewards.IsZero() {
if blockRewards.GT(emissionPoolBalance) {
ctx.Logger().Info(fmt.Sprintf("Block rewards %s are greater than emission pool balance %s", blockRewards.String(), emissionPoolBalance.String()))
return
}
validatorRewards := sdk.MustNewDecFromStr(keeper.GetParams(ctx).ValidatorEmissionPercentage).Mul(blockRewards).TruncateInt()
observerRewards := sdk.MustNewDecFromStr(keeper.GetParams(ctx).ObserverEmissionPercentage).Mul(blockRewards).TruncateInt()
tssSignerRewards := sdk.MustNewDecFromStr(keeper.GetParams(ctx).TssSignerEmissionPercentage).Mul(blockRewards).TruncateInt()
err := DistributeValidatorRewards(ctx, validatorRewards, keeper.GetBankKeeper(), keeper.GetFeeCollector())
// Use a tmpCtx, which is a cache-wrapped context to avoid writing to the store
// We commit only if all three distributions are successful, if not the funds stay in the emission pool
tmpCtx, commit := ctx.CacheContext()
err := DistributeValidatorRewards(tmpCtx, validatorRewards, keeper.GetBankKeeper(), keeper.GetFeeCollector())
if err != nil {
panic(err)
ctx.Logger().Error(fmt.Sprintf("Error while distributing validator rewards %s", err))
return
}
err = DistributeObserverRewards(ctx, observerRewards, keeper)
err = DistributeObserverRewards(tmpCtx, observerRewards, keeper)
if err != nil {
panic(err)
ctx.Logger().Error(fmt.Sprintf("Error while distributing observer rewards %s", err))
return
}
err = DistributeTssRewards(ctx, tssSignerRewards, keeper.GetBankKeeper())
err = DistributeTssRewards(tmpCtx, tssSignerRewards, keeper.GetBankKeeper())
if err != nil {
panic(err)
ctx.Logger().Error(fmt.Sprintf("Error while distributing tss signer rewards %s", err))
return
}
types.EmitValidatorEmissions(ctx, bondFactor.String(), reservesFactor.String(),
durationFactor.String(),
commit()

types.EmitValidatorEmissions(ctx, "", "",
"",
validatorRewards.String(),
observerRewards.String(),
tssSignerRewards.String())
Expand All @@ -44,6 +54,7 @@ func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
// This function uses the distribution module of cosmos-sdk , by directly sending funds to the feecollector.
func DistributeValidatorRewards(ctx sdk.Context, amount sdkmath.Int, bankKeeper types.BankKeeper, feeCollector string) error {
coin := sdk.NewCoins(sdk.NewCoin(config.BaseDenom, amount))
ctx.Logger().Info(fmt.Sprintf(fmt.Sprintf("Distributing Validator Rewards Total:%s To FeeCollector : %s", amount.String(), feeCollector)))
return bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, feeCollector, coin)
}

Expand Down Expand Up @@ -76,7 +87,7 @@ func DistributeObserverRewards(ctx sdk.Context, amount sdkmath.Int, keeper keepe
if totalRewardsUnits > 0 && amount.IsPositive() {
rewardPerUnit = amount.Quo(sdk.NewInt(totalRewardsUnits))
}

ctx.Logger().Debug(fmt.Sprintf("Total Rewards Units : %d , rewards per Unit %s ,number of ballots :%d", totalRewardsUnits, rewardPerUnit.String(), len(ballotIdentifiers)))
sortedKeys := make([]string, 0, len(rewardsDistributer))
for k := range rewardsDistributer {
sortedKeys = append(sortedKeys, k)
Expand Down
Loading

0 comments on commit e7fd290

Please sign in to comment.