Ethernet Square Source Peer-to-peer Network and node discovery mechanism __p2p

Source: Internet
Author: User

Http://www.cnblogs.com/blockchain/p/7943962.html



Directory

1 Distributed Network Introduction

Introduction of 1.1 Kad network

1.2 Kad network node distance

1.3 K Barrel

1.4 Kad Communication protocol

2 Neighbor Nodes

Main members of the 2.1 nodetable class

2.2 Neighbor Node Discovery method

2.3 Neighbor node network topology and refresh mechanism.

1 Distributed Network Introduction

Ethernet Square bottom of the distributed network is Peer-to-peer network, using the classic Kademlia network, referred to as Kad.


Introduction of 1.1 Kad network

Kademlia in 2002 by Petarp.manmounkov and Davidmazieres of the United States University of New York, is a distributed hash table (DHT) technology, based on XOR or operational distance metrics, has been in BitTorrent, BitComet, emule and other software are applied.

1.2 Kad network node distance

Network node distance calculation method of Ethernet square:

NODE1: Node 1 NodeId

Node2: Node 2 NodeId

1.3 K Barrel

The Kad routing table is constructed by the data called K-bucket, and the K-bucket records the information such as node Nodeid,distance,endpoint,ip. The ether square K bucket is sorted by the distance from target node, a total of 256 k barrels, each k bucket contains 16 nodes.

Figure 1.1

1.4 Kad Communication protocol

Ethernet square Kad communication between nodes based on UDP, mainly by the following several commands, if the two nodes between the ping-pong handshake, the corresponding node is considered online.

Kad communication protocol, based on UDP

Serial number

Classification

Function description

Constitute

1

PING

Probe a node to determine if it is online

struct Pingnode

{

         h256  version = 0x3;

         Endpoint  from;

         Endpoint  to;

         uint32_t  timestamp;

};

2

PONG

Ping response

 struct PONG 
 {
                         Endpoint to; 
                         h256 Echo; 
                         uint32_t timestamp; 
}; 

3

Findnode

Query a node to a node that is close to the target node ID

struct findneighbours
{
NodeId Target; uint32_t timestamp;
};

4

Neighbors

Find_node command response, sending a node in K bucket close to the target node ID

struct neighbours
{
List Nodes:struct neighbour
{
inline Endpoint Endpoint;
NODEID node;
};
uint32_t timestamp;
};

2 Neighbor Nodes main members of the 2.1 nodetable class

C + + version of the ether square source, nodetable is an Ethernet Peer-to-peer network of key classes, all the data and methods related to neighboring nodes are implemented by the Nodetable class.

Serial number

Member name

Description

Note

1

M_node

This node, including Nodeid, endpoint, IP, etc.

2

M_state

K Bucket, including Nodeid, distance, endpoint, IP of neighboring nodes

3

M_nodes

Known node information, but did not add to K-barrel

Serial number

The name of the function

Path

Function

1

Nodetable::nodetable (ba::io_service& _io, KeyPair const& _alias, Nodeipendpoint const& _endpoint, BOOL _ena Bled)

Cpp-ethereum

/libp2p/nodetable.cpp

Nodetable class constructor, initialize K bucket, initiate neighbor node discovery process

2

void nodetable::d odiscovery ()

Cpp-ethereum

/libp2p/nodetable.cpp

Specific Discovery function

3

Shared_ptr<nodeentry> Nodetable::addnode (Node const& _node, noderelation _relation)

Cpp-ethereum

/libp2p/nodetable.cpp

Add a node to the M_nodes and initiate a ping handshake

4

void nodetable::d odiscover (NodeID _node, unsigned _round, shared_ptr<set<shared_ptr<nodeentry>>> _ Tried)

Cpp-ethereum

/libp2p/nodetable.cpp

Bottom Discovery function, select node from K Bucket, send Findnode command

5

Vector<shared_ptr<nodeentry>> nodetable::nearestnodeentries (NodeID _target)

Cpp-ethereum

/libp2p/nodetable.cpp

Select the node from the K bucket

6

void Nodetable::onreceived (udpsocketface*, Bi::udp::endpoint const& _from, Bytesconstref _packet)

Cpp-ethereum

/libp2p/nodetable.cpp

Kad protocol processing

7

void Nodetable::noteactivenode (Public const& _pubk, Bi::udp::endpoint const& _endpoint)

Cpp-ethereum

/libp2p/nodetable.cpp

Add new node to K bucket

2.2 Neighbor Node Discovery method

A neighbor node is a node that joins the K bucket and shakes hands through the ping-pong.

Figure 2.1

Neighbor Node Discovery process Description:

The first time the system starts the random Generating machine node Nodeid, which is recorded as Localid, will be fixed unchanged after the generation, and the local node is recorded as Local-eth.

The system reads the public node information and writes it to the K bucket after the ping-pong handshake is completed.

The system refreshes the K-barrel once every 7200ms.

Refresh the K bucket process as follows:

A. Randomly generate the target node ID, recorded as Targetid, recording the discovery count and refresh time starting at 1.

B. Calculate the distance between Targetid and Localid, as DLT

C. The nodeid of the nodes in K-barrel is kadid, and the distance between Kadid and Targetid is calculated as Dkt

D. Find the node in K bucket which is more than dkt, remember as K bucket node, send Findnode command to K bucket node, Findnode command contains Targetid

E. K Bucket node after receiving the Findnode command, the same process of b-d is performed, and the node found from the K bucket is sent back to the native node using the Neighbours command.

F. When the native node receives the neighbours, the received node is written to the K bucket.

G. If the search does not exceed 8 times, the refresh time does not exceed 600ms, then return to the B step loop execution.

2.3 Neighbor Node network topology and refresh mechanism.

Figure 2.2

1 Targetid is a randomly generated virtual node ID.

2 The difference between Ethernet Kad network and traditional Kad network:

In the 8 cycles of discovering neighbor nodes, the nodes in the Ethernet square converge to the randomly generated targetid in the distance.

When a traditional Kad network discovers a node, it converges to the node itself on the distance.

This article is organized by HPB (core chain) team.

 <script> $ (".
        Mathjax "). Remove (); </script> <script type= "Text/javascript" src= "//static-blog.csdn.net/mdeditor/public/res/bower-libs/math JAX/MATHJAX.JS?CONFIG=TEX-AMS-MML_HTMLORMML "></script> </div> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.