github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/tracer/internal/duplicate_msgs_counter_entity.go (about)

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  )
    11  
    12  // duplicateMessagesCounterEntity cache record that keeps track of the amount of duplicate messages received from a peer.
    13  type duplicateMessagesCounterEntity struct {
    14  	// ID the entity ID.
    15  	Id flow.Identifier
    16  	// Value the number of duplicate messages.
    17  	Value       float64
    18  	lastUpdated time.Time
    19  }
    20  
    21  func newDuplicateMessagesCounter(id flow.Identifier) duplicateMessagesCounterEntity {
    22  	return duplicateMessagesCounterEntity{
    23  		Id:          id,
    24  		Value:       0.0,
    25  		lastUpdated: time.Now(),
    26  	}
    27  }
    28  
    29  var _ flow.Entity = (*duplicateMessagesCounterEntity)(nil)
    30  
    31  func (d duplicateMessagesCounterEntity) ID() flow.Identifier {
    32  	return d.Id
    33  }
    34  
    35  func (d duplicateMessagesCounterEntity) Checksum() flow.Identifier {
    36  	return d.Id
    37  }
    38  
    39  // mustBeDuplicateMessagesCounterEntity is a helper function for type assertion of the flow.Entity to duplicateMessagesCounterEntity.
    40  // It panics if the type assertion fails.
    41  // Args:
    42  // - entity: the flow.Entity to be type asserted.
    43  // Returns:
    44  // - the duplicateMessagesCounterEntity entity.
    45  func mustBeDuplicateMessagesCounterEntity(entity flow.Entity) duplicateMessagesCounterEntity {
    46  	c, ok := entity.(duplicateMessagesCounterEntity)
    47  	if !ok {
    48  		// sanity check
    49  		// This should never happen, because the cache only contains duplicateMessagesCounterEntity entities.
    50  		panic(fmt.Sprintf("invalid entity type, expected duplicateMessagesCounterEntity type, got: %T", entity))
    51  	}
    52  	return c
    53  }
    54  
    55  // makeId is a helper function for creating the id field of the duplicateMessagesCounterEntity by hashing the peerID.
    56  // Returns:
    57  // - the hash of the peerID as a flow.Identifier.
    58  func makeId(peerID peer.ID) flow.Identifier {
    59  	return flow.MakeID([]byte(peerID))
    60  }