Documentation ¶
Index ¶
- Constants
- Variables
- func ExpectMsg(r MsgReader, code uint64, content interface{}) error
- func Send(w MsgWriter, msgcode uint64, data interface{}) error
- func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error
- type Cap
- type Config
- type DiscReason
- type Msg
- type MsgPipeRW
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type NodeDialer
- type NodeInfo
- type P2PStart
- type Peer
- func (p *Peer) Caps() []Cap
- func (p *Peer) Disconnect(reason DiscReason)
- func (p *Peer) Fullname() string
- func (p *Peer) ID() enode.ID
- 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) Node() *enode.Node
- func (p *Peer) RemoteAddr() net.Addr
- func (p *Peer) RunningCap(protocol string, versions []uint) bool
- type PeerEvent
- type PeerEventType
- type PeerInfo
- type Protocol
- type Server
- func (srv *Server) AddPeer(node *enode.Node)
- func (srv *Server) AddTrustedPeer(node *enode.Node)
- func (srv *Server) NodeInfo() *NodeInfo
- func (srv *Server) PeerCount() int
- func (srv *Server) Peers() []*Peer
- func (srv *Server) PeersInfo() []*PeerInfo
- func (srv *Server) RemovePeer(node *enode.Node)
- func (srv *Server) RemoveTrustedPeer(node *enode.Node)
- func (srv *Server) Self() *enode.Node
- func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error
- func (srv *Server) Start() (err error)
- func (srv *Server) Stop()
- func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription
Constants ¶
const (
// HandleHistName 是每个数据包服务时间直方图的前缀。
HandleHistName = "p2p/handle"
)
Variables ¶
var (
ErrShuttingDown = errors.New("shutting down")
)
Functions ¶
Types ¶
type Config ¶
type Config struct { // 该字段必须设置为有效的secp256k1私钥。 PrivateKey *ecdsa.PrivateKey `toml:"-"` // MaxPeers是可以连接的最大对等数。它必须大于零。 MaxPeers int // MaxPendingPeers是在握手阶段可以挂起的最大对等点数,对于入站和出站连接分别计算。零默认为预设值。 MaxPendingPeers int `toml:",omitempty"` // DialRatio控制入站连接与已拨连接的比率。 //示例:2的拨号率允许拨打1/2的连接。将DialRatio设置为零默认为3。 DialRatio int `toml:",omitempty"` // 节点发现可用于禁用对等发现机制。禁用对协议调试(手动拓扑)很有用。 NoDiscovery bool // DiscoveryV5指定是否应启动基于新主题发现的V5发现协议。 DiscoveryV5 bool `toml:",omitempty"` // 名称设置此服务器的节点名称。使用common。MakeName以创建符合现有约定的名称。 Name string `toml:"-"` // 引导节点用于建立与网络其余部分的连接。 BootstrapNodes []*enode.Node // BootstrapNodesV5用于使用V5发现协议建立与网络其余部分的连接。 BootstrapNodesV5 []*enode.Node `toml:",omitempty"` // 静态节点用作预先配置的连接,在断开连接时始终保持并重新连接。 StaticNodes []*enode.Node // 受信任节点用作预先配置的连接,始终允许连接,甚至超过对等限制。 TrustedNodes []*enode.Node // 连接可以限制在某些IP网络上。如果此选项设置为非nil值,则只考虑与列表中包含的一个IP网络匹配的主机。 NetRestrict *netutil.Netlist `toml:",omitempty"` // NodeDatabase是指向数据库的路径,其中包含以前在网络中看到的活动节点。 NodeDatabase string `toml:",omitempty"` // 协议应包含服务器支持的协议。为每个对等点启动匹配协议。 Protocols []Protocol `toml:"-" json:"-"` // 如果ListenAddr设置为非nil地址,服务器将侦听传入的连接。 //如果端口为零,操作系统将选择一个端口。当服务器启动时,ListenAddr字段将用实际地址更新。 ListenAddr string // 如果discadr设置为非nil值,则服务器将使用ListenAddr作为TCP,使用discadr作为UDP发现协议。 DiscAddr string // 如果设置为非零值,则使用给定的NAT端口映射器使侦听端口可用于Internet。 NAT nat.Interface `toml:",omitempty"` // 如果拨号器设置为非零值,则给定的拨号器用于拨号出站对等连接。 Dialer NodeDialer `toml:"-"` // 如果NoDial为true,服务器将不会拨打任何对等方。 NoDial bool `toml:",omitempty"` // 如果设置了EnableMsgEvents,那么每当向对等方发送消息或从对等方接收消息时,服务器将发出对等事件 EnableMsgEvents bool // Logger是一个用于p2p的自定义记录器。服务器 Logger log.Logger `toml:",omitempty"` // contains filtered or unexported fields }
Config保存服务器选项。
type DiscReason ¶
type DiscReason uint8
const ( DiscRequested DiscReason = iota DiscNetworkError DiscProtocolError DiscUselessPeer DiscTooManyPeers DiscAlreadyConnected DiscIncompatibleVersion DiscInvalidIdentity DiscQuitting DiscUnexpectedIdentity DiscSelf DiscReadTimeout DiscSubprotocolError = DiscReason(0x10) )
func (DiscReason) Error ¶
func (d DiscReason) Error() string
func (DiscReason) String ¶
func (d DiscReason) String() string
type Msg ¶
type Msg struct { Code uint64 Size uint32 // 原始有效负载的大小 Payload io.Reader ReceivedAt time.Time // contains filtered or unexported fields }
Msg定义p2p消息的结构。 请注意,由于有效负载读取器在发送过程中被消耗,因此消息只能发送一次。 创建消息并发送任意次数都是不可能的。 如果要重用编码结构,请将有效负载编码到字节数组中,并使用字节创建单独的消息。 读卡器作为每次发送的有效负载。
type MsgPipeRW ¶
type MsgPipeRW struct {
// contains filtered or unexported fields
}
MsgPipeRW是MsgReadWriter管道的端点。
type MsgReadWriter ¶
MsgReadWriter提供对编码消息的读写。 实现应该确保可以从多个goroutine同时调用ReadMsg和WriteMsg。
type NodeDialer ¶
NodeDialer用于连接网络中的节点,通常使用底层网络。 拨号器,但也使用网络。管道测试。
type NodeInfo ¶
type NodeInfo struct { ID string `json:"id"` // 唯一节点标识符(也是加密密钥) Name string `json:"name"` // 节点的名称,包括客户端类型、版本、操作系统、自定义数据 Enode string `json:"enode"` // 用于从远程对等添加此对等的Enode URL ONR string `json:"enr"` // 辐射章鱼节点记录 IP string `json:"ip"` // 节点的IP地址 Ports struct { Discovery int `json:"discovery"` // 发现协议的UDP侦听端口 Listener int `json:"listener"` // RLPx的TCP侦听端口 } `json:"ports"` ListenAddr string `json:"listenAddr"` Protocols map[string]interface{} `json:"protocols"` }
NodeInfo表示关于主机的已知信息的简短摘要。
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
对等表示连接的远程节点。
func (*Peer) Disconnect ¶
func (p *Peer) Disconnect(reason DiscReason)
Disconnect以给定的原因终止对等连接。它立即返回,不会等到连接关闭。
type PeerEvent ¶
type PeerEvent struct { Type PeerEventType `json:"type"` Peer enode.ID `json:"peer"` Error string `json:"error,omitempty"` Protocol string `json:"protocol,omitempty"` MsgCode *uint64 `json:"msg_code,omitempty"` MsgSize *uint32 `json:"msg_size,omitempty"` LocalAddress string `json:"local,omitempty"` RemoteAddress string `json:"remote,omitempty"` }
PeerEvent 是对等点添加或删除时发出的事件。服务器或在对等连接上发送或接收消息时
type PeerEventType ¶
type PeerEventType string
PeerEventType 是p2p发出的对等事件的类型。服务器
const ( // PeerEventTypeAdd是将对等添加到p2p时发出的事件类型。服务器 PeerEventTypeAdd PeerEventType = "add" // PeerEventTypeDrop是对等点从p2p中删除时发出的事件类型。服务器 PeerEventTypeDrop PeerEventType = "drop" // PeerEventTypeMsgSend是消息成功发送到对等方时发出的事件类型 PeerEventTypeMsgSend PeerEventType = "msgsend" // PeerEventTypeMsgRecv是从对等方接收消息时发出的事件类型 PeerEventTypeMsgRecv PeerEventType = "msgrecv" )
type PeerInfo ¶
type PeerInfo struct { ENR string `json:"enr,omitempty"` // 以太坊节点记录 Enode string `json:"enode"` // 节点URL ID string `json:"id"` // 唯一节点标识符 Name string `json:"name"` // 节点的名称,包括客户端类型、版本、操作系统、自定义数据 Caps []string `json:"caps"` // 此对等方公布的协议 Network struct { LocalAddress string `json:"localAddress"` // TCP数据连接的本地端点 RemoteAddress string `json:"remoteAddress"` // TCP数据连接的远程端点 Inbound bool `json:"inbound"` Trusted bool `json:"trusted"` Static bool `json:"static"` } `json:"network"` Protocols map[string]interface{} `json:"protocols"` // 子协议特定元数据字段 }
PeerInfo表示有关已连接对等方的已知信息的简短摘要。 此处包含并初始化了与子协议无关的字段,并将协议细节委托给所有连接的子协议。
type Protocol ¶
type Protocol struct { // 名称应包含正式协议名称,通常为三个字母的单词。 Name string // 版本应包含协议的版本号。 Version uint // 长度应包含协议使用的消息代码数。 Length uint64 // 当与对等方协商协议时,在新的goroutine中调用Run。 //它应该读写来自rw的消息。必须完全消耗每条消息的有效负载。 //当Start返回时,对等连接关闭。它应该返回遇到的任何协议级错误(例如输入/输出错误)。 Run func(peer *Peer, rw MsgReadWriter) error // NodeInfo是一种可选的辅助方法,用于检索有关主机节点的协议特定元数据。 NodeInfo func() interface{} //PeerInfo是一种可选的辅助方法,用于检索网络中特定对等方的协议特定元数据。 //如果设置了信息检索函数,但返回nil,则假设协议握手仍在运行。 PeerInfo func(id enode.ID) interface{} // DialCandidates(若非nil)是一种告诉服务器应该拨打的协议特定节点的方法。 //服务器不断地从迭代器中读取节点,并尝试创建到这些节点的连接。 DialCandidates enode.Iterator // 属性包含节点记录的协议特定信息。 Attributes []enr.Entry }
协议表示一个P2P子协议实现。
type Server ¶
type Server struct { // 服务器运行时,不能修改配置字段。 Config DiscV5 *discover.UDPv5 // contains filtered or unexported fields }
服务器管理所有对等连接。
func (*Server) AddTrustedPeer ¶
AddTrustedPeer将给定节点添加到保留的受信任列表中,该列表允许节点始终连接,即使插槽已满。
func (*Server) RemovePeer ¶
RemovePeer从静态节点集中删除节点。如果给定节点当前作为对等节点连接,它也会断开与该节点的连接。此方法阻止,直到退出所有协议并删除对等方。 不要在协议实现中使用RemovePeer,而是在对等机上调用Disconnect。
func (*Server) RemoveTrustedPeer ¶
RemoveTrustedPeer从受信任对等集中删除给定节点。
func (*Server) SubscribeEvents ¶
func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription
SubscribeEvents将给定通道订阅给对等事件
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package discover implements the Node Discovery Protocol.
|
Package discover implements the Node Discovery Protocol. |
v4wire
Package v4wire implements the Discovery v4 Wire Protocol.
|
Package v4wire implements the Discovery v4 Wire Protocol. |
Package dnsdisc implements node discovery via DNS (EIP-1459).
|
Package dnsdisc implements node discovery via DNS (EIP-1459). |
Package enr implements Ethereum Node Records as defined in EIP-778.
|
Package enr implements Ethereum Node Records as defined in EIP-778. |
Package msgrate allows estimating the throughput of peers for more balanced syncs.
|
Package msgrate allows estimating the throughput of peers for more balanced syncs. |
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 rlpx implements the RLPx transport protocol.
|
Package rlpx implements the RLPx transport protocol. |
Package simulations simulates p2p networks.
|
Package simulations simulates p2p networks. |