From 83a865315c96b7e1d26ce110790a2da336604607 Mon Sep 17 00:00:00 2001 From: Kevin Pita Date: Mon, 4 May 2026 16:53:30 +0200 Subject: [PATCH] fix: poa emit post-create validator tokens --- x/poa/keeper/keeper.go | 7 +++- x/poa/keeper/keeper_test.go | 66 +++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/x/poa/keeper/keeper.go b/x/poa/keeper/keeper.go index 5a6af7b..9bec61f 100644 --- a/x/poa/keeper/keeper.go +++ b/x/poa/keeper/keeper.go @@ -192,13 +192,18 @@ func (k Keeper) ExecuteAddValidator(ctx sdk.Context, msg *types.MsgAddValidator) if err != nil { return err } + // Fetch the validator after creation so the event reports the post-create tokens. + newValidator, err := k.sk.GetValidator(ctx, valAddress) + if err != nil { + return err + } ctx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeAddValidator, sdk.NewAttribute(types.AttributeValidator, accAddress.String()), sdk.NewAttribute(types.AttributeHeight, fmt.Sprintf("%d", ctx.BlockHeight())), - sdk.NewAttribute(types.AttributeStakingTokens, fmt.Sprintf("%d", validator.Tokens)), + sdk.NewAttribute(types.AttributeStakingTokens, fmt.Sprintf("%d", newValidator.Tokens)), sdk.NewAttribute(types.AttributeBankTokens, balance.String()), ), ) diff --git a/x/poa/keeper/keeper_test.go b/x/poa/keeper/keeper_test.go index 9010397..9ce78ee 100644 --- a/x/poa/keeper/keeper_test.go +++ b/x/poa/keeper/keeper_test.go @@ -2,6 +2,7 @@ package keeper import ( "errors" + "fmt" "testing" "cosmossdk.io/math" @@ -50,6 +51,18 @@ func poaKeeperTestSetup(t *testing.T) (*Keeper, sdk.Context) { return setupPoaKeeper(t, stakingExpectations, bankExpectations) } +func successfulAddValidatorStakingMocks(ctx sdk.Context, stakingKeeper *testutil.MockStakingKeeper) { + stakingKeeper.EXPECT().GetParams(ctx).Return(stakingtypes.Params{ + BondDenom: "BND", + MaxValidators: 2, + }, nil) + stakingKeeper.EXPECT().GetAllValidators(ctx).Return([]stakingtypes.Validator{}, nil) + stakingKeeper.EXPECT().GetValidator(ctx, gomock.Any()).Return(stakingtypes.Validator{Tokens: math.NewInt(0)}, nil).Times(1) + stakingKeeper.EXPECT().GetAllDelegatorDelegations(ctx, gomock.Any()).Return([]stakingtypes.Delegation{}, nil) + stakingKeeper.EXPECT().GetUnbondingDelegationsFromValidator(ctx, gomock.Any()).Return([]stakingtypes.UnbondingDelegation{}, nil) + stakingKeeper.EXPECT().GetValidator(ctx, gomock.Any()).Return(stakingtypes.Validator{Tokens: sdk.DefaultPowerReduction}, nil).Times(1) +} + // Define here Keeper methods to be unit tested func TestKeeper_ExecuteAddValidator(t *testing.T) { ctrl := gomock.NewController(t) @@ -347,16 +360,7 @@ func TestKeeper_ExecuteAddValidator(t *testing.T) { name: "should pass - MsgAddValidator", validatorAddress: "ethm1a0pd5cyew47pvgf7rd7axxy3humv9ev0nnkprp", pubKey: msgPubKey, - stakingMocks: func(ctx sdk.Context, stakingKeeper *testutil.MockStakingKeeper) { - stakingKeeper.EXPECT().GetParams(ctx).Return(stakingtypes.Params{ - BondDenom: "BND", - MaxValidators: 2, - }, nil) - stakingKeeper.EXPECT().GetAllValidators(ctx).Return([]stakingtypes.Validator{}, nil) - stakingKeeper.EXPECT().GetValidator(ctx, gomock.Any()).Return(stakingtypes.Validator{Tokens: math.NewInt(0)}, nil) - stakingKeeper.EXPECT().GetAllDelegatorDelegations(ctx, gomock.Any()).Return([]stakingtypes.Delegation{}, nil) - stakingKeeper.EXPECT().GetUnbondingDelegationsFromValidator(ctx, gomock.Any()).Return([]stakingtypes.UnbondingDelegation{}, nil) - }, + stakingMocks: successfulAddValidatorStakingMocks, bankMocks: func(ctx sdk.Context, bankKeeper *testutil.MockBankKeeper) { bankKeeper.EXPECT().GetBalance(ctx, gomock.Any(), gomock.Any()).Return(sdk.Coin{ Denom: "BND", @@ -386,6 +390,7 @@ func TestKeeper_ExecuteAddValidator(t *testing.T) { }, }, nil) stakingKeeper.EXPECT().GetUnbondingDelegationsFromValidator(ctx, gomock.Any()).Return([]stakingtypes.UnbondingDelegation{}, nil) + stakingKeeper.EXPECT().GetValidator(ctx, gomock.Any()).Return(stakingtypes.Validator{Tokens: sdk.DefaultPowerReduction}, nil).Times(1) }, bankMocks: func(ctx sdk.Context, bankKeeper *testutil.MockBankKeeper) { bankKeeper.EXPECT().GetBalance(ctx, gomock.Any(), gomock.Any()).Return(sdk.Coin{ @@ -425,6 +430,47 @@ func TestKeeper_ExecuteAddValidator(t *testing.T) { } } +func TestKeeper_ExecuteAddValidator_EmitsPostCreateStakingTokens(t *testing.T) { + ctrl := gomock.NewController(t) + pubKey := testutil.NewMockPubKey(ctrl) + msgPubKey, _ := types1.NewAnyWithValue(pubKey) + + keeper, ctx := setupPoaKeeper( + t, + successfulAddValidatorStakingMocks, + func(ctx sdk.Context, bankKeeper *testutil.MockBankKeeper) { + bankKeeper.EXPECT().GetBalance(ctx, gomock.Any(), gomock.Any()).Return(sdk.Coin{ + Denom: "BND", + Amount: math.NewInt(0), + }) + bankKeeper.EXPECT().MintCoins(ctx, gomock.Any(), gomock.Any()).Return(nil) + bankKeeper.EXPECT().SendCoinsFromModuleToAccount(ctx, gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + }, + ) + + msg := &types.MsgAddValidator{ + Authority: keeper.GetAuthority(), + ValidatorAddress: "ethm1a0pd5cyew47pvgf7rd7axxy3humv9ev0nnkprp", + Description: stakingtypes.Description{ + Moniker: "test", + }, + Pubkey: msgPubKey, + } + + err := keeper.ExecuteAddValidator(ctx, msg) + require.NoError(t, err) + + events := ctx.EventManager().Events() + require.NotEmpty(t, events) + + event := events[len(events)-1] + require.Equal(t, types.EventTypeAddValidator, event.Type) + + attribute, ok := event.GetAttribute(types.AttributeStakingTokens) + require.True(t, ok) + require.Equal(t, fmt.Sprintf("%d", sdk.DefaultPowerReduction), attribute.Value) +} + func TestKeeper_ExecuteRemoveValidator(t *testing.T) { ctrl := gomock.NewController(t)