\documentclass[11pt]{article}
\newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}
\usepackage{color}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{mathrsfs}
\usepackage{amssymb}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[all]{xy}
\usepackage{varwidth}

\newtheorem{theorem}{Theorem}
\newtheorem{prop}{Proposition}
\newtheorem{lem}{Lemma}
\newtheorem{defn}{Definition}
\newcommand{\note}[1]{{\color{red} \bf [[#1]]}}
\newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}
\newcommand*\xor{\mathbin{\oplus}}
\begin{document}


\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text


\section{\textbf{Introduction}}





\section{\textbf{Server}}
The server maintains a collection of slots such that each slot contains some data.
The operations on the slot are as follows:
\begin{itemize}
    \item Put slot
    \item Get slot
    \item Delete Slot
\end{itemize}

\subsection{\textbf{Server Notation Conventions}}
$s \in SN$ is a server sequence number\\
$sv \in SV$ is a slot's value\\
$slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$

\subsection{\textbf{Server State}}
\textit{n = current server sequence number}\\
\textit{SL = set of live slots on the server}\\

\textbf{Initial Server State}\\
$SL = \emptyset$\\
$n = 0$

\subsection{\textbf{Put Slot}}
Put slot is an operation that inserts data into a new slot at the server.\\

%Put Function
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Put Function:}
\begin{algorithmic}[1]
\Function{PutSlotServer}{$sv_p$}
    \State $s_p \gets n$
    \State $n \gets n + 1$
    \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

\subsection{\textbf{Get Slot}}
Get slot is an operation that returns all server slots that are greater than some server sequence number.\\

% Get Function
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get Function:}
\begin{algorithmic}[1]
\Function{GetSlotServer}{$s_g$}
    \State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

\subsection{\textbf{Delete Slot}}
Delete slot is an operation that deletes all live slots that have server sequence numbers that are equal to or less than some server sequence number.\\

%Delete Function
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Delete Function:}
\begin{algorithmic}[1]
\Function{DeleteSlotServer}{$s_d$}
    \State $SD \gets \{\tuple{s, sv} \in SL \mid s \leq s_g\}$
    \State $SL \gets SL - SD$
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}








\section{\textbf{Client}}
The data structure acts as a key store where key-value pairs can be read and set.
The data structure exposes the following functions:
\begin{itemize}
    \item Put Transaction
    \item Get key-value pair
    \item Create new key
\end{itemize}

\subsection{\textbf{Client Notation Conventions}}

$K$ is the set of all keys.\\
$MID$ is the set of the machine IDs of the devices that are in the system.
$K_{mid}$ is a set of all keys that have device mid as the arbitrator\\
$ssn_s$ is the server sequence number of a record $s$\\
$mid_s \in MID$ is the machine ID for the device that created $record_s$.\\
$hmac_s$ is the HMAC of $record_s$\\
$c_{mid}$ is the latest read clock for a device with machine ID $mid$\\
$vc_s = \{c_{mid} | mid \in MID\}$\\
$rid_s = \tuple{mid_s, c_{mid_s}}$
$k$ is a key entry\\
$v$ is a value entry\\
$kv_n$ is a key-value entry $\tuple{k_n,v_n} , k \in K$\\

$tid_s = \tuple{mid_s,c_{mid_s}}$\\
$guard_s = \tuple{\{kv_1, kv_2, ... ,kv_n | \exists mid \in MID, \forall n, kv_n[k] \in K_{mid}\},$ boolean condition using $ \{kv_1, kv_2, ... ,kv_n\}} $\\
$transaction_s = \{mid_s,vc_{s_t} ,\{kv_1, kv_2,...kv_n | \exists mid \in MID, mid = guard_s[mid], \forall n, kv_n[k] \in K_{mid}\},guard_s\}$\\
$commit_s = \tuple{tid_s,vc_s}$\\
$abort_s = \tuple{tid_s,mid_s,vc_s}$\\
$sequence_s = \tuple{rid_s, ssn_s}$\\
$delete_s = \tuple{ssn_d}$\\
$resize_s = \tuple{x | x \in \mathbb{N}}$\\
$newkey_s = \tuple{k_s, vc_s, ssn_s$ or $NULL, mid_s}$\\

$payload_s = \{x_1, x_2,..., x_k | \forall k, x_k \in \{$transaction, commit, abort, resize, newkey, sequence, delete$\}\}$\\
$rd_s = \tuple{mid_s, vc_s, hmac_s, payload_s}$ \\
$record_s = \tuple{ssn_s,rd_s}$\\

\subsection{\textbf{Client State}}
\textit{s = largest server sequence number pulled from the server by a device} \\
\textit{R = set of records pulled from the server so far with their server sequence numbers} \\
\textit{RL = set of records that contain live data} \\

\subsection{Helper Functions}
The following helper functions are needed:\\


%Throw Error
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Throw Error:}
\begin{algorithmic}[1]
\Function{Error}{$msg$}
    \State $Print(msg)$
    \State $Halt()$
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Error Message
\begin{algorithmic}[1]
\Procedure{Error}{$msg$}
\State $Print(msg)$
\State $Halt()$
\EndProcedure
\end{algorithmic}


%Get Latest Data Structure From Server
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get  Latest Data Structure From Server}:
\begin{algorithmic}[1]
\Function{GetLatestDataStruct}{}
    \State $largestSsn \gets $ Largest $ssn$ of all $ssn$ in $R$
    \State $R \gets R \cup $ \Call{GetSlotServer}{$largestSsn + 1$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Get Payload Items from Record with SSN
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get Payload Items from Record with SSN}:
\begin{algorithmic}[1]
\Function{GetPayloadItemsWithSSN}{$record_s$}
    \State $PISSN \gets \emptyset$ \Comment{Set of Payload Items with ssn}
    \State $\tuple{ssn_s, rd_s} \gets record_s$
    \State $\tuple{mid_s, vc_s, hmac_s, payload_s} \gets rd_s$\\
    
    \ForAll{$payloadItems$ in $payload_s$}
        \State $PISSN \gets PISSN \cup \tuple{payloadItem, ssn_s}$
    \EndFor\\
    
    \State \Return {$PISSN$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Get rid For Record
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get rid For Record}:
\begin{algorithmic}[1]
\Function{GetRid}{$record_s$}
    \State $\tuple{ssn_s, \tuple{mid_s, \{c_{mid_1}, c_{mid_2}, ... , c_{mid_k}\}, hmac_s, payload_s}} \gets record_s$
    \State \Return {$\tuple{mid_s, c_{mid_s}}$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Sequence Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Sequence Live}:
\begin{algorithmic}[1]
\Function{SequenceLive}{$sequence_s, ssn_{s1}$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $AS \gets \emptyset$    \Comment{Set of all Payload Items that are sequences}
    \State $StillHasRecord \gets False$
    \State $\tuple{rid_s, ssn_{s2}} \gets sequence_s$\\
    
    \ForAll{$record$ in $R$}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
        \If{$rid_s = $\Call{GetRid}{$record$}}
            \State $StillHasRecord \gets True$
        \EndIf
    \EndFor\\
    
    \If{$\lnot StillHasRecord$}  \Comment{The Record does not exists anymore}
        \State \Return{False}
    \EndIf\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{$payload$ is a $sequence$}
            \State $AS \gets AS \cup \{\tuple{ssn, payload}\}$   \Comment{Extract all sequence payloads}
        \EndIf
    \EndFor\\
    
    \ForAll{$\tuple{ssn_1', \tuple{rid', ssn_2'}}$ in $AS$}
        \If{$(rid'=rid_s) \land (ssn_1' > ssn_{s_1})$}
            \State \Return{False}
        \EndIf
    \EndFor \\
    \State \Return{True}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

% Delete Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Delete Live}:
\begin{algorithmic}[1]
\Function{DeleteLive}{$delete_s, ssn_s$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $AD \gets \emptyset$    \Comment{Set of all Payload Items that are deletes}
    \State $\tuple{ssn_d} \gets delete_s$\\
    
    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{$payload$ is a $delete$}
            \State $AD \gets AD \cup \{\tuple{ssn, payload}\}$   \Comment{Extract all delete payloads}
        \EndIf
    \EndFor\\
    
    \ForAll{delete in AD}
        \State $\tuple{{ssn_s}', \tuple{{ssn_d}'}} \gets delete$
        \If{${ssn_d}' > ssn_d$}    \Comment{More recently deleted record}
            \State \Return{False}
        \ElsIf{$({ssn_d}'= ssn_d) \land ({ssn_s}' > ssn_s)$} \Comment{More recent delete of same record}
            \State \Return{False}
        \EndIf
    \EndFor \\
    \State \Return{True}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Abort Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Abort Live}:
\begin{algorithmic}[1]
\Function{AbortLive}{$abort_s$}
    \State $\tuple{tid_s,mid_s,vc_s} \gets abort_s$\\
    \ForAll{record in R}
        \State $\tuple{ssn_s',\tuple{mid_s', vc_s', hmac_s', payload_s'}} \gets record$
        \If{$(mid_s'=mid_s) \land (vc_s' > vc_s)$}
            \State \Return{True}
        \EndIf
    \EndFor\\
    \State \Return{False}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Resize Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Resize Live}:
\begin{algorithmic}[1]
\Function{ResizeLive}{$resize_s, ssn_s$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $AR \gets \emptyset$    \Comment{Set of all Payload Items that are resize}
    \State $\tuple{size} \gets resize_s$\\
    
    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{$payload$ is a $resize$}
            \State $AR \gets AR \cup \{\tuple{ssn, payload}\}$   \Comment{Extract all resize payloads}
        \EndIf
    \EndFor\\
    
    \ForAll{$\tuple{ssn', \tuple{size'}}$ in $AR$}
        \If{$size' > size$}
            \State \Return{False}
        \ElsIf{$(size'=size) \land (ssn' > ssn_s)$}
            \State \Return{False}
        \EndIf
    \EndFor \\
    \State \Return{True}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%New Key Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{New Key Live:}
\begin{algorithmic}[1]
\Function{NewKeyLive}{$newkey_s, ssn_r$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $ANK \gets \emptyset$    \Comment{Set of all Payload Items that are new keys}
    \State $\tuple{k_s, vc_s, ssn_s, mid_s}\gets newkey_s$\\
    
    \If{$ssn_s = NULL$}                    \Comment{Make sure ssn is the correct one}
        \State $ssn_s \gets ssn_r$
    \EndIf\\
    
    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn', payload'}$ in $API$}
        \If{$payload'$ is a $newkey$}
            \State $ANK \gets ANK \cup \{\tuple{ssn', payload'}\}$   \Comment{Extract all new key payloads}
        \EndIf
    \EndFor\\
    
    \ForAll{$\tuple{ssn', \tuple{k_s', vc_s', ssn_s', mid_s'}}$ in $ANK$}
        \If{$vc_s' < vc_s$}
            \State \Return{False}
        \ElsIf{$\lnot (vc_s' < vc_s) \land (ssn_s' < ssn_s)$}
            \State \Return{False}
        \EndIf
    \EndFor \\
    \State \Return{True}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Is Live
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Is Live:}:
\begin{algorithmic}[1]
\Function{IsLive}{$item_s, ssn_S$}
    \If{$item_s$ is a $tansaction$}
        \State \Return{\Call{TransactionLive}{$item_s, ssn_s$}}
    
    \ElsIf{$item_s$ is a $commit$}
        \State \Return{\Call{CommitLive}{$item_s, ssn_s$}}
    
    \ElsIf{$item_s$ is a $abort$}
        \State \Return{\Call{AbortLive}{$item_s$}}
            
    \ElsIf{$item_s$ is a $delete$}
        \State \Return{\Call{DeleteLive}{$item_s, ssn_s$}}
        
    \ElsIf{$item_s$ is a $commit$}
        \State \Return{\Call{ResizeLive}{$item_s, ssn_s$}}
        
    \ElsIf{$item_s$ is a $sequence$}
        \State \Return{\Call{SequenceLive}{$item_s, ssn_s$}}
        
    \ElsIf{$item_s$ is a $newkey$}
        \State \Return{\Call{NewKeyLive}{$item_s, ssn_s$}}
        
    \ElsIf{$item_s$ is a $keyvalue$}
        \State \Return{\Call{KeyValueLive}{$item_s, ssn_s$}}
    \EndIf\\
    
    \State \Return{False}  \Comment{Will never get here}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Record has live payload data
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Record has live payload data:}:
\begin{algorithmic}[1]
\Function{HasLivePayload}{$record_s, ssn_s$}
    \State $\tuple{mid_s, vc_s, hmac_s, payload_s} \gets record_s$
    \ForAll{$item$ in $payload_s$}
        \If{\Call{IsLive}{$item, ssn_s$}}
            \State \Return{True}
        \EndIf
    \EndFor
    \State \Return{False}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

% Get Data Structure Size
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get Data Structure Size:}
\begin{algorithmic}[1]
\Function{GetDataStrucSize}{ }
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $AR \gets \emptyset$    \Comment{Set of all Payload Items that are resize}

    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{($payload$ is a $resize) \land$ \Call{IsLive}{$payload, ssn$}}
            \State $\tuple{size'} \gets payload$
            \State \Return{$size'$}
        \EndIf
    \EndFor\\
    
    \State \Return{$0$}  \Comment{Get Here only if data structure has no entries}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Rescue Payload Items
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Rescue Payload Items}:
\begin{algorithmic}[1]
\Function{RescuePayloadItems}{$payload_s$}
    \State $PIList =$ empty list of $\tuple{ssn, payload}$
    \State $SpaceRemaining = MaxPayloadSize - $ \Call{GetSize}{$payload_s$}
    
    \If{$SpaceRemaining = 0$}
        \Return{$payload_s$}
    \EndIf\\
    
    \ForAll{record in R}
        \State $PIList \gets PIList \cup$ \Call{GetPayloadItemsWithSSN}{$record$}
    \EndFor\\
    
    \State Sort $PIList$ by $ssn$ from lowest to highest\\
    
    \ForAll{$\tuple{ssn', payload'}$ in $PIList$}
        \If{\Call{IsLive}{$payload', ssn'$} $\land$ \Call{getRandomBoolean}{ } }
        
            \If{\Call{GetSize}{$payload'$} $ > SpaceRemaining$} \Comment{No more space for resuces in payload}
                \State \Return{$payload_s$}
            \EndIf
            
            \State $SpaceRemaining \gets SpaceRemaining -$ \Call{GetSize}{$payload'$}
            \State $payload_s \gets payload_s \cup payload'$
        \EndIf
    \EndFor\\
    \State \Return{$payload_s$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Pad Payload
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Pad Payload}:
\begin{algorithmic}[1]
\Function{PadPayload}{$payload_s$}    \State $SpaceRemaining =  MaxPayloadSize - $ \Call{GetSize}{$payload_s$}
    
    \If{$SpaceRemaining = 0$}
        \State \Return{$payload_s$}
    \EndIf\\
    \State \Return{$payload_s \gets payload_s \cup$ \Call{GenerateFiller}{$SpaceRemaining$}}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Delete Records
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Delete Records (Deletes n records)}:
\begin{algorithmic}[1]
\Function{Delete}{$num\_of\_records$}
    \State $RS \gets$ List of all Records sorted by ssn and has form $\tuple{ssn_s, record_s}$
    \State $n \gets 0$\\
        
    \ForAll{$\tuple{ssn_s, record_s}$ in $RS$}
        \If{\Call{HasLivePayload}{$record_s, ssn_s$}}
            \State \Return{False}
        \Else
            \State \Call{DeleteSlotServer}{$ssn_s$}
            \State $n \gets n + 1$
        \EndIf\\
        
        \If{$n = num\_of\_records$}
            \State \textbf{break}
        \EndIf
    \EndFor\\
    
    \State \Return{True}
    
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Create Resize Data Structure Payload Item
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Create Resize Data Structure Payload Item:}
\begin{algorithmic}[1]
\Function{MakeResizePayload}{ }
    \State $rp \gets$ Empty $resize$ payload
    \State $cs \gets$ \Call{GetDataStrucSize}{ }
    \State $rp \gets \tuple{cs * 2}$
    \State \Return{$rp$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

%Get Arbitrator
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Get Arbitrator:}
\begin{algorithmic}[1]
\Function{GetArbitrator}{$key$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}\\

    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{($payload$ is a $newkey) \land$ \Call{IsLive}{$payload, ssn$}}
            \State $\tuple{k', vc', ssn', mid'} \gets payload$
            
            \If{$k' = key$}
                \State \Return{$mid'$}
            \EndIf
        \EndIf
    \EndFor\\
    \State \Call{Error}{No arbitrator for key: $key$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetArbitrator}{$guard$}
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}
    \State $\tuple{kvSet, condition} \gets guard$\\
    
    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{key, value}$ in $kvSet$}
        \ForAll{$\tuple{ssn, payload}$ in $API$}
            \If{($payload$ is a $newkey) \land$ \Call{IsLive}{$payload, ssn$}}
                \State $\tuple{k', vc', ssn', mid'} \gets payload$
                \If{$k' = key$}
                    \State \Return{$mid'$}
                \EndIf
            \EndIf
        \EndFor
    \EndFor\\
    \State \Call{Error}{No arbitrator for key: $key$}
\EndFunction
\end{algorithmic}

\end{varwidth}% 
}

%Insert Payload
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Insert Payload:}
\begin{algorithmic}[1]
\Function{InsertPayload}{$payload_s$}
    
    \State $numOfRecords \gets |R|+1$
    \State $targetSize \gets $ \Call{GetDataStrucSize}{ }
    \State $numToDelete \gets numOfRecords - targetSize$
    \State $rp \gets NULL$
    \State $hmac \gets NULL$
    \State $vc \gets NULL$
    \State $record \gets NULL$\\

    \If{\Call{GetSize}{$payload_s$} $> MaxPayloadSize$}
        \State \Call{Error}{Payload too large}
    \EndIf\\
    
    \If{$numToDelete > 0$}
        \If{$\lnot$\Call{Delete}{$numToDelete$}}
            \State $rp \gets$\Call{MakeResizePayload}{ }
            \If{\Call{GetSize}{$payload_s \cup rp$} $> MaxPayloadSize$}
                \State \Call{InsertPayload}{$rp$}
            \Else
                \State $payload_s \gets payload_s \cup rp$
            \EndIf
        \EndIf
    \EndIf\\
    
    \State $payload_s \gets$ \Call{RescuePayloadItems}{$payload_s$}
    \State $payload_s \gets$ \Call{PadPayload}{$payload_s$}
    \State $vc \gets $ \Call{GenerateVectorClock}{ }
    \State $hmac \gets$ \Call{GetHmac}{$mid, vc, payload_s$}
    \State $record \gets \tuple{mid,vc,hmac,payload}$
    \State $record \gets $\Call{Encrypt}{$record$}
    \State \Call{PutSlotServer}{$record$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}





\textbf{Check Data Structure for Malicious Activity:}\\
\textbf{Transaction Live}:\\
\textbf{Commit Live}:\\
\textbf{Key Value Live}:\\


%-Rescues
%    - Need to Change Transaction sizes
%    - Need to Change New Key ssn in transaction
%-Gets
%-Arbitration
%   - Insert Commit
%-Check Data Structure



\subsection{\textbf{Create new key}}
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Create new key:}
\begin{algorithmic}[1]
\Function{CreateNewKey}{$keyName, mid$}
    \State $vc \gets $ \Call{GenerateVectorClock}{ }
    \State $newKeyPayload \gets \tuple{keyName, vc, NULL, mid}$
    \State $API \gets \emptyset$   \Comment{Set of all Payload Items}\\

    \State \Call{GetLatestDataStruct}{ } \Comment{Update local version of data struct}\\

    \ForAll{record in R}
        \State $API \gets API \cup$ \Call{GetPayloadItemsWithSSN}{record}
    \EndFor\\
    
    \ForAll{$\tuple{ssn, payload}$ in $API$}
        \If{($payload$ is a $newkey) \land$ \Call{IsLive}{$payload, ssn$}}
            \State $\tuple{k', vc', ssn', mid'} \gets payload$
            
            \If{$k' = key$}
                \State \Return{False}
            \EndIf
        \EndIf
    \EndFor\\
    
    \State \Call{InsertPayload}{$newKeyPayload$}
    \State \Return{True}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

\subsection{\textbf{Put Transaction}}
This operation puts a transaction into the data structure if this transaction fits inside a payload.
If by adding this transaction the data structure size exceeds the proposed max data structure size then deletes take place.  If no deletes can take place (too much live data) then the data structure is resized.\\

%
\noindent\fbox{%
\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\textbf{Put Transaction:}
\begin{algorithmic}[1]
\Function{PutTransaction}{$kVUpdates, guard$}
    \State $mid \gets NULL$
    \State $vc \gets $ \Call{GenerateVectorClock}{ }
    \State $transPayload \gets NULL$\\

    \State \Call{GetLatestDataStruct}{ } \Comment{Update local version of data struct}\\

    \ForAll{$kv$ in $kVUpdates$}
        \State $\tuple{k', v'} \gets kv$
        
        \If{$mid = NULL$}
            \State $mid \gets$ \Call{GetArbitrator}{$k'$}
        \ElsIf{$mid \neq $ \Call{GetArbitrator}{$k'$}}
            \State \Call{Error}{Multiple Arbitrators}
        \EndIf
    \EndFor\\
    
    \If{\Call{GetArbitrator}{$guard$} $\neq mid$}
        \State \Call{Error}{Multiple Arbitrators}
    \EndIf\\
    
    \State $transPayload \gets \tuple{mid, vc, kVUpdates, guard}$
    \State \Call{InsertPayload}{$transPayload$}
\EndFunction
\end{algorithmic}
\end{varwidth}% 
}

\subsection{\textbf{Get key-value pair}}






\end{document}
