-
Notifications
You must be signed in to change notification settings - Fork 177
/
blocks.go
46 lines (38 loc) · 1011 Bytes
/
blocks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
)
// Blocks implements the blocks memory pool.
type Blocks struct {
*Backend
}
// NewBlocks creates a new memory pool for blocks.
func NewBlocks(limit uint) (*Blocks, error) {
a := &Blocks{
Backend: NewBackend(WithLimit(limit)),
}
return a, nil
}
// Add adds an block to the mempool.
func (a *Blocks) Add(block *flow.Block) bool {
return a.Backend.Add(block)
}
// ByID returns the block with the given ID from the mempool.
func (a *Blocks) ByID(blockID flow.Identifier) (*flow.Block, bool) {
entity, exists := a.Backend.ByID(blockID)
if !exists {
return nil, false
}
block := entity.(*flow.Block)
return block, true
}
// All returns all blocks from the pool.
func (a *Blocks) All() []*flow.Block {
entities := a.Backend.All()
blocks := make([]*flow.Block, 0, len(entities))
for _, entity := range entities {
blocks = append(blocks, entity.(*flow.Block))
}
return blocks
}