network

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: GPL-3.0, LGPL-3.0 Imports: 23 Imported by: 0

README

Peer Package

The peer package handles networking for the VM.

Network

The Network interface implements the networking portion of the required VM interface. The VM utilizes the Network interface to:

  • Set an App Gossip handler for incoming VM gossip messages
  • Set an App Request handler for incoming VM request messages
  • Send App Requests to peers in the network and specify a response handler to be called upon receiving a response or failure notification
  • Send App Gossip messages to the network

Client

The client utilizes the Network interface to send requests to peers on the network and utilizes the waitingHandler to wait until a response or failure is received from the AvalancheGo networking layer.

This allows the user of Client to treat it as if it were returning results from the network synchronously.

result, err := client.Request(nodeID, request) // Blocks until receiving a response from the network
if err != nil {
    return err
}

foo(result) // do something with the result

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNetworkRequest

func IsNetworkRequest(requestID uint32) bool

IsNetworkRequest checks if the given requestID is a request for this network handler (even-numbered requestIDs) SDK requests are odd-numbered requestIDs (see invariant: https://github.com/ava-labs/avalanchego/blob/v1.13.0/network/p2p/router.go#L83)

func NewPeerTracker

func NewPeerTracker() *peerTracker

Types

type Network

type Network interface {
	validators.Connector
	common.AppHandler

	SyncedNetworkClient

	// SendAppRequestAny sends request to an arbitrary peer with a
	// node version greater than or equal to minVersion.
	// Returns the ID of the chosen peer, and an error if the request could not
	// be sent to a peer with the desired [minVersion].
	SendAppRequestAny(ctx context.Context, minVersion *version.Application, message []byte, handler message.ResponseHandler) (ids.NodeID, error)

	// SendAppRequest sends message to given nodeID, notifying handler when there's a response or timeout
	SendAppRequest(ctx context.Context, nodeID ids.NodeID, message []byte, handler message.ResponseHandler) error

	// Shutdown stops all peer channel listeners and marks the node to have stopped
	// n.Start() can be called again but the peers will have to be reconnected
	// by calling OnPeerConnected for each peer
	Shutdown()

	// SetRequestHandler sets the provided request handler as the request handler
	SetRequestHandler(handler message.RequestHandler)

	// Size returns the size of the network in number of connected peers
	Size() uint32

	// NewClient returns a client to send messages with for the given protocol
	NewClient(protocol uint64, options ...p2p.ClientOption) *p2p.Client
	// AddHandler registers a server handler for an application protocol
	AddHandler(protocol uint64, handler p2p.Handler) error

	P2PValidators() *p2p.Validators
}

func NewNetwork

func NewNetwork(
	ctx *snow.Context,
	appSender common.AppSender,
	codec codec.Manager,
	maxActiveAppRequests int64,
	registerer prometheus.Registerer,
) (Network, error)

type SyncedNetworkClient

type SyncedNetworkClient interface {
	// SendSyncedAppRequestAny synchronously sends request to an arbitrary peer with a
	// node version greater than or equal to minVersion.
	// Returns response bytes, the ID of the chosen peer, and ErrRequestFailed if
	// the request should be retried.
	SendSyncedAppRequestAny(ctx context.Context, minVersion *version.Application, request []byte) ([]byte, ids.NodeID, error)

	// SendSyncedAppRequest synchronously sends request to the selected nodeID
	// Returns response bytes, and ErrRequestFailed if the request should be retried.
	SendSyncedAppRequest(ctx context.Context, nodeID ids.NodeID, request []byte) ([]byte, error)

	// TrackBandwidth should be called for each valid request with the bandwidth
	// (length of response divided by request time), and with 0 if the response is invalid.
	TrackBandwidth(nodeID ids.NodeID, bandwidth float64)
}

SyncedNetworkClient defines ability to send request / response through the Network

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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