network

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 43 Imported by: 4

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 added in v0.5.0

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 added in v0.5.0

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 WaitUntilRoutingTableIsFilled added in v1.1.1

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

WaitUntilRoutingTableIsFilled 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
}

Config details the params for the base networking server

func DefaultConfig

func DefaultConfig() *Config

type ConnectionInfo added in v0.3.3

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

ConnectionInfo keeps track of current connection information for the networking server

func NewBlankConnectionInfo added in v0.3.3

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

NewBlankConnectionInfo returns a cleared ConnectionInfo instance

func (*ConnectionInfo) GetInboundConnCount added in v0.3.3

func (ci *ConnectionInfo) GetInboundConnCount() int64

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

func (*ConnectionInfo) GetOutboundConnCount added in v0.3.3

func (ci *ConnectionInfo) GetOutboundConnCount() int64

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

func (*ConnectionInfo) GetPendingInboundConnCount added in v0.3.3

func (ci *ConnectionInfo) GetPendingInboundConnCount() int64

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

func (*ConnectionInfo) GetPendingOutboundConnCount added in v0.3.3

func (ci *ConnectionInfo) GetPendingOutboundConnCount() int64

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

func (*ConnectionInfo) HasFreeConnectionSlot added in v0.3.3

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 added in v0.3.3

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 added in v0.3.3

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 added in v0.3.3

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 added in v0.3.3

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 PeerConnInfo added in v0.3.3

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, error)
	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 added in v0.3.3

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 added in v0.3.3

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 added in v0.3.3

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

CloseProtocolStream closes a protocol stream to the specified peer

func (*Server) DisconnectFromPeer added in v0.3.3

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

DisconnectFromPeer disconnects the networking server from the specified peer

func (*Server) EmitEvent added in v0.3.3

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

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

func (*Server) FetchOrSetTemporaryDial added in v0.3.3

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 added in v0.3.3

func (s *Server) GetBootnodeConnCount() int64

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

func (*Server) GetPeerDistance added in v0.5.0

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 added in v0.3.3

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

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

func (*Server) GetRandomPeer added in v0.3.3

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

GetRandomPeer fetches a random peer from the peers list

func (*Server) HasFreeConnectionSlot added in v0.3.3

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 added in v0.5.0

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

IsConnected checks if the networking server is connected to a peer

func (*Server) IsTemporaryDial added in v0.3.3

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

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

func (*Server) JoinPeer added in v0.3.3

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

JoinPeer attempts to add a new peer to the networking server

func (*Server) NewDiscoveryClient added in v0.3.3

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

NewDiscoveryClient returns a new or existing discovery service client connection

func (*Server) NewIdentityClient added in v0.3.3

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

NewIdentityClient returns a new identity service client connection

func (*Server) NewProtoConnection added in v0.5.0

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 added in v0.3.3

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

func (*Server) RemoveFromPeerStore added in v0.3.3

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

RemoveFromPeerStore removes peer information from the node's peer store

func (*Server) RemoveTemporaryDial added in v0.3.3

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

RemoveTemporaryDial removes a peer connection as temporary [Thread safe]

func (*Server) SaveProtocolStream added in v0.5.0

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(ctx context.Context, handler func(evnt *peerEvent.PeerEvent)) error

Subscribe is a helper method to run subscription of PeerEvents

func (*Server) SubscribeCh

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

SubscribeCh returns an event of of subscription events

func (*Server) TemporaryDialPeer added in v0.6.1

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

func (*Server) UpdatePendingConnCount added in v0.3.3

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

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

type Slots added in v1.1.0

type Slots chan struct{}

Slots is synchronization structure A routine can invoke the Take method, which will block until at least one slot becomes available The Release method can be called by other routines to increase the number of available slots by one

func NewSlots added in v1.1.0

func NewSlots(maximal int64) Slots

NewSlots creates Slots object with maximal slots available

func (Slots) Release added in v1.1.0

func (s Slots) Release()

Release returns back one slot. If all slots are already released, nothing will happen

func (Slots) Take added in v1.1.0

func (s Slots) Take(ctx context.Context) bool

Take takes slot if available or blocks until slot is available or context is done

type Topic

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

func (*Topic) Close added in v0.8.0

func (t *Topic) Close()

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