github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/discovery/discovery.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package discovery
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/hyperledger/fabric/gossip/common"
    13  	proto "github.com/hyperledger/fabric/protos/gossip"
    14  )
    15  
    16  // CryptoService is an interface that the discovery expects to be implemented and passed on creation
    17  type CryptoService interface {
    18  	// ValidateAliveMsg validates that an Alive message is authentic
    19  	ValidateAliveMsg(message *proto.SignedGossipMessage) bool
    20  
    21  	// SignMessage signs a message
    22  	SignMessage(m *proto.GossipMessage, internalEndpoint string) *proto.Envelope
    23  }
    24  
    25  // EnvelopeFilter may or may not remove part of the Envelope
    26  // that the given SignedGossipMessage originates from.
    27  type EnvelopeFilter func(message *proto.SignedGossipMessage) *proto.Envelope
    28  
    29  // Sieve defines the messages that are allowed to be sent to some remote peer,
    30  // based on some criteria.
    31  // Returns whether the sieve permits sending a given message.
    32  type Sieve func(message *proto.SignedGossipMessage) bool
    33  
    34  // DisclosurePolicy defines which messages a given remote peer
    35  // is eligible of knowing about, and also what is it eligible
    36  // to know about out of a given SignedGossipMessage.
    37  // Returns:
    38  // 1) A Sieve for a given remote peer.
    39  //    The Sieve is applied for each peer in question and outputs
    40  //    whether the message should be disclosed to the remote peer.
    41  // 2) A EnvelopeFilter for a given SignedGossipMessage, which may remove
    42  //    part of the Envelope the SignedGossipMessage originates from
    43  type DisclosurePolicy func(remotePeer *NetworkMember) (Sieve, EnvelopeFilter)
    44  
    45  // CommService is an interface that the discovery expects to be implemented and passed on creation
    46  type CommService interface {
    47  	// Gossip gossips a message
    48  	Gossip(msg *proto.SignedGossipMessage)
    49  
    50  	// SendToPeer sends to a given peer a message.
    51  	// The nonce can be anything since the communication module handles the nonce itself
    52  	SendToPeer(peer *NetworkMember, msg *proto.SignedGossipMessage)
    53  
    54  	// Ping probes a remote peer and returns if it's responsive or not
    55  	Ping(peer *NetworkMember) bool
    56  
    57  	// Accept returns a read-only channel for membership messages sent from remote peers
    58  	Accept() <-chan *proto.SignedGossipMessage
    59  
    60  	// PresumedDead returns a read-only channel for peers that are presumed to be dead
    61  	PresumedDead() <-chan common.PKIidType
    62  
    63  	// CloseConn orders to close the connection with a certain peer
    64  	CloseConn(peer *NetworkMember)
    65  }
    66  
    67  // NetworkMember is a peer's representation
    68  type NetworkMember struct {
    69  	Endpoint         string
    70  	Metadata         []byte
    71  	PKIid            common.PKIidType
    72  	InternalEndpoint string
    73  }
    74  
    75  // String returns a string representation of the NetworkMember
    76  func (n *NetworkMember) String() string {
    77  	return fmt.Sprintf("Endpoint: %s, InternalEndpoint: %s, PKI-ID: %v, Metadata: %v", n.Endpoint, n.InternalEndpoint, n.PKIid, n.Metadata)
    78  }
    79  
    80  // PreferredEndpoint computes the endpoint to connect to,
    81  // while preferring internal endpoint over the standard
    82  // endpoint
    83  func (n NetworkMember) PreferredEndpoint() string {
    84  	if n.InternalEndpoint != "" {
    85  		return n.InternalEndpoint
    86  	}
    87  	return n.Endpoint
    88  }
    89  
    90  // PeerIdentification encompasses a remote peer's
    91  // PKI-ID and whether its in the same org as the current
    92  // peer or not
    93  type PeerIdentification struct {
    94  	ID      common.PKIidType
    95  	SelfOrg bool
    96  }
    97  
    98  type identifier func() (*PeerIdentification, error)
    99  
   100  // Discovery is the interface that represents a discovery module
   101  type Discovery interface {
   102  
   103  	// Lookup returns a network member, or nil if not found
   104  	Lookup(PKIID common.PKIidType) *NetworkMember
   105  
   106  	// Self returns this instance's membership information
   107  	Self() NetworkMember
   108  
   109  	// UpdateMetadata updates this instance's metadata
   110  	UpdateMetadata([]byte)
   111  
   112  	// UpdateEndpoint updates this instance's endpoint
   113  	UpdateEndpoint(string)
   114  
   115  	// Stops this instance
   116  	Stop()
   117  
   118  	// GetMembership returns the alive members in the view
   119  	GetMembership() []NetworkMember
   120  
   121  	// InitiateSync makes the instance ask a given number of peers
   122  	// for their membership information
   123  	InitiateSync(peerNum int)
   124  
   125  	// Connect makes this instance to connect to a remote instance
   126  	// The identifier param is a function that can be used to identify
   127  	// the peer, and to assert its PKI-ID, whether its in the peer's org or not,
   128  	// and whether the action was successful or not
   129  	Connect(member NetworkMember, id identifier)
   130  }