OSDN Git Service

fix match collector (#481)
authorPoseidon <shenao.78@163.com>
Thu, 6 Feb 2020 10:39:34 +0000 (18:39 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2020 10:39:34 +0000 (18:39 +0800)
* fix match collector

* add comment

application/mov/match_collector.go

index 033194e..21cd662 100644 (file)
@@ -63,6 +63,15 @@ func (m *matchCollector) collect() ([]*types.Tx, error) {
        defer close(m.closeCh)
 
        var matchedTxs []*types.Tx
+       appendMatchedTxs := func(data *matchTxResult) bool {
+               gasUsed := calcMatchedTxGasUsed(data.matchedTx)
+               if m.gasLeft -= gasUsed; m.gasLeft >= 0 {
+                       matchedTxs = append(matchedTxs, data.matchedTx)
+                       return false
+               }
+               return true
+       }
+
        completed := 0
        for !m.isTimeout() {
                select {
@@ -71,14 +80,20 @@ func (m *matchCollector) collect() ([]*types.Tx, error) {
                                return nil, data.err
                        }
 
-                       gasUsed := calcMatchedTxGasUsed(data.matchedTx)
-                       if m.gasLeft -= gasUsed; m.gasLeft >= 0 {
-                               matchedTxs = append(matchedTxs, data.matchedTx)
-                       } else {
+                       if done := appendMatchedTxs(data); done {
                                return matchedTxs, nil
                        }
                case <-m.workerNumCh:
                        if completed++; completed == m.workerNum {
+                               // read the remaining process results
+                               close(m.processCh)
+                               for data := range m.processCh {
+                                       if data.err != nil {
+                                               return nil, data.err
+                                       }
+
+                                       appendMatchedTxs(data)
+                               }
                                return matchedTxs, nil
                        }
                }