From a735de4adf49cd221636d03968effdfe196db7fc Mon Sep 17 00:00:00 2001 From: MiniFrenchBread <103425574+MiniFrenchBread@users.noreply.github.com> Date: Tue, 13 Aug 2024 17:01:10 +0800 Subject: [PATCH] feat: add params event --- x/dasigners/v1/keeper/keeper.go | 16 +++++++++++++++- x/dasigners/v1/keeper/msg_server_test.go | 16 ++++++++++++++++ x/dasigners/v1/types/events.go | 15 +++++++++++---- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/x/dasigners/v1/keeper/keeper.go b/x/dasigners/v1/keeper/keeper.go index 047d1813..fd0e63b9 100644 --- a/x/dasigners/v1/keeper/keeper.go +++ b/x/dasigners/v1/keeper/keeper.go @@ -2,18 +2,21 @@ package keeper import ( "encoding/hex" + "fmt" "math/big" "cosmossdk.io/math" "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/prefix" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/0glabs/0g-chain/chaincfg" "github.com/0glabs/0g-chain/x/dasigners/v1/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) var BondedConversionRate = math.NewIntFromBigInt(big.NewInt(0).Exp(big.NewInt(10), big.NewInt(chaincfg.GasDenomUnit), nil)) @@ -57,6 +60,17 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(¶ms) store.Set(types.ParamsKey, bz) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventTypeUpdateParams, + sdk.NewAttribute(types.AttributeKeyBlockHeight, fmt.Sprint(ctx.BlockHeader().Height)), + sdk.NewAttribute(types.AttributeKeyTokensPerVote, fmt.Sprint(params.TokensPerVote)), + sdk.NewAttribute(types.AttributeKeyMaxQuorums, fmt.Sprint(params.MaxQuorums)), + sdk.NewAttribute(types.AttributeKeyEpochBlocks, fmt.Sprint(params.EpochBlocks)), + sdk.NewAttribute(types.AttributeKeyEncodedSlices, fmt.Sprint(params.EncodedSlices)), + ), + ) } func (k Keeper) GetEpochNumber(ctx sdk.Context) (uint64, error) { diff --git a/x/dasigners/v1/keeper/msg_server_test.go b/x/dasigners/v1/keeper/msg_server_test.go index d6934ba6..6223620f 100644 --- a/x/dasigners/v1/keeper/msg_server_test.go +++ b/x/dasigners/v1/keeper/msg_server_test.go @@ -1,10 +1,12 @@ package keeper_test import ( + "fmt" "testing" "github.com/0glabs/0g-chain/x/dasigners/v1/testutil" "github.com/0glabs/0g-chain/x/dasigners/v1/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" ) @@ -53,6 +55,7 @@ func (suite *MsgServerTestSuite) TestChangeParams() { } for _, tc := range testCases { suite.Run(tc.name, func() { + oldEventNum := len(suite.Ctx.EventManager().Events()) _, err := suite.Keeper.ChangeParams(suite.Ctx, tc.req) if tc.expectErr { suite.Require().Error(err) @@ -61,6 +64,19 @@ func (suite *MsgServerTestSuite) TestChangeParams() { suite.Require().NoError(err) params := suite.Keeper.GetParams(suite.Ctx) suite.Require().EqualValues(*tc.req.Params, params) + suite.Assert().NoError(err) + + events := suite.Ctx.EventManager().Events() + suite.Assert().EqualValues(len(events), oldEventNum+1) + suite.Assert().EqualValues(events[len(events)-1], sdk.NewEvent( + types.EventTypeUpdateParams, + sdk.NewAttribute(types.AttributeKeyBlockHeight, fmt.Sprint(suite.Ctx.BlockHeader().Height)), + sdk.NewAttribute(types.AttributeKeyTokensPerVote, fmt.Sprint(params.TokensPerVote)), + sdk.NewAttribute(types.AttributeKeyMaxQuorums, fmt.Sprint(params.MaxQuorums)), + sdk.NewAttribute(types.AttributeKeyEpochBlocks, fmt.Sprint(params.EpochBlocks)), + sdk.NewAttribute(types.AttributeKeyEncodedSlices, fmt.Sprint(params.EncodedSlices)), + ), + ) } }) } diff --git a/x/dasigners/v1/types/events.go b/x/dasigners/v1/types/events.go index 00927dae..6ffb4d71 100644 --- a/x/dasigners/v1/types/events.go +++ b/x/dasigners/v1/types/events.go @@ -3,9 +3,16 @@ package types // Module event types const ( EventTypeUpdateSigner = "update_signer" + EventTypeUpdateParams = "update_params" - AttributeKeySigner = "signer" - AttributeKeySocket = "socket" - AttributeKeyPublicKeyG1 = "pubkey_g1" - AttributeKeyPublicKeyG2 = "pubkey_g2" + AttributeKeySigner = "signer" + AttributeKeySocket = "socket" + AttributeKeyPublicKeyG1 = "pubkey_g1" + AttributeKeyPublicKeyG2 = "pubkey_g2" + AttributeKeyBlockHeight = "block_height" + AttributeKeyTokensPerVote = "tokens_per_vote" + AttributeKeyMaxVotesPerSigner = "max_votes_per_signer" + AttributeKeyMaxQuorums = "max_quorums" + AttributeKeyEpochBlocks = "epoch_blocks" + AttributeKeyEncodedSlices = "encoded_slices" )