Skip to content

Commit

Permalink
use if exist in emissions
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Mar 8, 2024
1 parent 2625443 commit 4e2b0ed
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 20 deletions.
9 changes: 4 additions & 5 deletions x/emissions/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
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()

// Get the distribution of rewards
validatorRewards, observerRewards, tssSignerRewards := keeper.GetDistributions(ctx)

// 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
Expand Down Expand Up @@ -62,7 +62,6 @@ func DistributeValidatorRewards(ctx sdk.Context, amount sdkmath.Int, bankKeeper
// The total rewards are distributed equally among all Successful votes
// NotVoted or Unsuccessful votes are slashed
// rewards given or slashed amounts are in azeta

func DistributeObserverRewards(ctx sdk.Context, amount sdkmath.Int, keeper keeper.Keeper) error {

rewardsDistributer := map[string]int64{}
Expand Down Expand Up @@ -113,7 +112,7 @@ func DistributeObserverRewards(ctx sdk.Context, amount sdkmath.Int, keeper keepe
continue
}
if observerRewardUnits < 0 {
slashAmount := keeper.GetParams(ctx).ObserverSlashAmount
slashAmount := keeper.GetParamSetIfExists(ctx).ObserverSlashAmount
keeper.SlashObserverEmission(ctx, observerAddress.String(), slashAmount)
finalDistributionList = append(finalDistributionList, &types.ObserverEmission{
EmissionType: types.EmissionType_Slash,
Expand Down
6 changes: 3 additions & 3 deletions x/emissions/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func TestBeginBlocker(t *testing.T) {
emissionPool := sk.AuthKeeper.GetModuleAccount(ctx, emissionstypes.ModuleName).GetAddress()

blockRewards := emissionstypes.BlockReward
observerRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParams(ctx).ObserverEmissionPercentage)).TruncateInt()
validatorRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParams(ctx).ValidatorEmissionPercentage)).TruncateInt()
tssSignerRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParams(ctx).TssSignerEmissionPercentage)).TruncateInt()
observerRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).ObserverEmissionPercentage)).TruncateInt()
validatorRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).ValidatorEmissionPercentage)).TruncateInt()
tssSignerRewardsForABlock := blockRewards.Mul(sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).TssSignerEmissionPercentage)).TruncateInt()
distributedRewards := observerRewardsForABlock.Add(validatorRewardsForABlock).Add(tssSignerRewardsForABlock)

require.True(t, blockRewards.TruncateInt().GT(distributedRewards))
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
// ExportGenesis returns the emissions module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
var genesis types.GenesisState
genesis.Params = k.GetParams(ctx)
genesis.Params = k.GetParamSetIfExists(ctx)
genesis.WithdrawableEmissions = k.GetAllWithdrawableEmission(ctx)

return &genesis
Expand Down
10 changes: 5 additions & 5 deletions x/emissions/keeper/block_rewards_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func (k Keeper) GetBlockRewardComponents(ctx sdk.Context) (sdk.Dec, sdk.Dec, sdk
return reservesFactor, bondFactor, durationFactor
}
func (k Keeper) GetBondFactor(ctx sdk.Context, stakingKeeper types.StakingKeeper) sdk.Dec {
targetBondRatio := sdk.MustNewDecFromStr(k.GetParams(ctx).TargetBondRatio)
maxBondFactor := sdk.MustNewDecFromStr(k.GetParams(ctx).MaxBondFactor)
minBondFactor := sdk.MustNewDecFromStr(k.GetParams(ctx).MinBondFactor)
targetBondRatio := sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).TargetBondRatio)
maxBondFactor := sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).MaxBondFactor)
minBondFactor := sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).MinBondFactor)

Check warning on line 22 in x/emissions/keeper/block_rewards_components.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/block_rewards_components.go#L20-L22

Added lines #L20 - L22 were not covered by tests

currentBondedRatio := stakingKeeper.BondedRatio(ctx)
// Bond factor ranges between minBondFactor (0.75) to maxBondFactor (1.25)
Expand All @@ -37,10 +37,10 @@ func (k Keeper) GetBondFactor(ctx sdk.Context, stakingKeeper types.StakingKeeper
}

func (k Keeper) GetDurationFactor(ctx sdk.Context) sdk.Dec {
avgBlockTime := sdk.MustNewDecFromStr(k.GetParams(ctx).AvgBlockTime)
avgBlockTime := sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).AvgBlockTime)

Check warning on line 40 in x/emissions/keeper/block_rewards_components.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/block_rewards_components.go#L40

Added line #L40 was not covered by tests
NumberOfBlocksInAMonth := sdk.NewDec(types.SecsInMonth).Quo(avgBlockTime)
monthFactor := sdk.NewDec(ctx.BlockHeight()).Quo(NumberOfBlocksInAMonth)
logValueDec := sdk.MustNewDecFromStr(k.GetParams(ctx).DurationFactorConstant)
logValueDec := sdk.MustNewDecFromStr(k.GetParamSetIfExists(ctx).DurationFactorConstant)

Check warning on line 43 in x/emissions/keeper/block_rewards_components.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/block_rewards_components.go#L43

Added line #L43 was not covered by tests
// month * log(1 + 0.02 / 12)
fractionNumerator := monthFactor.Mul(logValueDec)
// (month * log(1 + 0.02 / 12) ) + 1
Expand Down
35 changes: 35 additions & 0 deletions x/emissions/keeper/distributions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package keeper

import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/x/emissions/types"
)

// GetDistributions returns the current distribution of rewards
// for validators, observers and TSS signers
// If the percentage is not set, it returns 0
func (k Keeper) GetDistributions(ctx sdk.Context) (sdkmath.Int, sdkmath.Int, sdkmath.Int) {

Check warning on line 12 in x/emissions/keeper/distributions.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/distributions.go#L12

Added line #L12 was not covered by tests
// Fetch the validator rewards, use 0 if the percentage is not set
validatorRewards := sdk.NewInt(0)
validatorRewardsDec, err := sdk.NewDecFromStr(k.GetParamSetIfExists(ctx).ValidatorEmissionPercentage)
if err == nil {
validatorRewards = validatorRewardsDec.Mul(types.BlockReward).TruncateInt()

Check warning on line 17 in x/emissions/keeper/distributions.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/distributions.go#L14-L17

Added lines #L14 - L17 were not covered by tests
}

// Fetch the observer rewards, use 0 if the percentage is not set
observerRewards := sdk.NewInt(0)
observerRewardsDec, err := sdk.NewDecFromStr(k.GetParamSetIfExists(ctx).ObserverEmissionPercentage)
if err == nil {
observerRewards = observerRewardsDec.Mul(types.BlockReward).TruncateInt()

Check warning on line 24 in x/emissions/keeper/distributions.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/distributions.go#L21-L24

Added lines #L21 - L24 were not covered by tests
}

// Fetch the TSS signer rewards, use 0 if the percentage is not set
tssSignerRewards := sdk.NewInt(0)
tssSignerRewardsDec, err := sdk.NewDecFromStr(k.GetParamSetIfExists(ctx).TssSignerEmissionPercentage)
if err == nil {
tssSignerRewards = tssSignerRewardsDec.Mul(types.BlockReward).TruncateInt()

Check warning on line 31 in x/emissions/keeper/distributions.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/distributions.go#L28-L31

Added lines #L28 - L31 were not covered by tests
}

return validatorRewards, observerRewards, tssSignerRewards

Check warning on line 34 in x/emissions/keeper/distributions.go

View check run for this annotation

Codecov / codecov/patch

x/emissions/keeper/distributions.go#L34

Added line #L34 was not covered by tests
}
2 changes: 1 addition & 1 deletion x/emissions/keeper/grpc_query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types
}
ctx := sdk.UnwrapSDKContext(c)

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
return &types.QueryParamsResponse{Params: k.GetParamSetIfExists(ctx)}, nil
}
7 changes: 4 additions & 3 deletions x/emissions/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"github.com/zeta-chain/zetacore/x/emissions/types"
)

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, &params)
// GetParamSetIfExists get all parameters as types.Params if they exist
// non existent parameters will return zero values
func (k Keeper) GetParamSetIfExists(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSetIfExists(ctx, &params)
return
}

Expand Down
4 changes: 2 additions & 2 deletions x/emissions/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func TestKeeper_GetParams(t *testing.T) {
}, tt.isPanic)

if tt.isPanic != "" {
require.Equal(t, emissionstypes.DefaultParams(), k.GetParams(ctx))
require.Equal(t, emissionstypes.DefaultParams(), k.GetParamSetIfExists(ctx))
} else {
require.Equal(t, tt.params, k.GetParams(ctx))
require.Equal(t, tt.params, k.GetParamSetIfExists(ctx))
}
})
}
Expand Down

0 comments on commit 4e2b0ed

Please sign in to comment.