p2p

package
v1.10.10 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: BSD-3-Clause Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAppRequestFailed = errors.New("app request failed")
	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

This section is empty.

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 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,
	) error
	// 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 NoOpHandler

type NoOpHandler struct{}

func (NoOpHandler) AppGossip

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

func (NoOpHandler) AppRequest

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

func (NoOpHandler) CrossChainAppRequest

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

type NodeSampler

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 Peers

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

Peers contains a set of nodes that we are connected to.

func (*Peers) Connected

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

func (*Peers) Disconnected

func (p *Peers) Disconnected(_ context.Context, nodeID ids.NodeID) error

func (*Peers) Sample

func (p *Peers) Sample(_ context.Context, limit int) []ids.NodeID

type Router

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

Router routes incoming application messages to the corresponding registered app handler. App messages must be made using the registered handler's corresponding Client.

func NewRouter

func NewRouter(
	log logging.Logger,
	sender common.AppSender,
	metrics prometheus.Registerer,
	namespace string,
) *Router

NewRouter returns a new instance of Router

func (*Router) AppGossip

func (r *Router) AppGossip(ctx context.Context, nodeID ids.NodeID, gossip []byte) error

func (*Router) AppRequest

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

func (*Router) AppRequestFailed

func (r *Router) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error

func (*Router) AppResponse

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

func (*Router) CrossChainAppRequest

func (r *Router) CrossChainAppRequest(
	ctx context.Context,
	chainID ids.ID,
	requestID uint32,
	deadline time.Time,
	msg []byte,
) error

func (*Router) CrossChainAppRequestFailed

func (r *Router) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32) error

func (*Router) CrossChainAppResponse

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

func (*Router) RegisterAppProtocol

func (r *Router) RegisterAppProtocol(handlerID uint64, handler Handler, nodeSampler NodeSampler) (*Client, error)

RegisterAppProtocol reserves an identifier for an application protocol and returns a Client that can be used to send messages for the corresponding protocol.

type SlidingWindowThrottler

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

SlidingWindowThrottler is an implementation of the sliding window throttling algorithm.

func NewSlidingWindowThrottler

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

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 Throttler

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

type ThrottlerHandler

type ThrottlerHandler struct {
	Handler
	Throttler Throttler
}

func (ThrottlerHandler) AppGossip

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

func (ThrottlerHandler) AppRequest

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

type ValidatorHandler

type ValidatorHandler struct {
	Handler
	ValidatorSet ValidatorSet
}

ValidatorHandler drops messages from non-validators

func (ValidatorHandler) AppGossip

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

func (ValidatorHandler) AppRequest

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

type ValidatorSet

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

type Validators

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

Validators contains a set of nodes that are staking.

func NewValidators

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

func (*Validators) Has

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

func (*Validators) Sample

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

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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