Documentation ¶
Overview ¶
Package p2p implements the Klaytn p2p network protocols.
Index ¶
- Constants
- Variables
- func ConvertConnType(nt discover.NodeType) common.ConnType
- func ConvertConnTypeToString(ct common.ConnType) string
- func ConvertNodeType(ct common.ConnType) discover.NodeType
- func ConvertStringToConnType(s string) common.ConnType
- func ExpectMsg(r MsgReader, code uint64, content interface{}) error
- func MsgPipe() (*MsgPipeRW, *MsgPipeRW)
- func Send(w MsgWriter, msgcode uint64, data interface{}) error
- func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error
- type BaseServer
- func (srv *BaseServer) AddLastLookup() time.Time
- func (srv *BaseServer) AddPeer(node *discover.Node)
- func (srv *BaseServer) AddProtocols(p []Protocol)
- func (srv *BaseServer) CheckNilNetworkTable() bool
- func (srv *BaseServer) Dial(dest *discover.Node) (net.Conn, error)
- func (srv *BaseServer) DialMulti(dest *discover.Node) ([]net.Conn, error)
- func (srv *BaseServer) Disconnect(destID discover.NodeID)
- func (srv *BaseServer) GetListenAddress() []string
- func (srv *BaseServer) GetNodes(nType discover.NodeType, max int) []*discover.Node
- func (srv *BaseServer) GetProtocols() []Protocol
- func (srv *BaseServer) Lookup(target discover.NodeID, nType discover.NodeType) []*discover.Node
- func (srv *BaseServer) MaxPeers() int
- func (srv *BaseServer) Name() string
- func (srv *BaseServer) NodeInfo() *NodeInfo
- func (srv *BaseServer) PeerCount() int
- func (srv *BaseServer) PeerCountByType() map[string]uint
- func (srv *BaseServer) Peers() []*Peer
- func (srv *BaseServer) PeersInfo() []*PeerInfo
- func (srv *BaseServer) RemovePeer(node *discover.Node)
- func (srv *BaseServer) Resolve(target discover.NodeID, nType discover.NodeType) *discover.Node
- func (srv *BaseServer) Self() *discover.Node
- func (srv *BaseServer) SetLastLookupToNow()
- func (srv *BaseServer) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error
- func (srv *BaseServer) Start() (err error)
- func (srv *BaseServer) Stop()
- func (srv *BaseServer) SubscribeEvents(ch chan *PeerEvent) event.Subscription
- type Cap
- type Config
- type DiscReason
- type ErrorPeer
- type Msg
- type MsgPipeRW
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type MultiChannelServer
- type NetworkInfo
- type NodeDialer
- type NodeInfo
- type Peer
- func (p *Peer) Caps() []Cap
- func (p *Peer) ConnType() common.ConnType
- func (p *Peer) Disconnect(reason DiscReason)
- func (p *Peer) GetNumberInboundAndOutbound() (int, int)
- func (p *Peer) ID() discover.NodeID
- func (p *Peer) Inbound() bool
- func (p *Peer) Info() *PeerInfo
- func (p *Peer) LocalAddr() net.Addr
- func (p *Peer) Log() log.Logger
- func (p *Peer) Name() string
- func (p *Peer) RemoteAddr() net.Addr
- func (p *Peer) String() string
- type PeerEvent
- type PeerEventType
- type PeerInfo
- type PeerTypeValidator
- type PortOrder
- type Protocol
- type RWTimerConfig
- type Server
- type SingleChannelServer
- type TCPDialer
Examples ¶
Constants ¶
const ( DT_UNLIMITED = dialType("DIAL_TYPE_UNLIMITED") DT_CN = dialType("CN") DT_PN = dialType("PN") DT_EN = dialType("EN") )
const ( ConnDefault = iota ConnTxMsg )
Variables ¶
var ErrPipeClosed = errors.New("p2p: read or write on closed message pipe")
ErrPipeClosed is returned from pipe operations after the pipe has been closed.
Functions ¶
func ConvertConnTypeToString ¶
func ConvertStringToConnType ¶
func ExpectMsg ¶
ExpectMsg reads a message from r and verifies that its code and encoded RLP content match the provided values. If content is nil, the payload is discarded and not verified.
func MsgPipe ¶
MsgPipe creates a message pipe. Reads on one end are matched with writes on the other. The pipe is full-duplex, both ends implement MsgReadWriter.
Example ¶
rw1, rw2 := MsgPipe() go func() { Send(rw1, 8, [][]byte{{0, 0}}) Send(rw1, 5, [][]byte{{1, 1}}) rw1.Close() }() for { msg, err := rw2.ReadMsg() if err != nil { break } var data [][]byte msg.Decode(&data) fmt.Printf("msg: %d, %x\n", msg.Code, data[0]) }
Output: msg: 8, 0000 msg: 5, 0101
Types ¶
type BaseServer ¶
type BaseServer struct { // Config fields may not be modified while the server is running. Config // contains filtered or unexported fields }
BaseServer is a common data structure used by implementation of Server.
func (*BaseServer) AddLastLookup ¶
func (srv *BaseServer) AddLastLookup() time.Time
AddLastLookup adds lastLookup to duration.
func (*BaseServer) AddPeer ¶
func (srv *BaseServer) AddPeer(node *discover.Node)
AddPeer connects to the given node and maintains the connection until the server is shut down. If the connection fails for any reason, the server will attempt to reconnect the peer.
func (*BaseServer) AddProtocols ¶
func (srv *BaseServer) AddProtocols(p []Protocol)
AddProtocols adds protocols to the server.
func (*BaseServer) CheckNilNetworkTable ¶
func (srv *BaseServer) CheckNilNetworkTable() bool
CheckNilNetworkTable returns whether network table is nil.
func (*BaseServer) Disconnect ¶
func (srv *BaseServer) Disconnect(destID discover.NodeID)
Disconnect tries to disconnect peer.
func (*BaseServer) GetListenAddress ¶
func (srv *BaseServer) GetListenAddress() []string
GetListenAddress returns the listen address of the server.
func (*BaseServer) GetProtocols ¶
func (srv *BaseServer) GetProtocols() []Protocol
GetProtocols returns a slice of protocols.
func (*BaseServer) Lookup ¶
Lookup performs a network search for nodes close to the given target. It approaches the target by querying nodes that are closer to it on each iteration. The given target does not need to be an actual node identifier.
func (*BaseServer) MaxPeers ¶
func (srv *BaseServer) MaxPeers() int
MaxPhysicalConnections returns maximum count of peers.
func (*BaseServer) NodeInfo ¶
func (srv *BaseServer) NodeInfo() *NodeInfo
NodeInfo gathers and returns a collection of metadata known about the host.
func (*BaseServer) PeerCount ¶
func (srv *BaseServer) PeerCount() int
PeerCount returns the number of connected peers.
func (*BaseServer) PeerCountByType ¶
func (srv *BaseServer) PeerCountByType() map[string]uint
func (*BaseServer) Peers ¶
func (srv *BaseServer) Peers() []*Peer
Peers returns all connected peers.
func (*BaseServer) PeersInfo ¶
func (srv *BaseServer) PeersInfo() []*PeerInfo
PeersInfo returns an array of metadata objects describing connected peers.
func (*BaseServer) RemovePeer ¶
func (srv *BaseServer) RemovePeer(node *discover.Node)
RemovePeer disconnects from the given node.
func (*BaseServer) Resolve ¶
Resolve searches for a specific node with the given ID and NodeType. It returns nil if the node could not be found.
func (*BaseServer) Self ¶
func (srv *BaseServer) Self() *discover.Node
Self returns the local node's endpoint information.
func (*BaseServer) SetLastLookupToNow ¶
func (srv *BaseServer) SetLastLookupToNow()
SetLastLookupToNow sets LastLookup to the current time.
func (*BaseServer) SetupConn ¶
SetupConn runs the handshakes and attempts to add the connection as a peer. It returns when the connection has been added as a peer or the handshakes have failed.
func (*BaseServer) Start ¶
func (srv *BaseServer) Start() (err error)
Start starts running the server. Servers can not be re-used after stopping.
func (*BaseServer) Stop ¶
func (srv *BaseServer) Stop()
Stop terminates the server and all active peer connections. It blocks until all active connections are closed.
func (*BaseServer) SubscribeEvents ¶
func (srv *BaseServer) SubscribeEvents(ch chan *PeerEvent) event.Subscription
SubscribePeers subscribes the given channel to peer events.
type Config ¶
type Config struct { // This field must be set to a valid secp256k1 private key. PrivateKey *ecdsa.PrivateKey `toml:"-"` // MaxPhysicalConnections is the maximum number of physical connections. // A peer uses one connection if single channel peer and uses two connections if // multi channel peer. It must be greater than zero. MaxPhysicalConnections int // ConnectionType is a type of connection like Consensus or Normal // described at common.ConnType // When the connection is established, each peer exchange each connection type ConnectionType common.ConnType // MaxPendingPeers is the maximum number of peers that can be pending in the // handshake phase, counted separately for inbound and outbound connections. // Zero defaults to preset values. MaxPendingPeers int `toml:",omitempty"` // DialRatio controls the ratio of inbound to dialed connections. // Example: a DialRatio of 2 allows 1/2 of connections to be dialed. // Setting DialRatio to zero defaults it to 3. DialRatio int `toml:",omitempty"` // NoDiscovery can be used to disable the peer discovery mechanism. // Disabling is useful for protocol debugging (manual topology). NoDiscovery bool // Name sets the node name of this server. // Use common.MakeName to create a name that follows existing conventions. Name string `toml:"-"` // BootstrapNodes are used to establish connectivity // with the rest of the network. BootstrapNodes []*discover.Node // Static nodes are used as pre-configured connections which are always // maintained and re-connected on disconnects. StaticNodes []*discover.Node // Trusted nodes are used as pre-configured connections which are always // allowed to connect, even above the peer limit. TrustedNodes []*discover.Node // Connectivity can be restricted to certain IP networks. // If this option is set to a non-nil value, only hosts which match one of the // IP networks contained in the list are considered. NetRestrict *netutil.Netlist `toml:",omitempty"` // NodeDatabase is the path to the database containing the previously seen // live nodes in the network. NodeDatabase string `toml:",omitempty"` // Protocols should contain the protocols supported // by the server. Matching protocols are launched for // each peer. Protocols []Protocol `toml:"-"` // If ListenAddr is set to a non-nil address, the server // will listen for incoming connections. // // If the port is zero, the operating system will pick a port. The // ListenAddr field will be updated with the actual address when // the server is started. ListenAddr string // NoListen can be used to disable the listening for incoming connections. NoListen bool // SubListenAddr is the list of the secondary listen address used for peer-to-peer connections. SubListenAddr []string // If EnableMultiChannelServer is true, multichannel can communicate with other nodes EnableMultiChannelServer bool // If set to a non-nil value, the given NAT port mapper // is used to make the listening port available to the // Internet. NAT nat.Interface `toml:",omitempty"` // If Dialer is set to a non-nil value, the given Dialer // is used to dial outbound peer connections. Dialer NodeDialer `toml:"-"` // If NoDial is true, the server will not dial any peers. NoDial bool `toml:",omitempty"` // If EnableMsgEvents is set then the server will emit PeerEvents // whenever a message is sent to or received from a peer EnableMsgEvents bool // Logger is a custom logger to use with the p2p.Server. Logger log.Logger `toml:",omitempty"` // RWTimerConfig is a configuration for interval based timer for rw. // It checks if a rw successfully writes its task in given time. RWTimerConfig RWTimerConfig // NetworkID to use for selecting peers to connect to NetworkID uint64 }
Config holds Server options.
type DiscReason ¶
type DiscReason uint
const ( DiscRequested DiscReason = iota DiscNetworkError DiscProtocolError DiscUselessPeer DiscTooManyPeers DiscAlreadyConnected DiscIncompatibleVersion DiscInvalidIdentity DiscQuitting DiscUnexpectedIdentity DiscSelf DiscReadTimeout DiscSubprotocolError = 0x10 )
func (DiscReason) Error ¶
func (d DiscReason) Error() string
func (DiscReason) String ¶
func (d DiscReason) String() string
type ErrorPeer ¶
type ErrorPeer struct {
// contains filtered or unexported fields
}
ErrorPeer is a peer error
type Msg ¶
type Msg struct { Code uint64 Size uint32 // size of the paylod Payload io.Reader ReceivedAt time.Time }
Msg defines the structure of a p2p message.
Note that a Msg can only be sent once since the Payload reader is consumed during sending. It is not possible to create a Msg and send it any number of times. If you want to reuse an encoded structure, encode the payload into a byte array and create a separate Msg with a bytes.Reader as Payload for each send.
func (Msg) Decode ¶
Decode parses the RLP content of a message into the given value, which must be a pointer.
For the decoding rules, please see package rlp.
type MsgPipeRW ¶
type MsgPipeRW struct {
// contains filtered or unexported fields
}
MsgPipeRW is an endpoint of a MsgReadWriter pipe.
func (*MsgPipeRW) Close ¶
Close unblocks any pending ReadMsg and WriteMsg calls on both ends of the pipe. They will return ErrPipeClosed. Close also interrupts any reads from a message payload.
type MsgReadWriter ¶
MsgReadWriter provides reading and writing of encoded messages. Implementations should ensure that ReadMsg and WriteMsg can be called simultaneously from multiple goroutines.
type MultiChannelServer ¶
type MultiChannelServer struct { *BaseServer ListenAddrs []string CandidateConns map[discover.NodeID][]*conn // contains filtered or unexported fields }
MultiChannelServer is a server that uses a multi channel.
func (*MultiChannelServer) GetListenAddress ¶ added in v1.3.0
func (srv *MultiChannelServer) GetListenAddress() []string
GetListenAddress returns the listen addresses of the server.
func (*MultiChannelServer) SetupConn ¶
func (srv *MultiChannelServer) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error
SetupConn runs the handshakes and attempts to add the connection as a peer. It returns when the connection has been added as a peer or the handshakes have failed.
func (*MultiChannelServer) Start ¶
func (srv *MultiChannelServer) Start() (err error)
Start starts running the MultiChannelServer. MultiChannelServer can not be re-used after stopping.
func (*MultiChannelServer) Stop ¶ added in v1.3.0
func (srv *MultiChannelServer) Stop()
Stop terminates the server and all active peer connections. It blocks until all active connections are closed.
type NetworkInfo ¶
type NetworkInfo struct { LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection Inbound bool `json:"inbound"` Trusted bool `json:"trusted"` Static bool `json:"static"` NodeType string `json:"nodeType"` }
NetworkInfo represents the connection information with the peer.
type NodeDialer ¶
type NodeDialer interface { Dial(*discover.Node) (net.Conn, error) DialMulti(*discover.Node) ([]net.Conn, error) }
NodeDialer is used to connect to nodes in the network, typically by using an underlying net.Dialer but also using net.Pipe in tests.
type NodeInfo ¶
type NodeInfo struct { ID string `json:"id"` // Unique node identifier (also the encryption key) Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Enode string `json:"kni"` // Enode URL for adding this peer from remote peers IP string `json:"ip"` // IP address of the node Ports struct { Discovery int `json:"discovery"` // UDP listening port for discovery protocol Listener int `json:"listener"` // TCP listening port for RLPx } `json:"ports"` ListenAddr string `json:"listenAddr"` Protocols map[string]interface{} `json:"protocols"` }
NodeInfo represents a short summary of the information known about the host.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a connected remote node.
func (*Peer) Disconnect ¶
func (p *Peer) Disconnect(reason DiscReason)
Disconnect terminates the peer connection with the given reason. It returns immediately and does not wait until the connection is closed.
func (*Peer) GetNumberInboundAndOutbound ¶
GetNumberInboundAndOutbound returns the number of inbound and outbound connections connected to the peer.
func (*Peer) RemoteAddr ¶
RemoteAddr returns the remote address of the network connection.
type PeerEvent ¶
type PeerEvent struct { Type PeerEventType `json:"type"` Peer discover.NodeID `json:"peer"` Error string `json:"error,omitempty"` Protocol string `json:"protocol,omitempty"` MsgCode *uint64 `json:"msg_code,omitempty"` MsgSize *uint32 `json:"msg_size,omitempty"` }
PeerEvent is an event emitted when peers are either added or dropped from a p2p.Server or when a message is sent or received on a peer connection
type PeerEventType ¶
type PeerEventType string
PeerEventType is the type of peer events emitted by a p2p.Server
const ( // PeerEventTypeAdd is the type of event emitted when a peer is added // to a p2p.Server PeerEventTypeAdd PeerEventType = "add" // PeerEventTypeDrop is the type of event emitted when a peer is // dropped from a p2p.Server PeerEventTypeDrop PeerEventType = "drop" // PeerEventTypeMsgSend is the type of event emitted when a // message is successfully sent to a peer PeerEventTypeMsgSend PeerEventType = "msgsend" // PeerEventTypeMsgRecv is the type of event emitted when a // message is received from a peer PeerEventTypeMsgRecv PeerEventType = "msgrecv" )
type PeerInfo ¶
type PeerInfo struct { ID string `json:"id"` // Unique node identifier (also the encryption key) Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Caps []string `json:"caps"` // Sum-protocols advertised by this particular peer Networks []NetworkInfo `json:"networks"` // Networks is all the NetworkInfo associated with the peer Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields }
PeerInfo represents a short summary of the information known about a connected peer. Sub-protocol independent fields are contained and initialized here, with protocol specifics delegated to all connected sub-protocols.
type PeerTypeValidator ¶
type Protocol ¶
type Protocol struct { // Name should contain the official protocol name, // often a three-letter word. Name string // Version should contain the version number of the protocol. Version uint // Length should contain the number of message codes used // by the protocol. Length uint64 // Run is called in a new groutine when the protocol has been // negotiated with a peer. It should read and write messages from // rw. The Payload for each message must be fully consumed. // // The peer connection is closed when Start returns. It should return // any protocol-level error (such as an I/O error) that is // encountered. Run func(peer *Peer, rw MsgReadWriter) error // RunWithRWs is called in a new groutine when the protocol has been // negotiated with a peer. It should read and write messages from // rws. The Payload for each message must be fully consumed. // // The peer connection is closed when Start returns. It should return // any protocol-level error (such as an I/O error) that is // encountered. RunWithRWs func(peer *Peer, rws []MsgReadWriter) error // NodeInfo is an optional helper method to retrieve protocol specific metadata // about the host node. NodeInfo func() interface{} // PeerInfo is an optional helper method to retrieve protocol specific metadata // about a certain peer in the network. If an info retrieval function is set, // but returns nil, it is assumed that the protocol handshake is still running. PeerInfo func(id discover.NodeID) interface{} }
Protocol represents a P2P subprotocol implementation.
type RWTimerConfig ¶
type Server ¶
type Server interface { // GetProtocols returns a slice of protocols. GetProtocols() []Protocol // AddProtocols adds protocols to the server. AddProtocols(p []Protocol) // SetupConn runs the handshakes and attempts to add the connection // as a peer. It returns when the connection has been added as a peer // or the handshakes have failed. SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error // AddLastLookup adds lastLookup to duration. AddLastLookup() time.Time // SetLastLookupToNow sets LastLookup to the current time. SetLastLookupToNow() // CheckNilNetworkTable returns whether network table is nil. CheckNilNetworkTable() bool // GetNodes returns up to max alive nodes which a NodeType is nType GetNodes(nType discover.NodeType, max int) []*discover.Node // Lookup performs a network search for nodes close // to the given target. It approaches the target by querying // nodes that are closer to it on each iteration. // The given target does not need to be an actual node // identifier. Lookup(target discover.NodeID, nType discover.NodeType) []*discover.Node // Resolve searches for a specific node with the given ID and NodeType. // It returns nil if the node could not be found. Resolve(target discover.NodeID, nType discover.NodeType) *discover.Node // Start starts running the server. // Servers can not be re-used after stopping. Start() (err error) // Stop terminates the server and all active peer connections. // It blocks until all active connections are closed. Stop() // AddPeer connects to the given node and maintains the connection until the // server is shut down. If the connection fails for any reason, the server will // attempt to reconnect the peer. AddPeer(node *discover.Node) // RemovePeer disconnects from the given node. RemovePeer(node *discover.Node) // SubscribePeers subscribes the given channel to peer events. SubscribeEvents(ch chan *PeerEvent) event.Subscription // PeersInfo returns an array of metadata objects describing connected peers. PeersInfo() []*PeerInfo // NodeInfo gathers and returns a collection of metadata known about the host. NodeInfo() *NodeInfo // Name returns name of server. Name() string // PeerCount returns the number of connected peers. PeerCount() int // PeerCountByType returns the number of connected specific tyeps of peers. PeerCountByType() map[string]uint // MaxPhysicalConnections returns maximum count of peers. MaxPeers() int // Disconnect tries to disconnect peer. Disconnect(destID discover.NodeID) // GetListenAddress returns the listen address list of the server. GetListenAddress() []string // Peers returns all connected peers. Peers() []*Peer // NodeDialer is used to connect to nodes in the network, typically by using // an underlying net.Dialer but also using net.Pipe in tests. NodeDialer }
Server manages all peer connections.
type SingleChannelServer ¶
type SingleChannelServer struct {
*BaseServer
}
SingleChannelServer is a server that uses a single channel.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package discover implements the Node Discovery Protocol.
|
Package discover implements the Node Discovery Protocol. |
Package nat provides access to common network port mapping protocols.
|
Package nat provides access to common network port mapping protocols. |
Package netutil contains extensions to the net package.
|
Package netutil contains extensions to the net package. |
Package simulations simulates p2p networks.
|
Package simulations simulates p2p networks. |
adapters
Package adapters implements simulation network adapters in several ways.
|
Package adapters implements simulation network adapters in several ways. |
pipes
Package pipes implements in process pipes on a localhost TCP socket.
|
Package pipes implements in process pipes on a localhost TCP socket. |