From 31d589fa9b2613a83accb8c551075daf661243d5 Mon Sep 17 00:00:00 2001 From: Tuna Date: Mon, 29 Jul 2019 11:53:55 +0700 Subject: [PATCH 1/3] add more log --- miner/worker.go | 1 + tomox/orderbook.go | 23 +++++++++++++++++++++-- tomox/orderlist.go | 3 ++- tomox/ordertree.go | 13 +++++++++++-- tomox/redblacktree.go | 26 +++++++++++++++++++------- tomox/snapshot.go | 2 +- tomox/tomox.go | 1 + 7 files changed, 56 insertions(+), 13 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index 5797b8b2ac34..98aff70b79b0 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -609,6 +609,7 @@ func (self *worker) commitNewWork() { } } + log.Debug("Start processing order pending") txMatches := tomoX.ProcessOrderPending() if len(txMatches) > 0 { log.Debug("transaction matches found", "txMatches", txMatches) diff --git a/tomox/orderbook.go b/tomox/orderbook.go index bcfd06520348..371775cab764 100755 --- a/tomox/orderbook.go +++ b/tomox/orderbook.go @@ -253,21 +253,26 @@ 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) 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) 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) minPrice = orderBook.Asks.MinPrice(dryrun) + log.Debug("New min price in asks tree", "price", minPrice.String(), "dryrun", dryrun) } 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) if err := orderBook.Bids.InsertOrder(order, dryrun); err != nil { - log.Error("Failed to insert order to bidTree", "pairName", order.PairName, "orderItem", order, "err", err) + log.Error("Failed to insert order to bidTree", "pairName", order.PairName, "orderItem", order, "err", err, "dryrun", dryrun) return nil, nil, err } orderInBook = order @@ -275,21 +280,26 @@ func (orderBook *OrderBook) processLimitOrder(order *OrderItem, verbose bool, dr } else { maxPrice := orderBook.Bids.MaxPrice(dryrun) + log.Debug("Max price in bids tree", "price", maxPrice.String(), "dryrun", dryrun) 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) 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) maxPrice = orderBook.Bids.MaxPrice(dryrun) + log.Debug("New max price in bids tree", "price", maxPrice.String(), "dryrun", dryrun) } 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) if err := orderBook.Asks.InsertOrder(order, dryrun); err != nil { - log.Error("Failed to insert order to askTree", "pairName", order.PairName, "orderItem", order, "err", err) + log.Error("Failed to insert order to askTree", "pairName", order.PairName, "orderItem", order, "err", err, "dryrun", dryrun) return nil, nil, err } orderInBook = order @@ -311,11 +321,13 @@ func (orderBook *OrderBook) ProcessOrder(order *OrderItem, verbose bool, dryrun orderBook.Item.NextOrderID++ if orderType == Market { + log.Debug("Process market order", "order", order) trades, orderInBook, err = orderBook.processMarketOrder(order, verbose, dryrun) if err != nil { return nil, nil, err } } else { + log.Debug("Process limit order", "order", order) trades, orderInBook, err = orderBook.processLimitOrder(order, verbose, dryrun) if err != nil { return nil, nil, err @@ -332,6 +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) quantityToTrade := CloneBigInt(quantityStillToTrade) var ( trades []map[string]string @@ -345,6 +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) tradedPrice := CloneBigInt(headOrder.Item.Price) @@ -360,6 +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) quantityToTrade = Zero() orderInBook = headOrder.Item } else if IsEqual(quantityToTrade, headOrder.Item.Quantity) { @@ -368,10 +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) } 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) } quantityToTrade = Zero() @@ -381,10 +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) } 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) } quantityToTrade = Sub(quantityToTrade, tradedQuantity) } diff --git a/tomox/orderlist.go b/tomox/orderlist.go index 178a2c73edb3..0cb3e988d573 100755 --- a/tomox/orderlist.go +++ b/tomox/orderlist.go @@ -263,7 +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) prevOrder := orderList.GetOrder(order.Item.PrevOrder, dryrun) + log.Debug("Orderlist remove order debug", "prevOrder", prevOrder.Item) orderList.Item.Volume = Sub(orderList.Item.Volume, order.Item.Quantity) orderList.Item.Length-- @@ -298,7 +300,6 @@ func (orderList *OrderList) RemoveOrder(order *Order, dryrun bool) error { orderList.Item.HeadOrder = EmptyKey() orderList.Item.TailOrder = EmptyKey() } - return nil } diff --git a/tomox/ordertree.go b/tomox/ordertree.go index c0cf8276a234..d4308b08a101 100755 --- a/tomox/ordertree.go +++ b/tomox/ordertree.go @@ -184,13 +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) orderListKey := GetOrderListCommonKey(priceKey) + log.Debug("Remove price", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey), "dryrun", dryrun) orderTree.PriceTree.Remove(orderListKey, dryrun) - + log.Debug("Removed price from price tree", "price", price.String(), "orderListKey", hex.EncodeToString(orderListKey), "dryrun", dryrun) // should use batch to optimize the performance if err := orderTree.Save(dryrun); err != nil { return err } + log.Debug("Remove price - saved ordertree", "dryrun", dryrun) } return nil } @@ -313,18 +316,20 @@ func (orderTree *OrderTree) RemoveOrderFromOrderList(order *Order, orderList *Or if err := orderList.RemoveOrder(order, dryrun); err != nil { return err } + log.Debug("Removed order from orderlist", "order", order.Item, "orderlist", orderList.Item) // snapshot order list if err := orderList.Save(dryrun); err != nil { return err } + log.Debug("Saved orderlist", "orderlist", orderList.Item) // no items left than safety remove if orderList.Item.Length == uint64(0) { if err := orderTree.RemovePrice(order.Item.Price, dryrun); err != nil { return err } - log.Debug("remove price list", "price", order.Item.Price.String()) + log.Debug("Removed price list", "price", order.Item.Price.String()) } // update orderTree @@ -398,14 +403,18 @@ func (orderTree *OrderTree) MaxPrice(dryrun bool) *big.Int { // MinPrice : get the min price func (orderTree *OrderTree) MinPrice(dryrun bool) *big.Int { if orderTree.Depth() > 0 { + log.Debug("ordertree isn't empty") if bytes, found := orderTree.PriceTree.GetMin(dryrun); found { item, err := orderTree.getOrderListItem(bytes) if err != nil { + log.Error("Failed to get orderlist with min price", "err", err) return Zero() } if item != nil { return CloneBigInt(item.Price) } + } else { + log.Debug("Min not found") } } return Zero() diff --git a/tomox/redblacktree.go b/tomox/redblacktree.go index 3b2b1a9fc065..3bcdcacfdf5e 100644 --- a/tomox/redblacktree.go +++ b/tomox/redblacktree.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/log" + "encoding/hex" ) // Tree holds elements of the red-black tree @@ -39,7 +40,11 @@ func NewWithBytesComparator(db OrderDao) *Tree { } func (tree *Tree) Root(dryrun bool) *Node { - root, _ := tree.GetNode(tree.rootKey, dryrun) + root, err := tree.GetNode(tree.rootKey, dryrun) + if err != nil { + log.Error("Can't get tree.Root", "rootKey", hex.EncodeToString(tree.rootKey), "err", err) + return nil + } return root } @@ -150,10 +155,10 @@ func (tree *Tree) Get(key []byte, dryrun bool) (value []byte, found bool) { func (tree *Tree) Remove(key []byte, dryrun bool) { var child *Node node, err := tree.GetNode(key, dryrun) - if err != nil || node == nil { return } + log.Debug("Get node", "node", node, "dryrun", dryrun) var left, right *Node = nil, nil if !tree.IsEmptyKey(node.LeftKey()) { @@ -389,6 +394,7 @@ func (tree *Tree) rotateRight(node *Node, dryrun bool) { } func (tree *Tree) replaceNode(old *Node, new *Node, dryrun bool) { + log.Debug("Replace node", "old", old, "new", new, "dryrun", dryrun) // we do not change any byte of Key so we can copy the reference to save directly to db var newKey []byte @@ -410,8 +416,9 @@ func (tree *Tree) replaceNode(old *Node, new *Node, dryrun bool) { oldParent.RightKey(newKey) } // we can have case like: remove a node, then add it again - tree.Save(oldParent, dryrun) - // } + if oldParent != nil { + tree.Save(oldParent, dryrun) + } } if new != nil { // here is the swap, not update key @@ -503,10 +510,12 @@ 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) return tree.db.Put(node.Key, node.Item, dryrun) } func (tree *Tree) deleteCase1(node *Node, dryrun bool) { + log.Debug("delete case 1", "node value", hex.EncodeToString(node.Value())) if tree.IsEmptyKey(node.ParentKey()) { tree.deleteNode(node, dryrun) return @@ -516,6 +525,7 @@ func (tree *Tree) deleteCase1(node *Node, dryrun bool) { } func (tree *Tree) deleteCase2(node *Node, dryrun bool) { + log.Debug("delete case 2", "node value", hex.EncodeToString(node.Value())) parent := node.Parent(tree, dryrun) sibling := node.sibling(tree, dryrun) @@ -535,7 +545,7 @@ func (tree *Tree) deleteCase2(node *Node, dryrun bool) { } func (tree *Tree) deleteCase3(node *Node, dryrun bool) { - + log.Debug("delete case 3", "node value", hex.EncodeToString(node.Value())) parent := node.Parent(tree, dryrun) sibling := node.sibling(tree, dryrun) siblingLeft := sibling.Left(tree, dryrun) @@ -548,9 +558,7 @@ func (tree *Tree) deleteCase3(node *Node, dryrun bool) { sibling.Item.Color = red tree.Save(sibling, dryrun) tree.deleteCase1(parent, dryrun) - log.Debug("delete node, key: %x, parentKey :%x\n", node.Key, parent.Key) tree.deleteNode(node, dryrun) - } else { tree.deleteCase4(node, dryrun) } @@ -558,6 +566,7 @@ func (tree *Tree) deleteCase3(node *Node, dryrun bool) { } func (tree *Tree) deleteCase4(node *Node, dryrun bool) { + log.Debug("delete case 4", "node value", hex.EncodeToString(node.Value())) parent := node.Parent(tree, dryrun) sibling := node.sibling(tree, dryrun) siblingLeft := sibling.Left(tree, dryrun) @@ -577,6 +586,7 @@ func (tree *Tree) deleteCase4(node *Node, dryrun bool) { } func (tree *Tree) deleteCase5(node *Node, dryrun bool) { + log.Debug("delete case 5", "node value", hex.EncodeToString(node.Value())) parent := node.Parent(tree, dryrun) sibling := node.sibling(tree, dryrun) siblingLeft := sibling.Left(tree, dryrun) @@ -612,6 +622,7 @@ func (tree *Tree) deleteCase5(node *Node, dryrun bool) { } func (tree *Tree) deleteCase6(node *Node, dryrun bool) { + log.Debug("delete case 6", "node value", hex.EncodeToString(node.Value())) parent := node.Parent(tree, dryrun) sibling := node.sibling(tree, dryrun) siblingLeft := sibling.Left(tree, dryrun) @@ -645,5 +656,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) tree.db.Delete(node.Key, dryrun) } diff --git a/tomox/snapshot.go b/tomox/snapshot.go index 6c80211d61a0..878c5705f738 100644 --- a/tomox/snapshot.go +++ b/tomox/snapshot.go @@ -104,7 +104,7 @@ func prepareOrderTreeData(tree *OrderTree) (*OrderTreeSnapshot, error) { snap.OrderTreeItem = serializedTree snap.OrderList = make(map[common.Hash]*OrderListSnapshot) - if !tree.NotEmpty() { + if !tree.NotEmpty() || tree.PriceTree.Size() == 0 { return snap, nil } // foreach each price, snapshot its orderlist diff --git a/tomox/tomox.go b/tomox/tomox.go index 8b3892a1ebe1..7b48dd900143 100644 --- a/tomox/tomox.go +++ b/tomox/tomox.go @@ -793,6 +793,7 @@ func (tomox *TomoX) ProcessOrderPending() map[common.Hash]TxDataMatch { txMatches := make(map[common.Hash]TxDataMatch) tomox.db.InitDryRunMode() + log.Debug("Get pending hashes") pendingHashes := tomox.getPendingHashes() if len(pendingHashes) > 0 { for i, orderHash := range pendingHashes { From baaf435df77218f132a40c2c40052b8692830dbd Mon Sep 17 00:00:00 2001 From: Tuna Date: Tue, 30 Jul 2019 10:54:01 +0700 Subject: [PATCH 2/3] fix delete logic --- tomox/redblacktree.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tomox/redblacktree.go b/tomox/redblacktree.go index 3bcdcacfdf5e..2cff30efb772 100644 --- a/tomox/redblacktree.go +++ b/tomox/redblacktree.go @@ -178,19 +178,22 @@ func (tree *Tree) Remove(key []byte, dryrun bool) { } else { child = right } + if child == nil { + tree.deleteNode(node, dryrun) + } else { + if node.Item.Color == black { + node.Item.Color = nodeColor(child) + tree.Save(node, dryrun) - if node.Item.Color == black { - node.Item.Color = nodeColor(child) - tree.Save(node, dryrun) - - tree.deleteCase1(node, dryrun) - } + tree.deleteCase1(node, dryrun) + } - tree.replaceNode(node, child, dryrun) + tree.replaceNode(node, child, dryrun) - if tree.IsEmptyKey(node.ParentKey()) && child != nil { - child.Item.Color = black - tree.Save(child, dryrun) + if tree.IsEmptyKey(node.ParentKey()) && child != nil { + child.Item.Color = black + tree.Save(child, dryrun) + } } } From 58ddd2f7d9011da328ea42fc90df788777f4066e Mon Sep 17 00:00:00 2001 From: Tuna Date: Tue, 30 Jul 2019 13:57:46 +0700 Subject: [PATCH 3/3] add more log 2 --- tomox/orderbook.go | 38 +++++++++++++++++++------------------- tomox/orderlist.go | 6 +++--- tomox/ordertree.go | 8 ++++---- tomox/redblacktree.go | 12 ++++++++---- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/tomox/orderbook.go b/tomox/orderbook.go index 371775cab764..77478b70f4d9 100755 --- a/tomox/orderbook.go +++ b/tomox/orderbook.go @@ -253,26 +253,26 @@ 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 @@ -280,26 +280,26 @@ func (orderBook *OrderBook) processLimitOrder(order *OrderItem, verbose bool, dr } 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 @@ -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 @@ -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) @@ -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) { @@ -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() @@ -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) } diff --git a/tomox/orderlist.go b/tomox/orderlist.go index 0cb3e988d573..f7dbe10cdf55 100755 --- a/tomox/orderlist.go +++ b/tomox/orderlist.go @@ -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) } @@ -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-- diff --git a/tomox/ordertree.go b/tomox/ordertree.go index d4308b08a101..51302d3e0c4f 100755 --- a/tomox/ordertree.go +++ b/tomox/ordertree.go @@ -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 } diff --git a/tomox/redblacktree.go b/tomox/redblacktree.go index 2cff30efb772..cd5e4468843b 100644 --- a/tomox/redblacktree.go +++ b/tomox/redblacktree.go @@ -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()) { @@ -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()) @@ -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()) @@ -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 @@ -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) } @@ -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) }