Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Dec 27, 2023
1 parent cd484a3 commit 1df87a5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 61 deletions.
24 changes: 13 additions & 11 deletions x/uibc/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package uibc

import (
"encoding/json"
"errors"

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

"github.com/umee-network/umee/v6/util/checkers"
)

Expand All @@ -26,21 +25,24 @@ func (msg *MsgGovUpdateQuota) String() string {

// ValidateBasic implements Msg
func (msg *MsgGovUpdateQuota) ValidateBasic() error {
var errs []error
if err := checkers.Proposal(msg.Authority, msg.Description); err != nil {
return err
errs = append(errs, err)
}

if msg.Total.IsNil() || !msg.Total.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrap("total quota must be positive")
}
if msg.PerDenom.IsNil() || !msg.PerDenom.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrap("quota per denom must be positive")
}
errs = checkers.DecPositive(msg.Total, "total quota", errs)
errs = checkers.DecPositive(msg.PerDenom, "per_denom quota", errs)
errs = checkers.DecNotNegative(msg.InflowOutflowQuotaBase, "inflow_outflow_quota_base", errs)
errs = checkers.DecNotNegative(msg.InflowOutflowTokenQuotaBase, "inflow_outflow_token_quota_base", errs)
errs = checkers.DecNotNegative(msg.InflowOutflowQuotaRate, "inflow_outflow_quota_rate", errs)
if msg.Total.LT(msg.PerDenom) {
return sdkerrors.ErrInvalidRequest.Wrap("total quota must be greater than or equal to per_denom quota")
errs = append(errs, errors.New("total quota must be greater than or equal to per_denom quota"))
}
if msg.InflowOutflowQuotaBase.LT(msg.InflowOutflowTokenQuotaBase) {
errs = append(errs, errors.New("inflow_outflow_quota_base must be greater than or equal than inflow_outflow_token_quota_base"))

Check failure on line 42 in x/uibc/msg.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

line is 129 characters (lll)
}

return nil
return errors.Join(errs...)
}

func (msg *MsgGovUpdateQuota) GetSigners() []sdk.AccAddress {
Expand Down
20 changes: 15 additions & 5 deletions x/uibc/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
func TestMsgGovUpdateQuota(t *testing.T) {
t.Parallel()
validMsg := MsgGovUpdateQuota{
Authority: checkers.GovModuleAddr,
Description: "",
Total: sdk.MustNewDecFromStr("1000"),
PerDenom: sdk.MustNewDecFromStr("1000"),
QuotaDuration: 100,
Authority: checkers.GovModuleAddr,
Description: "",
Total: sdk.MustNewDecFromStr("1000"),
PerDenom: sdk.MustNewDecFromStr("1000"),
InflowOutflowQuotaBase: sdk.MustNewDecFromStr("500"),
InflowOutflowTokenQuotaBase: sdk.MustNewDecFromStr("500"),
InflowOutflowQuotaRate: sdk.MustNewDecFromStr("5"),
QuotaDuration: 100,
}

validEmergencyGroup := validMsg
Expand All @@ -33,6 +36,9 @@ func TestMsgGovUpdateQuota(t *testing.T) {
invalidTotalQuota.PerDenom = sdk.NewDec(10)
invalidTotalQuota.Total = sdk.NewDec(2)

invalidInflowOutflow := validMsg
invalidInflowOutflow.InflowOutflowTokenQuotaBase = sdk.MustNewDecFromStr("501")

tests := []struct {
name string
msg MsgGovUpdateQuota
Expand All @@ -58,6 +64,10 @@ func TestMsgGovUpdateQuota(t *testing.T) {
name: "invalid total quota with respect to per denom",
msg: invalidTotalQuota,
errMsg: "total quota must be greater than or equal to per_denom quota",
}, {
name: "invalid inflow outflow quota abse with respect to per denom",
msg: invalidInflowOutflow,
errMsg: "inflow_outflow_quota_base must be greater than",
},
}

Expand Down
56 changes: 34 additions & 22 deletions x/uibc/quota/intest/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,58 @@ func TestMsgServer_GovUpdateQuota(t *testing.T) {
errMsg string
}{
{
name: "invalid authority",
name: "unauthorized to increase the quota",
msg: uibc.MsgGovUpdateQuota{
Description: "some description",
Authority: accs.Alice.String(),
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(100),
Description: "some description",
Authority: accs.Alice.String(),
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(10000),
InflowOutflowQuotaBase: sdk.NewDec(200),
InflowOutflowTokenQuotaBase: sdk.NewDec(200),
InflowOutflowQuotaRate: sdk.NewDecWithPrec(1, 1),
},
errMsg: "total quota must be greater than or equal to per_denom quota",
errMsg: "unauthorized",
},
{
name: "invalid quota in msg",
msg: uibc.MsgGovUpdateQuota{
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(100),
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(100),
InflowOutflowQuotaBase: sdk.NewDec(200),
InflowOutflowTokenQuotaBase: sdk.NewDec(200),
InflowOutflowQuotaRate: sdk.NewDecWithPrec(1, 1),
},
errMsg: "total quota must be greater than or equal to per_denom quota",
},
{
name: "valid msg",
msg: uibc.MsgGovUpdateQuota{
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(10000),
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 100),
PerDenom: sdk.NewDec(1000),
Total: sdk.NewDec(10000),
InflowOutflowQuotaBase: sdk.NewDec(200),
InflowOutflowTokenQuotaBase: sdk.NewDec(200),
InflowOutflowQuotaRate: sdk.NewDecWithPrec(1, 1),
},
errMsg: "",
},
{
name: "valid update the new params again",
msg: uibc.MsgGovUpdateQuota{
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 1000),
PerDenom: sdk.NewDec(10000),
Total: sdk.NewDec(100000),
Description: "",
Authority: checkers.GovModuleAddr,
QuotaDuration: time.Duration(time.Minute * 1000),
PerDenom: sdk.NewDec(10000),
Total: sdk.NewDec(100000),
InflowOutflowQuotaBase: sdk.NewDec(200),
InflowOutflowTokenQuotaBase: sdk.NewDec(200),
InflowOutflowQuotaRate: sdk.NewDecWithPrec(1, 1),
},
errMsg: "",
},
Expand Down
2 changes: 1 addition & 1 deletion x/uibc/quota/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m msgServer) GovUpdateQuota(ctx context.Context, msg *uibc.MsgGovUpdateQuo
return nil, err
}

if err := k.UpdateQuotaParams(msg.Total, msg.PerDenom, msg.QuotaDuration, byEmergencyGroup); err != nil {
if err := k.UpdateQuotaParams(msg, byEmergencyGroup); err != nil {
return nil, err
}
return &uibc.MsgGovUpdateQuotaResponse{}, nil
Expand Down
17 changes: 7 additions & 10 deletions x/uibc/quota/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package quota

import (
"errors"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v6/x/uibc"
)

Expand All @@ -31,17 +29,16 @@ func (k Keeper) GetParams() (params uibc.Params) {
}

// UpdateQuotaParams update the ibc-transfer quota params for ibc denoms
func (k Keeper) UpdateQuotaParams(totalQuota, quotaPerDenom, inOutBase, inOutTokenBase, inOutRate sdk.Dec,
quotaDuration time.Duration, byEmergencyGroup bool) error {
func (k Keeper) UpdateQuotaParams(msg *uibc.MsgGovUpdateQuota, byEmergencyGroup bool) error {

pOld := k.GetParams()
pNew := pOld
pNew.TotalQuota = totalQuota
pNew.QuotaDuration = quotaDuration
pNew.TokenQuota = quotaPerDenom
pNew.InflowOutflowQuotaBase = inOutBase
pNew.InflowOutflowTokenQuotaBase = inOutTokenBase
pNew.InflowOutflowQuotaRate = inOutRate
pNew.TotalQuota = msg.Total
pNew.QuotaDuration = msg.QuotaDuration
pNew.TokenQuota = msg.PerDenom
pNew.InflowOutflowQuotaBase = msg.InflowOutflowQuotaBase
pNew.InflowOutflowTokenQuotaBase = msg.InflowOutflowTokenQuotaBase
pNew.InflowOutflowQuotaRate = msg.InflowOutflowQuotaRate
if byEmergencyGroup {
if err := validateEmergencyQuotaParamsUpdate(pOld, pNew); err != nil {
return err
Expand Down
30 changes: 18 additions & 12 deletions x/uibc/quota/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,35 @@ func TestUnitParams(t *testing.T) {
}

func TestValidateEmergencyQuotaParamsUpdate(t *testing.T) {
mkParams := func(total, token int64, duration time.Duration) uibc.Params {
mkParams := func(total, token, ioBase, ioTokenBase int64, duration time.Duration) uibc.Params {
return uibc.Params{
TotalQuota: sdk.NewDec(total),
TokenQuota: sdk.NewDec(token),
QuotaDuration: duration,
TotalQuota: sdk.NewDec(total),
TokenQuota: sdk.NewDec(token),
InflowOutflowQuotaBase: sdk.NewDec(ioBase),
InflowOutflowTokenQuotaBase: sdk.NewDec(ioTokenBase),
InflowOutflowQuotaRate: sdk.NewDecWithPrec(1, 1),
QuotaDuration: duration,
}
}

p := mkParams(100, 10, 50)
p := mkParams(100, 10, 30, 40, 50)
tcs := []struct {
name string
p uibc.Params
errMsg string
}{
{"no change", p, ""},
{"valid total quota update", mkParams(99, 10, 50), ""},
{"valid update", mkParams(0, 0, 50), ""},
{"valid total quota update", mkParams(99, 10, 29, 1, 50), ""},
{"valid update", mkParams(0, 0, 0, 0, 50), ""},
{"valid update", mkParams(10, 10, 10, 10, 49), "can't change QuotaDuration"},

{"invalid update", mkParams(201, 11, 50), "can't increase"},
{"invalid total quota update", mkParams(101, 10, 50), "can't increase"},
{"invalid token quota update", mkParams(10, 12, 50), "can't increase"},
{"invalid quota duration update1", mkParams(100, 10, 51), "can't change QuotaDuration"},
{"invalid quota duration update2", mkParams(100, 10, 49), "can't change QuotaDuration"},
{"invalid update", mkParams(201, 9, 30, 40, 50), "can't increase"},
{"invalid total quota update", mkParams(100, 11, 30, 40, 50), "can't increase"},
{"invalid token quota update", mkParams(10, 12, 9, 9, 50), "can't increase"},
{"invalid token quota update", mkParams(10, 10, 31, 10, 50), "can't increase"},
{"invalid token quota update", mkParams(10, 10, 10, 41, 50), "can't increase"},
{"invalid quota duration update1", mkParams(10, 10, 10, 10, 51), "can't change QuotaDuration"},
{"invalid quota duration update2", mkParams(10, 10, 10, 10, 49), "can't change QuotaDuration"},
}

assert := assert.New(t)
Expand Down

0 comments on commit 1df87a5

Please sign in to comment.