all: use big.Sign to compare with zero #29490 (#1387)

This commit is contained in:
Daniel Liu 2025-08-28 18:56:39 +08:00 committed by GitHub
parent f561e7664d
commit 0be5580b98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 15 additions and 15 deletions

View file

@ -372,7 +372,7 @@ func (XDCx *XDCX) SyncDataToSDKNode(takerOrderInTx *tradingstate.OrderItem, txHa
tradeRecord := &tradingstate.Trade{} tradeRecord := &tradingstate.Trade{}
quantity := tradingstate.ToBigInt(trade[tradingstate.TradeQuantity]) quantity := tradingstate.ToBigInt(trade[tradingstate.TradeQuantity])
price := tradingstate.ToBigInt(trade[tradingstate.TradePrice]) price := tradingstate.ToBigInt(trade[tradingstate.TradePrice])
if price.Cmp(big.NewInt(0)) <= 0 || quantity.Cmp(big.NewInt(0)) <= 0 { if price.Sign() <= 0 || quantity.Sign() <= 0 {
return fmt.Errorf("trade misses important information. tradedPrice %v, tradedQuantity %v", price, quantity) return fmt.Errorf("trade misses important information. tradedPrice %v, tradedQuantity %v", price, quantity)
} }
tradeRecord.Amount = quantity tradeRecord.Amount = quantity

View file

@ -288,7 +288,7 @@ func (o *OrderItem) encodedSide() *big.Int {
// verifyPrice make sure price is a positive number // verifyPrice make sure price is a positive number
func (o *OrderItem) verifyPrice() error { func (o *OrderItem) verifyPrice() error {
if o.Price == nil || o.Price.Cmp(big.NewInt(0)) <= 0 { if o.Price == nil || o.Price.Sign() <= 0 {
log.Debug("Invalid price", "price", o.Price.String()) log.Debug("Invalid price", "price", o.Price.String())
return ErrInvalidPrice return ErrInvalidPrice
} }
@ -297,7 +297,7 @@ func (o *OrderItem) verifyPrice() error {
// verifyQuantity make sure quantity is a positive number // verifyQuantity make sure quantity is a positive number
func (o *OrderItem) verifyQuantity() error { func (o *OrderItem) verifyQuantity() error {
if o.Quantity == nil || o.Quantity.Cmp(big.NewInt(0)) <= 0 { if o.Quantity == nil || o.Quantity.Sign() <= 0 {
log.Debug("Invalid quantity", "quantity", o.Quantity.String()) log.Debug("Invalid quantity", "quantity", o.Quantity.String())
return ErrInvalidQuantity return ErrInvalidQuantity
} }

View file

@ -52,7 +52,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "makerFee", makerFee, "defaultFee", defaultFee) log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "makerFee", makerFee, "defaultFee", defaultFee)
return result, ErrQuantityTradeTooSmall return result, ErrQuantityTradeTooSmall
} }
if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 { if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Sign() > 0 {
// defaultFeeInXDC // defaultFeeInXDC
defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice) defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice)
defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal) defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal)
@ -108,7 +108,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "takerFee", takerFee) log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "takerFee", takerFee)
return result, ErrQuantityTradeTooSmall return result, ErrQuantityTradeTooSmall
} }
if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 { if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Sign() > 0 {
// defaultFeeInXDC // defaultFeeInXDC
defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice) defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice)
defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal) defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal)

View file

@ -72,7 +72,7 @@ func GetSettleBalance(isXDCXLendingFork bool,
log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "takerFee", takerFee) log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "takerFee", takerFee)
return result, ErrQuantityTradeTooSmall return result, ErrQuantityTradeTooSmall
} }
if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Cmp(common.Big0) > 0 { if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Sign() > 0 {
exTakerReceivedFee := new(big.Int).Mul(takerFee, lendTokenXDCPrice) exTakerReceivedFee := new(big.Int).Mul(takerFee, lendTokenXDCPrice)
exTakerReceivedFee = new(big.Int).Div(exTakerReceivedFee, lendTokenDecimal) exTakerReceivedFee = new(big.Int).Div(exTakerReceivedFee, lendTokenDecimal)
@ -122,7 +122,7 @@ func GetSettleBalance(isXDCXLendingFork bool,
log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "makerFee", makerFee) log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "makerFee", makerFee)
return result, ErrQuantityTradeTooSmall return result, ErrQuantityTradeTooSmall
} }
if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Cmp(common.Big0) > 0 { if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Sign() > 0 {
exMakerReceivedFee := new(big.Int).Mul(makerFee, lendTokenXDCPrice) exMakerReceivedFee := new(big.Int).Mul(makerFee, lendTokenXDCPrice)
exMakerReceivedFee = new(big.Int).Div(exMakerReceivedFee, lendTokenDecimal) exMakerReceivedFee = new(big.Int).Div(exMakerReceivedFee, lendTokenDecimal)
@ -172,7 +172,7 @@ func GetSettleBalance(isXDCXLendingFork bool,
log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "borrowFee", borrowFee) log.Debug("quantity lending too small", "quantityToLend", quantityToLend, "borrowFee", borrowFee)
return result, ErrQuantityTradeTooSmall return result, ErrQuantityTradeTooSmall
} }
if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Cmp(common.Big0) > 0 { if lendingToken != common.XDCNativeAddressBinary && lendTokenXDCPrice != nil && lendTokenXDCPrice.Sign() > 0 {
// exReceivedFee: the fee amount which borrowingRelayer will receive // exReceivedFee: the fee amount which borrowingRelayer will receive
exReceivedFee := new(big.Int).Mul(borrowFee, lendTokenXDCPrice) exReceivedFee := new(big.Int).Mul(borrowFee, lendTokenXDCPrice)
exReceivedFee = new(big.Int).Div(exReceivedFee, lendTokenDecimal) exReceivedFee = new(big.Int).Div(exReceivedFee, lendTokenDecimal)

View file

@ -336,7 +336,7 @@ func TestStatedbUtils(t *testing.T) {
if cap.Cmp(cap_statedb) != 0 { if cap.Cmp(cap_statedb) != 0 {
t.Fatalf("cap not equal, statedb utils is wrong") t.Fatalf("cap not equal, statedb utils is wrong")
} }
if cap.Cmp(big.NewInt(0)) == 0 { if cap.Sign() == 0 {
t.Fatalf("cap should not be zero") t.Fatalf("cap should not be zero")
} }
owner, err := validator.GetCandidateOwner(it) owner, err := validator.GetCandidateOwner(it)
@ -368,7 +368,7 @@ func TestStatedbUtils(t *testing.T) {
if cap.Cmp(cap_statedb) != 0 { if cap.Cmp(cap_statedb) != 0 {
t.Fatalf("cap not equal, statedb utils is wrong") t.Fatalf("cap not equal, statedb utils is wrong")
} }
if cap.Cmp(big.NewInt(0)) == 0 { if cap.Sign() == 0 {
t.Fatalf("cap should not be zero") t.Fatalf("cap should not be zero")
} }
} }

View file

@ -443,11 +443,11 @@ func (pool *OrderPool) validateOrder(tx *types.OrderTransaction) error {
cloneXDCXStateDb := pool.currentOrderState.Copy() cloneXDCXStateDb := pool.currentOrderState.Copy()
if !tx.IsCancelledOrder() { if !tx.IsCancelledOrder() {
if quantity == nil || quantity.Cmp(big.NewInt(0)) <= 0 { if quantity == nil || quantity.Sign() <= 0 {
return ErrInvalidOrderQuantity return ErrInvalidOrderQuantity
} }
if orderType != OrderTypeMarket { if orderType != OrderTypeMarket {
if price == nil || price.Cmp(big.NewInt(0)) <= 0 { if price == nil || price.Sign() <= 0 {
return ErrInvalidOrderPrice return ErrInvalidOrderPrice
} }
} }

View file

@ -755,7 +755,7 @@ func (ipp *InnerProdArg) Serialize() []byte {
spa = serializePointArray(ipp.R, false) spa = serializePointArray(ipp.R, false)
proof = append(proof, spa[:]...) proof = append(proof, spa[:]...)
if ipp.A.Cmp(big.NewInt(0)) < 0 { if ipp.A.Sign() < 0 {
ipp.A.Mod(ipp.A, EC.N) ipp.A.Mod(ipp.A, EC.N)
} }
sp := PadTo32Bytes(ipp.A.Bytes()) sp := PadTo32Bytes(ipp.A.Bytes())
@ -833,7 +833,7 @@ func (mrp *MultiRangeProof) Serialize() []byte {
sp = PadTo32Bytes(mrp.Tau.Bytes()) sp = PadTo32Bytes(mrp.Tau.Bytes())
proof = append(proof, sp[:]...) proof = append(proof, sp[:]...)
if mrp.Th.Cmp(big.NewInt(0)) < 0 { if mrp.Th.Sign() < 0 {
mrp.Th.Mod(mrp.Th, EC.N) mrp.Th.Mod(mrp.Th, EC.N)
} }
sp = PadTo32Bytes(mrp.Th.Bytes()) sp = PadTo32Bytes(mrp.Th.Bytes())
@ -975,7 +975,7 @@ func MRPProve(values []*big.Int) (MultiRangeProof, error) {
for j := range values { for j := range values {
v := values[j] v := values[j]
if v.Cmp(big.NewInt(0)) == -1 { if v.Sign() == -1 {
return MultiRangeProof{}, errors.New("value is below range! Not proving") return MultiRangeProof{}, errors.New("value is below range! Not proving")
} }