github.com/true-sqn/fabric@v2.1.1+incompatible/discovery/support/gossip/support.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package gossip
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/gossip"
    11  	"github.com/hyperledger/fabric/gossip/api"
    12  	"github.com/hyperledger/fabric/gossip/common"
    13  	"github.com/hyperledger/fabric/gossip/discovery"
    14  	"github.com/hyperledger/fabric/gossip/protoext"
    15  )
    16  
    17  //go:generate counterfeiter -o mocks/gossip.go -fake-name Gossip . Gossip
    18  
    19  type Gossip interface {
    20  	// IdentityInfo returns identity information about peers
    21  	IdentityInfo() api.PeerIdentitySet
    22  	// GetPeers returns the NetworkMembers considered alive
    23  	Peers() []discovery.NetworkMember
    24  	// PeersOfChannel returns the NetworkMembers considered alive
    25  	// and also subscribed to the channel given
    26  	PeersOfChannel(common.ChannelID) []discovery.NetworkMember
    27  	// SelfChannelInfo returns the peer's latest StateInfo message of a given channel
    28  	SelfChannelInfo(common.ChannelID) *protoext.SignedGossipMessage
    29  	// SelfMembershipInfo returns the peer's membership information
    30  	SelfMembershipInfo() discovery.NetworkMember
    31  }
    32  
    33  // DiscoverySupport implements support that is used for service discovery
    34  // that is obtained from gossip
    35  type DiscoverySupport struct {
    36  	Gossip
    37  }
    38  
    39  // NewDiscoverySupport creates a new DiscoverySupport
    40  func NewDiscoverySupport(g Gossip) *DiscoverySupport {
    41  	return &DiscoverySupport{g}
    42  }
    43  
    44  // ChannelExists returns whether a given channel exists or not
    45  func (s *DiscoverySupport) ChannelExists(channel string) bool {
    46  	return s.SelfChannelInfo(common.ChannelID(channel)) != nil
    47  }
    48  
    49  // PeersOfChannel returns the NetworkMembers considered alive
    50  // and also subscribed to the channel given
    51  func (s *DiscoverySupport) PeersOfChannel(chain common.ChannelID) discovery.Members {
    52  	msg := s.SelfChannelInfo(chain)
    53  	if msg == nil {
    54  		return nil
    55  	}
    56  	stateInf := msg.GetStateInfo()
    57  	selfMember := discovery.NetworkMember{
    58  		Properties: stateInf.Properties,
    59  		PKIid:      stateInf.PkiId,
    60  		Envelope:   msg.Envelope,
    61  	}
    62  	return append(s.Gossip.PeersOfChannel(chain), selfMember)
    63  }
    64  
    65  // Peers returns the NetworkMembers considered alive
    66  func (s *DiscoverySupport) Peers() discovery.Members {
    67  	peers := s.Gossip.Peers()
    68  	peers = append(peers, s.Gossip.SelfMembershipInfo())
    69  	// Return only the peers that have an external endpoint, and sanitizes the envelopes.
    70  	return discovery.Members(peers).Filter(discovery.HasExternalEndpoint).Map(sanitizeEnvelope)
    71  }
    72  
    73  func sanitizeEnvelope(member discovery.NetworkMember) discovery.NetworkMember {
    74  	// Make a local copy of the member
    75  	returnedMember := member
    76  	if returnedMember.Envelope == nil {
    77  		return returnedMember
    78  	}
    79  	returnedMember.Envelope = &gossip.Envelope{
    80  		Payload:   member.Envelope.Payload,
    81  		Signature: member.Envelope.Signature,
    82  	}
    83  	return returnedMember
    84  }