p2p

package
v0.0.0-...-de85661 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2022 License: Unlicense Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// HandleHistName 是每个数据包服务时间直方图的前缀。
	HandleHistName = "p2p/handle"
)

Variables

View Source
var (
	ErrShuttingDown = errors.New("shutting down")
)

Functions

func ExpectMsg

func ExpectMsg(r MsgReader, code uint64, content interface{}) error

ExpectMsg从r读取消息,并验证其代码和编码的RLP内容是否与提供的值匹配。 如果内容为零,则丢弃有效负载,并且不进行验证。

func Send

func Send(w MsgWriter, msgcode uint64, data interface{}) error

Send用给定的代码编写RLP编码的消息。数据应编码为RLP列表。

func SendItems

func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error

Types

type Cap

type Cap struct {
	Name    string
	Version uint
}

Cap是对等能力的结构。

func (Cap) String

func (cap Cap) String() string

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消息的结构。 请注意,由于有效负载读取器在发送过程中被消耗,因此消息只能发送一次。 创建消息并发送任意次数都是不可能的。 如果要重用编码结构,请将有效负载编码到字节数组中,并使用字节创建单独的消息。 读卡器作为每次发送的有效负载。

func (Msg) Decode

func (msg Msg) Decode(val interface{}) error

解码将消息的RLP内容解析为给定值,该值必须是指针。有关解码规则,请参阅包rlp。

func (Msg) Discard

func (msg Msg) Discard() error

Discard将剩余的有效载荷数据读入黑洞。

func (Msg) Time

func (msg Msg) Time() time.Time

type MsgPipeRW

type MsgPipeRW struct {
	// contains filtered or unexported fields
}

MsgPipeRW是MsgReadWriter管道的端点。

func (*MsgPipeRW) Close

func (p *MsgPipeRW) Close() error

Close取消阻止管道两端的任何挂起的ReadMsg和WriteMsg调用。 他们将在关闭后返回。Close还会中断对消息负载的任何读取。

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

MsgReadWriter提供对编码消息的读写。 实现应该确保可以从多个goroutine同时调用ReadMsg和WriteMsg。

type MsgReader

type MsgReader interface {
	ReadMsg() (Msg, error)
}

type MsgWriter

type MsgWriter interface {
	// WriteMsg发送消息。它将阻塞,直到消息的有效负载被另一端消耗。
	//请注意,消息只能发送一次,因为它们的有效负载读取器已耗尽。
	WriteMsg(Msg) error
}

type NodeDialer

type NodeDialer interface {
	Dial(context.Context, *enode.Node) (net.Conn, error)
}

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 P2PStart

type P2PStart struct {
}

func (*P2PStart) Start

func (ps *P2PStart) Start()

type Peer

type Peer struct {
	// contains filtered or unexported fields
}

对等表示连接的远程节点。

func (*Peer) Caps

func (p *Peer) Caps() []Cap

Caps返回远程对等方的功能(支持的子程序)。

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

Disconnect以给定的原因终止对等连接。它立即返回,不会等到连接关闭。

func (*Peer) Fullname

func (p *Peer) Fullname() string

Fullname返回远程节点播发的节点名。

func (*Peer) ID

func (p *Peer) ID() enode.ID

ID 返回节点的公钥。

func (*Peer) Inbound

func (p *Peer) Inbound() bool

如果对等方是入站连接,则Inbound返回true

func (*Peer) Info

func (p *Peer) Info() *PeerInfo

Info收集并返回关于对等方的已知元数据集合。

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

LocalAddr返回网络连接的本地地址。

func (*Peer) Log

func (p *Peer) Log() log.Logger

func (*Peer) Name

func (p *Peer) Name() string

Name 返回名称的缩写形式

func (*Peer) Node

func (p *Peer) Node() *enode.Node

Node返回对等节点的节点描述符。

func (*Peer) RemoteAddr

func (p *Peer) RemoteAddr() net.Addr

RemoteAddr返回网络连接的远程地址。

func (*Peer) RunningCap

func (p *Peer) RunningCap(protocol string, versions []uint) bool

如果对等方使用特定协议的任何枚举版本进行活动连接,则RunningCap返回true,这意味着该节点和对等方都支持至少一个版本。

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) AddPeer

func (srv *Server) AddPeer(node *enode.Node)

AddPeer将给定节点添加到静态节点集中。当对等集中有空间时,服务器将连接到节点。 如果连接因任何原因失败,服务器将尝试重新连接对等方。

func (*Server) AddTrustedPeer

func (srv *Server) AddTrustedPeer(node *enode.Node)

AddTrustedPeer将给定节点添加到保留的受信任列表中,该列表允许节点始终连接,即使插槽已满。

func (*Server) NodeInfo

func (srv *Server) NodeInfo() *NodeInfo

NodeInfo收集并返回关于主机的已知元数据集合。

func (*Server) PeerCount

func (srv *Server) PeerCount() int

PeerCount返回连接的对等点的数量。

func (*Server) Peers

func (srv *Server) Peers() []*Peer

对等点返回所有连接的对等点。

func (*Server) PeersInfo

func (srv *Server) PeersInfo() []*PeerInfo

PeerInfo返回描述连接对等点的元数据对象数组。

func (*Server) RemovePeer

func (srv *Server) RemovePeer(node *enode.Node)

RemovePeer从静态节点集中删除节点。如果给定节点当前作为对等节点连接,它也会断开与该节点的连接。此方法阻止,直到退出所有协议并删除对等方。 不要在协议实现中使用RemovePeer,而是在对等机上调用Disconnect。

func (*Server) RemoveTrustedPeer

func (srv *Server) RemoveTrustedPeer(node *enode.Node)

RemoveTrustedPeer从受信任对等集中删除给定节点。

func (*Server) Self

func (srv *Server) Self() *enode.Node

Self返回本地节点的端点信息。

func (*Server) SetupConn

func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error

SetupConn运行握手并尝试将连接添加为对等连接。 当连接被添加为对等连接或握手失败时,它返回。

func (*Server) Start

func (srv *Server) Start() (err error)

Start开始运行服务器。停止后无法重新使用服务器。

func (*Server) Stop

func (srv *Server) Stop()

Stop终止服务器和所有活动对等连接。它会一直阻塞,直到所有活动连接都已关闭。

func (*Server) SubscribeEvents

func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription

SubscribeEvents将给定通道订阅给对等事件

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL