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 1 commit
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
Next Next commit
test: increase test coverage of liquidstaking (cherry-picked)
  • Loading branch information
dongsam authored and crypin committed Oct 31, 2022
commit 704bda0ad87558b8e4e0158c31dab27e43bff1c5
83 changes: 83 additions & 0 deletions x/liquidstaking/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/stretchr/testify/require"
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)

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
}
65 changes: 65 additions & 0 deletions x/liquidstaking/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"

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

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

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

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

// 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