\documentclass[conference]{IEEEtran}
\IEEEoverridecommandlockouts
% The preceding line is only needed to identify funding in the first footnote. If that is unneeded, please comment it out.
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{url}
\usepackage{listings}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
     *{0}{{{\color{numb}0}}}{1}
      {1}{{{\color{numb}1}}}{1}
      {2}{{{\color{numb}2}}}{1}
      {3}{{{\color{numb}3}}}{1}
      {4}{{{\color{numb}4}}}{1}
      {5}{{{\color{numb}5}}}{1}
      {6}{{{\color{numb}6}}}{1}
      {7}{{{\color{numb}7}}}{1}
      {8}{{{\color{numb}8}}}{1}
      {9}{{{\color{numb}9}}}{1}
      {:}{{{\color{punct}{:}}}}{1}
      {,}{{{\color{punct}{,}}}}{1}
      {\{}{{{\color{delim}{\{}}}}{1}
      {\}}{{{\color{delim}{\}}}}}{1}
      {[}{{{\color{delim}{[}}}}{1}
      {]}{{{\color{delim}{]}}}}{1},
}

\begin{document}

\title{How to implement a distributed social network using IPFS}

\author{\IEEEauthorblockN{Matthias Beyer}
\IEEEauthorblockA{Tübingen, Germany \\
mail@beyermatthias.de}
}

\maketitle

\begin{abstract}
    This document describes how one can implement a distributed social network
    or publishing platform using the IPFS\cite{ipfsbennet}, \cite{ipfsio} and related technologies.
    It proposes a block data format for a blockchain and conceptual ideas for
    implementing "profiles" upon this data format.
    It also describes profile discovery algorithms and lays out problems with
    trust and how they could be approached.
    Finally, it proposes an architecture how a client for such a social network
    could be implemented, focusing on reusability of components to be able to
    make them available to a broader audience, for example for federated caching
    or even embedded usage.
\end{abstract}

\begin{IEEEkeywords}
\end{IEEEkeywords}

\section{Introduction}
% Intro about the general idea

\section{Data types}
% Intro about the data types required for building the blockchain

The implementation of an IPFS-based social network would be based on a DAG of
blocks of data.
Each DAG would resemble a "profile" in the social network.
A "profile", though, would not necessarily be bound to a person, but would
represent a unique identity.
Embedded ("smart") devices or automated tools (bots) posting to a profile without
human interaction could be possible and are imaginable.

Because distribution and availability is curcial, two "layers" of blocks should
be considered:

\begin{itemize}
    \item A more general "block", implementing nothing more than the chain and
        some minimal meta data, e.g. a format version and a, and a pointer to
        the actual "metadata block"
    \item A "metadata block", holding all the information about the profile,
        the content and other valuable metadata, such as timestamps, a mimetype
        of the content linked and, of course, a link to the actual content
\end{itemize}

The third data type required for implementing a block would be holding the data
itself.
Thus, it would be only a stream of bytes - which would be added to IPFS
directly.

In the following chapters, details of the individual types are layed out.
For simplicity, JSON is used for examples of objects of the layed out
types.


\subsection{Block}
% What a "block" is used for and what data it holds
% - metadata pointer

As described above, the "Block" data type is used for building the blockchain.
Because this kind of object must be easy to distribute to ensure high
availability of blocks of a single chain, this type must be small in size.
Thus, it should only hold the following elements:

\begin{itemize}
    \item A version number, which describes the version of the format used
    \item A list of pointers to parent blocks
    \item An (optional) pointer to a metadata Block
\end{itemize}

A block may hold more than one pointer to a parent block to make merges possible
on the block-level.

A block that references more than one parent should not contain any pointer to a
metadata object.
If a "merge block" would point to metadata, complexity would be increased
unecessarily\footnotemark[1].

\footnotetext[1]{
    A possible optimization would be that merge-blocks are defined in a way that
    two nodes can always generate the byte-exact same merge block.
    This would result in the possibility of two devices that do not know
    eachother to merge two branches of a chain with the exact same merge block
    object.
    Encoding metadata like a timestamp in the "Block" type would prevent that.
}

\ref{pic:blockchain} describes a minimal chain of blocks.
Each block, save $A$, points to its predecessor.
$D$ points both to $B$ and $C$, because it merges these blocks, that were both
created with $A$ as their predecessor.

\begin{lstlisting}[language=json,caption={Example of a Block},label=jsonblock]
{
"v": 1,
"parents": ["Qmabc...", "Qmdef..."],
"content": "Qmghi..."
}
\end{lstlisting}

As shown in \ref{jsonblock}, minimal data is held in the "Block" object.
Because of the small size, replication of block objects is cheap and every
modern end-user device can easily hold millions of blocks.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.45\textwidth]{gen/blockchain.png}
    \caption{A chain of blocks}
    \label{pic:blockchain}
\end{figure}

If $S_{parents}(b)$ is the size of the list of parents of a block $b$,
and
$S_{base}$ is the size of a block without any parents,
the required disk space can be described by

\begin{equation}
    S_{sum} = n * S_{base} + \sum_{i=1}^{n} S_{parents}(b_i)
\end{equation}

where $b_i$ is a block and $n$ is the number of blocks.
Depending on the overhead of the encoding technology used, the size of an
individual block is in the lower bytes range.
E.G. assuming a size of 46 bytes for a IPFS hash, a block with only one parent fits
into 100 bytes, even if encoded as JSON. That means that one Gigabyte of storage
could hold more than 10 Million blocks.


\subsection{Metadata-Block}
% What a "metadata-block" is used for and what data it holds
% - mime
% - timestamp
% - IPNS List
% - content
%   - content pointer
%   - references to other blocks

A "metadata" block would hold the actual profile post data, although not the
actual content of the post to the profile.
As its name tells, it holds the metadata of the post, that is

\begin{itemize}
    \item A format version number
    \item Date and Time when the block was created
    \item A list of public keys that post to the profile
    \item a sub-object that is either
        \begin{itemize}
            \item Empty

            \item A "post" object, holding
                \begin{itemize}
                    \item A pointer to the content "blob" (see \ref{sec:datatypecontent})
                    \item A mimetype for the content
                    \item An (optional) "In-Reply-To" reference
                    \item A (optional) list of references to other (metadata) blocks
                    \item possibly more (to be defined)
                \end{itemize}

            \item A "profile" object, holding for example
                \begin{itemize}
                    \item a Profile name
                    \item a Profile picture
                    \item a Profile description/text
                    \item possibly more (to be defined)
                \end{itemize}

            \item A ref object, holding a reference to another block or metadata
                block that is referenced (see \ref{sec:postreplies})

            \item or something else (to be defined)
        \end{itemize}
\end{itemize}

Because of its size, this object type might not be replicated as much as the
"block" object type.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.45\textwidth]{gen/blockchain_meta.png}
    \caption{A chain of blocks with pointers to their corrosponding meta block}
    \label{pic:blockchain:meta}
\end{figure}

In the chain of blocks from \ref{pic:blockchain}, each block (except $D$) would
point to another metadata block, resulting in \ref{pic:blockchain:meta}.

In \ref{pic:blockchain:meta}, the metadata objects $Am$, $Bm$, $Cm$ and $Em$
would then point to content objects (assuming these are posts, not "profile"
objects or a "ref" object).


\subsection{Content}\label{sec:datatypecontent}
% What a "content" object is (and what it is not)

The content that a user posts can be added to IPFS directly.
This enables several other possibilities and use-cases:

\begin{itemize}
    \item
        Because no meta-data is attached to the content, hosting that metadata
        becomes less resource intensive in relation to the size of the actual
        content.

        In turn, hosting the content is easier, because a hoster does not need
        to attach metadata they do not care about, increasing the distribution
        of the content.

    \item
        A user can post content that is already on IPFS.
        This content does not necessariliy have to be in the social network, but
        can of course.

        If a user posts content that is already on IPFS, the post is essentially
        what is called a "repost" in regular social networks, although other
        methods of reposting might be appropriate
        (see also \ref{sec:postreplies:reposts}).

    \item
        Recurring posts of the same content are cheap.

        Because IPFS automatically deduplicates content, posting the same
        content twice does not result in twice the disk space used.

        Also, if a user posts a set of files and another user posts a subset of
        these files, deduplication applies.
\end{itemize}

\subsection{Data format}
% Short note about possible data formats



\section{Profiles}
% How profiles are implemented on top of the data types

\subsection{Multi-device support}
% How multi-device support for a profile is implemented using IPNS

\subsection{Device "Authorization"}
% How devices are authorized to post to a profile

\subsection{Device "De-authorization"}
% How devices are de-authorized from posting to a profile


\section{Posts}
% what a post is

\subsection{Post types}
% What kinds of posts exist

\subsection{Replies}\label{sec:postreplies}
% How comments on posts could be implemented

\subsubsection{Comments on Posts}

\subsubsection{Reposts}\label{sec:postreplies:reposts}


\section{Traversing Chains and profile discovery}
% What needs to be done for traversing a block chain and discover a profile

\subsection{Profile fetching}
% How profiles are fetched

\subsection{Trusting a chain}
% How a chain can be trusted



\section{Possibilities for Optimization}
% Possibilities how the block chain model could be optimized


\section{Implementation}
% Intro for architecture description

\subsection{Architecture}
% Description of the general architecture

\subsection{Backend}
% Interfacing with IPFS and providing an abstraction to
% interact with "blocks" and "profiles", using either
% - a REST interface to be run as a (possibly network-local) server, or
% - a library API for embedding into a "fat" application


\subsection{Middleware}
% Middleware library, providing
% - interfaces for working with a backend (either REST or library)
% - public-private-key crypto interfaces for IPNS name publishing

% Itself providing either a REST interface via a webserver or
% a library API for embedding into a "fat" application


\subsection{Frontend}
% Frontend implementation, abstract over the middleware implementation, which
% needs to be trusted.
%
% A frontend impl could be started on a embedded device where no public-private
% key crypto is possible, so the middleware runs on a better-suited device
%
% The frontend could also embedd the middleware and implement a desktop client
% using a CLI, TUI, GUI or WUI.


\section{Future work}
% What could be implemented on top of this idea

\section*{Acknowledgment}

\section*{References}

\begin{thebibliography}{00}
\bibitem{ipfsbennet} J. Bennet, IPFS - Content Addressed, Versioned, P2P File System(DRAFT 3)
\bibitem{ipfsio} \url{https://docs.ipfs.io/}
\end{thebibliography}
\end{document}
