以太坊(ethereum)实现研究以太坊

以太坊p2p之网络及节点发现机制

2018-04-16  本文已影响18人  古则

1.Kademlia

以太坊底层分布式网络即P2P网络,使用了经典的Kademlia网络,简称kad。
Kad的路由表是通过称为K桶的数据构造而成,K桶记录了节点NodeId,distance,endpoint,ip等信息。以太坊K桶按照与target节点距离进行排序,共256个K桶,每个K桶包含16个节点。

2.Kad通信协议

以太坊Kad网络中节点间通信基于UDP,主要由以下几个命令构成,若两个节点间PING-PONG握手通过,则认为相应节点在线。

struct PingNode
{
         h256  version = 0x3;
         Endpoint  from;
         Endpoint  to;
         uint32_t  timestamp;
};
struct Pong
{
       Endpoint to;
       h256 echo;
       uint32_t timestamp;
};
struct FindNeighbours
{
       NodeId target;                 
       uint32_t timestamp;
};
struct Neighbours
{
      list nodes: struct Neighbour
      {
             inline Endpoint endpoint;
             NodeId node;
       };
       uint32_t timestamp;
};

3. 邻居节点发现方法

邻居节点是指加入到K桶,并通过PING-PONG握手的节点。


image.png

邻居节点发现流程说明:

其中d中为什么要这么做呢?因为Kademlia距离是两个节点key的异或运算,d中的算法就是根据异或运算的性质

上一篇下一篇

猜你喜欢

热点阅读