diff --git a/x/uibc/msg.go b/x/uibc/msg.go index e452de978f..3b385d09cc 100644 --- a/x/uibc/msg.go +++ b/x/uibc/msg.go @@ -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" ) @@ -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")) } - return nil + return errors.Join(errs...) } func (msg *MsgGovUpdateQuota) GetSigners() []sdk.AccAddress { diff --git a/x/uibc/msg_test.go b/x/uibc/msg_test.go index 0a332b21a1..d2c687594f 100644 --- a/x/uibc/msg_test.go +++ b/x/uibc/msg_test.go @@ -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 @@ -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 @@ -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", }, } diff --git a/x/uibc/quota/intest/msg_server_test.go b/x/uibc/quota/intest/msg_server_test.go index a652a4f598..d8d1d6235c 100644 --- a/x/uibc/quota/intest/msg_server_test.go +++ b/x/uibc/quota/intest/msg_server_test.go @@ -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: "", }, diff --git a/x/uibc/quota/msg_server.go b/x/uibc/quota/msg_server.go index ec1dc5006a..4fddd35a18 100644 --- a/x/uibc/quota/msg_server.go +++ b/x/uibc/quota/msg_server.go @@ -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 diff --git a/x/uibc/quota/params.go b/x/uibc/quota/params.go index 52e65b12b1..af57db6959 100644 --- a/x/uibc/quota/params.go +++ b/x/uibc/quota/params.go @@ -2,9 +2,7 @@ package quota import ( "errors" - "time" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/umee-network/umee/v6/x/uibc" ) @@ -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 diff --git a/x/uibc/quota/params_test.go b/x/uibc/quota/params_test.go index 3e53a93c82..a1f9697096 100644 --- a/x/uibc/quota/params_test.go +++ b/x/uibc/quota/params_test.go @@ -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)