github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/chunks/chunkLocator.go (about)

     1  package chunks
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  )
     6  
     7  // Locator is used to locate a chunk by providing the execution result the chunk belongs to as well as the chunk index within that execution result.
     8  // Since a chunk is unique by the result ID and its index in the result's chunk list.
     9  type Locator struct {
    10  	ResultID flow.Identifier // execution result id that chunk belongs to
    11  	Index    uint64          // index of chunk in the execution result
    12  }
    13  
    14  // ID returns a unique id for chunk locator.
    15  func (c Locator) ID() flow.Identifier {
    16  	return flow.MakeID(c)
    17  }
    18  
    19  // Checksum provides a cryptographic commitment for a chunk locator content.
    20  func (c Locator) Checksum() flow.Identifier {
    21  	return flow.MakeID(c)
    22  }
    23  
    24  // ChunkLocatorID is a util function that returns identifier of corresponding chunk locator to
    25  // the specified result and chunk index.
    26  func ChunkLocatorID(resultID flow.Identifier, chunkIndex uint64) flow.Identifier {
    27  	return Locator{
    28  		ResultID: resultID,
    29  		Index:    chunkIndex,
    30  	}.ID()
    31  }
    32  
    33  // LocatorMap maps keeps chunk locators based on their locator id.
    34  type LocatorMap map[flow.Identifier]*Locator
    35  
    36  func (l LocatorMap) ToList() LocatorList {
    37  	locatorList := LocatorList{}
    38  	for _, locator := range l {
    39  		locatorList = append(locatorList, locator)
    40  	}
    41  
    42  	return locatorList
    43  }
    44  
    45  type LocatorList []*Locator
    46  
    47  func (l LocatorList) ToMap() LocatorMap {
    48  	locatorMap := make(LocatorMap)
    49  	for _, locator := range l {
    50  		locatorMap[locator.ID()] = locator
    51  	}
    52  	return locatorMap
    53  }