p2p

package
v0.7.0-rc6 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientParameters

type ClientParameters struct {
	// the target minimum amount of responses with the same chain head
	MinResponses int
	// MaxRequestSize defines the max amount of headers that can be handled at once.
	MaxRequestSize uint64 // TODO: Rename to MaxRangeRequestSize
	// MaxHeadersPerRequest defines the max amount of headers that can be requested per 1 request.
	MaxHeadersPerRequest uint64 // TODO: Rename to MaxHeadersPerRangeRequest
	// MaxAwaitingTime specifies the duration that gives to the disconnected peer to be back online,
	// otherwise it will be removed on the next GC cycle.
	MaxAwaitingTime time.Duration
	// DefaultScore specifies the score for newly connected peers.
	DefaultScore float32
	// RequestTimeout defines a timeout after which the session will try to re-request headers
	// from another peer.
	RequestTimeout time.Duration // TODO: Rename to RangeRequestTimeout
	// TrustedPeersRequestTimeout a timeout for any request to a trusted peer.
	TrustedPeersRequestTimeout time.Duration
	// MaxTrackerSize specifies the max amount of peers that can be added to the peerTracker.
	MaxPeerTrackerSize int
	// contains filtered or unexported fields
}

ClientParameters is the set of parameters that must be configured for the exchange. TODO: #1667

func DefaultClientParameters

func DefaultClientParameters() ClientParameters

DefaultClientParameters returns the default params to configure the store.

func (*ClientParameters) Validate

func (p *ClientParameters) Validate() error

type Exchange

type Exchange[H header.Header] struct {
	Params ClientParameters
	// contains filtered or unexported fields
}

Exchange enables sending outbound HeaderRequests to the network as well as handling inbound HeaderRequests from the network.

func NewExchange

func NewExchange[H header.Header](
	host host.Host,
	peers peer.IDSlice,
	connGater *conngater.BasicConnectionGater,
	opts ...Option[ClientParameters],
) (*Exchange[H], error)

func (*Exchange[H]) Get

func (ex *Exchange[H]) Get(ctx context.Context, hash header.Hash) (H, error)

Get performs a request for the Header by the given hash corresponding to the RawHeader. Note that the Header must be verified thereafter.

func (*Exchange[H]) GetByHeight

func (ex *Exchange[H]) GetByHeight(ctx context.Context, height uint64) (H, error)

GetByHeight performs a request for the Header at the given height to the network. Note that the Header must be verified thereafter.

func (*Exchange[H]) GetRangeByHeight

func (ex *Exchange[H]) GetRangeByHeight(ctx context.Context, from, amount uint64) ([]H, error)

GetRangeByHeight performs a request for the given range of Headers to the network. Note that the Headers must be verified thereafter.

func (*Exchange[H]) GetVerifiedRange

func (ex *Exchange[H]) GetVerifiedRange(
	ctx context.Context,
	from H,
	amount uint64,
) ([]H, error)

GetVerifiedRange performs a request for the given range of Headers to the network and ensures that returned headers are correct against the passed one.

func (*Exchange[H]) Head

func (ex *Exchange[H]) Head(ctx context.Context) (H, error)

Head requests the latest Header. Note that the Header must be verified thereafter. NOTE: It is fine to continue handling head request if the timeout will be reached. As we are requesting head from multiple trusted peers, we may already have some headers when the timeout will be reached.

func (*Exchange[H]) Start

func (ex *Exchange[H]) Start(context.Context) error

func (*Exchange[H]) Stop

func (ex *Exchange[H]) Stop(context.Context) error

type ExchangeServer

type ExchangeServer[H header.Header] struct {
	Params ServerParameters
	// contains filtered or unexported fields
}

ExchangeServer represents the server-side component for responding to inbound header-related requests.

func NewExchangeServer

func NewExchangeServer[H header.Header](
	host host.Host,
	store header.Store[H],
	opts ...Option[ServerParameters],
) (*ExchangeServer[H], error)

NewExchangeServer returns a new P2P server that handles inbound header-related requests.

func (*ExchangeServer[H]) Start

func (serv *ExchangeServer[H]) Start(context.Context) error

Start sets the stream handler for inbound header-related requests.

func (*ExchangeServer[H]) Stop

func (serv *ExchangeServer[H]) Stop(context.Context) error

Stop removes the stream handler for serving header-related requests.

type Option

type Option[T parameters] func(*T)

Option is the functional option that is applied to the exchange instance to configure parameters.

func WithDefaultScore

func WithDefaultScore[T ClientParameters](score float32) Option[T]

WithDefaultScore is a functional option that configures the `DefaultScore` parameter.

func WithMaxAwaitingTime

func WithMaxAwaitingTime[T ClientParameters](duration time.Duration) Option[T]

WithMaxAwaitingTime is a functional option that configures the `MaxAwaitingTime` parameter.

func WithMaxHeadersPerRequest

func WithMaxHeadersPerRequest[T ClientParameters](amount uint64) Option[T]

WithMaxHeadersPerRequest is a functional option that configures the // `MaxRequestSize` parameter.

func WithMaxRequestSize

func WithMaxRequestSize[T parameters](size uint64) Option[T]

WithMaxRequestSize is a functional option that configures the `MaxRequestSize` parameter.

func WithMaxTrackerSize

func WithMaxTrackerSize[T ClientParameters](size int) Option[T]

WithMaxTrackerSize is a functional option that configures the `MaxTrackerSize` parameter.

func WithMinResponses

func WithMinResponses[T ClientParameters](responses int) Option[T]

WithMinResponses is a functional option that configures the `MinResponses` parameter.

func WithProtocolSuffix

func WithProtocolSuffix[T parameters](protocolSuffix string) Option[T]

WithProtocolSuffix is a functional option that configures the `protocolSuffix` parameter.

func WithReadDeadline

func WithReadDeadline[T ServerParameters](deadline time.Duration) Option[T]

WithReadDeadline is a functional option that configures the `WithReadDeadline` parameter.

func WithRequestTimeout

func WithRequestTimeout[T parameters](duration time.Duration) Option[T]

WithRequestTimeout is a functional option that configures the `RequestTimeout` parameter.

func WithWriteDeadline

func WithWriteDeadline[T ServerParameters](deadline time.Duration) Option[T]

WithWriteDeadline is a functional option that configures the `WriteDeadline` parameter.

type ServerParameters

type ServerParameters struct {
	// WriteDeadline sets the timeout for sending messages to the stream
	WriteDeadline time.Duration
	// ReadDeadline sets the timeout for reading messages from the stream
	ReadDeadline time.Duration
	// MaxRequestSize defines the max amount of headers that can be handled at once.
	MaxRequestSize uint64
	// RequestTimeout defines a timeout after which the session will try to re-request headers
	// from another peer.
	RequestTimeout time.Duration
	// contains filtered or unexported fields
}

ServerParameters is the set of parameters that must be configured for the exchange.

func DefaultServerParameters

func DefaultServerParameters() ServerParameters

DefaultServerParameters returns the default params to configure the store.

func (*ServerParameters) Validate

func (p *ServerParameters) Validate() error

type Subscriber

type Subscriber[H header.Header] struct {
	// contains filtered or unexported fields
}

Subscriber manages the lifecycle and relationship of header Module with the "header-sub" gossipsub topic.

func NewSubscriber

func NewSubscriber[H header.Header](
	ps *pubsub.PubSub,
	msgID pubsub.MsgIdFunction,
	protocolSuffix string,
) *Subscriber[H]

NewSubscriber returns a Subscriber that manages the header Module's relationship with the "header-sub" gossipsub topic.

func (*Subscriber[H]) AddValidator

func (p *Subscriber[H]) AddValidator(val func(context.Context, H) pubsub.ValidationResult) error

AddValidator applies basic pubsub validator for the topic.

func (*Subscriber[H]) Broadcast

func (p *Subscriber[H]) Broadcast(ctx context.Context, header H, opts ...pubsub.PubOpt) error

Broadcast broadcasts the given Header to the topic.

func (*Subscriber[H]) Start

func (p *Subscriber[H]) Start(context.Context) (err error)

Start starts the Subscriber, registering a topic validator for the "header-sub" topic and joining it.

func (*Subscriber[H]) Stop

func (p *Subscriber[H]) Stop(context.Context) error

Stop closes the topic and unregisters its validator.

func (*Subscriber[H]) Subscribe

func (p *Subscriber[H]) Subscribe() (header.Subscription[H], error)

Subscribe returns a new subscription to the Subscriber's topic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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