Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #810 from tomochain/revert-806-ignore-orders-in-ol…
Browse files Browse the repository at this point in the history
…d-blocks

Revert "Ignore orders in old blocks"
  • Loading branch information
thanhson1085 authored Nov 8, 2019
2 parents e84311f + d2f4b7f commit 38c8d21
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
5 changes: 1 addition & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2263,10 +2263,7 @@ func (bc *BlockChain) logExchangeData(block *types.Block) {
}()
for _, txMatchBatch := range txMatchBatchData {
for _, txMatch := range txMatchBatch.Data {
// the smallest time unit in mongodb is millisecond
// hence, we should update time in second
// old txData has been attached with nanosecond, to avoid hard fork, convert nanosecond to millisecond here
txMatchTime := time.Unix(txMatchBatch.Timestamp / 1e6, 0).UTC()
txMatchTime := time.Unix(0, txMatchBatch.Timestamp)
if err := tomoXService.SyncDataToSDKNode(txMatch, txMatchBatch.TxHash, txMatchTime, currentState); err != nil {
log.Error("failed to SyncDataToSDKNode ", "blockNumber", block.Number(), "err", err)
return
Expand Down
8 changes: 4 additions & 4 deletions tomox/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package tomox
import (
"encoding/json"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/tomox/tomox_state"
"math/big"
"strconv"

"github.com/ethereum/go-ethereum/common"
)

type Comparator func(a, b []byte) int
Expand Down Expand Up @@ -269,8 +269,8 @@ func DecodeTxMatchesBatch(data []byte) (TxMatchBatch, error) {
return txMatchResult, nil
}

func GetOrderHistoryKey(baseToken, quoteToken common.Address, orderId uint64) common.Hash {
return crypto.Keccak256Hash(baseToken.Bytes(), quoteToken.Bytes(), []byte(strconv.FormatUint(orderId, 10)))
func GetOrderHistoryKey(pairName string, orderId uint64) common.Hash {
return common.StringToHash(pairName + strconv.FormatUint(orderId, 10))
}
func (tx TxDataMatch) DecodeOrder() (*tomox_state.OrderItem, error) {
order := &tomox_state.OrderItem{}
Expand Down
33 changes: 8 additions & 25 deletions tomox/tomox.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,10 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
if updatedTakerOrder.CreatedAt.IsZero() {
updatedTakerOrder.CreatedAt = txMatchTime
}
if !txMatchTime.After(updatedTakerOrder.UpdatedAt) {
log.Debug("Ignore old orders/trades", "txHash", txHash.Hex(), "txTime", txMatchTime)
return nil
}
tomox.UpdateOrderCache(updatedTakerOrder.BaseToken, updatedTakerOrder.QuoteToken, updatedTakerOrder.OrderID, txHash, lastState)
updatedTakerOrder.UpdatedAt = txMatchTime

tomox.UpdateOrderCache(updatedTakerOrder.PairName, updatedTakerOrder.OrderID, txHash, lastState)

// 2. put trades to db and update status to FILLED
trades := txDataMatch.GetTrades()
log.Debug("Got trades", "number", len(trades), "txhash", txHash.Hex())
Expand Down Expand Up @@ -397,15 +394,12 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
makerOrders := db.GetListOrderByHashes(makerDirtyHashes)
log.Debug("Maker dirty orders", "len", len(makerOrders), "txhash", txHash.Hex())
for _, o := range makerOrders {
if !txMatchTime.After(o.UpdatedAt) {
continue
}
lastState = OrderHistoryItem{
TxHash: o.TxHash,
FilledAmount: CloneBigInt(o.FilledAmount),
Status: o.Status,
}
tomox.UpdateOrderCache(o.BaseToken, o.QuoteToken, o.OrderID, txHash, lastState)
tomox.UpdateOrderCache(o.PairName, o.OrderID, txHash, lastState)
o.TxHash = txHash
o.UpdatedAt = txMatchTime
o.FilledAmount.Add(o.FilledAmount, makerDirtyFilledAmount[o.Hash.Hex()])
Expand All @@ -432,15 +426,7 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
// updateRejectedOrders
for _, rejectedOrder := range rejectedOrders {
rejectedHashes = append(rejectedHashes, rejectedOrder.Hash.Hex())
if updatedTakerOrder.Hash == rejectedOrder.Hash && txMatchTime.After(updatedTakerOrder.UpdatedAt) {
// cache order history for handling reorg
orderHistoryRecord := OrderHistoryItem{
TxHash: updatedTakerOrder.TxHash,
FilledAmount: CloneBigInt(updatedTakerOrder.FilledAmount),
Status: updatedTakerOrder.Status,
}
tomox.UpdateOrderCache(updatedTakerOrder.BaseToken, updatedTakerOrder.QuoteToken, updatedTakerOrder.OrderID, txHash, orderHistoryRecord)

if updatedTakerOrder.Hash == rejectedOrder.Hash {
updatedTakerOrder.Status = OrderStatusRejected
if err := db.PutObject(updatedTakerOrder.Hash, updatedTakerOrder); err != nil {
return fmt.Errorf("SDKNode: failed to reject takerOrder. Hash: %s Error: %s", updatedTakerOrder.Hash.Hex(), err.Error())
Expand All @@ -449,16 +435,13 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
}
dirtyRejectedOrders := db.GetListOrderByHashes(rejectedHashes)
for _, order := range dirtyRejectedOrders {
if !txMatchTime.After(order.UpdatedAt) {
continue
}
// cache order history for handling reorg
orderHistoryRecord := OrderHistoryItem{
TxHash: order.TxHash,
FilledAmount: CloneBigInt(order.FilledAmount),
Status: order.Status,
}
tomox.UpdateOrderCache(order.BaseToken, order.QuoteToken, order.OrderID, txHash, orderHistoryRecord)
tomox.UpdateOrderCache(order.PairName, order.OrderID, txHash, orderHistoryRecord)

order.Status = OrderStatusRejected
if err = db.PutObject(order.Hash, order); err != nil {
Expand Down Expand Up @@ -495,15 +478,15 @@ func (tomox *TomoX) GetTomoxStateRoot(block *types.Block) (common.Hash, error) {
return tomox_state.EmptyRoot, nil
}

func (tomox *TomoX) UpdateOrderCache(baseToken, quoteToken common.Address, orderId uint64, txhash common.Hash, lastState OrderHistoryItem) {
func (tomox *TomoX) UpdateOrderCache(pair string, orderId uint64, txhash common.Hash, lastState OrderHistoryItem) {
var orderCacheAtTxHash map[common.Hash]OrderHistoryItem
c, ok := tomox.orderCache.Get(txhash)
if !ok || c == nil {
orderCacheAtTxHash = make(map[common.Hash]OrderHistoryItem)
} else {
orderCacheAtTxHash = c.(map[common.Hash]OrderHistoryItem)
}
orderKey := GetOrderHistoryKey(baseToken, quoteToken, orderId)
orderKey := GetOrderHistoryKey(pair, orderId)
_, ok = orderCacheAtTxHash[orderKey]
if !ok {
orderCacheAtTxHash[orderKey] = lastState
Expand All @@ -524,7 +507,7 @@ func (tomox *TomoX) RollbackReorgTxMatch(txhash common.Hash) {
continue
}
orderCacheAtTxHash := c.(map[common.Hash]OrderHistoryItem)
orderHistoryItem, _ := orderCacheAtTxHash[GetOrderHistoryKey(order.BaseToken, order.QuoteToken, order.OrderID)]
orderHistoryItem, _ := orderCacheAtTxHash[GetOrderHistoryKey(order.PairName, order.OrderID)]
if (orderHistoryItem == OrderHistoryItem{}) {
log.Debug("Tomox reorg: remove order due to empty orderHistory", "order", ToJSON(order))
if err := db.DeleteObject(order.Hash); err != nil {
Expand Down

0 comments on commit 38c8d21

Please sign in to comment.