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

     1  package herocache
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/module"
    10  	herocache "github.com/onflow/flow-go/module/mempool/herocache/backdata"
    11  	"github.com/onflow/flow-go/module/mempool/herocache/backdata/heropool"
    12  	"github.com/onflow/flow-go/module/mempool/stdmap"
    13  )
    14  
    15  type Transactions struct {
    16  	c *stdmap.Backend
    17  }
    18  
    19  // NewTransactions implements a transactions mempool based on hero cache.
    20  func NewTransactions(limit uint32, logger zerolog.Logger, collector module.HeroCacheMetrics) *Transactions {
    21  	t := &Transactions{
    22  		c: stdmap.NewBackend(
    23  			stdmap.WithBackData(
    24  				herocache.NewCache(limit,
    25  					herocache.DefaultOversizeFactor,
    26  					heropool.LRUEjection,
    27  					logger.With().Str("mempool", "transactions").Logger(),
    28  					collector))),
    29  	}
    30  
    31  	return t
    32  }
    33  
    34  // Has checks whether the transaction with the given hash is currently in
    35  // the memory pool.
    36  func (t Transactions) Has(id flow.Identifier) bool {
    37  	return t.c.Has(id)
    38  }
    39  
    40  // Add adds a transaction to the mempool.
    41  func (t *Transactions) Add(tx *flow.TransactionBody) bool {
    42  	// Warning! reference pointer must be dereferenced before adding to HeroCache.
    43  	// This is crucial for its heap object optimizations.
    44  	return t.c.Add(*tx)
    45  }
    46  
    47  // ByID returns the transaction with the given ID from the mempool.
    48  func (t Transactions) ByID(txID flow.Identifier) (*flow.TransactionBody, bool) {
    49  	entity, exists := t.c.ByID(txID)
    50  	if !exists {
    51  		return nil, false
    52  	}
    53  	tx, ok := entity.(flow.TransactionBody)
    54  	if !ok {
    55  		panic(fmt.Sprintf("invalid entity in transaction pool (%T)", entity))
    56  	}
    57  	return &tx, true
    58  }
    59  
    60  // All returns all transactions from the mempool. Since it is using the HeroCache, All guarantees returning
    61  // all transactions in the same order as they are added.
    62  func (t Transactions) All() []*flow.TransactionBody {
    63  	entities := t.c.All()
    64  	txs := make([]*flow.TransactionBody, 0, len(entities))
    65  	for _, entity := range entities {
    66  		tx, ok := entity.(flow.TransactionBody)
    67  		if !ok {
    68  			panic(fmt.Sprintf("invalid entity in transaction pool (%T)", entity))
    69  		}
    70  		txs = append(txs, &tx)
    71  	}
    72  	return txs
    73  }
    74  
    75  // Clear removes all transactions stored in this mempool.
    76  func (t *Transactions) Clear() {
    77  	t.c.Clear()
    78  }
    79  
    80  // Size returns total number of stored transactions.
    81  func (t Transactions) Size() uint {
    82  	return t.c.Size()
    83  }
    84  
    85  // Remove removes transaction from mempool.
    86  func (t *Transactions) Remove(id flow.Identifier) bool {
    87  	return t.c.Remove(id)
    88  }