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

     1  package network
     2  
     3  import (
     4  	"github.com/ipfs/go-datastore"
     5  	"github.com/libp2p/go-libp2p/core/protocol"
     6  
     7  	"github.com/onflow/flow-go/model/flow"
     8  	"github.com/onflow/flow-go/module"
     9  	"github.com/onflow/flow-go/module/component"
    10  	"github.com/onflow/flow-go/network/channels"
    11  )
    12  
    13  // NetworkingType is the type of the Flow networking layer. It is used to differentiate between the public (i.e., unstaked)
    14  // and private (i.e., staked) networks.
    15  type NetworkingType uint8
    16  
    17  func (t NetworkingType) String() string {
    18  	switch t {
    19  	case PrivateNetwork:
    20  		return "private"
    21  	case PublicNetwork:
    22  		return "public"
    23  	default:
    24  		return "unknown"
    25  	}
    26  }
    27  
    28  const (
    29  	// PrivateNetwork indicates that the staked private-side of the Flow blockchain that nodes can only join and leave
    30  	// with a staking requirement.
    31  	PrivateNetwork NetworkingType = iota + 1
    32  	// PublicNetwork indicates that the unstaked public-side of the Flow blockchain that nodes can join and leave at will
    33  	// with no staking requirement.
    34  	PublicNetwork
    35  )
    36  
    37  // EngineRegistry is one of the networking layer interfaces in Flow (i.e., EngineRegistry, ConduitAdapter, and Underlay). It represents the interface that networking layer
    38  // offers to the Flow protocol layer, i.e., engines. It is responsible for creating conduits through which engines
    39  // can send and receive messages to and from other engines on the network, as well as registering other services
    40  // such as BlobService and PingService.
    41  type EngineRegistry interface {
    42  	component.Component
    43  	// Register will subscribe to the channel with the given engine and
    44  	// the engine will be notified with incoming messages on the channel.
    45  	// The returned Conduit can be used to send messages to engines on other nodes subscribed to the same channel
    46  	// On a single node, only one engine can be subscribed to a channel at any given time.
    47  	Register(channel channels.Channel, messageProcessor MessageProcessor) (Conduit, error)
    48  
    49  	// RegisterBlobService registers a BlobService on the given channel, using the given datastore to retrieve values.
    50  	// The returned BlobService can be used to request blocks from the network.
    51  	// RegisterBlobService starts the BlobService component using the network's context.
    52  	// TODO: We should return a function that can be called to unregister / close the BlobService
    53  	RegisterBlobService(channel channels.Channel, store datastore.Batching, opts ...BlobServiceOption) (BlobService, error)
    54  
    55  	// RegisterPingService registers a ping protocol handler for the given protocol ID
    56  	RegisterPingService(pingProtocolID protocol.ID, pingInfoProvider PingInfoProvider) (PingService, error)
    57  }
    58  
    59  // ConduitAdapter is one of the networking layer interfaces in Flow (i.e., EngineRegistry, ConduitAdapter, and Underlay). It represents the interface that networking layer
    60  // offers to a single conduit which enables the conduit to send different types of messages i.e., unicast, multicast,
    61  // and publish, to other conduits on the network.
    62  type ConduitAdapter interface {
    63  	MisbehaviorReportConsumer
    64  	// UnicastOnChannel sends the message in a reliable way to the given recipient.
    65  	UnicastOnChannel(channels.Channel, interface{}, flow.Identifier) error
    66  
    67  	// PublishOnChannel sends the message in an unreliable way to all the given recipients.
    68  	PublishOnChannel(channels.Channel, interface{}, ...flow.Identifier) error
    69  
    70  	// MulticastOnChannel unreliably sends the specified event over the channel to randomly selected number of recipients
    71  	// selected from the specified targetIDs.
    72  	MulticastOnChannel(channels.Channel, interface{}, uint, ...flow.Identifier) error
    73  
    74  	// UnRegisterChannel unregisters the engine for the specified channel. The engine will no longer be able to send or
    75  	// receive messages from that channel.
    76  	UnRegisterChannel(channel channels.Channel) error
    77  }
    78  
    79  // Underlay is one of the networking layer interfaces in Flow (i.e., EngineRegistry, ConduitAdapter, and Underlay). It represents the interface that networking layer
    80  // offers to lower level networking components such as libp2p. It is responsible for subscribing to and unsubscribing
    81  // from channels, as well as updating the addresses of all the authorized participants in the Flow protocol.
    82  type Underlay interface {
    83  	module.ReadyDoneAware
    84  	DisallowListNotificationConsumer
    85  
    86  	// Subscribe subscribes the network Underlay to a channel.
    87  	// No errors are expected during normal operation.
    88  	Subscribe(channel channels.Channel) error
    89  
    90  	// Unsubscribe unsubscribes the network Underlay from a channel.
    91  	// All errors returned from this function can be considered benign.
    92  	Unsubscribe(channel channels.Channel) error
    93  
    94  	// UpdateNodeAddresses fetches and updates the addresses of all the authorized participants
    95  	// in the Flow protocol.
    96  	UpdateNodeAddresses()
    97  }
    98  
    99  // Connection represents an interface to read from & write to a connection.
   100  type Connection interface {
   101  	Send(msg interface{}) error
   102  	Receive() (interface{}, error)
   103  }
   104  
   105  // MisbehaviorReportConsumer set of funcs used to handle MisbehaviorReport disseminated from misbehavior reporters.
   106  type MisbehaviorReportConsumer interface {
   107  	// ReportMisbehaviorOnChannel reports the misbehavior of a node on sending a message to the current node that appears
   108  	// valid based on the networking layer but is considered invalid by the current node based on the Flow protocol.
   109  	// The misbehavior report is sent to the current node's networking layer on the given channel to be processed.
   110  	// Args:
   111  	// - channel: The channel on which the misbehavior report is sent.
   112  	// - report: The misbehavior report to be sent.
   113  	// Returns:
   114  	// none
   115  	ReportMisbehaviorOnChannel(channel channels.Channel, report MisbehaviorReport)
   116  }