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

     1  package stdmap
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  	"github.com/onflow/flow-go/module/mempool"
     6  	"github.com/onflow/flow-go/module/mempool/entity"
     7  	_ "github.com/onflow/flow-go/utils/binstat"
     8  )
     9  
    10  // Hold all the missing collections.
    11  // Each entry is a missing collection, and all the blocks that contain
    12  // this collection
    13  type BlockByCollections struct {
    14  	*Backend
    15  }
    16  
    17  // BlockByCollectionBackdata contains all the collections is being requested,
    18  // for each collection it stores the blocks that contains the collection.
    19  // the Backdata is essentially map<collectionID>map<blockID>*ExecutableBlock
    20  type BlockByCollectionBackdata struct {
    21  	mempool.BackData
    22  }
    23  
    24  func NewBlockByCollections() *BlockByCollections {
    25  	return &BlockByCollections{NewBackend(WithEject(EjectPanic))}
    26  }
    27  
    28  func (b *BlockByCollections) Add(block *entity.BlocksByCollection) bool {
    29  	return b.Backend.Add(block)
    30  }
    31  
    32  func (b *BlockByCollections) Get(collID flow.Identifier) (*entity.BlocksByCollection, bool) {
    33  	backdata := &BlockByCollectionBackdata{b.backData}
    34  	return backdata.ByID(collID)
    35  }
    36  
    37  func (b *BlockByCollections) Run(f func(backdata *BlockByCollectionBackdata) error) error {
    38  	//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(BlockByCollections).Run")
    39  	b.Lock()
    40  	//binstat.Leave(bs1)
    41  
    42  	//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(BlockByCollections).Run)")
    43  	defer b.Unlock()
    44  	err := f(&BlockByCollectionBackdata{b.backData})
    45  	//binstat.Leave(bs2)
    46  
    47  	if err != nil {
    48  		return err
    49  	}
    50  	return nil
    51  }
    52  
    53  func (b *BlockByCollectionBackdata) ByID(id flow.Identifier) (*entity.BlocksByCollection, bool) {
    54  	e, exists := b.BackData.ByID(id)
    55  	if !exists {
    56  		return nil, false
    57  	}
    58  	block := e.(*entity.BlocksByCollection)
    59  	return block, true
    60  }