ipfs-cap2pfs.tex 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. \documentclass{sig-alternate}
  2. \usepackage{tikz}
  3. \usetikzlibrary{arrows}
  4. \usetikzlibrary{trees}
  5. \usetikzlibrary{positioning}
  6. \usepackage{array}
  7. \usepackage{amstext}
  8. \usepackage{mathtools}
  9. \DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
  10. \begin{document}
  11. \title{IPFS - Content Addressed, Versioned, P2P File System (DRAFT 2)}
  12. \subtitle{}
  13. \numberofauthors{1}
  14. \author{
  15. \alignauthor
  16. Juan Benet\\
  17. \email{juan@benet.ai}
  18. }
  19. \maketitle
  20. \begin{abstract}
  21. The InterPlanetary File System is a peer-to-peer distributed file system
  22. capable of sharing the same files with millions of nodes. IPFS combines a
  23. distributed hashtable, cryptographic techniques, merkle trees, content-
  24. addressable storage, bittorrent, and tag-based filesystems to build a single
  25. massive file system shared between peers. IPFS has no single point of failure,
  26. and nodes do not need to trust each other.
  27. \end{abstract}
  28. \section{Introduction}
  29. [Motivate IPFS. Introduce problems. Describe BitTorrent existing problems (
  30. multiple files. one swarm. sloppy dht implementation.) Describe version
  31. control efforts. Propose potential combinations of good ideas.]
  32. [Cite:
  33. CFS,
  34. Kademlia,
  35. Bittorrent,
  36. Chord,
  37. DHash,
  38. SFS,
  39. Ori,
  40. Coral]
  41. This paper introduces
  42. IPFS, a novel peer-to-peer version-controlled filesystem;
  43. and BitSwap, the novel peer-to-peer block exchange protocol serving IPFS.
  44. %The rest of the paper is organized as follows.
  45. %Section 2 describes the design of the filesystem.
  46. %Section 3 evaluates various facets of the system under benchmark and common
  47. %workloads.
  48. %Section 4 presents and evaluates a world-wide deployment of IPFS.
  49. %Section 5 describes existing and potential applications of IPFS.
  50. %Section 6 discusses related and future work.
  51. Notation Notes:
  52. (a) data structures are specified in Go syntax,
  53. (b) rpc protocols are specified in capnp interfaces,
  54. (c) wire protocols are specified in capnp schemas.
  55. \section{Background}
  56. This section reviews important properties of successful peer-to-peer systems, which IPFS combines.
  57. \subsection{Distributed Hash Tables}
  58. Distributed Hash Tables (DHTs) are widely used to coordinate and maintain metadata about peer-to-peer systems. For example, the BitTorrent MainlineDHT tracks sets of peers part of a torrent swarm.
  59. \subsubsection{Kademlia DHT}
  60. Kademlia \cite{Kademlia} is a popular DHT that provides:
  61. \begin{enumerate}
  62. \item Efficient lookup through massive networks:
  63. queries on average contact $ \ceil{log_2 (n)} $ nodes.
  64. (e.g. $20$ hops for a network of $10,000,000$ nodes).
  65. \item Low coordination overhead: it optimizes the number of
  66. control messages it sends to other nodes.
  67. \item Resistance to various attacks, by preferring nodes who have been
  68. part of the DHT longer.
  69. \item wide useage in peer-to-peer applications, including \\
  70. Gnutella and Bittorrent, forming networks of over 100 million nodes.
  71. \end{enumerate}
  72. \subsubsection{Coral DSHT}
  73. While some peer-to-peer filesystems store data blocks directly in DHTs,
  74. this ``wastes storage and bandwidth, as data must be stored at nodes where it
  75. is not needed'' \cite{Coral}. Coral extends Kademlia in three particularly important ways:
  76. \begin{enumerate}
  77. \item Kademlia stores values in nodes whose ids are ``nearest'' (using
  78. XOR-distance) to the key. This does not take into account application
  79. data locality, ignores ``far'' nodes who may already have the data, and
  80. forces ``nearest'' nodes to store it, whether they need it or not.
  81. This wastes significant storage and bandwith. Instead, Coral stores
  82. addresses to peers who can provide the data blocks.
  83. \item Coral relaxes the DHT API from \texttt{get\_value(key)} to
  84. \texttt{get\_any\_values(key)} (the ``sloppy'' in DSHT).
  85. This still works since Coral users only need a single (working) peer,
  86. not the complete list. In return, Coral can distribute only subsets of
  87. the values to the ``nearest'' nodes, avoiding hot-spots (overloading
  88. \textit{all the nearest nodes} when a key becomes popular).
  89. \item Additionally, Coral organizes a hierarchy of separate DSHTs called
  90. \textit{clusters} depending on region and size. This enables nodes to
  91. query peers in their region first, ``finding nearby data without
  92. querying distant nodes'' and greatly reducing the latency of
  93. lookups.
  94. \end{enumerate}
  95. \subsubsection{S/Kademlia DHT}
  96. S/Kademlia extends Kademlia to protect against malicious attacks:
  97. \begin{enumerate}
  98. \item S/Kademlia provides schemes to secure \texttt{NodeId} generation,
  99. and prevent Sybill attacks. It requires nodes to create a PKI key pair, derive their identity from it, and sign their messages to each other. One scheme includes a proof-of-work crypto puzzle to make generating Sybills expensive.
  100. \item S/Kademlia nodes lookup values over disjoint paths, in order to
  101. ensure honest nodes can connect to each other in the presence of a large fraction of adversaries in the network. S/Kademlia achieves a success rate of 0.85 even with an adversarial fraction as large as half of the nodes.
  102. \end{enumerate}
  103. \subsection{Block Exchanges - BitTorrent}
  104. BitTorrent \cite{BitTorrent} is a widely successful peer-to-peer filesharing system, which succeeds in coordinating networks of untrusting peers (swarms) to cooperate in distributing pieces of files to each other. Key BitTorrent features that inform IPFS design:
  105. \begin{enumerate}
  106. \item BitTorrent's data exchange protocol uses a quasi tit-for-tat strategy
  107. which rewards nodes that contribute to each other, and punishes nodes who would only leech others' resources.
  108. \item BitTorrent peers track the availability of file pieces, prioritizing
  109. sending rarest-first. This takes load off seeds, making non-seed peers capable of trading with each other.
  110. \item BitTorrent's standard tit-for-tat is vulnerable to some exploitative
  111. bandwidth sharing strategies. PropShare \cite{propshare} is a different peer bandwidth allocation strategy that better resists exploitative strategies, and improves the performance of swarms.
  112. \end{enumerate}
  113. \subsection{Version Control Systems - Git}
  114. Version Control Systems provide facilities to model files changing over time and distribute different versions efficiently. The popular version control system Git provides a powerful Merkle DAG \footnote{Merkle Directed Acyclic Graph -- similar but more general construction than a Merkle Tree. Deduplicated, does not need to be balanced, and non-leaf nodes contain data.} object model that captures changes to a filesystem tree in a distributed-friendly way.
  115. \begin{enumerate}
  116. \item Immutable objects represent Files (\texttt{blob}), Directories (\texttt{tree}), and Changes (\texttt{commit}).
  117. \item Objects are content-addressed, by the cryptographic hash of their contents.
  118. \item Links to other objects are embedded, forming a Merkle DAG. This
  119. provides many useful integrity and workflow properties.
  120. \item Most versioning metadata (branches, tags, etc) are simply pointer references, and thus inexpensive to create and update.
  121. \item Version changes only update references or add objects.
  122. \item Distributing version changes to other users is simply transferring objects and updating remote references.
  123. \end{enumerate}
  124. \section{Design}
  125. IPFS is a distributed file system which synthesizes successful ideas from previous peer-to-peer sytems, including DHTs, BitTorrent, Git, and SFS. The contribution of IPFS is simplifying, evolving, and connecting proven techniques into a single cohesive system, greater than the sum of its parts. IPFS presents a new platform for writing and deploying applications, a new system for distributing and versioning large data, and could evolve the web itself.
  126. \subsection{IPFS Nodes}
  127. IPFS is peer-to-peer; no nodes are privileged. Nodes are identified by a \texttt{NodeId}, the cryptographic hash of a public-key (note that \textit{checksum} will henceforth refer specifically to cryptographic hashes of an object), created as in \cite{skademlia}. Nodes store their public and private keys. Users are free to instatiate a ``new'' node identity on every launch, though that loses accrued network benefits. Nodes are incentivized to remain the same.
  128. \begin{verbatim}
  129. type Checksum string
  130. type PublicKey string
  131. type PrivateKey string
  132. type NodeId Checksum
  133. type Node struct {
  134. nodeid NodeID
  135. pubkey PublicKey
  136. prikey PrivateKey
  137. }
  138. \end{verbatim}
  139. IPFS nodes store IPFS objects (which represent files and other data structures) in local storage. Nodes transfer objects to each other. The IPFS Protocol is divided into a stack of sub-protocols responsible for different functionality:
  140. \begin{enumerate}
  141. \item \textbf{Network} - manages connections to other peers, using various underlying network protocols. Configurable. Described in Section 2.2.
  142. \item \textbf{Routing} - maintains information to locate specific peers and objects. Responds to both local and remote queries. Defaults to a DHT, but is swappable. Described in Section 2.3.
  143. \item \textbf{Exchange} - a block exchange protocol (BitSwap) that governs efficient block distribution. Modelled as a market, weakly intentivizes replication. Trade Strategies swappable. Described in Section 2.4.
  144. \item \textbf{Objects} - a Merkle DAG of content-addressed immutable objects with links. Used to represent arbitrary datastructures, e.g. file hierarchies and communication systems. Described in Section 2.5.
  145. \item \textbf{Files} - a versioned file-system inspired by Git. Described in Section 2.6.
  146. \item \textbf{Naming} - A self-certifying mutable name system. Described in Section 2.7.
  147. \end{enumerate}
  148. These subsystems are not independent. They are well integrated and leverage
  149. their blended properties. However, it is useful to describe them separately,
  150. building the protocol stack from the bottom up.
  151. \subsection{Connectivity}
  152. IPFS Nodes communicate regualrly with hundreds of other nodes in the network across the wide internet. It can use any reliable transport protocol, and it is best suited for LEDBAT \ref{LEDBAT} (uTP) \ref{uTP}. IPFS also uses the ICE NAT traversal techniques \ref{ICE} to increase connectivity between peers.
  153. \subsection{Routing}
  154. IPFS nodes maintain a DSHT based on S/Kademlia and Coral, to coordinate
  155. and identify which nodes can serve a particular block of data.
  156. \subsubsection{IPFS DSHT}
  157. Instead, IPFS stores a list of peers that can provide the data block.
  158. The IPFS DSHT supports four RPC calls:
  159. \subsection{Block Exchange - BitSwap Protocol}
  160. The exchange of data in IPFS happens by exchanging blocks with peers using a
  161. BitTorrent inspired protocol: BitSwap. Like BitTorrent, BitSwap peers are
  162. looking to acquire a set of blocks, and have blocks to offer in exchange.
  163. Unlike BitTorrent, BitSwap is not limited to the blocks in one torrent.
  164. BitSwap operates as a persistent marketplace where node can acquire the
  165. blocks they need, regardless of what files the blocks are part of. The
  166. blocks could come from completely unrelated files in the filesystem.
  167. But nodes come together to barter in the marketplace.
  168. While the notion of a barter system implies a virtual currency could be
  169. created, this would require a global ledger (blockchain) to track ownership
  170. and transfer of the currency. This will be explored in a future paper.
  171. Instead, BitSwap nodes have to provide direct value to each other
  172. in the form of blocks. This works fine when the distribution of blocks across
  173. nodes is such that they have the complements, what each other wants. This will
  174. seldom be the case. Instead, it is more likely that nodes must \textit{work}
  175. for their blocks. In the case that a node has nothing that its peers want (or
  176. nothing at all), it seeks the pieces its peers might want, with lower
  177. priority. This incentivizes nodes to cache and disseminate rare pieces, even
  178. if they are not interested in them directly.
  179. \subsubsection{BitSwap Credit}
  180. The protocol must also incentivize nodes to seed when they do not need
  181. anything in particular, as they might have the blocks others want. Thus,
  182. BitSwap nodes send blocks to their peers, optimistically expecting the debt to
  183. be repaid. But, leeches (free-loading nodes that never share) must be avoided. A simple credit-like system solves the problem:
  184. \begin{enumerate}
  185. \item Peers track their balance (in bytes verified) with other nodes.
  186. \item Peers send blocks to debtor peers probabilistically, according to
  187. a function that falls as debt increases.
  188. \end{enumerate}
  189. Note that if a peer decides not to send, the peer subsequently ignores the
  190. other node for an \texttt{ignore\_cooldown} timeout. This prevents senders
  191. from trying to game the probability by just causing more dice-rolls.
  192. (Default BitSwap is 10 seconds).
  193. \subsubsection{BitSwap Strategy}
  194. The differing strategies that BitSwap peers might employ have wildly different
  195. effects on the performance of the exchange as a whole. In BitTorrent,
  196. while a standard strategy is specified (tit-for-tat), a variety of others have
  197. been implemented, ranging from BitTyrant (sharing the least-possible),
  198. to BitThief (exploiting a vulnerability and never share), to PropShare
  199. (sharing proportionally). A range of strategies (good and malicious) could
  200. similarly be implemented by BitSwap peers. The choice of function, then, should
  201. aim to:
  202. \begin{enumerate}
  203. \item maximize the trade performance for the node, and the whole exchange
  204. \item prevent freeloaders from exploiting and degrading the exchange
  205. \item be effective with and resistant to other, unknown strategies
  206. \item be lenient to trusted peers
  207. \end{enumerate}
  208. The exploration of the space of such strategies is future work.
  209. One choice of function that works in practice is the sigmoid, scaled by a
  210. \textit{debt retio}:
  211. Let the \textit{debt ratio} $ r $ between a node and its peer be:
  212. \[ r = \dfrac{\texttt{bytes\_sent}}{\texttt{bytes\_recv} + 1} \]
  213. Given $r$, let the probability of sending to a debtor be:
  214. \[ P\Big( \; send \; | \; r \;\Big) = 1 - \dfrac{1}{1 + exp(6-3r)} \]
  215. \begin{figure}
  216. \centering
  217. \begin{tikzpicture}[domain=0:4]
  218. \draw[->] (-0,0) -- (4.2,0) node[right] {$r$};
  219. \draw[->] (0,-0) -- (0,1.20) node[above] {$P(\;send\;|\;r\;)$};
  220. %ticks
  221. \foreach \x in {0,...,4}
  222. \draw (\x,1pt) -- (\x,-3pt)
  223. node[anchor=north] {\x};
  224. \foreach \y in {1,...,1}
  225. \draw (1pt,\y) -- (-3pt,\y)
  226. node[anchor=east] {\y};
  227. \draw[color=red] plot[] function{1 - 1/(1+exp(6-3*x))};
  228. \end{tikzpicture}
  229. \caption{Probability of Sending as $r$ increases}
  230. \label{fig:psending-graph}
  231. \end{figure}
  232. As you can see in Figure \ref{fig:psending-graph}, this function drops off quickly as the nodes' \
  233. \textit{debt ratio} surpasses twice the established credit.
  234. The \textit{debt ratio} is a measure of trust:
  235. lenient to debts between nodes that have previously exchanged lots of data
  236. successfully, and merciless to unknown, untrusted nodes. This
  237. (a) provides resistance to attackers who would create lots of new nodes
  238. (sybill attacks),
  239. (b) protects previously successful trade relationships, even if one of the
  240. nodes is temporarily unable to provide value, and
  241. (c) eventually chokes relationships that have deteriorated until they
  242. improve.
  243. % \begin{center}
  244. % \begin{tabular}{ >{$}c<{$} >{$}c<{$}}
  245. % P(\;send\;|\quad r) \;\;\;\;\;& \\
  246. % \hline
  247. % \hline
  248. % P(\;send\;|\;0.0) =& 1.00 \\
  249. % P(\;send\;|\;0.5) =& 1.00 \\
  250. % P(\;send\;|\;1.0) =& 0.98 \\
  251. % P(\;send\;|\;1.5) =& 0.92 \\
  252. % P(\;send\;|\;2.0) =& 0.73 \\
  253. % P(\;send\;|\;2.5) =& 0.38 \\
  254. % P(\;send\;|\;3.0) =& 0.12 \\
  255. % P(\;send\;|\;3.5) =& 0.03 \\
  256. % P(\;send\;|\;4.0) =& 0.01 \\
  257. % P(\;send\;|\;4.5) =& 0.00 \\
  258. % \end{tabular}
  259. % \end{center}
  260. % TODO look into computing share of the bandwidth, as described in propshare.
  261. \subsubsection{BitSwap Ledger}
  262. BitSwap nodes keep ledgers accounting the transfers with other nodes.
  263. A ledger snapshot contains a pointer to the previous snapshot (its checksum),
  264. forming a hash-chain. This allows nodes to keep track of history, and to avoid
  265. tampering. When activating a connection, BitSwap nodes exchange their ledger
  266. information.
  267. If it does not match exactly, the ledger is reinitialized from scratch,
  268. loosing the accrued credit or debt. It is possible for malicious nodes to
  269. purposefully ``loose'' the Ledger, hoping the erase debts. It is unlikely that
  270. nodes will have accrued enough debt to warrant also losing the accrued trust,
  271. however the partner node is free to count it as \textit{misconduct} (discussed
  272. later).
  273. \begin{verbatim}
  274. type Ledger struct {
  275. parent Checksum
  276. owner NodeId
  277. partner NodeId
  278. bytes_sent int
  279. bytes_recv int
  280. timestamp Timestamp
  281. }
  282. \end{verbatim}
  283. Nodes are free to keep the ledger history, though it is not necessary for
  284. correct operation. Only the current ledger entries are useful. Nodes are
  285. also free to garbage collect ledgers as necessary, starting with the less
  286. useful ledgers: the old (peers may not exist anymore) and small.
  287. \subsubsection{BitSwap Specification}
  288. BitSwap nodes follow a simple protocol.
  289. \begin{verbatim}
  290. # Additional state kept:
  291. type BitSwap struct {
  292. ledgers map[NodeId]Ledger
  293. // Ledgers known to this node, inc inactive
  294. active map[NodeId]Peer
  295. // currently open connections to other nodes
  296. need_list []Checksum
  297. // checksums of blocks this node needs
  298. have_list []Checksum
  299. // checksums of blocks this node has
  300. }
  301. type Peer struct {
  302. nodeid NodeId
  303. ledger Ledger
  304. // Ledger between the node and this peer
  305. last_seen Timestamp
  306. // timestamp of last received message
  307. want_list []Checksum
  308. // checksums of all blocks wanted by peer
  309. // includes blocks wanted by peer's peers
  310. }
  311. # Protocol interface:
  312. interface Peer {
  313. open (nodeid :NodeId, ledger :Ledger);
  314. send_want_list (want_list :WantList);
  315. send_block (block :Block) -> (complete :Bool);
  316. close (final :Bool);
  317. }
  318. \end{verbatim}
  319. Sketch of the lifetime of a peer connection:
  320. \begin{enumerate}
  321. \item Open: peers send \texttt{ledgers} until they agree.
  322. \item Sending: peers exchange \texttt{want\_lists} and \texttt{blocks}.
  323. \item Close: peers deactivate a connection.
  324. \item Ignored: (special) a peer is ignored (for the duration of a timeout)
  325. if a node's strategy avoids sending
  326. \end{enumerate}
  327. \paragraph{Peer.open(NodeId, Ledger)}
  328. When connecting, a node initializes a connection with a
  329. \texttt{Ledger}, either stored from a connection in the past or a new one
  330. zeroed out. Then, sends an Open message with the \texttt{Ledger} to the peer.
  331. Upon receiving an \texttt{Open} message, a peer chooses whether to activate
  332. the connection. If -- acording to the receiver's \texttt{Ledger} -- the sender
  333. is not a trusted agent (transmission below zero, or large outstanding debt) the
  334. receiver may opt to ignore the request. This should be done probabilistically
  335. with an \texttt{ignore\_cooldown} timeout, as to allow errors to be corrected
  336. and attackers to be thwarted.
  337. If activating the connection, the receiver initializes a Peer object, with the
  338. local version of the \texttt{Ledger}, and setting the \texttt{last\_seen}
  339. timestamp). Then, it compares the received
  340. \texttt{Ledger} with its own. If they match exactly, the connections have
  341. opened. If they do not match, the peer creates a new zeroed out
  342. \texttt{Ledger}, and sends it.
  343. \paragraph{Peer.send\_want\_list(WantList)}
  344. While the connection is open, nodes advertise their
  345. \texttt{want\_list} to all connected peers. This is done (a) upon opening the
  346. connection, (b) after a randomized periodic timeout, (c) after a change in
  347. the \texttt{want\_list} and (d) after receiving a new block.
  348. Upon receiving a \texttt{want\_list}, a node stores it. Then, it checks whether
  349. it has any of the wanted blocks. If so, it sends them according to the
  350. \textit{BitSwap Strategy} above.
  351. \paragraph{Peer.send\_block(Block)}
  352. Sending a block is straightforward. The node simply transmits the block of
  353. data. Upon receiving all the data, the receiver computes the Checksum to
  354. verify it matches the expected one, and returns confirmation.
  355. Upon finalizing the correct transmission of a block, the receiver moves the
  356. block from \texttt{need\_list} to \texttt{have\_list}, and both the receiver
  357. and sender update their ledgers to reflect the additional bytes transmitted.
  358. If a transmission verfication fails, the receiver instead \textit{penalizes}
  359. the sender. Both receiver and sender should update their ledgers accordingly,
  360. though the sender is either malfunctioning or attacking the receiver. Note that
  361. BitSwap expects to operate on a reliable transmission channel, so data errors
  362. -- which could lead to incorrect penalization of an honest sender -- are
  363. expected to be caught before the data is given to BitSwap. IPFS uses the uTP
  364. protocol.
  365. \paragraph{Peer.close(Bool)}
  366. The \texttt{final} parameter to \texttt{close} signals whether the intention
  367. to tear down the connection is the sender's or not. If false, the receiver
  368. may opt to re-open the connection immediatelty. This avoids premature
  369. closes.
  370. A peer connection should be closed under two conditions:
  371. \begin{itemize}
  372. \item a \texttt{silence\_wait} timeout has expired without receiving any
  373. messages from the peer (default BitSwap uses 30 seconds).
  374. In this case, the node issues a \texttt{Peer.close(false)} message.
  375. \item the node is exiting and BitSwap is being shut down.
  376. In this case, the node issues a \texttt{Peer.close(true)} message.
  377. \end{itemize}
  378. After a \texttt{close} message, both receiver and sender tear down the
  379. connection, clearing any state stored. The \texttt{Ledger} may be stored for
  380. the future, if it is useful to do so.
  381. \paragraph{Notes}
  382. \begin{itemize}
  383. \item Non-\texttt{open} messages on an inactive connection should be ignored.
  384. In case of a \texttt{send\_block} message, the receiver may check
  385. the block to see if it is needed and correct, and if so, use it.
  386. Regardless, all such out-of-order messages trigger a
  387. \texttt{close(false)} message from the receiver, to force
  388. re-initialization of the connection.
  389. \end{itemize}
  390. % TODO: Rate Limiting / Node Silencing
  391. \subsection{Object Model}
  392. The DHT and BitSwap allow IPFS to form a massive peer-to-peer system for storing
  393. and distributing blocks quickly and robustly to users.
  394. IPFS builds a filesystem out of this efficient block distribution system,
  395. constructing files and directories out of blocks.
  396. Files in IPFS are represented as a collection of inter-related objects, like in
  397. the version control system Git. Each object is addressed by the cryptographic
  398. hash of its contents (\texttt{Checksum}). The file objects are:
  399. \begin{enumerate}
  400. \item \texttt{block}: a variable-size block of data.
  401. \item \texttt{list}: a collection of blocks or other lists.
  402. \item \texttt{tree}: a collection of blocks, lists, or other trees.
  403. \item \texttt{commit}: a snapshot in the version history of a tree.
  404. \end{enumerate}
  405. We hoped to use the Git object formats exactly, but had to depart to introduce
  406. certain features useful in a distributed filesystem, for example fast size
  407. lookups (aggregate byte sizes have been added to objects), large file
  408. deduplication and versioning (adding a \texttt{list} object), and more.
  409. However, our objects are perfectly compatible with Git and
  410. conversion between the two does not lose any information.
  411. Notes:
  412. \begin{itemize}
  413. \item \texttt{varint} is a variable size int, as in protocol-buffers.
  414. \item objects are serialized using \texttt{capnp}.
  415. \end{itemize}
  416. \subsubsection{Block Object}
  417. The \texttt{Block} object contains an addressable unit of data, and
  418. represents a file.
  419. IPFS Blocks are like Git blobs or filesystem data blocks. They store the
  420. users' data. (The name \textit{block} is preferred over \textit{blob}, as the
  421. Git-inspired view of a \textit{blob} as a \textit{file} breaks down in IPFS.
  422. IPFS files can be represented by both \texttt{lists} and \texttt{blocks}.)
  423. Format:
  424. \begin{verbatim}
  425. block <size>
  426. <block data bytes>
  427. ...
  428. \end{verbatim}
  429. \subsubsection{List Object}
  430. The \texttt{List} object represents a large or de-duplicated file made up of
  431. several IPFS \texttt{Blocks} concatenated together. \texttt{Lists} contain
  432. an ordered sequence of \texttt{block} or \texttt{list} objects.
  433. In a sense, the IPFS \texttt{List} functions like a filesystem file with
  434. indirect blocks. Since \texttt{lists} can contain other \texttt{lists}, topologies including linked lists and balanced trees are possible. Directed graphs where the same node appears in multiple places allow in-file deduplication. Cycles are not possible (enforced by hash addessing).
  435. Format:
  436. \begin{verbatim}
  437. list <num objects> <size varint>
  438. <list or block> <checksum> <size varint>
  439. <list or block> <checksum> <size varint>
  440. ...
  441. \end{verbatim}
  442. \subsubsection{Tree Object}
  443. The \texttt{tree} object in IPFS is similar to Git trees: it represents a
  444. directory, a list of checksums and names. The checksums reference \texttt{blob}
  445. or other \texttt{tree} objects. Note that traditional path naming
  446. is implemented entirely by the \texttt{tree} objects. \texttt{Blocks} and
  447. \texttt{lists} are only addressed by their \texttt{checksums}.
  448. Format:
  449. \begin{verbatim}
  450. tree <num objects> <size varint>
  451. <tree or list or block> <checksum> <size varint> <name>
  452. <tree or list or block> <checksum> <size varint> <name>
  453. ...
  454. \end{verbatim}
  455. \subsubsection{Commit Object}
  456. The \texttt{commit} object in IPFS is similar to Git's. It represents a
  457. snapshot in the version history of a \texttt{tree}. Note that user
  458. addresses are NodeIds (the hash of the public key).
  459. \begin{verbatim}
  460. commit <size varint>
  461. parent <commit checksum>
  462. tree <tree checksum>
  463. author <author public key> <ISO UTC date>
  464. committer <committer public key> <ISO UTC date>
  465. <commit message>
  466. \end{verbatim}
  467. \subsubsection{Version control}
  468. The \texttt{commit} object represents a particular snapshot in the version
  469. history of a tree. Comparing the \texttt{trees} and children objects of two
  470. different commits reveals the differences between two versions of the
  471. filesystem. As long as a single \texttt{commit} and all the children objects
  472. it references are accessible, all preceding versions are retrievable and the
  473. full history of the filesystem changes can be accessed. This is a consequence
  474. of the \texttt{Git} object model and the graph it forms.
  475. The full power of the \texttt{Git} version control tools is available to IPFS
  476. users. The object model is compatible (though not the same). The standard
  477. \texttt{Git} tools can be used on the \texttt{IPFS} object graph after a
  478. conversion. Additionally, a fork of the tools is under development that will
  479. allow users to use them directly without conversion.
  480. \subsubsection{Object-level Cryptoraphy}
  481. IPFS is equipped to handle object-level cryptographic operations. Any additional
  482. bytes are appended to the bottom of the object. This changes the object's hash
  483. (defining a different object, as it should). IPFS exposes an API that
  484. automatically verifies signatures or decrypts data.
  485. \begin{itemize}
  486. \item \texttt{Signing}. Signature appended.
  487. \item \texttt{Encryption}. Optional recipient's public key appended.
  488. \end{itemize}
  489. \subsubsection{Merkle Trees}
  490. The object model in IPFS forms a \textit{Merkle Tree}, which provides IPFS with
  491. useful properties:
  492. \begin{enumerate}
  493. \item \textbf{Content Addressing:} all content is uniquely identified by its
  494. \texttt{checksum}, \textbf{including child checksums}. This means a
  495. particular \texttt{tree} object points to \textit{specific} children.
  496. Committing changes to a \texttt{block} also commits changes to the
  497. containing \texttt{tree}.
  498. \item \textbf{Tamper resistance:} all content is verified with its Checksum.
  499. If data is tampered with, before being delivered, the client
  500. detects and discards it.
  501. \item \textbf{Deduplication:} all objects who hold the exact same content
  502. are the same, and only stored once. This is particularly useful with
  503. parent objects, such as lists, trees, and commits.
  504. \end{enumerate}
  505. \subsection{The Filesystem}
  506. \subsubsection{Filesystem Paths}
  507. IPFS exposes a slash-delimited path-based API. Paths work the same as in any
  508. traditional UNIX filesystem. Path subcomponents have different meanings per
  509. object:
  510. \begin{center}
  511. \begin{tabular}{ll}
  512. \texttt{object} & subcomponent meaning \\
  513. \hline
  514. \hline
  515. \texttt{block} & N/A (no children) \\
  516. \texttt{list} & integer index \\
  517. \texttt{tree} & string name \\
  518. \texttt{commit} & string name (in tree) \\
  519. \end{tabular}
  520. \end{center}
  521. \begin{figure}
  522. \centering
  523. \begin{tikzpicture}[->,>=stealth',auto,thick,
  524. minimum height=2em,minimum width=5em]
  525. \tikzstyle{ghost}=[rectangle,rounded corners=.8ex];
  526. \tikzstyle{block}=[rectangle,draw,fill=blue!20,rounded corners=.8ex];
  527. \tikzstyle{list}=[rectangle,draw,fill=cyan!20,rounded corners=.8ex];
  528. \tikzstyle{tree}=[rectangle,draw,fill=green!20,rounded corners=.8ex];
  529. \tikzstyle{commit}=[rectangle,draw,fill=magenta!20,rounded corners=.8ex];
  530. \tikzstyle{every path}=[draw]
  531. \node[commit] (ccc111) {ccc111};
  532. \node[tree] (ttt111) [below=3em of ccc111] {ttt111};
  533. \node[tree] (ttt222) [below left=3em and 3em of ttt111] {ttt222};
  534. \node[tree] (ttt333) [below=3em of ttt111] {ttt333};
  535. \node[ghost] (ghost1) [below right=3em and 3em of ttt111] {};
  536. \node[list] (lll111) [below=3em of ttt333] {lll111};
  537. \node[block] (bbb111) [below=3em of ttt222] {bbb111};
  538. \node[block] (bbb222) [below right=3em and 3em of ttt333] {bbb222};
  539. \node[block] (bbb333) [below left=3em and 3em of lll111] {bbb333};
  540. \node[block] (bbb444) [below=3em of lll111] {bbb444};
  541. \node[block] (bbb555) [below right=3em and 3em of lll111] {bbb555};
  542. \path[every node/.style={font=\sffamily\small}]
  543. (ccc111) edge[out=-90,in=90] (ttt111)
  544. (ttt111) edge[out=-90,in=90] (ttt222)
  545. edge[out=-90,in=90] (ttt333)
  546. to [out=-90,in=90] (ghost1)
  547. to [out=-90,in=90] (bbb222)
  548. (ttt222) edge[out=-90,in=90] (bbb111)
  549. (ttt333) edge[out=-90,in=90] (lll111)
  550. edge[out=-90,in=90] (bbb222)
  551. (lll111) edge[out=-90,in=90] (bbb333)
  552. edge[out=-90,in=90] (bbb444)
  553. edge[out=-90,in=90] (bbb555)
  554. ;
  555. \end{tikzpicture}
  556. \caption{Sample Object Graph} \label{fig:sample-object-graph}
  557. \begin{verbatim}
  558. # ccc111 contents
  559. commit 313
  560. tree ttt111
  561. author <author public key> <ISO UTC date>
  562. committer <committer public key> <ISO UTC date>
  563. # ttt111 contents
  564. tree 3 250
  565. tree ttt222 46 ttt222-name
  566. tree ttt333 166 ttt333-name
  567. block bbb222 11 bbb222-name
  568. # ttt222 contents
  569. tree 1 10
  570. block bbb111 10 bbb111-name
  571. # ttt333 contents
  572. tree 2 104
  573. list lll111 93 lll111-name
  574. block bbb222 11 bbb222-eman
  575. # lll111 contents
  576. list 3 39
  577. block bbb333 12
  578. block bbb444 13
  579. block bbb555 14
  580. # bbb111 contents # block bbb222 contents
  581. block 1 block 2
  582. 1 22
  583. # bbb333 contents # block bbb444 contents
  584. block 3 block 4
  585. 333 4444
  586. # bbb555 contents
  587. block 5
  588. 55555
  589. \end{verbatim}
  590. \caption{Sample Objects} \label{fig:sample-objects}
  591. \end{figure}
  592. For example, given the sample objects in Figures \ref{fig:sample-object-graph} and \ref{fig:sample-objects}:
  593. \begin{verbatim}
  594. # to access tree ttt333:
  595. ccc111/ttt333-name
  596. # to access block bbb222:
  597. ccc111/bbb222-name
  598. ccc111/ttt333-name/bbb222-eman
  599. # to access list lll111:
  600. ccc111/ttt333-name/lll111-name
  601. # to access block bbb555:
  602. ccc111/ttt333-name/lll111-name/2
  603. \end{verbatim}
  604. Note that:
  605. \begin{itemize}
  606. \item[(a)] blocks have no children \\
  607. \texttt{.../<block>/<child>} is impossible
  608. \item[(b)] commits implicitly access their trees \\
  609. \texttt{.../<commit>/name}
  610. looks up \texttt{"name"} in \texttt{<commit>}'s \texttt{<tree>}
  611. \item[(c)] \texttt{list} children are accessed by their index \\
  612. \texttt{.../<list>/4} looks up the fifth block.
  613. \end{itemize}
  614. \paragraph{Path Lookup Performance}
  615. Path-based access traverses the object graph. Retrieving
  616. each object requires potentially looking up its key in the DHT,
  617. connecting to peers, and retrieving its blocks. This is considerable
  618. overhead, particularly when looking up paths with many components.
  619. This is mitigated by:
  620. \begin{itemize}
  621. \item \textbf{tree caching}: since all objects are hash-addressed, they
  622. can be cached indefinitely. Additionally, \texttt{trees} tend to be
  623. small in size so IPFS prioritizes caching them over \texttt{blocks}.
  624. \item \textbf{flattened trees}: for any given \texttt{tree}, a special
  625. \texttt{flattened tree} can be constructed to list all objects
  626. reachable from the \texttt{tree}. Figure \ref{flattened-ttt111} shows
  627. an example of a flattened tree. While IPFS does not construct flattened
  628. trees by default, it provides a function for users. For example,
  629. \end{itemize}
  630. \begin{figure}
  631. \begin{verbatim}
  632. tree 5 250
  633. tree ttt222 46 ttt222-name
  634. block bbb111 10 ttt222-name/bbb111-name
  635. tree ttt333 166 ttt333-name
  636. list lll111 93 ttt222-name/lll111-name
  637. block bbb222 11 ttt333-name/bbb222-eman
  638. block bbb222 11 bbb222-name
  639. \end{verbatim}
  640. \caption{Flattened Tree for \texttt{ttt111}} \label{fig:flattened-ttt111}
  641. \end{figure}
  642. \subsubsection{Publishing Objects}
  643. IPFS is globally distributed. It is designed to allow the files of millions
  644. of users to coexist together. The \textbf{DHT} with content-hash addressing
  645. allows publishing objects in a fair, secure, and entirely distributed way.
  646. Anyone can publish an object by simply adding its key to the DHT, adding
  647. themselves as a peer, and giving other users the object's hash.
  648. Additionally, the IPFS root directory supports special functionality to
  649. allow namespacing and naming objects in a fair, secure, and distributed
  650. manner.
  651. \begin{itemize}
  652. \item[(a)] All objects are accessible by their hash. Thus, users can
  653. always reference an object (and its children) using
  654. \texttt{/<object\_hash>}.
  655. \item[(b)] \texttt{/<node\_id>} provides a self-certifying filesystem
  656. for user \texttt{node\_id}. If it exists, the object returned is a
  657. special \texttt{tree} signed by \texttt{node\_id}'s private key. Thus,
  658. a user can publish a \texttt{tree} or \texttt{commit} under their
  659. name, and others can verify it by checking the signature matches.
  660. \item[(c)] If \texttt{/<domain>} is a valid domain name, IPFS
  661. looks up key \texttt{gfs} in its \texttt{DNS TXT} record. IPFS
  662. interprets the value as either an object hash or another IPFS path:
  663. \begin{verbatim}
  664. # this DNS TXT record
  665. fs.benet.ai. TXT "gfs=/aabbccddeeffgg ..."
  666. # behaves as symlink
  667. ln -s /aabbccddeeffgg /fs.benet.ai
  668. \end{verbatim}
  669. \end{itemize}
  670. \subsection{Local Objects}
  671. IPFS clients require some \textit{local storage}, an external system
  672. on which to store and retrieve local raw data for the objects IPFS manages.
  673. The type of storage depends on the node's use case.
  674. In most cases, this is simply a portion of disk space (either managed by
  675. the native filesystem, or directly by the IPFS client). In others, non-
  676. persistent caches for example, this storage is just a portion of RAM.
  677. Ultimately, all blocks available in IPFS are in some node's
  678. \textit{local storage}. And when nodes open files with IPFS, the objects are
  679. downloaded and stored locally, at least temporarily. This provides
  680. fast lookup for some configurable amount of time thereafter.
  681. \subsubsection{Object Pinning}
  682. Nodes who wish to ensure the survival of particular objects can do so by
  683. \texttt{pinning} the objects. This ensures the objects are kept in the node's
  684. \textit{local storage}. Pinning can be done recursively, to pin down all
  685. descendent objects as well. For example, recursively pinning a \texttt{tree}
  686. or \texttt{commit} ensures \textit{all} objects pointed to are stored locally
  687. too. This is particularly useful for nodes wishing to keep all their own files.
  688. %\section{Acknowledgments}
  689. %\bibliographystyle{abbrv}
  690. %\bibliography{gfs}
  691. %\balancecolumns
  692. %\subsection{References}
  693. \end{document}