github.com/koko1123/flow-go-1@v0.29.6/module/mempool/stdmap/transaction_timings.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package stdmap
     4  
     5  import (
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  )
     8  
     9  // TransactionTimings implements the transaction timings memory pool of access nodes,
    10  // used to store transaction timings to report the timing of individual transactions
    11  type TransactionTimings struct {
    12  	*Backend
    13  }
    14  
    15  // NewTransactionTimings creates a new memory pool for transaction timings
    16  func NewTransactionTimings(limit uint) (*TransactionTimings, error) {
    17  	t := &TransactionTimings{
    18  		Backend: NewBackend(WithLimit(limit)),
    19  	}
    20  
    21  	return t, nil
    22  }
    23  
    24  // Add adds a transaction timing to the mempool.
    25  func (t *TransactionTimings) Add(tx *flow.TransactionTiming) bool {
    26  	return t.Backend.Add(tx)
    27  }
    28  
    29  // ByID returns the transaction timing with the given ID from the mempool.
    30  func (t *TransactionTimings) ByID(txID flow.Identifier) (*flow.TransactionTiming, bool) {
    31  	entity, exists := t.Backend.ByID(txID)
    32  	if !exists {
    33  		return nil, false
    34  	}
    35  	tt, ok := entity.(*flow.TransactionTiming)
    36  	if !ok {
    37  		return nil, false
    38  	}
    39  	return tt, true
    40  }
    41  
    42  // Adjust will adjust the transaction timing using the given function if the given key can be found.
    43  // Returns a bool which indicates whether the value was updated as well as the updated value.
    44  func (t *TransactionTimings) Adjust(txID flow.Identifier, f func(*flow.TransactionTiming) *flow.TransactionTiming) (
    45  	*flow.TransactionTiming, bool) {
    46  	e, updated := t.Backend.Adjust(txID, func(e flow.Entity) flow.Entity {
    47  		tt, ok := e.(*flow.TransactionTiming)
    48  		if !ok {
    49  			return nil
    50  		}
    51  		return f(tt)
    52  	})
    53  	if !updated {
    54  		return nil, false
    55  	}
    56  	tt, ok := e.(*flow.TransactionTiming)
    57  	if !ok {
    58  		return nil, false
    59  	}
    60  	return tt, updated
    61  }
    62  
    63  // All returns all transaction timings from the mempool.
    64  func (t *TransactionTimings) All() []*flow.TransactionTiming {
    65  	entities := t.Backend.All()
    66  	txs := make([]*flow.TransactionTiming, 0, len(entities))
    67  	for _, entity := range entities {
    68  		txs = append(txs, entity.(*flow.TransactionTiming))
    69  	}
    70  	return txs
    71  }
    72  
    73  // Remove removes the transaction timing with the given ID.
    74  func (t *TransactionTimings) Remove(txID flow.Identifier) bool {
    75  	return t.Backend.Remove(txID)
    76  }