ecosistema-social-decentral.../protocols/gun.md

4.7 KiB

Gun

Gun is a distributed database with a graph structure. Related libraries include a constellation of tools for merging conflicting data and handling routing and security.

In GunDB's graph store, entries are JSON objects under UUID keys. Data is stored in browser localStorage by default, with backup "supernode" servers to ensure persistence. Peers connect over websockets to other peers, and choose what data to synchronize and persist.

Identity

Gun's SEA module provides the capability to create a public/private keypair for a user.

Network structure

Gun uses websockets to transfer data between peers. When a client puts something in a graph, a message is sent to the network and other clients that have subscribed to that data pick it up and update their state. Those who are offline will eventually receive those updates.

Gun uses a flooding algorithm for message propagation, called DAM (Daisy-chain ad-hoc mesh-network). DAM's API allows for the swapping out of transport layers (websockets, WebRTC, etc). A proposed future routing protocol, the AXE protocol, includes a mechanism for monetization through bandwidth-based payments.

Data

Gun saves data two ways, in a client browser's localStorage, and through RAD, a storage adaptor that stores data on disk using a radix tree. Clients subscribe to the data they need from the "superpeer", a persistent node storing data on disk that should be run with most applications to ensure availability of data when other browser-based peers are offline. Superpeers are servers running node.js and persisting data on disk. Clients may serve the data they have to other clients as well.

Peers connect to graphs relevant to their application's logic. There is no universally shared graph across the network.

GunDB uses CRDTs (Conflict-free Replicated Data Types). Conflicts are handled by a conflict resolution algorithm that uses lexical sort. Gun is eventually consistent, not strongly consistent, meaning that peers will eventually converge upon the last updated value.

Gun focuses on mutability by not using an append-only CRDT, instead allowing insertion and deletion into the graph. Deletion works by inserting null at the deleted node, de-referencing it.

Filtering

There is a GraphQL API for the gun p2p graph database.

Privacy and Access Control

Privacy and access control can be built on top of GunDB's graph by encrypting data for the intended user. Gun's encryption utilities, SEA (Security, Encryption, and Authorization), intend to make it easier to add private space to applications.

For access control, cryptographic keys are assigned to roles, groups, or data points. This information is either used to derive a shared ECDH secret to decrypt (read), or to load collaborative multi-writer edits (signed).

Iris, a decentralized social network building on Gun, has a library for encrypted GUN channels and contact list management.

Interoperablity

Plugins, such as backup storage on centralized databases or file systems, can be used to extend GUN.

Scalability

Metrics

Monetization

The GUN protocol is developed by a VC-funded company.

Implementations

  • HackerNoon integrated Gun for annotations
  • Meething is a Mozilla backed secure & decentralized video conferencing that uses GUN
  • Party, an encrypted messaging browser extension, uses Gun
  • Notabug, a decentralized Reddit clone, uses Gun
  • DTube uses GunDB for messaging
  • Iris, a web-of-trust based social network, is built on Gun

Comparisons can be made to other databases designed for p2p use cases:

OrbitDB - see under IPFS.md

HyperDB - see under hypercore.md

KappaDB

Site Documentation Clarification