Documentation
¶
Index ¶
- Constants
- Variables
- func ParseMessage(msg []byte) (uint64, []byte, bool)
- func PrefixMessage(prefix, msg []byte) []byte
- func ProtocolPrefix(handlerID uint64) []byte
- type Client
- func (c *Client) Gossip(ctx context.Context, config p2p.SendConfig, gossipBytes []byte) error
- func (c *Client) Request(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestBytes []byte, ...) error
- func (c *Client) RequestAny(ctx context.Context, requestBytes []byte, onResponse ResponseCallback) error
- type ClientOption
- type Error
- type Handler
- type Network
- func (n *Network) AddHandler(handlerID uint64, handler Handler) error
- func (n *Network) Connected(_ context.Context, nodeID ids.NodeID, _ *consensusversion.Application) error
- func (n *Network) Disconnected(_ context.Context, nodeID ids.NodeID) error
- func (n *Network) Gossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error
- func (n *Network) NewClient(handlerID uint64, options ...ClientOption) *Client
- func (n *Network) Request(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, ...) ([]byte, *p2p.Error)
- func (n *Network) RequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, err *p2p.Error) error
- func (n *Network) Response(ctx context.Context, nodeID ids.NodeID, requestID uint32, msg []byte) error
- type NoOpHandler
- type NodeSampler
- type PeerSampler
- type PeerTracker
- func (p *PeerTracker) Connected(nodeID ids.NodeID, nodeVersion *version.Application)
- func (p *PeerTracker) Disconnected(nodeID ids.NodeID)
- func (p *PeerTracker) RegisterFailure(nodeID ids.NodeID)
- func (p *PeerTracker) RegisterRequest(nodeID ids.NodeID)
- func (p *PeerTracker) RegisterResponse(nodeID ids.NodeID, bandwidth float64)
- func (p *PeerTracker) SelectPeer() (ids.NodeID, bool)
- func (p *PeerTracker) Size() int
- type Peers
- type ResponseCallback
- type SendConfig
- type Sender
- type SlidingWindowThrottler
- type TestHandler
- type Throttler
- type ThrottlerHandler
- type ValidatorHandler
- type ValidatorSet
- type ValidatorSubset
- type Validators
Constants ¶
const ( TxGossipHandlerID = iota AtomicTxGossipHandlerID // SignatureRequestHandlerID is specified in LP-118: https://github.com/luxfi/LPs/tree/main/LPs/118-warp-signature-request SignatureRequestHandlerID )
Standardized identifiers for application protocol handlers
Variables ¶
var ( ErrRequestPending = errors.New("request pending") ErrNoPeers = errors.New("no peers") )
var ( ErrUnexpected = p2p.ErrUnexpected ErrUnregisteredHandler = p2p.ErrUnregisteredHandler ErrNotValidator = p2p.ErrNotValidator ErrThrottled = p2p.ErrThrottled )
Re-export error types from p2p package
var ( ErrExistingAppProtocol = errors.New("existing app protocol") ErrUnrequestedResponse = errors.New("unrequested response") )
Functions ¶
func ParseMessage ¶
Parse a gossip or request message.
Returns: - The protocol ID. - The unprefixed protocol message. - A boolean indicating that parsing succeeded.
func PrefixMessage ¶
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 ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) Request ¶ added in v1.16.56
func (c *Client) Request( ctx context.Context, nodeIDs set.Set[ids.NodeID], requestBytes []byte, onResponse ResponseCallback, ) error
Request issues an arbitrary request to a node. [onResponse] is invoked upon an error or a response.
func (*Client) RequestAny ¶ added in v1.16.56
func (c *Client) RequestAny( ctx context.Context, requestBytes []byte, onResponse ResponseCallback, ) error
RequestAny issues a request to an arbitrary node decided by Client. If a specific node needs to be requested, use Request instead. See Request for more docs.
type ClientOption ¶
type ClientOption interface {
// contains filtered or unexported methods
}
ClientOption configures Client
func WithValidatorSampling ¶
func WithValidatorSampling(validators *Validators) ClientOption
WithValidatorSampling configures Client.AppRequestAny to sample validators
type Handler ¶
Handler is the server-side logic for virtual machine application protocols. This is an alias for p2p.Handler.
type Network ¶
type Network struct {
Peers *Peers
// contains filtered or unexported fields
}
Network exposes networking state and supports building p2p application protocols
func NewNetwork ¶
func NewNetwork( log log.Logger, sender p2p.Sender, registerer metric.Registerer, namespace string, ) (*Network, error)
NewNetwork returns an instance of Network
func (*Network) AddHandler ¶
AddHandler reserves an identifier for an application protocol
func (*Network) Connected ¶
func (n *Network) Connected(_ context.Context, nodeID ids.NodeID, _ *consensusversion.Application) error
func (*Network) Disconnected ¶
func (*Network) NewClient ¶
func (n *Network) NewClient(handlerID uint64, options ...ClientOption) *Client
NewClient returns a Client that can be used to send messages for the corresponding protocol.
func (*Network) Request ¶ added in v1.16.56
func (n *Network) Request(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, msg []byte) ([]byte, *p2p.Error)
Request handles an incoming request
type NoOpHandler ¶
type NoOpHandler struct{}
NoOpHandler drops all messages
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 PeerTracker ¶
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 ¶
func NewPeerTracker( log log.Logger, metricsNamespace string, registerer metric.Registerer, ignoredNodes set.Set[ids.NodeID], minVersion *version.Application, ) (*PeerTracker, error)
func (*PeerTracker) Connected ¶
func (p *PeerTracker) Connected(nodeID ids.NodeID, nodeVersion *version.Application)
Connected should be called when [nodeID] connects to this node.
func (*PeerTracker) Disconnected ¶
func (p *PeerTracker) Disconnected(nodeID ids.NodeID)
Disconnected should be called when [nodeID] disconnects from this node.
func (*PeerTracker) RegisterFailure ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (p *PeerTracker) Size() int
Returns the number of peers the node is connected to.
type Peers ¶
type Peers struct {
// contains filtered or unexported fields
}
Peers contains metadata about the current set of connected peers
type ResponseCallback ¶ added in v1.16.56
type ResponseCallback func( ctx context.Context, nodeID ids.NodeID, responseBytes []byte, err error, )
ResponseCallback is called upon receiving a response to a request issued by Client. Callers should check [err] to see whether the request failed or not.
type SendConfig ¶ added in v1.16.56
type SendConfig = p2p.SendConfig
SendConfig is an alias for p2p.SendConfig
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 TestHandler ¶
type TestHandler struct {
GossipF func(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)
RequestF func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *p2p.Error)
}
type ThrottlerHandler ¶
type ThrottlerHandler struct {
// contains filtered or unexported fields
}
func NewThrottlerHandler ¶
func NewThrottlerHandler(handler Handler, throttler Throttler, log log.Logger) *ThrottlerHandler
type ValidatorHandler ¶
type ValidatorHandler struct {
// contains filtered or unexported fields
}
ValidatorHandler drops messages from non-validators
func NewValidatorHandler ¶
func NewValidatorHandler( handler Handler, validatorSet ValidatorSet, log log.Logger, ) *ValidatorHandler
type ValidatorSet ¶
type ValidatorSubset ¶
type Validators ¶
type Validators struct {
// contains filtered or unexported fields
}
Validators contains a set of nodes that are staking.
func NewValidators ¶
func NewValidators( peers *Peers, log log.Logger, netID ids.ID, validators validators.State, maxValidatorSetStaleness time.Duration, ) *Validators