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

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