Commmunication module exposes three main abstraction:
In ideal world: Transport Layer has zero dependecies. It provides abstracton to send any (serializable) messages between two nodes.
Node Discovery is depended on Transport Layer. It will use Transport Layer to send messages, providing constant discovery of nodes in P2P network. It will expose two functionalities: addMe (to add given node to the network) and listPeers (to list peers [neighbours] that Node Discovery knows at the given moment). This abstraction does not know anything about RChain yet. It just gives an abstraction of a P2P network - allowing external entities (like for example RChain protocol) learn about neighbouring nodes.
RChain Protocol depends on both Transport Layer and Node Discovery. It uses NodeDiscovery.listPeers in order to learn about neighbouring nodes. It uses Transport Layer to send messages to those nodes. It exposes concept of a "connection" (exchange of ProtocolHandshake-ProtocolHandshake response) and ability to send messages of type Packet between nodes. RChain Protocol allows to send Packet messages only to nodes who previously went through the protocol handshake.
Diagram below shows dependency usage between all three abstractions in the ideal world
+-------------------------+ | | | RChain Protocol | | | +-------------------------+ | | uses |uses v | | +-----------+ | | | | | Node | | | Discovery | | | | | +-----------+ | | v v uses +--------------------+ | Transport Layer | +--------------------+
That's the dream. Reality is...
Reality is that Transport Layer, Node Discovery and RChain Protocol have circular dependencies. Some of those dependencies we could live with for now (just add overall complexity to the design), some are potential cause of weird bugs and security issues (and should be considered as high priority issues).
Diagram below illustrates those dependencies
uses messages +-----------+ +----------------------+ Transport | <---------------+ | | Layer | | | +-----------+ | use to send | +----------------------^ | messages | | uses to send messages | v | | | | +-----------------+ uses findPeers | | | <----------------------------------------------------+ | Node Discovery | | Rchain | | | | Protocol | +-----------------+ +--------------+ +------------------------------------------------^ peers only added by rchan protocol
This cycle has lower priority. It is not crucial to fix it right away. Details in CORE-836 and CORE-839.
This cycle has high priority and should be addressed ASAP. Details in CORE-770.
Below detailed description of how the abstractions are currently implemented and what are the issues that have to be addressed.
Transport Layer abstracts the communication pipeline, allowing two nodes sending messages between each other. Transport Layer is neither aware of Node Discovery nor RChain protocol. Current implementation TcpTransportLayer is missing integration tests, minimum set of missing tests are defined under CORE-607.
Transport Layer exposes following functions:
trait TransportLayer[F[_]] { def local: F[PeerNode] def roundTrip(peer: PeerNode, msg: Protocol, timeout: FiniteDuration): F[CommErr[Protocol]] def send(peer: PeerNode, msg: Protocol): F[Unit] def broadcast(peers: Seq[PeerNode], msg: Protocol): F[Unit] def receive(dispatch: Protocol => F[CommunicationResponse]): F[Unit] def disconnect(peer: PeerNode): F[Unit] def shutdown(msg: Protocol): F[Unit] }
Method local returns PeerNode representing given running node. This method should be removed and PeerNode information should be provided via `ApplicativeAsk` abstraction (see CORE-738 and CORE739 for detail).
Method disconnect should never be part of TransportLayer abstraction. It is TcpTransportLayer implementation detail. It will be removed from the abstraction (see CORE-835 for details).
Shutdown method gracefully shuts down the Transport Layer. After being called, no message can be send (via roundTrip, send, broadcast) nor received.
Methods roundTrip and send allow sending and receiving messages of type Protocol - object defined in routing.proto. Protocol message can be one of the following:
message Protocol { Header header = 1; oneof message { google.protobuf.Any upstream = 2; Ping ping = 3; Pong pong = 4; Lookup lookup = 5; LookupResponse lookup_response = 6; Disconnect disconnect = 7; } }
Ping+Pong and Lookup+LookupResponse are part of the Node Discovery abstraction, so there is clear coupeling between those two abstraction. More details in CORE-836.
Disconnect is currently send by TcpTransportLayer when it shuts down (Disconnect is being broadcast). This method is NOT needed as nodes should realize that other peers disconnect regardlessly if they managed to shut down gracefully or not. This message will be removed from the Protocol object (see CORE-838 for details).
This is a placeholder for ANY possible message that is build on top Protocol. Effectively this is where messages from RChain protocol will be placed. Originally was designed to decouple TransportLayer, NodeDiscovery and RChain protocol (not successfully). This coupeling should be removed, see CORE-839 for details.
Below we describe how Node Discovery works currently
Node discovery is using Tranport Layer to fullfil its work. It uses
+----------------+ | | | NODE DISCOVERY | | | +----------------+ | v uses +----------------+ | TRANSPORT | | LAYER | +----------------+
This is almost true, as mentioned in previous section, TransportLayer sends messages of type Protocol (routing.proto) that are very Kademlia specific. See CORE-838 and CORE-839 to see the issue how to remove that cyclic dependency between Node Discovery and Transport Layer.
2. How current Kademlia implementation is different from the original paper
TBD
3. Kademlia in etherum
Etherum also uses its own Kademlia-like protocol. Description under https://github.com/ethereum/wiki/wiki/Kademlia-Peer-Selection
RChain protocol messages are defined in p2p.proto and are the following:
message Protocol { oneof message { Disconnect diconnect = 1; Hello hello = 2; ProtocolHandshake protocol_handshake = 3; ProtocolHandshakeResponse protocol_handshake_response = 4; Packet packet = 5; } }
Messages Hello and Disconnect are not used in the RChain protocol (even though defined) and will be removed from it (see CORE-840 for details).
RChain Protocol exposes few functionalities: