github.com/koko1123/flow-go-1@v0.29.6/module/mempool/stdmap/blocks.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  // Blocks implements the blocks memory pool.
    10  type Blocks struct {
    11  	*Backend
    12  }
    13  
    14  // NewBlocks creates a new memory pool for blocks.
    15  func NewBlocks(limit uint) (*Blocks, error) {
    16  	a := &Blocks{
    17  		Backend: NewBackend(WithLimit(limit)),
    18  	}
    19  
    20  	return a, nil
    21  }
    22  
    23  // Add adds an block to the mempool.
    24  func (a *Blocks) Add(block *flow.Block) bool {
    25  	return a.Backend.Add(block)
    26  }
    27  
    28  // ByID returns the block with the given ID from the mempool.
    29  func (a *Blocks) ByID(blockID flow.Identifier) (*flow.Block, bool) {
    30  	entity, exists := a.Backend.ByID(blockID)
    31  	if !exists {
    32  		return nil, false
    33  	}
    34  	block := entity.(*flow.Block)
    35  	return block, true
    36  }
    37  
    38  // All returns all blocks from the pool.
    39  func (a *Blocks) All() []*flow.Block {
    40  	entities := a.Backend.All()
    41  	blocks := make([]*flow.Block, 0, len(entities))
    42  	for _, entity := range entities {
    43  		blocks = append(blocks, entity.(*flow.Block))
    44  	}
    45  	return blocks
    46  }