Skip to content

Commit

Permalink
Merge branch 'main' into 142-prune-state
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed Nov 12, 2021
2 parents a6bb4c2 + fd562de commit 7900e1d
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 299 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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: 1 addition & 0 deletions app/params/amino.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build test_amino
// +build test_amino

package params
Expand Down
1 change: 1 addition & 0 deletions app/params/proto.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !test_amino
// +build !test_amino

package params
Expand Down
7 changes: 5 additions & 2 deletions cmd/farmingd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ func NewRootCmd() (*cobra.Command, farmingparams.EncodingConfig) {
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)
initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags())
if err != nil {
return err
}

initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
initClientCtx, err = config.ReadFromClientConfig(initClientCtx)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module github.com/tendermint/farming
go 1.16

require (
github.com/cosmos/cosmos-sdk v0.44.2
github.com/cosmos/cosmos-sdk v0.44.3
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/gorilla/mux v1.8.0
github.com/gravity-devs/liquidity v1.4.1
github.com/gravity-devs/liquidity v1.4.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.1
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tendermint/budget v0.1.1
github.com/tendermint/tendermint v0.34.13
github.com/tendermint/budget v1.0.0-rc1
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.4
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0
Expand Down
294 changes: 16 additions & 278 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions x/farming/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build norace
// +build norace

package testutil
Expand Down
1 change: 0 additions & 1 deletion x/farming/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}


for i, record := range genState.PlanRecords {
plan, err := types.UnpackPlan(&record.Plan)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions x/farming/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (k Keeper) ModifyPublicPlanProposal(ctx sdk.Context, proposals []types.Modi
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "plan %d is not found", p.GetPlanId())
}

if plan.GetType() != types.PlanTypePublic {
return sdkerrors.Wrapf(types.ErrInvalidPlanType, "plan %d is not a public plan", p.GetPlanId())
}

if p.GetName() != "" {
if err := plan.SetName(p.GetName()); err != nil {
return err
Expand Down Expand Up @@ -169,6 +173,10 @@ func (k Keeper) DeletePublicPlanProposal(ctx sdk.Context, proposals []types.Dele
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "plan %d is not found", p.GetPlanId())
}

if plan.GetType() != types.PlanTypePublic {
return sdkerrors.Wrapf(types.ErrInvalidPlanType, "plan %d is not a public plan", p.GetPlanId())
}

if err := k.TerminatePlan(ctx, plan); err != nil {
panic(err)
}
Expand Down
25 changes: 25 additions & 0 deletions x/farming/keeper/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ func (suite *KeeperTestSuite) TestModifyPlanRequest() {
ratioPlan, ok := plan.(*types.RatioPlan)
suite.Require().True(ok)
suite.Require().True(decEq(sdk.MustNewDecFromStr("0.05"), ratioPlan.EpochRatio))

// Test for private plan cannot be modified.
err = plan.SetType(types.PlanTypePrivate)
suite.Require().NoError(err)
suite.Require().Equal(plan.GetType(), types.PlanTypePrivate)
suite.keeper.SetPlan(suite.ctx, plan)

req = testModifyPlanRequest(1, "", "", "", "", "", "", "", "0.1")
proposal = types.NewPublicPlanProposal("title", "description", nil, []types.ModifyPlanRequest{req}, nil)
err = suite.govHandler(suite.ctx, proposal)
suite.Require().ErrorIs(err, types.ErrInvalidPlanType, "plan 2 is not a public plan: invalid plan type")
}

func (suite *KeeperTestSuite) TestDeletePlanRequest() {
Expand All @@ -172,6 +183,20 @@ func (suite *KeeperTestSuite) TestDeletePlanRequest() {

plans := suite.keeper.GetPlans(suite.ctx)
suite.Require().Empty(plans)

// Test for private plan cannot be deleted.
suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000})
plans = suite.keeper.GetPlans(suite.ctx)
suite.Require().Equal(plans[0].GetId(), uint64(2))

err = plans[0].SetType(types.PlanTypePrivate)
suite.Require().NoError(err)
suite.Require().Equal(plans[0].GetType(), types.PlanTypePrivate)
suite.keeper.SetPlan(suite.ctx, plans[0])

proposal = types.NewPublicPlanProposal("title", "description", nil, nil, []types.DeletePlanRequest{{PlanId: 2}})
err = suite.govHandler(suite.ctx, proposal)
suite.Require().ErrorIs(err, types.ErrInvalidPlanType, "plan 2 is not a public plan: invalid plan type")
}

func (suite *KeeperTestSuite) TestWithdrawRewardsAfterPlanDeleted() {
Expand Down
99 changes: 88 additions & 11 deletions x/farming/keeper/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
)

func (suite *KeeperTestSuite) TestAllocationInfos() {
normalPlans := []types.PlanI{
fixedAmountPlans := []types.PlanI{
types.NewFixedAmountPlan(
types.NewBasePlan(
1,
"",
types.PlanTypePrivate,
types.PlanTypePublic,
suite.addrs[0].String(),
suite.addrs[0].String(),
sdk.NewDecCoins(sdk.NewDecCoinFromDec(denom1, sdk.NewDec(1))),
Expand All @@ -29,7 +29,7 @@ func (suite *KeeperTestSuite) TestAllocationInfos() {
types.NewBasePlan(
2,
"",
types.PlanTypePrivate,
types.PlanTypePublic,
suite.addrs[0].String(),
suite.addrs[0].String(),
sdk.NewDecCoins(sdk.NewDecCoinFromDec(denom1, sdk.NewDec(1))),
Expand All @@ -39,6 +39,46 @@ func (suite *KeeperTestSuite) TestAllocationInfos() {
sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))),
}

ratioPlans := []types.PlanI{
types.NewRatioPlan(
types.NewBasePlan(
3,
"",
types.PlanTypePublic,
suite.addrs[0].String(),
suite.addrs[0].String(),
sdk.NewDecCoins(sdk.NewDecCoinFromDec(denom1, sdk.NewDec(1))),
types.ParseTime("2021-07-27T00:00:00Z"),
types.ParseTime("2021-07-28T00:00:00Z"),
),
sdk.MustNewDecFromStr("0.5")),
types.NewRatioPlan(
types.NewBasePlan(
4,
"",
types.PlanTypePublic,
suite.addrs[0].String(),
suite.addrs[0].String(),
sdk.NewDecCoins(sdk.NewDecCoinFromDec(denom1, sdk.NewDec(1))),
types.ParseTime("2021-07-27T12:00:00Z"),
types.ParseTime("2021-07-28T12:00:00Z"),
),
sdk.MustNewDecFromStr("0.6")),
}

hugeRatioPlan := types.NewRatioPlan(
types.NewBasePlan(
5,
"",
types.PlanTypePrivate,
suite.addrs[0].String(),
suite.addrs[0].String(),
sdk.NewDecCoins(sdk.NewDecCoinFromDec(denom1, sdk.NewDec(1))),
types.ParseTime("2021-07-27T12:00:00Z"),
types.ParseTime("2021-07-28T12:00:00Z"),
),
sdk.MustNewDecFromStr("0.999999"))

for _, tc := range []struct {
name string
plans []types.PlanI
Expand Down Expand Up @@ -66,58 +106,95 @@ func (suite *KeeperTestSuite) TestAllocationInfos() {
},
{
"start time & end time edgecase #1",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-26T23:59:59Z"),
nil,
},
{
"start time & end time edgecase #2",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-27T00:00:00Z"),
map[uint64]sdk.Coins{1: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #3",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-27T11:59:59Z"),
map[uint64]sdk.Coins{1: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #4",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-27T12:00:00Z"),
map[uint64]sdk.Coins{
1: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000)),
2: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #5",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-27T23:59:59Z"),
map[uint64]sdk.Coins{
1: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000)),
2: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #6",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-28T00:00:00Z"),
map[uint64]sdk.Coins{2: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #7",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-28T11:59:59Z"),
map[uint64]sdk.Coins{2: sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000))},
},
{
"start time & end time edgecase #8",
normalPlans,
fixedAmountPlans,
types.ParseTime("2021-07-28T12:00:00Z"),
nil,
},
{
"test case for ratio plans #1",
ratioPlans,
types.ParseTime("2021-07-27T00:00:00Z"),
map[uint64]sdk.Coins{
3: sdk.NewCoins(sdk.NewInt64Coin(denom1, 500000000), sdk.NewInt64Coin(denom2, 500000000),
sdk.NewInt64Coin(denom3, 500000000), sdk.NewInt64Coin(sdk.DefaultBondDenom, 500000000))},
},
{
"test case for ratio plans #2",
ratioPlans,
types.ParseTime("2021-07-27T12:00:00Z"),
nil,
},
{
"test case for ratio plans #3",
ratioPlans,
types.ParseTime("2021-07-28T11:00:00Z"),
map[uint64]sdk.Coins{
4: sdk.NewCoins(sdk.NewInt64Coin(denom1, 600000000), sdk.NewInt64Coin(denom2, 600000000),
sdk.NewInt64Coin(denom3, 600000000), sdk.NewInt64Coin(sdk.DefaultBondDenom, 600000000))},
},
{
"test case for fixed plans with a ratio plan over balance #1",
append(fixedAmountPlans, hugeRatioPlan),
types.ParseTime("2021-07-27T12:00:00Z"),
nil,
},
{
"test case for fixed plans with a ratio plan over balance #2",
append([]types.PlanI{hugeRatioPlan}, fixedAmountPlans...),
types.ParseTime("2021-07-27T12:00:00Z"),
nil,
},
} {
suite.Run(tc.name, func() {
for _, plan := range suite.keeper.GetPlans(suite.ctx) {
suite.keeper.RemovePlan(suite.ctx, plan)
}
for _, plan := range tc.plans {
suite.keeper.SetPlan(suite.ctx, plan)
}
Expand Down

0 comments on commit 7900e1d

Please sign in to comment.