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) SendN(ctx context.Context, msg any, nTokens int) error
- func (c *Client) SendTxs(ctx context.Context, peerID types.NodeID, tx ...types.Tx) error
- type ConsumerHandler
- type ConsumerMiddlewareFunc
- func WithErrorLoggerMiddleware(logger log.Logger) ConsumerMiddlewareFunc
- func WithRecoveryMiddleware(logger log.Logger) ConsumerMiddlewareFunc
- func WithRecvRateLimitPerPeerHandler(ctx context.Context, limit float64, nTokensFunc TokenNumberFunc, drop bool, ...) ConsumerMiddlewareFunc
- func WithValidateMessageHandler(allowedChannelIDs []p2p.ChannelID) ConsumerMiddlewareFunc
- type ConsumerParams
- type OptionFunc
- type RateLimit
- type Sender
- type SnapshotClient
- type TokenNumberFunc
- 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
const DefaultRecvBurstMultiplier = 10
DefaultRecvBurstMultiplier tells how many times burst is bigger than the limit in recvRateLimitPerPeerHandler
const PeerRateLimitLifetime = 60 // number of seconds to keep the rate limiter for a peer
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 WithRecvRateLimitPerPeerHandler ¶ added in v1.0.0
func WithRecvRateLimitPerPeerHandler(ctx context.Context, limit float64, nTokensFunc TokenNumberFunc, drop bool, logger log.Logger) ConsumerMiddlewareFunc
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
func WithSendRateLimits ¶ added in v1.0.0
func WithSendRateLimits(rateLimit *RateLimit, channels ...p2p.ChannelID) OptionFunc
WithSendRateLimits defines a rate limiter for the provided channels.
Provided rate limiter will be shared between provided channels. Use this function multiple times to set different rate limiters for different channels.
type RateLimit ¶ added in v1.0.0
type RateLimit struct {
// contains filtered or unexported fields
}
RateLimit is a rate limiter for p2p messages. It is used to limit the rate of incoming messages from a peer. Each peer has its own independent limit.
Use NewRateLimit to create a new rate limiter. Use [Limit()] to wait for the rate limit to allow the message to be sent.
func NewRateLimit ¶ added in v1.0.0
NewRateLimit creates a new rate limiter.
Arguments ¶
* `ctx` - context; used to gracefully shutdown the garbage collection routine * `limit` - rate limit per peer per second; 0 means no limit * `drop` - silently drop the message if the rate limit is exceeded; otherwise we will wait until the message is allowed * `logger` - logger
func (*RateLimit) Limit ¶ added in v1.0.0
func (h *RateLimit) Limit(ctx context.Context, peerID types.NodeID, nTokens int) (allowed bool, err error)
Limit waits for the rate limit to allow the message to be sent. It returns true if the message is allowed, false otherwise.
If peerID is empty, messages is always allowed.
Returns true when the message is allowed, false if it should be dropped.
Arguments: - ctx: context - peerID: peer ID; if empty, the message is always allowed - nTokens: number of tokens to consume; use 1 if unsure
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 TokenNumberFunc ¶ added in v1.0.0
TokenNumberFunc is a function that returns number of tokens to consume for a given envelope