libp2p
koganezawa@MoneyForward Financial
Agenda
● About libp2p
● libp2p architecture
● Let's code a blockchain!
○ About shared objects
○ Required import packages for P2P
○ BlockChain specific functions
○ Host to deal with incoming data steams
● Demo
About libp2p
➢ libp2p...
○ modular network stack ( network framework ) for creating p2p apps
○ the modular P2P network stack by IPFS for better decentralized computing
➢ Adopters
About libp2p
Polkadot
Ethereum2.0
MetaMask
OpenBazaar
Filecoin
IPFS
About libp2p
➢ Languages
About libp2p
➢ P2P Application vs. Web Application: What's the Difference?
■ client also acts as a server
■ It is not fixed function like server client model
● dial / listen both possible
libp2p architecture
➢ Indentity
➢ multiaddr
➢ Peer Routing
➢ Swarm
➢ Distributed Record Store
➢ Discovery
libp2p architecture
➢ Indentity
○ Upon first connecting, peers exchange public keys,
○ check: multihash(other.PublicKey) equals other.NodeId.
○ If not the connection is terminated.
➢ What is multihash?
○ Multihash is a protocol for differentiating outputs from various well-established cryptographic hash functions
○ allow multiple hash functions to coexist.
○ varint hash function code(tag: multihash)
■ https://github.com/multiformats/multicodec/blob/master/table.csv
➢ Indentity
○ ID for identifying distributed nodes
libp2p architecture
# Identity generation:
libp2p architecture
➢ multihash
# Format:
# Binary example (only 4 bytes for simplicity):
# Format:
➢ multiaddr
○ Multiple protocols are multiplexed using only one port
libp2p architecture
➢ Peer Routing
○ kad-routing
■ kad-routing implements the Kademlia Routing table
■ where each peer holds a set of k-buckets
■ each of them containing several PeerInfo objects from other peers in the network.
libp2p architecture
➢ Peer Routing
○ What is Kademlia?
■ Kademlia is a distributed hash table for decentralized peer-to-peer computer networks
designed by Petar Maymounkov and David Mazières in 2002
● Kademlia stores values in nodes whose ids are “nearest”
○ using XOR-distance to the key.
● The ID uses the hash value of the IP address.
● see: https://libp2p.github.io/js-libp2p-kad-dht/
libp2p architecture
001
➢ Peer Routing
○ mDNS-routing
■ mDNS-routing uses mDNS probes to identify if local area network peers have a given key or
they are simply present.
○ other-routing-mechanisms
■ other Peer Routing mechanisms can be implemented
libp2p architecture
➢ Swarm
○ Conn-a connection to a single Peer
■ MultiConn-a set of connections to a single Peer
■ SecureConn-an encrypted (tls-like) connection
○ Swarm-holds connections to Peers
■ multiplexes from / to each MultiConn
○ Muxer-Multiples between Services and Swarm. Handles Request / Reply.
■ Service-connects between an outside client service and Network.
■ Handler-the client service part that handles requests
libp2p architecture
➢ Distributed Record Store
○ A system to store and distribute records.
○ Records are small entries used by other systems for signaling, establishing links, announcing peers or
content, and so on. They have a similar role to DNS in the broader Internet.
libp2p architecture
➢ Discovery
○ mDNS-discovery
■ mDNS-discovery is a Discovery Protocol that uses mDNS over local area networks.
■ It emits mDNS beacons to find if there are more peers available.
○ Random-Walk
■ Random-Walk is a Discovery Protocol for DHTs (and other protocols with routing tables).
■ It makes random DHT queries in order to learn about a large number of peers quickly.
■ at the expense of a small load at the very beginning.
○ Bootstrap-List
■ Bootstrap-List is a Discovery Protocol that uses local storage to cache the addresses of highly
stable (and somewhat trusted) peers available in the network
libp2p architecture
➢ Required import packages for go-libp2p
Let's code a blockchain!
library module description
github.com/libp2p/go-libp2p libp2p dial / listen, network package
github.com/libp2p/go-libp2p-net
github.com/libp2p/go-libp2p-host
swarm protocol/stream muxer
github.com/libp2p/go-libp2p-peerstore distributed record store an object to manage peers, their addresses, and other
metadata about them.
github.com/libp2p/go-libp2p-peer utilities PKI-based identity management
github.com/libp2p/go-libp2p-protocol utilities type for protocol
github.com/libp2p/go-libp2p-crypto utilities various cryptographic utilities
➢ About shared objects
Let's code a blockchain!
➢ BlockChain specific functions
Let's code a blockchain!
➢ BlockChain specific functions
Let's code a blockchain!
➢ BlockChain specific functions
Let's code a blockchain!
➢ Host to deal with incoming data steams
Let's code a blockchain!
➢ New Stream
○ We have a peer ID and a target Addr so we add it to the peer store
○ so libp2p knows how to contact it
Let's code a blockchain!
➢ Host to deal with incoming data steams
○ ReadBlockChain
Let's code a blockchain!
➢ Host to deal with incoming data steams
○ WriteBlockChain is broadcast
Let's code a blockchain!
➢ Host to deal with incoming data steams
○ WriteBlockChain is broadcast
Let's code a blockchain!
➢ BasicHost
○ Generate host
Let's code a blockchain!
➢ Host to deal with incoming data steams
○ main
Let's code a blockchain!
Demo!
Next Action
➢ demo devp2p!!
● libp2p-specs
● libp2p.io
● ÐΞVp2p
● code-a-simple-p2p-blockchain-in-go
● ethereum-wireshark-dissectors
● ethereum-swarm
References

Try to implement Blockchain using libp2p

  • 1.
  • 2.
    Agenda ● About libp2p ●libp2p architecture ● Let's code a blockchain! ○ About shared objects ○ Required import packages for P2P ○ BlockChain specific functions ○ Host to deal with incoming data steams ● Demo
  • 3.
    About libp2p ➢ libp2p... ○modular network stack ( network framework ) for creating p2p apps ○ the modular P2P network stack by IPFS for better decentralized computing
  • 4.
  • 5.
  • 6.
    About libp2p ➢ P2PApplication vs. Web Application: What's the Difference? ■ client also acts as a server ■ It is not fixed function like server client model ● dial / listen both possible
  • 7.
    libp2p architecture ➢ Indentity ➢multiaddr ➢ Peer Routing ➢ Swarm ➢ Distributed Record Store ➢ Discovery
  • 8.
    libp2p architecture ➢ Indentity ○Upon first connecting, peers exchange public keys, ○ check: multihash(other.PublicKey) equals other.NodeId. ○ If not the connection is terminated. ➢ What is multihash? ○ Multihash is a protocol for differentiating outputs from various well-established cryptographic hash functions ○ allow multiple hash functions to coexist. ○ varint hash function code(tag: multihash) ■ https://github.com/multiformats/multicodec/blob/master/table.csv
  • 9.
    ➢ Indentity ○ IDfor identifying distributed nodes libp2p architecture # Identity generation:
  • 10.
    libp2p architecture ➢ multihash #Format: # Binary example (only 4 bytes for simplicity):
  • 11.
    # Format: ➢ multiaddr ○Multiple protocols are multiplexed using only one port libp2p architecture
  • 12.
    ➢ Peer Routing ○kad-routing ■ kad-routing implements the Kademlia Routing table ■ where each peer holds a set of k-buckets ■ each of them containing several PeerInfo objects from other peers in the network. libp2p architecture
  • 13.
    ➢ Peer Routing ○What is Kademlia? ■ Kademlia is a distributed hash table for decentralized peer-to-peer computer networks designed by Petar Maymounkov and David Mazières in 2002 ● Kademlia stores values in nodes whose ids are “nearest” ○ using XOR-distance to the key. ● The ID uses the hash value of the IP address. ● see: https://libp2p.github.io/js-libp2p-kad-dht/ libp2p architecture 001
  • 14.
    ➢ Peer Routing ○mDNS-routing ■ mDNS-routing uses mDNS probes to identify if local area network peers have a given key or they are simply present. ○ other-routing-mechanisms ■ other Peer Routing mechanisms can be implemented libp2p architecture
  • 15.
    ➢ Swarm ○ Conn-aconnection to a single Peer ■ MultiConn-a set of connections to a single Peer ■ SecureConn-an encrypted (tls-like) connection ○ Swarm-holds connections to Peers ■ multiplexes from / to each MultiConn ○ Muxer-Multiples between Services and Swarm. Handles Request / Reply. ■ Service-connects between an outside client service and Network. ■ Handler-the client service part that handles requests libp2p architecture
  • 16.
    ➢ Distributed RecordStore ○ A system to store and distribute records. ○ Records are small entries used by other systems for signaling, establishing links, announcing peers or content, and so on. They have a similar role to DNS in the broader Internet. libp2p architecture
  • 17.
    ➢ Discovery ○ mDNS-discovery ■mDNS-discovery is a Discovery Protocol that uses mDNS over local area networks. ■ It emits mDNS beacons to find if there are more peers available. ○ Random-Walk ■ Random-Walk is a Discovery Protocol for DHTs (and other protocols with routing tables). ■ It makes random DHT queries in order to learn about a large number of peers quickly. ■ at the expense of a small load at the very beginning. ○ Bootstrap-List ■ Bootstrap-List is a Discovery Protocol that uses local storage to cache the addresses of highly stable (and somewhat trusted) peers available in the network libp2p architecture
  • 18.
    ➢ Required importpackages for go-libp2p Let's code a blockchain! library module description github.com/libp2p/go-libp2p libp2p dial / listen, network package github.com/libp2p/go-libp2p-net github.com/libp2p/go-libp2p-host swarm protocol/stream muxer github.com/libp2p/go-libp2p-peerstore distributed record store an object to manage peers, their addresses, and other metadata about them. github.com/libp2p/go-libp2p-peer utilities PKI-based identity management github.com/libp2p/go-libp2p-protocol utilities type for protocol github.com/libp2p/go-libp2p-crypto utilities various cryptographic utilities
  • 19.
    ➢ About sharedobjects Let's code a blockchain!
  • 20.
    ➢ BlockChain specificfunctions Let's code a blockchain!
  • 21.
    ➢ BlockChain specificfunctions Let's code a blockchain!
  • 22.
    ➢ BlockChain specificfunctions Let's code a blockchain!
  • 23.
    ➢ Host todeal with incoming data steams Let's code a blockchain!
  • 24.
    ➢ New Stream ○We have a peer ID and a target Addr so we add it to the peer store ○ so libp2p knows how to contact it Let's code a blockchain!
  • 25.
    ➢ Host todeal with incoming data steams ○ ReadBlockChain Let's code a blockchain!
  • 26.
    ➢ Host todeal with incoming data steams ○ WriteBlockChain is broadcast Let's code a blockchain!
  • 27.
    ➢ Host todeal with incoming data steams ○ WriteBlockChain is broadcast Let's code a blockchain!
  • 28.
    ➢ BasicHost ○ Generatehost Let's code a blockchain!
  • 29.
    ➢ Host todeal with incoming data steams ○ main Let's code a blockchain!
  • 30.
  • 31.
  • 32.
    ● libp2p-specs ● libp2p.io ●ÐΞVp2p ● code-a-simple-p2p-blockchain-in-go ● ethereum-wireshark-dissectors ● ethereum-swarm References