-
Notifications
You must be signed in to change notification settings - Fork 175
/
pending_blocks.go
69 lines (55 loc) · 1.57 KB
/
pending_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package buffer
import (
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/module"
)
type PendingBlocks struct {
backend *backend
}
var _ module.PendingBlockBuffer = (*PendingBlocks)(nil)
func NewPendingBlocks() *PendingBlocks {
b := &PendingBlocks{backend: newBackend()}
return b
}
func (b *PendingBlocks) Add(originID flow.Identifier, proposal *messages.BlockProposal) bool {
return b.backend.add(originID, proposal.Header, proposal.Payload)
}
func (b *PendingBlocks) ByID(blockID flow.Identifier) (*flow.PendingBlock, bool) {
item, ok := b.backend.byID(blockID)
if !ok {
return nil, false
}
block := &flow.PendingBlock{
OriginID: item.originID,
Header: item.header,
Payload: item.payload.(*flow.Payload),
}
return block, true
}
func (b *PendingBlocks) ByParentID(parentID flow.Identifier) ([]*flow.PendingBlock, bool) {
items, ok := b.backend.byParentID(parentID)
if !ok {
return nil, false
}
blocks := make([]*flow.PendingBlock, 0, len(items))
for _, item := range items {
block := &flow.PendingBlock{
OriginID: item.originID,
Header: item.header,
Payload: item.payload.(*flow.Payload),
}
blocks = append(blocks, block)
}
return blocks, true
}
func (b *PendingBlocks) DropForParent(parentID flow.Identifier) {
b.backend.dropForParent(parentID)
}
// PruneByView prunes any pending blocks with views less or equal to the given view.
func (b *PendingBlocks) PruneByView(view uint64) {
b.backend.pruneByView(view)
}
func (b *PendingBlocks) Size() uint {
return b.backend.size()
}