Documentation ¶
Overview ¶
Package v4wire implements the Discovery v4 Wire Protocol.
Index ¶
- Constants
- Variables
- func Decode(input []byte) (Packet, Pubkey, []byte, error)
- func DecodePubkey(curve elliptic.Curve, e Pubkey) (*ecdsa.PublicKey, error)
- func Encode(priv *ecdsa.PrivateKey, req Packet) (packet, hash []byte, err error)
- func Expired(ts uint64) bool
- type ENRRequest
- type ENRResponse
- type Endpoint
- type Findnode
- type Neighbors
- type Node
- type Packet
- type Ping
- type Pong
- type Pubkey
Constants ¶
const ( PingPacket = iota + 1 // zero is 'reserved' PongPacket FindnodePacket NeighborsPacket ENRRequestPacket ENRResponsePacket )
RPC packet types 定义了六种包类型
const MaxNeighbors = 12
This number is the maximum number of neighbor nodes in a Neighbors packet.
Variables ¶
Functions ¶
func Decode ¶
Decode reads a discovery v4 packet. 输入v4数据包的字节数组,解码出来数据包对象和远程节点公钥 数据包的结构 32字节哈希 65字节签名 其余数据 其余数据的第一个字节保存了包类型 返回包对象,远程节点的公钥,包中签名和数据整体的哈希,错误
func DecodePubkey ¶
DecodePubkey reads an encoded secp256k1 public key. 输入曲线和64字节数组解码出来公钥对象
func Encode ¶
func Encode(priv *ecdsa.PrivateKey, req Packet) (packet, hash []byte, err error)
Encode encodes a discovery packet. 构造数据包的字节数组,返回的哈希是签名和数据的哈希,就是包字节数组的最开始部分 packet = packet-header || packet-data packet-header = hash || signature || packet-type hash = keccak256(signature || packet-type || packet-data) signature = sign(packet-type || packet-data) hash长度32字节,signature长度65字节 1. 留空97字节,写入一字节类型 2. 对数据包进行编码写入bytes.Buffer 3. 对编码计算哈希然后签名填入前面留空的后65字节 4. 对签名和编码计算哈希,填入最开始32字节
Types ¶
type ENRRequest ¶
type ENRRequest struct { Expiration uint64 // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
enrRequest queries for the remote node's record.
func (*ENRRequest) Kind ¶
func (req *ENRRequest) Kind() byte
func (*ENRRequest) Name ¶
func (req *ENRRequest) Name() string
type ENRResponse ¶
type ENRResponse struct { // 响应的ENRRequest包的哈希 ReplyTok []byte // Hash of the enrRequest packet. Record enr.Record // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
enrResponse is the reply to enrRequest.
func (*ENRResponse) Kind ¶
func (req *ENRResponse) Kind() byte
func (*ENRResponse) Name ¶
func (req *ENRResponse) Name() string
type Endpoint ¶
type Endpoint struct { IP net.IP // len 4 for IPv4 or 16 for IPv6 UDP uint16 // for discovery protocol TCP uint16 // for RLPx protocol }
Endpoint represents a network endpoint. 一个网络端点,包括对方的ip,UDP端口,TCP端口
type Findnode ¶
type Findnode struct { // 要查询与Target最近的节点 Target Pubkey Expiration uint64 // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
Findnode is a query for nodes close to the given target.
type Neighbors ¶
type Neighbors struct { // 查询到的结果 Nodes []Node Expiration uint64 // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
Neighbors is the reply to findnode.
type Node ¶
type Node struct { IP net.IP // len 4 for IPv4 or 16 for IPv6 UDP uint16 // for discovery protocol TCP uint16 // for RLPx protocol ID Pubkey }
Node represents information about a node. 一个节点信息 相比于Endpoint,增加了对方的公钥所以就成为了一个节点
type Ping ¶
type Ping struct { // 协议版本,就是4 Version uint // ping包的发送方和接收方的ip、端口信息 From, To Endpoint // ping包的过期时间 Expiration uint64 // 本地节点的记录序号 ENRSeq uint64 `rlp:"optional"` // Sequence number of local record, added by EIP-868. // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
RPC request structures 以下定义了六种包的具体数据结构
type Pong ¶
type Pong struct { // This field should mirror the UDP envelope address // of the ping packet, which provides a way to discover the // the external address (after NAT). // Pong包返回的目的地址 To Endpoint // 这个pong包对应的ping包的哈希 ReplyTok []byte // This contains the hash of the ping packet. Expiration uint64 // Absolute timestamp at which the packet becomes invalid. ENRSeq uint64 `rlp:"optional"` // Sequence number of local record, added by EIP-868. // Ignore additional fields (for forward compatibility). Rest []rlp.RawValue `rlp:"tail"` }
Pong is the reply to ping.
type Pubkey ¶
type Pubkey [64]byte
Pubkey represents an encoded 64-byte secp256k1 public key.
func EncodePubkey ¶
EncodePubkey encodes a secp256k1 public key. 编码公钥对象到64字节数组,去掉固定前缀04