github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/buffer/pending_blocks.go (about)

     1  package buffer
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  	"github.com/onflow/flow-go/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(block flow.Slashable[*flow.Block]) bool {
    20  	return b.backend.add(flow.Slashable[*flow.Header]{
    21  		OriginID: block.OriginID,
    22  		Message:  block.Message.Header,
    23  	}, block.Message.Payload)
    24  }
    25  
    26  func (b *PendingBlocks) ByID(blockID flow.Identifier) (flow.Slashable[*flow.Block], bool) {
    27  	item, ok := b.backend.byID(blockID)
    28  	if !ok {
    29  		return flow.Slashable[*flow.Block]{}, false
    30  	}
    31  
    32  	block := flow.Slashable[*flow.Block]{
    33  		OriginID: item.header.OriginID,
    34  		Message: &flow.Block{
    35  			Header:  item.header.Message,
    36  			Payload: item.payload.(*flow.Payload),
    37  		},
    38  	}
    39  
    40  	return block, true
    41  }
    42  
    43  func (b *PendingBlocks) ByParentID(parentID flow.Identifier) ([]flow.Slashable[*flow.Block], bool) {
    44  	items, ok := b.backend.byParentID(parentID)
    45  	if !ok {
    46  		return nil, false
    47  	}
    48  
    49  	blocks := make([]flow.Slashable[*flow.Block], 0, len(items))
    50  	for _, item := range items {
    51  		block := flow.Slashable[*flow.Block]{
    52  			OriginID: item.header.OriginID,
    53  			Message: &flow.Block{
    54  				Header:  item.header.Message,
    55  				Payload: item.payload.(*flow.Payload),
    56  			},
    57  		}
    58  		blocks = append(blocks, block)
    59  	}
    60  
    61  	return blocks, true
    62  }
    63  
    64  func (b *PendingBlocks) DropForParent(parentID flow.Identifier) {
    65  	b.backend.dropForParent(parentID)
    66  }
    67  
    68  // PruneByView prunes any pending blocks with views less or equal to the given view.
    69  func (b *PendingBlocks) PruneByView(view uint64) {
    70  	b.backend.pruneByView(view)
    71  }
    72  
    73  func (b *PendingBlocks) Size() uint {
    74  	return b.backend.size()
    75  }