OSDN Git Service

fix_add_order_of_order_book (#497)
authorPoseidon <shenao.78@163.com>
Sun, 23 Feb 2020 04:32:18 +0000 (12:32 +0800)
committerGitHub <noreply@github.com>
Sun, 23 Feb 2020 04:32:18 +0000 (12:32 +0800)
* fix_add_order_of_order_book

* fix order book

* opt code

Co-authored-by: Paladz <yzhu101@uottawa.ca>
application/mov/common/type.go

index d038902..11d89bb 100644 (file)
@@ -38,24 +38,32 @@ func (o *Order) Rate() float64 {
        return result
 }
 
-// Cmp compares x and y and returns -1 if x <  y, 0 if x == y, +1 if x >  y
-func (o *Order) Cmp(other *Order) int {
+// cmpRate compares rate of x and y and returns -1 if x <  y, 0 if x == y, +1 if x >  y
+func (o *Order) cmpRate(other *Order) int {
        rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
        otherRate := big.NewRat(other.RatioNumerator, other.RatioDenominator)
        return rate.Cmp(otherRate)
 }
 
+// Cmp first compare the rate, if rate is equals, then compare the utxo hash
+func (o *Order) Cmp(other *Order) int {
+       cmp := o.cmpRate(other)
+       if cmp == 0 {
+               if hex.EncodeToString(o.UTXOHash().Bytes()) < hex.EncodeToString(other.UTXOHash().Bytes()) {
+                       return -1
+               }
+               return 1
+       }
+       return cmp
+}
+
 // OrderSlice is define for order's sort
 type OrderSlice []*Order
 
 func (o OrderSlice) Len() int      { return len(o) }
 func (o OrderSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
 func (o OrderSlice) Less(i, j int) bool {
-       cmp := o[i].Cmp(o[j])
-       if cmp == 0 {
-               return hex.EncodeToString(o[i].UTXOHash().Bytes()) < hex.EncodeToString(o[j].UTXOHash().Bytes())
-       }
-       return cmp < 0
+       return o[i].Cmp(o[j]) < 0
 }
 
 // NewOrderFromOutput convert txinput to order