github.com/annchain/OG@v0.0.9/archive/tx_counter.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 archive
    15  
    16  import (
    17  	"github.com/annchain/OG/arefactor/common/goroutine"
    18  	types2 "github.com/annchain/OG/arefactor/og/types"
    19  	"github.com/annchain/OG/og/types"
    20  	"go.uber.org/atomic"
    21  	"time"
    22  )
    23  
    24  type TxCounter struct {
    25  	TxGenerated        atomic.Uint64 `json:"txGenerated"`        // tx generated by me
    26  	TxReceived         atomic.Uint64 `json:"txReceived"`         // tx generated by me and others
    27  	TxConfirmed        atomic.Uint64 `json:"txConfirmed"`        // tx confirmed
    28  	SequencerGenerated atomic.Uint64 `json:"sequencerGenerated"` // sequencer generated by me
    29  	SequencerReceived  atomic.Uint64 `json:"sequencerReceived"`  // sequencer generated by me and others
    30  	SequencerConfirmed atomic.Uint64 `json:"sequencerConfirmed"` // sequencer confirmed
    31  	StartupTime        time.Time     `json:"startupTime"`        // timestamp of the program started.
    32  
    33  	// listeners
    34  	NewTxReceivedChan  chan types.Txi                 `json:"-"`
    35  	NewTxGeneratedChan chan types.Txi                 `json:"-"`
    36  	BatchConfirmedChan chan map[types2.Hash]types.Txi `json:"-"`
    37  	quit               chan bool                      `json:"-"`
    38  }
    39  
    40  func NewTxCounter() *TxCounter {
    41  	return &TxCounter{
    42  		BatchConfirmedChan: make(chan map[types2.Hash]types.Txi),
    43  		NewTxReceivedChan:  make(chan types.Txi),
    44  		NewTxGeneratedChan: make(chan types.Txi),
    45  		quit:               make(chan bool),
    46  		StartupTime:        time.Now(),
    47  	}
    48  }
    49  
    50  func (t *TxCounter) loop() {
    51  	for {
    52  		select {
    53  		case <-t.quit:
    54  			break
    55  		case tx := <-t.NewTxReceivedChan:
    56  			switch tx.GetType() {
    57  
    58  			case types.TxBaseTypeSequencer:
    59  				t.SequencerReceived.Inc()
    60  			default:
    61  				t.TxReceived.Inc()
    62  				//logrus.WithField("type", tx.GetType()).Debug("Unknown tx type")
    63  			}
    64  		case batch := <-t.BatchConfirmedChan:
    65  			for _, tx := range batch {
    66  				switch tx.GetType() {
    67  				case types.TxBaseTypeSequencer:
    68  					t.SequencerConfirmed.Inc()
    69  				default:
    70  					t.TxConfirmed.Inc()
    71  					//logrus.WithField("type", tx.GetType()).Debug("Unknown tx type")
    72  				}
    73  			}
    74  		case tx := <-t.NewTxGeneratedChan:
    75  			switch tx.GetType() {
    76  			case types.TxBaseTypeSequencer:
    77  				t.SequencerGenerated.Inc()
    78  			default:
    79  				t.TxGenerated.Inc()
    80  				//logrus.WithField("type", tx.GetType()).Debug("Unknown tx type")
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  func (t *TxCounter) Start() {
    87  	goroutine.New(t.loop)
    88  }
    89  
    90  func (t *TxCounter) Stop() {
    91  	close(t.quit)
    92  }
    93  
    94  func (*TxCounter) Name() string {
    95  	return "TxCounter"
    96  }
    97  
    98  func (t *TxCounter) GetBenchmarks() map[string]interface{} {
    99  	return map[string]interface{}{
   100  		"txGenerated":        t.TxGenerated.Load(),
   101  		"txReceived":         t.TxReceived.Load(),
   102  		"txConfirmed":        t.TxConfirmed.Load(),
   103  		"sequencerGenerated": t.SequencerGenerated.Load(),
   104  		"sequencerReceived":  t.SequencerReceived.Load(),
   105  		"sequencerConfirmed": t.SequencerConfirmed.Load(),
   106  		"startupTime":        t.StartupTime.Unix(),
   107  	}
   108  }