Documentation
¶
Index ¶
- Constants
- func DialPeers(peers []string, r *SeedNodeReactor) []error
- func NewBaseReactor(name string, impl Reactor) *p2p.BaseReactor
- type AddrBook
- type Channel
- type ChannelDescriptor
- type ChannelStatus
- type ConnFilterFunc
- type ConnectionStatus
- type IPResolver
- type IPeerSet
- type MConnConfig
- type MConnection
- func (c *MConnection) CanSend(chID byte) bool
- func (c *MConnection) FlushStop()
- func (c *MConnection) OnStart() error
- func (c *MConnection) OnStop()
- func (c *MConnection) Send(chID byte, msgBytes []byte) bool
- func (c *MConnection) SetLogger(l log.Logger)
- func (c *MConnection) Status() ConnectionStatus
- func (c *MConnection) String() string
- func (c *MConnection) TrySend(chID byte, msgBytes []byte) bool
- type Metrics
- type MultiplexTransport
- func (mt *MultiplexTransport) Accept(cfg peerConfig) (Peer, error)
- func (mt *MultiplexTransport) AddChannel(chID byte)
- func (mt *MultiplexTransport) Cleanup(p Peer)
- func (mt *MultiplexTransport) Close() error
- func (mt *MultiplexTransport) Dial(addr p2p.NetAddress, cfg peerConfig) (Peer, error)
- func (mt *MultiplexTransport) Listen(addr p2p.NetAddress) error
- func (mt *MultiplexTransport) NetAddress() p2p.NetAddress
- type MultiplexTransportOption
- type Peer
- type PeerFilterFunc
- type PeerOption
- type PeerSet
- func (ps *PeerSet) Add(peer Peer) error
- func (ps *PeerSet) Clear()
- func (ps *PeerSet) Get(peerKey p2p.ID) Peer
- func (ps *PeerSet) Has(peerKey p2p.ID) bool
- func (ps *PeerSet) HasIP(peerIP net.IP) bool
- func (ps *PeerSet) List() []Peer
- func (ps *PeerSet) Remove(peer Peer) bool
- func (ps *PeerSet) Size() int
- type Reactor
- type SeedNodeReactor
- func (br *SeedNodeReactor) AddPeer(p Peer)
- func (br *SeedNodeReactor) GetChannels() []*ChannelDescriptor
- func (*SeedNodeReactor) InitPeer(peer Peer) Peer
- func (br *SeedNodeReactor) Receive(chID byte, src Peer, msgBytes []byte)
- func (*SeedNodeReactor) RemovePeer(Peer, interface{})
- func (br *SeedNodeReactor) RequestAddrs(p Peer)
- func (br *SeedNodeReactor) SetSwitch(sw *Switch)
- type Switch
- func (sw *Switch) AddPersistentPeers(addrs []string) error
- func (sw *Switch) AddPrivatePeerIDs(ids []string) error
- func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor
- func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error
- func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool
- func (sw *Switch) DialPeerWithAddress(addr *p2p.NetAddress) error
- func (sw *Switch) GetPersistentPeers() *PeerSet
- func (sw *Switch) IsDialingOrExistingAddress(addr *p2p.NetAddress) bool
- func (sw *Switch) IsPeerPersistent(na *p2p.NetAddress) bool
- func (sw *Switch) IsPeerUnconditional(id p2p.ID) bool
- func (sw *Switch) MarkPeerAsGood(peer Peer)
- func (sw *Switch) MaxNumOutboundPeers() int
- func (sw *Switch) NetAddress() *p2p.NetAddress
- func (sw *Switch) NodeInfo() p2p.NodeInfo
- func (sw *Switch) NumPeers() (outbound, inbound, dialing int)
- func (sw *Switch) OnStart() error
- func (sw *Switch) OnStop()
- func (sw *Switch) Peers() IPeerSet
- func (sw *Switch) Reactor(name string) Reactor
- func (sw *Switch) Reactors() map[string]Reactor
- func (sw *Switch) RemoveReactor(name string, reactor Reactor)
- func (sw *Switch) SetAddrBook(addrBook AddrBook)
- func (sw *Switch) SetNodeInfo(nodeInfo p2p.NodeInfo)
- func (sw *Switch) SetNodeKey(nodeKey *p2p.NodeKey)
- func (sw *Switch) StopPeerForError(peer Peer, reason interface{})
- type SwitchOption
- type Transport
Constants ¶
const ( // MetricsSubsystem is a subsystem shared by all metrics exposed by this // package. MetricsSubsystem = "p2p" )
Variables ¶
This section is empty.
Functions ¶
func DialPeers ¶
func DialPeers(peers []string, r *SeedNodeReactor) []error
DialPeers Dials a peer and retrieve the resulting seed list
func NewBaseReactor ¶
func NewBaseReactor(name string, impl Reactor) *p2p.BaseReactor
Types ¶
type AddrBook ¶
type AddrBook interface { AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error AddPrivateIDs([]string) AddOurAddress(*p2p.NetAddress) OurAddress(*p2p.NetAddress) bool MarkGood(id p2p.ID) RemoveAddress(*p2p.NetAddress) HasAddress(*p2p.NetAddress) bool Save() }
An AddrBook represents an address book from the pex package, which is used to store peer addresses.
type ChannelDescriptor ¶
type ChannelDescriptor struct { ID byte Priority int SendQueueCapacity int RecvBufferCapacity int RecvMessageCapacity int }
func (ChannelDescriptor) FillDefaults ¶
func (chDesc ChannelDescriptor) FillDefaults() (filled ChannelDescriptor)
type ChannelStatus ¶
type ConnFilterFunc ¶
ConnFilterFunc to be implemented by filter hooks after a new connection has been established. The set of exisiting connections is passed along together with all resolved IPs for the new connection.
type ConnectionStatus ¶
type IPResolver ¶
IPResolver is a behaviour subset of net.Resolver.
type IPeerSet ¶
type IPeerSet interface { Has(key p2p.ID) bool HasIP(ip net.IP) bool Get(key p2p.ID) Peer List() []Peer Size() int }
IPeerSet has a (immutable) subset of the methods of PeerSet.
type MConnConfig ¶
type MConnConfig struct { SendRate int64 `mapstructure:"send_rate"` RecvRate int64 `mapstructure:"recv_rate"` // Maximum payload size MaxPacketMsgPayloadSize int `mapstructure:"max_packet_msg_payload_size"` // Interval to flush writes (throttled) FlushThrottle time.Duration `mapstructure:"flush_throttle"` // Interval to send pings PingInterval time.Duration `mapstructure:"ping_interval"` // Maximum wait time for pongs PongTimeout time.Duration `mapstructure:"pong_timeout"` }
MConnConfig is a MConnection configuration.
func DefaultMConnConfig ¶
func DefaultMConnConfig() MConnConfig
DefaultMConnConfig returns the default config.
func NewMConnConfig ¶
func NewMConnConfig(cfg *config.P2PConfig) MConnConfig
NewMConnConfig returns an MConnConfig with fields updated from the P2PConfig.
type MConnection ¶
type MConnection struct { service.BaseService // contains filtered or unexported fields }
MConnection Each peer has one `MConnection` (multiplex connection) instance.
__multiplex__ *noun* a system or signal involving simultaneous transmission of several messages along a single channel of communication.
Each `MConnection` handles message transmission on multiple abstract communication `Channel`s. Each channel has a globally unique byte id. The byte id and the relative priorities of each `Channel` are configured upon initialization of the connection.
There are two methods for sending messages:
func (m MConnection) Send(chID byte, msgBytes []byte) bool {} func (m MConnection) TrySend(chID byte, msgBytes []byte}) bool {}
`Send(chID, msgBytes)` is a blocking call that waits until `msg` is successfully queued for the channel with the given id byte `chID`, or until the request times out. The message `msg` is serialized using Protobuf.
`TrySend(chID, msgBytes)` is a nonblocking call that returns false if the channel's queue is full.
Inbound message bytes are handled with an onReceive callback function.
func NewMConnection ¶
func NewMConnection( conn net.Conn, chDescs []*ChannelDescriptor, onReceive receiveCbFunc, onError errorCbFunc, ) *MConnection
NewMConnection wraps net.Conn and creates multiplex connection
func NewMConnectionWithConfig ¶
func NewMConnectionWithConfig( conn net.Conn, chDescs []*ChannelDescriptor, onReceive receiveCbFunc, onError errorCbFunc, config MConnConfig, ) *MConnection
NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config
func (*MConnection) CanSend ¶
func (c *MConnection) CanSend(chID byte) bool
CanSend returns true if you can send more data onto the chID, false otherwise. Use only as a heuristic.
func (*MConnection) FlushStop ¶
func (c *MConnection) FlushStop()
FlushStop replicates the logic of OnStop. It additionally ensures that all successful .Send() calls will get flushed before closing the connection.
func (*MConnection) Send ¶
func (c *MConnection) Send(chID byte, msgBytes []byte) bool
Queues a message to be sent to channel.
func (*MConnection) SetLogger ¶
func (c *MConnection) SetLogger(l log.Logger)
func (*MConnection) Status ¶
func (c *MConnection) Status() ConnectionStatus
func (*MConnection) String ¶
func (c *MConnection) String() string
type Metrics ¶
type Metrics struct { // Number of peers received from a given seed. SeedReceivePeers metrics.Gauge }
Metrics contains metrics exposed by this package.
func PrometheusMetrics ¶
PrometheusMetrics returns Metrics build using Prometheus client library. Optionally, labels can be provided along with their values ("foo", "fooValue").
type MultiplexTransport ¶
type MultiplexTransport struct {
// contains filtered or unexported fields
}
MultiplexTransport accepts and dials tcp connections and upgrades them to multiplexed peers.
func NewMultiplexTransport ¶
func NewMultiplexTransport( nodeInfo p2p.NodeInfo, nodeKey p2p.NodeKey, mConfig MConnConfig, ) *MultiplexTransport
NewMultiplexTransport returns a tcp connected multiplexed peer.
func (*MultiplexTransport) Accept ¶
func (mt *MultiplexTransport) Accept(cfg peerConfig) (Peer, error)
Accept implements Transport.
func (*MultiplexTransport) AddChannel ¶
func (mt *MultiplexTransport) AddChannel(chID byte)
AddChannel registers a channel to nodeInfo. NOTE: NodeInfo must be of type DefaultNodeInfo else channels won't be updated This is a bit messy at the moment but is cleaned up in the following version when NodeInfo changes from an interface to a concrete type
func (*MultiplexTransport) Cleanup ¶
func (mt *MultiplexTransport) Cleanup(p Peer)
Cleanup removes the given address from the connections set and closes the connection.
func (*MultiplexTransport) Close ¶
func (mt *MultiplexTransport) Close() error
Close implements transportLifecycle.
func (*MultiplexTransport) Dial ¶
func (mt *MultiplexTransport) Dial( addr p2p.NetAddress, cfg peerConfig, ) (Peer, error)
Dial implements Transport.
func (*MultiplexTransport) Listen ¶
func (mt *MultiplexTransport) Listen(addr p2p.NetAddress) error
Listen implements transportLifecycle.
func (*MultiplexTransport) NetAddress ¶
func (mt *MultiplexTransport) NetAddress() p2p.NetAddress
NetAddress implements Transport.
type MultiplexTransportOption ¶
type MultiplexTransportOption func(*MultiplexTransport)
MultiplexTransportOption sets an optional parameter on the MultiplexTransport.
type Peer ¶
type Peer interface { service.Service FlushStop() ID() p2p.ID // peer's cryptographic ID RemoteIP() net.IP // remote IP of the connection RemoteAddr() net.Addr // remote address of the connection IsOutbound() bool // did we dial the peer IsPersistent() bool // do we redial this peer when we disconnect CloseConn() error // close original connection NodeInfo() p2p.NodeInfo // peer's info Status() ConnectionStatus SocketAddr() *p2p.NetAddress // actual address of the socket Send(byte, []byte) bool TrySend(byte, []byte) bool Set(string, interface{}) Get(string) interface{} // GetMetrics -------------- Additional attributes for the metrics -----------// GetMetrics() *Metrics }
Peer is an interface representing a peer connected on a reactor.
type PeerFilterFunc ¶
PeerFilterFunc to be implemented by filter hooks after a new Peer has been fully setup.
type PeerOption ¶
type PeerOption func(*peer)
type PeerSet ¶
type PeerSet struct {
// contains filtered or unexported fields
}
PeerSet is a special structure for keeping a table of peers. Iteration over the peers is super fast and thread-safe.
func NewPeerSet ¶
func NewPeerSet() *PeerSet
NewPeerSet creates a new peerSet with a list of initial capacity of 256 items.
func (*PeerSet) Add ¶
Add adds the peer to the PeerSet. It returns an error carrying the reason, if the peer is already present.
func (*PeerSet) Get ¶
Get looks up a peer by the provided peerKey. Returns nil if peer is not found.
func (*PeerSet) Has ¶
Has returns true if the set contains the peer referred to by this peerKey, otherwise false.
func (*PeerSet) HasIP ¶
HasIP returns true if the set contains the peer referred to by this IP address, otherwise false.
type SeedNodeReactor ¶
type SeedNodeReactor struct { p2p.BaseReactor Switch *Switch }
func (*SeedNodeReactor) AddPeer ¶
func (br *SeedNodeReactor) AddPeer(p Peer)
AddPeer normally implements Reactor by adding peer to the address book (if inbound) or by requesting more addresses (if outbound). This version only request addressed
func (*SeedNodeReactor) GetChannels ¶
func (br *SeedNodeReactor) GetChannels() []*ChannelDescriptor
GetChannels implements Reactor
func (*SeedNodeReactor) InitPeer ¶
func (*SeedNodeReactor) InitPeer(peer Peer) Peer
func (*SeedNodeReactor) Receive ¶
func (br *SeedNodeReactor) Receive(chID byte, src Peer, msgBytes []byte)
func (*SeedNodeReactor) RemovePeer ¶
func (*SeedNodeReactor) RemovePeer(Peer, interface{})
func (*SeedNodeReactor) RequestAddrs ¶
func (br *SeedNodeReactor) RequestAddrs(p Peer)
RequestAddrs asks peer for more addresses if we do not already have a request out for this peer.
func (*SeedNodeReactor) SetSwitch ¶
func (br *SeedNodeReactor) SetSwitch(sw *Switch)
type Switch ¶
type Switch struct { service.BaseService // contains filtered or unexported fields }
Switch handles peer connections and exposes an API to receive incoming messages on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one or more `Channels`. So while sending outgoing messages is typically performed on the peer, incoming messages are received on the reactor.
func NewSwitch ¶
func NewSwitch( cfg *config.P2PConfig, transport Transport, options ...SwitchOption, ) *Switch
NewSwitch creates a new Switch with the given config.
func (*Switch) AddPersistentPeers ¶
AddPersistentPeers allows you to set persistent peers. It ignores ErrNetAddressLookup. However, if there are other errors, first encounter is returned.
func (*Switch) AddPrivatePeerIDs ¶
func (*Switch) AddReactor ¶
AddReactor adds the given reactor to the switch. NOTE: Not goroutine safe.
func (*Switch) AddUnconditionalPeerIDs ¶
func (*Switch) Broadcast ¶
Broadcast runs a go routine for each attempted send, which will block trying to send for defaultSendTimeoutSeconds. Returns a channel which receives success values for each attempted send (false if times out). Channel will be closed once msg bytes are sent to all peers (or time out).
NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
func (*Switch) DialPeerWithAddress ¶
func (sw *Switch) DialPeerWithAddress(addr *p2p.NetAddress) error
DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. If we're currently dialing this address or it belongs to an existing peer, ErrCurrentlyDialingOrExistingAddress is returned.
func (*Switch) GetPersistentPeers ¶
func (*Switch) IsDialingOrExistingAddress ¶
func (sw *Switch) IsDialingOrExistingAddress(addr *p2p.NetAddress) bool
IsDialingOrExistingAddress returns true if switch has a peer with the given address or dialing it at the moment.
func (*Switch) IsPeerPersistent ¶
func (sw *Switch) IsPeerPersistent(na *p2p.NetAddress) bool
func (*Switch) MarkPeerAsGood ¶
MarkPeerAsGood marks the given peer as good when it did something useful like contributed to consensus.
func (*Switch) MaxNumOutboundPeers ¶
MaxNumOutboundPeers returns a maximum number of outbound peers.
func (*Switch) NetAddress ¶
func (sw *Switch) NetAddress() *p2p.NetAddress
NetAddress returns the address the switch is listening on.
func (*Switch) NumPeers ¶
NumPeers returns the count of outbound/inbound and outbound-dialing peers. unconditional peers are not counted here.
func (*Switch) OnStop ¶
func (sw *Switch) OnStop()
OnStop implements BaseService. It stops all peers and reactors.
func (*Switch) Reactors ¶
Reactors returns a map of reactors registered on the switch. NOTE: Not goroutine safe.
func (*Switch) RemoveReactor ¶
RemoveReactor removes the given Reactor from the Switch. NOTE: Not goroutine safe.
func (*Switch) SetAddrBook ¶
SetAddrBook allows to set address book on Switch.
func (*Switch) SetNodeInfo ¶
SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes. NOTE: Not goroutine safe.
func (*Switch) SetNodeKey ¶
SetNodeKey sets the switch's private key for authenticated encryption. NOTE: Not goroutine safe.
func (*Switch) StopPeerForError ¶
StopPeerForError disconnects from a peer due to external error.
type SwitchOption ¶
type SwitchOption func(*Switch)
SwitchOption sets an optional parameter on the Switch.
type Transport ¶
type Transport interface { NetAddress() p2p.NetAddress // Accept returns a newly connected Peer. Accept(peerConfig) (Peer, error) // Dial connects to the Peer for the address. Dial(p2p.NetAddress, peerConfig) (Peer, error) // Cleanup any resources associated with Peer. Cleanup(Peer) }
Transport emits and connects to Peers. The implementation of Peer is left to the transport. Each transport is also responsible to filter establishing peers specific to its domain.