-
Notifications
You must be signed in to change notification settings - Fork 178
/
pending_blocks.go
75 lines (61 loc) · 1.73 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
70
71
72
73
74
75
package buffer
import (
"github.com/onflow/flow-go/model/flow"
"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(block flow.Slashable[*flow.Block]) bool {
return b.backend.add(flow.Slashable[*flow.Header]{
OriginID: block.OriginID,
Message: block.Message.Header,
}, block.Message.Payload)
}
func (b *PendingBlocks) ByID(blockID flow.Identifier) (flow.Slashable[*flow.Block], bool) {
item, ok := b.backend.byID(blockID)
if !ok {
return flow.Slashable[*flow.Block]{}, false
}
block := flow.Slashable[*flow.Block]{
OriginID: item.header.OriginID,
Message: &flow.Block{
Header: item.header.Message,
Payload: item.payload.(*flow.Payload),
},
}
return block, true
}
func (b *PendingBlocks) ByParentID(parentID flow.Identifier) ([]flow.Slashable[*flow.Block], bool) {
items, ok := b.backend.byParentID(parentID)
if !ok {
return nil, false
}
blocks := make([]flow.Slashable[*flow.Block], 0, len(items))
for _, item := range items {
block := flow.Slashable[*flow.Block]{
OriginID: item.header.OriginID,
Message: &flow.Block{
Header: item.header.Message,
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()
}