blog, c++,

Ethereum p2p network c++ implementation

Jing Chi Jing Chi Follow Aug 16, 2019 · 1 min read
Ethereum p2p network c++ implementation
Share this

Ethereum p2p network is based on Kademlia protocol over UDP. For each peer, TCP connect session is established after capatibility handshake.

Get and build source code

Pre-requisite: git, cmake, g++ (Linux) / Visual Studio 2017 (on Windows)

git clone --recursive https://github.com/ethereum/aleth.git
cd aleth

mkdir build; cd build                        # Create a build directory.
cmake .. -G "Visual Studio 15 2017 Win64"    # Configure the project, ignore '-G XXX' for Linux.
cmake --build .                              # Build all default targets on Linux, or open aleth.sln with VS2017.

Kademlia network

Ethereum session

When every node added to / dropped from Kad NodeTable, Host will get notified by an event Host::onNodeTableEvent, then connect to this peer and do handshake before session established.

void Host::connect(shared_ptr<Peer> const& _p)
{
    auto handshake = make_shared<RLPXHandshake>(this, socket, _p->id);
    handshake->start();
}

After handshked and session established, EthereumCapability::onConnect is called to add the new peer, then it Session::readPacket and handles the packet in the registered capatibilities, such as EthereumCapability or WarpCapability, and forwards to their observers finally, EthereumPeerObserver or WarpPeerObserver.

Ethereum broadcasts new blocks to the peers in the function EthereumCapability::maintainBlocks.