31612024664b7701ebde6f18c2243d3bf40a1e6d
[iotcloud.git] / doc2 / iotcloud.tex
1 \documentclass[11pt]{article}\r
2 \newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}\r
3 \usepackage{color}\r
4 \usepackage{amsthm}\r
5 \usepackage{amsmath}\r
6 \usepackage{graphicx}\r
7 \usepackage{mathrsfs}\r
8 \usepackage{amssymb}\r
9 \usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx\r
10 \usepackage[all]{xy}\r
11 \usepackage{varwidth}\r
12 \r
13 \newtheorem{theorem}{Theorem}\r
14 \newtheorem{prop}{Proposition}\r
15 \newtheorem{lem}{Lemma}\r
16 \newtheorem{defn}{Definition}\r
17 \newcommand{\note}[1]{{\color{red} \bf [[#1]]}}\r
18 \newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}\r
19 \newcommand*\xor{\mathbin{\oplus}}\r
20 \algnewcommand{\LeftComment}[1]{\Statex \(\triangleright\) #1}\r
21 \begin{document}\r
22 \r
23 \r
24 \setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text\r
25 \r
26 \r
27 \section{\textbf{Introduction}}\r
28 \r
29 \r
30 \section{Approach}\r
31 \r
32 \subsection{Keys}\r
33 \r
34 Each device has: user id + password\r
35 \r
36 Server login is:\r
37 hash1(user id), hash1(password)\r
38 \r
39 Symmetric Crypto keys is:\r
40 hash2(user id | password)\r
41 \r
42 Server has finite length queue of entries + max\_entry\_identifier +\r
43 server login key\r
44 \r
45 \subsection{Entry layout}\r
46 Each entry has:\r
47 \begin{enumerate}\r
48 \item Sequence identifier\r
49 \item Random IV (if needed by crypto algorithm)\r
50 \item Encrypted payload\r
51 \end{enumerate}\r
52 \r
53 Payload has:\r
54 \begin{enumerate}\r
55 \item Sequence identifier\r
56 \item Machine id (most probably something like a 64-bit random number \r
57 that is self-generated by client)\r
58 \item HMAC of previous slot\r
59 \item Data entries\r
60 \item HMAC of current slot\r
61 \end{enumerate}\r
62 \r
63 A data entry can be one of these:\r
64 \begin{enumerate}\r
65     \item A transaction:\r
66         \begin{itemize}\r
67             \item Contains a sequence number, a set of key value pair updates and a guard condition that can be evaluated.\r
68             \item Must have the same arbitrator for all its key value pair updates and reads within the guard condition\r
69         \end{itemize}\r
70     \r
71     \item A Commit\r
72         \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
73     \r
74     \item An Abort\r
75         \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
76     \r
77     \item New Key:\r
78         \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
79         \r
80     \item Slot sequence entry: Machine id + last message identifier \r
81         \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
82 \r
83     \item Queue state entry: Includes queue size \r
84         \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
85 \r
86     \item Collision resolution entry: message identifier + machine id of a collision winner\r
87         \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
88 \end{enumerate}\r
89 \r
90 \subsection{Live status}\r
91 \r
92 Live status of entries:\r
93 \begin{enumerate}\r
94     \item Transaction is live if it has not been committed or aborted yet.\r
95     \r
96     \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
97         \r
98     \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
99     \r
100     \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
101     \r
102     \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
103 \r
104     \item Queue state entry is dead if there is a newer queue state entry.\r
105     {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
106 \r
107     \item Collision resolution entry is dead if this entry has been seen by all clients after a collision happens.\r
108 \end{enumerate}\r
109 \r
110 When data is at the end of the queue ready to expunge, if:\r
111 \begin{enumerate}\r
112     \item If any entry is not dead it must be reinserted into the queue.\r
113 \r
114     \item If the slot sequence number is not dead, then a message sequence entry must be inserted.\r
115 \end{enumerate}\r
116 \r
117 \paragraph{Validation procedure on client:}\r
118 \begin{enumerate}\r
119     \item Decrypt each new slot in order.\r
120     \item For each slot:\r
121         (a) check its HMAC, and\r
122         (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
123     \item That no slots are slots we have seen before (server trying to pass old slots).    \r
124     \r
125     \item For all other machines, check that the latest sequence number is at least as large (never goes backwards).\r
126     \r
127     \item That the queue has a current queue state entry.\r
128     \r
129     \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
130 \end{enumerate}\r
131 \r
132 \subsection{Resizing Queue}\r
133 Client can make a request to resize the queue. This is done as a write that combines:\r
134   (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
135 \r
136 \r
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
138 \r
139 \subsection{The Arbitrator}\r
140 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
141 \r
142 \r
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
144 \section{Server Algorithm}\r
145 $s \in SN$ is a sequence number\\\r
146 $sv \in SV$ is a slot's value\\\r
147 $slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$ \\ \\\r
148 \textbf{State} \\\r
149 \textit{SL = set of live slots on server} \\\r
150 \textit{max = maximum number of slots (input only for resize message)} \\\r
151 \textit{n = number of slots} \\ \\\r
152 \textbf{Helper Function} \\\r
153 $MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv}\r
154 \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\\r
155 $MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \r
156 \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\\r
157 $SeqN(slot_s = \tuple{s, sv})=s$ \\\r
158 $SlotVal(slot_s = \tuple{s, sv})=sv$ \\\r
159 \r
160 \noindent\fbox{%\r
161 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
162 \textbf{Get Slot:}\\\r
163 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
164 \begin{algorithmic}[1]\r
165 \Function{GetSlot}{$s_g$}\r
166 \State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}\r
167 \EndFunction\r
168 \end{algorithmic}\end{varwidth}% \r
169 }\r
170 \r
171 \r
172 \noindent\fbox{%\r
173 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
174 \textbf{Get Slot:}\\\r
175 Puts a slot in the server memory if the slot has the correct sequence number.  Also resizes the server memory if needed.\\\r
176 \begin{algorithmic}[1]\r
177 \Function{PutSlot}{$s_p,sv_p,max'$}\r
178 \If{$(max' \neq \emptyset)$}  \Comment{Resize}\r
179 \State $max \gets max'$\r
180 \EndIf\r
181 \State $\tuple{s_n,sv_n} \gets MaxSlot(SL)$\Comment{Last sv}\r
182 %\State $s_n \gets SeqN(\tuple{s_n,sv_n})$\r
183 \If{$(s_p = s_n + 1)$}\r
184     \If{$n = max$}\r
185         \State $\tuple{s_m,sv_m} \gets MinSlot(SL)$\Comment{First sv}\r
186         \State $SL \gets SL - \{\tuple{s_m,sv_m}\}$\r
187     \Else \Comment{$n < max$}\r
188         \State $n \gets n + 1$\r
189     \EndIf\r
190     \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$\r
191     \State \Return{$(true,\emptyset)$}\r
192 \Else\r
193     \State \Return{$(false,\{\tuple{s,sv}\in SL \mid \r
194     s \geq s_p\})$}\r
195 \EndIf\r
196 \EndFunction\r
197 \end{algorithmic}\end{varwidth}% \r
198 }\r
199 \r
200 \r
201 \r
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
203 \section{\textbf{Client}}\r
204 \r
205 \subsection{\textbf{Client Notation Conventions}}\r
206 $k$ is key of entry \\\r
207 $v$ is value of entry \\\r
208 $size$ is a size (target size of the current block chain) \\\r
209 $kv$ is a key-value pair $\tuple{k,v}$ \\\r
210 $KV$ is a set of $kv$ \\\r
211 $id$ is a machine ID \\\r
212 $seq$ is a sequence number \\\r
213 $hmac_p$ is the HMAC value of the previous slot \\\r
214 $hmac_c$ is the HMAC value of the current slot \\\r
215 $Guard$ is a set of$ \tuple{k,v,$logical operator$}$ which can be evaluated to a boolean \\\r
216 \r
217 $trans$ is a transaction entry , $\tuple{seq, id, KV, Guard}$  \\\r
218 $lastmsg$ is a last message entry, $\tuple{seq, id}$ \\\r
219 $qstate$ is a queue state entry, $\tuple{size}$ \\\r
220 $colres$ is a collision resolution entry, $\tuple{id, seq_{old}, seq_{new}, true \lor false}$ \\\r
221 $newkey$ is a new key entry, $\tuple{k, id}$, $id$ is ID of arbitrator \\\r
222 $commit$ is a commit transaction entry, $\tuple{seq_{trans},KV}$, id is id of arbitrator \\\r
223 $abort$ is an abort transaction entry, $\tuple{seq_{trans},id_{trans}}$ \\\r
224 \r
225 \r
226 $de$ is a data entry that can one of: $trans$, $lastmsg$, $qstate$, $colres$, $newkey$, $commit$, $abort$ \\\r
227 $DE$ is a set of all data entries, possibly of different types, in a single message, set of $de$\\\r
228 \r
229 $slotDat = \tuple{seq,id,DE,hmac_p,hmac_c}$ \\\r
230 $slot = \tuple{seq, Encrpt(slotDat)}$\\\r
231 \r
232 \subsection{\textbf{Client State}}\r
233 \r
234 \subsubsection{Constants}\r
235 $LOCAL\_ID$ = machine ID of this client.\\\r
236 $RESIZE\_THRESH\_PERCENT$ = percent of slots that need to have live data to trigger a resize.\\\r
237 $RESIZE\_PERCENT$ = percent that we should grow the block chain to.\\\r
238 $DATA\_ENTRY\_SET\_MAX\_SIZE$ = max size that a data entry set can have (in bytes).\\\r
239 $DEAD\_SLOT\_COUNT$ = number of slots to keep dead if possible at the end of the block chain.\\\r
240 $MAX\_RESCUE\_SKIPS$ = number of skips that are allowed when saving data entries.\\\r
241 \r
242 \subsubsection{Primitive Variables}\r
243 $max\_size$ = max size of the block chain\\\r
244 \r
245 \subsubsection{Sets and Lists}\r
246 \r
247 $PendingTransQueue$ = Queue of pending transactions that need to be pushed to the block chain, $\tuple{PendingTrans}$\\\r
248 $PendingTrans= \tuple{KV, Guard} = \tuple{$set of key value pairs, set of guard conditions$}$.\\\r
249 $Arbitrator$ = set of $\tuple{k,id}$ containing the key and its arbitrating device.\\\r
250 $LastSlot$ = set of $\tuple{id, seq}$ containing the machine ID and the largest sequence number from that machine ID.\\\r
251 $LocalSlots$ = set of slots that are in the clients local buffer (initially $\emptyset$), data is decrypted.\\\r
252 $RejectedSlotList$ = ordered list of the sequence numbers of slots that this client tried to insert but were rejected.\\\r
253 $CommittedKV$ = set of committed key value pairs (initially $\emptyset$).\\\r
254 $SpeculatedKV$ = set of speculated key value pairs (initially $\emptyset$).\r
255 \r
256 \subsection{Helper Functions}\r
257 The following helper functions are needed:\\\r
258 \r
259 $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
260 $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
261 \r
262 \r
263 % Get Size\r
264 \noindent\fbox{%\r
265 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
266 \textbf{Get Byte Size:}\\\r
267 Get the size in bytes of the thing that is passed in.\\\r
268 \begin{algorithmic}[1]\r
269 \Function{GetSize}{$a$}\r
270     \State \Return{Size in bytes of $a$}\r
271 \EndFunction\r
272 \end{algorithmic}\end{varwidth}% \r
273 }\r
274 \r
275 % Error\r
276 \noindent\fbox{%\r
277 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
278 \textbf{ Error:}\\\r
279 Prints an error message and halts the execution of the client.\\\r
280 \begin{algorithmic}[1]\r
281 \Function{Error}{$msg$}\r
282     \State $Print(msg)$\r
283     \State $Halt()$\r
284 \EndFunction\r
285 \end{algorithmic}\end{varwidth}% \r
286 }\r
287 \r
288 % Get Next Sequence Number\r
289 \noindent\fbox{%\r
290 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
291 \textbf{Get Next Sequence Number:}\\\r
292 Get the next sequence number for insertion into the block chain.\\\r
293 \begin{algorithmic}[1]\r
294 \Function{GetNextSeq}{$k$}\r
295     \LeftComment{Get the largest known sequence number}\r
296     \State $seq_{ret} \gets seq$ such that $\tuple{id, seq}\in LastSlo \land (\forall \tuple{id', seq'} \in LastSlo, seq \geq seq')$\\\r
297     \r
298     \LeftComment{Add one to the largest seq number to generate the new seq number}\r
299     \State \Return{$seq_{ret} + 1$}\r
300 \EndFunction\r
301 \end{algorithmic}\r
302 \end{varwidth}% \r
303 }\r
304 \r
305 % Get Arbitrator\r
306 \noindent\fbox{%\r
307 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
308 \textbf{Get Arbitrator:}\\\r
309 Get the arbitrator for a given key.\\\r
310 \begin{algorithmic}[1]\r
311 \Function{GetArbitrator}{$k$}\r
312     \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
313     \State \Return{$id_1$}\r
314 \EndFunction\r
315 \end{algorithmic}\r
316 \end{varwidth}% \r
317 }\r
318 \r
319 % Get Arbitrator KV\r
320 \noindent\fbox{%\r
321 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
322 \textbf{Get Arbitrator for KV Set:}\\\r
323 Get the arbitrator for a given key value set.\\\r
324 \begin{algorithmic}[1]\r
325 \Function{GetArbitratorKV}{$KV$}\r
326     \State $\tuple{k,v} \gets \tuple{k',v'}$ such that $\tuple{k',v'} \in KV$\r
327     \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
328     \State \Return{$id_1$}\r
329 \EndFunction\r
330 \end{algorithmic}\r
331 \end{varwidth}% \r
332 }\r
333 \r
334 % Check Transaction arbitrator\r
335 \noindent\fbox{%\r
336 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
337 \textbf{Check Arbitrator for a Transaction:}\\\r
338 Check that the arbitrators for a given set are all the same arbitrator.\\\r
339 \begin{algorithmic}[1]\r
340 \Function{CheckArbitrator}{$PendingTrans_a$}\r
341     \State $id_{arb} \gets NULL$\\\r
342     \State $\tuple{KV_a, Guard_a} \gets PendingTrans_a$\r
343     \r
344     \ForAll{$\tuple{k',v'} \in KV_a$}\r
345         \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
346         \r
347         \If{$id_{arb} = NULL$}  \r
348             \State $id_{arb} \gets id'$\r
349         \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
350             \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
351         \EndIf\r
352     \EndFor\r
353     \r
354     \ForAll{$\tuple{k',v', lop'} \in Guard_a$}\r
355         \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
356         \r
357         \If{$id_{arb} = NULL$}  \r
358             \State $id_{arb} \gets id'$\r
359         \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
360             \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
361         \EndIf\r
362     \EndFor\r
363 \EndFunction\r
364 \end{algorithmic}\r
365 \end{varwidth}% \r
366 }\r
367 \r
368 % Get all Commits\r
369 \noindent\fbox{%\r
370 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
371 \textbf{Get all Commits:}\\\r
372 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
373 \begin{algorithmic}[1]\r
374 \Function{GetCommits}{$ $}\r
375     \State $ComSet \gets \emptyset$ \Comment{Set of the commits}\\\r
376         \r
377     \LeftComment{Iterate over all the slots saved locally}\r
378     \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
379         \State $ComSet \gets ComSet \cup \{c |c \in DE',c$is a $commit\}$\r
380     \EndFor\r
381     \State \Return{$ComSet$}\r
382 \EndFunction\r
383 \end{algorithmic}\r
384 \end{varwidth}% \r
385 }\r
386 \r
387 % Get all Transactions\r
388 \noindent\fbox{%\r
389 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
390 \textbf{Get all Transactions:}\\\r
391 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
392 \begin{algorithmic}[1]\r
393 \Function{GetTrans}{$ $}\r
394     \State $TransSet \gets \emptyset$ \Comment{Set of the trans}\\\r
395         \r
396     \LeftComment{Iterate over all the slots saved locally}\r
397     \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
398         \State $TransSet \gets TransSet \cup \{c |c \in DE',c$is a $trans\}$\r
399     \EndFor\r
400     \State \Return{$TransSet$}\r
401 \EndFunction\r
402 \end{algorithmic}\r
403 \end{varwidth}% \r
404 }\r
405 \r
406 % Get all aborts\r
407 \noindent\fbox{%\r
408 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
409 \textbf{Get all aborts:}\\\r
410 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
411 \begin{algorithmic}[1]\r
412 \Function{GetAborts}{$ $}\r
413     \State $AbrtSet \gets \emptyset$ \Comment{Set of the aborts}\\\r
414         \r
415     \LeftComment{Iterate over all the slots saved locally}\r
416     \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
417         \State $AbrtSet \gets AbrtSet \cup \{c |c \in DE',c$is a $abort\}$\r
418     \EndFor\r
419     \State \Return{$AbrtSet$}\r
420 \EndFunction\r
421 \end{algorithmic}\r
422 \end{varwidth}% \r
423 }\r
424 \r
425 % Get all Queue States\r
426 \noindent\fbox{%\r
427 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
428 \textbf{Get all queue states:}\\\r
429 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
430 \begin{algorithmic}[1]\r
431 \Function{GetQStates}{$ $}\r
432     \State $QSet \gets \emptyset$ \Comment{Set of the qstates}\\\r
433         \r
434     \LeftComment{Iterate over all the slots saved locally}\r
435     \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
436         \State $QSet \gets QSet \cup \{c |c \in DE',c$is a $qstate\}$\r
437     \EndFor\r
438     \State \Return{$QSet$}\r
439 \EndFunction\r
440 \end{algorithmic}\r
441 \end{varwidth}% \r
442 }\r
443 \r
444 % Get all Last Messages States\r
445 \noindent\fbox{%\r
446 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
447 \textbf{Get all last message data entrues:}\\\r
448 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
449 \begin{algorithmic}[1]\r
450 \Function{GetLastMsg}{$ $}\r
451     \State $LMSet \gets \emptyset$ \Comment{Set of the last msg}\\\r
452         \r
453     \LeftComment{Iterate over all the slots saved locally}\r
454     \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
455         \State $LMSet \gets LMSet \cup \{c |c \in DE',c$is a $lastmsg\}$\r
456     \EndFor\r
457     \State \Return{$LMSet$}\r
458 \EndFunction\r
459 \end{algorithmic}\r
460 \end{varwidth}% \r
461 }\r
462 \r
463 % Check Queue State Live\r
464 \noindent\fbox{%\r
465 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
466 \textbf{Check Queue State Live:}\\\r
467 A queue state is dead if there is another queue state data entry that has a larger queue state.\\\r
468 \begin{algorithmic}[1]\r
469 \Function{CheckQStateLive}{$qstate_a$}\r
470     \State $\tuple{size_a} \gets qstate_a$\r
471     \State $AllQStates \gets$ \Call{GetQState}{} \Comment{Get all the qstates} \\\r
472     \r
473     \If{$\exists \tuple{size'} \in AllQStates, size' > size_a$}\r
474         \State \Return{false}\r
475     \EndIf\r
476     \State \Return{true}\r
477     \r
478 \EndFunction\r
479 \end{algorithmic}\r
480 \end{varwidth}% \r
481 }\r
482 \r
483 % Check Commit Live\r
484 \noindent\fbox{%\r
485 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
486 \textbf{Check Commit Live:}\\\r
487 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
488 \begin{algorithmic}[1]\r
489 \Function{CheckCommitLive}{$commit_a$}\r
490     \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
491     \State $KSet \gets \{k|\tuple{k,v} \in KV\}$\r
492     \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits} \\\r
493     \r
494     \LeftComment{Iterate all commits that are newer in time}\r
495     \ForAll{$\tuple{seq_{trans}',KV'}\in AllCommits, seq_{trans}' > seq_{a_{trans}}$}\r
496         \State $KVSet \gets KVSet \setminus \{k|\tuple{k,v} \in KV'\}$\\\r
497         \r
498         \If{$KVSet = \emptyset$}\r
499             \State \Return{false} \Comment{All keys have a newer commit}\r
500         \EndIf\r
501     \EndFor\r
502     \State \Return{true} \Comment{If got here then some keys still live}\r
503 \EndFunction\r
504 \end{algorithmic}\r
505 \end{varwidth}% \r
506 }\r
507 \r
508 % Check Last Message Live\r
509 \noindent\fbox{%\r
510 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
511 \textbf{Check Last Message Live:}\\\r
512 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
513 \begin{algorithmic}[1]\r
514 \Function{CheckLastMsgLive}{$lastmsg_a$}\r
515     \State $\tuple{seq_a, id_a} \gets lastmsg_a$\\\r
516     \r
517     \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
518         \State \Return{false}\r
519     \EndIf\r
520     \State \Return{True}    \r
521 \EndFunction\r
522 \end{algorithmic}\r
523 \end{varwidth}% \r
524 }\r
525 \r
526 %Check Collision Resolution Live\r
527 \noindent\fbox{%\r
528 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
529 \textbf{Check Collision Resolution Live:}\\\r
530 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
531 \begin{algorithmic}[1]\r
532 \Function{CheckColResLive}{$colres_a$}\r
533     \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, equal_a} \gets colres_a$\\\r
534     \r
535     \If{$\forall \tuple{id', seq'} \in LastSlot, seq' \geq seq_{a_{new}}$}\r
536         \State \Return{false}\r
537     \EndIf\r
538     \State \Return{true}\r
539 \EndFunction\r
540 \end{algorithmic}\r
541 \end{varwidth}% \r
542 }\r
543 \r
544 % Check New Key Live\r
545 \noindent\fbox{%\r
546 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
547 \textbf{Check New Key Live:}\\\r
548 A new key data entry is always live.\\\r
549 \begin{algorithmic}[1]\r
550 \Function{CheckNewkeyLive}{$newkey_a$}\r
551     \State \Return{True}    \r
552 \EndFunction\r
553 \end{algorithmic}\r
554 \end{varwidth}% \r
555 }\r
556 \r
557 % Check Abort Live\r
558 \noindent\fbox{%\r
559 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
560 \textbf{Check Abort Live:}\\\r
561 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
562 \begin{algorithmic}[1]\r
563 \Function{CheckAbortLive}{$abort_a, seq_a$}\r
564     \State $\tuple{seq_{a_{trans}},id_a} \gets abort_a$\\\r
565     \r
566     \LeftComment{The device whos transaction was aborted saw the abort}\r
567     \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
568         \State \Return{false}\r
569     \EndIf\r
570     \State \Return{True}    \r
571 \EndFunction\r
572 \end{algorithmic}\r
573 \end{varwidth}% \r
574 }\r
575 \r
576 % Check Transaction Live\r
577 \noindent\fbox{%\r
578 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
579 \textbf{Check Transaction Live:}\\\r
580 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
581 \begin{algorithmic}[1]\r
582 \Function{CheckTransLive}{$trans_a$}\r
583     \State $\tuple{seq_a, id_a, KV_a, Guard_a} \gets trans_a$\r
584     \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits}\r
585     \State $AllAborts \gets$ \Call{GetAborts}{} \Comment{Get all the aborts} \\\r
586     \r
587     \If{$\exists \tuple{seq_{abrt}',seq_{trans}',id'} \in AllAborts, seq_{trans}' = seq_a$}\r
588         \State \Return{false}\r
589     \ElsIf{$\exists \tuple{seq_{trans}',KV'} \in AllCommits, seq_{trans}' \geq seq_a$}\r
590         \State \Return{false}\r
591     \EndIf\r
592     \State \Return{true}\r
593 \EndFunction\r
594 \end{algorithmic}\r
595 \end{varwidth}% \r
596 }\r
597 \r
598 % Check Live\r
599 \noindent\fbox{%\r
600 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
601 \textbf{Check Live:}\\\r
602 Checks if a data entry is live based on its type.\\\r
603 \begin{algorithmic}[1]\r
604 \Function{CheckLive}{$datentry, seq$}\r
605     \If{$datentry$ is a $commit$}\r
606         \State \Return{\Call{CheckCommitLive}{$datentry$}}\\r
607     \ElsIf{$datentry$ is a $abort$}\r
608         \State \Return{\Call{CheckAbortLive}{$datentry, seq$}}\\r
609     \ElsIf{$datentry$ is a $trans$}\r
610         \State \Return{\Call{CheckTransLive}{$datentry$}}\\r
611     \ElsIf{$datentry$ is a $lastmsg$}\r
612         \State \Return{\Call{CheckLastMsgLive}{$datentry$}}\\r
613     \ElsIf{$datentry$ is a $colres$}\r
614         \State \Return{\Call{CheckColResLive}{$datentry$}}\\r
615     \ElsIf{$datentry$ is a $qstate$}\r
616         \State \Return{\Call{CheckQStateLive}{$datentry$}}\r
617     \ElsIf{$datentry$ is a $newkey$}\r
618         \State \Return{\Call{CheckNewkeyLive}{$datentry$}}\r
619     \Else\r
620         \State \Call{Error}{"Unknown data entry type."}\r
621     \EndIf\r
622 \EndFunction\r
623 \end{algorithmic}\r
624 \end{varwidth}% \r
625 }\r
626 \r
627 % Slot Has Live\r
628 \noindent\fbox{%\r
629 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
630 \textbf{Slot Has Live:}\\\r
631 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
632 \begin{algorithmic}[1]\r
633 \Function{SlotHasLive}{$slot_a$}\r
634     \State $\tuple{s_1, \tuple{seq_2,id,DE,hmac_p,hmac_c}} \in LocalSlots$\r
635     \r
636     \ForAll{$datentry \in DE$}\r
637         \If{\Call{CheckLive}{$datentry, s_1$}} \Comment{an entry is alive}\r
638             \State \Return{true}\r
639         \EndIf\r
640     \EndFor\r
641     \r
642     \State \Return{false} \Comment{All entries were dead}    \r
643 \EndFunction\r
644 \end{algorithmic}\r
645 \end{varwidth}% \r
646 }\r
647 \r
648 % Calculate Resize Threshold\r
649 \noindent\fbox{%\r
650 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
651 \textbf{Calculate Resize Threshold:}\\\r
652 Calculate a threshold for how many slots need to have live data entries in them for a resize to take place.\\\r
653 \begin{algorithmic}[1]\r
654 \Function{CalcResizeThresh}{$maxsize$}\r
655     \State \Return{$\left \lfloor {maxsize * RESIZE\_THRESH\_PERCENT} \right \rfloor$}\r
656 \EndFunction\r
657 \end{algorithmic}\r
658 \end{varwidth}% \r
659 }\r
660 \r
661 % Calculate Block Chain New Size\r
662 \noindent\fbox{%\r
663 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
664 \textbf{Calculate Block Chain New Size:}\\\r
665 Calculate the new size of the block chain which we need if we are to resize the data structure.\\\r
666 \begin{algorithmic}[1]\r
667 \Function{CalcNewSize}{$maxsize$}\r
668     \State \Return{$\left \lceil {maxsize * RESIZE\_THRESH\_PERCENT} \right \rceil$}\r
669 \EndFunction\r
670 \end{algorithmic}\r
671 \end{varwidth}% \r
672 }\r
673 \r
674 % Should Resize\r
675 \noindent\fbox{%\r
676 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
677 \textbf{Should Resize:}\\\r
678 Check if the block should resize based on some metric of how many slots in the block chain are filled with live data. \\\r
679 \begin{algorithmic}[1]\r
680 \Function{ShouldResize}{$ $}\r
681     \State $LiveSlots \gets \{slot_s|slot_s \in LocalSlots \land $\Call{SlotHasLive}{$slot_s$}$\}$\r
682     \State $resizethreshold \gets $ \Call{CalcResizeThresh}{$max\_size$}\r
683     \State \Return{$|LiveSlots| \geq resizethreshold$} \Comment{If passes threshold then resize}\r
684 \EndFunction\r
685 \end{algorithmic}\r
686 \end{varwidth}% \r
687 }\r
688 \r
689 % Create Queue State \r
690 \noindent\fbox{%\r
691 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
692 \textbf{Create Queue State:}\\\r
693 Generate a queue state data entry.\\\r
694 \begin{algorithmic}[1]\r
695 \Function{CreateQState}{$size_a$}\r
696     \State \Return{$\tuple{size_a}$}    \r
697 \EndFunction\r
698 \end{algorithmic}\r
699 \end{varwidth}% \r
700 }\r
701 \r
702 % Create Abort\r
703 \noindent\fbox{%\r
704 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
705 \textbf{Create Abort:}\\\r
706 Generate a abort data entry.\\\r
707 \begin{algorithmic}[1]\r
708 \Function{CreateAbort}{$seq_a, id_a$}\r
709     \State \Return{$\tuple{seq_a, id_a}$}\r
710 \EndFunction\r
711 \end{algorithmic}\r
712 \end{varwidth}% \r
713 }\r
714 \r
715 % Create Collision\r
716 \noindent\fbox{%\r
717 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
718 \textbf{Create ColRes:}\\\r
719 Generate a colres data entry.\\\r
720 \begin{algorithmic}[1]\r
721 \Function{CreateColRes}{$is_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a$}\r
722     \State \Return{$\tuple{id_a, seq_{a_{old}}, seq_{a_{new}},}isequal_a$}\r
723 \EndFunction\r
724 \end{algorithmic}\r
725 \end{varwidth}% \r
726 }\r
727 \r
728 \r
729 % Create Transaction\r
730 \noindent\fbox{%\r
731 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
732 \textbf{Create Transaction:}\\\r
733 Generate a transaction data entry.\\\r
734 \begin{algorithmic}[1]\r
735 \Function{CreateTrans}{$pendingtrans_a, seq_a$}\r
736     \State $\tuple{KV_a, Guard_a} \gets pendingtrans_a$\r
737     \State \Return{$\tuple{seq_a, LOCAL\_ID, KV_a, Guard_a}$}    \r
738 \EndFunction\r
739 \end{algorithmic}\r
740 \end{varwidth}% \r
741 }\r
742 \r
743 % Create Commit\r
744 \noindent\fbox{%\r
745 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
746 \textbf{Create Commit:}\\\r
747 Generate a commit data entry.\\\r
748 \begin{algorithmic}[1]\r
749 \Function{CreateCommit}{$seq_a,KV_a$}\r
750     \State \Return{$\tuple{seq_a,KV_a}$}\r
751 \EndFunction\r
752 \end{algorithmic}\r
753 \end{varwidth}% \r
754 }\r
755 \r
756 % Create New Key\r
757 \noindent\fbox{%\r
758 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
759 \textbf{Create New Key:}\\\r
760 Generate a new key data entry.\\\r
761 \begin{algorithmic}[1]\r
762 \Function{CreateNewKey}{$k_a, id_a$}\r
763     \State \Return{$\tuple{k_a,id_a}$}\r
764 \EndFunction\r
765 \end{algorithmic}\r
766 \end{varwidth}% \r
767 }\r
768 \r
769 % Data Entry Set Has Space \r
770 \noindent\fbox{%\r
771 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
772 \textbf{Data Entry Set Has Space :}\\\r
773 Checks if a data entry set has enough space for a new data entry to be inserted.\\\r
774 \begin{algorithmic}[1]\r
775 \Function{DEHasSpace}{$DE_a, de_a$}\r
776     \State $newsize \gets $ \Call{GetSize}{$DE_a$}\r
777     \State $newsize \gets newsize +$ \Call{GetSize}{$de_a$}\r
778     \State \Return{$newsize \leq DATA\_ENTRY\_SET\_MAX\_SIZE$}\r
779 \EndFunction\r
780 \end{algorithmic}\r
781 \end{varwidth}% \r
782 }\r
783 \r
784 % Create Rescued Commit\r
785 \noindent\fbox{%\r
786 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
787 \textbf{Create Rescued Date Entry:}\\\r
788 For commits only the key-value pairs that are most recent (no newer commit that has those key values in it).\\\r
789 \begin{algorithmic}[1]\r
790 \Function{CreateRescuedCommit}{$commit_a$}\r
791     \State $AllCommits \gets $ \Call{GetCommits}{}\r
792     \State $\tuple{seq_{a_{trans}},KV_a} \gets de_a$\r
793     \State $NewKV \gets KV_a$\\\r
794 \r
795     \LeftComment{Get rid of all key values that have newer commits}\r
796     \ForAll{$\tuple{k_a, v_a} \in KV_a$}\r
797         \LeftComment{Iterate over all commits that are newer than the rescue commit}\r
798         \ForAll{$\tuple{seq', KV'} \in AllCommits, seq' > seq_{a_{trans}}$}\r
799             \If{$\exists \tuple{k', v'} \in KV', k' = k_a$}\r
800                 \State $NewKV \gets NewKV \setminus \tuple{k_a, v_a}$\r
801                 \State Break\r
802             \EndIf\r
803         \EndFor\r
804     \EndFor\r
805     \State \Return{$\tuple{seq_{a_{trans}}, NewKV}$}\r
806 \EndFunction\r
807 \end{algorithmic}\r
808 \end{varwidth}% \r
809 }\r
810 \r
811 % Create Rescued Date Entry\r
812 \noindent\fbox{%\r
813 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
814 \textbf{Create Rescued Date Entry:}\\\r
815 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
816 \begin{algorithmic}[1]\r
817 \Function{CreateRescuedEntry}{$de_a$}\r
818 \r
819     \If{$de_a$is a $commit$}\r
820         \State \Return{\Call{CreateRescuedCommit}{$de_a$}}\r
821     \EndIf\r
822     \r
823     \State \Return{$de_a$} \Comment{No Modification needed}\r
824 \EndFunction\r
825 \end{algorithmic}\r
826 \end{varwidth}% \r
827 }\r
828 \r
829 % Check Slot Hmacs\r
830 \noindent\fbox{%\r
831 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
832 \textbf{Check Slot HMACs:}\\\r
833 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
834 \begin{algorithmic}[1]\r
835 \Function{CheckSlotsHmacAndSeq}{$Slots_a$}\r
836     \ForAll{$slot_a \in Slots_a$}\r
837         \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
838         \State $calchmac \gets $ \Call{GenerateHmac}{$seq_{a_2}, id_a, DE_a, hmac_{a_p}$}\r
839     \r
840         \If{$seq_{a_1} \neq seq_{a_2}$}\r
841           \State \Call{Error}{"Slot sequence number mismatch"}\r
842         \ElsIf{$calchmac \neq hmac_{a_c}$}\r
843             \State \Call{Error}{"Slot HMAC mismatch"}\r
844         \EndIf\r
845     \EndFor\r
846 \EndFunction\r
847 \end{algorithmic}\r
848 \end{varwidth}% \r
849 }\r
850 \r
851 % Check HMAC Chain\r
852 \noindent\fbox{%\r
853 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
854 \textbf{Check HMAC Chain:}\\\r
855 Check that the HMAC chain has not been violated.\\\r
856 \begin{algorithmic}[1]\r
857 \Function{CheckHmacChain}{$Slots_a$}\r
858     \State $SlotsList \gets Slots_a$ sorted by sequence number\\  \r
859     \r
860     \r
861     \LeftComment{Check all new slots}\r
862     \ForAll{$index \in [2: |SlotsList|]$}\r
863         \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[i-1]$\r
864         \State $\tuple{seq_{b_1}, \tuple{seq_{b_2},id_b,DE_b,hmac_{b_p},hmac_{b_c}}} \gets SlotList[i]$\r
865         \r
866         \If{$hmac_{b_p} \neq hmac_{b_c}$}\r
867             \State \Call{Error}{"Invalid previous HMAC."}\r
868         \EndIf    \r
869     \EndFor\\\r
870     \r
871     \LeftComment{Check against slots that we already have in the block chain}\r
872     \If{$|LocalSlots| \neq 0$}\r
873         \State $\tuple{seq, SDE} \gets $\Call{MaxSlot}{$LocalSlots$}\r
874         \State $\tuple{seq{last_2},id_{last},DE_{last},hmac_{last_p},hmac_{last_c}} \gets SDE$\\\r
875         \r
876         \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[1]$\\\r
877 \r
878         \If{$(seq_{last_2} + 1) = seq_{a_1}$}\r
879             \If{$hmac_{a_p} \neq hmac_{last_c}$}\r
880                 \State \Call{Error}{"Invalid previous HMAC."}\r
881             \EndIf\r
882         \EndIf\r
883     \EndIf\r
884     \r
885 \EndFunction\r
886 \end{algorithmic}\r
887 \end{varwidth}% \r
888 }\r
889 \r
890 \r
891 % Check For Old Slots\r
892 \noindent\fbox{%\r
893 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
894 \textbf{Check For Old Slots:}\\\r
895 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
896 \begin{algorithmic}[1]\r
897 \Function{CheckOldSlots}{$Slots_a$}\r
898     \State $\tuple{seq_{new}, Dat_{new}} \gets$ \Call{MinSlot }{$Slots_a$} \Comment{Get the oldest new slot}\r
899     \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot }{$LocalSlots$} \Comment{Get the newest slot seen}\\\r
900     \r
901     \If{$seq_{new} \leq seq_{local}$} \Comment{The slots were not newer than what was already seen}\r
902         \State \Call{Error}{"Server sent old slots."}\r
903     \EndIf\\\r
904     \r
905     \LeftComment{Check if slots have the same sequence number but different data entries}\r
906     \ForAll{$\tuple{seq, Dat} \in Slots_a$}\r
907         \If{$\exists \tuple{seq', Dat'} \in (LocalSlots \cup Slots_a), seq'=seq \land Dat' \neq Dat$}\r
908             \State \Call{Error}{"Slot sequence number match but data does not"}\r
909         \EndIf\r
910     \EndFor\r
911     \r
912 \EndFunction\r
913 \end{algorithmic}\r
914 \end{varwidth}% \r
915 }\r
916 \r
917 % Get All Queue States with Sequence numbers\r
918 % \noindent\fbox{%\r
919 % \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
920 % \textbf{Get All Queue States with Sequence numbers:}\\\r
921 % Gets all the queue states with the sequence number of the slot that the queue state was inside.\r
922 % \begin{algorithmic}[1]\r
923 % \Function{GetQStateWithSeq}{$Slots_a$}\r
924 %     \State $QSet \gets \emptyset$\\\r
925     \r
926 %     \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
927 %         \ForAll{$de' \in DE'$}\r
928 %             \If{$de'$ is a  $qstate$}\r
929 %                 \State $QSet \gets QSet \cup \{\tuple{seq_1', de'}\}$\r
930 %             \EndIf\r
931 %         \EndFor\r
932 %     \EndFor\\\r
933     \r
934 %     \State \Return{$QSet$}\r
935 % \EndFunction\r
936 % \end{algorithmic}\r
937 % \end{varwidth}% \r
938 % }\r
939 \r
940 % Get All Queue States\r
941 \noindent\fbox{%\r
942 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
943 \textbf{Get All Queue States:}\\\r
944 Gets all the queue states from the slots that were passed in.\\\r
945 \begin{algorithmic}[1]\r
946 \Function{GetQState}{$Slots_a$}\r
947     \State $QSet \gets \emptyset$\\\r
948     \r
949     \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
950         \ForAll{$de' \in DE'$}\r
951             \If{$de'$ is a  $qstate$}\r
952                 \State $QSet \gets QSet \cup \{de'\}$\r
953             \EndIf\r
954         \EndFor\r
955     \EndFor\\\r
956     \r
957     \State \Return{$QSet$}\r
958 \EndFunction\r
959 \end{algorithmic}\r
960 \end{varwidth}% \r
961 }\r
962 \r
963 \r
964 % Check Size With Gap\r
965 \noindent\fbox{%\r
966 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
967 \textbf{Check Size With Gap:}\\\r
968 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
969 \begin{algorithmic}[1]\r
970 \Function{CheckSizeWithGap}{$Slots_a$}    \r
971     %\State $QSSet \gets $ \Call{GetQStateWithSeq}{$Slots_a$}\r
972     %\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
973     %\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
974     \r
975     \State $QSet \gets $ \Call{GetQState}{$Slots_a$}\r
976     \State $size_{max} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \geq size'$ \r
977     \State $size_{min} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \leq size'$     \r
978     \State $Slots_{oldmax} \gets \emptyset$\\\r
979 \r
980     \r
981     \LeftComment{If only 1 max size then we must have all the slots for that size}\r
982     \If{$(|QSSet| = 1) \land (|Slots_a| \neq size_{max})$}\r
983         \State \Call{Error}{"Missing Slots"}\r
984     \EndIf\\\r
985     \r
986     \LeftComment{We definitely have all the slots}\r
987     \If$|Slots_a| = size_{max}$\r
988         \State \Return{} \Comment{We have all the slots}\r
989     \EndIf\\\r
990     \r
991     \LeftComment{We must have at least this many slots}\r
992     \If$|Slots_a| < size_{min}$\r
993         \State \Call{Error}{"Missing Slots"}\r
994     \EndIf\\\r
995 \r
996 \EndFunction\r
997 \end{algorithmic}\r
998 \end{varwidth}% \r
999 }\r
1000 \r
1001 % Check Size\r
1002 \noindent\fbox{%\r
1003 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1004 \textbf{Check Size:}\\\r
1005 \begin{algorithmic}[1]\r
1006 \Function{CheckSize}{$Slots_a$}    \r
1007     \State $\tuple{seq_{old_{max}}, Dat_{old_{max}}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
1008     \State $\tuple{seq_{new_{max}}, Dat_{new_{max}}} \gets $ \Call{MinSlot}{$Slots_a$}\\\r
1009     \r
1010     \If{$(seq_{old_{max}} + 1) = seq_{new_{max}}$}\r
1011         \LeftComment{No Gap so cannot say anything about the size}\r
1012         \State \Return{} \r
1013     \Else \r
1014         \LeftComment{Has a gap so we need to do checks}\r
1015         \State \Call{CheckSizeWithGap}{$Slots_a$}\r
1016     \EndIf\r
1017 \EndFunction\r
1018 \end{algorithmic}\r
1019 \end{varwidth}% \r
1020 }\r
1021 \r
1022 \r
1023 % % Initialize the expected size of the block chain\r
1024 % \noindent\fbox{%\r
1025 % \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1026 % \textbf{Initialize the expected size of the block chain:}\\\r
1027 % Initialize the expected size of the block chain based on the size at the server.\\\r
1028 % \begin{algorithmic}[1]\r
1029 % \Function{InitExpSize}{$seq_a$}\r
1030 %     \State $startingsize \gets 0$\\\r
1031 \r
1032 %     \If{$seq_a < max\_size$} \Comment{Check whether saves slots are full on server}\r
1033 %         \State $startingsize \gets seq_a$\r
1034 %     \Else\r
1035 %         \State $startingsize \gets max\_size$\r
1036 %     \EndIf\\\r
1037     \r
1038 %     \State \Return{$startingsize$}\r
1039 % \EndFunction\r
1040 % \end{algorithmic}\r
1041 % \end{varwidth}% \r
1042 % }\r
1043 \r
1044 % % Update the expected size of the block chain\r
1045 % \noindent\fbox{%\r
1046 % \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1047 % \textbf{Update the expected size of the block chain:}\\\r
1048 % Update the expected size of the block chain.\\\r
1049 % \begin{algorithmic}[1]\r
1050 % \Function{UpdateExpSize}{$size_a$}\r
1051 %     \State $size_a \gets size_a + 1$\\\r
1052     \r
1053 %     \If{$size_a > max\_size$}\Comment{Expected size $\leq max\_size$}\r
1054 %         \State $ssize_a \gets max_\_size$\r
1055 %     \EndIf\\\r
1056     \r
1057 %     \State \Return{$size_a$}\r
1058 % \EndFunction\r
1059 % \end{algorithmic}\r
1060 % \end{varwidth}% \r
1061 % }\r
1062 \r
1063 \r
1064 \r
1065 % Update Last Message\r
1066 \noindent\fbox{%\r
1067 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1068 \textbf{Process Commit Data Entry:}\\\r
1069 Process a commit entry.  Updates the local copy of commits.\\\r
1070 \begin{algorithmic}[1]\r
1071 \Function{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, updateinglocal_a$}\r
1072     \State $\tuple{id_{old}, seq_{old}} \gets \tuple{id', seq'}$ such that $\tuple{id', seq'} \in LastSlot \land id'=id$\\\r
1073     \r
1074     \If{$id_a = LOCAL\_ID$}\r
1075         \If{$\lnot updateinglocal_a \land (seq_a \neq seq_{old})$}\r
1076             \LeftComment{This client did not make any updates so its latest sequence number should not change}\r
1077             \State \Call{Error}{"Mismatch on local machine sequence number"}\r
1078         \EndIf\r
1079     \Else\r
1080         \If{$seq_{old} > seq_a$}\r
1081             \State \Call{Error}{"Rollback on remote machine sequence number"}\r
1082         \EndIf\r
1083     \EndIf\\\r
1084     \r
1085     \State $LastSlot \gets LastSlot \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LastSlot, id=id_a\}$\r
1086     \State $LastSlot \gets LastSlot \cup \{\tuple{id_a, seq_a}\}$\r
1087     \r
1088     \State \Return{$LstSlt_a \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LstSlt_a, id=id_a\}$}\r
1089 \EndFunction\r
1090 \end{algorithmic}\r
1091 \end{varwidth}% \r
1092 }\r
1093 \r
1094 % Process Commit Data Entry\r
1095 \noindent\fbox{%\r
1096 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1097 \textbf{Process Commit Data Entry:}\\\r
1098 Process a commit entry.  Updates the local copy of commits.\\\r
1099 \begin{algorithmic}[1]\r
1100 \Function{ProcessCommit}{$commit_a$}\r
1101     \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
1102     \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CommittedKV \land \tuple{k',v'}\in KV_a \land k'=k\}$\r
1103     \State $CommittedKV \gets (CommittedKV \setminus DKV) \cup KV_a$\r
1104 \EndFunction\r
1105 \end{algorithmic}\r
1106 \end{varwidth}% \r
1107 }\r
1108 \r
1109 % Process Queue State Data Entry\r
1110 \noindent\fbox{%\r
1111 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1112 \textbf{Process Queue State Entry:}\\\r
1113 Process a queue state entry. Updates the max size of the block chain\\\r
1114 \begin{algorithmic}[1]\r
1115 \Function{ProcessQState}{$qstate_a$}\r
1116     \State $\tuple{size_a} \gets qstate_a$\r
1117     \State $max\_size \gets size_a$ \Comment{Update the max size we can have}\r
1118 \EndFunction\r
1119 \end{algorithmic}\r
1120 \end{varwidth}% \r
1121 }\r
1122 \r
1123 % Process Collision Resolution Entry\r
1124 \noindent\fbox{%\r
1125 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1126 \textbf{Process Queue State Entry:}\\\r
1127 Process a collision resolution entry.\\\r
1128 \begin{algorithmic}[1]\r
1129 \Function{ProcessColres}{$colres_a, NewSlots_a$}\r
1130     \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a}$\r
1131     \State $AllSlots \gets LocalSlots \cup NewSlots_a$\r
1132     \State $index \gets seq_{a_{old}}$\\\r
1133     \r
1134     \While{$index <= seq_{a_{new}}$}\r
1135         \State $slt \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
1136         \r
1137         \If{$\exists \tuple{seq' Dat'} \in AllSlots, seq' = index$}\r
1138             \State $\tuple{seq, Dat} \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
1139             \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets Dat$\r
1140             \If{$isequal_a \neq (id=id_a)$}\r
1141                 \State \Call{Error}{"Trying to insert rejected messages for slot"}\r
1142             \EndIf\r
1143         \EndIf\\\r
1144         \State $index \gets index + 1$\r
1145     \EndWhile\r
1146     \r
1147     \r
1148 \EndFunction\r
1149 \end{algorithmic}\r
1150 \end{varwidth}% \r
1151 }\r
1152 \r
1153 % Process New Key Data Entry\r
1154 \noindent\fbox{%\r
1155 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1156 \textbf{Process New Key Entry:}\\\r
1157 Process a queue state entry. Adds a key to the key arbitrator set\\\r
1158 \begin{algorithmic}[1]\r
1159 \Function{ProcessNewkey}{$newkey_a$}\r
1160     \State $\tuple{seq_a, k_a, id_a} \gets newkey_a$\r
1161     \State $Arbitrator \gets Arbitrator \cup \{\tuple{k_a,id_a}\}$\r
1162 \EndFunction\r
1163 \end{algorithmic}\r
1164 \end{varwidth}% \r
1165 }\r
1166 \r
1167 % Process Process Data Entry\r
1168 \noindent\fbox{%\r
1169 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1170 \textbf{Process Data Entry:}\\\r
1171 Process the data entry based on what kind of entry it is.\\\r
1172 \begin{algorithmic}[1]\r
1173 \Function{ProcessDatEntry}{$slot_a, NewSlots_a,LstSlt_a$}\r
1174     \If{$datentry_a$ is a $commit$}\r
1175         \State \Call{ProcessCommit}{$dataentry_a$}\r
1176         \r
1177     \ElsIf{$datentry_a$ is a $abort$}\r
1178         \LeftComment{Do Nothing in this case}\r
1179         \r
1180     \ElsIf{$datentry_a$ is a $trans$}\r
1181         \LeftComment{Do Nothing in this case}\r
1182     \r
1183     \ElsIf{$datentry_a$ is a $lastmsg$}\r
1184         \State $\tuple{seq_a, id_a} \gets dataentry_a$\r
1185         \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, false$}\r
1186     \r
1187     \ElsIf{$datentry_a$ is a $colres$}\r
1188         \State \Call{ProcessColres}{$dataentry_a, NewSlots_a$}\r
1189         \r
1190     \ElsIf{$datentry_a$ is a $qstate$}\r
1191         \State \Call{ProcessQState}{$dataentry_a$}\r
1192         \r
1193     \ElsIf{$datentry_a$ is a $newkey$}\r
1194         \State \Call{ProcessNewkey}{$dataentry_a$}\r
1195         \r
1196     \Else\r
1197         \State \Call{Error}{"Unknown data entry type."}\r
1198     \EndIf\r
1199     \r
1200     \State \Return{$LstSlt_a$}    \r
1201 \EndFunction\r
1202 \end{algorithmic}\r
1203 \end{varwidth}% \r
1204 }\r
1205 \r
1206 \r
1207 % Delete Local Slots\r
1208 \noindent\fbox{%\r
1209 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1210 \textbf{Delete Local Slots:}\\\r
1211 Deletes local slots that are deleted at the server.  This keeps the size of the local block chain bounded.\\\r
1212 \begin{algorithmic}[1]\r
1213 \Function{DeleteLocalSlots}{$ $}\r
1214     \State $\tuple{seq_{max}, Dat_{max}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
1215     \State $seq_{min} \gets seq_{max} - max\_size$ \Comment{Min sequence number we should keep}\r
1216     \State $LSDelete \gets \emptyset$\r
1217         \r
1218     \If{$|LocalSlots| \leq max\_size$}\r
1219         \State \Return{} \Comment{Nothing to delete}\r
1220     \EndIf\\\r
1221     \r
1222     \State $LSDelete \gets \{\tuple{seq', Dat'}|\tuple{seq', Dat'} \in LocalSlots, seq' > seq_{min}\}$\r
1223     \State $LocalSlots \gets LocalSlots \setminus LSDelete$    \r
1224 \EndFunction\r
1225 \end{algorithmic}\r
1226 \end{varwidth}% \r
1227 }\r
1228 \r
1229 % Create Speculative KV\r
1230 \noindent\fbox{%\r
1231 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1232 \textbf{Create Speculative KV:}\\\r
1233 Speculates on what the most recent key value pairs will be based on the latest committed key value pairs and the uncommitted transactions.\\\r
1234 \begin{algorithmic}[1]\r
1235 \Function{SpeculateKV}{$ $}\r
1236     \State $AllTrans \gets$ \Call{GetTrans}{}\r
1237     \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
1238     \State $CurrKV \gets CommittedKV$\r
1239     \State $DKV \gets \emptyset$\r
1240 \r
1241     \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
1242         \If{\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
1243             \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
1244             \State $CurrKV \gets (CurrKV \setminus DKV) \cup KV_t$\r
1245         \EndIf\r
1246     \EndFor\r
1247     \r
1248     \State \Return{$CurrKV$}\r
1249 \EndFunction\r
1250 \end{algorithmic}\r
1251 \end{varwidth}% \r
1252 }\r
1253 \r
1254 \r
1255 % Validate and Update \r
1256 \noindent\fbox{%\r
1257 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1258 \textbf{Validate Update:}\\\r
1259 Validate the block chain and insert into the local block chain.\\\r
1260 \begin{algorithmic}[1]\r
1261 \Function{ValidateUpdate}{$NewSlots_a, updatinglocal_a$}\r
1262     \State $\tuple{seq_{oldest}, Dat_{oldest}} \gets$ \Call{MinSlot}{$NewSlots_a$}\r
1263     \State $\tuple{seq_{newest}, Dat_{newest}} \gets$ \Call{MaxSlot}{$NewSlots_a$}\r
1264     \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot}{$LocalSlots$}\r
1265     \State $LastSlotTmp \gets LastSlot$\\\r
1266     %\State $currsize \gets $\Call{InitExpSize}{$seq_{oldest}$}\\\r
1267     \r
1268     \State \Call{CheckSlotsHmacAndSeq}{$NewSlots_a$} \Comment{Check all the HMACs}\r
1269     \State \Call{CheckHmacChain}{$NewSlots_a$} \Comment{Check HMAC Chain} \r
1270     \State \Call{CheckOldSlots}{$NewSlots_a$} \Comment{Check if new slots are actually old slots} \r
1271     \State \Call{CheckSize}{$NewSlots_a$} \Comment{Check if the size is correct}\\\r
1272     \r
1273     \ForAll{$slot_a \in NewSlots_a$ in order of sequence number}\r
1274         \If{$slot_a \in LocalSlots$} \Comment{Client already has this slot}\r
1275             \State $NewSlots_a \gets NewSlots_a \setminus \{slot_a\}$\r
1276             \State Continue\r
1277         \EndIf\\\r
1278     \r
1279         \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
1280         \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_{a_1}, id_a, LstSlt_a, updatinglocal_a$}\\\r
1281         \r
1282         \ForAll{$de_a \in DE_a$} \Comment{Process each data entry}\r
1283             \State $LstSlt_a \gets $ \Call{ProccessDatEntry}{$de_a, NewSlots_a,LstSlt_a$}\r
1284         \EndFor\\\r
1285     \r
1286         %\State $currsize \gets $ \Call{UpdateExpSize}{$currsize$}\\\r
1287         \State $LocalSlots \gets LocalSlots \cup \{slot_a\}$ \Comment{Add to local Chain}\r
1288     \EndFor\\\r
1289     \r
1290     \If{$seq_{oldest} > (seq_{local} +1) \land LastSlotTmp \neq \emptyset$}\r
1291         \LeftComment{There was a gap so there should be a complete set of information on each previously seen client}\r
1292         \State \Call{Error}{"Missing records for machines"}\r
1293     \EndIf\\\r
1294     \r
1295     \State \Call{DeleteLocalSlots}{ } \Comment{Delete old slots from local}\r
1296     \State $SpeculatedKV \gets $\Call{SpeculateKV}{ } \Comment{Speculate on what will be latest KV set}\r
1297     \r
1298 \EndFunction\r
1299 \end{algorithmic}\r
1300 \end{varwidth}% \r
1301 }\r
1302 \r
1303 % Decrypt Validate Insert Slots\r
1304 \noindent\fbox{%\r
1305 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1306 \textbf{Decrypt Validate Insert Slots:}\\\r
1307 Decrypts slots, validates (checks for malicious activity) slots and inserts the slots into the local block chain.\\\r
1308 \begin{algorithmic}[1]\r
1309 \Function{DecryptValidateInsert}{$NewSlots_a, updatinglocal_a$}\r
1310     \State $DecryptedSlots \gets \emptyset$\r
1311     \State $DDat \gets NULL$\\\r
1312     \r
1313     \ForAll{$\tuple{seq', EDat'} \in NewSlots_a$}\r
1314         \State $DDat \gets $ \Call{Decrypt}{$EDat'$}\r
1315         \State $DecryptedSlots \gets DecryptedSlots \cup \tuple{seq',DDat}$\r
1316     \EndFor\\\r
1317     \State \Call{ValidateUpdate}{$DecryptedSlots, updatinglocal_a$}\r
1318 \EndFunction\r
1319 \end{algorithmic}\r
1320 \end{varwidth}% \r
1321 }\r
1322 \r
1323 % Check and Create Last Message Data Entry\r
1324 \noindent\fbox{%\r
1325 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1326 \textbf{Check and Create Last Message Data Entry:}\\\r
1327 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
1328 \begin{algorithmic}[1]\r
1329 \Function{CheckCreateLastMsgEntry}{$seq_a, id_a$}\r
1330     \State $AllLastMsg \gets$ \Call{GetLastMsg}{}\\\r
1331     \r
1332     \LeftComment{Already Has one}\r
1333     \If{$\exists  \tuple{seq', id'} \in AllLastMsg, id_a=id' \land seq'=seq_a$}\r
1334         \State \Return{$\{\}$}\\\r
1335     \EndIf\\\r
1336     \r
1337     \LeftComment{Not latest slot from that client}\r
1338     \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
1339         \State \Return{$\{\}$}\\\r
1340     \EndIf\\\r
1341     \r
1342     \r
1343     \State \Return{$\{\tuple{seq_a, id_a}\}$}\r
1344 \EndFunction\r
1345 \end{algorithmic}\r
1346 \end{varwidth}% \r
1347 }\r
1348 \r
1349 % Mandatory Rescue\r
1350 \noindent\fbox{%\r
1351 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1352 \textbf{Mandatory Rescue:}\\\r
1353 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
1354 \begin{algorithmic}[1]\r
1355 \Function{MandatoryRescue}{$DE_a$}\r
1356     \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
1357     \State $cseq \gets smallestseq$\\\r
1358     \r
1359     \LeftComment{Check the least slots to rescue and live entries}\r
1360     \While{$cseq < (smallestseq + DEAD\_SLOT\_COUNT)$}\r
1361         \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
1362         \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\r
1363         \State $DE' \gets DE' \cup$ \Call{CheckCreateLastMsgEntry}{$seq', id'$} \Comment{Get the last message too if we need it}\\\r
1364         \r
1365         \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
1366             \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
1367                 \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\r
1368                 \If{\Call{DEHasSpace}{$DE_a, de$}}\r
1369                     \State $DE_a \gets DE_a \cup de$ \Comment{Had enough space to add it}\r
1370                 \ElsIf{$currentseq = smallestseq$}\r
1371                     \State \Return{$NULL$}\r
1372                 \Else\r
1373                     \State \Return{$DE_a$}\r
1374                 \EndIf\r
1375             \EndIf\r
1376         \EndFor\\\r
1377         \r
1378         \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
1379     \EndWhile\r
1380     \r
1381     \State \Return{$DE_a$}\r
1382 \EndFunction\r
1383 \end{algorithmic}\r
1384 \end{varwidth}% \r
1385 }\r
1386 \r
1387 % Optional Rescue\r
1388 \noindent\fbox{%\r
1389 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1390 \textbf{Optional Rescue:}\\\r
1391 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
1392 \begin{algorithmic}[1]\r
1393 \Function{OptionalRescue}{$DE_a$}\r
1394     \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
1395     \State $largestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \geq seq')$\r
1396 \r
1397     \State $numofskips \gets 0$\r
1398     \State $cseq \gets smallestseq$\\\r
1399     \r
1400     \LeftComment{Check the least slots to rescue and live entries}\r
1401     \While{$cseq < largestseq$}\r
1402         \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
1403         \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\\\r
1404         \r
1405         \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
1406             \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
1407                 \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\\\r
1408                 \r
1409                 \If{$de \in DE_a$} \Comment{Already being rescued}\r
1410                     \State Continue\r
1411                 \EndIf\\\r
1412                 \r
1413                 \If{\Call{DEHasSpace}{$DE_a, de$}}\r
1414                     \State $DE_a \gets DE_a \cup de$ \Comment{Had enoug space to add it}\r
1415                 \ElsIf{$numofskips \geq MAX\_RESCUE\_SKIPS$}\r
1416                     \State \Return{$DE_a$}\r
1417                 \Else\r
1418                     $numofskips \gets numofskips +1$\r
1419                 \EndIf\r
1420             \EndIf\r
1421         \EndFor\\\r
1422         \r
1423         \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
1424     \EndWhile\r
1425     \r
1426     \State \Return{$DE_a$}\r
1427 \EndFunction\r
1428 \end{algorithmic}\r
1429 \end{varwidth}% \r
1430 }\r
1431 \r
1432 \r
1433 % Rejected Messages\r
1434 \noindent\fbox{%\r
1435 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1436 \textbf{Rejected Messages:}\\\r
1437 \begin{algorithmic}[1]\r
1438 \Function{RejectedMessages}{$DE_a$}\r
1439     \State $seq_{old} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \geq seq'$\r
1440     \State $prev \gets -1$\\\r
1441     \r
1442     \r
1443     \r
1444     \If{$|RejectedSlotList| \geq REJECTED\_THRESH$}\r
1445         \State $seq_{new} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \leq seq'$\\\r
1446         \State $colres \gets $ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, seq_{new}, false$}    \r
1447         \State \Return{$DE_a \cup \{colres\}$}\r
1448     \EndIf\\\r
1449     \r
1450     \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
1451         \If{$\exists \tuple{seq',Dat'} \in LocalSlots$}\r
1452             \State Break\r
1453         \EndIf\r
1454         \State $prev \gets seq$\r
1455     \EndFor\\\r
1456     \r
1457     \If{$prev \neq -1$}\r
1458         \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, prev, false$}\r
1459     \EndIf\\\r
1460     \r
1461     \State $RejectedSlotList \gets \{\tuple{seq}| \tuple{seq} \in RejectedSlotList, seq > prev\}$\\\r
1462     \r
1463     \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
1464         \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq,seq, false$}\r
1465     \EndFor\\\r
1466     \r
1467     \State \Return{$DE_a$}\r
1468 \EndFunction\r
1469 \end{algorithmic}\r
1470 \end{varwidth}% \r
1471 }\r
1472 \r
1473 \r
1474 \r
1475 % Arbitrate\r
1476 \noindent\fbox{%\r
1477 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1478 \textbf{Arbitrate:}\\\r
1479 \begin{algorithmic}[1]\r
1480 \Function{Arbitrate}{$DE_a$}\r
1481     \State $AllCommits \gets$ \Call{GetCommits}{}\r
1482     \State $AllTrans \gets$ \Call{GetTrans}{}\r
1483     \State $LiveCommits \gets \{c| c\in AllCommits, $\Call{CheckCommitLive}{$c$}$\}$\r
1484     \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
1485     \State $KV \gets \emptyset$\r
1486     \State $lastcomseq \gets -1$\r
1487     \State $CurrKV \gets \emptyset$\r
1488     \State $DKV \gets \emptyset$\r
1489     \State $KVTmp \gets \emptyset$\\\r
1490     \r
1491     \LeftComment{Get all the latest commits}\r
1492     \ForAll{$\tuple{seq_{trans}',KV'} \in LiveCommits$}\r
1493         \State $CurrKV \gets CurrKV \cup KV'$\r
1494     \EndFor\\\r
1495     \r
1496     \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
1497         \If{\Call{GetArbitratorKV}{$KV_t$} $\neq LOCAL\_ID$}\r
1498             \State Continue \Comment{Client not arbitrator for this transaction}\r
1499         \EndIf\\\r
1500     \r
1501         \If{$\lnot$\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
1502             \State $abortde \gets $\Call{CreateAbort}{$seq_t, id_t$}\r
1503             \LeftComment{No more space so we cant arbitrate any further}\r
1504             \If($lnot$\Call{DeHasSpace}{$DE_a, abortde$})\r
1505                 \State \Return{$DE_a$}\r
1506             \EndIf\r
1507             \State $DE_a \gets DE_a \cup abortde$\r
1508         \Else\r
1509             \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in KV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
1510             \State $KVTmp \gets (KV \setminus DKV) \cup KV'$\r
1511             \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KVTmp \land k'=k\}$\r
1512             \State $CurrKV \gets (CurrKV \setminus DKV) \cup KVTmp$\r
1513             \State $commitde \gets $ \Call{CreateCommit}{$seq_t,KVTmp$}\r
1514             \r
1515             \If{$\lnot$ \Call{DeHasSpace}{$DE_a, commitde$}}\r
1516                 \If{$lastcomseq \neq -1$}\r
1517                     \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
1518                 \EndIf\r
1519                 \State \Return{$DE_a$}\r
1520             \Else\r
1521                 \State $KV \gets KVTmp$\r
1522                 \State $lastcomseq \gets seq_t$\r
1523             \EndIf\r
1524         \EndIf\r
1525     \EndFor\r
1526     \r
1527     \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
1528     \State \Return{$DE_a$}\r
1529 \r
1530     \r
1531 \EndFunction\r
1532 \end{algorithmic}\r
1533 \end{varwidth}% \r
1534 }\r
1535 \r
1536 % Create New Slot\r
1537 \noindent\fbox{%\r
1538 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1539 \textbf{Create New Slot:}\\\r
1540 Create a slot and encrypt it.\\\r
1541 \begin{algorithmic}[1]\r
1542 \Function{CreateNewSlot}{$seq_a, DE_a$}\r
1543     \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
1544     \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets SDE$\\\r
1545     \r
1546     \State $newhmac \gets $ \Call{GenerateHmac}{$seq_a, LOCAL\_ID, DE_a, hmac_p$}\r
1547     \State $newSDE \gets \tuple{seq,LOCAL\_ID,DE_a,hmac_c,newhmac}$\r
1548     \State $encryptnewSDE \gets $\Call{Encrypt}{newSDE}\\\r
1549     \r
1550     \State \Return{$\tuple{seq_a, encryptnewSDE}$}\r
1551 \EndFunction\r
1552 \end{algorithmic}\r
1553 \end{varwidth}% \r
1554 }\r
1555 \r
1556 % Send Data to Server\r
1557 \noindent\fbox{%\r
1558 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1559 \textbf{Send Data to Server:}\\\r
1560 Send the data to the server.  If this fails then new slots will be returned by the server.\\\r
1561 \begin{algorithmic}[1]\r
1562 \Function{SendToServer}{$seq_a, DE_a, newsize_a$}\r
1563     \LeftComment{Make the slot and try to send to server}\r
1564     \State $newslot \gets $ \Call{CreateNewSlot}{$seq_a, DE_a$}\r
1565     \State $\tuple{success, newslots} \gets$ \Call{PutSlot}{$seq_a, newslot, newsize_a$}\\\r
1566     \r
1567     \If{$success$}\r
1568         \State $RejectedSlotList \gets \emptyset$\r
1569         \State \Return{$\tuple{true, \{newslot\}}$}\r
1570     \Else\r
1571         \If{$|newslots| = 0$}\r
1572             \State \Call{Error}{"Server rejected but did not send any slots"}\r
1573         \EndIf\r
1574         \State $RejectedSlotList \gets RejectedSlotList \cup \{seq_a\}$\r
1575         \State \Return{$\tuple{false, newslots}$}\r
1576     \EndIf\\\r
1577 \EndFunction\r
1578 \end{algorithmic}\r
1579 \end{varwidth}% \r
1580 }\r
1581 \r
1582 \r
1583 % Try Insert Transaction\r
1584 \noindent\fbox{%\r
1585 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1586 \textbf{Try Insert Transaction:}\\\r
1587 Try to insert a transaction into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
1588 \begin{algorithmic}[1]\r
1589 \Function{TryInsertTransaction}{$pendingtrans_a, forceresize$}\r
1590     \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
1591     \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
1592     \State $newsize \gets 0$\r
1593     \State $trans \gets$ \Call{CreateTrans}{$pendingtrans_a, seq$}\r
1594     \State $transinserted \gets false$\r
1595     \State $slotstoinsert \gets \emptyset$\\\r
1596     \r
1597     \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
1598     \State $resize \gets resize \lor forceresize$\r
1599     \If{$resize$}\r
1600         \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
1601         \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
1602     \EndIf\\\r
1603     \r
1604     \If{$RejectedSlotList \neq \emptyset$}   \r
1605         \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
1606     \EndIf\\\r
1607     \r
1608     \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
1609     \If{$DE = NULL$}\r
1610         \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
1611         \State \Return{\Call{TryInsertTransaction}{$trans_a, true$}}\r
1612     \EndIf\\\r
1613     \r
1614     \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
1615     \r
1616     \If{\Call{DEHasSpace}{$DE, trans$}} \Comment{transaction fits}\r
1617         \State $DE \gets DE \cup trans$\r
1618         \State $transinserted \gets true$\r
1619     \EndIf\\\r
1620     \r
1621     \LeftComment{Rescue data to fill slot data entry section}\r
1622     \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
1623     \r
1624     \LeftComment{Send to server.}\r
1625     \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
1626     \r
1627     \LeftComment{Insert the slots into the local bloakc chain}\r
1628     \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
1629     \r
1630     \State \Return{$transinserted \land success$} \Comment{Return if  succeeded or not}\r
1631     \r
1632 \EndFunction\r
1633 \end{algorithmic}\r
1634 \end{varwidth}% \r
1635 }\r
1636 \r
1637 \r
1638 % Try Insert New Key\r
1639 \noindent\fbox{%\r
1640 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1641 \textbf{Try Insert New Key:}\\\r
1642 Try to insert a new key into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
1643 \begin{algorithmic}[1]\r
1644 \Function{TryInsertNewKey}{$k_a, id_a, forceresize$}\r
1645     \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
1646     \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
1647     \State $newsize \gets 0$\r
1648     \r
1649     \r
1650     \State $newkey \gets$ \Call{CreateNewKey}{$k_a, id_a$}\r
1651     \State $newkeyinserted \gets false$\r
1652     \State $slotstoinsert \gets \emptyset$\\\r
1653     \r
1654     \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
1655     \State $resize \gets resize \lor forceresize$\r
1656     \If{$resize$}\r
1657         \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
1658         \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
1659     \EndIf\\\r
1660     \r
1661     \If{$RejectedSlotList \neq \emptyset$}   \r
1662         \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
1663     \EndIf\\\r
1664     \r
1665     \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
1666     \If{$DE = NULL$}\r
1667         \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
1668         \State \Return{\Call{TryInsertNewKey}{$k_a, id_a, true$}}\r
1669     \EndIf\\\r
1670     \r
1671     \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
1672     \r
1673     \If{\Call{DEHasSpace}{$DE, newkey$}} \Comment{new key fits}\r
1674         \State $DE \gets DE \cup newkey$\r
1675         \State $newkeyinserted \gets true$\r
1676     \EndIf\\\r
1677     \r
1678     \LeftComment{Rescue data to fill slot data entry section}\r
1679     \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
1680     \r
1681     \LeftComment{Send to server.}\r
1682     \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
1683     \r
1684     \LeftComment{Insert the slots into the local block chain}\r
1685     \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
1686     \r
1687     \State \Return{$newkeyinserted \land success$} \Comment{Return if  succeeded or not}\r
1688     \r
1689 \EndFunction\r
1690 \end{algorithmic}\r
1691 \end{varwidth}% \r
1692 }\r
1693 \r
1694 \r
1695 \r
1696 \subsection{Client Interfaces}\r
1697 \r
1698 % Put KV pair\r
1699 \noindent\fbox{%\r
1700 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1701 \textbf{Put Key Value Pair:}\\\r
1702 Puts a key value pair into the key value pair buffer\\\r
1703 \begin{algorithmic}[1]\r
1704 \Function{PutKeyValue}{$k,v$}\r
1705     \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
1706     \r
1707     \LeftComment{Check if KV already has a key value pair for the specified key}\r
1708     \State $DSet \gets \{\tuple{k_1,v_1} | \tuple{k_1,v_1} \in KV \land k_1 = k\}$\\\r
1709     \r
1710     \If{$DSet \neq \emptyset$}\r
1711         \State \Call{Error}{"Value for key already in most recent update"}\r
1712     \EndIf\\\r
1713         \r
1714     \State $KV \gets KV \cup \{\tuple{k,v}\}$ \Comment{Add key value pair}\r
1715     \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
1716     \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
1717 \EndFunction\r
1718 \end{algorithmic}\r
1719 \end{varwidth}% \r
1720 }\r
1721 \r
1722 % Get KV Pair Speculative\r
1723 \noindent\fbox{%\r
1724 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1725 \textbf{Get KV Pair Speculative:}\\\r
1726 Get the value for the key while speculating.\\\r
1727 \begin{algorithmic}[1]\r
1728 \Function{GetValueSpeculate}{$k_a$}\r
1729     %\State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
1730     \r
1731     \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
1732 \State \Return{$v$}\r
1733 \EndFunction\r
1734 \end{algorithmic}\r
1735 \end{varwidth}% \r
1736 }\r
1737 \r
1738 % Update\r
1739 \noindent\fbox{%\r
1740 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1741 \textbf{Update}\\\r
1742 Sync with the server and get all the latest slots.\\\r
1743 \begin{algorithmic}[1]\r
1744 \Function{Update}{$ $}\r
1745     \State $\tuple{seq, Dat} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
1746     \State $NewSlots \gets$ \Call{GetSlots}{$seq$}\r
1747     \State \Call{DecryptValidateInsert}{$NewSlots, false$}\r
1748 \EndFunction\r
1749 \end{algorithmic}\r
1750 \end{varwidth}% \r
1751 }\r
1752 \r
1753 % Get KV Pair Committed\r
1754 \noindent\fbox{%\r
1755 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1756 \textbf{Get KV Pair Committed:}\\\r
1757 Get the value for the key which have been committed.\\\r
1758 \begin{algorithmic}[1]\r
1759 \Function{GetValueCommit}{$k_a$}\r
1760     \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in Committed \land k = k_a$\r
1761     \State \Return{$v$}\r
1762 \EndFunction\r
1763 \end{algorithmic}\r
1764 \end{varwidth}% \r
1765 }\r
1766 \r
1767 % Put guard condition\r
1768 \noindent\fbox{%\r
1769 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1770 \textbf{Put Guard:}\\\r
1771 Puts a guard transaction into the key value update.  A guard is a key value with a logical operator ($lop$).\\\r
1772 \begin{algorithmic}[1]\r
1773 \Function{PutGuard}{$k,v, lop$}\r
1774     \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
1775     \r
1776     \If{$\tuple{k,v, lop} \in Guard$}\r
1777         \State \Return{} \Comment{Already have guard condition in update}\r
1778     \EndIf\\\r
1779     \r
1780     \State $Guard \gets Guard \cup \{\tuple{k,v,lop}\}$\r
1781     \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
1782     \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
1783 \EndFunction    \r
1784 \end{algorithmic}\r
1785 \end{varwidth}% \r
1786 }\r
1787 \r
1788 % Transaction Start\r
1789 \noindent\fbox{%\r
1790 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1791 \textbf{ Transaction Start:}\\\r
1792 Starts a transaction.  Clears out the key value pair update buffer.\\\r
1793 \begin{algorithmic}[1]\r
1794 \Function{TransactionStart}{$ $}\r
1795     % \LeftComment{Reset the key value update buffer}\r
1796     % \State $KVUpdate \gets \tuple{\emptyset, \emptyset}$\r
1797     \State $PendingTrans \gets NULL$\r
1798 \EndFunction\r
1799 \end{algorithmic}\r
1800 \end{varwidth}% \r
1801 }\r
1802 \r
1803 % Transaction Commit\r
1804 \noindent\fbox{%\r
1805 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1806 \textbf{ Transaction Commit:}\\\r
1807 Commits the transaction into the block chain.  Keeps attempting to insert the transaction into the block chain until it succeeds.\\\r
1808 \begin{algorithmic}[1]\r
1809 \Function{Transaction Commit}{$ $}\r
1810     \State $DKV \gets \emptyset$\r
1811     \State $pt \gets NULL$\\\r
1812     \r
1813     \State $PendingTransQueue.$\Call{push}{$PendingTrans$}\\\r
1814         \r
1815     \While{\Call{HasConnectionToServer}{ } $\land PendingTransQueue \neq \emptyset$}\r
1816         \State $pt \gets PendingTransQueue.$\Call{peak}{ }\\\r
1817     \r
1818         \If{\Call{TryInsertTransaction}{$pt, false$}}\r
1819             \State $PendingTransQueue.$\Call{pop}{ }\r
1820         \EndIf\r
1821     \EndWhile\\    \r
1822     \r
1823     \LeftComment{Go Through local pending transactions and speculate}\r
1824     \ForAll{$\tuple{KV, Guard} \in PendingTransQueue$}\r
1825         \If{\Call{EvaluateGuard}{$Guard, SpeculatedKV$}}\r
1826             \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in SpeculatedKV \land \tuple{k',v'}\in KV \land k'=k\}$\r
1827             \State $SpeculatedKV \gets (SpeculatedKV \setminus DKV) \cup KV$\r
1828         \EndIf\r
1829     \EndFor\r
1830     \r
1831 \EndFunction\r
1832 \end{algorithmic}\r
1833 \end{varwidth}% \r
1834 }\r
1835 \r
1836 \r
1837 %Create New Key \r
1838 \noindent\fbox{%\r
1839 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
1840 \textbf{Create New Key:}\\\r
1841 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
1842 \begin{algorithmic}[1]\r
1843 \Function{Transaction Commit}{$k_a, id_a$}\r
1844     \State $success \gets false$\\\r
1845     \While{$\lnot success$}\r
1846         \If{$\exists \tuple{k',id'} \in Arbitrator, k' = k_a$}\r
1847             \State \Return{$false$} \Comment{Key already created}\r
1848         \EndIf\\\r
1849     \r
1850         \State $success \gets$ \Call{TryInsertNewKey}{$k_a, id_a$}\r
1851     \EndWhile\r
1852     \r
1853     \State \Return{$true$} \Comment{If got here then insertion was correct}\r
1854 \EndFunction\r
1855 \end{algorithmic}\r
1856 \end{varwidth}% \r
1857 }\r
1858 \r
1859 \r
1860 \end{document}\r