mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 11:17:28 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									102cc0fff3
								
							
						
					
					
						commit
						ae4cb15d73
					
				@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"math/rand"
 | 
						"math/rand"
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -536,3 +537,31 @@ func NewFundedGenStateWithSameCoinsWithModuleAccount(cdc codec.JSONCodec, coins
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return builder.BuildMarshalled(cdc)
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sdk "github.com/cosmos/cosmos-sdk/types"
 | 
						sdk "github.com/cosmos/cosmos-sdk/types"
 | 
				
			||||||
 | 
						"github.com/kava-labs/kava/x/community/types"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CheckAndDisableMintAndKavaDistInflation compares the disable inflation time and block time,
 | 
					// CheckAndDisableMintAndKavaDistInflation compares the disable inflation time and block time,
 | 
				
			||||||
@ -27,6 +28,11 @@ func (k Keeper) CheckAndDisableMintAndKavaDistInflation(ctx sdk.Context) {
 | 
				
			|||||||
	params.StakingRewardsPerSecond = params.UpgradeTimeSetStakingRewardsPerSecond
 | 
						params.StakingRewardsPerSecond = params.UpgradeTimeSetStakingRewardsPerSecond
 | 
				
			||||||
	k.SetParams(ctx, params)
 | 
						k.SetParams(ctx, params)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx.EventManager().EmitEvent(
 | 
				
			||||||
 | 
							sdk.NewEvent(
 | 
				
			||||||
 | 
								types.EventTypeInflationStop,
 | 
				
			||||||
 | 
							),
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: double check this is correct method for disabling inflation in kavadist without
 | 
					// TODO: double check this is correct method for disabling inflation in kavadist without
 | 
				
			||||||
 | 
				
			|||||||
@ -51,6 +51,15 @@ func (k Keeper) PayoutAccumulatedStakingRewards(ctx sdk.Context) {
 | 
				
			|||||||
			// occur in cases where the chain is running in an invalid state
 | 
								// occur in cases where the chain is running in an invalid state
 | 
				
			||||||
			panic(err)
 | 
								panic(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// emit event with amount transferred
 | 
				
			||||||
 | 
							ctx.EventManager().EmitEvent(
 | 
				
			||||||
 | 
								sdk.NewEvent(
 | 
				
			||||||
 | 
									types.EventTypeStakingRewardsPaid,
 | 
				
			||||||
 | 
									sdk.NewAttribute(types.AttributeKeyStakingRewardAmount, transferAmount.String()),
 | 
				
			||||||
 | 
								),
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// update accumulation state
 | 
						// update accumulation state
 | 
				
			||||||
@ -60,8 +69,6 @@ func (k Keeper) PayoutAccumulatedStakingRewards(ctx sdk.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// save state
 | 
						// save state
 | 
				
			||||||
	k.SetStakingRewardsState(ctx, state)
 | 
						k.SetStakingRewardsState(ctx, state)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// calculateStakingRewards takees the currentBlockTime, state of last accumulation, rewards per second, and the community pool balance
 | 
					// calculateStakingRewards takees the currentBlockTime, state of last accumulation, rewards per second, and the community pool balance
 | 
				
			||||||
 | 
				
			|||||||
@ -93,6 +93,8 @@ func (suite *disableInflationTestSuite) TestDisableInflation() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			expectedKavadistState.Params.Active = false
 | 
								expectedKavadistState.Params.Active = false
 | 
				
			||||||
			msgSuffix = "after upgrade"
 | 
								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)
 | 
							suite.Require().Equal(expectedMintState.Params.InflationMin, mintParams.InflationMin, msg+": expected mint inflation min to match state "+msgSuffix)
 | 
				
			||||||
 | 
				
			|||||||
@ -258,10 +258,15 @@ func (suite *stakingRewardsTestSuite) TestStakingRewards() {
 | 
				
			|||||||
			params.StakingRewardsPerSecond = tc.rewardsPerSecond
 | 
								params.StakingRewardsPerSecond = tc.rewardsPerSecond
 | 
				
			||||||
			keeper.SetParams(ctx, params)
 | 
								keeper.SetParams(ctx, params)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								stakingRewardEvents := sdk.Events{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for {
 | 
								for {
 | 
				
			||||||
				// run community begin blocker logic
 | 
									// run community begin blocker logic
 | 
				
			||||||
				suite.testFunc(ctx, keeper)
 | 
									suite.testFunc(ctx, keeper)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// accumulate event rewards from events
 | 
				
			||||||
 | 
									stakingRewardEvents = append(stakingRewardEvents, filterStakingRewardEvents(ctx.EventManager().Events())...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// exit loop if we are at last block
 | 
									// exit loop if we are at last block
 | 
				
			||||||
				if blockTime.Equal(tc.periodEnd) {
 | 
									if blockTime.Equal(tc.periodEnd) {
 | 
				
			||||||
					break
 | 
										break
 | 
				
			||||||
@ -287,6 +292,20 @@ func (suite *stakingRewardsTestSuite) TestStakingRewards() {
 | 
				
			|||||||
			// assert fee pool was payed the correct rewards
 | 
								// assert fee pool was payed the correct rewards
 | 
				
			||||||
			suite.Equal(tc.expectedRewardsTotal.String(), feeCollectorBalanceAdded.String(), "expected fee collector balance to match")
 | 
								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
 | 
								// assert the community pool deducted the same amount
 | 
				
			||||||
			expectedCommunityPoolBalance := tc.communityPoolFunds.Sub(tc.expectedRewardsTotal)
 | 
								expectedCommunityPoolBalance := tc.communityPoolFunds.Sub(tc.expectedRewardsTotal)
 | 
				
			||||||
			actualCommunityPoolBalance := bankKeeper.GetBalance(ctx, poolAcc.GetAddress(), "ukava").Amount
 | 
								actualCommunityPoolBalance := bankKeeper.GetBalance(ctx, poolAcc.GetAddress(), "ukava").Amount
 | 
				
			||||||
@ -373,3 +392,30 @@ func newIntFromString(str string) sdkmath.Int {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return num
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,11 @@ package types
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Community module event types
 | 
					// Community module event types
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
 | 
						EventTypeInflationStop      = "inflation_stop"
 | 
				
			||||||
 | 
						EventTypeStakingRewardsPaid = "staking_rewards_paid"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						AttributeKeyStakingRewardAmount = "staking_reward_amount"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	AttributeValueFundCommunityPool = "fund_community_pool"
 | 
						AttributeValueFundCommunityPool = "fund_community_pool"
 | 
				
			||||||
	AttributeValueCategory          = ModuleName
 | 
						AttributeValueCategory          = ModuleName
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user