Skip to content

Commit e8123d3

Browse files
authored
docs: add initial architecture diagrams for js-libp2p (#2121)
1 parent 5a6a437 commit e8123d3

File tree

5 files changed

+219
-37
lines changed

5 files changed

+219
-37
lines changed

doc/ARCHITECTURE.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# Libp2p Architecture
2+
3+
js-libp2p is comprised of a number of components that work together to provide functionality such as dailling peers, managing connections, registering protocols, storing information about peers and much more. This document aims to provide a high level overview of the components and how they interact with each other.
4+
5+
- [Libp2p Architecture](#libp2p-architecture)
6+
- [Component Diagram](#component-diagram)
7+
- [Sequence Diagrams](#sequence-diagrams)
8+
- [Dialing a Peer](#dialing-a-peer)
9+
- [Opening a stream on a connection](#opening-a-stream-on-a-connection)
10+
11+
## Component Diagram
12+
13+
```mermaid
14+
flowchart TB
15+
direction TB
16+
subgraph Components
17+
direction TB
18+
PeerId ~~~
19+
Events ~~~
20+
ConnectionGater ~~~
21+
Upgrader
22+
AddressManager ~~~
23+
ConnectionManager ~~~
24+
TransportManager ~~~
25+
Registrar
26+
PeerStore ~~~
27+
Datastore ~~~
28+
PeerRouting ~~~
29+
_xx[ ]
30+
ContentRouting ~~~
31+
Metrics ~~~
32+
ConnectionProtector ~~~
33+
_x[ ]
34+
35+
style _x opacity:0;
36+
style _xx opacity:0;
37+
38+
end
39+
40+
subgraph Connections[Connection Configuration]
41+
direction TB
42+
43+
subgraph Transports
44+
direction TB
45+
TCP
46+
WebRTC
47+
Websocket
48+
Webtransport
49+
end
50+
51+
subgraph Encryption[Connection Encryptions]
52+
direction TB
53+
Noise
54+
Plaintext
55+
end
56+
57+
subgraph Multiplexer[Stream Multiplexers]
58+
direction TB
59+
Yamux
60+
Mplex
61+
end
62+
63+
Multiplexer ~~~ Encryption ~~~ Transports
64+
65+
end
66+
```
67+
68+
```mermaid
69+
---
70+
title: Components Dependency Graph
71+
---
72+
flowchart TD
73+
PeerId
74+
Events
75+
ConnectionGater
76+
Upgrader
77+
AddressManager
78+
ConnectionManager
79+
TransportManager
80+
Registrar
81+
PeerStore
82+
Datastore
83+
PeerRouting
84+
ContentRouting
85+
Metrics
86+
ConnectionProtector
87+
88+
%% AddressManager
89+
PeerId --> AddressManager
90+
TransportManager --> AddressManager
91+
PeerStore --> AddressManager
92+
Events --> AddressManager
93+
94+
%% ConnectionManager
95+
PeerId --> ConnectionManager
96+
Metrics --> ConnectionManager
97+
PeerStore --> ConnectionManager
98+
TransportManager --> ConnectionManager
99+
ConnectionGater --> ConnectionManager
100+
Events --> ConnectionManager
101+
102+
%% TransportManager
103+
Metrics --> TransportManager
104+
AddressManager --> TransportManager
105+
Upgrader --> TransportManager
106+
Events --> TransportManager
107+
108+
%% Upgrader
109+
PeerId --> Upgrader
110+
Metrics --> Upgrader
111+
ConnectionManager --> Upgrader
112+
ConnectionGater --> Upgrader
113+
ConnectionProtector --> Upgrader
114+
Registrar --> Upgrader
115+
PeerStore --> Upgrader
116+
Events --> Upgrader
117+
118+
%% Registrar
119+
PeerId --> Registrar
120+
ConnectionManager --> Registrar
121+
PeerStore --> Registrar
122+
Events --> Registrar
123+
124+
%% PeerStore
125+
PeerId --> PeerStore
126+
Datastore --> PeerStore
127+
Events --> PeerStore
128+
129+
%% PeerRouting
130+
PeerId --> PeerRouting
131+
PeerStore --> PeerRouting
132+
133+
%% ContentRouting
134+
PeerStore --> ContentRouting
135+
```
136+
137+
## Sequence Diagrams
138+
139+
These diagrams show the interactions between the components in common scenarios. They are not exhaustive and are intended to provide a high level overview of the interactions between the components.
140+
141+
### Dialing a Peer
142+
143+
This illustrates an outbound connection being established to a peer.
144+
145+
```mermaid
146+
%% how an outbound connection is opened when a user calls .dial(),
147+
%% assuming user is not connected to the PeerId for the
148+
%% Multiaddr that was dialed.
149+
%%
150+
%% This is
151+
%%
152+
sequenceDiagram
153+
User->>+libp2p: dial a multiaddr `.dial()`
154+
libp2p->>+Connection Manager: open a connection for me to MA `.openConnection()`
155+
%% obfuscating the dial queue.
156+
%% Connection Manager->>+Transport Manager: Choose transport to use for Multiaddr
157+
Connection Manager->>+Transport Manager: Network level reach out `.dial()`
158+
Transport Manager->>+Transport: Get MultiaddrConn `socket + multiaddr`
159+
%% Transport->>+Transport Manager: Return MultiaddrConn `socket + multiaddr`
160+
%% how the upgrade happens is transport specific, so transports directly call upgrader
161+
Transport-->>+Upgrader: upgrade my connection??
162+
Upgrader-->>+Upgrader: Perform upgrade (see other diagram)
163+
Upgrader->>+Connection Manager: Connection (link to interface)
164+
%% Connection Manager->>+Connection Manager: Connection (link to interface)
165+
Connection Manager->>+User: Connection (link to interface)
166+
```
167+
168+
169+
### Opening a stream on a connection
170+
171+
This illustrates a stream being opened on an existing connection that will echo a message back to the sender. This assumes that a stable connection has been established between the two peers.
172+
173+
```mermaid
174+
%% pushing data over stream
175+
%% register stream handler, local opens a stream for proto, send data,
176+
%% remote receives data and sends data back
177+
%% local receives data
178+
%% stream may or may not then be closed.
179+
%% Local is the node sending data, Remote is other peer the conn is with
180+
%% Echo protocol
181+
sequenceDiagram
182+
box Local side
183+
participant Local
184+
participant Connection
185+
participant LocalMuxer
186+
end
187+
participant Stream
188+
box pink Connection
189+
end
190+
box Remote side
191+
participant Remote
192+
participant RemoteMuxer
193+
participant RemoteUpgrader
194+
participant RemoteRegistrar
195+
participant RemoteStreamHandler
196+
end
197+
198+
Remote->>RemoteRegistrar: Register Stream Handler `libp2p.handle`
199+
%% only register stream handlers when you want to listen for protocols. SENDERs do not need to listen
200+
Local->>Connection: Open outbound stream
201+
Connection->>LocalMuxer: Open stream
202+
LocalMuxer->>RemoteMuxer: Open stream
203+
RemoteMuxer->>RemoteUpgrader: notify Stream created
204+
Note over Connection,RemoteUpgrader: multi stream select handles protocol negotiation
205+
Connection->>Local: return Stream
206+
RemoteUpgrader->>RemoteRegistrar: select stream handler
207+
RemoteRegistrar->>RemoteStreamHandler: handle stream
208+
Note over RemoteStreamHandler,Local: Stream data flow & control is dictated by protocol, below is example of "echo"
209+
activate Stream
210+
Local->>Stream: send bytes "hello world"
211+
Stream->>RemoteStreamHandler: receive bytes "hello world"
212+
%% RemoteStreamHandler->>+RemoteStreamHandler: [echo] pipe back received bytes
213+
RemoteStreamHandler->>Stream: echo bytes back to sender
214+
Stream->>Local: receive echoed bytes
215+
deactivate Stream
216+
217+
```

doc/CONFIGURATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ libp2p is a modular networking stack. It's designed to be able to suit a variety
4444

4545
Regardless of how you configure libp2p, the top level [API](./API.md) will always remain the same. **Note**: if some modules are not configured, like Content Routing, using those methods will throw errors.
4646

47+
To get a high-level overview of the js-libp2p architecture, please read the [Architecture](./ARCHITECTURE.md) document.
48+
4749
## Modules
4850

4951
`js-libp2p` acts as the composer for this modular p2p networking stack using libp2p compatible modules as its subsystems. For getting an instance of `js-libp2p` compliant with all types of networking requirements, it is possible to specify the following subsystems:

doc/CONNECTION_MANAGER.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

doc/DIALER.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/nat-traversal/README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)