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

     1  package scoring
     2  
     3  import "github.com/libp2p/go-libp2p/core/peer"
     4  
     5  // SubscriptionCache implements an in-memory cache that keeps track of the topics a peer is subscribed to.
     6  // The cache is modeled abstracted to be used in update cycles, i.e., every regular interval of time, the cache is updated for
     7  // all peers.
     8  type SubscriptionCache interface {
     9  	// GetSubscribedTopics returns the list of topics a peer is subscribed to.
    10  	// Returns:
    11  	// - []string: the list of topics the peer is subscribed to.
    12  	// - bool: true if there is a record for the peer, false otherwise.
    13  	GetSubscribedTopics(pid peer.ID) ([]string, bool)
    14  
    15  	// MoveToNextUpdateCycle moves the subscription cache to the next update cycle.
    16  	// A new update cycle is started when the subscription cache is first created, and then every time the subscription cache
    17  	// is updated. The update cycle is used to keep track of the last time the subscription cache was updated. It is used to
    18  	// implement a notion of time in the subscription cache.
    19  	// Returns:
    20  	// - uint64: the current update cycle.
    21  	MoveToNextUpdateCycle() uint64
    22  
    23  	// AddTopicForPeer appends a topic to the list of topics a peer is subscribed to. If the peer is not subscribed to any
    24  	// topics yet, a new record is created.
    25  	// If the last update cycle is older than the current cycle, the list of topics for the peer is first cleared, and then
    26  	// the topic is added to the list. This is to ensure that the list of topics for a peer is always up to date.
    27  	// Args:
    28  	// - pid: the peer id of the peer.
    29  	// - topic: the topic to add.
    30  	// Returns:
    31  	// - []string: the list of topics the peer is subscribed to after the update.
    32  	// - error: an error if the update failed; any returned error is an irrecoverable error and indicates a bug or misconfiguration.
    33  	// Implementation must be thread-safe.
    34  	AddWithInitTopicForPeer(pid peer.ID, topic string) ([]string, error)
    35  }