Skip to content

Commit

Permalink
add more log 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Jul 30, 2019
1 parent baaf435 commit 58ddd2f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
38 changes: 19 additions & 19 deletions tomox/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,53 +253,53 @@ func (orderBook *OrderBook) processLimitOrder(order *OrderItem, verbose bool, dr

if side == Bid {
minPrice := orderBook.Asks.MinPrice(dryrun)
log.Debug("Min price in asks tree", "price", minPrice.String(), "dryrun", dryrun)
log.Debug("Min price in asks tree", "price", minPrice.String())
for quantityToTrade.Cmp(zero) > 0 && orderBook.Asks.NotEmpty() && price.Cmp(minPrice) >= 0 {
bestPriceAsks := orderBook.Asks.MinPriceList(dryrun)
log.Debug("Orderlist at min price", "orderlist", bestPriceAsks.Item, "dryrun", dryrun)
log.Debug("Orderlist at min price", "orderlist", bestPriceAsks.Item)
quantityToTrade, newTrades, orderInBook, err = orderBook.processOrderList(Ask, bestPriceAsks, quantityToTrade, order, verbose, dryrun)
if err != nil {
return nil, nil, err
}
trades = append(trades, newTrades...)
log.Debug("New trade found", "newTrades", newTrades, "orderInBook", orderInBook, "quantityToTrade", quantityToTrade, "dryrun", dryrun)
log.Debug("New trade found", "newTrades", newTrades, "orderInBook", orderInBook, "quantityToTrade", quantityToTrade)
minPrice = orderBook.Asks.MinPrice(dryrun)
log.Debug("New min price in asks tree", "price", minPrice.String(), "dryrun", dryrun)
log.Debug("New min price in asks tree", "price", minPrice.String())
}

if quantityToTrade.Cmp(zero) > 0 {
order.OrderID = orderBook.Item.NextOrderID
order.Quantity = quantityToTrade
log.Debug("After matching, order (unmatched part) is now added to bids tree", "order", order, "dryrun", dryrun)
log.Debug("After matching, order (unmatched part) is now added to bids tree", "order", order)
if err := orderBook.Bids.InsertOrder(order, dryrun); err != nil {
log.Error("Failed to insert order to bidTree", "pairName", order.PairName, "orderItem", order, "err", err, "dryrun", dryrun)
log.Error("Failed to insert order to bidTree", "pairName", order.PairName, "orderItem", order, "err", err)
return nil, nil, err
}
orderInBook = order
}

} else {
maxPrice := orderBook.Bids.MaxPrice(dryrun)
log.Debug("Max price in bids tree", "price", maxPrice.String(), "dryrun", dryrun)
log.Debug("Max price in bids tree", "price", maxPrice.String())
for quantityToTrade.Cmp(zero) > 0 && orderBook.Bids.NotEmpty() && price.Cmp(maxPrice) <= 0 {
bestPriceBids := orderBook.Bids.MaxPriceList(dryrun)
log.Debug("Orderlist at max price", "orderlist", bestPriceBids.Item, "dryrun", dryrun)
log.Debug("Orderlist at max price", "orderlist", bestPriceBids.Item)
quantityToTrade, newTrades, orderInBook, err = orderBook.processOrderList(Bid, bestPriceBids, quantityToTrade, order, verbose, dryrun)
if err != nil {
return nil, nil, err
}
trades = append(trades, newTrades...)
log.Debug("New trade found", "newTrades", newTrades, "orderInBook", orderInBook, "quantityToTrade", quantityToTrade, "dryrun", dryrun)
log.Debug("New trade found", "newTrades", newTrades, "orderInBook", orderInBook, "quantityToTrade", quantityToTrade)
maxPrice = orderBook.Bids.MaxPrice(dryrun)
log.Debug("New max price in bids tree", "price", maxPrice.String(), "dryrun", dryrun)
log.Debug("New max price in bids tree", "price", maxPrice.String())
}

if quantityToTrade.Cmp(zero) > 0 {
order.OrderID = orderBook.Item.NextOrderID
order.Quantity = quantityToTrade
log.Debug("After matching, order (unmatched part) is now back to asks tree", "order", order, "dryrun", dryrun)
log.Debug("After matching, order (unmatched part) is now back to asks tree", "order", order)
if err := orderBook.Asks.InsertOrder(order, dryrun); err != nil {
log.Error("Failed to insert order to askTree", "pairName", order.PairName, "orderItem", order, "err", err, "dryrun", dryrun)
log.Error("Failed to insert order to askTree", "pairName", order.PairName, "orderItem", order, "err", err)
return nil, nil, err
}
orderInBook = order
Expand Down Expand Up @@ -344,7 +344,7 @@ func (orderBook *OrderBook) ProcessOrder(order *OrderItem, verbose bool, dryrun

// processOrderList : process the order list
func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList, quantityStillToTrade *big.Int, order *OrderItem, verbose bool, dryrun bool) (*big.Int, []map[string]string, *OrderItem, error) {
log.Debug("Process matching between order and orderlist", "dryrun", dryrun)
log.Debug("Process matching between order and orderlist")
quantityToTrade := CloneBigInt(quantityStillToTrade)
var (
trades []map[string]string
Expand All @@ -358,7 +358,7 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,
if headOrder == nil {
return nil, nil, nil, fmt.Errorf("headOrder is null")
}
log.Debug("Get head order in the orderlist", "headOrder", headOrder.Item, "dryrun", dryrun)
log.Debug("Get head order in the orderlist", "headOrder", headOrder.Item)

tradedPrice := CloneBigInt(headOrder.Item.Price)

Expand All @@ -374,7 +374,7 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,
if err := headOrder.UpdateQuantity(orderList, newBookQuantity, headOrder.Item.UpdatedAt, dryrun); err != nil {
return nil, nil, nil, err
}
log.Debug("Update quantity for head order", "headOrder", headOrder.Item, "dryrun", dryrun)
log.Debug("Update quantity for head order", "headOrder", headOrder.Item)
quantityToTrade = Zero()
orderInBook = headOrder.Item
} else if IsEqual(quantityToTrade, headOrder.Item.Quantity) {
Expand All @@ -383,12 +383,12 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,
if err := orderBook.Bids.RemoveOrderFromOrderList(headOrder, orderList, dryrun); err != nil {
return nil, nil, nil, err
}
log.Debug("Removed headOrder from bids orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side, "dryrun", dryrun)
log.Debug("Removed headOrder from bids orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side)
} else {
if err := orderBook.Asks.RemoveOrderFromOrderList(headOrder, orderList, dryrun); err != nil {
return nil, nil, nil, err
}
log.Debug("Removed headOrder from asks orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side, "dryrun", dryrun)
log.Debug("Removed headOrder from asks orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side)
}
quantityToTrade = Zero()

Expand All @@ -398,12 +398,12 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,
if err := orderBook.Bids.RemoveOrderFromOrderList(headOrder, orderList, dryrun); err != nil {
return nil, nil, nil, err
}
log.Debug("Removed headOrder from bids orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side, "dryrun", dryrun)
log.Debug("Removed headOrder from bids orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side)
} else {
if err := orderBook.Asks.RemoveOrderFromOrderList(headOrder, orderList, dryrun); err != nil {
return nil, nil, nil, err
}
log.Debug("Removed headOrder from asks orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side, "dryrun", dryrun)
log.Debug("Removed headOrder from asks orderlist", "headOrder", headOrder.Item, "orderlist", orderList.Item, "side", side)
}
quantityToTrade = Sub(quantityToTrade, tradedQuantity)
}
Expand Down
6 changes: 3 additions & 3 deletions tomox/orderlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewOrderListWithItem(item *OrderListItem, orderTree *OrderTree) *OrderList
func (orderList *OrderList) GetOrder(key []byte, dryrun bool) *Order {
// re-use method from orderbook, because orderlist has the same slot as orderbook
storedKey := orderList.GetOrderIDFromKey(key)
log.Debug("Get order from key", "storedKey", storedKey)
log.Debug("Get order from key", "storedKey", hex.EncodeToString(storedKey))
return orderList.orderTree.orderBook.GetOrder(storedKey, key, dryrun)
}

Expand Down Expand Up @@ -263,9 +263,9 @@ func (orderList *OrderList) RemoveOrder(order *Order, dryrun bool) error {
}

nextOrder := orderList.GetOrder(order.Item.NextOrder, dryrun)
log.Debug("Orderlist remove order debug", "nextOrder", nextOrder.Item)
log.Debug("Orderlist remove order debug", "nextOrder", nextOrder)
prevOrder := orderList.GetOrder(order.Item.PrevOrder, dryrun)
log.Debug("Orderlist remove order debug", "prevOrder", prevOrder.Item)
log.Debug("Orderlist remove order debug", "prevOrder", prevOrder)

orderList.Item.Volume = Sub(orderList.Item.Volume, order.Item.Quantity)
orderList.Item.Length--
Expand Down
8 changes: 4 additions & 4 deletions tomox/ordertree.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@ func (orderTree *OrderTree) Depth() uint64 {
func (orderTree *OrderTree) RemovePrice(price *big.Int, dryrun bool) error {
if orderTree.Depth() > 0 {
priceKey := orderTree.getKeyFromPrice(price)
log.Debug("Remove price", "price", price.String(), "priceKey", hex.EncodeToString(priceKey), "dryrun", dryrun)
log.Debug("Remove price", "price", price.String(), "priceKey", hex.EncodeToString(priceKey))
orderListKey := GetOrderListCommonKey(priceKey)
log.Debug("Remove price", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey), "dryrun", dryrun)
log.Debug("Remove price", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey))
orderTree.PriceTree.Remove(orderListKey, dryrun)
log.Debug("Removed price from price tree", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey), "dryrun", dryrun)
log.Debug("Removed price from price tree", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey))
// should use batch to optimize the performance
if err := orderTree.Save(dryrun); err != nil {
return err
}
log.Debug("Remove price - saved ordertree", "dryrun", dryrun)
log.Debug("Remove price - saved ordertree")
}
return nil
}
Expand Down
12 changes: 8 additions & 4 deletions tomox/redblacktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (tree *Tree) Remove(key []byte, dryrun bool) {
if err != nil || node == nil {
return
}
log.Debug("Get node", "node", node, "dryrun", dryrun)
log.Debug("Get node", "node", node)

var left, right *Node = nil, nil
if !tree.IsEmptyKey(node.LeftKey()) {
Expand Down Expand Up @@ -367,6 +367,7 @@ func output(tree *Tree, node *Node, prefix string, isTail bool, str *string, dry
}

func (tree *Tree) rotateLeft(node *Node, dryrun bool) {
log.Debug("Rotate left - before", "nodeRoot", hex.EncodeToString(tree.Root(dryrun).Value()))
right := node.Right(tree, dryrun)
tree.replaceNode(node, right, dryrun)
node.RightKey(right.LeftKey())
Expand All @@ -379,9 +380,11 @@ func (tree *Tree) rotateLeft(node *Node, dryrun bool) {
node.ParentKey(right.Key)
tree.Save(node, dryrun)
tree.Save(right, dryrun)
log.Debug("Rotate left - after", "nodeRoot", hex.EncodeToString(tree.Root(dryrun).Value()))
}

func (tree *Tree) rotateRight(node *Node, dryrun bool) {
log.Debug("Rotate right - before", "nodeRoot", hex.EncodeToString(tree.Root(dryrun).Value()))
left := node.Left(tree, dryrun)
tree.replaceNode(node, left, dryrun)
node.LeftKey(left.RightKey())
Expand All @@ -394,10 +397,11 @@ func (tree *Tree) rotateRight(node *Node, dryrun bool) {
node.ParentKey(left.Key)
tree.Save(node, dryrun)
tree.Save(left, dryrun)
log.Debug("Rotate right - after", "nodeRoot", hex.EncodeToString(tree.Root(dryrun).Value()))
}

func (tree *Tree) replaceNode(old *Node, new *Node, dryrun bool) {
log.Debug("Replace node", "old", old, "new", new, "dryrun", dryrun)
log.Debug("Replace node", "old", old, "new", new)

// we do not change any byte of Key so we can copy the reference to save directly to db
var newKey []byte
Expand Down Expand Up @@ -513,7 +517,7 @@ func (tree *Tree) insertCase5(node *Node, dryrun bool) {
}

func (tree *Tree) Save(node *Node, dryrun bool) error {
log.Debug("Save node", "node", node, "dryrun", dryrun)
log.Debug("Save node", "node", node)
return tree.db.Put(node.Key, node.Item, dryrun)
}

Expand Down Expand Up @@ -659,6 +663,6 @@ func nodeColor(node *Node) bool {
}

func (tree *Tree) deleteNode(node *Node, dryrun bool) {
log.Debug("Delete node", "node value", hex.EncodeToString(node.Value()), "dryrun", dryrun)
log.Debug("Delete node", "node value", hex.EncodeToString(node.Value()))
tree.db.Delete(node.Key, dryrun)
}

0 comments on commit 58ddd2f

Please sign in to comment.