Documentation
¶
Overview ¶
Package lp2p implements auxiliary functions for go-libp2p integration in CometBFT. The name is chosen to avoid conflicts with the p2p package.
Index ¶
- Constants
- Variables
- func AddrInfoFromHostAndID(host, id string) (peer.AddrInfo, error)
- func AddressToMultiAddr(addr string, transport string) (ma.Multiaddr, error)
- func BootstrapPeersFromConfig(config config.LibP2PConfig) (map[peer.ID]BootstrapPeer, error)
- func IDFromPrivateKey(cosmosPK cmcrypto.PrivKey) (peer.ID, error)
- func IsDNSAddr(addr ma.Multiaddr) bool
- func PrivateKeyFromCosmosKey(key cmcrypto.PrivKey) (crypto.PrivKey, error)
- func ProtocolID(channelID byte) protocol.ID
- func ResourceManagerFromConfig(cfg config.LibP2PConfig) (network.ResourceManager, rcmgr.Limiter, error)
- func StreamRead(s network.Stream) ([]byte, error)
- func StreamReadClose(s network.Stream) (payload []byte, err error)
- func StreamReadSized(s network.Stream, maxSize uint64) ([]byte, error)
- func StreamReadSizedClose(s network.Stream, maxSize uint64) (payload []byte, err error)
- func StreamWrite(s network.Stream, data []byte) (int, error)
- func StreamWriteClose(s network.Stream, data []byte) (err error)
- type BootstrapPeer
- type ConnGater
- func (c *ConnGater) InterceptAccept(network.ConnMultiaddrs) bool
- func (c *ConnGater) InterceptAddrDial(pid peer.ID, _ multiaddr.Multiaddr) bool
- func (c *ConnGater) InterceptPeerDial(pid peer.ID) bool
- func (c *ConnGater) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) bool
- func (c *ConnGater) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)
- func (c *ConnGater) SetHost(host *Host)
- type ErrorTransient
- type Host
- func (h *Host) AddPeerFailureHandler(handler func(id peer.ID, err error))
- func (h *Host) AddrInfo() peer.AddrInfo
- func (h *Host) BootstrapPeer(id peer.ID) (BootstrapPeer, bool)
- func (h *Host) BootstrapPeers() map[peer.ID]BootstrapPeer
- func (h *Host) EmitPeerFailure(id peer.ID, err error)
- func (h *Host) Logger() log.Logger
- func (h *Host) Ping(ctx context.Context, addrInfo peer.AddrInfo) (time.Duration, error)
- type Peer
- func (p *Peer) AddrInfo() peer.AddrInfo
- func (p *Peer) CloseConn() error
- func (*Peer) FlushStop()
- func (p *Peer) Get(key string) any
- func (*Peer) GetRemovalFailed() bool
- func (p *Peer) ID() p2p.ID
- func (*Peer) IsOutbound() bool
- func (p *Peer) IsPersistent() bool
- func (p *Peer) IsPrivate() bool
- func (p *Peer) IsUnconditional() bool
- func (p *Peer) NodeInfo() p2p.NodeInfo
- func (p *Peer) RemoteAddr() net.Addr
- func (p *Peer) RemoteIP() net.IP
- func (p *Peer) Send(e p2p.Envelope) bool
- func (p *Peer) Set(key string, value any)
- func (*Peer) SetRemovalFailed()
- func (p *Peer) SocketAddr() *p2p.NetAddress
- func (*Peer) Status() conn.ConnectionStatus
- func (p *Peer) String() string
- func (p *Peer) TrySend(e p2p.Envelope) bool
- type PeerAddOptions
- type PeerRemovalOptions
- type PeerSet
- func (ps *PeerSet) Add(addrInfo peer.AddrInfo, opts PeerAddOptions) (*Peer, error)
- func (ps *PeerSet) Copy() []p2p.Peerdeprecated
- func (ps *PeerSet) ForEach(fn func(p2p.Peer))
- func (ps *PeerSet) Get(key p2p.ID) p2p.Peer
- func (ps *PeerSet) Has(key p2p.ID) bool
- func (ps *PeerSet) Random() p2p.Peerdeprecated
- func (ps *PeerSet) Remove(key p2p.ID, opts PeerRemovalOptions) error
- func (ps *PeerSet) RemoveAll(opts PeerRemovalOptions)
- func (ps *PeerSet) Size() int
- type Switch
- func (s *Switch) AddPersistentPeers(addrs []string) error
- func (s *Switch) AddPrivatePeerIDs(ids []string) error
- func (s *Switch) AddReactor(name string, reactor p2p.Reactor) p2p.Reactor
- func (s *Switch) AddUnconditionalPeerIDs(ids []string) error
- func (s *Switch) Broadcast(e p2p.Envelope) chan bool
- func (s *Switch) BroadcastAsync(e p2p.Envelope)
- func (s *Switch) DialPeerWithAddress(_ *p2p.NetAddress) error
- func (s *Switch) DialPeersAsync(peers []string) error
- func (s *Switch) IsDialingOrExistingAddress(addr *p2p.NetAddress) bool
- func (s *Switch) IsPeerPersistent(netAddr *p2p.NetAddress) bool
- func (s *Switch) IsPeerUnconditional(id p2p.ID) bool
- func (s *Switch) Log() log.Logger
- func (s *Switch) MarkPeerAsGood(_ p2p.Peer)
- func (s *Switch) MaxNumOutboundPeers() int
- func (s *Switch) NodeInfo() p2p.NodeInfo
- func (s *Switch) NumPeers() (outbound, inbound, dialing int)
- func (s *Switch) OnStart() error
- func (s *Switch) OnStop()
- func (s *Switch) Peers() p2p.IPeerSet
- func (s *Switch) Reactor(name string) (p2p.Reactor, bool)
- func (s *Switch) RemoveReactor(_ string, _ p2p.Reactor)
- func (s *Switch) StopPeerForError(peer p2p.Peer, reason any)
- func (s *Switch) StopPeerGracefully(_ p2p.Peer)
- func (s *Switch) TryBroadcast(e p2p.Envelope)
- type SwitchReactor
Constants ¶
const MaxReconnectBackoff = 5 * time.Minute
const MaxStreamSize = 4 * (1 << 20)
MaxStreamSize is the global maximum size of a stream. Protocols should configure their own maximum size.
const ProtocolIDPrefix = "/p2p/cometbft/1.0.0"
ProtocolIDPrefix is the prefix for all protocol IDs.
const TimeoutStream = 10 * time.Second
TimeoutStream is the timeout for a stream.
const TransportQUIC = "quic-v1"
TransportQUIC quic transport. @see https://docs.libp2p.io/concepts/transports/quic
Variables ¶
var ( ErrPeerExists = errors.New("peer already exists") ErrSelfPeer = errors.New("peer is self") )
var ErrUnsupportedPeerFormat = errors.New("unsupported peer format")
Functions ¶
func AddressToMultiAddr ¶
AddressToMultiAddr converts a `listenAddress` to a multiaddr for the given transport Currently, only QUIC is supported. Example: "tcp://1.1.1.1:5678" yields to "/ip4/1.1.1.1/udp/5678/quic-v1"
func BootstrapPeersFromConfig ¶
func BootstrapPeersFromConfig(config config.LibP2PConfig) (map[peer.ID]BootstrapPeer, error)
func PrivateKeyFromCosmosKey ¶
PrivateKeyFromCosmosKey converts a Cosmos private key to a libp2p private key.
func ProtocolID ¶
ProtocolID returns the protocol ID for a given channel Byte is used for compatibility with the original CometBFT implementation.
func ResourceManagerFromConfig ¶
func ResourceManagerFromConfig(cfg config.LibP2PConfig) (network.ResourceManager, rcmgr.Limiter, error)
ResourceManagerFromConfig creates a resource manager from the given config.
func StreamRead ¶
StreamRead reads payload from a stream. It doesn't control stream's lifecycle, so it's up to the caller to close the stream. Note: this method doesn't enforce any size limits! Use StreamReadSized instead.
func StreamReadClose ¶
StreamReadClose reads payload from a stream and closes it right after. Also, resets the stream on both ends in case of error.
func StreamReadSized ¶
StreamReadSized reads payload from a stream with a maximum size. It doesn't control stream's lifecycle, so it's up to the caller to close the stream.
func StreamReadSizedClose ¶
StreamReadSizedClose reads payload from a stream and closes it right after with a maximum size. Also, resets the stream on both ends in case of error.
func StreamWrite ¶
StreamWrite sends payload over a stream w/o waiting for a response. Only guarantees that the recipient will receive the bytes (no "message processed" guarantee). It doesn't control stream's lifecycle, so it's up to the caller to close the stream.
Types ¶
type BootstrapPeer ¶
type BootstrapPeer struct {
AddrInfo peer.AddrInfo
Private bool
Persistent bool
Unconditional bool
}
BootstrapPeer initial peers to connect to
type ConnGater ¶
type ConnGater struct {
// contains filtered or unexported fields
}
ConnGater limits the number of simultaneously connected peers. It is only enabled when `lp2p.limits.mode = "custom"` and uses `lp2p.limits.max_peers` as the cap.
The host is injected after host creation because libp2p requires the connection gater option during `libp2p.New(...)`, before the host exists.
func ConnectionGaterFromConfig ¶
func ConnectionGaterFromConfig(cfg config.LibP2PConfig, host *Host) (*ConnGater, bool)
ConnectionGaterFromConfig creates a connection gater from the given config or returns false if disabled.
func (*ConnGater) InterceptAccept ¶
func (c *ConnGater) InterceptAccept(network.ConnMultiaddrs) bool
InterceptAccept is called when a peer attempts to connect. It returns false to reject the connection if the peer count has reached max_peers.
func (*ConnGater) InterceptAddrDial ¶
func (*ConnGater) InterceptSecured ¶
func (*ConnGater) InterceptUpgraded ¶
type ErrorTransient ¶
type ErrorTransient struct {
Err error
}
ErrorTransient is an error that is transient and can be retried.
func TransientErrorFromAny ¶
func TransientErrorFromAny(v any) (*ErrorTransient, bool)
func (*ErrorTransient) Error ¶
func (e *ErrorTransient) Error() string
func (*ErrorTransient) Unwrap ¶
func (e *ErrorTransient) Unwrap() error
type Host ¶
Host is a wrapper around the libp2p host. Note that host should NOT be responsible for high-level peer management as it's Switch's responsibility.
func (*Host) AddPeerFailureHandler ¶
func (*Host) BootstrapPeer ¶
func (h *Host) BootstrapPeer(id peer.ID) (BootstrapPeer, bool)
func (*Host) BootstrapPeers ¶
func (h *Host) BootstrapPeers() map[peer.ID]BootstrapPeer
func (*Host) EmitPeerFailure ¶
EmitPeerFailure emits a peer failure event to all registered handlers. This semantic is over host.eventBus for simplicity.
type Peer ¶
type Peer struct {
service.BaseService
// contains filtered or unexported fields
}
Peer represents a remote node connected via libp2p. It implements p2p.Peer interface and wraps the libp2p connection with CometBFT-specific peer attributes and messaging capabilities.
func (*Peer) AddrInfo ¶
AddrInfo returns original addr info. Note it might differ from host's peerstore
func (*Peer) GetRemovalFailed ¶
func (*Peer) IsOutbound ¶
IsOutbound returns true because all lp2p peers are bi-directional.
func (*Peer) IsPersistent ¶
func (*Peer) IsUnconditional ¶
func (*Peer) NodeInfo ¶
NodeInfo returns a DefaultNodeInfo populated with the peer's ID and address. Since libp2p does not perform a CometBFT-style handshake, only the fields derivable from the connection are filled in (ID, listen address).
func (*Peer) RemoteAddr ¶
RemoteAddr returns the remote address of the peer as a net.Addr.
func (*Peer) RemoteIP ¶
RemoteIP returns the remote IP address of the peer derived from its address info.
func (*Peer) SetRemovalFailed ¶
func (*Peer) SetRemovalFailed()
func (*Peer) SocketAddr ¶
func (p *Peer) SocketAddr() *p2p.NetAddress
func (*Peer) Status ¶
func (*Peer) Status() conn.ConnectionStatus
Status returns an empty ConnectionStatus. Per-channel send queue statistics are not available with the libp2p transport.
type PeerAddOptions ¶
type PeerAddOptions struct {
Private bool
Persistent bool
Unconditional bool
OnBeforeStart func(p *Peer)
OnAfterStart func(p *Peer)
OnStartFailed func(p *Peer, reason any)
}
PeerAddOptions options for adding a peer to the PeerSet. It includes behavioral flags and lifecycle callbacks for peer initialization.
type PeerRemovalOptions ¶
PeerRemovalOptions options for removing a peer from the PeerSet. If OnAfterStop is provided, it will be called after the peer is stopped. Note that Reason is any due to backwards compatibility.
type PeerSet ¶
type PeerSet struct {
// contains filtered or unexported fields
}
PeerSet represents a single entrypoint for managing Peer's lifecycle. Note that PeerSet DOESN'T manage networking (e.g. opening/closing connections).
func NewPeerSet ¶
NewPeerSet manager peers for a given switch
func (*PeerSet) Add ¶
Add adds a new peer to the peer set. Fails if: - peer is already present - peer is self - peer has no addresses
func (*PeerSet) RemoveAll ¶
func (ps *PeerSet) RemoveAll(opts PeerRemovalOptions)
type Switch ¶
type Switch struct {
service.BaseService
// contains filtered or unexported fields
}
Switch represents p2p.Switcher alternative implementation based on go-libp2p. todo add comments to exported methods todo group unused methods
func NewSwitch ¶
func NewSwitch( nodeInfo p2p.NodeInfo, host *Host, reactors []SwitchReactor, metrics *p2p.Metrics, logger log.Logger, ) (*Switch, error)
NewSwitch constructs a new Switch.
func (*Switch) AddPersistentPeers ¶
func (*Switch) AddPrivatePeerIDs ¶
func (*Switch) AddReactor ¶
AddReactor adds the given reactor to the switch. NOTE: Not goroutine safe.
func (*Switch) AddUnconditionalPeerIDs ¶
func (*Switch) BroadcastAsync ¶
func (*Switch) DialPeerWithAddress ¶
func (s *Switch) DialPeerWithAddress(_ *p2p.NetAddress) error
func (*Switch) DialPeersAsync ¶
func (*Switch) IsDialingOrExistingAddress ¶
func (s *Switch) IsDialingOrExistingAddress(addr *p2p.NetAddress) bool
func (*Switch) IsPeerPersistent ¶
func (s *Switch) IsPeerPersistent(netAddr *p2p.NetAddress) bool
func (*Switch) MarkPeerAsGood ¶
func (*Switch) MaxNumOutboundPeers ¶
func (*Switch) StopPeerGracefully ¶
func (*Switch) TryBroadcast ¶
type SwitchReactor ¶
SwitchReactor is a pair of name and reactor. Preserves order when adding.