Skip to content

Commit

Permalink
test: test code for distribution & mstaking msg server (#192)
Browse files Browse the repository at this point in the history
* [Test] Test code

distribution
- msg server
- custom msg server
- grpc query server

mstaking
- msg server

* fix: custom_msg_server_test
  • Loading branch information
djm07073 authored Jun 1, 2024
1 parent 9e37292 commit 9589c4e
Show file tree
Hide file tree
Showing 6 changed files with 691 additions and 3 deletions.
30 changes: 30 additions & 0 deletions x/distribution/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,36 @@ func createValidatorWithBalance(
return valAddr
}

func createValidatorAndOperatorWithBalance(
ctx sdk.Context,
input TestKeepers,
balance int64,
delBalance int64,
index int,
) (sdk.ValAddress, sdk.AccAddress) {
valPubKey := testutilsims.CreateTestPubKeys(index)[index-1]

pubKey := secp256k1.GenPrivKey().PubKey()
accAddr := sdk.AccAddress(sdk.AccAddress(pubKey.Address()))
valAddr := sdk.ValAddress(sdk.AccAddress(pubKey.Address()))

input.Faucet.Fund(ctx, accAddr, sdk.NewCoin(bondDenom, math.NewInt(balance)))

sh := stakingkeeper.NewMsgServerImpl(input.StakingKeeper)
_, err := sh.CreateValidator(ctx, newTestMsgCreateValidator(valAddr, valPubKey, sdk.NewCoin(bondDenom, math.NewInt(delBalance))))
if err != nil {
panic(err)
}

// power update
_, err = input.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}

return valAddr, accAddr
}

func createValidatorWithCoin(
ctx sdk.Context,
input TestKeepers,
Expand Down
76 changes: 76 additions & 0 deletions x/distribution/keeper/custom_msg_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package keeper_test

import (
"testing"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types"
"github.com/initia-labs/initia/x/distribution/keeper"
customTypes "github.com/initia-labs/initia/x/distribution/types"
"github.com/stretchr/testify/require"
)

func TestDepositValidatorRewardsPool(t *testing.T) {
ctx, keepers := createDefaultTestInput(t)
customMsgServer := keeper.NewCustomMsgServerImpl(keepers.DistKeeper)
_, _, depositor := keyPubAddr()
keepers.Faucet.Mint(ctx, depositor, types.NewCoin("uinit", math.NewInt(20)))
val := createValidatorWithBalance(ctx, keepers, 100, 100, 1)

cases := []struct {
name string
msg *customTypes.MsgDepositValidatorRewardsPool
resp *customTypes.MsgDepositValidatorRewardsPoolResponse
errMsg string
}{
{
name: "success",
msg: &customTypes.MsgDepositValidatorRewardsPool{
Depositor: depositor.String(),
ValidatorAddress: val.String(),
Denom: "uinit",
Amount: types.NewCoins(types.NewCoin("uinit", math.NewInt(10))),
},

resp: &customTypes.MsgDepositValidatorRewardsPoolResponse{},
errMsg: "",
},
{
name: "invalid depositor address",
msg: &customTypes.MsgDepositValidatorRewardsPool{
Depositor: "invalid",
ValidatorAddress: val.String(),
Denom: "uinit",
Amount: types.NewCoins(types.NewCoin("uinit", math.NewInt(10))),
},
resp: &customTypes.MsgDepositValidatorRewardsPoolResponse{},
errMsg: "decoding bech32 failed: invalid bech32 string length 7",
},
{
name: "invalid validaor address",
msg: &customTypes.MsgDepositValidatorRewardsPool{
Depositor: depositor.String(),
ValidatorAddress: "invalid",
Denom: "uinit",
Amount: types.NewCoins(types.NewCoin("uinit", math.NewInt(10))),
},
resp: &customTypes.MsgDepositValidatorRewardsPoolResponse{},
errMsg: "decoding bech32 failed: invalid bech32 string length 7",
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
out, err := customMsgServer.DepositValidatorRewardsPool(ctx, tc.msg)
if tc.errMsg == "" {
require.NoError(t, err)
require.Equal(t, tc.resp, out)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
}
})
}

}
90 changes: 90 additions & 0 deletions x/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package keeper_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/initia-labs/initia/x/distribution/keeper"
"github.com/stretchr/testify/require"
)

func TestQueryParams(t *testing.T) {
ctx, testKp := createDefaultTestInput(t)
queryServer := keeper.NewQueryServer(testKp.DistKeeper)

cases := []struct {
name string
req *types.QueryParamsRequest
resp *types.QueryParamsResponse
errMsg string
}{
{
name: "success",
req: &types.QueryParamsRequest{},
resp: &types.QueryParamsResponse{
Params: types.DefaultParams(),
},
errMsg: "",
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
out, err := queryServer.Params(ctx, (*types.QueryParamsRequest)(tc.req))
if tc.errMsg == "" {
require.NoError(t, err)
require.Equal(t, tc.resp, out)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
require.Nil(t, out)
}
})
}
}

func TestQueryValidatorDistributionInfo(t *testing.T) {
ctx, testKp := createDefaultTestInput(t)
queryServer := keeper.NewQueryServer(testKp.DistKeeper)
// operator, err := codectestutil.CodecOptions{}.NewInterfaceRegistry().SigningContext().ValidatorAddressCodec().BytesToString(operatorAddr)

vali, operator := createValidatorAndOperatorWithBalance(ctx, testKp, 100, 100, 1)
cases := []struct {
name string
req *types.QueryValidatorDistributionInfoRequest
resp *types.QueryValidatorDistributionInfoResponse
errMsg string
}{
{
name: "invalid validator address",
req: &types.QueryValidatorDistributionInfoRequest{
ValidatorAddress: "invalid address",
},
resp: &types.QueryValidatorDistributionInfoResponse{},
errMsg: "decoding bech32 failed",
},
{
name: "validator",
req: &types.QueryValidatorDistributionInfoRequest{
ValidatorAddress: vali.String(),
},
resp: &types.QueryValidatorDistributionInfoResponse{
OperatorAddress: operator.String(),
},
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
out, err := queryServer.ValidatorDistributionInfo(ctx, tc.req)
if tc.errMsg == "" {
require.NoError(t, err)
require.Equal(t, tc.resp, out)
} else {
require.Error(t, err)
}
})
}
}
6 changes: 3 additions & 3 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func (k Keeper) WithdrawDelegationRewards(ctx context.Context, delAddr sdk.AccAd
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawRewards,
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, rewards.Sum().String()),
sdk.NewAttribute(customtypes.AttributeKeyAmountPerPool, rewards.String()),
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()), // validator address
sdk.NewAttribute(sdk.AttributeKeyAmount, rewards.Sum().String()), // rewards
sdk.NewAttribute(customtypes.AttributeKeyAmountPerPool, rewards.String()), // rewards per pool
),
)

Expand Down
Loading

0 comments on commit 9589c4e

Please sign in to comment.