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

fix: refine sentinel errors #167

Merged
merged 5 commits into from
Oct 15, 2021
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
7 changes: 4 additions & 3 deletions x/farming/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
}
}

// CurrentEpochDays is intialized with the value of NextEpochDays in genesis and
// CurrentEpochDays is initialized with the value of NextEpochDays in genesis, and
// it is used here to prevent from affecting the epoch days for farming rewards allocation.
// Suppose NextEpochDays is 7 days and it is proposed to change the value to 1 day through governance proposal.
// Although the proposal is passed, farming rewards allocation should continue to proceed with 7 days and then it gets updated.
// Suppose NextEpochDays is 7 days, and it is proposed to change the value to 1 day through governance proposal.
// Although the proposal is passed, farming rewards allocation should continue to proceed with 7 days,
// and then it gets updated.
currentEpochDays := k.GetCurrentEpochDays(ctx)

lastEpochTime, found := k.GetLastEpochTime(ctx)
Expand Down
12 changes: 6 additions & 6 deletions x/farming/keeper/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *KeeperTestSuite) TestValidateAddPublicPlanProposal() {
sdk.NewCoins(sdk.NewInt64Coin(denom3, 100_000_000)),
sdk.ZeroDec(),
)},
sdkerrors.Wrapf(types.ErrInvalidPlanNameLength, "plan name cannot be longer than max length of %d", types.MaxNameLength),
sdkerrors.Wrapf(types.ErrInvalidPlanName, "plan name cannot be longer than max length of %d", types.MaxNameLength),
},
{
"staking coin weights case #1",
Expand All @@ -72,7 +72,7 @@ func (suite *KeeperTestSuite) TestValidateAddPublicPlanProposal() {
sdk.NewCoins(sdk.NewInt64Coin(denom3, 100_000_000)),
sdk.ZeroDec(),
)},
sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "staking coin weights must not be empty"),
sdkerrors.Wrap(types.ErrInvalidStakingCoinWeights, "staking coin weights must not be empty"),
},
{
"staking coin weights case #2",
Expand All @@ -91,7 +91,7 @@ func (suite *KeeperTestSuite) TestValidateAddPublicPlanProposal() {
sdk.NewCoins(sdk.NewInt64Coin(denom3, 100_000_000)),
sdk.ZeroDec(),
)},
sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "total weight must be 1"),
sdkerrors.Wrap(types.ErrInvalidStakingCoinWeights, "total weight must be 1"),
},
{
"start time & end time case #1",
Expand Down Expand Up @@ -256,7 +256,7 @@ func (suite *KeeperTestSuite) TestValidateUpdatePublicPlanProposal() {
nil,
plan.(*types.RatioPlan).EpochRatio,
)},
sdkerrors.Wrapf(types.ErrInvalidPlanNameLength, "plan name cannot be longer than max length of %d", types.MaxNameLength),
sdkerrors.Wrapf(types.ErrInvalidPlanName, "plan name cannot be longer than max length of %d", types.MaxNameLength),
},
{
"staking coin weights case #1",
Expand All @@ -271,7 +271,7 @@ func (suite *KeeperTestSuite) TestValidateUpdatePublicPlanProposal() {
nil,
plan.(*types.RatioPlan).EpochRatio,
)},
sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "staking coin weights must not be empty"),
sdkerrors.Wrap(types.ErrInvalidStakingCoinWeights, "staking coin weights must not be empty"),
},
{
"staking coin weights case #2",
Expand All @@ -291,7 +291,7 @@ func (suite *KeeperTestSuite) TestValidateUpdatePublicPlanProposal() {
nil,
plan.(*types.RatioPlan).EpochRatio,
)},
sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "total weight must be 1"),
sdkerrors.Wrap(types.ErrInvalidStakingCoinWeights, "total weight must be 1"),
},
{
"start time & end time case #1",
Expand Down
18 changes: 8 additions & 10 deletions x/farming/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (

// farming module sentinel errors
var (
ErrPlanNotExists = sdkerrors.Register(ModuleName, 2, "plan does not exist")
ErrInvalidPlanType = sdkerrors.Register(ModuleName, 3, "invalid plan type")
ErrInvalidPlanType = sdkerrors.Register(ModuleName, 2, "invalid plan type")
ErrInvalidPlanName = sdkerrors.Register(ModuleName, 3, "invalid plan name")
ErrInvalidPlanEndTime = sdkerrors.Register(ModuleName, 4, "invalid plan end time")
ErrStakingNotExists = sdkerrors.Register(ModuleName, 5, "staking not exists")
ErrRewardNotExists = sdkerrors.Register(ModuleName, 6, "reward not exists")
ErrFeeCollectionFailure = sdkerrors.Register(ModuleName, 7, "fee collection failure")
ErrInvalidPlanNameLength = sdkerrors.Register(ModuleName, 8, "invalid plan name length")
ErrInvalidPlanName = sdkerrors.Register(ModuleName, 9, "invalid plan name")
ErrConflictPrivatePlanFarmingPool = sdkerrors.Register(ModuleName, 10, "the address is already in use, please use a different plan name")
ErrInvalidStakingReservedAmount = sdkerrors.Register(ModuleName, 11, "staking reserved amount invariant broken")
ErrInvalidRemainingRewardsAmount = sdkerrors.Register(ModuleName, 12, "remaining rewards amount invariant broken")
ErrInvalidStakingCoinWeights = sdkerrors.Register(ModuleName, 5, "invalid staking coin weights")
ErrInvalidTotalEpochRatio = sdkerrors.Register(ModuleName, 6, "invalid total epoch ratio")
ErrStakingNotExists = sdkerrors.Register(ModuleName, 7, "staking not exists")
ErrConflictPrivatePlanFarmingPool = sdkerrors.Register(ModuleName, 8, "the address is already in use, please use a different plan name")
ErrInvalidStakingReservedAmount = sdkerrors.Register(ModuleName, 9, "staking reserved amount invariant broken")
ErrInvalidRemainingRewardsAmount = sdkerrors.Register(ModuleName, 10, "remaining rewards amount invariant broken")
)
2 changes: 1 addition & 1 deletion x/farming/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestValidateGenesis(t *testing.T) {
},
}
},
"total epoch ratio must be lower than 1: invalid request",
"total epoch ratio must be lower than 1: invalid total epoch ratio",
},
{
"invalid staking records - invalid staking coin denom",
Expand Down
2 changes: 1 addition & 1 deletion x/farming/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (msg MsgCreateRatioPlan) ValidateBasic() error {
if err := ValidateEpochRatio(msg.EpochRatio); err != nil {
return err
}
if msg.EpochRatio.GT(sdk.NewDec(1)) {
if msg.EpochRatio.GT(sdk.OneDec()) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid epoch ratio")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions x/farming/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestMsgCreateFixedAmountPlan(t *testing.T) {
),
},
{
"staking coin weights must not be empty: invalid request",
"staking coin weights must not be empty: invalid staking coin weights",
types.NewMsgCreateFixedAmountPlan(
name, creatorAddr, sdk.NewDecCoins(),
startTime, endTime, sdk.Coins{sdk.NewCoin("uatom", sdk.NewInt(1))},
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestMsgCreateRatioPlan(t *testing.T) {
),
},
{
"staking coin weights must not be empty: invalid request",
"staking coin weights must not be empty: invalid staking coin weights",
types.NewMsgCreateRatioPlan(
name, creatorAddr, sdk.NewDecCoins(),
startTime, endTime, sdk.NewDec(1),
Expand Down
19 changes: 7 additions & 12 deletions x/farming/types/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (plan BasePlan) Validate() error {
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot contain %s", PoolAddrSplitter)
}
if len(plan.Name) > MaxNameLength {
return sdkerrors.Wrapf(ErrInvalidPlanNameLength, "plan name cannot be longer than max length of %d", MaxNameLength)
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot be longer than max length of %d", MaxNameLength)
}
if err := ValidateStakingCoinTotalWeights(plan.StakingCoinWeights); err != nil {
return err
Expand Down Expand Up @@ -264,12 +264,7 @@ type PlanI interface {
}

// ValidateTotalEpochRatio validates a farmer's total epoch ratio that must be equal to 1.
func ValidateTotalEpochRatio(i interface{}) error {
plans, ok := i.([]PlanI)
if !ok {
return sdkerrors.Wrapf(ErrInvalidPlanType, "invalid plan type %T", i)
}

func ValidateTotalEpochRatio(plans []PlanI) error {
totalEpochRatio := make(map[string]sdk.Dec)

for _, plan := range plans {
Expand All @@ -289,8 +284,8 @@ func ValidateTotalEpochRatio(i interface{}) error {
}

for _, farmerRatio := range totalEpochRatio {
if farmerRatio.GT(sdk.NewDec(1)) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "total epoch ratio must be lower than 1")
if farmerRatio.GT(sdk.OneDec()) {
return sdkerrors.Wrap(ErrInvalidTotalEpochRatio, "total epoch ratio must be lower than 1")
}
}

Expand Down Expand Up @@ -366,17 +361,17 @@ func UnpackPlans(plansAny []*codectypes.Any) ([]PlanI, error) {
// ValidateStakingCoinTotalWeights validates the total staking coin weights must be equal to 1.
func ValidateStakingCoinTotalWeights(weights sdk.DecCoins) error {
if weights.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "staking coin weights must not be empty")
return sdkerrors.Wrap(ErrInvalidStakingCoinWeights, "staking coin weights must not be empty")
}
if err := weights.Validate(); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid staking coin weights: %v", err)
return sdkerrors.Wrapf(ErrInvalidStakingCoinWeights, "invalid staking coin weights: %v", err)
}
totalWeight := sdk.ZeroDec()
for _, w := range weights {
totalWeight = totalWeight.Add(w.Amount)
}
if !totalWeight.Equal(sdk.OneDec()) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "total weight must be 1")
return sdkerrors.Wrap(ErrInvalidStakingCoinWeights, "total weight must be 1")
}
return nil
}
Expand Down
24 changes: 12 additions & 12 deletions x/farming/types/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ func TestBasePlanValidate(t *testing.T) {
func(plan *types.BasePlan) {
plan.Name = strings.Repeat("a", 256)
},
"plan name cannot be longer than max length of 140: invalid plan name length",
"plan name cannot be longer than max length of 140: invalid plan name",
},
{
"invalid staking coin weights - empty weights",
func(plan *types.BasePlan) {
plan.StakingCoinWeights = sdk.DecCoins{}
},
"staking coin weights must not be empty: invalid request",
"staking coin weights must not be empty: invalid staking coin weights",
},
{
"invalid staking coin weights - invalid denom",
Expand All @@ -265,7 +265,7 @@ func TestBasePlanValidate(t *testing.T) {
sdk.DecCoin{Denom: "!", Amount: sdk.NewDec(1)},
}
},
"invalid staking coin weights: invalid denom: !: invalid request",
"invalid staking coin weights: invalid denom: !: invalid staking coin weights",
},
{
"invalid staking coin weights - invalid amount",
Expand All @@ -274,7 +274,7 @@ func TestBasePlanValidate(t *testing.T) {
sdk.DecCoin{Denom: "stake1", Amount: sdk.NewDec(-1)},
}
},
"invalid staking coin weights: coin -1.000000000000000000stake1 amount is not positive: invalid request",
"invalid staking coin weights: coin -1.000000000000000000stake1 amount is not positive: invalid staking coin weights",
},
{
"invalid staking coin weights - invalid sum of weights #1",
Expand All @@ -283,7 +283,7 @@ func TestBasePlanValidate(t *testing.T) {
sdk.NewDecCoinFromDec("stake1", sdk.NewDecWithPrec(7, 1)),
)
},
"total weight must be 1: invalid request",
"total weight must be 1: invalid staking coin weights",
},
{
"invalid staking coin weights - invalid sum of weights #2",
Expand All @@ -293,7 +293,7 @@ func TestBasePlanValidate(t *testing.T) {
sdk.NewDecCoinFromDec("stake2", sdk.NewDecWithPrec(4, 1)),
)
},
"total weight must be 1: invalid request",
"total weight must be 1: invalid staking coin weights",
},
{
"invalid start/end time",
Expand Down Expand Up @@ -380,12 +380,12 @@ func TestValidateStakingCoinTotalWeights(t *testing.T) {
{
"nil case",
nil,
"staking coin weights must not be empty: invalid request",
"staking coin weights must not be empty: invalid staking coin weights",
},
{
"empty case",
sdk.DecCoins{},
"staking coin weights must not be empty: invalid request",
"staking coin weights must not be empty: invalid staking coin weights",
},
{
"valid case 1",
Expand All @@ -406,22 +406,22 @@ func TestValidateStakingCoinTotalWeights(t *testing.T) {
sdk.NewDecCoinFromDec("stake1", sdk.NewDecWithPrec(3, 1)),
sdk.NewDecCoinFromDec("stake2", sdk.NewDecWithPrec(6, 1)),
),
"total weight must be 1: invalid request",
"total weight must be 1: invalid staking coin weights",
},
{
"invalid case 2",
sdk.NewDecCoins(
sdk.NewDecCoinFromDec("stake1", sdk.NewDecWithPrec(5, 1)),
sdk.NewDecCoinFromDec("stake2", sdk.NewDecWithPrec(6, 1)),
),
"total weight must be 1: invalid request",
"total weight must be 1: invalid staking coin weights",
},
{
"invalid case 3",
sdk.DecCoins{
sdk.DecCoin{Denom: "stake1", Amount: sdk.NewDec(-1)},
},
"invalid staking coin weights: coin -1.000000000000000000stake1 amount is not positive: invalid request",
"invalid staking coin weights: coin -1.000000000000000000stake1 amount is not positive: invalid staking coin weights",
},
} {
err := types.ValidateStakingCoinTotalWeights(tc.stakingCoinWeights)
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestTotalEpochRatio(t *testing.T) {
sdk.NewDec(1),
),
},
sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "total epoch ratio must be lower than 1"),
sdkerrors.Wrap(types.ErrInvalidTotalEpochRatio, "total epoch ratio must be lower than 1"),
},
}

Expand Down
13 changes: 10 additions & 3 deletions x/farming/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -121,10 +122,13 @@ func (p *AddRequestProposal) IsForRatioPlan() bool {
// Validate validates AddRequestProposal.
func (p *AddRequestProposal) Validate() error {
if p.Name == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "plan name must not be empty")
return sdkerrors.Wrap(ErrInvalidPlanName, "plan name must not be empty")
}
if strings.Contains(p.Name, PoolAddrSplitter) {
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot contain %s", PoolAddrSplitter)
}
if len(p.Name) > MaxNameLength {
return sdkerrors.Wrapf(ErrInvalidPlanNameLength, "plan name cannot be longer than max length of %d", MaxNameLength)
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot be longer than max length of %d", MaxNameLength)
}
if _, err := sdk.AccAddressFromBech32(p.FarmingPoolAddress); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid farming pool address %q: %v", p.FarmingPoolAddress, err)
Expand Down Expand Up @@ -200,8 +204,11 @@ func (p *UpdateRequestProposal) Validate() error {
if p.PlanId == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid plan id: %d", p.PlanId)
}
if strings.Contains(p.Name, PoolAddrSplitter) {
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot contain %s", PoolAddrSplitter)
}
if len(p.Name) > MaxNameLength {
return sdkerrors.Wrapf(ErrInvalidPlanNameLength, "plan name cannot be longer than max length of %d", MaxNameLength)
return sdkerrors.Wrapf(ErrInvalidPlanName, "plan name cannot be longer than max length of %d", MaxNameLength)
}
if p.FarmingPoolAddress != "" {
if _, err := sdk.AccAddressFromBech32(p.FarmingPoolAddress); err != nil {
Expand Down
Loading