Documentation ¶
Index ¶
- Constants
- Variables
- func ResponseFuncFromEnvelope(channel *Client, envelope *p2p.Envelope) func(ctx context.Context, msg proto.Message) error
- type BlockClient
- type Client
- func (c *Client) Consume(ctx context.Context, params ConsumerParams) error
- func (c *Client) GetBlock(ctx context.Context, height int64, peerID types.NodeID) (*promise.Promise[*bcproto.BlockResponse], error)
- func (c *Client) GetChunk(ctx context.Context, peerID types.NodeID, height uint64, version uint32, ...) (*promise.Promise[*statesync.ChunkResponse], error)
- func (c *Client) GetLightBlock(ctx context.Context, peerID types.NodeID, height uint64) (*promise.Promise[*statesync.LightBlockResponse], error)
- func (c *Client) GetParams(ctx context.Context, peerID types.NodeID, height uint64) (*promise.Promise[*statesync.ParamsResponse], error)
- func (c *Client) GetSnapshots(ctx context.Context, peerID types.NodeID) error
- func (c *Client) GetSyncStatus(ctx context.Context) error
- func (c *Client) Send(ctx context.Context, msg any) error
- func (c *Client) SendTxs(ctx context.Context, peerID types.NodeID, tx ...types.Tx) error
- type ConsumerHandler
- type ConsumerMiddlewareFunc
- type ConsumerParams
- type OptionFunc
- type Sender
- type SnapshotClient
- type TxSender
Constants ¶
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 ¶
var ( ErrPeerNotResponded = errors.New("peer did not send us anything") ErrCannotResolveResponse = errors.New("cannot resolve a result") )
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 ¶
GetSnapshots requests snapshots from a peer
func (*Client) GetSyncStatus ¶
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
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 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