Skip to content

Commit

Permalink
Merge pull request ethereum#588 from ngtuna/remove-order-mongo
Browse files Browse the repository at this point in the history
remove processed order in mongodb, only keep cancelled order
  • Loading branch information
ngtuna authored Jul 25, 2019
2 parents a9c207f + aeacca2 commit 56610b5
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions tomox/tomox.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (tomox *TomoX) CancelOrder(order *OrderItem, dryrun bool) error {
return err
}
if o := tomox.getOrderPending(order.Hash); o != nil {
if err := tomox.removeOrderPending(order.Hash); err != nil {
if err := tomox.cancelOrderPending(order.Hash); err != nil {
return err
}
}
Expand Down Expand Up @@ -912,12 +912,12 @@ func (tomox *TomoX) addOrderPending(order *OrderItem) error {
return nil
}

func (tomox *TomoX) removeOrderPending(orderHash common.Hash) error {
func (tomox *TomoX) cancelOrderPending(orderHash common.Hash) error {
prefix := []byte(pendingPrefix)
key := append(prefix, orderHash.Bytes()...)
log.Debug("Remove order pending", "orderHash", orderHash, "key", hex.EncodeToString(key))
log.Debug("Cancel order pending", "orderHash", orderHash, "key", hex.EncodeToString(key))
if tomox.IsSDKNode() {
log.Debug("Update order status to CANCELLED", "orderHash", orderHash)
log.Debug("Update order status to CANCELLED in sdk node", "orderHash", orderHash)
data, err := tomox.db.Get(key, &OrderItem{}, false)
if err != nil || data == nil {
log.Error("Order doesn't exist in pending", "orderHash", orderHash, "err", err)
Expand All @@ -926,7 +926,6 @@ func (tomox *TomoX) removeOrderPending(orderHash common.Hash) error {
switch data.(type) {
case *OrderItem:
o := data.(*OrderItem)
//change status to CANCELLED then put it back to DB
o.Status = Cancel
if err = tomox.db.Put(key, o, false); err != nil {
log.Error("Can't update order status in mongo", "err", err)
Expand All @@ -945,6 +944,28 @@ func (tomox *TomoX) removeOrderPending(orderHash common.Hash) error {
return nil
}

func (tomox *TomoX) removeOrderPending(orderHash common.Hash) error {
prefix := []byte(pendingPrefix)
key := append(prefix, orderHash.Bytes()...)
log.Debug("Remove order pending", "orderHash", orderHash, "key", hex.EncodeToString(key))
data, err := tomox.db.Get(key, &OrderItem{}, false)
if err != nil || data == nil {
log.Error("Order doesn't exist in pending", "orderHash", orderHash, "err", err)
return err
}
switch data.(type) {
case *OrderItem:
if err := tomox.db.Delete(key, false); err != nil {
log.Error("Fail to delete order pending", "err", err)
return err
}
default:
return fmt.Errorf("Order is corrupted")
}

return nil
}

func (tomox *TomoX) addPendingHash(orderHash common.Hash) error {
log.Debug("Add pending hash", "orderHash", orderHash)
pendingHashes := tomox.getPendingHashes()
Expand Down

0 comments on commit 56610b5

Please sign in to comment.