2021年最新以太坊源码导读-p2p/protocol.go

这一部分描述了节点间的交互协议,内容比较简单

// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package p2p

import (
	"fmt"

	"github.com/ethereum/go-ethereum/p2p/enode"
	"github.com/ethereum/go-ethereum/p2p/enr"
)

// Protocol represents a P2P subprotocol implementation.
// Protocol代表一个p2p子协议的实现
type Protocol struct {
	// Name should contain the official protocol name,
	// often a three-letter word.
	// Name应当包含官方的协议名,通常是一个三子母的单词
	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 goroutine 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.
	// 当同一个对等节点协商协议完成后,Run将在一个新的goroutine中被调用。
	// 它会通过rw读写消息。每个消息的载荷都必须被完整地消费。
	// The peer connection is closed when Start returns. It should return
	// any protocol-level error (such as an I/O error) that is
	// encountered.
	// Start返回后,节点间的连接将会被关闭。它应该返回任意协议层面的错误。
	Run func(peer *Peer, rw MsgReadWriter) error

	// NodeInfo is an optional helper method to retrieve protocol specific metadata
	// about the host node.
	// NodeInfo 是一种可选的辅助方法,用于获取主节点的协议特定元数据。
	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是一种可选的辅助方法,用于获取网络中指定节点的协议特定元数据。如果此方法返回了nil,
	// 则意味着协议仍处于握手阶段。
	PeerInfo func(id enode.ID) interface{}

	// DialCandidates, if non-nil, is a way to tell Server about protocol-specific nodes
	// that should be dialed. The server continuously reads nodes from the iterator and
	// attempts to create connections to them.
	// 如果DialCandidates不是nil, 则是一种告知Server应当与协议指定的节点建立连接的方式。Server连续从迭代器
	// 读取节点并尝试与之建立连接
	DialCandidates enode.Iterator

	// Attributes contains protocol specific information for the node record.
	// 包含关于节点的协议指定信息
	Attributes []enr.Entry
}

func (p Protocol) cap() Cap {
	return Cap{p.Name, p.Version}
}

// Cap is the structure of a peer capability.
type Cap struct {
	Name    string
	Version uint
}

func (cap Cap) String() string {
	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
}

type capsByNameAndVersion []Cap

func (cs capsByNameAndVersion) Len() int      { return len(cs) }
func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
func (cs capsByNameAndVersion) Less(i, j int) bool {
	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
}

猜你喜欢

转载自blog.csdn.net/HardRedStone/article/details/118355304