Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase test coverage of liquidstaking #386 #90

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions x/liquidstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keeper_test

import (
_ "github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -13,3 +15,54 @@ func (s *KeeperTestSuite) TestGRPCParams() {
s.Require().NoError(err)
s.Require().Equal(s.keeper.GetParams(s.ctx), resp.Params)
}

func (s *KeeperTestSuite) TestGRPCQueries() {
vals, valOpers, _ := s.CreateValidators([]int64{1000000, 2000000, 3000000})
params := s.keeper.GetParams(s.ctx)
params.MinLiquidStakingAmount = sdk.NewInt(50000)
s.keeper.SetParams(s.ctx, params)
s.keeper.UpdateLiquidValidatorSet(s.ctx)

// add active validator
params.WhitelistedValidators = []types.WhitelistedValidator{
{ValidatorAddress: valOpers[0].String(), TargetWeight: sdk.NewInt(1)},
{ValidatorAddress: valOpers[1].String(), TargetWeight: sdk.NewInt(1)},
{ValidatorAddress: valOpers[2].String(), TargetWeight: sdk.NewInt(1)},
}
s.keeper.SetParams(s.ctx, params)
s.keeper.UpdateLiquidValidatorSet(s.ctx)

// Test LiquidValidators grpc query
res := s.keeper.GetAllLiquidValidatorStates(s.ctx)
resp, err := s.querier.LiquidValidators(sdk.WrapSDKContext(s.ctx), &types.QueryLiquidValidatorsRequest{})
s.Require().NoError(err)
s.Require().Equal(resp.LiquidValidators, res)

resp, err = s.querier.LiquidValidators(sdk.WrapSDKContext(s.ctx), nil)
s.Require().Nil(resp)
s.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "invalid request"))

// Test States grpc query
respStates, err := s.querier.States(sdk.WrapSDKContext(s.ctx), &types.QueryStatesRequest{})
resNetAmountState := s.keeper.GetNetAmountState(s.ctx)
s.Require().NoError(err)
s.Require().Equal(respStates.NetAmountState, resNetAmountState)

respStates, err = s.querier.States(sdk.WrapSDKContext(s.ctx), nil)
s.Require().Nil(respStates)
s.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "invalid request"))

// Test VotingPower grpc query
respVotingPower, err := s.querier.VotingPower(sdk.WrapSDKContext(s.ctx), &types.QueryVotingPowerRequest{Voter: vals[0].String()})
resVotingPower := s.keeper.GetVotingPower(s.ctx, vals[0])
s.Require().NoError(err)
s.Require().Equal(respVotingPower.VotingPower, resVotingPower)

respVotingPower, err = s.querier.VotingPower(sdk.WrapSDKContext(s.ctx), nil)
s.Require().Nil(respVotingPower)
s.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "invalid request"))

respVotingPower, err = s.querier.VotingPower(sdk.WrapSDKContext(s.ctx), &types.QueryVotingPowerRequest{Voter: "invalidaddr"})
s.Require().Nil(respVotingPower)
s.Require().EqualError(err, "decoding bech32 failed: invalid separator index -1")
}
135 changes: 135 additions & 0 deletions x/liquidstaking/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package simulation_test

import (
"math/rand"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

chain "github.com/crescent-network/crescent/v3/app"
"github.com/crescent-network/crescent/v3/app/params"
"github.com/crescent-network/crescent/v3/x/liquidstaking/simulation"
"github.com/crescent-network/crescent/v3/x/liquidstaking/types"
minttypes "github.com/crescent-network/crescent/v3/x/mint/types"
)

// TestWeightedOperations tests the weights of the operations.
func TestWeightedOperations(t *testing.T) {
app, ctx := createTestApp(false)

ctx.WithChainID("test-chain")

cdc := types.ModuleCdc
appParams := make(simtypes.AppParams)

weightedOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper, app.BankKeeper, app.LiquidStakingKeeper)

s := rand.NewSource(2)
r := rand.New(s)
accs := getTestingAccounts(t, r, app, ctx, 10)

// setup accounts[0] as validator0 and accounts[1] as validator1
val0 := getTestingValidator0(t, app, ctx, accs)
val1 := getTestingValidator1(t, app, ctx, accs)

param := app.LiquidStakingKeeper.GetParams(ctx)
param.WhitelistedValidators = []types.WhitelistedValidator{
{
ValidatorAddress: val0.OperatorAddress,
TargetWeight: sdk.OneInt(),
},
{
ValidatorAddress: val1.OperatorAddress,
TargetWeight: sdk.OneInt(),
},
}
app.LiquidStakingKeeper.SetParams(ctx, param)

// begin a new block
blockTime := time.Now().UTC()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
app.EndBlock(abci.RequestEndBlock{Height: app.LastBlockHeight() + 1})

expected := []struct {
weight int
opMsgRoute string
opMsgName string
}{
{params.DefaultWeightMsgLiquidStake, types.ModuleName, types.TypeMsgLiquidStake},
{params.DefaultWeightMsgLiquidUnstake, types.ModuleName, types.TypeMsgLiquidUnstake},
}

for i, w := range weightedOps {
operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
// the following checks are very much dependent from the ordering of the output given
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
// will fail
require.Equal(t, expected[i].weight, w.Weight(), "weight should be the same")
require.Equal(t, expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
require.Equal(t, expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
}
}

func createTestApp(isCheckTx bool) (*chain.App, sdk.Context) {
app := chain.Setup(isCheckTx)

ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
app.MintKeeper.SetParams(ctx, minttypes.DefaultParams())
app.LiquidStakingKeeper.SetParams(ctx, types.DefaultParams())

return app, ctx
}

func getTestingAccounts(t *testing.T, r *rand.Rand, app *chain.App, ctx sdk.Context, n int) []simtypes.Account {
accounts := simtypes.RandomAccounts(r, n)

initAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 100_000_000)
initCoins := sdk.NewCoins(
sdk.NewCoin(sdk.DefaultBondDenom, initAmt),
sdk.NewCoin("bstake", sdk.NewInt(50000000)),
)

// add coins to the accounts
for _, account := range accounts {
acc := app.AccountKeeper.NewAccountWithAddress(ctx, account.Address)
app.AccountKeeper.SetAccount(ctx, acc)
err := simapp.FundAccount(app.BankKeeper, ctx, account.Address, initCoins)
require.NoError(t, err)
}

return accounts
}

func getTestingValidator0(t *testing.T, app *chain.App, ctx sdk.Context, accounts []simtypes.Account) stakingtypes.Validator {
commission0 := stakingtypes.NewCommission(sdk.ZeroDec(), sdk.OneDec(), sdk.OneDec())
return getTestingValidator(t, app, ctx, accounts, commission0, 0)
}

func getTestingValidator1(t *testing.T, app *chain.App, ctx sdk.Context, accounts []simtypes.Account) stakingtypes.Validator {
commission1 := stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
return getTestingValidator(t, app, ctx, accounts, commission1, 1)
}

func getTestingValidator(t *testing.T, app *chain.App, ctx sdk.Context, accounts []simtypes.Account, commission stakingtypes.Commission, n int) stakingtypes.Validator {
account := accounts[n]
valPubKey := account.PubKey
valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
validator := teststaking.NewValidator(t, valAddr, valPubKey)
validator, err := validator.SetInitialCommission(commission)
require.NoError(t, err)

validator.DelegatorShares = sdk.NewDec(100)
validator.Tokens = app.StakingKeeper.TokensFromConsensusPower(ctx, 100)

app.StakingKeeper.SetValidator(ctx, validator)

return validator
}
92 changes: 92 additions & 0 deletions x/liquidstaking/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package simulation_test

import (
"math/rand"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/crescent-network/crescent/v3/app/params"
"github.com/crescent-network/crescent/v3/x/liquidstaking/simulation"
"github.com/crescent-network/crescent/v3/x/liquidstaking/types"
)

func TestProposalContents(t *testing.T) {
app, ctx := createTestApp(false)

s := rand.NewSource(1)
r := rand.New(s)

accounts := getTestingAccounts(t, r, app, ctx, 10)

// setup accounts[0] as validator0 and accounts[1] as validator1
val0 := getTestingValidator0(t, app, ctx, accounts)
val1 := getTestingValidator1(t, app, ctx, accounts)

param := app.LiquidStakingKeeper.GetParams(ctx)
param.WhitelistedValidators = []types.WhitelistedValidator{
{
ValidatorAddress: val0.OperatorAddress,
TargetWeight: sdk.OneInt(),
},
{
ValidatorAddress: val1.OperatorAddress,
TargetWeight: sdk.OneInt(),
},
}
app.LiquidStakingKeeper.SetParams(ctx, param)

// begin a new block
blockTime := time.Now().UTC()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
app.EndBlock(abci.RequestEndBlock{Height: app.LastBlockHeight() + 1})

// execute ProposalContents function
weightedProposalContent := simulation.ProposalContents(app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GovKeeper, app.LiquidStakingKeeper)
require.Len(t, weightedProposalContent, 5)

w0 := weightedProposalContent[0]
w1 := weightedProposalContent[1]
w2 := weightedProposalContent[2]
w3 := weightedProposalContent[3]
w4 := weightedProposalContent[4]

// tests w0 interface:
require.Equal(t, simulation.OpWeightSimulateAddWhitelistValidatorsProposal, w0.AppParamsKey())
require.Equal(t, params.DefaultWeightAddWhitelistValidatorsProposal, w0.DefaultWeight())

// tests w1 interface:
require.Equal(t, simulation.OpWeightSimulateUpdateWhitelistValidatorsProposal, w1.AppParamsKey())
require.Equal(t, params.DefaultWeightUpdateWhitelistValidatorsProposal, w1.DefaultWeight())

// tests w2 interface:
require.Equal(t, simulation.OpWeightSimulateDeleteWhitelistValidatorsProposal, w2.AppParamsKey())
require.Equal(t, params.DefaultWeightDeleteWhitelistValidatorsProposal, w2.DefaultWeight())

// tests w3 interface:
require.Equal(t, simulation.OpWeightCompleteRedelegationUnbonding, w3.AppParamsKey())
require.Equal(t, params.DefaultWeightCompleteRedelegationUnbonding, w3.DefaultWeight())

// tests w4 interface:
require.Equal(t, simulation.OpWeightTallyWithLiquidStaking, w4.AppParamsKey())
require.Equal(t, params.DefaultWeightTallyWithLiquidStaking, w4.DefaultWeight())

content0 := w0.ContentSimulatorFn()(r, ctx, accounts)
require.Nil(t, content0)

content1 := w1.ContentSimulatorFn()(r, ctx, accounts)
require.Nil(t, content1)

content2 := w2.ContentSimulatorFn()(r, ctx, accounts)
require.Nil(t, content2)

content3 := w3.ContentSimulatorFn()(r, ctx, accounts)
require.Nil(t, content3)

content4 := w4.ContentSimulatorFn()(r, ctx, accounts)
require.Nil(t, content4)
}
Loading