1 of 45

CORE COURSE B

SOLVING DISTRIBUTED NETWORKING PROBLEMS WITH LIBP2P

Jacob Heun

@jacobheun

Cole Brown

@bigs

Alex Potsides

@achingbrain

2 of 45

  • JS: libp2p/js-libp2p-examples
    • ./chat/browser
    • ./chat/nodejs
  • GO: libp2p/go-libp2p-examples
    • ./ipfs-camp-2019

GETTING THE STARTER CODE

Setup

3 of 45

  • The networking stack IPFS uses to communicate
  • A modular system of protocols that can be used to solve specific, distributed networking problems.

WHAT IS LIBP2P?

4 of 45

WHAT ARE WE BUILDING?

5 of 45

Chat App

  • Connecting to the network
  • Encrypting connections
  • Creating our own protocol
  • Reusing connections
  • Discovering Peers
  • Connecting to NAT’d nodes
  • Handling more complex data on our protocol
  • Broadcasting data with Pubsub
  • Finding peers with the DHT

WHY A CHAT APP?

6 of 45

Connecting to Peers:

  • Libp2p uses Transports to connect to peers
  • Transports include dialing and listening components
  • Not all Transports will be able to listen (browsers)

TRANSPORTS

19

PEERS

90

PEERS

93

PEERS

77

PEERS

33

PEERS

87

PEERS

17

PEERS

7 of 45

Transport

Browser

Node

Go

Rust

TCP

Websockets

WebRTC

QUIC

SELECTING TRANSPORTS

8 of 45

Connecting to Peers:

  • Let’s add
    • TCP
    • Websockets
    • WebRTC ( JS only )
      • Direct browser to browser dialing and discovery
      • Ability to listen via an intermediary node

CONFIGURING TRANSPORTS

9 of 45

< Configure the Transports >

01 Transports

02 Multiaddrs

03 Muxing & Encryption

04 Protocol

01

10 of 45

Connecting to Peers:

  • Most commonly the first peer will be a Bootstrap peer
    • A well known peer used to join the network
  • We need an address for the peer!

THE FIRST CONNECTION

11 of 45

Connecting to Peers:

  • Allow us to compose flexible addresses in a path structure
  • Phone numbers, domains, postal addresses all have �different formats
  • multiaddrs can tell us a lot about the peer we want to contact

MULTIADDRESS

(MULTIADDRS)

12 of 45

Connecting to Peers:

  • /ip4/127.0.0.1/tcp/4001
    • IP(v4): 127.0.0.1, Port: 4001, Transport: TCP

  • /ip4/127.0.0.1/tcp/4002/ws
    • IP(v4): 127.0.0.1, Port: 4002, Transport: Websocket over a TCP connection

MULTIADDRS

13 of 45

Connecting to Peers:

  • /ip4/0.0.0.0/tcp/15555/ws/p2p-webrtc-star
    • A websocket address at 0.0.0.0, port 15555
    • /p2p-webrtc-star indicates a webRTC rendezvous/signaling server
    • Our target peer is available at that signaling server

  • /ip4/127.0.0.1/udp/4001/quic/p2p/QmRelay/p2p-circuit
    • A Quic address running over UDP at 127.0.0.1, port 4001
    • /p2p-circuit indicates our target peer is available over a relay
    • Our target peer is available over the relay QmRelay at that Quic address

MULTIADDRS

14 of 45

Connecting to Peers:

  • Setup multiaddrs for us to listen on (if possible)
  • Connect to our destination peer via its multiaddr

ADDING THE MULTIADDR

15 of 45

< Add the multiaddrs >

01 Transports

02 Multiaddrs

03 Muxing & Encryption

04 Protocol

02

16 of 45

17 of 45

  • We’re connected to a node and we can send it data
  • How do we reuse a connection to handle more complex communication?
    • Pubsub messages
    • DHT queries
    • Circuit relaying
    • Direct communication
    • Our Chat protocol
    • All future protocols

REUSING CONNECTIONS

18 of 45

REUSING CONNECTIONS

19 of 45

REUSING CONNECTIONS

20 of 45

Reusing Connections

  • Isolate communication to individual streams
    • A TCP connection multiplexes over many ports
    • Telephone line connections multiplex via different frequencies
    • Mplex multiplexes by prepending data with stream identified headers
  • Muxers
    • Mplex, Yamux, Spdy, QUIC (built in), muxado

STREAM MULTIPLEXING (MUXING)

21 of 45

< Configure the Muxers >

04 Protocol

03 Muxing & Encryption

02 Multiaddrs

01 Transports

03

22 of 45

23 of 45

  • Encrypt all data aside from the initial encryption protocol selection
  • Supported protocols
  • Secio, TLS1.3, Noise, Quic Transport

ENCRYPTING DATA

24 of 45

< Configure encryption >

01 Transports

02 Multiaddrs

03 Muxing & Encryption

04 Protocol

03

25 of 45

26 of 45

  • What are they?
    • /multistream/1.0.0
    • /ipfs/ping/1.0.0
    • /libp2p/circuit/relay/0.1.0
    • ...

PROTOCOLS IN LIBP2P

27 of 45

Protocols

  • We’re building a chat app
  • Let’s create a chat protocol
  • /libp2p/chat/1.0.0

CREATING A PROTOCOL

28 of 45

< Create the Protocol >

04

05 Discovery

04 Protocol

03 Muxing & Encryption

02 Multiaddrs

29 of 45

5 minutes

TIME FOR BREAK!

30 of 45

Chatting with a Robot, how fun!

31 of 45

  • Bootstrap
  • MDNS
  • DHT
  • Rendezvous Servers
  • Bluetooth?
  • NFC?
  • ...

PEER DISCOVERY

32 of 45

Peer Discovery

  • Find a Random ID
    • “Brandon"
  • Ask our “closest” known peers
  • Continue until we find no closer peers

DHT RANDOM WALK

Bob

Me

Alice

Brenda

33 of 45

Peer Discovery

  • Bootstrap
  • MDNS
  • DHT Random Walk
  • Rendezvous Server (WebRTC Signaling)

MULTIPLE DISCOVERY SERVICES

34 of 45

< Configure discovery >

03 Muxing & Encryption

04 Protocol

05 Discovery

06 Pubsub

05

35 of 45

Recap

  • We can connect to peers
  • We can discover peers
  • We can chat with connected peers
  • Chatting is done over a direct connection
    • So, how can we make this better?

WELCOME BACK!

36 of 45

Pubsub

  • Floodsub
  • Gossipsub

BROADCASTING MESSAGES

37 of 45

Pubsub

GOSSIPSUB OR FLOODSUB?

Transport

JS

Go

Rust

Gossipsub

Floodsub

38 of 45

< Code chat over Pubsub >

04 Protocol

05 Discovery

06 Pubsub

07 Messaging

06

39 of 45

Protobuf

  • We’re chatting over Pubsub, hooray!
    • Messages are being exchanged using Protobuf
    • The Protobuf declaration also has other request types, such as UpdatePeer

Complex Messaging

40 of 45

message Request {

enum Type {

SEND_MESSAGE = 0;

UPDATE_PEER = 1;

}

required Type type = 1;

optional SendMessage sendMessage = 2;

optional UpdatePeer updatePeer = 3;

}

message SendMessage {

required bytes data = 1;

required int64 created = 2;

required bytes id = 3;

}

message UpdatePeer {

optional bytes userHandle = 1;

}

41 of 45

< Complex Messaging >

07

07 Messaging

06 Pubsub

05 Discovery

04 Protocol

42 of 45

Subnets

How do we broadcast to a small set of peers in a network of millions?

ONE OF MANY

43 of 45

Subnets

  • Provider Records
  • Query similar to Random Walk

BACK TO THE DHT

QmBob

QmMe

QmAlice

QmBrenda

44 of 45

Subnets

  • /libp2p/chat/1.0.0
  • Register as a provider in the DHT on subscribe
  • Find providers on subscribe to connect to other topic peers

APPLYING THE DHT TO OUR CHAT

45 of 45

CLOSING THOUGHTS

The End!

https://github.com/js-libp2p-examples

JS EXAMPLE

discuss.libp2p.io

FORUM

https://github.com/go-libp2p-examples

GO EXAMPLE