Squashed Last bugs
[iotcloud.git] / version2 / doc / iotcloud.tex
index 3acd7aa0d63ec62e1eaa9940593d8c13c262208a..1d760d7a29214fe945e5ee91cb902f320d9a97e2 100644 (file)
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-% Short Sectioned Assignment\r
-% LaTeX Template\r
-% Version 1.0 (5/5/12)\r
-%\r
-% This template has been downloaded from:\r
-% http://www.LaTeXTemplates.com\r
-%\r
-% Original author:\r
-% Frits Wenneker (http://www.howtotex.com)\r
-%\r
-% License:\r
-% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)\r
-%\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-\r
-%----------------------------------------------------------------------------------------\r
-%   PACKAGES AND OTHER DOCUMENT CONFIGURATIONS\r
-%----------------------------------------------------------------------------------------\r
-\r
-\documentclass[paper=letter, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size\r
-\r
-\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs\r
-\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default\r
-\usepackage[english]{babel} % English language/hyphenation\r
-\usepackage{amsmath,amsfonts,amsthm} % Math packages\r
+\documentclass[11pt]{article}\r
+\newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}\r
+\usepackage{color}\r
+\usepackage{amsthm}\r
+\usepackage{amsmath}\r
 \usepackage{graphicx}\r
-\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template\r
-\usepackage{hyperref}\r
+\usepackage{mathrsfs}\r
 \usepackage{amssymb}\r
-\usepackage{listings}\r
-\usepackage[]{algorithm2e}\r
-\usepackage{algpseudocode}\r
-\usepackage{enumerate}\r
-\usepackage[table,xcdraw]{xcolor}\r
-\usepackage{sectsty} % Allows customizing section commands\r
-\usepackage{float}\r
-\usepackage{caption}\r
-\usepackage{gensymb} % to used degree symbol \r
-\usepackage{siunitx} \r
-\usepackage{enumitem}\r
-\r
-\usepackage[sc]{mathpazo}\r
-\allsectionsfont{ \normalfont\scshape} % Make all sections the default font and small caps\r
-\usepackage{fancyhdr} % Custom headers and footers\r
-\pagestyle{fancyplain} % Makes all pages in the document conform to the custom headers and footers\r
-\fancyhead{} % No page header - if you want one, create it in the same way as the footers below\r
-\fancyfoot[L]{} % Empty left footer\r
-\fancyfoot[C]{} % Empty center footer\r
-\fancyfoot[R]{\thepage} % Page numbering for right footer\r
-\renewcommand{\headrulewidth}{0pt} % Remove header underlines\r
-\renewcommand{\footrulewidth}{0pt} % Remove footer underlines\r
-\setlength{\headheight}{13.6pt} % Customize the height of the header\r
-\r
-\numberwithin{equation}{section} % Number equations within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)\r
-\numberwithin{figure}{section} % Number figures within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)\r
-\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)\r
+\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx\r
+\usepackage[all]{xy}\r
+\usepackage{varwidth}\r
 \r
-\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text\r
+\newtheorem{theorem}{Theorem}\r
+\newtheorem{prop}{Proposition}\r
+\newtheorem{lem}{Lemma}\r
+\newtheorem{defn}{Definition}\r
+\newcommand{\note}[1]{{\color{red} \bf [[#1]]}}\r
+\newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}\r
+\newcommand*\xor{\mathbin{\oplus}}\r
+\algnewcommand{\LeftComment}[1]{\Statex \(\triangleright\) #1}\r
+\begin{document}\r
 \r
-%----------------------------------------------------------------------------------------\r
-%   TITLE SECTION\r
-%----------------------------------------------------------------------------------------\r
-\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height\r
 \r
-\title{ \r
-\normalfont \normalsize \r
-\textsc{University of California Irvine} \\  % Your university, school and/or department name(s)\r
-\textsc{Prgramming Language Research Group} \\ [25pt]\r
-\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule\r
-\huge IoTCloud Version 2.0\\ % The assignment title\r
-\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule\r
-}\r
+\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text\r
 \r
-\author{Authors} % Your name\r
 \r
+\section{\textbf{Introduction}}\r
 \r
-\date{\normalsize\today} % Today's date or a custom date\r
 \r
-\begin{document}\r
+\section{Approach}\r
 \r
-\maketitle % Print the title\r
+\subsection{Keys}\r
 \r
+Each device has: user id + password\r
 \r
+Server login is:\r
+hash1(user id), hash1(password)\r
 \r
+Symmetric Crypto keys is:\r
+hash2(user id | password)\r
 \r
-%---------------------------------------------------------------------------------------\r
-% Custom Stuff\r
-%---------------------------------------------------------------------------------------\r
-\newcommand{\tab}[1]{\hspace{.2\textwidth}\rlap{#1}}\r
+Server has finite length queue of entries + max\_entry\_identifier +\r
+server login key\r
 \r
+\subsection{Entry layout}\r
+Each entry has:\r
+\begin{enumerate}\r
+\item Sequence identifier\r
+\item Random IV (if needed by crypto algorithm)\r
+\item Encrypted payload\r
+\end{enumerate}\r
 \r
+Payload has:\r
+\begin{enumerate}\r
+\item Sequence identifier\r
+\item Machine id (most probably something like a 64-bit random number \r
+that is self-generated by client)\r
+\item HMAC of previous slot\r
+\item Data entries\r
+\item HMAC of current slot\r
+\end{enumerate}\r
 \r
+A data entry can be one of these:\r
+\begin{enumerate}\r
+    \item A transaction:\r
+        \begin{itemize}\r
+            \item Contains a sequence number, a set of key value pair updates and a guard condition that can be evaluated.\r
+            \item Must have the same arbitrator for all its key value pair updates and reads within the guard condition\r
+        \end{itemize}\r
+    \r
+    \item A Commit\r
+        \newline{Commits a transaction into the block chain.  Until a transaction is committed, no client can be sure if that transaction's key value updates will be used to update the state of the system.  Once an arbitrator commits a transaction then that transaction becomes a permanent state change in the system.  Transactions should be committed and aborted in order of their sequence numbers.}\r
+    \r
+    \item An Abort\r
+        \newline{An abort is used to show that a transactions key value update should not be used in the state change of the system.  This occurs when the guard of a transaction evaluates to false meaning that the conditions under-which this transaction should be committed no longer exists in the system (another transaction could have been committed first that would have changed the system in a way that makes the current transaction invalid).}\r
+    \r
+    \item New Key:\r
+        \newline{This creates a new key and assignes an arbitrator to that key.  Only the first new key message for a given key is valid.  Once a new key message is inserted into the block chain it is never removed and no other new key entries for the same key name can be inserted into the block chain.}\r
+        \r
+    \item Slot sequence entry: Machine id + last message identifier \r
+        \newline {The purpose of this is to keep the record of the last slot from a certain client if a client's update has to expunge that other client's last entry from the queue. This is kept in the slot until the entry owner inserts a newer update into the queue.}\r
 \r
-\section{\textbf{Introduction}}\r
+    \item Queue state entry: Includes queue size \r
+        \newline {The purpose of this is for the client to tell if the server lies about the number of slots in the queue, e.g. if there are 2 queue state entry in the queue, e.g. 50 and 70, the client knows that when it sees 50, it should expect at most 50 slots in the queue and after it sees 70, it should expect 50 slots before that queue state entry slot 50 and at most 70 slots. The queue state entry slot 70 is counted as slot number 51 in the queue.}\r
+\r
+    \item Collision resolution entry: message identifier + machine id of a collision winner\r
+        \newline {The purpose of this is to keep keep track of the winner of all the collisions until all clients have seen the particular entry.}\r
+\end{enumerate}\r
 \r
-\section{\textbf{Approach}}\r
+\subsection{Live status}\r
 \r
-\subsection{\textbf{Records}}\r
-Each record has the following information included in it:\r
-\begin{itemize}\r
-    \item Machine ID of the device creating the record\r
-    \item The vector clock using the largest clock values from each device it knows and its own largest clock value incremented by 1.\r
-    \item Add a random salt (or nonce) for the encryption safety\r
-    \item Data payload\r
-    \item HMAC of the record.\r
-\end{itemize}\r
+Live status of entries:\r
+\begin{enumerate}\r
+    \item Transaction is live if it has not been committed or aborted yet.\r
+    \r
+    \item Abort is live until the machine ID that created the transaction that is being aborted inserts into the block chain a message with a sequence number greater than the abort (that client sees the abort).\r
+        \r
+    \item Commit is dead if for all key value updates in the commit there is a commit with the same key value update that is newer (larger sequence number).  The committing client (arbitrator) will see those newer commits since it is the one that generates them.\r
     \r
+    \item New Key messages are always kept alive.  Keys can not be deleted.  Deleted keys will cause arbitration to fail if a key is deleted then reassigned to a new client device for arbitration.\r
+    \r
+    \item Slot sequence number (of either a message version data or user-level data) is dead if there is a newer slot from the same machine.\r
 \r
+    \item Queue state entry is dead if there is a newer queue state entry.\r
+    {In the case of queue state entries 50 and 70, this means that queue state entry 50 is dead and 70 is live. However, not until the number of slots reaches 70 that queue state entry 50 will be expunged from the queue.  Further all entries before the 50 entry will also not be expunged until the queue size has reached 70}\r
 \r
-\subsubsection{\textbf{Types of Payloads}}\r
-The different types of record payloads are:\r
-\begin{itemize}\r
-    \item Delete notifications\r
-        \begin{itemize}\r
-            \item Contains the HMAC of records that were deleted by devices, Their vector clocks and the server sequence numbers.\r
-            \item Generated when a device deletes a record.\r
-            \item The delete notification with the largest server sequence number in its delete payload is the live one (the one that contains the largest server sequence number of the record deleted to date).  \r
-        \end{itemize}\r
-    \item Commit notifications\r
-        \begin{itemize}\r
-            \item Contains list of transactions that are committed in order of commit and the current key-value pair for that key.\r
-            \item Generated by the arbitrator of a key and only the for that key (1 arbitrator per key).\r
-            \item Used in the case that there is a key value pair that needs reordering.\r
-        \end{itemize}\r
-    \item Abort notifications\r
-        \begin{itemize}\r
-            \item Contains a transaction ID of an aborted transaction and the machine ID of the device that created that transaction.\r
-            \item Causes a transaction to be aborted.\r
-        \end{itemize}\r
-    \item Abort acknowledgement notifications\r
-        \begin{itemize}\r
-            \item Contains a transaction ID of an aborted transaction, the machine ID of the device that created that transaction and the abort notification ID that this is acknowledging.\r
-            \item Causes an abort notification to become dead.\r
-            \item Is generated by the device that had created an aborted transaction as an acknowledgement that it saw the aborted transaction notification.\r
-            \item This payload type immediately becomes dead (not live) upon insertion into the data structure.\r
-        \end{itemize}\r
-    \item Data structure re-size notifications\r
-        \begin{itemize}\r
-            \item Contains new size of data structure (number of record allowed in the data structure or something like that).\r
-            \item Causes old data Structure re-size notification to no longer be live.\r
-        \end{itemize}\r
-    \item Server sequence number for a specific record notifications\r
-        \begin{itemize}\r
-            \item Contains a record HMAC and the server sequence number for that record\r
-        \end{itemize}\r
-    \item Transactions\r
-        \begin{itemize}\r
-            \item Contains:\r
-            \begin{itemize}\r
-                \item Transaction ID\r
-                \item A guard condition that can be evaluated\r
-                \item A set of key-value pairs that are to be updated if the guard condition is met.\r
-            \end{itemize}\r
-        \end{itemize}\r
-\end{itemize}\r
+    \item Collision resolution entry is dead if this entry has been seen by all clients after a collision happens.\r
+\end{enumerate}\r
 \r
+When data is at the end of the queue ready to expunge, if:\r
+\begin{enumerate}\r
+    \item If any entry is not dead it must be reinserted into the queue.\r
+\r
+    \item If the slot sequence number is not dead, then a message sequence entry must be inserted.\r
+\end{enumerate}\r
 \r
-\subsection{\textbf{Updates (Online Updates)}}\r
-Updates take place as follows:\r
+\paragraph{Validation procedure on client:}\r
 \begin{enumerate}\r
-    \item A device pulls the latest version of the data structure.  If the device cannot pull the latest version because of network connectivity or some other issues then that device will just work using the local copy of the data structure it has.\r
-    \item The device makes a record as follows:\r
-        \begin{enumerate}\r
-            \item Adds its machine ID.\r
-            \item Creates a vector clock using the largest clock values from each device it knows and its own largest clock value incremented by 1.\r
-            \item Add a random salt (or nonce) for the encryption safety\r
-            \item Fill the record data section with the transactions, key-value pairs, ext.\r
-            \item Fill the remainder of the data section with rescued key-value pairs, transactions, ext (Discussed later).\r
-            \item Pad the record to be the same size for all records.\r
-            \item Calculate the HMAC of the record and add that to the record\r
-            \item Encrypt the record.\r
-        \end{enumerate}\r
-    \item Send the record to the server for insertion into the device's queue.\r
-    \item Wait for response from server stating the new records (the one just sent) server sequence number.  Save this server sequence number for when creating the next record.\r
-    \item \r
+    \item Decrypt each new slot in order.\r
+    \item For each slot:\r
+        (a) check its HMAC, and\r
+        (b) check that the previous entry HMAC field matches the previous entry (in case of a gap do not check for slots on gap margins).\r
+    \item That no slots are slots we have seen before (server trying to pass old slots).    \r
+    \r
+    \item For all other machines, check that the latest sequence number is at least as large (never goes backwards).\r
+    \r
+    \item That the queue has a current queue state entry.\r
+    \r
+    \item That the number of entries received is consistent with the size specified in the queue state entry and/or the queue is growing in size.\r
 \end{enumerate}\r
 \r
+\subsection{Resizing Queue}\r
+Client can make a request to resize the queue. This is done as a write that combines:\r
+  (a) a slot with the message, and (b) a request to the server. The queue can only be expanded, never contracted; attempting to decrease the size of the queue will cause future clients to throw an error.\r
 \r
-\subsection{\textbf{Updates when offline}}\r
-When offline and making updates, the devices should use their local copy of the data structure but do no deletes.  When connection is reestablished the following should take place:\r
 \r
-\begin{enumerate}\r
-    \item Pull the latest version of the data structure.\r
-    \item Update local copy of the data structure except for own devices device queue (do deletions as needed)\r
-    \item Calculate many records are "new" to the data structure and pick the same amount to be deleted\r
-    \item Push the updates and the deletes to the server\r
-    \item Wait for sequence numbers for the recently pushed records\r
-    \item Push the sequence numbers for the recently pushed records (using online updates from the section above)\r
-\end{enumerate}\r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
 \r
-This kind of update will result in the latest key-value pair being the last pushed record from this update (if no other updates are occurring at the same time).  The arbitrator can then commit or abort as needed but in the mean time the key-value pair may be an old one (but have the largest server sequence number).\r
+\subsection{The Arbitrator}\r
+Each key has an arbitrator that makes the final decision when it comes to whether a specific transaction containing that key updates the state of the system or is aborted.  This ensures that clients can make offline updates and then push those updates to the server at a later time.  The arbitrator then tries to merge those updates and if possible will commit them into the current working state of the system.  If not possible then the arbitrator will abort that transaction.  The arbitrator arbitrates on transactions in order of transaction sequence number.\r
 \r
-\subsection{\textbf{Deletions}}\r
-When deciding which records to delete the following is to be done:\r
-\begin{enumerate}\r
-    \item Order all the records in order based on their server sequence numbers\r
-    \item Calculate the difference between the current size of the data structure and the minimum size of the data structure (lets call this $m$)\r
-    \item Delete the oldest m records based on the ordering from step 1. \r
-    \begin{itemize}\r
-        \item If a record to be deleted has live data in it then the whole data structure needs to be resized.\r
-    \end{itemize}\r
-\end{enumerate}\r
 \r
-Note this makes that size of the data structure be bounded.\r
-If there are $n$ devices and the data structure has a minimum size of $m$.  Then the max size of the data structure is given by $m + n -1$ for the case when all the devices make an update at the same time.   \r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
+\section{Server Algorithm}\r
+$s \in SN$ is a sequence number\\\r
+$sv \in SV$ is a slot's value\\\r
+$slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$ \\ \\\r
+\textbf{State} \\\r
+\textit{SL = set of live slots on server} \\\r
+\textit{max = maximum number of slots (input only for resize message)} \\\r
+\textit{n = number of slots} \\ \\\r
+\textbf{Helper Function} \\\r
+$MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv}\r
+\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\\r
+$MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \r
+\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\\r
+$SeqN(slot_s = \tuple{s, sv})=s$ \\\r
+$SlotVal(slot_s = \tuple{s, sv})=sv$ \\\r
 \r
-\subsection{\textbf{Rescuing Transactions, Commits, Aborts, Ext}}\r
-Data should be proactively rescued from the "oldest" records currently in the data structure.  Unused space in new records should be used to rescue data from old records so that when it comes time to delete the old records, there are no live pieces of data that need to be rescued.  When a piece of data is rescued, it is rescued with its vector clock as well (so that the time of that data can be saved).\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Slot:}\\\r
+Returns to the client the slots that have a sequence number that is greater than or equal to the sequence number that is in the requese.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetSlot}{$s_g$}\r
+\State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
 \r
-When deciding which data to rescue the following is to be done:\r
-\begin{enumerate}\r
-    \item Order all the records in order based on their server sequence numbers\r
-    \item Create an ordered list of currently live transactions, commits, aborts, ext from the oldest $n$ records from step one where the order is based on the age of the data (how old the record .\r
-    \item Randomly select from the list of live data to save.  Save as much as can fit in the current new record.  The random selection could give higher probability to data from records that are to be deleted sooner.\r
-\end{enumerate}\r
 \r
-If a record needs to be deleted but still contains live data then the data structure needs to be resized.  \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Slot:}\\\r
+Puts a slot in the server memory if the slot has the correct sequence number.  Also resizes the server memory if needed.\\\r
+\begin{algorithmic}[1]\r
+\Function{PutSlot}{$s_p,sv_p,max'$}\r
+\If{$(max' \neq \emptyset)$}  \Comment{Resize}\r
+\State $max \gets max'$\r
+\EndIf\r
+\State $\tuple{s_n,sv_n} \gets MaxSlot(SL)$\Comment{Last sv}\r
+%\State $s_n \gets SeqN(\tuple{s_n,sv_n})$\r
+\If{$(s_p = s_n + 1)$}\r
+    \If{$n = max$}\r
+        \State $\tuple{s_m,sv_m} \gets MinSlot(SL)$\Comment{First sv}\r
+        \State $SL \gets SL - \{\tuple{s_m,sv_m}\}$\r
+    \Else \Comment{$n < max$}\r
+        \State $n \gets n + 1$\r
+    \EndIf\r
+    \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$\r
+    \State \Return{$(true,\emptyset)$}\r
+\Else\r
+    \State \Return{$(false,\{\tuple{s,sv}\in SL \mid \r
+    s \geq s_p\})$}\r
+\EndIf\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
 \r
-\subsection{\textbf{Checking the Data Structure}}\r
-Checking the data structure for consistency is done as follows:\r
-\begin{enumerate}\r
-    \item Verify that each record in the data structure has an HMAC that matches the data in the record.\r
-    \item Verify that there are at least as many records in the data structure as stated in the largest data structure size record.\r
-    \item Make sure that for each device queue the difference between the vector clock value of the device queues clock is at most 1 between 2 consecutive messages.\r
-    \item Verify that no currently live data Structure re-size notification is smaller than the last known data structure size.  Data structure can only grow in size.\r
-    \item Verify that all the server sequence numbers for the records that are currently present have unique numbers that have a difference of 1 (no gaps).\r
-    \item Verify record server sequence numbers against the stated server sequence numbers in the server sequence number notification payloads (make sure the server is not changing the sequence number on the fly).\r
-    \item Verify that no to records have the same server sequence number\r
-\end{enumerate}\r
 \r
 \r
-\subsection{\textbf{The Arbitrator}}\r
-The arbitrator can:\r
-\begin{enumerate}\r
-    \item Send Commits\r
-    \item Send Aborts\r
-\end{enumerate}\r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
+\section{\textbf{Client}}\r
+\r
+\subsection{\textbf{Client Notation Conventions}}\r
+$k$ is key of entry \\\r
+$v$ is value of entry \\\r
+$size$ is a size (target size of the current block chain) \\\r
+$kv$ is a key-value pair $\tuple{k,v}$ \\\r
+$KV$ is a set of $kv$ \\\r
+$id$ is a machine ID \\\r
+$seq$ is a sequence number \\\r
+$hmac_p$ is the HMAC value of the previous slot \\\r
+$hmac_c$ is the HMAC value of the current slot \\\r
+$Guard$ is a set of$ \tuple{k,v,$logical operator$}$ which can be evaluated to a boolean \\\r
+\r
+$trans$ is a transaction entry , $\tuple{seq, id, KV, Guard}$  \\\r
+$lastmsg$ is a last message entry, $\tuple{seq, id}$ \\\r
+$qstate$ is a queue state entry, $\tuple{size}$ \\\r
+$colres$ is a collision resolution entry, $\tuple{id, seq_{old}, seq_{new}, true \lor false}$ \\\r
+$newkey$ is a new key entry, $\tuple{k, id}$, $id$ is ID of arbitrator \\\r
+$commit$ is a commit transaction entry, $\tuple{seq_{trans},KV}$, id is id of arbitrator \\\r
+$abort$ is an abort transaction entry, $\tuple{seq_{trans},id_{trans}}$ \\\r
+\r
+\r
+$de$ is a data entry that can one of: $trans$, $lastmsg$, $qstate$, $colres$, $newkey$, $commit$, $abort$ \\\r
+$DE$ is a set of all data entries, possibly of different types, in a single message, set of $de$\\\r
+\r
+$slotDat = \tuple{seq,id,DE,hmac_p,hmac_c}$ \\\r
+$slot = \tuple{seq, Encrpt(slotDat)}$\\\r
+\r
+\subsection{\textbf{Client State}}\r
+\r
+\subsubsection{Constants}\r
+$LOCAL\_ID$ = machine ID of this client.\\\r
+$RESIZE\_THRESH\_PERCENT$ = percent of slots that need to have live data to trigger a resize.\\\r
+$RESIZE\_PERCENT$ = percent that we should grow the block chain to.\\\r
+$DATA\_ENTRY\_SET\_MAX\_SIZE$ = max size that a data entry set can have (in bytes).\\\r
+$DEAD\_SLOT\_COUNT$ = number of slots to keep dead if possible at the end of the block chain.\\\r
+$MAX\_RESCUE\_SKIPS$ = number of skips that are allowed when saving data entries.\\\r
+\r
+\subsubsection{Primitive Variables}\r
+$max\_size$ = max size of the block chain\\\r
+\r
+\subsubsection{Sets and Lists}\r
+\r
+$PendingTransQueue$ = Queue of pending transactions that need to be pushed to the block chain, $\tuple{PendingTrans}$\\\r
+$PendingTrans= \tuple{KV, Guard} = \tuple{$set of key value pairs, set of guard conditions$}$.\\\r
+$Arbitrator$ = set of $\tuple{k,id}$ containing the key and its arbitrating device.\\\r
+$LastSlot$ = set of $\tuple{id, seq}$ containing the machine ID and the largest sequence number from that machine ID.\\\r
+$LocalSlots$ = set of slots that are in the clients local buffer (initially $\emptyset$), data is decrypted.\\\r
+$RejectedSlotList$ = ordered list of the sequence numbers of slots that this client tried to insert but were rejected.\\\r
+$CommittedKV$ = set of committed key value pairs (initially $\emptyset$).\\\r
+$SpeculatedKV$ = set of speculated key value pairs (initially $\emptyset$).\r
+\r
+\subsection{Helper Functions}\r
+The following helper functions are needed:\\\r
+\r
+$MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\\r
+$MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\\r
+\r
+\r
+% Get Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Byte Size:}\\\r
+Get the size in bytes of the thing that is passed in.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetSize}{$a$}\r
+    \State \Return{Size in bytes of $a$}\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+% Error\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Error:}\\\r
+Prints an error message and halts the execution of the client.\\\r
+\begin{algorithmic}[1]\r
+\Function{Error}{$msg$}\r
+    \State $Print(msg)$\r
+    \State $Halt()$\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+% Get Next Sequence Number\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Next Sequence Number:}\\\r
+Get the next sequence number for insertion into the block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetNextSeq}{$k$}\r
+    \LeftComment{Get the largest known sequence number}\r
+    \State $seq_{ret} \gets seq$ such that $\tuple{id, seq}\in LastSlo \land (\forall \tuple{id', seq'} \in LastSlo, seq \geq seq')$\\\r
     \r
-\subsubsection{\textbf{Commits}}\r
-Commits have the following properties\r
-\begin{itemize}\r
-    \item Agree with the ordering of the server sequence numbers\r
-    \item Once a key-value pair is commited it can not be commited again.\r
-    \item Cannot commit an already aborted transaction.\r
-    \item Commits state the ordering of key-value pairs.\r
-    \item Can disagree with the ordering of server sequence numbers but\r
-    \item Should occur frequently as to make sure that the commit order matches the server sequence ordering as closely as possible (prevent large divergence of the 2 orderings)\r
-\end{itemize}\r
-    \r
-\subsubsection{\textbf{Aborts}}\r
-Aborts are used to show which transactions have been aborted and will not be used in the total ordering of the transactions.  Aborts are considered live until an abort acknowledgement is presented.\r
-When the transaction is aborted then the devices should simply act as if it were never present when evaluating for the latest key-value pair.\r
-    \r
-\subsection{\textbf{Live Status}}\r
-Live Status of entries:\r
-\begin{enumerate}\r
-    \item Key-Value Entry/Data Entry is dead if either:\r
-    \begin{enumerate}\r
-        \item There is a newer key-value pair:\r
-        \begin{itemize}\r
-            \item There is a transaction with a newer vector clock value.\r
-            \item There is a commit that for this key-value pair.\r
-            \item There is an abort for this key-value pair.\r
-        \end{itemize}\r
-        \item It is incomplete.\r
-        \item It is an abort notification that has an abort notification acknowledgment\r
-        \item It is an abort notification acknowledgment (dead on arrival).\r
-    \end{enumerate}\r
-    \item Data is live if there are multiple versions of the same data (key-value pair) in which the vector clock values show concurrency.  All versions are kept live the arbitrator arbitrates.\r
-    \item Multiple versions of the same data (same transaction ID for example) are not all live.  Only the version with the largest server sequence number is live.  \r
+    \LeftComment{Add one to the largest seq number to generate the new seq number}\r
+    \State \Return{$seq_{ret} + 1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get Arbitrator\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Arbitrator:}\\\r
+Get the arbitrator for a given key.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetArbitrator}{$k$}\r
+    \State $\tuple{k_1,id_1} \gets \tuple{k_2,id_2} $ \textit{such that} $ \tuple{k_2,id_2} \in Arbitrator \land k_2=k$\r
+    \State \Return{$id_1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get Arbitrator KV\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Arbitrator for KV Set:}\\\r
+Get the arbitrator for a given key value set.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetArbitratorKV}{$KV$}\r
+    \State $\tuple{k,v} \gets \tuple{k',v'}$ such that $\tuple{k',v'} \in KV$\r
+    \State $\tuple{k_1,id_1} \gets \tuple{k_2,id_2} $ \textit{such that} $ \tuple{k_2,id_2} \in Arbitrator \land k_2=k$\r
+    \State \Return{$id_1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Transaction arbitrator\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Arbitrator for a Transaction:}\\\r
+Check that the arbitrators for a given set are all the same arbitrator.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckArbitrator}{$PendingTrans_a$}\r
+    \State $id_{arb} \gets NULL$\\\r
+    \State $\tuple{KV_a, Guard_a} \gets PendingTrans_a$\r
     \r
-\end{enumerate}\r
+    \ForAll{$\tuple{k',v'} \in KV_a$}\r
+        \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
+        \r
+        \If{$id_{arb} = NULL$}  \r
+            \State $id_{arb} \gets id'$\r
+        \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
+            \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \ForAll{$\tuple{k',v', lop'} \in Guard_a$}\r
+        \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
+        \r
+        \If{$id_{arb} = NULL$}  \r
+            \State $id_{arb} \gets id'$\r
+        \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
+            \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
+        \EndIf\r
+    \EndFor\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Commits\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all Commits:}\\\r
+Get all commits that are currently in the local block chain.  Iterate over all the local slots and extract all the commits from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetCommits}{$ $}\r
+    \State $ComSet \gets \emptyset$ \Comment{Set of the commits}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $ComSet \gets ComSet \cup \{c |c \in DE',c$is a $commit\}$\r
+    \EndFor\r
+    \State \Return{$ComSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Transactions\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all Transactions:}\\\r
+Get all transactions that are currently in the local block chain.  Iterate over all the local slots and extract all the transactions from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetTrans}{$ $}\r
+    \State $TransSet \gets \emptyset$ \Comment{Set of the trans}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $TransSet \gets TransSet \cup \{c |c \in DE',c$is a $trans\}$\r
+    \EndFor\r
+    \State \Return{$TransSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all aborts\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all aborts:}\\\r
+Get all aborts that are currently in the local block chain.  Iterate over all the local slots and extract all the aborts from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetAborts}{$ $}\r
+    \State $AbrtSet \gets \emptyset$ \Comment{Set of the aborts}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $AbrtSet \gets AbrtSet \cup \{c |c \in DE',c$is a $abort\}$\r
+    \EndFor\r
+    \State \Return{$AbrtSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Queue States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all queue states:}\\\r
+Get all qstates that are currently in the local block chain.  Iterate over all the local slots and extract all the qstates from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetQStates}{$ $}\r
+    \State $QSet \gets \emptyset$ \Comment{Set of the qstates}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $QSet \gets QSet \cup \{c |c \in DE',c$is a $qstate\}$\r
+    \EndFor\r
+    \State \Return{$QSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Last Messages States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all last message data entrues:}\\\r
+Get all last msg that are currently in the local block chain.  Iterate over all the local slots and extract all the last msg from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetLastMsg}{$ $}\r
+    \State $LMSet \gets \emptyset$ \Comment{Set of the last msg}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $LMSet \gets LMSet \cup \{c |c \in DE',c$is a $lastmsg\}$\r
+    \EndFor\r
+    \State \Return{$LMSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Queue State Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Queue State Live:}\\\r
+A queue state is dead if there is another queue state data entry that has a larger queue state.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckQStateLive}{$qstate_a$}\r
+    \State $\tuple{size_a} \gets qstate_a$\r
+    \State $AllQStates \gets$ \Call{GetQState}{} \Comment{Get all the qstates} \\\r
+    \r
+    \If{$\exists \tuple{size'} \in AllQStates, size' > size_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Commit Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Commit Live:}\\\r
+A commit is dead if for every key value pair in the commit there is a commit with a larger transaction sequence number that has a key value pair with the same key.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckCommitLive}{$commit_a$}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
+    \State $KSet \gets \{k|\tuple{k,v} \in KV\}$\r
+    \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits} \\\r
+    \r
+    \LeftComment{Iterate all commits that are newer in time}\r
+    \ForAll{$\tuple{seq_{trans}',KV'}\in AllCommits, seq_{trans}' > seq_{a_{trans}}$}\r
+        \State $KVSet \gets KVSet \setminus \{k|\tuple{k,v} \in KV'\}$\\\r
+        \r
+        \If{$KVSet = \emptyset$}\r
+            \State \Return{false} \Comment{All keys have a newer commit}\r
+        \EndIf\r
+    \EndFor\r
+    \State \Return{true} \Comment{If got here then some keys still live}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Last Message Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Last Message Live:}\\\r
+The last message is dead if the device in question pushed a slot that has a larger sequence number than the one recorded in the last message data entry. \\\r
+\begin{algorithmic}[1]\r
+\Function{CheckLastMsgLive}{$lastmsg_a$}\r
+    \State $\tuple{seq_a, id_a} \gets lastmsg_a$\\\r
+    \r
+    \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+%Check Collision Resolution Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Collision Resolution Live:}\\\r
+Check if a collision resolution data entry is live or not.  This done by checking if all clients that we know about have seen the collision resolution entry.  This is checked by seeing if all devices have inserted a message with a larger sequence number into the block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckColResLive}{$colres_a$}\r
+    \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, equal_a} \gets colres_a$\\\r
+    \r
+    \If{$\forall \tuple{id', seq'} \in LastSlot, seq' \geq seq_{a_{new}}$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check New Key Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check New Key Live:}\\\r
+A new key data entry is always live.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckNewkeyLive}{$newkey_a$}\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Abort Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Abort Live:}\\\r
+Check if an abort data entry is live or not.  Abort is dead if the device whos transaction was aborted sees the abort.  This is checked by seeing if that device inserted a slot into the block chain which has a sequence numberl that is larger than the aborts sequence number.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckAbortLive}{$abort_a, seq_a$}\r
+    \State $\tuple{seq_{a_{trans}},id_a} \gets abort_a$\\\r
+    \r
+    \LeftComment{The device whos transaction was aborted saw the abort}\r
+    \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Transaction Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Transaction Live:}\\\r
+A transaction is dead if there is an abort for that transaction or if there is a commit for that a transaction that came after this transaction.  Since transactions must be committed in order of there insertion, seeing a transaction that is committed and has a larger sequence number than the transaction in question means that the transaction in question was committed at some point.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckTransLive}{$trans_a$}\r
+    \State $\tuple{seq_a, id_a, KV_a, Guard_a} \gets trans_a$\r
+    \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits}\r
+    \State $AllAborts \gets$ \Call{GetAborts}{} \Comment{Get all the aborts} \\\r
+    \r
+    \If{$\exists \tuple{seq_{abrt}',seq_{trans}',id'} \in AllAborts, seq_{trans}' = seq_a$}\r
+        \State \Return{false}\r
+    \ElsIf{$\exists \tuple{seq_{trans}',KV'} \in AllCommits, seq_{trans}' \geq seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Live:}\\\r
+Checks if a data entry is live based on its type.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckLive}{$datentry, seq$}\r
+    \If{$datentry$ is a $commit$}\r
+        \State \Return{\Call{CheckCommitLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $abort$}\r
+        \State \Return{\Call{CheckAbortLive}{$datentry, seq$}}\\r
+    \ElsIf{$datentry$ is a $trans$}\r
+        \State \Return{\Call{CheckTransLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $lastmsg$}\r
+        \State \Return{\Call{CheckLastMsgLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $colres$}\r
+        \State \Return{\Call{CheckColResLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $qstate$}\r
+        \State \Return{\Call{CheckQStateLive}{$datentry$}}\r
+    \ElsIf{$datentry$ is a $newkey$}\r
+        \State \Return{\Call{CheckNewkeyLive}{$datentry$}}\r
+    \Else\r
+        \State \Call{Error}{"Unknown data entry type."}\r
+    \EndIf\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Slot Has Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Slot Has Live:}\\\r
+Check if the slot has any live data entries in it. Do this by looking at all the data entries in the slot and checking if they are live\\\r
+\begin{algorithmic}[1]\r
+\Function{SlotHasLive}{$slot_a$}\r
+    \State $\tuple{s_1, \tuple{seq_2,id,DE,hmac_p,hmac_c}} \in LocalSlots$\r
+    \r
+    \ForAll{$datentry \in DE$}\r
+        \If{\Call{CheckLive}{$datentry, s_1$}} \Comment{an entry is alive}\r
+            \State \Return{true}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State \Return{false} \Comment{All entries were dead}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Calculate Resize Threshold\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Calculate Resize Threshold:}\\\r
+Calculate a threshold for how many slots need to have live data entries in them for a resize to take place.\\\r
+\begin{algorithmic}[1]\r
+\Function{CalcResizeThresh}{$maxsize$}\r
+    \State \Return{$\left \lfloor {maxsize * RESIZE\_THRESH\_PERCENT} \right \rfloor$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Calculate Block Chain New Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Calculate Block Chain New Size:}\\\r
+Calculate the new size of the block chain which we need if we are to resize the data structure.\\\r
+\begin{algorithmic}[1]\r
+\Function{CalcNewSize}{$maxsize$}\r
+    \State \Return{$\left \lceil {maxsize * RESIZE\_THRESH\_PERCENT} \right \rceil$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
 \r
+% Should Resize\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Should Resize:}\\\r
+Check if the block should resize based on some metric of how many slots in the block chain are filled with live data. \\\r
+\begin{algorithmic}[1]\r
+\Function{ShouldResize}{$ $}\r
+    \State $LiveSlots \gets \{slot_s|slot_s \in LocalSlots \land $\Call{SlotHasLive}{$slot_s$}$\}$\r
+    \State $resizethreshold \gets $ \Call{CalcResizeThresh}{$max\_size$}\r
+    \State \Return{$|LiveSlots| \geq resizethreshold$} \Comment{If passes threshold then resize}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Queue State \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Queue State:}\\\r
+Generate a queue state data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateQState}{$size_a$}\r
+    \State \Return{$\tuple{size_a}$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Abort\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Abort:}\\\r
+Generate a abort data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateAbort}{$seq_a, id_a$}\r
+    \State \Return{$\tuple{seq_a, id_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Collision\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create ColRes:}\\\r
+Generate a colres data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateColRes}{$is_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a$}\r
+    \State \Return{$\tuple{id_a, seq_{a_{old}}, seq_{a_{new}},}isequal_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Create Transaction\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Transaction:}\\\r
+Generate a transaction data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateTrans}{$pendingtrans_a, seq_a$}\r
+    \State $\tuple{KV_a, Guard_a} \gets pendingtrans_a$\r
+    \State \Return{$\tuple{seq_a, LOCAL\_ID, KV_a, Guard_a}$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Commit:}\\\r
+Generate a commit data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateCommit}{$seq_a,KV_a$}\r
+    \State \Return{$\tuple{seq_a,KV_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create New Key\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Key:}\\\r
+Generate a new key data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateNewKey}{$k_a, id_a$}\r
+    \State \Return{$\tuple{k_a,id_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Data Entry Set Has Space \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Data Entry Set Has Space :}\\\r
+Checks if a data entry set has enough space for a new data entry to be inserted.\\\r
+\begin{algorithmic}[1]\r
+\Function{DEHasSpace}{$DE_a, de_a$}\r
+    \State $newsize \gets $ \Call{GetSize}{$DE_a$}\r
+    \State $newsize \gets newsize +$ \Call{GetSize}{$de_a$}\r
+    \State \Return{$newsize \leq DATA\_ENTRY\_SET\_MAX\_SIZE$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Rescued Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Rescued Date Entry:}\\\r
+For commits only the key-value pairs that are most recent (no newer commit that has those key values in it).\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateRescuedCommit}{$commit_a$}\r
+    \State $AllCommits \gets $ \Call{GetCommits}{}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets de_a$\r
+    \State $NewKV \gets KV_a$\\\r
+\r
+    \LeftComment{Get rid of all key values that have newer commits}\r
+    \ForAll{$\tuple{k_a, v_a} \in KV_a$}\r
+        \LeftComment{Iterate over all commits that are newer than the rescue commit}\r
+        \ForAll{$\tuple{seq', KV'} \in AllCommits, seq' > seq_{a_{trans}}$}\r
+            \If{$\exists \tuple{k', v'} \in KV', k' = k_a$}\r
+                \State $NewKV \gets NewKV \setminus \tuple{k_a, v_a}$\r
+                \State Break\r
+            \EndIf\r
+        \EndFor\r
+    \EndFor\r
+    \State \Return{$\tuple{seq_{a_{trans}}, NewKV}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Rescued Date Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Rescued Date Entry:}\\\r
+Generate the data entry rescued version of the entry.  For some data entry types such as commits, the entry is not rescued as is.  For commits only the key-value pairs that are most recent (no newer commit that has those key values in it).\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateRescuedEntry}{$de_a$}\r
+\r
+    \If{$de_a$is a $commit$}\r
+        \State \Return{\Call{CreateRescuedCommit}{$de_a$}}\r
+    \EndIf\r
+    \r
+    \State \Return{$de_a$} \Comment{No Modification needed}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Slot Hmacs\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Slot HMACs:}\\\r
+Check that each slot has not been tampered with by checking that the stored HMAC matches the calculated HMAC.  Also check thatthe slot number reported by the server matches the slot number of the actual slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSlotsHmacAndSeq}{$Slots_a$}\r
+    \ForAll{$slot_a \in Slots_a$}\r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
+        \State $calchmac \gets $ \Call{GenerateHmac}{$seq_{a_2}, id_a, DE_a, hmac_{a_p}$}\r
+    \r
+        \If{$seq_{a_1} \neq seq_{a_2}$}\r
+          \State \Call{Error}{"Slot sequence number mismatch"}\r
+        \ElsIf{$calchmac \neq hmac_{a_c}$}\r
+            \State \Call{Error}{"Slot HMAC mismatch"}\r
+        \EndIf\r
+    \EndFor\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
 \r
-\section{\textbf{System Guarantees}}\r
-\begin{itemize}\r
-    \item Server cannot view data inside records\r
-    \item Server cannot forge or modify or create any records\r
-    \item Server cannot withhold any records\r
-    \item Server cannot reorder records that could not have been ordered differently due to network latency\r
-    \item Server cannot delete records unless told to do so.\r
-    \item There will always be an obvious key-value pair that is the latest key value pair.\r
-    \item The data structure is bounded in size such that $m$ is the minimum size of the data structure,  $n$ is the number of devices in the system and $s$ is the current size of the data structure: $m \leq s \leq (m+n-1)$\r
-    \item Data structure can only grow when there are too may key-value pairs (and aborts) than what fit in the current data structure size within reason.\r
-    \item No currently valid data can be lost by the system and go undetected.\r
-    \item Devices can operate offline and re-sync with the system and get a consistent view of the system\r
-    \item If the server tries to hold a device on an older version of the data structure, that device can eventually rejoin the main data structure without problems.\r
-    \item Devices that have a transaction aborted will be able to be notified about the abort indefinately (no time frame when notification must be accepted).\r
+% Check HMAC Chain\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check HMAC Chain:}\\\r
+Check that the HMAC chain has not been violated.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckHmacChain}{$Slots_a$}\r
+    \State $SlotsList \gets Slots_a$ sorted by sequence number\\  \r
+    \r
+    \r
+    \LeftComment{Check all new slots}\r
+    \ForAll{$index \in [2: |SlotsList|]$}\r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[i-1]$\r
+        \State $\tuple{seq_{b_1}, \tuple{seq_{b_2},id_b,DE_b,hmac_{b_p},hmac_{b_c}}} \gets SlotList[i]$\r
+        \r
+        \If{$hmac_{b_p} \neq hmac_{b_c}$}\r
+            \State \Call{Error}{"Invalid previous HMAC."}\r
+        \EndIf    \r
+    \EndFor\\\r
+    \r
+    \LeftComment{Check against slots that we already have in the block chain}\r
+    \If{$|LocalSlots| \neq 0$}\r
+        \State $\tuple{seq, SDE} \gets $\Call{MaxSlot}{$LocalSlots$}\r
+        \State $\tuple{seq{last_2},id_{last},DE_{last},hmac_{last_p},hmac_{last_c}} \gets SDE$\\\r
+        \r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[1]$\\\r
+\r
+        \If{$(seq_{last_2} + 1) = seq_{a_1}$}\r
+            \If{$hmac_{a_p} \neq hmac_{last_c}$}\r
+                \State \Call{Error}{"Invalid previous HMAC."}\r
+            \EndIf\r
+        \EndIf\r
+    \EndIf\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
 \r
-\end{itemize}\r
+\r
+% Check For Old Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check For Old Slots:}\\\r
+Check if the slots are not new.  Checks if the "new" slots are actually new or if they are older than the most recent slot that we have.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckOldSlots}{$Slots_a$}\r
+    \State $\tuple{seq_{new}, Dat_{new}} \gets$ \Call{MinSlot }{$Slots_a$} \Comment{Get the oldest new slot}\r
+    \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot }{$LocalSlots$} \Comment{Get the newest slot seen}\\\r
+    \r
+    \If{$seq_{new} \leq seq_{local}$} \Comment{The slots were not newer than what was already seen}\r
+        \State \Call{Error}{"Server sent old slots."}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Check if slots have the same sequence number but different data entries}\r
+    \ForAll{$\tuple{seq, Dat} \in Slots_a$}\r
+        \If{$\exists \tuple{seq', Dat'} \in (LocalSlots \cup Slots_a), seq'=seq \land Dat' \neq Dat$}\r
+            \State \Call{Error}{"Slot sequence number match but data does not"}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get All Queue States with Sequence numbers\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Get All Queue States with Sequence numbers:}\\\r
+% Gets all the queue states with the sequence number of the slot that the queue state was inside.\r
+% \begin{algorithmic}[1]\r
+% \Function{GetQStateWithSeq}{$Slots_a$}\r
+%     \State $QSet \gets \emptyset$\\\r
+    \r
+%     \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
+%         \ForAll{$de' \in DE'$}\r
+%             \If{$de'$ is a  $qstate$}\r
+%                 \State $QSet \gets QSet \cup \{\tuple{seq_1', de'}\}$\r
+%             \EndIf\r
+%         \EndFor\r
+%     \EndFor\\\r
+    \r
+%     \State \Return{$QSet$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+% Get All Queue States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get All Queue States:}\\\r
+Gets all the queue states from the slots that were passed in.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetQState}{$Slots_a$}\r
+    \State $QSet \gets \emptyset$\\\r
+    \r
+    \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
+        \ForAll{$de' \in DE'$}\r
+            \If{$de'$ is a  $qstate$}\r
+                \State $QSet \gets QSet \cup \{de'\}$\r
+            \EndIf\r
+        \EndFor\r
+    \EndFor\\\r
     \r
+    \State \Return{$QSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Check Size With Gap\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Size With Gap:}\\\r
+Checks that the block chain size is correct when there is a gap in the block chain.  This check makes sure that the server is not hiding any information from the client.  If there is a gap and there is only 1 queue state in the new slot entries then there must have at least that many slots since the old slot entry must have been purged.  If there is more than 1 queue state then the block chain is still growing check the smallest max size and there should be at least that many slots. \\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSizeWithGap}{$Slots_a$}    \r
+    %\State $QSSet \gets $ \Call{GetQStateWithSeq}{$Slots_a$}\r
+    %\State $\tuple{seq_{max}, size_{max}} \gets \tuple{seq, size}$ such that $\tuple{seq, size} \in QSSet \land \forall \tuple{seq', size'} \in QSSet, size \geq size'$ \Comment{Get largest size}\r
+    %\State $\tuple{seq_{min}, size_{min}} \gets \tuple{seq, size}$ such that $\tuple{seq, size} \in QSSet \land \forall \tuple{seq', size'} \in QSSet , size \leq size'$ \Comment{Get smallest size}\r
+    \r
+    \State $QSet \gets $ \Call{GetQState}{$Slots_a$}\r
+    \State $size_{max} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \geq size'$ \r
+    \State $size_{min} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \leq size'$     \r
+    \State $Slots_{oldmax} \gets \emptyset$\\\r
+\r
+    \r
+    \LeftComment{If only 1 max size then we must have all the slots for that size}\r
+    \If{$(|QSSet| = 1) \land (|Slots_a| \neq size_{max})$}\r
+        \State \Call{Error}{"Missing Slots"}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{We definitely have all the slots}\r
+    \If$|Slots_a| = size_{max}$\r
+        \State \Return{} \Comment{We have all the slots}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{We must have at least this many slots}\r
+    \If$|Slots_a| < size_{min}$\r
+        \State \Call{Error}{"Missing Slots"}\r
+    \EndIf\\\r
+\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Size:}\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSize}{$Slots_a$}    \r
+    \State $\tuple{seq_{old_{max}}, Dat_{old_{max}}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $\tuple{seq_{new_{max}}, Dat_{new_{max}}} \gets $ \Call{MinSlot}{$Slots_a$}\\\r
+    \r
+    \If{$(seq_{old_{max}} + 1) = seq_{new_{max}}$}\r
+        \LeftComment{No Gap so cannot say anything about the size}\r
+        \State \Return{} \r
+    \Else \r
+        \LeftComment{Has a gap so we need to do checks}\r
+        \State \Call{CheckSizeWithGap}{$Slots_a$}\r
+    \EndIf\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% % Initialize the expected size of the block chain\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Initialize the expected size of the block chain:}\\\r
+% Initialize the expected size of the block chain based on the size at the server.\\\r
+% \begin{algorithmic}[1]\r
+% \Function{InitExpSize}{$seq_a$}\r
+%     \State $startingsize \gets 0$\\\r
+\r
+%     \If{$seq_a < max\_size$} \Comment{Check whether saves slots are full on server}\r
+%         \State $startingsize \gets seq_a$\r
+%     \Else\r
+%         \State $startingsize \gets max\_size$\r
+%     \EndIf\\\r
+    \r
+%     \State \Return{$startingsize$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+% % Update the expected size of the block chain\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Update the expected size of the block chain:}\\\r
+% Update the expected size of the block chain.\\\r
+% \begin{algorithmic}[1]\r
+% \Function{UpdateExpSize}{$size_a$}\r
+%     \State $size_a \gets size_a + 1$\\\r
+    \r
+%     \If{$size_a > max\_size$}\Comment{Expected size $\leq max\_size$}\r
+%         \State $ssize_a \gets max_\_size$\r
+%     \EndIf\\\r
+    \r
+%     \State \Return{$size_a$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+\r
+\r
+% Update Last Message\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Commit Data Entry:}\\\r
+Process a commit entry.  Updates the local copy of commits.\\\r
+\begin{algorithmic}[1]\r
+\Function{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, updateinglocal_a$}\r
+    \State $\tuple{id_{old}, seq_{old}} \gets \tuple{id', seq'}$ such that $\tuple{id', seq'} \in LastSlot \land id'=id$\\\r
+    \r
+    \If{$id_a = LOCAL\_ID$}\r
+        \If{$\lnot updateinglocal_a \land (seq_a \neq seq_{old})$}\r
+            \LeftComment{This client did not make any updates so its latest sequence number should not change}\r
+            \State \Call{Error}{"Mismatch on local machine sequence number"}\r
+        \EndIf\r
+    \Else\r
+        \If{$seq_{old} > seq_a$}\r
+            \State \Call{Error}{"Rollback on remote machine sequence number"}\r
+        \EndIf\r
+    \EndIf\\\r
+    \r
+    \State $LastSlot \gets LastSlot \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LastSlot, id=id_a\}$\r
+    \State $LastSlot \gets LastSlot \cup \{\tuple{id_a, seq_a}\}$\r
+    \r
+    \State \Return{$LstSlt_a \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LstSlt_a, id=id_a\}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Commit Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Commit Data Entry:}\\\r
+Process a commit entry.  Updates the local copy of commits.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessCommit}{$commit_a$}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
+    \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CommittedKV \land \tuple{k',v'}\in KV_a \land k'=k\}$\r
+    \State $CommittedKV \gets (CommittedKV \setminus DKV) \cup KV_a$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Queue State Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Queue State Entry:}\\\r
+Process a queue state entry. Updates the max size of the block chain\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessQState}{$qstate_a$}\r
+    \State $\tuple{size_a} \gets qstate_a$\r
+    \State $max\_size \gets size_a$ \Comment{Update the max size we can have}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Collision Resolution Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Queue State Entry:}\\\r
+Process a collision resolution entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessColres}{$colres_a, NewSlots_a$}\r
+    \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a}$\r
+    \State $AllSlots \gets LocalSlots \cup NewSlots_a$\r
+    \State $index \gets seq_{a_{old}}$\\\r
+    \r
+    \While{$index <= seq_{a_{new}}$}\r
+        \State $slt \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
+        \r
+        \If{$\exists \tuple{seq' Dat'} \in AllSlots, seq' = index$}\r
+            \State $\tuple{seq, Dat} \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
+            \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets Dat$\r
+            \If{$isequal_a \neq (id=id_a)$}\r
+                \State \Call{Error}{"Trying to insert rejected messages for slot"}\r
+            \EndIf\r
+        \EndIf\\\r
+        \State $index \gets index + 1$\r
+    \EndWhile\r
+    \r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process New Key Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process New Key Entry:}\\\r
+Process a queue state entry. Adds a key to the key arbitrator set\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessNewkey}{$newkey_a$}\r
+    \State $\tuple{seq_a, k_a, id_a} \gets newkey_a$\r
+    \State $Arbitrator \gets Arbitrator \cup \{\tuple{k_a,id_a}\}$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Process Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Data Entry:}\\\r
+Process the data entry based on what kind of entry it is.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessDatEntry}{$slot_a, NewSlots_a,LstSlt_a$}\r
+    \If{$datentry_a$ is a $commit$}\r
+        \State \Call{ProcessCommit}{$dataentry_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $abort$}\r
+        \LeftComment{Do Nothing in this case}\r
+        \r
+    \ElsIf{$datentry_a$ is a $trans$}\r
+        \LeftComment{Do Nothing in this case}\r
+    \r
+    \ElsIf{$datentry_a$ is a $lastmsg$}\r
+        \State $\tuple{seq_a, id_a} \gets dataentry_a$\r
+        \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, false$}\r
+    \r
+    \ElsIf{$datentry_a$ is a $colres$}\r
+        \State \Call{ProcessColres}{$dataentry_a, NewSlots_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $qstate$}\r
+        \State \Call{ProcessQState}{$dataentry_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $newkey$}\r
+        \State \Call{ProcessNewkey}{$dataentry_a$}\r
+        \r
+    \Else\r
+        \State \Call{Error}{"Unknown data entry type."}\r
+    \EndIf\r
+    \r
+    \State \Return{$LstSlt_a$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Delete Local Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Delete Local Slots:}\\\r
+Deletes local slots that are deleted at the server.  This keeps the size of the local block chain bounded.\\\r
+\begin{algorithmic}[1]\r
+\Function{DeleteLocalSlots}{$ $}\r
+    \State $\tuple{seq_{max}, Dat_{max}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $seq_{min} \gets seq_{max} - max\_size$ \Comment{Min sequence number we should keep}\r
+    \State $LSDelete \gets \emptyset$\r
+        \r
+    \If{$|LocalSlots| \leq max\_size$}\r
+        \State \Return{} \Comment{Nothing to delete}\r
+    \EndIf\\\r
+    \r
+    \State $LSDelete \gets \{\tuple{seq', Dat'}|\tuple{seq', Dat'} \in LocalSlots, seq' > seq_{min}\}$\r
+    \State $LocalSlots \gets LocalSlots \setminus LSDelete$    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Speculative KV\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Speculative KV:}\\\r
+Speculates on what the most recent key value pairs will be based on the latest committed key value pairs and the uncommitted transactions.\\\r
+\begin{algorithmic}[1]\r
+\Function{SpeculateKV}{$ $}\r
+    \State $AllTrans \gets$ \Call{GetTrans}{}\r
+    \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
+    \State $CurrKV \gets CommittedKV$\r
+    \State $DKV \gets \emptyset$\r
+\r
+    \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
+        \If{\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
+            \State $CurrKV \gets (CurrKV \setminus DKV) \cup KV_t$\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State \Return{$CurrKV$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Validate and Update \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Validate Update:}\\\r
+Validate the block chain and insert into the local block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{ValidateUpdate}{$NewSlots_a, updatinglocal_a$}\r
+    \State $\tuple{seq_{oldest}, Dat_{oldest}} \gets$ \Call{MinSlot}{$NewSlots_a$}\r
+    \State $\tuple{seq_{newest}, Dat_{newest}} \gets$ \Call{MaxSlot}{$NewSlots_a$}\r
+    \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $LastSlotTmp \gets LastSlot$\\\r
+    %\State $currsize \gets $\Call{InitExpSize}{$seq_{oldest}$}\\\r
+    \r
+    \State \Call{CheckSlotsHmacAndSeq}{$NewSlots_a$} \Comment{Check all the HMACs}\r
+    \State \Call{CheckHmacChain}{$NewSlots_a$} \Comment{Check HMAC Chain} \r
+    \State \Call{CheckOldSlots}{$NewSlots_a$} \Comment{Check if new slots are actually old slots} \r
+    \State \Call{CheckSize}{$NewSlots_a$} \Comment{Check if the size is correct}\\\r
+    \r
+    \ForAll{$slot_a \in NewSlots_a$ in order of sequence number}\r
+        \If{$slot_a \in LocalSlots$} \Comment{Client already has this slot}\r
+            \State $NewSlots_a \gets NewSlots_a \setminus \{slot_a\}$\r
+            \State Continue\r
+        \EndIf\\\r
+    \r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
+        \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_{a_1}, id_a, LstSlt_a, updatinglocal_a$}\\\r
+        \r
+        \ForAll{$de_a \in DE_a$} \Comment{Process each data entry}\r
+            \State $LstSlt_a \gets $ \Call{ProccessDatEntry}{$de_a, NewSlots_a,LstSlt_a$}\r
+        \EndFor\\\r
+    \r
+        %\State $currsize \gets $ \Call{UpdateExpSize}{$currsize$}\\\r
+        \State $LocalSlots \gets LocalSlots \cup \{slot_a\}$ \Comment{Add to local Chain}\r
+    \EndFor\\\r
+    \r
+    \If{$seq_{oldest} > (seq_{local} +1) \land LastSlotTmp \neq \emptyset$}\r
+        \LeftComment{There was a gap so there should be a complete set of information on each previously seen client}\r
+        \State \Call{Error}{"Missing records for machines"}\r
+    \EndIf\\\r
+    \r
+    \State \Call{DeleteLocalSlots}{ } \Comment{Delete old slots from local}\r
+    \State $SpeculatedKV \gets $\Call{SpeculateKV}{ } \Comment{Speculate on what will be latest KV set}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Decrypt Validate Insert Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Decrypt Validate Insert Slots:}\\\r
+Decrypts slots, validates (checks for malicious activity) slots and inserts the slots into the local block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{DecryptValidateInsert}{$NewSlots_a, updatinglocal_a$}\r
+    \State $DecryptedSlots \gets \emptyset$\r
+    \State $DDat \gets NULL$\\\r
+    \r
+    \ForAll{$\tuple{seq', EDat'} \in NewSlots_a$}\r
+        \State $DDat \gets $ \Call{Decrypt}{$EDat'$}\r
+        \State $DecryptedSlots \gets DecryptedSlots \cup \tuple{seq',DDat}$\r
+    \EndFor\\\r
+    \State \Call{ValidateUpdate}{$DecryptedSlots, updatinglocal_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check and Create Last Message Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check and Create Last Message Data Entry:}\\\r
+Check if a last message entry needs to be created for this slot and if so create it.  The check is done by checking if there are any newer slots with the same id or if there is already a last message slot with a newer sequence number\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckCreateLastMsgEntry}{$seq_a, id_a$}\r
+    \State $AllLastMsg \gets$ \Call{GetLastMsg}{}\\\r
+    \r
+    \LeftComment{Already Has one}\r
+    \If{$\exists  \tuple{seq', id'} \in AllLastMsg, id_a=id' \land seq'=seq_a$}\r
+        \State \Return{$\{\}$}\\\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Not latest slot from that client}\r
+    \If{$\exists  \tuple{seq_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots, id_a=id' \land seq_1'>seq_a$}\r
+        \State \Return{$\{\}$}\\\r
+    \EndIf\\\r
+    \r
+    \r
+    \State \Return{$\{\tuple{seq_a, id_a}\}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Mandatory Rescue\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Mandatory Rescue:}\\\r
+This rescue is mandatory before any types of data entries (excpet queue states) can be placed into the data entry section of the new slot.  Returns the data entry Set or null if the first slot could not be cleared (the live data in that slot could not fit in this current slot). \\\r
+\begin{algorithmic}[1]\r
+\Function{MandatoryRescue}{$DE_a$}\r
+    \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
+    \State $cseq \gets smallestseq$\\\r
+    \r
+    \LeftComment{Check the least slots to rescue and live entries}\r
+    \While{$cseq < (smallestseq + DEAD\_SLOT\_COUNT)$}\r
+        \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
+        \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\r
+        \State $DE' \gets DE' \cup$ \Call{CheckCreateLastMsgEntry}{$seq', id'$} \Comment{Get the last message too if we need it}\\\r
+        \r
+        \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
+            \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
+                \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\r
+                \If{\Call{DEHasSpace}{$DE_a, de$}}\r
+                    \State $DE_a \gets DE_a \cup de$ \Comment{Had enough space to add it}\r
+                \ElsIf{$currentseq = smallestseq$}\r
+                    \State \Return{$NULL$}\r
+                \Else\r
+                    \State \Return{$DE_a$}\r
+                \EndIf\r
+            \EndIf\r
+        \EndFor\\\r
+        \r
+        \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
+    \EndWhile\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Optional Rescue\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Optional Rescue:}\\\r
+This rescue is not mandatory.  This is trying to fill the remaining portion of the slot with rescued data so that no space is wasted. If we encounter a data entry that does not fit move on to the next, maybe that one will fit.  Do this until we skipped too many live data entries\\\r
+\begin{algorithmic}[1]\r
+\Function{OptionalRescue}{$DE_a$}\r
+    \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
+    \State $largestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \geq seq')$\r
+\r
+    \State $numofskips \gets 0$\r
+    \State $cseq \gets smallestseq$\\\r
+    \r
+    \LeftComment{Check the least slots to rescue and live entries}\r
+    \While{$cseq < largestseq$}\r
+        \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
+        \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\\\r
+        \r
+        \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
+            \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
+                \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\\\r
+                \r
+                \If{$de \in DE_a$} \Comment{Already being rescued}\r
+                    \State Continue\r
+                \EndIf\\\r
+                \r
+                \If{\Call{DEHasSpace}{$DE_a, de$}}\r
+                    \State $DE_a \gets DE_a \cup de$ \Comment{Had enoug space to add it}\r
+                \ElsIf{$numofskips \geq MAX\_RESCUE\_SKIPS$}\r
+                    \State \Return{$DE_a$}\r
+                \Else\r
+                    $numofskips \gets numofskips +1$\r
+                \EndIf\r
+            \EndIf\r
+        \EndFor\\\r
+        \r
+        \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
+    \EndWhile\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Rejected Messages\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Rejected Messages:}\\\r
+\begin{algorithmic}[1]\r
+\Function{RejectedMessages}{$DE_a$}\r
+    \State $seq_{old} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \geq seq'$\r
+    \State $prev \gets -1$\\\r
+    \r
+    \r
+    \r
+    \If{$|RejectedSlotList| \geq REJECTED\_THRESH$}\r
+        \State $seq_{new} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \leq seq'$\\\r
+        \State $colres \gets $ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, seq_{new}, false$}    \r
+        \State \Return{$DE_a \cup \{colres\}$}\r
+    \EndIf\\\r
+    \r
+    \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
+        \If{$\exists \tuple{seq',Dat'} \in LocalSlots$}\r
+            \State Break\r
+        \EndIf\r
+        \State $prev \gets seq$\r
+    \EndFor\\\r
+    \r
+    \If{$prev \neq -1$}\r
+        \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, prev, false$}\r
+    \EndIf\\\r
+    \r
+    \State $RejectedSlotList \gets \{\tuple{seq}| \tuple{seq} \in RejectedSlotList, seq > prev\}$\\\r
+    \r
+    \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
+        \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq,seq, false$}\r
+    \EndFor\\\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+\r
+% Arbitrate\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Arbitrate:}\\\r
+\begin{algorithmic}[1]\r
+\Function{Arbitrate}{$DE_a$}\r
+    \State $AllCommits \gets$ \Call{GetCommits}{}\r
+    \State $AllTrans \gets$ \Call{GetTrans}{}\r
+    \State $LiveCommits \gets \{c| c\in AllCommits, $\Call{CheckCommitLive}{$c$}$\}$\r
+    \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
+    \State $KV \gets \emptyset$\r
+    \State $lastcomseq \gets -1$\r
+    \State $CurrKV \gets \emptyset$\r
+    \State $DKV \gets \emptyset$\r
+    \State $KVTmp \gets \emptyset$\\\r
+    \r
+    \LeftComment{Get all the latest commits}\r
+    \ForAll{$\tuple{seq_{trans}',KV'} \in LiveCommits$}\r
+        \State $CurrKV \gets CurrKV \cup KV'$\r
+    \EndFor\\\r
+    \r
+    \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
+        \If{\Call{GetArbitratorKV}{$KV_t$} $\neq LOCAL\_ID$}\r
+            \State Continue \Comment{Client not arbitrator for this transaction}\r
+        \EndIf\\\r
+    \r
+        \If{$\lnot$\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
+            \State $abortde \gets $\Call{CreateAbort}{$seq_t, id_t$}\r
+            \LeftComment{No more space so we cant arbitrate any further}\r
+            \If($lnot$\Call{DeHasSpace}{$DE_a, abortde$})\r
+                \State \Return{$DE_a$}\r
+            \EndIf\r
+            \State $DE_a \gets DE_a \cup abortde$\r
+        \Else\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in KV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
+            \State $KVTmp \gets (KV \setminus DKV) \cup KV'$\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KVTmp \land k'=k\}$\r
+            \State $CurrKV \gets (CurrKV \setminus DKV) \cup KVTmp$\r
+            \State $commitde \gets $ \Call{CreateCommit}{$seq_t,KVTmp$}\r
+            \r
+            \If{$\lnot$ \Call{DeHasSpace}{$DE_a, commitde$}}\r
+                \If{$lastcomseq \neq -1$}\r
+                    \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
+                \EndIf\r
+                \State \Return{$DE_a$}\r
+            \Else\r
+                \State $KV \gets KVTmp$\r
+                \State $lastcomseq \gets seq_t$\r
+            \EndIf\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
+    \State \Return{$DE_a$}\r
+\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create New Slot\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Slot:}\\\r
+Create a slot and encrypt it.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateNewSlot}{$seq_a, DE_a$}\r
+    \State $\tuple{seq, SDE} \gets \tuple{seq', SDE'}$ such that $\tuple{seq', SDE'}\in LocalSlots \land (\forall \tuple{seq'', DE''} \in LocalSlots, seq' \geq seq'')$\r
+    \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets SDE$\\\r
+    \r
+    \State $newhmac \gets $ \Call{GenerateHmac}{$seq_a, LOCAL\_ID, DE_a, hmac_p$}\r
+    \State $newSDE \gets \tuple{seq,LOCAL\_ID,DE_a,hmac_c,newhmac}$\r
+    \State $encryptnewSDE \gets $\Call{Encrypt}{newSDE}\\\r
+    \r
+    \State \Return{$\tuple{seq_a, encryptnewSDE}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Send Data to Server\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Send Data to Server:}\\\r
+Send the data to the server.  If this fails then new slots will be returned by the server.\\\r
+\begin{algorithmic}[1]\r
+\Function{SendToServer}{$seq_a, DE_a, newsize_a$}\r
+    \LeftComment{Make the slot and try to send to server}\r
+    \State $newslot \gets $ \Call{CreateNewSlot}{$seq_a, DE_a$}\r
+    \State $\tuple{success, newslots} \gets$ \Call{PutSlot}{$seq_a, newslot, newsize_a$}\\\r
+    \r
+    \If{$success$}\r
+        \State $RejectedSlotList \gets \emptyset$\r
+        \State \Return{$\tuple{true, \{newslot\}}$}\r
+    \Else\r
+        \If{$|newslots| = 0$}\r
+            \State \Call{Error}{"Server rejected but did not send any slots"}\r
+        \EndIf\r
+        \State $RejectedSlotList \gets RejectedSlotList \cup \{seq_a\}$\r
+        \State \Return{$\tuple{false, newslots}$}\r
+    \EndIf\\\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Try Insert Transaction\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Try Insert Transaction:}\\\r
+Try to insert a transaction into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
+\begin{algorithmic}[1]\r
+\Function{TryInsertTransaction}{$pendingtrans_a, forceresize$}\r
+    \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
+    \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
+    \State $newsize \gets 0$\r
+    \State $trans \gets$ \Call{CreateTrans}{$pendingtrans_a, seq$}\r
+    \State $transinserted \gets false$\r
+    \State $slotstoinsert \gets \emptyset$\\\r
+    \r
+    \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
+    \State $resize \gets resize \lor forceresize$\r
+    \If{$resize$}\r
+        \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
+        \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
+    \EndIf\\\r
+    \r
+    \If{$RejectedSlotList \neq \emptyset$}   \r
+        \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
+    \If{$DE = NULL$}\r
+        \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
+        \State \Return{\Call{TryInsertTransaction}{$trans_a, true$}}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
+    \r
+    \If{\Call{DEHasSpace}{$DE, trans$}} \Comment{transaction fits}\r
+        \State $DE \gets DE \cup trans$\r
+        \State $transinserted \gets true$\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Rescue data to fill slot data entry section}\r
+    \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
+    \r
+    \LeftComment{Send to server.}\r
+    \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
+    \r
+    \LeftComment{Insert the slots into the local bloakc chain}\r
+    \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
+    \r
+    \State \Return{$transinserted \land success$} \Comment{Return if  succeeded or not}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Try Insert New Key\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Try Insert New Key:}\\\r
+Try to insert a new key into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
+\begin{algorithmic}[1]\r
+\Function{TryInsertNewKey}{$k_a, id_a, forceresize$}\r
+    \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
+    \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
+    \State $newsize \gets 0$\r
+    \r
+    \r
+    \State $newkey \gets$ \Call{CreateNewKey}{$k_a, id_a$}\r
+    \State $newkeyinserted \gets false$\r
+    \State $slotstoinsert \gets \emptyset$\\\r
+    \r
+    \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
+    \State $resize \gets resize \lor forceresize$\r
+    \If{$resize$}\r
+        \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
+        \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
+    \EndIf\\\r
+    \r
+    \If{$RejectedSlotList \neq \emptyset$}   \r
+        \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
+    \If{$DE = NULL$}\r
+        \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
+        \State \Return{\Call{TryInsertNewKey}{$k_a, id_a, true$}}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
+    \r
+    \If{\Call{DEHasSpace}{$DE, newkey$}} \Comment{new key fits}\r
+        \State $DE \gets DE \cup newkey$\r
+        \State $newkeyinserted \gets true$\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Rescue data to fill slot data entry section}\r
+    \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
+    \r
+    \LeftComment{Send to server.}\r
+    \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
+    \r
+    \LeftComment{Insert the slots into the local block chain}\r
+    \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
+    \r
+    \State \Return{$newkeyinserted \land success$} \Comment{Return if  succeeded or not}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+\r
+\subsection{Client Interfaces}\r
+\r
+% Put KV pair\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Put Key Value Pair:}\\\r
+Puts a key value pair into the key value pair buffer\\\r
+\begin{algorithmic}[1]\r
+\Function{PutKeyValue}{$k,v$}\r
+    \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
+    \r
+    \LeftComment{Check if KV already has a key value pair for the specified key}\r
+    \State $DSet \gets \{\tuple{k_1,v_1} | \tuple{k_1,v_1} \in KV \land k_1 = k\}$\\\r
+    \r
+    \If{$DSet \neq \emptyset$}\r
+        \State \Call{Error}{"Value for key already in most recent update"}\r
+    \EndIf\\\r
+        \r
+    \State $KV \gets KV \cup \{\tuple{k,v}\}$ \Comment{Add key value pair}\r
+    \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
+    \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get KV Pair Speculative\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get KV Pair Speculative:}\\\r
+Get the value for the key while speculating.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetValueSpeculate}{$k_a$}\r
+    %\State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
+    \r
+    \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
+\State \Return{$v$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Update\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Update}\\\r
+Sync with the server and get all the latest slots.\\\r
+\begin{algorithmic}[1]\r
+\Function{Update}{$ $}\r
+    \State $\tuple{seq, Dat} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $NewSlots \gets$ \Call{GetSlots}{$seq$}\r
+    \State \Call{DecryptValidateInsert}{$NewSlots, false$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get KV Pair Committed\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get KV Pair Committed:}\\\r
+Get the value for the key which have been committed.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetValueCommit}{$k_a$}\r
+    \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in Committed \land k = k_a$\r
+    \State \Return{$v$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Put guard condition\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Put Guard:}\\\r
+Puts a guard transaction into the key value update.  A guard is a key value with a logical operator ($lop$).\\\r
+\begin{algorithmic}[1]\r
+\Function{PutGuard}{$k,v, lop$}\r
+    \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
+    \r
+    \If{$\tuple{k,v, lop} \in Guard$}\r
+        \State \Return{} \Comment{Already have guard condition in update}\r
+    \EndIf\\\r
+    \r
+    \State $Guard \gets Guard \cup \{\tuple{k,v,lop}\}$\r
+    \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
+    \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
+\EndFunction    \r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Transaction Start\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Transaction Start:}\\\r
+Starts a transaction.  Clears out the key value pair update buffer.\\\r
+\begin{algorithmic}[1]\r
+\Function{TransactionStart}{$ $}\r
+    % \LeftComment{Reset the key value update buffer}\r
+    % \State $KVUpdate \gets \tuple{\emptyset, \emptyset}$\r
+    \State $PendingTrans \gets NULL$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Transaction Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Transaction Commit:}\\\r
+Commits the transaction into the block chain.  Keeps attempting to insert the transaction into the block chain until it succeeds.\\\r
+\begin{algorithmic}[1]\r
+\Function{Transaction Commit}{$ $}\r
+    \State $DKV \gets \emptyset$\r
+    \State $pt \gets NULL$\\\r
+    \r
+    \State $PendingTransQueue.$\Call{push}{$PendingTrans$}\\\r
+        \r
+    \While{\Call{HasConnectionToServer}{ } $\land PendingTransQueue \neq \emptyset$}\r
+        \State $pt \gets PendingTransQueue.$\Call{peak}{ }\\\r
+    \r
+        \If{\Call{TryInsertTransaction}{$pt, false$}}\r
+            \State $PendingTransQueue.$\Call{pop}{ }\r
+        \EndIf\r
+    \EndWhile\\    \r
+    \r
+    \LeftComment{Go Through local pending transactions and speculate}\r
+    \ForAll{$\tuple{KV, Guard} \in PendingTransQueue$}\r
+        \If{\Call{EvaluateGuard}{$Guard, SpeculatedKV$}}\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in SpeculatedKV \land \tuple{k',v'}\in KV \land k'=k\}$\r
+            \State $SpeculatedKV \gets (SpeculatedKV \setminus DKV) \cup KV$\r
+        \EndIf\r
+    \EndFor\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+%Create New Key \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Key:}\\\r
+Creates a new key and specifies which machine ID is the arbitrator. If there is already a new key entry in the block chain for this key name then do not insert into the chain, another client got there first. \\\r
+\begin{algorithmic}[1]\r
+\Function{Transaction Commit}{$k_a, id_a$}\r
+    \State $success \gets false$\\\r
+    \While{$\lnot success$}\r
+        \If{$\exists \tuple{k',id'} \in Arbitrator, k' = k_a$}\r
+            \State \Return{$false$} \Comment{Key already created}\r
+        \EndIf\\\r
+    \r
+        \State $success \gets$ \Call{TryInsertNewKey}{$k_a, id_a, false$}\r
+    \EndWhile\r
+    \r
+    \State \Return{$true$} \Comment{If got here then insertion was correct}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
 \r
-\end{document}
\ No newline at end of file
+\end{document}\r