-
Notifications
You must be signed in to change notification settings - Fork 178
/
pending_blocks.go
65 lines (52 loc) · 1.42 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
package buffer
import (
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
)
type PendingBlocks struct {
backend *backend
}
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)
}
func (b *PendingBlocks) PruneByHeight(height uint64) {
b.backend.pruneByHeight(height)
}
func (b *PendingBlocks) Size() uint {
return uint(len(b.backend.blocksByID))
}