Documentation
¶
Overview ¶
Package p2p implements P2P protocol using libp2p library.
Index ¶
- Constants
- Variables
- type AddrInfo
- type Config
- type Connection
- type Discovery
- type Event
- type EventHandler
- type ExtendedConnection
- func (ec *ExtendedConnection) ConnsToPeer(pid PeerID) []network.Conn
- func (ec *ExtendedConnection) Info() *AddrInfo
- func (ec *ExtendedConnection) Listen(addrs []ma.Multiaddr) error
- func (ec *ExtendedConnection) NewStream(ctx context.Context, pid PeerID, pids ...protocol.ID) (network.Stream, error)
- func (ec *ExtendedConnection) SetStreamHandler(protocolID protocol.ID, hander network.StreamHandler)
- func (ec *ExtendedConnection) StartGossipSub(ctx context.Context, options ...pubsub.Option) error
- func (ec *ExtendedConnection) SwarmClear(pid PeerID) bool
- type GossipSub
- type Message
- func (e *Message) Decode(data []byte) error
- func (e *Message) DecodeFromReader(reader *codec.Reader) error
- func (e *Message) DecodeStrict(data []byte) error
- func (e *Message) DecodeStrictFromReader(reader *codec.Reader) error
- func (e *Message) Encode() []byte
- func (e *Message) MustDecode(data []byte)
- type MessageProtocol
- func (mp *MessageProtocol) Broadcast(ctx context.Context, procedure string, data []byte) error
- func (mp *MessageProtocol) RegisterRPCHandler(name string, handler RPCHandler, opts ...RPCHandlerOption) error
- func (mp *MessageProtocol) RequestFrom(ctx context.Context, peerID PeerID, procedure string, data []byte) Response
- type Peer
- func (p *Peer) BlacklistedPeers() []AddrInfo
- func (p *Peer) Connect(ctx context.Context, peer AddrInfo) error
- func (p *Peer) ConnectedPeers() PeerIDs
- func (p *Peer) Disconnect(peer PeerID) error
- func (p *Peer) ID() PeerID
- func (p *Peer) MultiAddress() ([]string, error)
- func (p *Peer) Ping(ctx context.Context, peer PeerID) (rtt time.Duration, err error)
- func (p *Peer) PingMultiTimes(ctx context.Context, peer PeerID) (rtt []time.Duration, err error)
- type PeerID
- type PeerIDs
- type RPCHandler
- type RPCHandlerOption
- type Request
- func (e *Request) Decode(data []byte) error
- func (e *Request) DecodeFromReader(reader *codec.Reader) error
- func (e *Request) DecodeStrict(data []byte) error
- func (e *Request) DecodeStrictFromReader(reader *codec.Reader) error
- func (e *Request) Encode() []byte
- func (e *Request) MustDecode(data []byte)
- type Response
- type ResponseWriter
- type ValidationResult
- type Validator
Constants ¶
const ( // ValidationAccept is a validation decision that indicates a valid message that should be accepted and // delivered to the application and forwarded to the network. ValidationAccept = ValidationResult(0) // ValidationReject is a validation decision that indicates an invalid message that should not be // delivered to the application or forwarded to the application. Furthermore the peer that forwarded // the message should be penalized. ValidationReject = ValidationResult(1) // ValidationIgnore is a validation decision that indicates a message that should be ignored: it will // be neither delivered to the application nor forwarded to the network. ValidationIgnore = ValidationResult(2) )
const ( ConnectionSecurityNone = "none" // Do not support any security. ConnectionSecurityTLS = "tls" // Support TLS connections. ConnectionSecurityNoise = "noise" // Support Noise connections. )
Connection security option type.
const (
MaxPenaltyScore = 100 // When a peer exceeded the MaxPenaltyScore, it should be banned.
)
Variables ¶
Functions ¶
This section is empty.
Types ¶
type AddrInfo ¶
func AddrInfoFromMultiAddr ¶
AddrInfoFromMultiAddr returns a peer info from multi address as string.
type Config ¶
type Config struct { Version string Addresses []string ConnectionSecurity string EnableNATService bool EnableUsingRelayService bool EnableRelayService bool EnableHolePunching bool SeedPeers []string FixedPeers []string BlacklistedIPs []string MinNumOfConnections int MaxNumOfConnections int // GossipSub configuration IsSeedPeer bool ChainID codec.Hex }
type Connection ¶
type Connection struct { *MessageProtocol *Peer *GossipSub // contains filtered or unexported fields }
Connection - a connection to p2p network.
func NewConnection ¶
func NewConnection(logger log.Logger, cfg *Config) *Connection
NewConnection creates a new P2P instance.
func (*Connection) ApplyPenalty ¶
func (conn *Connection) ApplyPenalty(pid PeerID, score int)
ApplyPenalty updates the score of the given PeerID (all its IP addresses) and bans the peer if the score exceeded. Also disconnected the peer immediately.
func (*Connection) BanPeer ¶
func (conn *Connection) BanPeer(pid PeerID)
BanPeer bans the given PeerID (all its IP addresses) and disconnects the peer immediately.
func (*Connection) Start ¶
func (conn *Connection) Start(seed []byte) error
Start the P2P and all other related services and handlers.
func (*Connection) Stop ¶
func (conn *Connection) Stop() error
Stop the connection to the P2P network.
func (*Connection) Version ¶
func (conn *Connection) Version() string
Version returns network version set for the protocol.
type Discovery ¶
type Discovery struct {
// contains filtered or unexported fields
}
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event holds event message from a peer.
type EventHandler ¶
type EventHandler func(event *Event)
EventHandler is a handler function for event received from a peer.
type ExtendedConnection ¶
type ExtendedConnection struct {
*Connection
}
ExtendedConnection extends the Connection for some extra utilities functions.
func NewExtendedConnection ¶
func NewExtendedConnection(logger log.Logger, cfg *Config) *ExtendedConnection
NewExtendedConnection returns a new ExtendedConnection.
func (*ExtendedConnection) ConnsToPeer ¶
func (ec *ExtendedConnection) ConnsToPeer(pid PeerID) []network.Conn
ConnToPeer returns the connections in the Network of the host/node for given peerID.
func (*ExtendedConnection) Info ¶
func (ec *ExtendedConnection) Info() *AddrInfo
Info returns an AddrInfo struct with the ID of the host/node and all of its Addrs.
func (*ExtendedConnection) Listen ¶
func (ec *ExtendedConnection) Listen(addrs []ma.Multiaddr) error
Listen tells the network of the host/node to start listening on given multiaddrs. It will be available with an empty addresses of the config in NewRawConnection.
func (*ExtendedConnection) NewStream ¶
func (ec *ExtendedConnection) NewStream(ctx context.Context, pid PeerID, pids ...protocol.ID) (network.Stream, error)
NewSteam opens a new stream to given peer in the host/node.
func (*ExtendedConnection) SetStreamHandler ¶
func (ec *ExtendedConnection) SetStreamHandler(protocolID protocol.ID, hander network.StreamHandler)
SetStreamHandler sets a new handler for given protocolID in the host/node.
func (*ExtendedConnection) StartGossipSub ¶
StartGossipSub starts a new gossipsub based on input options.
func (*ExtendedConnection) SwarmClear ¶
func (ec *ExtendedConnection) SwarmClear(pid PeerID) bool
SwarmClear returns true and removes a backoff record if the Network of the host/node has the given peerID.
type GossipSub ¶
type GossipSub struct {
// contains filtered or unexported fields
}
GossipSub type.
func (*GossipSub) RegisterEventHandler ¶
func (gs *GossipSub) RegisterEventHandler(name string, handler EventHandler, validator Validator) error
RegisterEventHandler registers an event handler for an event type.
type Message ¶
type Message struct { Timestamp int64 `json:"timestamp"` // Unix time when the message was received. Data []byte `fieldNumber:"1" json:"data"` // Message data (payload). }
Message is a message type sent to other peers in the network over GossipSub.
func (*Message) DecodeStrict ¶
func (*Message) DecodeStrictFromReader ¶
func (*Message) MustDecode ¶
type MessageProtocol ¶
type MessageProtocol struct {
// contains filtered or unexported fields
}
MessageProtocol type.
func (*MessageProtocol) Broadcast ¶
Broadcast sends a request message to all connected peers using a message protocol.
func (*MessageProtocol) RegisterRPCHandler ¶
func (mp *MessageProtocol) RegisterRPCHandler(name string, handler RPCHandler, opts ...RPCHandlerOption) error
RegisterRPCHandler registers a new RPC handler function.
func (*MessageProtocol) RequestFrom ¶
func (mp *MessageProtocol) RequestFrom(ctx context.Context, peerID PeerID, procedure string, data []byte) Response
RequestFrom sends a request message to a peer using a message protocol.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer type - a p2p node.
func (*Peer) BlacklistedPeers ¶
BlacklistedPeers returns a list of blacklisted peers and their addresses.
func (*Peer) Connect ¶
Connect to a peer using AddrInfo. Direct connection is discouraged. Manually connected peer must be manually disconnected.
func (*Peer) ConnectedPeers ¶
ConnectedPeers returns a list of all connected peers IDs.
func (*Peer) MultiAddress ¶
MultiAddress returns a peers's listen addresses.
type RPCHandler ¶
type RPCHandler func(w ResponseWriter, req *Request)
RPCHandler is a handler function for RPC request received from a peer.
type RPCHandlerOption ¶
type RPCHandlerOption func(*MessageProtocol, string) error
func WithRPCMessageCounter ¶
func WithRPCMessageCounter(limit, penalty int) RPCHandlerOption
WithRPCMessageCounter sets a rate limit for a specific RPC message handler.
type Request ¶
type Request struct { ID string `fieldNumber:"1" json:"id"` // Message ID. Procedure string `fieldNumber:"2" json:"procedure"` // Procedure to be called. Data []byte `fieldNumber:"3" json:"data"` // Request data. Timestamp int64 `json:"timestamp"` // Unix time when the message was received. PeerID PeerID `json:"peerID"` // ID of peer that created the request message. }
Request is a request message type sent to other peer.
func (*Request) DecodeStrict ¶
func (*Request) DecodeStrictFromReader ¶
func (*Request) MustDecode ¶
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Event holds event message from a peer.
func NewResponse ¶
NewResponse creates a new Response struct.
type ResponseWriter ¶
ResponseWriter is an interface for handler to write.
type ValidationResult ¶
type ValidationResult = pubsub.ValidationResult
ValidationResult represents the decision of a validator.