Skip to content

Commit

Permalink
Merge pull request ethereum#602 from ngtuna/orig-rbt-lib
Browse files Browse the repository at this point in the history
improve rbt, orderlist
  • Loading branch information
ngtuna committed Aug 6, 2019
2 parents 42237b5 + 15850e6 commit ac4fa8c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
2 changes: 1 addition & 1 deletion tomox/iterator.go
Expand Up @@ -156,4 +156,4 @@ func (iterator *Iterator) First(dryrun bool) bool {
func (iterator *Iterator) Last(dryrun bool) bool {
iterator.End()
return iterator.Prev(dryrun)
}
}
16 changes: 13 additions & 3 deletions tomox/orderbook.go
Expand Up @@ -125,23 +125,33 @@ func (orderBook *OrderBook) Save(dryrun bool) error {
}

func (orderBook *OrderBook) Restore(dryrun bool) error {
log.Debug("restore orderbook asks")
if err := orderBook.Asks.Restore(dryrun); err != nil {
log.Error("can't restore orderbook asks", "err", err)
return err
}
// rootKey might change once restoring thus to be safe, we should save changes to DB
if err := orderBook.Asks.Save(dryrun); err != nil {
log.Error("can't save asks changes to DB", "err", err)
return err
}
log.Debug("restored orderbook asks", "asks.Item", orderBook.Bids.Item)

log.Debug("restore orderbook bids")
if err := orderBook.Bids.Restore(dryrun); err != nil {
log.Error("can't restore orderbook bids", "err", err)
return err
}
// rootKey might change once restoring thus to be safe, we should save changes to DB
if err := orderBook.Bids.Save(dryrun); err != nil {
log.Error("can't save bids changes to DB", "err", err)
return err
}
log.Debug("restored orderbook bids", "bids.Item", orderBook.Bids.Item)

orderBookItemKey := append([]byte(orderbookItemPrefix), orderBook.Key...)
val, err := orderBook.db.Get(orderBookItemKey, orderBook.Item, dryrun)
if err == nil {
orderBook.Item = val.(*OrderBookItem)
log.Debug("orderbook restored", "orderBook.Item", orderBook.Item)
log.Debug("restored orderbook", "orderBook.Item", orderBook.Item)
}

return err
Expand Down
17 changes: 4 additions & 13 deletions tomox/orderlist.go
Expand Up @@ -8,7 +8,6 @@ import (

"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
)

Expand Down Expand Up @@ -67,26 +66,18 @@ func NewOrderList(price *big.Int, orderTree *OrderTree) *OrderList {

func NewOrderListWithItem(item *OrderListItem, orderTree *OrderTree) *OrderList {
key := orderTree.getKeyFromPrice(item.Price)

slot := new(big.Int).SetBytes(key)
orderList := &OrderList{
Item: item,
Key: key,
orderTree: orderTree,
}

// priceKey will be slot of order tree + plus price key
// we can use orderList slot as orderbook slot to store sequential of orders
if orderTree.orderBook != nil {
orderList.slot = orderTree.orderBook.Slot
} else {
orderList.slot = new(big.Int).SetBytes(crypto.Keccak256(key))
slot: slot,
}

return 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", hex.EncodeToString(storedKey))
return orderList.orderTree.orderBook.GetOrder(storedKey, key, dryrun)
Expand Down Expand Up @@ -347,6 +338,6 @@ func (orderList *OrderList) Hash() (common.Hash, error) {
return common.BytesToHash(olEncoded), nil
}

func GetOrderListCommonKey(key []byte) []byte {
return append([]byte("OL"), key...)
func GetOrderListCommonKey(key []byte, pairName string) []byte {
return append([]byte(pairName), key...)
}
21 changes: 14 additions & 7 deletions tomox/ordertree.go
Expand Up @@ -73,12 +73,19 @@ func (orderTree *OrderTree) Save(dryrun bool) error {

// update tree meta information, make sure item existed instead of checking rootKey
priceTreeRoot := orderTree.PriceTree.Root(dryrun)
orderTree.Item.PriceTreeSize = orderTree.Depth()

if priceTreeRoot != nil {
orderTree.Item.PriceTreeKey = priceTreeRoot.Key
orderTree.Item.PriceTreeSize = orderTree.Depth()
} else if orderTree.Depth() == 0 {
orderTree.Item.PriceTreeKey = EmptyKey()
orderTree.Item.PriceTreeSize = 0
orderTree.PriceTree.Clear()
} else {
return fmt.Errorf("Ordertree root not found")
}

log.Debug("Save ordertree", "key", hex.EncodeToString(orderTree.GetCommonKey()))
log.Debug("Save ordertree", "key", hex.EncodeToString(orderTree.GetCommonKey()), "order.Item", orderTree.Item)
return orderTree.orderDB.Put(orderTree.GetCommonKey(), orderTree.Item, dryrun)
}

Expand Down Expand Up @@ -135,7 +142,7 @@ func (orderTree *OrderTree) getKeyFromPrice(price *big.Int) []byte {
func (orderTree *OrderTree) PriceList(price *big.Int, dryrun bool) *OrderList {

key := orderTree.getKeyFromPrice(price)
bytes, found := orderTree.PriceTree.Get(GetOrderListCommonKey(key), dryrun)
bytes, found := orderTree.PriceTree.Get(GetOrderListCommonKey(key, orderTree.orderBook.Item.Name), dryrun)
log.Debug("Got orderlist by price", "key", hex.EncodeToString(key), "found", found)

if found {
Expand Down Expand Up @@ -171,7 +178,7 @@ func (orderTree *OrderTree) SaveOrderList(orderList *OrderList, dryrun bool) err
log.Error("Can't encode", "orderList.Item", orderList.Item, "err", err)
return err
}
olKey := GetOrderListCommonKey(orderList.Key)
olKey := GetOrderListCommonKey(orderList.Key, orderTree.orderBook.Item.Name)
log.Debug("Save orderlist", "key", hex.EncodeToString(olKey), "value", value)
return orderTree.PriceTree.Put(olKey, value, dryrun)
}
Expand All @@ -185,7 +192,7 @@ 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))
orderListKey := GetOrderListCommonKey(priceKey)
orderListKey := GetOrderListCommonKey(priceKey, orderTree.orderBook.Item.Name)
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))
Expand All @@ -202,7 +209,7 @@ func (orderTree *OrderTree) RemovePrice(price *big.Int, dryrun bool) error {
func (orderTree *OrderTree) PriceExist(price *big.Int, dryrun bool) bool {

priceKey := orderTree.getKeyFromPrice(price)
orderListKey := GetOrderListCommonKey(priceKey)
orderListKey := GetOrderListCommonKey(priceKey, orderTree.orderBook.Item.Name)
found, _ := orderTree.PriceTree.Has(orderListKey, dryrun)

return found
Expand Down Expand Up @@ -235,7 +242,7 @@ func (orderTree *OrderTree) InsertOrder(order *OrderItem, dryrun bool) error {
// order will be inserted to order list
if orderList != nil {

order := NewOrder(order, GetOrderListCommonKey(orderList.Key))
order := NewOrder(order, GetOrderListCommonKey(orderList.Key, orderTree.orderBook.Item.Name))

if orderList.OrderExist(order.Key, dryrun) {
if err := orderTree.RemoveOrder(order, dryrun); err != nil {
Expand Down

0 comments on commit ac4fa8c

Please sign in to comment.