Ethereum Source-to-peer network and node discovery mechanism

Source: Internet
Author: User

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



Directory

1 Introduction to Distributed networks

1.1 Kad net Introduction

1.2 Kad network node distance

1.3 k Barrels

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 Introduction to Distributed networks

Ethereum is the bottom-level distributed network, which uses the classic Kademlia network, referred to as Kad.


1.1 Kad net Introduction

Presented by Petarp.manmounkov and Davidmazieres of the University of New York in 2002, Kademlia is a distributed hash list (DHT) technology that is based on a distance metric of XOR or operation and has been BitTorrent, BitComet, Application in emule and other software.

1.2 Kad network node distance

Ethernet network Node distance calculation method:

NODE1: Node 1 NodeId

Node2: Node 2 NodeId

1.3 k Barrels

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

Figure 1.1

1.4 Kad Communication protocol

The communication between nodes in Ethereum Kad network is based on UDP, which is mainly composed of the following commands, if the ping-pong handshake between two nodes is passed, the corresponding node is considered to be online.

Kad communication protocol, UDP-based

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 command response

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

3

FINDNODE

Querying a node for a node that is close to the target node ID

struct findneighbours
{
NodeId Target; uint32_t timestamp;
};

4

Neighbors

Find_node command response, sending nodes in K-buckets 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

In the C + + version of Ethereum source, Nodetable is the key class for the Ethereum peer network, and all data and methods related to the neighbor 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, Nodeid, distance, endpoint, IP with neighbor nodes

3

M_nodes

Known node information, but not added to K-barrels

Serial number

Name of 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 functions

3

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

Cpp-ethereum

/libp2p/nodetable.cpp

Join the node to 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

The underlying discovery function, which selects the node from the K-bucket and sends the Findnode command

5

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

Cpp-ethereum

/libp2p/nodetable.cpp

Select a 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

Adding a new node to the K-bucket

2.2 Neighbor Node Discovery method

A neighbor node is a node that is joined to a K-bucket and is ping-pong by a handshake.

Figure 2.1

Neighbor Node Discovery process Description:

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

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

The system refreshes the K-barrels every 7200ms.

Refresh the K-bucket process as follows:

A. Randomly generate the target node ID, recorded as Targetid, starting from 1 to record the number of discoveries and the refresh time.

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

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.