Skip to content

Commit

Permalink
move distribution in types and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Mar 9, 2024
1 parent 6c07f0f commit 12e37a7
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 51 deletions.
38 changes: 27 additions & 11 deletions testutil/keeper/emissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ import (
)

type EmissionMockOptions struct {
UseBankMock bool
UseStakingMock bool
UseObserverMock bool
UseAccountMock bool
UseBankMock bool
UseStakingMock bool
UseObserverMock bool
UseAccountMock bool
UseParamStoreMock bool
}

func EmissionsKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers, ZetaKeepers) {
return EmissionKeeperWithMockOptions(t, EmissionMockOptions{
UseBankMock: false,
UseStakingMock: false,
UseObserverMock: false,
})
return EmissionKeeperWithMockOptions(t, EmissionMockOptions{})
}
func EmissionKeeperWithMockOptions(
t testing.TB,
Expand Down Expand Up @@ -91,18 +88,31 @@ func EmissionKeeperWithMockOptions(
observerKeeper = emissionsmocks.NewEmissionObserverKeeper(t)
}

var paramStore types.ParamStore
if mockOptions.UseParamStoreMock {
mock := emissionsmocks.NewEmissionParamStore(t)
// mock this method for the keeper constructor
mock.On("HasKeyTable").Maybe().Return(true)
paramStore = mock
} else {
paramStore = sdkKeepers.ParamsKeeper.Subspace(types.ModuleName)
}

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
sdkKeepers.ParamsKeeper.Subspace(types.ModuleName),
paramStore,
authtypes.FeeCollectorName,
bankKeeper,
stakingKeeper,
observerKeeper,
authKeeper,
)
k.SetParams(ctx, types.DefaultParams())

if !mockOptions.UseParamStoreMock {
k.SetParams(ctx, types.DefaultParams())
}

return k, ctx, sdkKeepers, zetaKeepers
}
Expand All @@ -112,3 +122,9 @@ func GetEmissionsBankMock(t testing.TB, keeper *keeper.Keeper) *emissionsmocks.E
require.True(t, ok)
return cbk
}

func GetEmissionsParamStoreMock(t testing.TB, keeper *keeper.Keeper) *emissionsmocks.EmissionParamStore {
m, ok := keeper.GetParamStore().(*emissionsmocks.EmissionParamStore)
require.True(t, ok)
return m
}
2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/fungible.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/emissions/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/emissions/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/emissions/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/emissions/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/evm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions testutil/keeper/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ type EmissionObserverKeeper interface {
emissionstypes.ObserverKeeper
}

//go:generate mockery --name EmissionParamStore --filename param_store.go --case underscore --output ./emissions
type EmissionParamStore interface {
emissionstypes.ParamStore
}

/**
* Observer Mocks
*/
Expand Down
2 changes: 1 addition & 1 deletion testutil/keeper/mocks/observer/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/observer/slashing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/observer/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion x/emissions/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
}

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

// TODO : Replace hardcoded slash amount with a parameter
// https://github.com/zeta-chain/node/pull/1861
Expand Down
11 changes: 7 additions & 4 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
paramStore types.ParamStore
feeCollectorName string
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
Expand All @@ -30,7 +29,7 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
ps types.ParamStore,
feeCollectorName string,
bankKeeper types.BankKeeper,
stakingKeeper types.StakingKeeper,
Expand All @@ -46,7 +45,7 @@ func NewKeeper(
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
paramStore: ps,
feeCollectorName: feeCollectorName,
bankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
Expand Down Expand Up @@ -78,3 +77,7 @@ func (k Keeper) GetObserverKeeper() types.ObserverKeeper {
func (k Keeper) GetAuthKeeper() types.AccountKeeper {
return k.authKeeper
}

func (k Keeper) GetParamStore() types.ParamStore {
return k.paramStore
}
4 changes: 2 additions & 2 deletions x/emissions/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
// 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)
k.paramStore.GetParamSetIfExists(ctx, &params)
return
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
k.paramStore.SetParamSet(ctx, &params)
}
13 changes: 5 additions & 8 deletions x/emissions/types/distributions.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package keeper
package types

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) {
params := k.GetParamSetIfExists(ctx)

func GetDistributions(params Params) (sdkmath.Int, sdkmath.Int, sdkmath.Int) {
// Fetch the validator rewards, use 0 if the percentage is not set
validatorRewards := sdk.NewInt(0)
validatorRewardsDec, err := sdk.NewDecFromStr(params.ValidatorEmissionPercentage)
if err == nil {
validatorRewards = validatorRewardsDec.Mul(types.BlockReward).TruncateInt()
validatorRewards = validatorRewardsDec.Mul(BlockReward).TruncateInt()
}

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

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

return validatorRewards, observerRewards, tssSignerRewards
Expand Down
30 changes: 23 additions & 7 deletions x/emissions/types/distributions_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package keeper_test
package types_test

import (
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/emissions/types"
)

func TestKeeper_GetDistributions(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionsKeeper(t)
t.Run("Return fractions of block reward", func(t *testing.T) {
val, obs, tss := types.GetDistributions(types.Params{
ValidatorEmissionPercentage: "0.5",
ObserverEmissionPercentage: "0.25",
TssSignerEmissionPercentage: "0.25",
})

val, obs, tss := k.GetDistributions(ctx)
require.EqualValues(t, "4810474537037037037", val.String()) // 0.5 * block reward
require.EqualValues(t, "2405237268518518518", obs.String()) // 0.25 * block reward
require.EqualValues(t, "2405237268518518518", tss.String()) // 0.25 * block reward
})

require.EqualValues(t, "4810474537037037037", val.String()) // 0.5 * block reward
require.EqualValues(t, "2405237268518518518", obs.String()) // 0.25 * block reward
require.EqualValues(t, "2405237268518518518", tss.String()) // 0.25 * block reward
t.Run("Return zero in case of invalid string", func(t *testing.T) {
val, obs, tss := types.GetDistributions(types.Params{
ValidatorEmissionPercentage: "invalid",
ObserverEmissionPercentage: "invalid",
TssSignerEmissionPercentage: "invalid",
})

require.True(t, val.IsZero())
require.True(t, obs.IsZero())
require.True(t, tss.IsZero())
})
}
9 changes: 9 additions & 0 deletions x/emissions/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

Expand All @@ -28,3 +29,11 @@ type BankKeeper interface {
type StakingKeeper interface {
BondedRatio(ctx sdk.Context) sdk.Dec
}

// ParamStore defines the expected paramstore methods to store and load Params (noalias)
type ParamStore interface {
GetParamSetIfExists(ctx sdk.Context, ps paramstypes.ParamSet)
SetParamSet(ctx sdk.Context, ps paramstypes.ParamSet)
WithKeyTable(table paramstypes.KeyTable) paramstypes.Subspace
HasKeyTable() bool
}

0 comments on commit 12e37a7

Please sign in to comment.