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: add test codes using gov handler #91

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ If you haven't already, install Golang by following the [official docs](https://
Requirement | Notes
----------- | -----------------
Go version | Go1.16 or higher
Cosmos SDK | v0.44.2 or higher
Cosmos SDK | v0.44.3 or higher

### Installation

Expand Down
1 change: 0 additions & 1 deletion x/budget/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func flagSetAddress() *flag.FlagSet {

fs.String(FlagType, "", "The Address Type, default 0 for ADDRESS_TYPE_32_BYTES or 1 for ADDRESS_TYPE_20_BYTES")
fs.String(FlagModuleName, "", "The module name to be used for address derivation, default is budget when type 0")
//fs.String(FlagName, "", "The name to be used for address derivation")

return fs
}
8 changes: 4 additions & 4 deletions x/budget/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ $ %s query %s budgets --destination-address %s1zaavvzxez0elundtn32qnk9lkm8kmcszz
// GetCmdQueryAddress implements the query an address that can be used as source and destination is derived according to the given type, module name, and name command.
func GetCmdQueryAddress() *cobra.Command {
cmd := &cobra.Command{
Use: "address",
Use: "address [name]",
Args: cobra.ExactArgs(1),
Short: "Query an address that derived can be used as source and destination",
Short: "Query an address that can be used as source or destination address",
Long: strings.TrimSpace(
fmt.Sprintf(`Query an address that can be used as source and destination is derived according to the given name, module name, and type.
fmt.Sprintf(`Query an address that can be used as source or destination address. It is derived with address derivation name, module name, and address type.

Example:
$ %s query %s address testSourceAddr
Expand All @@ -143,7 +143,7 @@ $ %s query %s address GravityDEXFarmingBudget --module-name farming

Default flag:
$ [--type 0] - ADDRESS_TYPE_32_BYTES of ADR 028
$ [--module-name %s] - When B, the default module name is %s
$ [--module-name %s] - When type 0, the default module name is %s
dongsam marked this conversation as resolved.
Show resolved Hide resolved
`,
version.AppName, types.ModuleName,
version.AppName, types.ModuleName,
Expand Down
183 changes: 133 additions & 50 deletions x/budget/keeper/budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"time"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
paramscutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
Expand Down Expand Up @@ -183,6 +184,10 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
suite.ctx = suite.ctx.WithBlockTime(types.MustParseRFC3339("2021-08-01T00:00:00Z"))
suite.ctx = suite.ctx.WithBlockHeight(int64(height))

// cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az
// inflation occurs by 1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake every blocks
budgetSource := types.DeriveAddress(types.AddressType32Bytes, types.ModuleName, "InflationPool")

for _, tc := range []struct {
name string
proposal *proposal.ParameterChangeProposal
Expand All @@ -191,6 +196,8 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
govTime time.Time
nextBlockTime time.Time
expErr error
accAsserts []sdk.AccAddress
balanceAsserts []sdk.Coins
}{
{
"add budget 1",
Expand All @@ -199,10 +206,10 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
}
Expand All @@ -213,6 +220,13 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
types.MustParseRFC3339("2021-08-01T00:00:00Z"),
types.MustParseRFC3339("2021-08-01T00:00:00Z"),
nil,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
{},
{},
{},
},
},
{
"add budget 2",
Expand All @@ -221,18 +235,18 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1czyx0dj2yd26gv3stpxzv23ddy8pld4j6p90a683mdcg8vzy72jqa8tm6p",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
}
Expand All @@ -243,6 +257,13 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
types.MustParseRFC3339("2021-09-03T00:00:00Z"),
types.MustParseRFC3339("2021-09-03T00:00:00Z"),
nil,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
{},
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
{},
},
},
{
"add budget 3 with invalid total rate case 1",
Expand All @@ -251,36 +272,43 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1czyx0dj2yd26gv3stpxzv23ddy8pld4j6p90a683mdcg8vzy72jqa8tm6p",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1e0n8jmeg4u8q3es2tmhz5zlte8a4q8687ndns8pj4q8grdl74a0sw3045s",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
]`,
}),
0,
0,
2, // left last budgets of 2nd tc
1, // left last budgets of 2nd tc
types.MustParseRFC3339("2021-09-29T00:00:00Z"),
types.MustParseRFC3339("2021-09-30T00:00:00Z"),
types.ErrInvalidTotalBudgetRate,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
mustParseCoinsNormalized("500000000denom1,500000000denom2,500000000denom3,500000000stake"),
mustParseCoinsNormalized("1500000000denom1,1500000000denom2,1500000000denom3,1500000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
{},
},
},
{
"add budget 3 with invalid total rate case 2",
Expand All @@ -289,36 +317,43 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1czyx0dj2yd26gv3stpxzv23ddy8pld4j6p90a683mdcg8vzy72jqa8tm6p",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1e0n8jmeg4u8q3es2tmhz5zlte8a4q8687ndns8pj4q8grdl74a0sw3045s",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
]`,
}),
0,
0,
2, // left last budgets of 2nd tc
1, // left last budgets of 2nd tc
types.MustParseRFC3339("2021-10-01T00:00:00Z"),
types.MustParseRFC3339("2021-10-01T00:00:00Z"),
types.ErrInvalidTotalBudgetRate,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
mustParseCoinsNormalized("750000000denom1,750000000denom2,750000000denom3,750000000stake"),
mustParseCoinsNormalized("2250000000denom1,2250000000denom2,2250000000denom3,2250000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
{},
},
},
{
"add budget 3",
Expand All @@ -327,18 +362,18 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1e0n8jmeg4u8q3es2tmhz5zlte8a4q8687ndns8pj4q8grdl74a0sw3045s",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
Expand All @@ -349,6 +384,13 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
types.MustParseRFC3339("2021-10-01T00:00:00Z"),
types.MustParseRFC3339("2021-10-01T00:00:00Z"),
nil,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
{},
mustParseCoinsNormalized("3125000000denom1,3125000000denom2,3125000000denom3,3125000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
mustParseCoinsNormalized("875000000denom1,875000000denom2,875000000denom3,875000000stake"),
},
},
{
"add budget 4 without date range overlap",
Expand All @@ -357,18 +399,18 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"name": "gravity-dex-farming-1",
"rate": "0.500000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1qceyjmnrl6hapntjq3z25vn38nh68u7yxvufs2thptxvqm7huxeqj7zyrq",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-4",
"rate": "1.000000000000000000",
"source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"destination_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"source_address": "cosmos10wy60v3zuks7rkwnqxs3e878zqfhus6m98l77q6rppz40kxwgllsruc0az",
"destination_address": "cosmos1e0n8jmeg4u8q3es2tmhz5zlte8a4q8687ndns8pj4q8grdl74a0sw3045s",
"start_time": "2031-09-30T00:00:01Z",
"end_time": "2031-12-10T00:00:00Z"
}
Expand All @@ -379,6 +421,33 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
types.MustParseRFC3339("2021-09-29T00:00:00Z"),
types.MustParseRFC3339("2021-09-30T00:00:00Z"),
nil,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
mustParseCoinsNormalized("500000000denom1,500000000denom2,500000000denom3,500000000stake"),
mustParseCoinsNormalized("3625000000denom1,3625000000denom2,3625000000denom3,3625000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
mustParseCoinsNormalized("875000000denom1,875000000denom2,875000000denom3,875000000stake"),
},
},
{
"remove all budgets",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[]`,
}),
0,
0,
types.MustParseRFC3339("2021-10-25T00:00:00Z"),
types.MustParseRFC3339("2021-10-26T00:00:00Z"),
nil,
[]sdk.AccAddress{budgetSource, suite.destinationAddrs[0], suite.destinationAddrs[1], suite.destinationAddrs[2]},
[]sdk.Coins{
mustParseCoinsNormalized("1500000000denom1,1500000000denom2,1500000000denom3,1500000000stake"),
mustParseCoinsNormalized("3625000000denom1,3625000000denom2,3625000000denom3,3625000000stake"),
mustParseCoinsNormalized("1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake"),
mustParseCoinsNormalized("875000000denom1,875000000denom2,875000000denom3,875000000stake"),
},
},
} {
suite.Run(tc.name, func() {
Expand All @@ -399,23 +468,37 @@ func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
params := suite.keeper.GetParams(suite.ctx)
suite.Require().Len(params.Budgets, tc.budgetCount)
for _, budget := range params.Budgets {
err := budget.Validate()
suite.Require().NoError(err)
}
// (new block)
height += 1
suite.ctx = suite.ctx.WithBlockHeight(int64(height))
suite.ctx = suite.ctx.WithBlockTime(tc.nextBlockTime)
budgets := types.CollectibleBudgets(params.Budgets, suite.ctx.BlockTime())
suite.Require().Len(budgets, tc.collectibleBudgetCount)
}

// (new block)
height += 1
suite.ctx = suite.ctx.WithBlockHeight(int64(height))
suite.ctx = suite.ctx.WithBlockTime(tc.nextBlockTime)

// BeginBlocker
err := suite.keeper.CollectBudgets(suite.ctx)
params := suite.keeper.GetParams(suite.ctx)
suite.Require().Len(params.Budgets, tc.budgetCount)
for _, budget := range params.Budgets {
err := budget.Validate()
suite.Require().NoError(err)
}

budgets := types.CollectibleBudgets(params.Budgets, suite.ctx.BlockTime())
suite.Require().Len(budgets, tc.collectibleBudgetCount)

// BeginBlocker - inflation or mint on budgetSource
// inflation occurs by 1000000000denom1,1000000000denom2,1000000000denom3,1000000000stake every blocks
err = simapp.FundAccount(suite.app.BankKeeper, suite.ctx, budgetSource, initialBalances)
suite.Require().NoError(err)

// BeginBlocker - Collect budgets
err = suite.keeper.CollectBudgets(suite.ctx)
suite.Require().NoError(err)

// Assert budget collections
for i, acc := range tc.accAsserts {
balances := suite.app.BankKeeper.GetAllBalances(suite.ctx, acc)
suite.Require().Equal(tc.balanceAsserts[i], balances)
}
})
}
}
Expand Down
Loading