# Teku networking ## Network endpoints Localhost net interface: - JSON API - Metrics - Validator External net interface: - **Libp2p**: TCP :9000 : [Spec](https://github.com/libp2p/specs), [JVM implementation (Kotlin)](https://github.com/libp2p/jvm-libp2p) - **Discovery**: UDP :9000 : [Spec](https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md), [JVM implementation (Java)](https://github.com/PegaSysEng/discovery) ![](https://i.imgur.com/UADAkuL.png) ## Libp2p ### Main Libp2p essenses ![](https://i.imgur.com/Dj0wiSW.png) - **Connection**: secured, multiplexed ([Connection.kt](https://github.com/mbaxter/jvm-libp2p/blob/9f741670cb27e55514768be1cce5c6d394e93cf5/src/main/kotlin/io/libp2p/core/Connection.kt), [Spec](https://github.com/libp2p/specs/tree/master/connections)) - **Stream**: Raw binary duplex channels atop of Connection ([Stream.kt](https://github.com/libp2p/jvm-libp2p/blob/9f741670cb27e55514768be1cce5c6d394e93cf5/src/main/kotlin/io/libp2p/core/Stream.kt)) - **Protocol binding**: client protocol handler ([ProtocolBinding.kt](https://github.com/libp2p/jvm-libp2p/blob/bbeda0153b4882eb617d2cfb830927958b6ed785/src/main/kotlin/io/libp2p/core/multistream/ProtocolBinding.kt)) ### Basic Libp2p Pipeline Sample ![](https://i.imgur.com/8aA7b61.png) ``` TCP Socket ^ Multistream: /noise ^ Noise (Negotiator then Cipher) ^ (secured connection) Multistream: /mplex/6.7.0 or /yamux (not supported yet) ^ Mplex stream multiplexer ^ ^ ^ ^ ^ (Streams) Multistream: /ipfs/id/1.0.0, /ipfs/ping/1.0.0, /meshsub/1.1.0, (Gossip 1.1) /eth2/beacon_chain/req/<method>/<ver> (Eth2 RPC) ^ ^ ^ ^ ^ Negotiated Protocol Handler ``` ### Implementation overview https://github.com/libp2p/jvm-libp2p #### Architecture - built _with_ [Netty](https://netty.io/) - expose a lot of Netty API: `Channel`, `ChannelPipeline`, `ChannelHandler`, `ByteBuf` - Libp2p `Connection`, `Stream` _are_ essntially Netty `Channel`s - All libp2p components like security handlers, multiplexers, protocol selectros, protocl bindings _are_ essentially Netty `ChannelHandler`s ![](https://i.imgur.com/eZ75Kge.png) #### Components Minimal Libp2p spec subset required for implementing Eth2 node - Transport: -- [`TCP`](https://github.com/libp2p/jvm-libp2p/blob/9f741670cb27e55514768be1cce5c6d394e93cf5/src/main/kotlin/io/libp2p/transport/tcp/TcpTransport.kt) -- `WebSocket` (disabled in Teku now) - Security: -- `Plain` (for tesing only) -- `Secio` (disabled in Teku now) -- [`Noise-XX-25519-ChaChaPoly-SHA256`](https://github.com/libp2p/jvm-libp2p/tree/98a72bdfe455cc4af95c7cf0de92116c31bd1a3f/src/main/kotlin/io/libp2p/security/noise) ([Spec](https://github.com/libp2p/specs/tree/master/noise)) - Stream multiplexing: only [`mplex`](https://github.com/libp2p/jvm-libp2p/tree/bbeda0153b4882eb617d2cfb830927958b6ed785/src/main/kotlin/io/libp2p/mux/mplex) ([Spec](https://github.com/libp2p/specs/tree/master/mplex)) - Protocols bindings: --[`Gossip 1.1`](https://github.com/libp2p/jvm-libp2p/tree/6753c3f06d2c9e89a76c7dd8b13f26c117c128fc/src/main/kotlin/io/libp2p/pubsub/gossip): Generic `pubsub` [spec](https://github.com/libp2p/specs/tree/master/pubsub), `Gossip` (v1.0 + v1.1) [spec](https://github.com/libp2p/specs/tree/master/pubsub/gossipsub) --[`Ping`](https://github.com/libp2p/jvm-libp2p/blob/bbeda0153b4882eb617d2cfb830927958b6ed785/src/main/kotlin/io/libp2p/protocol/Ping.kt) --[`Identity`](https://github.com/libp2p/jvm-libp2p/blob/bbeda0153b4882eb617d2cfb830927958b6ed785/src/main/kotlin/io/libp2p/protocol/Identify.kt) - No Connection manager - No Libp2p discovery (Just `MDns` which is kind of standalone service now) #### Maturity The JVM library can still be assumed as beta-version: - The first version was implemented in just 2.5 months with minimal testing and without any compatibility tests: aka Alpha version - While adoping it in Teku many compatibility and reliablity issues were fixed, a number of unit tests were added (though still far from acceptable coverage): aka Beta version ### Teku integration ![](https://i.imgur.com/q9GObtw.png) #### Overview [**networking:p2p** module](https://github.com/PegaSysEng/teku/tree/a8ae2210d40c0be865e8a0159f23ca62478405a7/networking/p2p): Eth2 agnostic Libp2p binding. Contains generic classes for RPC, subscriptions [**networking:eth** module](https://github.com/PegaSysEng/artemis/blob/914f8ff8726bde6ba22e425274d23cd8a79e9e15/networking/eth2): Extends **p2p** classes for Eth2 specific functions Configuration of Libp2p host: [LibP2PNetwork.java](https://github.com/PegaSysEng/teku/blob/a8ae2210d40c0be865e8a0159f23ca62478405a7/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetwork.java#L127-L156) - **Eth2 RPC** methods ([Spec](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/p2p-interface.md#the-reqresp-domain), [BeaconChainMethods.java](https://github.com/PegaSysEng/teku/blob/6f8ec886cee6f68b6464d124ffe842ea62003e86/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/rpc/beaconchain/BeaconChainMethods.java)): raw Libp2p `Stream`s -- `/eth2/beacon_chain/req/status` -- `/eth2/beacon_chain/req/ping` -- `/eth2/beacon_chain/req/metadata` -- `/eth2/beacon_chain/req/beacon_blocks_by_root` -- `/eth2/beacon_chain/req/beacon_blocks_by_range` - **Eth2 pubsub** topics ([Spec](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/p2p-interface.md#the-gossip-domain-gossipsub), [`tech.pegasys.teku.networking.eth2.gossip` package](https://github.com/PegaSysEng/teku/tree/bed34b73015a360ae10a3c4619a78f2623a69c46/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip)): Gossip 1.1 topics: -- `beacon_block`: new blocks broadcasting ([BlockTopicHandler.java](https://github.com/PegaSysEng/teku/blob/b5e65f6ccf299879c50a114028ed3fd3fe970893/networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/gossip/topics/BlockTopicHandler.java)) -- `beacon_attestation_{subnet_id}`: unaggregated attestations broadcasting to the subnet subnet_id -- `beacon_aggregate_and_proof` aggregated attestations broadcasting to be included in future blocks -- `voluntary_exit`, `proposer_slashing`, `attester_slashing` #### Eth2 RPC - `/eth2/beacon_chain/req/*` protocol ID - A new `Stream` per each request/response ([RpcHandler.java](https://github.com/PegaSysEng/teku/blob/6f8ec886cee6f68b6464d124ffe842ea62003e86/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/rpc/RpcHandler.java)) - `[snappy[ssz[request|response]]]` (Snappy [framing variant](https://github.com/google/snappy/blob/master/framing_format.txt)) - Response may contain several `chunk`s. Each `chunk` may be fragmented to several snappy `frames`. (IMO good place for hacking) Response sample `respCode len1 [chunk-1-1] len2 [chunk-2-1][chunk-2-2]` #### Eth2 pubsub - `beacon_attestation_{subnet_id}` topics subscriptions are volatile. A validator node needs to change subnet subscriptions on per epoch basis. - `[snappy[ssz[message]]]` (Snappy [block variant](https://github.com/google/snappy/blob/master/format_description.txt)) - Specific [Gossip parameters](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/p2p-interface.md#the-gossip-domain-gossipsub) ([LibP2PNetwork.createGossip()](https://github.com/PegaSysEng/teku/blob/a8ae2210d40c0be865e8a0159f23ca62478405a7/networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/libp2p/LibP2PNetwork.java#L159-L171)) for Eth2 network (Gossip 1.1 parameters are yet to be defined) ## Discovery Spec: https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md JVM Implementation: https://github.com/PegaSysEng/discovery - Netty UDP - RLP encoding - Contains ENR field with peer attestation subnet subscriptions to better handle frequently changed `beacon_attestation_{subnet_id}` Gossip subscriptions Maturity may also be considered as Beta # Blst library