OSDN Git Service

fix add order bug
[bytom/vapor.git] / application / mov / match / order_book.go
index e779289..9163823 100644 (file)
@@ -6,7 +6,6 @@ import (
 
        "github.com/bytom/vapor/application/mov/common"
        "github.com/bytom/vapor/application/mov/database"
-       "github.com/bytom/vapor/errors"
 )
 
 // OrderBook is used to handle the mov orders in memory like stack
@@ -62,11 +61,12 @@ func NewOrderBook(movStore database.MovStore, arrivalAddOrders, arrivalDelOrders
 func (o *OrderBook) AddOrder(order *common.Order) error {
        tradePairKey := order.TradePair().Key()
        orders := o.getArrivalAddOrders(tradePairKey)
-       if len(orders) > 0 && order.Cmp(orders[len(orders)-1]) > 0 {
-               return errors.New("rate of order must less than the min order in order table")
-       }
-
+       // use binary search to find the insert position
+       i := sort.Search(len(orders), func(i int) bool { return order.Cmp(orders[i]) > 0 })
        orders = append(orders, order)
+       copy(orders[i+1:], orders[i:])
+       orders[i] = order
+
        o.arrivalAddOrders.Store(tradePairKey, orders)
        return nil
 }