github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/stdmap/transaction_timings.go (about)

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