network

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDialRatio = 0.2

	DefaultLibp2pPort int = 1478

	MinimumBootNodes int = 1

	DefaultDialTimeout         = 30 * time.Second
	DefaultBackgroundTaskSleep = 10 * time.Second
)
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
	// Github action runners are very slow, so we need to increase the timeout
	DefaultJoinTimeout   = 100 * time.Second
	DefaultBufferTimeout = DefaultJoinTimeout + time.Second*30
	MaxConnectionTimeout = 5 * time.Minute
)

Functions

func DisconnectAndWait added in v1.2.1

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(
	t *testing.T,
	source,
	destination Server,
	connectTimeout time.Duration,
	joinTimeout time.Duration,
	static bool,
) 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 v1.2.1

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

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

func MeshJoin

func MeshJoin(t *testing.T, servers ...*DefaultServer) []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 *DefaultServer, size int) (bool, error)

WaitUntilRoutingTableToBeAdded check routing table has given ids and retry by timeout

Types

type Config

type Config struct {
	DiscoverIngoreCIDR []*net.IPNet // list of CIDR ranges to ignore when discovering peers

	NoDiscover       bool                   // flag indicating if the discovery mechanism should be turned on
	Addr             *net.TCPAddr           // the base address
	NatAddr          *net.TCPAddr           // 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 *DefaultServer) // Additional logic that needs to be executed on the server before starting
	Logger         hclog.Logger
}

type DefaultServer added in v1.2.2

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

func CreateServer

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

func (*DefaultServer) AddPeer added in v1.2.2

func (s *DefaultServer) 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 (*DefaultServer) AddToPeerStore added in v1.2.2

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

AddToPeerStore adds peer information to the node's peer store, static node and bootnode addresses are added with permanent TTL

func (*DefaultServer) AddrInfo added in v1.2.2

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

func (*DefaultServer) Close added in v1.2.2

func (s *DefaultServer) Close() error

func (*DefaultServer) Connect added in v1.2.4

func (s *DefaultServer) Connect(peerInfo peer.AddrInfo) error

func (*DefaultServer) DisconnectFromPeer added in v1.2.2

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

DisconnectFromPeer disconnects the networking server from the specified peer

func (*DefaultServer) EmitEvent added in v1.2.2

func (s *DefaultServer) EmitEvent(ctx context.Context, event *peerEvent.PeerEvent)

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

func (*DefaultServer) ForgetPeer added in v1.2.2

func (s *DefaultServer) ForgetPeer(peer peer.ID, reason string)

ForgetPeer disconnects, remove and forget peer to prevent broadcast discovery to other peers

Cauction: take care of using this to ignore peer from store, which may break peer discovery

func (*DefaultServer) GetMetrics added in v1.2.4

func (s *DefaultServer) GetMetrics() *Metrics

func (*DefaultServer) GetPeerDistance added in v1.2.2

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

func (*DefaultServer) GetPeerInfo added in v1.2.2

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

GetPeerInfo fetches the information of a peer

func (*DefaultServer) GetProtocols added in v1.2.2

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

GetProtocols fetches the list of node-supported protocols

func (*DefaultServer) GetRandomBootnode added in v1.2.2

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

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

func (*DefaultServer) GetRandomPeer added in v1.2.2

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

GetRandomPeer fetches a random peer from the peers list

func (*DefaultServer) GetTracer added in v1.2.4

func (s *DefaultServer) GetTracer() telemetry.Tracer

GetTracer returns the tracer instance

func (*DefaultServer) HasFreeConnectionSlot added in v1.2.2

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

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

func (*DefaultServer) HasPeer added in v1.2.2

func (s *DefaultServer) HasPeer(peerID peer.ID) bool

hasPeer checks if the peer is present in the peers list [Thread safe]

func (*DefaultServer) IsBootnode added in v1.2.4

func (s *DefaultServer) IsBootnode(peerID peer.ID) bool

IsBootnode checks if the peer is a bootnode

func (*DefaultServer) IsStaticPeer added in v1.2.4

func (s *DefaultServer) IsStaticPeer(peerID peer.ID) bool

IsStaticPeer checks if the peer is a static peer

func (*DefaultServer) JoinPeer added in v1.2.2

func (s *DefaultServer) JoinPeer(rawPeerMultiaddr string, static bool) error

JoinPeer attempts to add a new peer to the networking server

func (*DefaultServer) NewDiscoveryClient added in v1.2.2

func (s *DefaultServer) NewDiscoveryClient(ctx context.Context, peerID peer.ID) (client.DiscoveryClient, error)

NewDiscoveryClient returns a new or existing discovery service client connection

func (*DefaultServer) NewIdentityClient added in v1.2.2

func (s *DefaultServer) NewIdentityClient(ctx context.Context, peerID peer.ID) (client.IdentityClient, error)

NewIdentityClient returns a new identity service client connection

func (*DefaultServer) NewProtoConnection added in v1.2.2

func (s *DefaultServer) NewProtoConnection(
	ctx context.Context,
	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 (*DefaultServer) NewStream added in v1.2.2

func (s *DefaultServer) NewStream(ctx context.Context, proto string, id peer.ID) (network.Stream, error)

func (*DefaultServer) NewTopic added in v1.2.2

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

func (*DefaultServer) PeerCount added in v1.2.2

func (s *DefaultServer) PeerCount() int64

PeerCount returns the number of connected peers [Thread safe]

func (*DefaultServer) Peers added in v1.2.2

func (s *DefaultServer) 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 (*DefaultServer) RegisterProtocol added in v1.2.2

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

func (*DefaultServer) RemoveFromPeerStore added in v1.2.2

func (s *DefaultServer) RemoveFromPeerStore(peerID peer.ID)

RemoveFromPeerStore removes peer information from the node's peer store, ignoring static nodes and bootnodes

func (*DefaultServer) Start added in v1.2.2

func (s *DefaultServer) Start() error

Start starts the networking services

func (*DefaultServer) SubscribeFn added in v1.2.2

func (s *DefaultServer) SubscribeFn(ctx context.Context, handler func(evnt *peerEvent.PeerEvent)) error

SubscribeFn is a helper method to run subscription of PeerEvents

func (*DefaultServer) UpdatePendingConnCount added in v1.2.2

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

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

type Metrics

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

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

func (*Metrics) GetGrpcMetrics added in v1.2.4

func (m *Metrics) GetGrpcMetrics() client.Metrics

func (*Metrics) NewProtoConnectionCountInc added in v1.2.4

func (m *Metrics) NewProtoConnectionCountInc()

func (*Metrics) NewProtoConnectionErrorCountInc added in v1.2.4

func (m *Metrics) NewProtoConnectionErrorCountInc()

func (*Metrics) NewProtoConnectionSecondObserve added in v1.2.4

func (m *Metrics) NewProtoConnectionSecondObserve(v float64)

func (*Metrics) SetInboundConnectionsCount added in v1.2.2

func (m *Metrics) SetInboundConnectionsCount(v float64)

func (*Metrics) SetOutboundConnectionsCount added in v1.2.2

func (m *Metrics) SetOutboundConnectionsCount(v float64)

func (*Metrics) SetPendingInboundConnectionsCount added in v1.2.2

func (m *Metrics) SetPendingInboundConnectionsCount(v float64)

func (*Metrics) SetPendingOutboundConnectionsCount added in v1.2.2

func (m *Metrics) SetPendingOutboundConnectionsCount(v float64)

func (*Metrics) SetTotalPeerCount added in v1.2.2

func (m *Metrics) SetTotalPeerCount(v float64)

type Network added in v1.2.2

type Network interface {

	// AddrInfo returns Network Info
	AddrInfo() *peer.AddrInfo
	// Peers returns current connected peers
	Peers() []*PeerConnInfo
	// PeerCount returns the number of connected peers
	PeerCount() int64
	// GetPeerInfo returns the peer info for the given peer ID
	GetPeerInfo(peerID peer.ID) *peer.AddrInfo
	// JoinPeer joins a peer to the network
	JoinPeer(rawPeerMultiaddr string, static bool) error
	// HasPeer returns true if the peer is connected
	HasPeer(peerID peer.ID) bool
	// IsStaticPeer returns true if the peer is a static peer
	IsStaticPeer(peerID peer.ID) bool
	// DisconnectFromPeer disconnects the networking server from the specified peer
	DisconnectFromPeer(peer peer.ID, reason string)
	// ForgetPeer disconnects, remove and forget peer to prevent broadcast discovery to other peers
	ForgetPeer(peer peer.ID, reason string)

	// NewTopic Creates New Topic for gossip
	NewTopic(protoID string, obj proto.Message) (Topic, error)
	// SubscribeFn subscribe of peer event
	SubscribeFn(ctx context.Context, handler func(evnt *event.PeerEvent)) error

	// RegisterProtocol registers gRPC service
	RegisterProtocol(string, Protocol)
	// GetProtocols returns the list of protocols supported by the peer
	GetProtocols(peerID peer.ID) ([]string, error)
	// NewProtoConnection opens up a new client connect on the set protocol to the peer,
	// and returns a reference to the connection
	NewProtoConnection(ctx context.Context, protocol string, peerID peer.ID) (*rawGrpc.ClientConn, error)

	// **Metrics**
	// GetMetrics returns the metrics of the network
	GetMetrics() *Metrics
}

type NonetworkServer added in v1.2.2

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

NonetworkServer is a fake server that does nothing only used for testing or offline mode

func (*NonetworkServer) AddrInfo added in v1.2.2

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

func (*NonetworkServer) Close added in v1.2.2

func (s *NonetworkServer) Close() error

func (*NonetworkServer) DisconnectFromPeer added in v1.2.2

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

func (*NonetworkServer) ForgetPeer added in v1.2.2

func (s *NonetworkServer) ForgetPeer(peer peer.ID, reason string)

func (*NonetworkServer) GetMetrics added in v1.2.4

func (s *NonetworkServer) GetMetrics() *Metrics

func (*NonetworkServer) GetPeerInfo added in v1.2.2

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

func (*NonetworkServer) GetProtocols added in v1.2.2

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

func (*NonetworkServer) HasPeer added in v1.2.2

func (s *NonetworkServer) HasPeer(peerID peer.ID) bool

func (*NonetworkServer) IsConnected added in v1.2.2

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

func (*NonetworkServer) IsStaticPeer added in v1.2.4

func (s *NonetworkServer) IsStaticPeer(peerID peer.ID) bool

func (*NonetworkServer) JoinPeer added in v1.2.2

func (s *NonetworkServer) JoinPeer(rawPeerMultiaddr string, static bool) error

func (*NonetworkServer) NewProtoConnection added in v1.2.2

func (s *NonetworkServer) NewProtoConnection(
	ctx context.Context,
	protocol string,
	peerID peer.ID,
) (*rawGrpc.ClientConn, error)

func (*NonetworkServer) NewTopic added in v1.2.2

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

func (*NonetworkServer) PeerCount added in v1.2.2

func (s *NonetworkServer) PeerCount() int64

func (*NonetworkServer) Peers added in v1.2.2

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

func (*NonetworkServer) RegisterProtocol added in v1.2.2

func (s *NonetworkServer) RegisterProtocol(string, Protocol)

func (*NonetworkServer) Start added in v1.2.2

func (s *NonetworkServer) Start() error

func (*NonetworkServer) SubscribeFn added in v1.2.4

func (s *NonetworkServer) SubscribeFn(context.Context, func(evnt *event.PeerEvent)) error

type NonetworkTopic added in v1.2.2

type NonetworkTopic struct{}

NonetworkTopic is a fake topic that does nothing only used for testing or offline mode

func (*NonetworkTopic) Close added in v1.2.2

func (t *NonetworkTopic) Close() error

func (*NonetworkTopic) Publish added in v1.2.2

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

func (*NonetworkTopic) Subscribe added in v1.2.2

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

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(context.Context, network.Stream) (*rawGrpc.ClientConn, error)
	Handler() func(network.Stream)
}

type Server

type Server interface {
	Network

	// Start starts the server
	Start() error
	// Stop stops the server
	Close() error
}

func NewServer

func NewServer(ctx context.Context, logger hclog.Logger, tracer telemetry.Tracer, config *Config) (Server, error)

NewServer returns a new instance of the networking server

type Topic

type Topic interface {
	// Publish publishes a message to the topic
	Publish(obj proto.Message) error
	// Subscribe subscribes to the topic
	Subscribe(handler func(obj interface{}, from peer.ID)) error
	// Close closes the topic
	Close() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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