network

package
v0.0.0-...-3672da2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2022 License: Apache-2.0 Imports: 46 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDialRatio = 0.2

	DefaultLibp2pPort int = 1478

	MinimumBootNodes       int   = 1
	MinimumPeerConnections int64 = 1
)
View Source
const (
	DefaultLeaveTimeout = 30 * time.Second
)

Variables

View Source
var (
	ErrNoBootnodes  = errors.New("no bootnodes specified")
	ErrMinBootnodes = errors.New("minimum 1 bootnode is required")
)
View Source
var (
	// Anything below 35s is prone to false timeouts, as seen from empirical test data
	DefaultJoinTimeout   = 100 * time.Second
	DefaultBufferTimeout = DefaultJoinTimeout + time.Second*5
)

Functions

func DisconnectAndWait

func DisconnectAndWait(
	source *Server,
	target peer.ID,
	leaveTimeout time.Duration,
) error

func GenerateAndEncodeLibp2pKey

func GenerateAndEncodeLibp2pKey() (crypto.PrivKey, []byte, error)

GenerateAndEncodeLibp2pKey generates a new networking private key, and encodes it into hex

func GenerateTestLibp2pKey

func GenerateTestLibp2pKey(t *testing.T) (crypto.PrivKey, string)

func JoinAndWait

func JoinAndWait(
	source,
	destination *Server,
	connectTimeout time.Duration,
	joinTimeout time.Duration,
) error

JoinAndWait is a helper method for joining a destination server and waiting for the connection to be successful (destination node is a peer of source)

func JoinAndWaitMultiple

func JoinAndWaitMultiple(
	timeout time.Duration,
	servers ...*Server,
) error

JoinAndWait is a helper method to make multiple servers connect to corresponding peer

func MeshJoin

func MeshJoin(servers ...*Server) []error

MeshJoin is a helper method for joining all the passed in servers into a mesh

func ParseLibp2pKey

func ParseLibp2pKey(key []byte) (crypto.PrivKey, error)

ParseLibp2pKey converts a byte array to a private key

func ReadLibp2pKey

func ReadLibp2pKey(manager secrets.SecretsManager) (crypto.PrivKey, error)

ReadLibp2pKey reads the private networking key from the secrets manager

func WaitUntilPeerConnectsTo

func WaitUntilPeerConnectsTo(ctx context.Context, srv *Server, ids ...peer.ID) (bool, error)

func WaitUntilPeerDisconnectsFrom

func WaitUntilPeerDisconnectsFrom(ctx context.Context, srv *Server, ids ...peer.ID) (bool, error)

func WaitUntilRoutingTableToBeFilled

func WaitUntilRoutingTableToBeFilled(ctx context.Context, srv *Server, size int) (bool, error)

WaitUntilRoutingTableToBeAdded check routing table has given ids and retry by timeout

Types

type Config

type Config struct {
	NoDiscover       bool                   // flag indicating if the discovery mechanism should be turned on
	Addr             *net.TCPAddr           // the base address
	NatAddr          net.IP                 // the NAT address
	DNS              multiaddr.Multiaddr    // the DNS address
	DataDir          string                 // the base data directory for the client
	MaxPeers         int64                  // the maximum number of peer connections
	MaxInboundPeers  int64                  // the maximum number of inbound peer connections
	MaxOutboundPeers int64                  // the maximum number of outbound peer connections
	Chain            *chain.Chain           // the reference to the chain configuration
	SecretsManager   secrets.SecretsManager // the secrets manager used for key storage
	Metrics          *Metrics               // the metrics reporting reference
}

Config details the params for the base networking server

func DefaultConfig

func DefaultConfig() *Config

type ConnectionInfo

type ConnectionInfo struct {
	// contains filtered or unexported fields
}

ConnectionInfo keeps track of current connection information for the networking server

func NewBlankConnectionInfo

func NewBlankConnectionInfo(
	maxInboundConnCount int64,
	maxOutboundConnCount int64,
) *ConnectionInfo

NewBlankConnectionInfo returns a cleared ConnectionInfo instance

func (*ConnectionInfo) GetInboundConnCount

func (ci *ConnectionInfo) GetInboundConnCount() int64

GetInboundConnCount returns the number of active inbound connections [Thread safe]

func (*ConnectionInfo) GetOutboundConnCount

func (ci *ConnectionInfo) GetOutboundConnCount() int64

GetOutboundConnCount returns the number of active outbound connections [Thread safe]

func (*ConnectionInfo) GetPendingInboundConnCount

func (ci *ConnectionInfo) GetPendingInboundConnCount() int64

GetPendingInboundConnCount returns the number of pending inbound connections [Thread safe]

func (*ConnectionInfo) GetPendingOutboundConnCount

func (ci *ConnectionInfo) GetPendingOutboundConnCount() int64

GetPendingOutboundConnCount returns the number of pending outbound connections [Thread safe]

func (*ConnectionInfo) HasFreeConnectionSlot

func (ci *ConnectionInfo) HasFreeConnectionSlot(direction network.Direction) bool

HasFreeConnectionSlot checks if there is a free connection slot in the specified direction [Thread safe]

func (*ConnectionInfo) HasFreeInboundConn

func (ci *ConnectionInfo) HasFreeInboundConn() bool

HasFreeInboundConn checks if there are any open inbound connection slots. It takes into account the number of current (active) inbound connections and the number of pending inbound connections [Thread safe]

func (*ConnectionInfo) HasFreeOutboundConn

func (ci *ConnectionInfo) HasFreeOutboundConn() bool

HasFreeOutboundConn checks if there are any open outbound connection slots. It takes into account the number of current (active) outbound connections and the number of pending outbound connections [Thread safe]

func (*ConnectionInfo) UpdateConnCountByDirection

func (ci *ConnectionInfo) UpdateConnCountByDirection(
	delta int64,
	direction network.Direction,
)

UpdateConnCountByDirection updates the connection count by delta in the specified direction [Thread safe]

func (*ConnectionInfo) UpdatePendingConnCountByDirection

func (ci *ConnectionInfo) UpdatePendingConnCountByDirection(
	delta int64,
	direction network.Direction,
)

UpdatePendingConnCountByDirection updates the pending connection count by delta in the specified direction [Thread safe]

type CreateServerParams

type CreateServerParams struct {
	ConfigCallback func(c *Config)      // Additional logic that needs to be executed on the configuration
	ServerCallback func(server *Server) // Additional logic that needs to be executed on the server before starting
	Logger         hclog.Logger
}

type Metrics

type Metrics struct {
	// Number of connected peers
	TotalPeerCount metrics.Gauge

	// Number of outbound connections
	OutboundConnectionsCount metrics.Gauge

	// Number of inbound connections
	InboundConnectionsCount metrics.Gauge

	// Number of pending outbound connections
	PendingOutboundConnectionsCount metrics.Gauge

	// Number of pending inbound connections
	PendingInboundConnectionsCount metrics.Gauge
}

Metrics represents the network metrics

func GetPrometheusMetrics

func GetPrometheusMetrics(namespace string, labelsWithValues ...string) *Metrics

GetPrometheusMetrics return the network metrics instance

func NilMetrics

func NilMetrics() *Metrics

NilMetrics will return the non-operational metrics

type PeerConnInfo

type PeerConnInfo struct {
	Info peer.AddrInfo
	// contains filtered or unexported fields
}

PeerConnInfo holds the connection information about the peer

type Protocol

type Protocol interface {
	Client(network.Stream) *rawGrpc.ClientConn
	Handler() func(network.Stream)
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func CreateServer

func CreateServer(params *CreateServerParams) (*Server, error)

func NewServer

func NewServer(logger hclog.Logger, config *Config) (*Server, error)

NewServer returns a new instance of the networking server

func (*Server) AddPeer

func (s *Server) AddPeer(id peer.ID, direction network.Direction)

AddPeer adds a new peer to the networking server's peer list, and updates relevant counters and metrics

func (*Server) AddToPeerStore

func (s *Server) AddToPeerStore(peerInfo *peer.AddrInfo)

AddToPeerStore adds peer information to the node's peer store

func (*Server) AddrInfo

func (s *Server) AddrInfo() *peer.AddrInfo

func (*Server) Close

func (s *Server) Close() error

func (*Server) CloseProtocolStream

func (s *Server) CloseProtocolStream(protocol string, peerID peer.ID) error

CloseProtocolStream closes a protocol stream to the specified peer

func (*Server) DisconnectFromPeer

func (s *Server) DisconnectFromPeer(peer peer.ID, reason string)

DisconnectFromPeer disconnects the networking server from the specified peer

func (*Server) EmitEvent

func (s *Server) EmitEvent(event *peerEvent.PeerEvent)

EmitEvent emits a specified event to the networking server's event bus

func (*Server) FetchOrSetTemporaryDial

func (s *Server) FetchOrSetTemporaryDial(peerID peer.ID, newValue bool) bool

FetchOrSetTemporaryDial loads the temporary status of a peer connection, and sets a new value [Thread safe]

func (*Server) GetBootnodeConnCount

func (s *Server) GetBootnodeConnCount() int64

GetBootnodeConnCount fetches the number of active bootnode connections [Thread safe]

func (*Server) GetPeerDistance

func (s *Server) GetPeerDistance(peerID peer.ID) *big.Int

func (*Server) GetPeerInfo

func (s *Server) GetPeerInfo(peerID peer.ID) *peer.AddrInfo

GetPeerInfo fetches the information of a peer

func (*Server) GetProtocols

func (s *Server) GetProtocols(peerID peer.ID) ([]string, error)

GetProtocols fetches the list of node-supported protocols

func (*Server) GetRandomBootnode

func (s *Server) GetRandomBootnode() *peer.AddrInfo

GetRandomBootnode fetches a random bootnode that's currently NOT connected, if any

func (*Server) GetRandomPeer

func (s *Server) GetRandomPeer() *peer.ID

GetRandomPeer fetches a random peer from the peers list

func (*Server) HasFreeConnectionSlot

func (s *Server) HasFreeConnectionSlot(direction network.Direction) bool

HasFreeConnectionSlot checks if there are free connection slots in the specified direction [Thread safe]

func (*Server) IsConnected

func (s *Server) IsConnected(peerID peer.ID) bool

IsConnected checks if the networking server is connected to a peer

func (*Server) IsTemporaryDial

func (s *Server) IsTemporaryDial(peerID peer.ID) bool

IsTemporaryDial checks if a peer connection is temporary [Thread safe]

func (*Server) JoinPeer

func (s *Server) JoinPeer(rawPeerMultiaddr string) error

JoinPeer attempts to add a new peer to the networking server

func (*Server) NewDiscoveryClient

func (s *Server) NewDiscoveryClient(peerID peer.ID) (proto.DiscoveryClient, error)

NewDiscoveryClient returns a new or existing discovery service client connection

func (*Server) NewIdentityClient

func (s *Server) NewIdentityClient(peerID peer.ID) (proto.IdentityClient, error)

NewIdentityClient returns a new identity service client connection

func (*Server) NewProtoConnection

func (s *Server) NewProtoConnection(protocol string, peerID peer.ID) (*rawGrpc.ClientConn, error)

NewProtoConnection opens up a new stream on the set protocol to the peer, and returns a reference to the connection

func (*Server) NewStream

func (s *Server) NewStream(proto string, id peer.ID) (network.Stream, error)

func (*Server) NewTopic

func (s *Server) NewTopic(protoID string, obj proto.Message) (*Topic, error)

func (*Server) Peers

func (s *Server) Peers() []*PeerConnInfo

Peers returns a copy of the networking server's peer connection info set. Only one (initial) connection (inbound OR outbound) per peer is contained [Thread safe]

func (*Server) RegisterProtocol

func (s *Server) RegisterProtocol(id string, p Protocol)

func (*Server) RemoveFromPeerStore

func (s *Server) RemoveFromPeerStore(peerInfo *peer.AddrInfo)

RemoveFromPeerStore removes peer information from the node's peer store

func (*Server) RemoveTemporaryDial

func (s *Server) RemoveTemporaryDial(peerID peer.ID)

RemoveTemporaryDial removes a peer connection as temporary [Thread safe]

func (*Server) SaveProtocolStream

func (s *Server) SaveProtocolStream(
	protocol string,
	stream *rawGrpc.ClientConn,
	peerID peer.ID,
)

SaveProtocolStream saves the protocol stream to the peer protocol stream reference [Thread safe]

func (*Server) Start

func (s *Server) Start() error

Start starts the networking services

func (*Server) Subscribe

func (s *Server) Subscribe() (*Subscription, error)

Subscribe starts a PeerEvent subscription

func (*Server) SubscribeCh

func (s *Server) SubscribeCh() (<-chan *peerEvent.PeerEvent, error)

SubscribeCh returns an event of of subscription events

func (*Server) SubscribeFn

func (s *Server) SubscribeFn(handler func(evnt *peerEvent.PeerEvent)) error

SubscribeFn is a helper method to run subscription of PeerEvents

func (*Server) TemporaryDialPeer

func (s *Server) TemporaryDialPeer(peerAddrInfo *peer.AddrInfo)

func (*Server) UpdatePendingConnCount

func (s *Server) UpdatePendingConnCount(delta int64, direction network.Direction)

UpdatePendingConnCount updates the pending connection count in the specified direction [Thread safe]

type Subscription

type Subscription struct {
	// contains filtered or unexported fields
}

func (*Subscription) Close

func (s *Subscription) Close()

func (*Subscription) Get

func (s *Subscription) Get() *peerEvent.PeerEvent

func (*Subscription) GetCh

func (s *Subscription) GetCh() chan *peerEvent.PeerEvent

type Topic

type Topic struct {
	// contains filtered or unexported fields
}

func (*Topic) Publish

func (t *Topic) Publish(obj proto.Message) error

func (*Topic) Subscribe

func (t *Topic) Subscribe(handler func(obj interface{}, from peer.ID)) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL