Emit events for inflation disable and staking rewards (#1743)

* Emit events for staking rewards

* Update test to check disable inflation check

* Only emit staking rewards event when non-zero value

* use existing transfer amount for event

* add assertion that no events are emitted for zero rewards; refactor
event assertions to be a little cleaner

---------

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
This commit is contained in:
drklee3 2023-10-03 15:10:22 -07:00 committed by GitHub
parent 102cc0fff3
commit ae4cb15d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
@ -536,3 +537,31 @@ func NewFundedGenStateWithSameCoinsWithModuleAccount(cdc codec.JSONCodec, coins
return builder.BuildMarshalled(cdc)
}
// EventsContains returns an error if the expected event is not in the provided events
func EventsContains(events sdk.Events, expectedEvent sdk.Event) error {
foundMatch := false
for _, event := range events {
if event.Type == expectedEvent.Type {
if reflect.DeepEqual(attrsToMap(expectedEvent.Attributes), attrsToMap(event.Attributes)) {
foundMatch = true
}
}
}
if !foundMatch {
return fmt.Errorf("event of type %s not found or did not match", expectedEvent.Type)
}
return nil
}
func attrsToMap(attrs []abci.EventAttribute) []sdk.Attribute { // new cosmos changed the event attribute type
out := []sdk.Attribute{}
for _, attr := range attrs {
out = append(out, sdk.NewAttribute(string(attr.Key), string(attr.Value)))
}
return out
}

View File

@ -4,6 +4,7 @@ import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/community/types"
)
// CheckAndDisableMintAndKavaDistInflation compares the disable inflation time and block time,
@ -27,6 +28,11 @@ func (k Keeper) CheckAndDisableMintAndKavaDistInflation(ctx sdk.Context) {
params.StakingRewardsPerSecond = params.UpgradeTimeSetStakingRewardsPerSecond
k.SetParams(ctx, params)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeInflationStop,
),
)
}
// TODO: double check this is correct method for disabling inflation in kavadist without

View File

@ -51,6 +51,15 @@ func (k Keeper) PayoutAccumulatedStakingRewards(ctx sdk.Context) {
// occur in cases where the chain is running in an invalid state
panic(err)
}
// emit event with amount transferred
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeStakingRewardsPaid,
sdk.NewAttribute(types.AttributeKeyStakingRewardAmount, transferAmount.String()),
),
)
}
// update accumulation state
@ -60,8 +69,6 @@ func (k Keeper) PayoutAccumulatedStakingRewards(ctx sdk.Context) {
// save state
k.SetStakingRewardsState(ctx, state)
return
}
// calculateStakingRewards takees the currentBlockTime, state of last accumulation, rewards per second, and the community pool balance

View File

@ -93,6 +93,8 @@ func (suite *disableInflationTestSuite) TestDisableInflation() {
expectedKavadistState.Params.Active = false
msgSuffix = "after upgrade"
suite.Require().NoError(app.EventsContains(suite.Ctx.EventManager().Events(), sdk.NewEvent(types.EventTypeInflationStop)))
}
suite.Require().Equal(expectedMintState.Params.InflationMin, mintParams.InflationMin, msg+": expected mint inflation min to match state "+msgSuffix)

View File

@ -258,10 +258,15 @@ func (suite *stakingRewardsTestSuite) TestStakingRewards() {
params.StakingRewardsPerSecond = tc.rewardsPerSecond
keeper.SetParams(ctx, params)
stakingRewardEvents := sdk.Events{}
for {
// run community begin blocker logic
suite.testFunc(ctx, keeper)
// accumulate event rewards from events
stakingRewardEvents = append(stakingRewardEvents, filterStakingRewardEvents(ctx.EventManager().Events())...)
// exit loop if we are at last block
if blockTime.Equal(tc.periodEnd) {
break
@ -287,6 +292,20 @@ func (suite *stakingRewardsTestSuite) TestStakingRewards() {
// assert fee pool was payed the correct rewards
suite.Equal(tc.expectedRewardsTotal.String(), feeCollectorBalanceAdded.String(), "expected fee collector balance to match")
if tc.expectedRewardsTotal.IsZero() {
suite.Equal(0, len(stakingRewardEvents), "expected no events to be emitted")
} else {
// we add up all reward coin events
eventCoins := getRewardCoinsFromEvents(stakingRewardEvents)
// assert events emitted match expected rewards
suite.Equal(
tc.expectedRewardsTotal.String(),
eventCoins.AmountOf("ukava").String(),
"expected event coins to match",
)
}
// assert the community pool deducted the same amount
expectedCommunityPoolBalance := tc.communityPoolFunds.Sub(tc.expectedRewardsTotal)
actualCommunityPoolBalance := bankKeeper.GetBalance(ctx, poolAcc.GetAddress(), "ukava").Amount
@ -373,3 +392,30 @@ func newIntFromString(str string) sdkmath.Int {
}
return num
}
func filterStakingRewardEvents(events sdk.Events) (rewardEvents sdk.Events) {
for _, event := range events {
if event.Type == types.EventTypeStakingRewardsPaid {
rewardEvents = append(rewardEvents, event)
}
}
return
}
func getRewardCoinsFromEvents(events sdk.Events) sdk.Coins {
coins := sdk.NewCoins()
for _, event := range events {
if event.Type == types.EventTypeStakingRewardsPaid {
rewards, err := sdk.ParseCoinNormalized(string(event.Attributes[0].Value))
if err != nil {
panic(err)
}
coins = coins.Add(rewards)
}
}
return coins
}

View File

@ -2,6 +2,11 @@ package types
// Community module event types
const (
EventTypeInflationStop = "inflation_stop"
EventTypeStakingRewardsPaid = "staking_rewards_paid"
AttributeKeyStakingRewardAmount = "staking_reward_amount"
AttributeValueFundCommunityPool = "fund_community_pool"
AttributeValueCategory = ModuleName
)