1 \documentclass[11pt]{article}
\r
2 \newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}
\r
6 \usepackage{graphicx}
\r
7 \usepackage{mathrsfs}
\r
9 \usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\r
10 \usepackage[all]{xy}
\r
11 \usepackage{varwidth}
\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
24 \setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text
\r
27 \section{\textbf{Introduction}}
\r
34 Each device has: user id + password
\r
37 hash1(user id), hash1(password)
\r
39 Symmetric Crypto keys is:
\r
40 hash2(user id | password)
\r
42 Server has finite length queue of entries + max\_entry\_identifier +
\r
45 \subsection{Entry layout}
\r
48 \item Sequence identifier
\r
49 \item Random IV (if needed by crypto algorithm)
\r
50 \item Encrypted payload
\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
60 \item HMAC of current slot
\r
63 A data entry can be one of these:
\r
65 \item A transaction:
\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
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
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
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
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
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
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
90 \subsection{Live status}
\r
92 Live status of entries:
\r
94 \item Transaction is live if it has not been committed or aborted yet.
\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
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
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
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
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
107 \item Collision resolution entry is dead if this entry has been seen by all clients after a collision happens.
\r
110 When data is at the end of the queue ready to expunge, if:
\r
112 \item If any entry is not dead it must be reinserted into the queue.
\r
114 \item If the slot sequence number is not dead, then a message sequence entry must be inserted.
\r
117 \paragraph{Validation procedure on client:}
\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
125 \item For all other machines, check that the latest sequence number is at least as large (never goes backwards).
\r
127 \item That the queue has a current queue state entry.
\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
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
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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
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
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
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
168 \end{algorithmic}\end{varwidth}%
\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
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
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
190 \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$
\r
191 \State \Return{$(true,\emptyset)$}
\r
193 \State \Return{$(false,\{\tuple{s,sv}\in SL \mid
\r
197 \end{algorithmic}\end{varwidth}%
\r
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\r
203 \section{\textbf{Client}}
\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
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
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
229 $slotDat = \tuple{seq,id,DE,hmac_p,hmac_c}$ \\
\r
230 $slot = \tuple{seq, Encrpt(slotDat)}$\\
\r
232 \subsection{\textbf{Client State}}
\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
242 \subsubsection{Primitive Variables}
\r
243 $max\_size$ = max size of the block chain\\
\r
245 \subsubsection{Sets and Lists}
\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
256 \subsection{Helper Functions}
\r
257 The following helper functions are needed:\\
\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
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
272 \end{algorithmic}\end{varwidth}%
\r
277 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\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
285 \end{algorithmic}\end{varwidth}%
\r
288 % Get Next Sequence Number
\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
298 \LeftComment{Add one to the largest seq number to generate the new seq number}
\r
299 \State \Return{$seq_{ret} + 1$}
\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
319 % Get Arbitrator KV
\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
334 % Check Transaction arbitrator
\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
344 \ForAll{$\tuple{k',v'} \in KV_a$}
\r
345 \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\
\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
354 \ForAll{$\tuple{k',v', lop'} \in Guard_a$}
\r
355 \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\
\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
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
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
381 \State \Return{$ComSet$}
\r
387 % Get all Transactions
\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
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
400 \State \Return{$TransSet$}
\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
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
419 \State \Return{$AbrtSet$}
\r
425 % Get all Queue States
\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
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
438 \State \Return{$QSet$}
\r
444 % Get all Last Messages States
\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
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
457 \State \Return{$LMSet$}
\r
463 % Check Queue State Live
\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
473 \If{$\exists \tuple{size'} \in AllQStates, size' > size_a$}
\r
474 \State \Return{false}
\r
476 \State \Return{true}
\r
483 % Check Commit Live
\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
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
498 \If{$KVSet = \emptyset$}
\r
499 \State \Return{false} \Comment{All keys have a newer commit}
\r
502 \State \Return{true} \Comment{If got here then some keys still live}
\r
508 % Check Last Message Live
\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
517 \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}
\r
518 \State \Return{false}
\r
520 \State \Return{True}
\r
526 %Check Collision Resolution Live
\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
535 \If{$\forall \tuple{id', seq'} \in LastSlot, seq' \geq seq_{a_{new}}$}
\r
536 \State \Return{false}
\r
538 \State \Return{true}
\r
544 % Check New Key Live
\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
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
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
570 \State \Return{True}
\r
576 % Check Transaction Live
\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
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
592 \State \Return{true}
\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
620 \State \Call{Error}{"Unknown data entry type."}
\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
636 \ForAll{$datentry \in DE$}
\r
637 \If{\Call{CheckLive}{$datentry, s_1$}} \Comment{an entry is alive}
\r
638 \State \Return{true}
\r
642 \State \Return{false} \Comment{All entries were dead}
\r
648 % Calculate Resize Threshold
\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
661 % Calculate Block Chain New Size
\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
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
689 % Create Queue State
\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
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
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
729 % Create Transaction
\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
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
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
769 % Data Entry Set Has Space
\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
784 % Create Rescued Commit
\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
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
805 \State \Return{$\tuple{seq_{a_{trans}}, NewKV}$}
\r
811 % Create Rescued Date Entry
\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
819 \If{$de_a$is a $commit$}
\r
820 \State \Return{\Call{CreateRescuedCommit}{$de_a$}}
\r
823 \State \Return{$de_a$} \Comment{No Modification needed}
\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
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
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
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
866 \If{$hmac_{b_p} \neq hmac_{b_c}$}
\r
867 \State \Call{Error}{"Invalid previous HMAC."}
\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
876 \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[1]$\\
\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
891 % Check For Old Slots
\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
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
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
917 % Get All Queue States with Sequence numbers
\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
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
934 % \State \Return{$QSet$}
\r
936 % \end{algorithmic}
\r
940 % Get All Queue States
\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
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
957 \State \Return{$QSet$}
\r
964 % Check Size With Gap
\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
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
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
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
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
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
1010 \If{$(seq_{old_{max}} + 1) = seq_{new_{max}}$}
\r
1011 \LeftComment{No Gap so cannot say anything about the size}
\r
1014 \LeftComment{Has a gap so we need to do checks}
\r
1015 \State \Call{CheckSizeWithGap}{$Slots_a$}
\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
1032 % \If{$seq_a < max\_size$} \Comment{Check whether saves slots are full on server}
\r
1033 % \State $startingsize \gets seq_a$
\r
1035 % \State $startingsize \gets max\_size$
\r
1038 % \State \Return{$startingsize$}
\r
1040 % \end{algorithmic}
\r
1041 % \end{varwidth}%
\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
1053 % \If{$size_a > max\_size$}\Comment{Expected size $\leq max\_size$}
\r
1054 % \State $ssize_a \gets max_\_size$
\r
1057 % \State \Return{$size_a$}
\r
1059 % \end{algorithmic}
\r
1060 % \end{varwidth}%
\r
1065 % Update Last Message
\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
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
1080 \If{$seq_{old} > seq_a$}
\r
1081 \State \Call{Error}{"Rollback on remote machine sequence number"}
\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
1088 \State \Return{$LstSlt_a \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LstSlt_a, id=id_a\}$}
\r
1094 % Process Commit Data Entry
\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
1109 % Process Queue State Data Entry
\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
1123 % Process Collision Resolution Entry
\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
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
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
1144 \State $index \gets index + 1$
\r
1153 % Process New Key Data Entry
\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
1167 % Process Process Data Entry
\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
1177 \ElsIf{$datentry_a$ is a $abort$}
\r
1178 \LeftComment{Do Nothing in this case}
\r
1180 \ElsIf{$datentry_a$ is a $trans$}
\r
1181 \LeftComment{Do Nothing in this case}
\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
1187 \ElsIf{$datentry_a$ is a $colres$}
\r
1188 \State \Call{ProcessColres}{$dataentry_a, NewSlots_a$}
\r
1190 \ElsIf{$datentry_a$ is a $qstate$}
\r
1191 \State \Call{ProcessQState}{$dataentry_a$}
\r
1193 \ElsIf{$datentry_a$ is a $newkey$}
\r
1194 \State \Call{ProcessNewkey}{$dataentry_a$}
\r
1197 \State \Call{Error}{"Unknown data entry type."}
\r
1200 \State \Return{$LstSlt_a$}
\r
1207 % Delete Local Slots
\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
1218 \If{$|LocalSlots| \leq max\_size$}
\r
1219 \State \Return{} \Comment{Nothing to delete}
\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
1229 % Create Speculative KV
\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
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
1248 \State \Return{$CurrKV$}
\r
1255 % Validate and Update
\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
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
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
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
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
1286 %\State $currsize \gets $ \Call{UpdateExpSize}{$currsize$}\\
\r
1287 \State $LocalSlots \gets LocalSlots \cup \{slot_a\}$ \Comment{Add to local Chain}
\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
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
1303 % Decrypt Validate Insert Slots
\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
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
1317 \State \Call{ValidateUpdate}{$DecryptedSlots, updatinglocal_a$}
\r
1323 % Check and Create Last Message Data Entry
\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
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
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
1343 \State \Return{$\{\tuple{seq_a, id_a}\}$}
\r
1349 % Mandatory Rescue
\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
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
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
1373 \State \Return{$DE_a$}
\r
1378 \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}
\r
1381 \State \Return{$DE_a$}
\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
1397 \State $numofskips \gets 0$
\r
1398 \State $cseq \gets smallestseq$\\
\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
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
1409 \If{$de \in DE_a$} \Comment{Already being rescued}
\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
1418 $numofskips \gets numofskips +1$
\r
1423 \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}
\r
1426 \State \Return{$DE_a$}
\r
1433 % Rejected Messages
\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
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
1450 \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}
\r
1451 \If{$\exists \tuple{seq',Dat'} \in LocalSlots$}
\r
1454 \State $prev \gets seq$
\r
1457 \If{$prev \neq -1$}
\r
1458 \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, prev, false$}
\r
1461 \State $RejectedSlotList \gets \{\tuple{seq}| \tuple{seq} \in RejectedSlotList, seq > prev\}$\\
\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
1467 \State \Return{$DE_a$}
\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
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
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
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
1507 \State $DE_a \gets DE_a \cup abortde$
\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
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
1519 \State \Return{$DE_a$}
\r
1521 \State $KV \gets KVTmp$
\r
1522 \State $lastcomseq \gets seq_t$
\r
1527 \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}
\r
1528 \State \Return{$DE_a$}
\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
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
1550 \State \Return{$\tuple{seq_a, encryptnewSDE}$}
\r
1556 % Send Data to Server
\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
1568 \State $RejectedSlotList \gets \emptyset$
\r
1569 \State \Return{$\tuple{true, \{newslot\}}$}
\r
1571 \If{$|newslots| = 0$}
\r
1572 \State \Call{Error}{"Server rejected but did not send any slots"}
\r
1574 \State $RejectedSlotList \gets RejectedSlotList \cup \{seq_a\}$
\r
1575 \State \Return{$\tuple{false, newslots}$}
\r
1583 % Try Insert Transaction
\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
1597 \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}
\r
1598 \State $resize \gets resize \lor forceresize$
\r
1600 \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}
\r
1601 \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$
\r
1604 \If{$RejectedSlotList \neq \emptyset$}
\r
1605 \State $DE \gets$ \Call{RejectedMessages}{$DE$}
\r
1608 \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}
\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
1614 \State $DE \gets $\Call{Arbitrate}{$DE$}\\
\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
1621 \LeftComment{Rescue data to fill slot data entry section}
\r
1622 \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\
\r
1624 \LeftComment{Send to server.}
\r
1625 \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\
\r
1627 \LeftComment{Insert the slots into the local bloakc chain}
\r
1628 \State \Call{DecryptValidateInsert}{$newslots, true$}\\
\r
1630 \State \Return{$transinserted \land success$} \Comment{Return if succeeded or not}
\r
1638 % Try Insert New Key
\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
1650 \State $newkey \gets$ \Call{CreateNewKey}{$k_a, id_a$}
\r
1651 \State $newkeyinserted \gets false$
\r
1652 \State $slotstoinsert \gets \emptyset$\\
\r
1654 \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}
\r
1655 \State $resize \gets resize \lor forceresize$
\r
1657 \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}
\r
1658 \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$
\r
1661 \If{$RejectedSlotList \neq \emptyset$}
\r
1662 \State $DE \gets$ \Call{RejectedMessages}{$DE$}
\r
1665 \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}
\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
1671 \State $DE \gets $\Call{Arbitrate}{$DE$}\\
\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
1678 \LeftComment{Rescue data to fill slot data entry section}
\r
1679 \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\
\r
1681 \LeftComment{Send to server.}
\r
1682 \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\
\r
1684 \LeftComment{Insert the slots into the local block chain}
\r
1685 \State \Call{DecryptValidateInsert}{$newslots, true$}\\
\r
1687 \State \Return{$newkeyinserted \land success$} \Comment{Return if succeeded or not}
\r
1696 \subsection{Client Interfaces}
\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
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
1710 \If{$DSet \neq \emptyset$}
\r
1711 \State \Call{Error}{"Value for key already in most recent update"}
\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
1722 % Get KV Pair Speculative
\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
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
1740 \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
\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
1753 % Get KV Pair Committed
\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
1767 % Put guard condition
\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
1776 \If{$\tuple{k,v, lop} \in Guard$}
\r
1777 \State \Return{} \Comment{Already have guard condition in update}
\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
1788 % Transaction Start
\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
1803 % Transaction Commit
\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
1813 \State $PendingTransQueue.$\Call{push}{$PendingTrans$}\\
\r
1815 \While{\Call{HasConnectionToServer}{ } $\land PendingTransQueue \neq \emptyset$}
\r
1816 \State $pt \gets PendingTransQueue.$\Call{peak}{ }\\
\r
1818 \If{\Call{TryInsertTransaction}{$pt, false$}}
\r
1819 \State $PendingTransQueue.$\Call{pop}{ }
\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
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
1850 \State $success \gets$ \Call{TryInsertNewKey}{$k_a, id_a, false$}
\r
1853 \State \Return{$true$} \Comment{If got here then insertion was correct}
\r