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

     1  package internal
     2  
     3  import (
     4  	"github.com/rs/zerolog"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  	"github.com/onflow/flow-go/module"
     8  	herocache "github.com/onflow/flow-go/module/mempool/herocache/backdata"
     9  	"github.com/onflow/flow-go/module/mempool/herocache/backdata/heropool"
    10  	"github.com/onflow/flow-go/module/mempool/stdmap"
    11  	p2pmsg "github.com/onflow/flow-go/network/p2p/message"
    12  )
    13  
    14  // rpcCtrlMsgSentCacheConfig configuration for the rpc sent cache.
    15  type rpcCtrlMsgSentCacheConfig struct {
    16  	logger    zerolog.Logger
    17  	sizeLimit uint32
    18  	collector module.HeroCacheMetrics
    19  }
    20  
    21  // rpcSentCache cache that stores rpcSentEntity. These entity's represent RPC control messages sent from the local node.
    22  type rpcSentCache struct {
    23  	// c is the underlying cache.
    24  	c *stdmap.Backend
    25  }
    26  
    27  // newRPCSentCache creates a new *rpcSentCache.
    28  // Args:
    29  // - config: record cache config.
    30  // Returns:
    31  // - *rpcSentCache: the created cache.
    32  // Note that this cache is intended to track control messages sent by the local node,
    33  // it stores a RPCSendEntity using an Id which should uniquely identifies the message being tracked.
    34  func newRPCSentCache(config *rpcCtrlMsgSentCacheConfig) *rpcSentCache {
    35  	backData := herocache.NewCache(config.sizeLimit,
    36  		herocache.DefaultOversizeFactor,
    37  		heropool.LRUEjection,
    38  		config.logger.With().Str("mempool", "gossipsub-rpc-control-messages-sent").Logger(),
    39  		config.collector)
    40  	return &rpcSentCache{
    41  		c: stdmap.NewBackend(stdmap.WithBackData(backData)),
    42  	}
    43  }
    44  
    45  // add initializes the record cached for the given messageEntityID if it does not exist.
    46  // Returns true if the record is initialized, false otherwise (i.e.: the record already exists).
    47  // Args:
    48  // - messageId: the message ID.
    49  // - controlMsgType: the rpc control message type.
    50  // Returns:
    51  // - bool: true if the record is initialized, false otherwise (i.e.: the record already exists).
    52  // Note that if add is called multiple times for the same messageEntityID, the record is initialized only once, and the
    53  // subsequent calls return false and do not change the record (i.e.: the record is not re-initialized).
    54  func (r *rpcSentCache) add(messageId string, controlMsgType p2pmsg.ControlMessageType) bool {
    55  	return r.c.Add(newRPCSentEntity(r.rpcSentEntityID(messageId, controlMsgType), controlMsgType))
    56  }
    57  
    58  // has checks if the RPC message has been cached indicating it has been sent.
    59  // Args:
    60  // - messageId: the message ID.
    61  // - controlMsgType: the rpc control message type.
    62  // Returns:
    63  // - bool: true if the RPC has been cache indicating it was sent from the local node.
    64  func (r *rpcSentCache) has(messageId string, controlMsgType p2pmsg.ControlMessageType) bool {
    65  	return r.c.Has(r.rpcSentEntityID(messageId, controlMsgType))
    66  }
    67  
    68  // size returns the number of records in the cache.
    69  func (r *rpcSentCache) size() uint {
    70  	return r.c.Size()
    71  }
    72  
    73  // rpcSentEntityID creates an entity ID from the messageID and control message type.
    74  // Args:
    75  // - messageId: the message ID.
    76  // - controlMsgType: the rpc control message type.
    77  // Returns:
    78  // - flow.Identifier: the entity ID.
    79  func (r *rpcSentCache) rpcSentEntityID(messageId string, controlMsgType p2pmsg.ControlMessageType) flow.Identifier {
    80  	return flow.MakeIDFromFingerPrint([]byte(messageId + string(controlMsgType)))
    81  }