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

     1  package stdmap
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/model/chunks"
     7  	"github.com/onflow/flow-go/model/flow"
     8  	"github.com/onflow/flow-go/model/verification"
     9  )
    10  
    11  // ChunkStatuses is an implementation of in-memory storage for maintaining the chunk status data objects.
    12  type ChunkStatuses struct {
    13  	*Backend
    14  }
    15  
    16  func NewChunkStatuses(limit uint) *ChunkStatuses {
    17  	return &ChunkStatuses{
    18  		Backend: NewBackend(WithLimit(limit)),
    19  	}
    20  }
    21  
    22  func chunkStatus(entity flow.Entity) *verification.ChunkStatus {
    23  	status, ok := entity.(inMemChunkStatus)
    24  	if !ok {
    25  		panic(fmt.Sprintf("could not convert the entity into chunk status from the mempool: %v", entity))
    26  	}
    27  	return &verification.ChunkStatus{
    28  		ChunkIndex:      status.ChunkIndex,
    29  		ExecutionResult: status.ExecutionResult,
    30  		BlockHeight:     status.BlockHeight,
    31  	}
    32  }
    33  
    34  // Get returns a chunk status by its chunk index and result ID.
    35  // There is a one-to-one correspondence between the chunk statuses in memory, and
    36  // their pair of chunk index and result id.
    37  func (cs ChunkStatuses) Get(chunkIndex uint64, resultID flow.Identifier) (*verification.ChunkStatus, bool) {
    38  	entity, exists := cs.Backend.ByID(chunks.ChunkLocatorID(resultID, chunkIndex))
    39  	if !exists {
    40  		return nil, false
    41  	}
    42  
    43  	status := chunkStatus(entity)
    44  	return status, true
    45  }
    46  
    47  // Add provides insertion functionality into the memory pool.
    48  // The insertion is only successful if there is no duplicate status with the same
    49  // chunk ID in the memory. Otherwise, it aborts the insertion and returns false.
    50  func (cs *ChunkStatuses) Add(status *verification.ChunkStatus) bool {
    51  	return cs.Backend.Add(inMemChunkStatus{
    52  		ChunkIndex:      status.ChunkIndex,
    53  		ExecutionResult: status.ExecutionResult,
    54  		BlockHeight:     status.BlockHeight,
    55  	})
    56  }
    57  
    58  // Remove provides deletion functionality from the memory pool based on the pair of
    59  // chunk index and result id.
    60  // If there is a chunk status associated with this pair, Remove removes it and returns true.
    61  // Otherwise, it returns false.
    62  func (cs *ChunkStatuses) Remove(chunkIndex uint64, resultID flow.Identifier) bool {
    63  	return cs.Backend.Remove(chunks.ChunkLocatorID(resultID, chunkIndex))
    64  }
    65  
    66  // All returns all chunk statuses stored in this memory pool.
    67  func (cs ChunkStatuses) All() []*verification.ChunkStatus {
    68  	all := cs.Backend.All()
    69  	statuses := make([]*verification.ChunkStatus, 0, len(all))
    70  	for _, entity := range all {
    71  		chunk := chunkStatus(entity)
    72  		statuses = append(statuses, chunk)
    73  	}
    74  	return statuses
    75  }
    76  
    77  // Size returns total number of chunk statuses in the memory pool.
    78  func (cs ChunkStatuses) Size() uint {
    79  	return cs.Backend.Size()
    80  }
    81  
    82  // inMemChunkStatus is an internal type for storing ChunkStatus in the mempool.
    83  //
    84  // It is the same as ChunkStatus, but additionally it implements an Entity type which
    85  // makes it storable in the mempool.
    86  // Note that as an entity, the ID of a inMemChunkStatus is computed as the ID of the chunk locator
    87  // it represents. However, the usage of ID method is only confined to maintaining it on the mempool.
    88  // That is the motivation behind making it an internal type to make sure that no further decision out of
    89  // this package is taken based on ID of inMemChunkStatus.
    90  type inMemChunkStatus struct {
    91  	ChunkIndex      uint64
    92  	BlockHeight     uint64
    93  	ExecutionResult *flow.ExecutionResult
    94  }
    95  
    96  func (s inMemChunkStatus) ID() flow.Identifier {
    97  	return chunks.ChunkLocatorID(s.ExecutionResult.ID(), s.ChunkIndex)
    98  }
    99  
   100  func (s inMemChunkStatus) Checksum() flow.Identifier {
   101  	return chunks.ChunkLocatorID(s.ExecutionResult.ID(), s.ChunkIndex)
   102  }