github.com/koko1123/flow-go-1@v0.29.6/network/p2p/scoring/subscription_validator.go (about)

     1  package scoring
     2  
     3  import (
     4  	"github.com/libp2p/go-libp2p/core/peer"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	"github.com/koko1123/flow-go-1/network/p2p"
     8  	p2putils "github.com/koko1123/flow-go-1/network/p2p/utils"
     9  )
    10  
    11  type SubscriptionValidator struct {
    12  	subscriptionProvider p2p.SubscriptionProvider
    13  }
    14  
    15  func NewSubscriptionValidator() *SubscriptionValidator {
    16  	return &SubscriptionValidator{}
    17  }
    18  
    19  func (v *SubscriptionValidator) RegisterSubscriptionProvider(provider p2p.SubscriptionProvider) {
    20  	v.subscriptionProvider = provider
    21  }
    22  
    23  // CheckSubscribedToAllowedTopics validates all subscriptions a peer has with respect to all Flow topics.
    24  // All errors returned by this method are benign:
    25  // - InvalidSubscriptionError: the peer is subscribed to a topic that is not allowed for its role.
    26  func (v *SubscriptionValidator) CheckSubscribedToAllowedTopics(pid peer.ID, role flow.Role) error {
    27  	topics := v.subscriptionProvider.GetSubscribedTopics(pid)
    28  
    29  	for _, topic := range topics {
    30  		if !p2putils.AllowedSubscription(role, topic) {
    31  			return NewInvalidSubscriptionError(topic)
    32  		}
    33  	}
    34  
    35  	return nil
    36  }