Skip to content

P2P network

chimp1984 edited this page May 7, 2021 · 1 revision

Preliminary documentation

Design goals:

  • API prefers usage of CompletableFuture instead of callbacks.
  • Each layer should stay limited to one task to keep complexity low.
  • Shared state should be avoided as far as possible.
  • Immutable objects preferred.

Notes:

  • I use Java Serialisation atm as it is very convenient for prototyping. It will definitely not be used in production version.
  • Serialisation method is not decided yet but likely will be protobuf.
  • I use also Lombok atm for convenience, but I think we should avoid keeping that dependency for production. But up for discussion later.

Class hierarchy

Interface: misq.p2p.proxy.NetworkProxy:

Initializes the proxy, provides the serversocker and returns a client socker for outbound connection requests.

Implementations:

  • misq.p2p.proxy.TorNetworkProxy
  • misq.p2p.proxy.I2pNetworkProxy
  • misq.p2p.proxy.ClearNetNetworkProxy

misq.p2p.node.Node

Creates the NetworkProxy implementation based on the choosen network type. Starts the server and creates inbound connections when clients connect. A node can have multiple servers (e.g. multiple hidden services). Creates outbound connections for sending messages. Address is only known for outbound connections at that layer. Notfiy messageListeners and connectionListeners.

misq.p2p.capability.CapabilityExchange

Performs an initial handshake with the peer for exchanging the nodes capabilities. This contains the own address (inbound connections do not know the peers address), the supported network types and later metrics about the nodes network load which are used for the pow protection. Only after the handshake is completed the lower level node becomes accessible for higher level services. On send message requests with a give address it tries to resolve the connection based on the peers address taken from its capability. That way an inbound connection can be used for sending messages which is not supported on the lower level (as peeraddress is not available for inbound connections at the Node level).

misq.p2p.guard.Guard

Performs access control with multiple PermissionControl implementations. Proof of work is the core implementation but others might be provided as well. For sending messages the required tasks have to be performed and are represented in the AccessToken object stored in the GuardedMessage. At received messages the nodes checks if the provided AccessToken fulfills the requirement and only in that case forward the message to messageListeners.

misq.p2p.confidential.ConfidentialMessageService

Used for encrypted and signed messages sent to a peer. Only valid decrypted messages are forwarded to messageListeners. Handles also RelayMessage which are messages sent to a intermediary node to be forwared to the receiver node in case we do not support the receivers network type (e.g. a tor only node sends a message to a i2p only node using another node which supports both network types as relay).

misq.p2p.P2pNetworkService

The highest level layer supporting multiple network types. It creates for each network type (Tor, I2P, cleanet) the object structure which are operating in parallel using only the dataStorage as common shared object. It manages the guards for boostrapping, confidentialMessageServices for sending confidential messages and dataServices for the data related API (add, remove, retrieve).

misq.p2p.data.DataService

Has a reference to the guard node and the storage. It creates the router object used for dissemination of data related requests. AddDataRequests and RemoveDataRequests do not expect a response. For retrieving data we use a InventoryRequests carrying a filter object (can be a bloomfilter or the hash(es) of the requested data) and expect a InventoryResponse. The strategy which nodes are used for those requests and how many is responsibility of the router.

misq.p2p.storage.Storage

Maintains a hashmap of the data stored by a cryptographic key.

misq.p2p.router.Router

Manages different routers and the strategy how they are choosen. Currently there is only the GossipRouter, but a DHTRouter might get added as well.

misq.p2p.router.gossip.GossipRouter

Broadcasts messages to the peers provided by peerGroup.

misq.p2p.peers.PeerGroup

Maintains collections of connected peers, reported peers from other nodes and persisted peers.

misq.p2p.peers.PeerExchange

Request/response protocol for exchanging peers with other nodes.

misq.p2p.peers.PeerManager

Manages connections to peers. Use seed nodes as introduction nodes to the network. Maintains a target metric for connections.