p2p

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: BSD-3-Clause Imports: 22 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequestPending = errors.New("request pending")
	ErrNoPeers        = errors.New("no peers")
)
View Source
var (
	ErrExistingAppProtocol = errors.New("existing app protocol")
	ErrUnrequestedResponse = errors.New("unrequested response")
)
View Source
var (
	ErrNotValidator = errors.New("not a validator")
)
View Source
var (
	ErrThrottled = errors.New("throttled")
)

Functions

func ParseMessage added in v1.11.1

func ParseMessage(msg []byte) (uint64, []byte, bool)

Parse a gossip or request message.

Returns: - The protocol ID. - The unprefixed protocol message. - A boolean indicating that parsing succeeded.

func PrefixMessage added in v1.11.1

func PrefixMessage(prefix, msg []byte) []byte

PrefixMessage prefixes the original message with the protocol identifier.

Only gossip and request messages need to be prefixed. Response messages don't need to be prefixed because request ids are tracked which map to the expected response handler.

func ProtocolPrefix added in v1.11.1

func ProtocolPrefix(handlerID uint64) []byte

Types

type AppResponseCallback

type AppResponseCallback func(
	ctx context.Context,
	nodeID ids.NodeID,
	responseBytes []byte,
	err error,
)

AppResponseCallback is called upon receiving an AppResponse for an AppRequest issued by Client. Callers should check [err] to see whether the AppRequest failed or not.

type Client

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

func (*Client) AppGossip

func (c *Client) AppGossip(
	ctx context.Context,
	appGossipBytes []byte,
) error

AppGossip sends a gossip message to a random set of peers.

func (*Client) AppGossipSpecific

func (c *Client) AppGossipSpecific(
	ctx context.Context,
	nodeIDs set.Set[ids.NodeID],
	appGossipBytes []byte,
) error

AppGossipSpecific sends a gossip message to a predetermined set of peers.

func (*Client) AppRequest

func (c *Client) AppRequest(
	ctx context.Context,
	nodeIDs set.Set[ids.NodeID],
	appRequestBytes []byte,
	onResponse AppResponseCallback,
) error

AppRequest issues an arbitrary request to a node. [onResponse] is invoked upon an error or a response.

func (*Client) AppRequestAny

func (c *Client) AppRequestAny(
	ctx context.Context,
	appRequestBytes []byte,
	onResponse AppResponseCallback,
) error

AppRequestAny issues an AppRequest to an arbitrary node decided by Client. If a specific node needs to be requested, use AppRequest instead. See AppRequest for more docs.

func (*Client) CrossChainAppRequest

func (c *Client) CrossChainAppRequest(
	ctx context.Context,
	chainID ids.ID,
	appRequestBytes []byte,
	onResponse CrossChainAppResponseCallback,
) error

CrossChainAppRequest sends a cross chain app request to another vm. [onResponse] is invoked upon an error or a response.

type ClientOption added in v1.10.17

type ClientOption interface {
	// contains filtered or unexported methods
}

ClientOption configures Client

func WithValidatorSampling added in v1.10.17

func WithValidatorSampling(validators *Validators) ClientOption

WithValidatorSampling configures Client.AppRequestAny to sample validators

type CrossChainAppResponseCallback

type CrossChainAppResponseCallback func(
	ctx context.Context,
	chainID ids.ID,
	responseBytes []byte,
	err error,
)

CrossChainAppResponseCallback is called upon receiving an CrossChainAppResponse for a CrossChainAppRequest issued by Client. Callers should check [err] to see whether the AppRequest failed or not.

type Handler

type Handler interface {
	// AppGossip is called when handling an AppGossip message.
	AppGossip(
		ctx context.Context,
		nodeID ids.NodeID,
		gossipBytes []byte,
	)
	// AppRequest is called when handling an AppRequest message.
	// Returns the bytes for the response corresponding to [requestBytes]
	AppRequest(
		ctx context.Context,
		nodeID ids.NodeID,
		deadline time.Time,
		requestBytes []byte,
	) ([]byte, error)
	// CrossChainAppRequest is called when handling a CrossChainAppRequest
	// message.
	// Returns the bytes for the response corresponding to [requestBytes]
	CrossChainAppRequest(
		ctx context.Context,
		chainID ids.ID,
		deadline time.Time,
		requestBytes []byte,
	) ([]byte, error)
}

Handler is the server-side logic for virtual machine application protocols.

type Network added in v1.10.17

type Network struct {
	Peers *Peers
	// contains filtered or unexported fields
}

Network exposes networking state and supports building p2p application protocols

func NewNetwork added in v1.10.17

func NewNetwork(
	log logging.Logger,
	sender common.AppSender,
	registerer prometheus.Registerer,
	namespace string,
) (*Network, error)

NewNetwork returns an instance of Network

func (*Network) AddHandler added in v1.10.18

func (n *Network) AddHandler(handlerID uint64, handler Handler) error

AddHandler reserves an identifier for an application protocol

func (*Network) AppGossip added in v1.10.17

func (n *Network) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error

func (*Network) AppRequest added in v1.10.17

func (n *Network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error

func (*Network) AppRequestFailed added in v1.10.17

func (n *Network) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, appErr *common.AppError) error

func (*Network) AppResponse added in v1.10.17

func (n *Network) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error

func (*Network) Connected added in v1.10.17

func (n *Network) Connected(_ context.Context, nodeID ids.NodeID, _ *version.Application) error

func (*Network) CrossChainAppRequest added in v1.10.17

func (n *Network) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error

func (*Network) CrossChainAppRequestFailed added in v1.10.17

func (n *Network) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32, appErr *common.AppError) error

func (*Network) CrossChainAppResponse added in v1.10.17

func (n *Network) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error

func (*Network) Disconnected added in v1.10.17

func (n *Network) Disconnected(_ context.Context, nodeID ids.NodeID) error

func (*Network) NewClient added in v1.10.18

func (n *Network) NewClient(handlerID uint64, options ...ClientOption) *Client

NewClient returns a Client that can be used to send messages for the corresponding protocol.

type NoOpHandler added in v1.10.9

type NoOpHandler struct{}

NoOpHandler drops all messages

func (NoOpHandler) AppGossip added in v1.10.9

func (NoOpHandler) AppGossip(context.Context, ids.NodeID, []byte)

func (NoOpHandler) AppRequest added in v1.10.9

func (NoOpHandler) AppRequest(context.Context, ids.NodeID, time.Time, []byte) ([]byte, error)

func (NoOpHandler) CrossChainAppRequest added in v1.10.9

func (NoOpHandler) CrossChainAppRequest(context.Context, ids.ID, time.Time, []byte) ([]byte, error)

type NodeSampler added in v1.10.9

type NodeSampler interface {
	// Sample returns at most [limit] nodes. This may return fewer nodes if
	// fewer than [limit] are available.
	Sample(ctx context.Context, limit int) []ids.NodeID
}

NodeSampler samples nodes in network

type PeerTracker added in v1.10.17

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

Tracks the bandwidth of responses coming from peers, preferring to contact peers with known good bandwidth, connecting to new peers with an exponentially decaying probability.

func NewPeerTracker added in v1.10.17

func NewPeerTracker(
	log logging.Logger,
	metricsNamespace string,
	registerer prometheus.Registerer,
	ignoredNodes set.Set[ids.NodeID],
	minVersion *version.Application,
) (*PeerTracker, error)

func (*PeerTracker) Connected added in v1.10.17

func (p *PeerTracker) Connected(nodeID ids.NodeID, nodeVersion *version.Application)

Connected should be called when [nodeID] connects to this node.

func (*PeerTracker) Disconnected added in v1.10.17

func (p *PeerTracker) Disconnected(nodeID ids.NodeID)

Disconnected should be called when [nodeID] disconnects from this node.

func (*PeerTracker) RegisterFailure added in v1.11.1

func (p *PeerTracker) RegisterFailure(nodeID ids.NodeID)

Record that a request failed to [nodeID].

Adds the peer's bandwidth averager to the bandwidth heap.

func (*PeerTracker) RegisterRequest added in v1.11.1

func (p *PeerTracker) RegisterRequest(nodeID ids.NodeID)

Record that we sent a request to [nodeID].

Removes the peer's bandwidth averager from the bandwidth heap.

func (*PeerTracker) RegisterResponse added in v1.11.1

func (p *PeerTracker) RegisterResponse(nodeID ids.NodeID, bandwidth float64)

Record that we observed that [nodeID]'s bandwidth is [bandwidth].

Adds the peer's bandwidth averager to the bandwidth heap.

func (*PeerTracker) SelectPeer added in v1.11.1

func (p *PeerTracker) SelectPeer() (ids.NodeID, bool)

SelectPeer that we could send a request to.

If we should track more peers, returns a random untracked peer, if any exist. Otherwise, with probability [randomPeerProbability] returns a random peer from [p.responsivePeers]. With probability [1-randomPeerProbability] returns the peer in [p.bandwidthHeap] with the highest bandwidth.

Returns false if there are no connected peers.

func (*PeerTracker) Size added in v1.10.17

func (p *PeerTracker) Size() int

Returns the number of peers the node is connected to.

type Peers added in v1.10.9

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

Peers contains metadata about the current set of connected peers

func (*Peers) Sample added in v1.10.9

func (p *Peers) Sample(limit int) []ids.NodeID

Sample returns a pseudo-random sample of up to limit Peers

type SlidingWindowThrottler added in v1.10.10

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

SlidingWindowThrottler is an implementation of the sliding window throttling algorithm.

func NewSlidingWindowThrottler added in v1.10.10

func NewSlidingWindowThrottler(period time.Duration, limit int) *SlidingWindowThrottler

NewSlidingWindowThrottler returns a new instance of SlidingWindowThrottler. Nodes are throttled if they exceed [limit] messages during an interval of time over [period]. [period] and [limit] should both be > 0.

func (*SlidingWindowThrottler) Handle added in v1.10.10

func (s *SlidingWindowThrottler) Handle(nodeID ids.NodeID) bool

Handle returns true if the amount of calls received in the last [s.period] time is less than [s.limit]

This is calculated by adding the current period's count to a weighted count of the previous period.

type TestHandler added in v1.10.18

type TestHandler struct {
	AppGossipF            func(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)
	AppRequestF           func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)
	CrossChainAppRequestF func(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)
}

func (TestHandler) AppGossip added in v1.10.18

func (t TestHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)

func (TestHandler) AppRequest added in v1.10.18

func (t TestHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)

func (TestHandler) CrossChainAppRequest added in v1.10.18

func (t TestHandler) CrossChainAppRequest(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)

type Throttler added in v1.10.10

type Throttler interface {
	// Handle returns true if a message from [nodeID] should be handled.
	Handle(nodeID ids.NodeID) bool
}

type ThrottlerHandler added in v1.10.10

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

func NewThrottlerHandler added in v1.10.18

func NewThrottlerHandler(handler Handler, throttler Throttler, log logging.Logger) *ThrottlerHandler

func (ThrottlerHandler) AppGossip added in v1.10.10

func (t ThrottlerHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)

func (ThrottlerHandler) AppRequest added in v1.10.10

func (t ThrottlerHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)

func (ThrottlerHandler) CrossChainAppRequest added in v1.10.18

func (t ThrottlerHandler) CrossChainAppRequest(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)

type ValidatorHandler added in v1.10.10

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

ValidatorHandler drops messages from non-validators

func NewValidatorHandler added in v1.10.18

func NewValidatorHandler(
	handler Handler,
	validatorSet ValidatorSet,
	log logging.Logger,
) *ValidatorHandler

func (ValidatorHandler) AppGossip added in v1.10.10

func (v ValidatorHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)

func (ValidatorHandler) AppRequest added in v1.10.10

func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)

func (ValidatorHandler) CrossChainAppRequest added in v1.10.18

func (v ValidatorHandler) CrossChainAppRequest(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)

type ValidatorSet added in v1.10.10

type ValidatorSet interface {
	Has(ctx context.Context, nodeID ids.NodeID) bool // TODO return error
}

type Validators added in v1.10.9

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

Validators contains a set of nodes that are staking.

func NewValidators added in v1.10.9

func NewValidators(
	peers *Peers,
	log logging.Logger,
	subnetID ids.ID,
	validators validators.State,
	maxValidatorSetStaleness time.Duration,
) *Validators

func (*Validators) Has added in v1.10.9

func (v *Validators) Has(ctx context.Context, nodeID ids.NodeID) bool

Has returns if nodeID is a connected validator

func (*Validators) Sample added in v1.10.9

func (v *Validators) Sample(ctx context.Context, limit int) []ids.NodeID

Sample returns a random sample of connected validators

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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