diff --git a/x/emissions/abci.go b/x/emissions/abci.go index 8e009356a0..96be4ae613 100644 --- a/x/emissions/abci.go +++ b/x/emissions/abci.go @@ -22,6 +22,14 @@ func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) { observerRewards := sdk.MustNewDecFromStr(keeper.GetParams(ctx).ObserverEmissionPercentage).Mul(blockRewards).TruncateInt() tssSignerRewards := sdk.MustNewDecFromStr(keeper.GetParams(ctx).TssSignerEmissionPercentage).Mul(blockRewards).TruncateInt() + // TODO : Replace hardcoded slash amount with a parameter + // https://github.com/zeta-chain/node/pull/1861 + slashAmount, ok := sdkmath.NewIntFromString(types.ObserverSlashAmount) + if !ok { + ctx.Logger().Error(fmt.Sprintf("Error while parsing observer slash amount %s", types.ObserverSlashAmount)) + return + } + // 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() @@ -30,7 +38,7 @@ func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) { ctx.Logger().Error(fmt.Sprintf("Error while distributing validator rewards %s", err)) return } - err = DistributeObserverRewards(tmpCtx, observerRewards, keeper) + err = DistributeObserverRewards(tmpCtx, observerRewards, keeper, slashAmount) if err != nil { ctx.Logger().Error(fmt.Sprintf("Error while distributing observer rewards %s", err)) return @@ -63,7 +71,12 @@ func DistributeValidatorRewards(ctx sdk.Context, amount sdkmath.Int, bankKeeper // 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 { +func DistributeObserverRewards( + ctx sdk.Context, + amount sdkmath.Int, + keeper keeper.Keeper, + slashAmount sdkmath.Int, +) error { rewardsDistributer := map[string]int64{} totalRewardsUnits := int64(0) @@ -114,13 +127,6 @@ func DistributeObserverRewards(ctx sdk.Context, amount sdkmath.Int, keeper keepe } if observerRewardUnits < 0 { - // TODO : Replace hardcoded slash amount with a parameter - // https://github.com/zeta-chain/node/pull/1861 - slashAmount, ok := sdkmath.NewIntFromString(types.ObserverSlashAmount) - if !ok { - continue - } - keeper.SlashObserverEmission(ctx, observerAddress.String(), slashAmount) finalDistributionList = append(finalDistributionList, &types.ObserverEmission{ EmissionType: types.EmissionType_Slash, diff --git a/x/emissions/abci_test.go b/x/emissions/abci_test.go index 07240d1450..a4a66b34a0 100644 --- a/x/emissions/abci_test.go +++ b/x/emissions/abci_test.go @@ -153,6 +153,7 @@ func TestBeginBlocker(t *testing.T) { } func TestDistributeObserverRewards(t *testing.T) { + keepertest.SetConfig(false) observerSet := sample.ObserverSet(4) tt := []struct { @@ -286,8 +287,9 @@ func TestDistributeObserverRewards(t *testing.T) { ctx = ctx.WithBlockHeight(100) // Distribute the rewards and check if the rewards are distributed correctly - err = emissionsModule.DistributeObserverRewards(ctx, tc.totalRewardsForBlock, *k) + err = emissionsModule.DistributeObserverRewards(ctx, tc.totalRewardsForBlock, *k, tc.slashAmount) require.NoError(t, err) + for i, observer := range observerSet.ObserverList { observerEmission, found := k.GetWithdrawableEmission(ctx, observer) require.True(t, found, "withdrawable emission not found for observer %d", i)