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

     1  package internal
     2  
     3  import "github.com/onflow/flow-go/model/flow"
     4  
     5  // WrappedEntity is a wrapper around a flow.Entity that allows overriding the ID.
     6  // The has 2 main use cases:
     7  //   - when the ID is expensive to compute, we can pre-compute it and use it for the cache
     8  //   - when caching an entity using a different ID than what's returned by ID(). For example, if there
     9  //     is a 1:1 mapping between a block and an entity, we can use the block ID as the cache key.
    10  type WrappedEntity struct {
    11  	flow.Entity
    12  	id flow.Identifier
    13  }
    14  
    15  var _ flow.Entity = (*WrappedEntity)(nil)
    16  
    17  // NewWrappedEntity creates a new WrappedEntity
    18  func NewWrappedEntity(id flow.Identifier, entity flow.Entity) *WrappedEntity {
    19  	return &WrappedEntity{
    20  		Entity: entity,
    21  		id:     id,
    22  	}
    23  }
    24  
    25  // ID returns the cached ID of the wrapped entity
    26  func (w WrappedEntity) ID() flow.Identifier {
    27  	return w.id
    28  }
    29  
    30  // Checksum returns th cached ID of the wrapped entity
    31  func (w WrappedEntity) Checksum() flow.Identifier {
    32  	return w.id
    33  }