• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版75c0efa76341e93d6289480559c0881eefad14b7 (tree)
时间2020-02-13 20:59:04
作者paladz <453256728@qq.c...>
Commiterpaladz

Log Message

fix add order bug

更改概述

差异

--- a/application/mov/match/order_book.go
+++ b/application/mov/match/order_book.go
@@ -6,7 +6,6 @@ import (
66
77 "github.com/bytom/vapor/application/mov/common"
88 "github.com/bytom/vapor/application/mov/database"
9- "github.com/bytom/vapor/errors"
109 )
1110
1211 // OrderBook is used to handle the mov orders in memory like stack
@@ -62,11 +61,12 @@ func NewOrderBook(movStore database.MovStore, arrivalAddOrders, arrivalDelOrders
6261 func (o *OrderBook) AddOrder(order *common.Order) error {
6362 tradePairKey := order.TradePair().Key()
6463 orders := o.getArrivalAddOrders(tradePairKey)
65- if len(orders) > 0 && order.Cmp(orders[len(orders)-1]) > 0 {
66- return errors.New("rate of order must less than the min order in order table")
67- }
68-
64+ // use binary search to find the insert position
65+ i := sort.Search(len(orders), func(i int) bool { return order.Cmp(orders[i]) > 0 })
6966 orders = append(orders, order)
67+ copy(orders[i+1:], orders[i:])
68+ orders[i] = order
69+
7070 o.arrivalAddOrders.Store(tradePairKey, orders)
7171 return nil
7272 }
--- a/application/mov/match/order_book_test.go
+++ b/application/mov/match/order_book_test.go
@@ -300,3 +300,79 @@ func TestPeekArrivalOrder(t *testing.T) {
300300 }
301301 }
302302 }
303+
304+func TestAddOrder(t *testing.T) {
305+ cases := []struct {
306+ initOrders []*common.Order
307+ wantOrders []*common.Order
308+ addOrder *common.Order
309+ }{
310+ {
311+ initOrders: []*common.Order{},
312+ addOrder: &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 53, RatioDenominator: 1},
313+ wantOrders: []*common.Order{
314+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 53, RatioDenominator: 1},
315+ },
316+ },
317+ {
318+ initOrders: []*common.Order{
319+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
320+ },
321+ addOrder: &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
322+ wantOrders: []*common.Order{
323+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
324+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
325+ },
326+ },
327+ {
328+ initOrders: []*common.Order{
329+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
330+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
331+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
332+ },
333+ addOrder: &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 53, RatioDenominator: 1},
334+ wantOrders: []*common.Order{
335+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 53, RatioDenominator: 1},
336+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
337+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
338+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
339+ },
340+ },
341+ {
342+ initOrders: []*common.Order{
343+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
344+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
345+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
346+ },
347+ addOrder: &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 49, RatioDenominator: 1},
348+ wantOrders: []*common.Order{
349+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
350+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
351+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
352+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 49, RatioDenominator: 1},
353+ },
354+ },
355+ {
356+ initOrders: []*common.Order{
357+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
358+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
359+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 49, RatioDenominator: 1},
360+ },
361+ addOrder: &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
362+ wantOrders: []*common.Order{
363+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 52, RatioDenominator: 1},
364+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 51, RatioDenominator: 1},
365+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 50, RatioDenominator: 1},
366+ &common.Order{FromAssetID: &mock.BTC, ToAssetID: &mock.ETH, RatioNumerator: 49, RatioDenominator: 1},
367+ },
368+ },
369+ }
370+
371+ for i, c := range cases {
372+ orderBook := NewOrderBook(mock.NewMovStore(nil, nil), c.initOrders, nil)
373+ orderBook.AddOrder(c.addOrder)
374+ if gotOrders := orderBook.getArrivalAddOrders(btc2eth.Key()); !testutil.DeepEqual(gotOrders, c.wantOrders) {
375+ t.Fatalf("#%d: the gotOrders(%v) is differnt than wantOrders(%v)", i, gotOrders, c.wantOrders)
376+ }
377+ }
378+}
Show on old repository browser