github.com/koko1123/flow-go-1@v0.29.6/module/buffer/pending_blocks.go (about) 1 package buffer 2 3 import ( 4 "github.com/koko1123/flow-go-1/model/flow" 5 "github.com/koko1123/flow-go-1/module" 6 ) 7 8 type PendingBlocks struct { 9 backend *backend 10 } 11 12 var _ module.PendingBlockBuffer = (*PendingBlocks)(nil) 13 14 func NewPendingBlocks() *PendingBlocks { 15 b := &PendingBlocks{backend: newBackend()} 16 return b 17 } 18 19 func (b *PendingBlocks) Add(originID flow.Identifier, block *flow.Block) bool { 20 return b.backend.add(originID, block.Header, block.Payload) 21 } 22 23 func (b *PendingBlocks) ByID(blockID flow.Identifier) (flow.Slashable[flow.Block], bool) { 24 item, ok := b.backend.byID(blockID) 25 if !ok { 26 return flow.Slashable[flow.Block]{}, false 27 } 28 29 block := flow.Slashable[flow.Block]{ 30 OriginID: item.originID, 31 Message: &flow.Block{ 32 Header: item.header, 33 Payload: item.payload.(*flow.Payload), 34 }, 35 } 36 37 return block, true 38 } 39 40 func (b *PendingBlocks) ByParentID(parentID flow.Identifier) ([]flow.Slashable[flow.Block], bool) { 41 items, ok := b.backend.byParentID(parentID) 42 if !ok { 43 return nil, false 44 } 45 46 blocks := make([]flow.Slashable[flow.Block], 0, len(items)) 47 for _, item := range items { 48 block := flow.Slashable[flow.Block]{ 49 OriginID: item.originID, 50 Message: &flow.Block{ 51 Header: item.header, 52 Payload: item.payload.(*flow.Payload), 53 }, 54 } 55 blocks = append(blocks, block) 56 } 57 58 return blocks, true 59 } 60 61 func (b *PendingBlocks) DropForParent(parentID flow.Identifier) { 62 b.backend.dropForParent(parentID) 63 } 64 65 // PruneByView prunes any pending blocks with views less or equal to the given view. 66 func (b *PendingBlocks) PruneByView(view uint64) { 67 b.backend.pruneByView(view) 68 } 69 70 func (b *PendingBlocks) Size() uint { 71 return b.backend.size() 72 }