client

package
v0.14.0-dev.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestIDAttribute is used to provide unique request-id value
	RequestIDAttribute = "RequestID"
	// ResponseIDAttribute is used to provide response-id that should be taken from received request-id
	ResponseIDAttribute = "ResponseID"
)

These attributes should use as a key in Envelope.Attributes map

Variables

View Source
var (
	ErrPeerNotResponded      = errors.New("peer did not send us anything")
	ErrCannotResolveResponse = errors.New("cannot resolve a result")
)
View Source
var (
	ErrRequestIDAttributeRequired  = errors.New("envelope requestID attribute is required")
	ErrResponseIDAttributeRequired = errors.New("envelope responseID attribute is required")
)

Functions

func ResponseFuncFromEnvelope

func ResponseFuncFromEnvelope(channel *Client, envelope *p2p.Envelope) func(ctx context.Context, msg proto.Message) error

ResponseFuncFromEnvelope creates a response function that is taken some parameters from received envelope to make the valid message that will be sent back to the peer

Types

type BlockClient

type BlockClient interface {
	Sender
	// GetBlock is the method that requests a block by a specific height from a peer.
	// Since the request is asynchronous, then the method returns a promise that will be resolved
	// as a response will be received or rejected by timeout, otherwise returns an error
	GetBlock(ctx context.Context, height int64, peerID types.NodeID) (*promise.Promise[*bcproto.BlockResponse], error)
	// GetSyncStatus requests a block synchronization status from all connected peers
	GetSyncStatus(ctx context.Context) error
}

BlockClient defines the methods which must be implemented by block client

type Client

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

Client is a stateful implementation of a client, which means that the client stores a request ID in order to be able to resolve the response once it is received from the peer

func New

func New(descriptors map[p2p.ChannelID]*p2p.ChannelDescriptor, creator p2p.ChannelCreator, opts ...OptionFunc) *Client

New creates and returns Client with optional functions

func (*Client) Consume

func (c *Client) Consume(ctx context.Context, params ConsumerParams) error

Consume reads the messages from a p2p client and processes them using a consumer-handler

func (*Client) GetBlock

func (c *Client) GetBlock(ctx context.Context, height int64, peerID types.NodeID) (*promise.Promise[*bcproto.BlockResponse], error)

GetBlock requests a block from a peer and returns promise.Promise which resolve the result if response received in time otherwise reject

func (*Client) GetChunk

func (c *Client) GetChunk(
	ctx context.Context,
	peerID types.NodeID,
	height uint64,
	version uint32,
	chunkID []byte,
) (*promise.Promise[*statesync.ChunkResponse], error)

GetChunk requests a chunk from a peer and returns promise.Promise which resolve the result

func (*Client) GetLightBlock

func (c *Client) GetLightBlock(
	ctx context.Context,
	peerID types.NodeID,
	height uint64,
) (*promise.Promise[*statesync.LightBlockResponse], error)

GetLightBlock returns a promise.Promise which resolve the result if response received in time otherwise reject

func (*Client) GetParams

func (c *Client) GetParams(
	ctx context.Context,
	peerID types.NodeID,
	height uint64,
) (*promise.Promise[*statesync.ParamsResponse], error)

GetParams returns a promise.Promise which resolve the result if response received in time otherwise reject

func (*Client) GetSnapshots

func (c *Client) GetSnapshots(ctx context.Context, peerID types.NodeID) error

GetSnapshots requests snapshots from a peer

func (*Client) GetSyncStatus

func (c *Client) GetSyncStatus(ctx context.Context) error

GetSyncStatus requests a block synchronization status from all connected peers Since this is broadcast request, we can't use promise to process a response instead, we should be able to process the response as a normal message in the handler

func (*Client) Send

func (c *Client) Send(ctx context.Context, msg any) error

Send sends p2p message to a peer, allowed p2p.Envelope or p2p.PeerError types

func (*Client) SendTxs

func (c *Client) SendTxs(ctx context.Context, peerID types.NodeID, tx types.Tx) error

SendTxs sends a transaction to the peer

type ConsumerHandler

type ConsumerHandler interface {
	Handle(ctx context.Context, client *Client, envelope *p2p.Envelope) error
}

ConsumerHandler is the interface that wraps a Handler method. This interface must be implemented by the p2p message handler and must be used in conjunction with the p2p consumer.

func HandlerWithMiddlewares

func HandlerWithMiddlewares(handler ConsumerHandler, mws ...ConsumerMiddlewareFunc) ConsumerHandler

HandlerWithMiddlewares is a function that wraps a handler in middlewares

type ConsumerMiddlewareFunc

type ConsumerMiddlewareFunc func(next ConsumerHandler) ConsumerHandler

ConsumerMiddlewareFunc is used to wrap ConsumerHandler to provide the ability to do something before or after the handler execution

func WithErrorLoggerMiddleware

func WithErrorLoggerMiddleware(logger log.Logger) ConsumerMiddlewareFunc

WithErrorLoggerMiddleware creates error logging middleware

func WithRecoveryMiddleware

func WithRecoveryMiddleware(logger log.Logger) ConsumerMiddlewareFunc

WithRecoveryMiddleware creates panic recovery middleware

func WithValidateMessageHandler

func WithValidateMessageHandler(allowedChannelIDs []p2p.ChannelID) ConsumerMiddlewareFunc

WithValidateMessageHandler creates message validation middleware

type ConsumerParams

type ConsumerParams struct {
	ReadChannels []p2p.ChannelID
	Handler      ConsumerHandler
}

ConsumerParams is p2p handler parameters set

type OptionFunc

type OptionFunc func(c *Client)

OptionFunc is a client optional function, it is used to override the default parameters in a Client

func WithChanIDResolver

func WithChanIDResolver(resolver func(msg proto.Message) p2p.ChannelID) OptionFunc

WithChanIDResolver is an option function to set channel ID resolver function

func WithClock

func WithClock(clock clockwork.Clock) OptionFunc

WithClock is an optional function to set clock to Client

func WithLogger

func WithLogger(logger log.Logger) OptionFunc

WithLogger is an optional function to set logger to Client

type Sender

type Sender interface {
	Send(ctx context.Context, msg any) error
}

Sender is the interface that wraps Send method

type SnapshotClient

type SnapshotClient interface {
	// GetSnapshots requests a list of available snapshots from a peer without handling the response.
	// The snapshots will be sent by peer asynchronously and should be received by reading the channel separately.
	// The method returns an error if the request is not possible to send to the peer.
	GetSnapshots(ctx context.Context, peerID types.NodeID) error
	// GetChunk requests a snapshot chunk from a peer and returns a promise.Promise which will be resolved
	// as a response will be received or rejected by timeout, otherwise returns an error
	GetChunk(
		ctx context.Context,
		peerID types.NodeID,
		height uint64,
		format uint32,
		index uint32,
	) (*promise.Promise[*statesync.ChunkResponse], error)
	// GetParams requests a snapshot params from a peer.
	// The method returns a promise.Promise which will be resolved.
	GetParams(
		ctx context.Context,
		peerID types.NodeID,
		height uint64,
	) (*promise.Promise[*statesync.ParamsResponse], error)
	// GetLightBlock requests a light block from a peer.
	// The method returns a promise.Promise which will be resolved.
	GetLightBlock(
		ctx context.Context,
		peerID types.NodeID,
		height uint64,
	) (*promise.Promise[*statesync.LightBlockResponse], error)
}

SnapshotClient defines the methods which must be implemented by snapshot client

type TxSender

type TxSender interface {
	// SendTxs sends a transaction to a peer
	SendTxs(ctx context.Context, peerID types.NodeID, tx types.Tx) error
}

TxSender is the interface that wraps SendTxs method

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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