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

Commit 38c8d21

Browse files
authored
Merge pull request #810 from tomochain/revert-806-ignore-orders-in-old-blocks
Revert "Ignore orders in old blocks"
2 parents e84311f + d2f4b7f commit 38c8d21

File tree

3 files changed

+13
-33
lines changed

3 files changed

+13
-33
lines changed

core/blockchain.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,10 +2263,7 @@ func (bc *BlockChain) logExchangeData(block *types.Block) {
22632263
}()
22642264
for _, txMatchBatch := range txMatchBatchData {
22652265
for _, txMatch := range txMatchBatch.Data {
2266-
// the smallest time unit in mongodb is millisecond
2267-
// hence, we should update time in second
2268-
// old txData has been attached with nanosecond, to avoid hard fork, convert nanosecond to millisecond here
2269-
txMatchTime := time.Unix(txMatchBatch.Timestamp / 1e6, 0).UTC()
2266+
txMatchTime := time.Unix(0, txMatchBatch.Timestamp)
22702267
if err := tomoXService.SyncDataToSDKNode(txMatch, txMatchBatch.TxHash, txMatchTime, currentState); err != nil {
22712268
log.Error("failed to SyncDataToSDKNode ", "blockNumber", block.Number(), "err", err)
22722269
return

tomox/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package tomox
33
import (
44
"encoding/json"
55
"errors"
6-
"github.com/ethereum/go-ethereum/common"
7-
"github.com/ethereum/go-ethereum/crypto"
86
"github.com/ethereum/go-ethereum/tomox/tomox_state"
97
"math/big"
108
"strconv"
9+
10+
"github.com/ethereum/go-ethereum/common"
1111
)
1212

1313
type Comparator func(a, b []byte) int
@@ -269,8 +269,8 @@ func DecodeTxMatchesBatch(data []byte) (TxMatchBatch, error) {
269269
return txMatchResult, nil
270270
}
271271

272-
func GetOrderHistoryKey(baseToken, quoteToken common.Address, orderId uint64) common.Hash {
273-
return crypto.Keccak256Hash(baseToken.Bytes(), quoteToken.Bytes(), []byte(strconv.FormatUint(orderId, 10)))
272+
func GetOrderHistoryKey(pairName string, orderId uint64) common.Hash {
273+
return common.StringToHash(pairName + strconv.FormatUint(orderId, 10))
274274
}
275275
func (tx TxDataMatch) DecodeOrder() (*tomox_state.OrderItem, error) {
276276
order := &tomox_state.OrderItem{}

tomox/tomox.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,10 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
309309
if updatedTakerOrder.CreatedAt.IsZero() {
310310
updatedTakerOrder.CreatedAt = txMatchTime
311311
}
312-
if !txMatchTime.After(updatedTakerOrder.UpdatedAt) {
313-
log.Debug("Ignore old orders/trades", "txHash", txHash.Hex(), "txTime", txMatchTime)
314-
return nil
315-
}
316-
tomox.UpdateOrderCache(updatedTakerOrder.BaseToken, updatedTakerOrder.QuoteToken, updatedTakerOrder.OrderID, txHash, lastState)
317312
updatedTakerOrder.UpdatedAt = txMatchTime
318313

314+
tomox.UpdateOrderCache(updatedTakerOrder.PairName, updatedTakerOrder.OrderID, txHash, lastState)
315+
319316
// 2. put trades to db and update status to FILLED
320317
trades := txDataMatch.GetTrades()
321318
log.Debug("Got trades", "number", len(trades), "txhash", txHash.Hex())
@@ -397,15 +394,12 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
397394
makerOrders := db.GetListOrderByHashes(makerDirtyHashes)
398395
log.Debug("Maker dirty orders", "len", len(makerOrders), "txhash", txHash.Hex())
399396
for _, o := range makerOrders {
400-
if !txMatchTime.After(o.UpdatedAt) {
401-
continue
402-
}
403397
lastState = OrderHistoryItem{
404398
TxHash: o.TxHash,
405399
FilledAmount: CloneBigInt(o.FilledAmount),
406400
Status: o.Status,
407401
}
408-
tomox.UpdateOrderCache(o.BaseToken, o.QuoteToken, o.OrderID, txHash, lastState)
402+
tomox.UpdateOrderCache(o.PairName, o.OrderID, txHash, lastState)
409403
o.TxHash = txHash
410404
o.UpdatedAt = txMatchTime
411405
o.FilledAmount.Add(o.FilledAmount, makerDirtyFilledAmount[o.Hash.Hex()])
@@ -432,15 +426,7 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
432426
// updateRejectedOrders
433427
for _, rejectedOrder := range rejectedOrders {
434428
rejectedHashes = append(rejectedHashes, rejectedOrder.Hash.Hex())
435-
if updatedTakerOrder.Hash == rejectedOrder.Hash && txMatchTime.After(updatedTakerOrder.UpdatedAt) {
436-
// cache order history for handling reorg
437-
orderHistoryRecord := OrderHistoryItem{
438-
TxHash: updatedTakerOrder.TxHash,
439-
FilledAmount: CloneBigInt(updatedTakerOrder.FilledAmount),
440-
Status: updatedTakerOrder.Status,
441-
}
442-
tomox.UpdateOrderCache(updatedTakerOrder.BaseToken, updatedTakerOrder.QuoteToken, updatedTakerOrder.OrderID, txHash, orderHistoryRecord)
443-
429+
if updatedTakerOrder.Hash == rejectedOrder.Hash {
444430
updatedTakerOrder.Status = OrderStatusRejected
445431
if err := db.PutObject(updatedTakerOrder.Hash, updatedTakerOrder); err != nil {
446432
return fmt.Errorf("SDKNode: failed to reject takerOrder. Hash: %s Error: %s", updatedTakerOrder.Hash.Hex(), err.Error())
@@ -449,16 +435,13 @@ func (tomox *TomoX) SyncDataToSDKNode(txDataMatch TxDataMatch, txHash common.Has
449435
}
450436
dirtyRejectedOrders := db.GetListOrderByHashes(rejectedHashes)
451437
for _, order := range dirtyRejectedOrders {
452-
if !txMatchTime.After(order.UpdatedAt) {
453-
continue
454-
}
455438
// cache order history for handling reorg
456439
orderHistoryRecord := OrderHistoryItem{
457440
TxHash: order.TxHash,
458441
FilledAmount: CloneBigInt(order.FilledAmount),
459442
Status: order.Status,
460443
}
461-
tomox.UpdateOrderCache(order.BaseToken, order.QuoteToken, order.OrderID, txHash, orderHistoryRecord)
444+
tomox.UpdateOrderCache(order.PairName, order.OrderID, txHash, orderHistoryRecord)
462445

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

498-
func (tomox *TomoX) UpdateOrderCache(baseToken, quoteToken common.Address, orderId uint64, txhash common.Hash, lastState OrderHistoryItem) {
481+
func (tomox *TomoX) UpdateOrderCache(pair string, orderId uint64, txhash common.Hash, lastState OrderHistoryItem) {
499482
var orderCacheAtTxHash map[common.Hash]OrderHistoryItem
500483
c, ok := tomox.orderCache.Get(txhash)
501484
if !ok || c == nil {
502485
orderCacheAtTxHash = make(map[common.Hash]OrderHistoryItem)
503486
} else {
504487
orderCacheAtTxHash = c.(map[common.Hash]OrderHistoryItem)
505488
}
506-
orderKey := GetOrderHistoryKey(baseToken, quoteToken, orderId)
489+
orderKey := GetOrderHistoryKey(pair, orderId)
507490
_, ok = orderCacheAtTxHash[orderKey]
508491
if !ok {
509492
orderCacheAtTxHash[orderKey] = lastState
@@ -524,7 +507,7 @@ func (tomox *TomoX) RollbackReorgTxMatch(txhash common.Hash) {
524507
continue
525508
}
526509
orderCacheAtTxHash := c.(map[common.Hash]OrderHistoryItem)
527-
orderHistoryItem, _ := orderCacheAtTxHash[GetOrderHistoryKey(order.BaseToken, order.QuoteToken, order.OrderID)]
510+
orderHistoryItem, _ := orderCacheAtTxHash[GetOrderHistoryKey(order.PairName, order.OrderID)]
528511
if (orderHistoryItem == OrderHistoryItem{}) {
529512
log.Debug("Tomox reorg: remove order due to empty orderHistory", "order", ToJSON(order))
530513
if err := db.DeleteObject(order.Hash); err != nil {

0 commit comments

Comments
 (0)