mirror of
https://github.com/luxfi/p2p.git
synced 2026-07-28 08:11:52 +00:00
main
p2p
The p2p package provides a low-level networking abstraction for peer-to-peer communication in the Lux ecosystem.
Installation
go get github.com/luxfi/p2p
Core Interfaces
Handler
The Handler interface defines how incoming messages are processed:
type Handler interface {
// Gossip handles an incoming gossip message
Gossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)
// Request handles an incoming request and returns a response or error
Request(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *Error)
}
Sender
The Sender interface defines how outgoing messages are sent:
type Sender interface {
SendRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, request []byte) error
SendResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error
SendError(ctx context.Context, nodeID ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error
SendGossip(ctx context.Context, config SendConfig, msg []byte) error
}
Error
The Error type represents application-level errors:
type Error struct {
Code int32
Message string
}
Standard errors:
ErrUnexpected(-1): Generic unexpected errorErrUnregisteredHandler(-2): No handler registered for protocolErrNotValidator(-3): Requesting peer is not a validatorErrThrottled(-4): Request rate limit exceeded
Sub-packages
gossip
Implements gossip protocols for efficient data propagation:
import "github.com/luxfi/p2p/gossip"
// Create a push gossiper
gossiper := gossip.NewPushGossiper[MyGossipable](...)
// Start gossiping
gossiper.Gossip(ctx)
lp118
Implements LP-118 warp message signature handling:
import "github.com/luxfi/p2p/lp118"
// Create a signature aggregator
aggregator := lp118.NewSignatureAggregator(log, client)
// Aggregate signatures for a warp message
msg, stake, total, err := aggregator.AggregateSignatures(ctx, message, justification, validators, quorumNum, quorumDen)
Usage with warp
The warp package re-exports types from p2p for backward compatibility:
import "github.com/luxfi/warp"
// warp.Error is an alias for p2p.Error
// warp.Sender is an alias for p2p.Sender
// warp.SendConfig is an alias for p2p.SendConfig
License
See LICENSE file.
Languages
Go
100%