From 6a96b6eb96a68fba350fe6bc699f2b3213767c63 Mon Sep 17 00:00:00 2001 From: Albert Chon Date: Mon, 27 Dec 2021 07:13:18 -0500 Subject: [PATCH] re-gen --- chain/exchange/types/common_utils.go | 30 ++++++++++++++++++---------- chain/exchange/types/msgs.go | 23 +++++++++++++++------ chain/exchange/types/proposal.go | 28 ++++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/chain/exchange/types/common_utils.go b/chain/exchange/types/common_utils.go index 3f8f1827..deb1f694 100644 --- a/chain/exchange/types/common_utils.go +++ b/chain/exchange/types/common_utils.go @@ -4,6 +4,8 @@ import ( "bytes" "math/big" "reflect" + "regexp" + "strings" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" @@ -85,29 +87,35 @@ func StringInSlice(a string, list *[]string) bool { } func IsValidSubaccountID(subaccountID string) (*common.Address, bool) { - if len(subaccountID) != 66 { + if !IsHexHash(subaccountID) { return nil, false } subaccountIdBytes := common.FromHex(subaccountID) - - if len(subaccountIdBytes) != common.HashLength { - return nil, false - } addressBytes := subaccountIdBytes[:common.AddressLength] - if !common.IsHexAddress(common.Bytes2Hex(addressBytes)) { - return nil, false - } address := common.BytesToAddress(addressBytes) return &address, true } func IsValidOrderHash(orderHash string) bool { - if len(orderHash) != 66 { + return IsHexHash(orderHash) +} + +// IsHexHash verifies whether a string can represent a valid hex-encoded hash or not. +func IsHexHash(s string) bool { + if !isHexString(s) { return false } - orderHashBytes := common.FromHex(orderHash) - return len(orderHashBytes) == common.HashLength + if strings.HasPrefix(s, "0x") { + return len(s) == 2*common.HashLength+2 + } + + return len(s) == 2*common.HashLength +} + +func isHexString(str string) bool { + isMatched, _ := regexp.MatchString("^(0x)?[0-9a-fA-F]+$", str) + return isMatched } func BreachesMinimumTickSize(value sdk.Dec, minTickSize sdk.Dec) bool { diff --git a/chain/exchange/types/msgs.go b/chain/exchange/types/msgs.go index b2c02aa4..64b9fb22 100644 --- a/chain/exchange/types/msgs.go +++ b/chain/exchange/types/msgs.go @@ -34,7 +34,7 @@ var ( ) func (o *SpotOrder) ValidateBasic(senderAddr sdk.AccAddress) error { - if o.MarketId == "" { + if !IsHexHash(o.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId) } switch o.OrderType { @@ -75,7 +75,7 @@ func (o *OrderInfo) ValidateBasic(senderAddr sdk.AccAddress) error { } func (o *DerivativeOrder) ValidateBasic(senderAddr sdk.AccAddress) error { - if o.MarketId == "" { + if !IsHexHash(o.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId) } switch o.OrderType { @@ -101,7 +101,7 @@ func (o *DerivativeOrder) ValidateBasic(senderAddr sdk.AccAddress) error { } func (o *OrderData) ValidateBasic(senderAddr sdk.AccAddress) error { - if o.MarketId == "" { + if !IsHexHash(o.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId) } @@ -878,9 +878,10 @@ func (msg *MsgIncreasePositionMargin) ValidateBasic() error { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender) } - if msg.MarketId == "" { + if !IsHexHash(msg.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, msg.MarketId) } + if !msg.Amount.IsPositive() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) } @@ -928,7 +929,7 @@ func (msg *MsgLiquidatePosition) ValidateBasic() error { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender) } - if msg.MarketId == "" { + if !IsHexHash(msg.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, msg.MarketId) } @@ -975,7 +976,11 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error { hasSubaccountIdForCancelAll := msg.SubaccountId != "" if hasCancelAllMarketId && !hasSubaccountIdForCancelAll { - return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains cancel all market id but no subaccount id") + return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains cancel all marketIDs but no subaccountID") + } + + if hasSubaccountIdForCancelAll && !hasCancelAllMarketId { + return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains subaccountID but no cancel all marketIDs") } if hasSubaccountIdForCancelAll { @@ -1011,6 +1016,9 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error { if len(msg.SpotMarketIdsToCancelAll) > 0 && len(msg.SpotOrdersToCancel) > 0 { seen := make(map[common.Hash]struct{}) for _, marketID := range msg.SpotMarketIdsToCancelAll { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } seen[common.HexToHash(marketID)] = struct{}{} } @@ -1024,6 +1032,9 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error { if len(msg.DerivativeMarketIdsToCancelAll) > 0 && len(msg.DerivativeOrdersToCancel) > 0 { seen := make(map[common.Hash]struct{}) for _, marketID := range msg.DerivativeMarketIdsToCancelAll { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } seen[common.HexToHash(marketID)] = struct{}{} } diff --git a/chain/exchange/types/proposal.go b/chain/exchange/types/proposal.go index 302bc1a9..ec40a649 100644 --- a/chain/exchange/types/proposal.go +++ b/chain/exchange/types/proposal.go @@ -211,7 +211,7 @@ func (p *SpotMarketParamUpdateProposal) ProposalType() string { // ValidateBasic returns ValidateBasic result of this proposal. func (p *SpotMarketParamUpdateProposal) ValidateBasic() error { - if p.MarketId == "" { + if !IsHexHash(p.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, p.MarketId) } if p.MakerFeeRate == nil && p.TakerFeeRate == nil && p.RelayerFeeShareRate == nil && p.MinPriceTickSize == nil && p.MinQuantityTickSize == nil && p.Status == MarketStatus_Unspecified { @@ -403,7 +403,7 @@ func (p *DerivativeMarketParamUpdateProposal) ProposalType() string { // ValidateBasic returns ValidateBasic result of this proposal. func (p *DerivativeMarketParamUpdateProposal) ValidateBasic() error { - if p.MarketId == "" { + if !IsHexHash(p.MarketId) { return sdkerrors.Wrap(ErrMarketInvalid, p.MarketId) } if p.MakerFeeRate == nil && @@ -916,6 +916,18 @@ func (t *TradingRewardCampaignBoostInfo) ValidateBasic() error { return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "boosted spot market ids is not matching spot market multipliers") } + for _, marketID := range t.BoostedSpotMarketIds { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } + } + + for _, marketID := range t.BoostedDerivativeMarketIds { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } + } + if len(t.BoostedDerivativeMarketIds) != len(t.DerivativeMarketMultipliers) { return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "boosted derivative market ids is not matching derivative market multipliers") } @@ -971,6 +983,12 @@ func (c *TradingRewardCampaignInfo) ValidateBasic() error { } } + for _, marketID := range c.DisqualifiedMarketIds { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } + } + hasDuplicatesInDisqualifiedMarkets := c != nil && HasDuplicates(c.DisqualifiedMarketIds) if hasDuplicatesInDisqualifiedMarkets { return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "campaign contains duplicate disqualified market ids") @@ -1061,6 +1079,12 @@ func (p *FeeDiscountProposal) ValidateBasic() error { return sdkerrors.Wrap(ErrInvalidFeeDiscountSchedule, "new fee discount schedule cannot have duplicate quote denoms") } + for _, marketID := range p.Schedule.DisqualifiedMarketIds { + if !IsHexHash(marketID) { + return sdkerrors.Wrap(ErrMarketInvalid, marketID) + } + } + if HasDuplicates(p.Schedule.DisqualifiedMarketIds) { return sdkerrors.Wrap(ErrInvalidFeeDiscountSchedule, "new fee discount schedule cannot have duplicate disqualified market ids") }