Analysis of algorithm for node discovery of Ethereum-Peer module

Source: Internet
Author: User
Tags data structures hash
Core Data Structures:

Nodetable class is responsible for Ethereum node discovery, Nodetable uses Kademlia (KAD) algorithm for node Discovery Nodetable maintain a list of network nodes, this list is the currently available node for use in the upper layer The hash generated by the Nodeid after SHA3 is 256 bits. The list has 256-1 = 255 items, where 1 is because shaving has a node bucket (nodebucket) for each item in the current node (native) list, and a maximum of 16 node lists in each bucket represents the network node collection node discovery algorithm based on the current node (native) distance of i+1: the distance between nodes is defined as follows: node Nodeid (512-bit) first generates a 256-bit hash with the SHA3 algorithm. An XOR value that calculates the 256-bit hash of two nodes, and the node distance is defined as the number of bits of the 1-bit highest bit for this XOR value. (Example: 0010 0000 1000 0101-bit XOR value the distance of the two nodes is 14) The Nodeid here is the network node public key (512-bit) Note: the node distance here is independent of the physical distance of the machine, and this distance is only a logical convention discover the algorithm ideas as follows:

1. The Nodeid of a target node is randomly first 2. In the list, with relative Nodeid "distance" as the indicator, from near find the node near the node that you want to connect. And put these nodes in the nearby node collection 3. Send Findnode message 4 to each node in the nearby node collection of the target node. If no node is found near the target node, return to step 1 5. Otherwise wait for 600ms to jump to the state of the Step 2 node:

Pending:
Pending state, each newly discovered node, or the initial state of the node added through code, sends a PING message to this node when a new node is added, and is checked for online Alive: This status indicates that the Pong message was received and this node is online. evicted: because the bucket of the current node at a distance of up to only 16 nodes allowed, if the new node found at this distance just exceeds the limit, then the new node is retained, the oldest node in the bucket will be called out, into this state how cold start:

Because the point list is empty during initialization, it is not possible to find the so-called nearby nodes of the target node. This requires some initial seed nodes to be connected. When the ETH client is started, 5 seed nodes are added, and the Nodeid, IP, and port of these nodes are hardcoded in the Host.pochosts function.
Node Discovery Protocol: protocol:
FindNode: A node query protocol that asks the target node for its neighboring node list neighbours: Response FindNode message, when a node receives a FINDNODE message from another node, it sends back a neighbours message, which carries a nearby node of this node Pingnode: Used to see if the node is alive. For nodes that are missing Nodeid, you can also ask for their Nodeid Pong: Response Protocol interaction for Pingnode messages:

In the above algorithm description, the current node sends a FINDNODE message to each node in the nearby node collection of the randomly selected node, indicating that the nodes in the vicinity of these nodes will need to send back neighbours messages to the sending node when they receive the Findnode message. And this message contains the nodes in the vicinity of the node collection the current node after receiving the loopback neighbours message, the nodes that are carried in the neighbours are added to their list of network nodes and a ping message is sent to the carrying nodes until the pong message arrives. Prove that this node is alive and joined to the node list

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.