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