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

     1  package queue
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/onflow/flow-go/engine"
     7  	"github.com/onflow/flow-go/model/flow"
     8  )
     9  
    10  // MessageEntity is a data structure for storing messages in HeroQueue.
    11  type MessageEntity struct {
    12  	Msg engine.Message
    13  	id  flow.Identifier
    14  }
    15  
    16  var _ flow.Entity = (*MessageEntity)(nil)
    17  
    18  // NewMessageEntity returns a new message entity.
    19  func NewMessageEntity(msg *engine.Message) MessageEntity {
    20  	id := identifierOfMessage(msg)
    21  	return MessageEntity{
    22  		Msg: *msg,
    23  		id:  id,
    24  	}
    25  }
    26  
    27  // NewMessageEntityWithNonce creates a new message entity adding a nonce to the id calculation.
    28  // This prevents unexpected de-duplication of otherwise identical messages stored in the queue.
    29  func NewMessageEntityWithNonce(msg *engine.Message) MessageEntity {
    30  	id := identifierOfMessage(struct {
    31  		*engine.Message
    32  		Nonce uint64
    33  	}{
    34  		msg,
    35  		uint64(time.Now().UnixNano()),
    36  	})
    37  	return MessageEntity{
    38  		Msg: *msg,
    39  		id:  id,
    40  	}
    41  }
    42  
    43  func (m MessageEntity) ID() flow.Identifier {
    44  	return m.id
    45  }
    46  
    47  func (m MessageEntity) Checksum() flow.Identifier {
    48  	return m.id
    49  }
    50  
    51  func identifierOfMessage(msg interface{}) flow.Identifier {
    52  	return flow.MakeID(msg)
    53  }