Ethereum blockchain Java (EthereumJ) study notes: P2P Network

This article gives a brief introduction to the DevP2P-related code of EthereumJ.

DevP2P

Ethereum defines its own DevP2P protocol (https://github.com/ethereum/devp2p) to achieve block synchronization between nodes in the Ethereum network.

 

The network protocol of Ethereum can be roughly divided into three levels:

l Network layer (https://github.com/ethereum/devp2p/blob/master/rlpx.md), which defines how to discover adjacent nodes in the network, how to perform secure handshake between nodes, and how to implement upper-layer protocols The message is put into the transmission frame, and how to control the flow of the message.

l DEVP2P protocol layer (https://github.com/ethereum/wiki/wiki/%C3%90%CE%9EVp2p-Wire-Protocol), which defines the messages and messages needed to establish P2P links between Ethereum nodes interactive process.

l Ethereum protocol layer (https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol), which defines how to obtain blockHeaders, Blocks, Transactions and other information between Ethereum nodes and the process of message interaction .

 

EthereumJ Net

EthereumJ uses the UDP protocol to discover nodes in the Ethereum network, and uses the TCP protocol to exchange block messages between nodes in the Ethereum network.

 

EthereumJ uses netty's NIO class library to process the underlying TCP protocol and UDP protocol, and implements the protocol defined by Ethereum DEVP2P.

 

The modules of EthereumJ for Ethereum node discovery mainly include the following Classes.

l UDPListener, start netty's UDPChannel, and load PacketDecoder and MessageHandler on the Channel's pipeline to process DEVP2P messages.

 

l NodeManager, which is the main class for handling Ethereum node discovery. It uses NodeHandler to handle state transitions of Ethereum nodes.

 

After discovering other nodeshou on the network, EthereumJ mainly uses the following Class to process Ethereum DEVP2P messages.

l EthereumChannelInitializer, which extends Netty's ChannelInitializer<NioSocketChannel>. Create EthereumJ's own Channel for the client connection or server connection established by EthereumJ, and initialize it.

 

l PeerClient, which implements the management of the connection on the client side of the Ethereum node. Through connect(), other modules can actively establish a connection to an Ethereum node that has been discovered, and then send messages.

 

l PeerServer, which implements the management of the connection on the server side of the Ethereum node. ChannelManager starts the configured peep.listener port through start() and can respond to requests from other Ethereum nodes to establish connections.

 

l ChannelManager, which manages the Channels created by other Ethereum nodes that have been discovered. And provides the functions sendNewBlock(Block) and sendTransaction(List<Transaction>, Channel), other modules can call to send blocks and transactions to the Ethereum network.

 

l Channel, manages the DEVP2P connection established between the Ethereum nodes, transmits messages, and processes messages of different levels through the various handlers described below.

 

l HandshakeHandler, which handles DEVP2P handshake messages. HandshakeHandler extends netty's ByteToMessageDecoder and processes various messages through netty's callback function. For example, through channelActive() to initialize, through decode() to process handshake messages. When the DEV2P handshake interaction is over, load the P2pHandler by calling the Channel's publicRLPxHandshakeFinished().

 

l P2pHandler, which handles DEVP2P protocol messages. P2pHandler extends Netty's SimpleChannelInboundHandler, and netty triggers P2pHandler to process various messages by returning the function channelRead0().

 

EthHandler, used to handle the Ethereum protocol of DEVP2P. After receiving the DEVP2P HELLO message from the other party, the activateEth() of the Channel is used to start the message processing of Ethereum. EthereumJ can handle both versions of the Ethereum message protocol, Eth62 and Eth63.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871863&siteId=291194637