github.com/annchain/OG@v0.0.9/og/syncer/message_handler.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package syncer
    15  
    16  import (
    17  	"github.com/annchain/OG/og/types"
    18  	"github.com/annchain/OG/og/types/archive"
    19  	"sort"
    20  )
    21  
    22  func (m *IncrementalSyncer) HandleNewTxi(tx types.Txi, peerId string) {
    23  	// cancel pending requests if it is there
    24  	if !m.Enabled {
    25  		if !m.cacheNewTxEnabled() {
    26  			log.Debug("incremental received nexTx but sync disabled")
    27  			return
    28  		}
    29  	}
    30  	m.RemoveContrlMsgFromCache(tx.GetHash())
    31  	m.firedTxCache.Remove(tx.GetHash())
    32  	if m.isKnownHash(tx.GetHash()) {
    33  		log.WithField("tx ", tx).Debug("duplicated tx received")
    34  		return
    35  	}
    36  	if !m.Enabled {
    37  		log.WithField("tx ", tx).Debug("cache txs for future.")
    38  	}
    39  
    40  	if tx.GetType() == types.TxBaseTypeSequencer {
    41  		m.SequencerCache.Add(tx.GetHash(), peerId)
    42  	}
    43  
    44  	err := m.bufferedIncomingTxCache.EnQueue(tx)
    45  	if err != nil {
    46  		log.WithError(err).Warn("add tx to cache error")
    47  	}
    48  	if m.Enabled {
    49  		m.notifyTxEvent <- true
    50  	}
    51  	//m.notifyTxEvent <- true
    52  	//notify channel will be  blocked if tps is high ,check first and add
    53  }
    54  
    55  func (m *IncrementalSyncer) HandleNewTx(newTx *archive.MessageNewTx, peerId string) {
    56  	tx := newTx.RawTx.Tx()
    57  	if tx == nil {
    58  		log.Debug("empty MessageNewTx")
    59  		return
    60  	}
    61  	m.HandleNewTxi(tx, peerId)
    62  	log.WithField("q", newTx).Debug("incremental received MessageNewTx")
    63  
    64  }
    65  
    66  func (m *IncrementalSyncer) HandleNewTxs(newTxs *archive.MessageNewTxs, peerId string) {
    67  	if newTxs.RawTxs == nil || len(*newTxs.RawTxs) == 0 {
    68  		log.Debug("Empty MessageNewTx")
    69  		return
    70  	}
    71  	var validTxs types.Txis
    72  	if !m.Enabled {
    73  		if !m.cacheNewTxEnabled() {
    74  			log.Debug("incremental received nexTx but sync disabled")
    75  			return
    76  		}
    77  	}
    78  	for _, tx := range *newTxs.RawTxs {
    79  		m.RemoveContrlMsgFromCache(tx.GetHash())
    80  		m.firedTxCache.Remove(tx.GetHash())
    81  		if m.isKnownHash(tx.GetHash()) {
    82  			log.WithField("tx ", tx).Debug("duplicated tx received")
    83  			continue
    84  		}
    85  		if !m.Enabled {
    86  			log.WithField("tx ", tx).Debug("cache txs for future.")
    87  		}
    88  		validTxs = append(validTxs, tx.Tx())
    89  	}
    90  
    91  	err := m.bufferedIncomingTxCache.EnQueueBatch(validTxs)
    92  	if err != nil {
    93  		log.WithError(err).Warn("add tx to cache error")
    94  	}
    95  	if m.Enabled {
    96  		m.notifyTxEvent <- true
    97  	}
    98  	log.WithField("q", newTxs).Debug("incremental received MessageNewTxs")
    99  }
   100  
   101  func (m *IncrementalSyncer) HandleNewSequencer(newSeq *archive.MessageNewSequencer, peerId string) {
   102  	seq := newSeq.RawSequencer.Sequencer()
   103  	if seq == nil {
   104  		log.Debug("empty NewSequence")
   105  		return
   106  	}
   107  	m.HandleNewTxi(seq, peerId)
   108  	log.WithField("q", newSeq).Debug("incremental received NewSequence")
   109  }
   110  
   111  func (m *IncrementalSyncer) HandleCampaign(request *types.MessageCampaign, peerId string) {
   112  	cp := request.RawCampaign.Campaign()
   113  	if cp == nil {
   114  		log.Warn("got nil MessageCampaign")
   115  		return
   116  	}
   117  	m.HandleNewTxi(cp, peerId)
   118  	log.WithField("q", request).Debug("incremental received MessageCampaign")
   119  
   120  }
   121  
   122  func (m *IncrementalSyncer) HandleTermChange(request *types.MessageTermChange, peerId string) {
   123  	cp := request.RawTermChange.TermChange()
   124  	if cp == nil {
   125  		log.Warn("got nil MessageCampaign")
   126  		return
   127  	}
   128  	m.HandleNewTxi(cp, peerId)
   129  	log.WithField("q", request).Debug("incremental received MessageTermChange")
   130  
   131  }
   132  
   133  func (m *IncrementalSyncer) HandleArchive(request *types.MessageNewArchive, peerId string) {
   134  	ac := request.Archive
   135  	if ac == nil {
   136  		log.Warn("got nil MessageNewArchive")
   137  		return
   138  	}
   139  	m.HandleNewTxi(ac, peerId)
   140  	log.WithField("q", request).Debug("incremental received MessageNewArchive")
   141  
   142  }
   143  
   144  func (m *IncrementalSyncer) HandleActionTx(request *archive.MessageNewActionTx, peerId string) {
   145  	ax := request.ActionTx
   146  	if ax == nil {
   147  		log.Warn("got nil MessageNewActionTx")
   148  		return
   149  	}
   150  	m.HandleNewTxi(ax, peerId)
   151  	log.WithField("q", request).Debug("incremental received MessageNewActionTx")
   152  }
   153  
   154  func (m *IncrementalSyncer) HandleFetchByHashResponse(syncResponse *archive.MessageSyncResponse, sourceId string) {
   155  	m.bloomFilterStatus.UpdateResponse(syncResponse.RequestedId)
   156  	if syncResponse.RawTxs == nil || len(*syncResponse.RawTxs) == 0 {
   157  		log.Debug("empty MessageSyncResponse")
   158  		return
   159  	}
   160  	//	if len(syncResponse.RawSequencers) != len(syncResponse.SequencerIndex) || len(syncResponse.RawSequencers) > 20 {
   161  	//	log.WithField("length of sequencers", len(syncResponse.RawSequencers)).WithField(
   162  	//	"length of index", len(syncResponse.SequencerIndex)).Warn("is it an attacking ?????")
   163  	//return
   164  	//}
   165  
   166  	var txis types.Txis
   167  	//
   168  	//var currentIndex int
   169  	//var testVal int
   170  	//testVal = len(syncResponse.RawSequencers)
   171  	//}
   172  	for _, rawTx := range *syncResponse.RawTxs {
   173  		/*for ; currentIndex < len(syncResponse.RawSequencers) &&
   174  			uint32(i) == syncResponse.SequencerIndex[currentIndex] ;currentIndex++ {
   175  			testVal++
   176  			tx := syncResponse.RawSequencers[currentIndex].Sequencer()
   177  			m.firedTxCache.Remove(tx.Hash)
   178  			if m.isKnownHash(tx.GetHash()) {
   179  				log.WithField("tx ", tx).Debug("duplicated tx received")
   180  			} else {
   181  				if !m.Enabled {
   182  					log.WithField("tx ", tx).Debug("cache seqs for future.")
   183  				}
   184  				log.WithField("seq ", tx).Debug("received sync response seq")
   185  				txis = append(txis, tx)
   186  			}
   187  
   188  		}
   189  		*/
   190  		if rawTx == nil {
   191  			log.Warn("nil tx received")
   192  			continue
   193  		}
   194  		m.RemoveContrlMsgFromCache(rawTx.GetHash())
   195  		m.firedTxCache.Remove(rawTx.GetHash())
   196  		if m.isKnownHash(rawTx.GetHash()) {
   197  			log.WithField("tx ", rawTx).Debug("duplicated tx received")
   198  			continue
   199  		}
   200  		if !m.Enabled {
   201  			log.WithField("tx ", rawTx).Debug("cache txs for future.")
   202  		}
   203  		txis = append(txis, rawTx.Txi())
   204  	}
   205  	//if testVal!=len(syncResponse.RawSequencers) {
   206  	//panic(fmt.Sprintf("algorithm err ,len mismatch, %d,%d ",testVal, len(syncResponse.RawSequencers)))
   207  	//}
   208  	sort.Sort(txis)
   209  	err := m.bufferedIncomingTxCache.PrependBatch(txis)
   210  	if err != nil {
   211  		log.WithError(err).Warn("add txs error")
   212  	}
   213  	if m.Enabled {
   214  		m.notifyTxEvent <- true
   215  	}
   216  	//if len(txis) > 10 {
   217  	//	start := time.Now()
   218  	//	m.bufferedIncomingTxCache.Sort()
   219  	//	now := time.Now()
   220  	//	log.WithField("len ", m.bufferedIncomingTxCache.Len()).WithField("used for sort ", now.Sub(start)).Debug("sorted data")
   221  	//}
   222  	log.WithField("txis", txis).WithField("peer", sourceId).Debug("received sync response Txs")
   223  }