Block Chain Transactions, Commits multiple parts version
authorAli Younis <ayounis@uci.edu>
Sat, 7 Jan 2017 23:23:39 +0000 (15:23 -0800)
committerAli Younis <ayounis@uci.edu>
Sat, 7 Jan 2017 23:23:39 +0000 (15:23 -0800)
62 files changed:
version2/backup/doc/iotcloud.aux [new file with mode: 0644]
version2/backup/doc/iotcloud.log [new file with mode: 0644]
version2/backup/doc/iotcloud.pdf [new file with mode: 0644]
version2/backup/doc/iotcloud.tex [new file with mode: 0644]
version2/backup/doc/makefile [new file with mode: 0644]
version2/backup/src/java/.dir-locals.el [new file with mode: 0644]
version2/backup/src/java/iotcloud/Abort.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/CloudComm.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Commit.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Entry.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Guard.java_backup [new file with mode: 0644]
version2/backup/src/java/iotcloud/IoTString.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/KeyValue.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/LastMessage.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Liveness.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/LocalComm.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Makefile [new file with mode: 0644]
version2/backup/src/java/iotcloud/NewKey.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Pair.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/PendingTransaction.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/RejectedMessage.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/ServerException.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Slot.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/SlotBuffer.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/SlotIndexer.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Table.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/TableStatus.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Test.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/ThreeTuple.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/Transaction.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/TransactionStatus.java [new file with mode: 0644]
version2/backup/src/java/iotcloud/issues.txt [new file with mode: 0644]
version2/backup/src/script/C.cfg [new file with mode: 0644]
version2/backup/src/script/java.cfg [new file with mode: 0644]
version2/backup/src/script/makefile [new file with mode: 0644]
version2/backup/src/server/.dir-locals.el [new file with mode: 0644]
version2/backup/src/server/Makefile [new file with mode: 0644]
version2/backup/src/server/README.txt [new file with mode: 0644]
version2/backup/src/server/iotcloud.cpp [new file with mode: 0644]
version2/backup/src/server/iotquery.cpp [new file with mode: 0644]
version2/backup/src/server/iotquery.h [new file with mode: 0644]
version2/doc/iotcloud.tex [deleted file]
version2/doc/makefile [deleted file]
version2/src/java/iotcloud/Abort.java
version2/src/java/iotcloud/CloudComm.java
version2/src/java/iotcloud/Commit.java
version2/src/java/iotcloud/CommitPart.java [new file with mode: 0644]
version2/src/java/iotcloud/Entry.java
version2/src/java/iotcloud/LocalComm.java
version2/src/java/iotcloud/NewKey.java
version2/src/java/iotcloud/Pair.java
version2/src/java/iotcloud/PendingTransaction.java
version2/src/java/iotcloud/ServerException.java
version2/src/java/iotcloud/Table.java
version2/src/java/iotcloud/Test.java
version2/src/java/iotcloud/Transaction.java
version2/src/java/iotcloud/TransactionPart.java [new file with mode: 0644]
version2/src/java/iotcloud/TransactionStatus.java
version2/src/java/iotcloud/issues.txt [deleted file]
version2/src/script/C.cfg [deleted file]
version2/src/script/java.cfg [deleted file]
version2/src/script/makefile [deleted file]

diff --git a/version2/backup/doc/iotcloud.aux b/version2/backup/doc/iotcloud.aux
new file mode 100644 (file)
index 0000000..134fcf3
--- /dev/null
@@ -0,0 +1,18 @@
+\relax 
+\@writefile{toc}{\contentsline {section}{\numberline {1}\textbf  {Introduction}}{1}}
+\@writefile{toc}{\contentsline {section}{\numberline {2}Approach}{1}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {2.1}Keys}{1}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {2.2}Entry layout}{1}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {2.3}Live status}{3}}
+\@writefile{toc}{\contentsline {paragraph}{Validation procedure on client:}{4}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {2.4}Resizing Queue}{4}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {2.5}The Arbitrator}{4}}
+\@writefile{toc}{\contentsline {section}{\numberline {3}Server Algorithm}{4}}
+\@writefile{toc}{\contentsline {section}{\numberline {4}\textbf  {Client}}{6}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}\textbf  {Client Notation Conventions}}{6}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}\textbf  {Client State}}{7}}
+\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.2.1}Constants}{7}}
+\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.2.2}Primitive Variables}{7}}
+\@writefile{toc}{\contentsline {subsubsection}{\numberline {4.2.3}Sets and Lists}{7}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {4.3}Helper Functions}{8}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {4.4}Client Interfaces}{37}}
diff --git a/version2/backup/doc/iotcloud.log b/version2/backup/doc/iotcloud.log
new file mode 100644 (file)
index 0000000..f9f7b1e
--- /dev/null
@@ -0,0 +1,589 @@
+This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex 2015.5.24)  21 DEC 2016 15:40
+entering extended mode
+ restricted \write18 enabled.
+ %&-line parsing enabled.
+**iotcloud.tex
+(./iotcloud.tex
+LaTeX2e <2015/01/01>
+Babel <3.9l> and hyphenation patterns for 79 languages loaded.
+(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
+Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
+(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size11.clo
+File: size11.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
+)
+\c@part=\count79
+\c@section=\count80
+\c@subsection=\count81
+\c@subsubsection=\count82
+\c@paragraph=\count83
+\c@subparagraph=\count84
+\c@figure=\count85
+\c@table=\count86
+\abovecaptionskip=\skip41
+\belowcaptionskip=\skip42
+\bibindent=\dimen102
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/color.sty
+Package: color 2014/10/28 v1.1a Standard LaTeX Color (DPC)
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/color.cfg
+File: color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
+)
+Package color Info: Driver file: pdftex.def on input line 142.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/pdftex-def/pdftex.def
+File: pdftex.def 2011/05/27 v0.06d Graphics/color for pdfTeX
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/infwarerr.sty
+Package: infwarerr 2010/04/08 v1.3 Providing info/warning/error messages (HO)
+)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/ltxcmds.sty
+Package: ltxcmds 2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
+)
+\Gread@gobject=\count87
+))
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amscls/amsthm.sty
+Package: amsthm 2015/03/04 v2.20.2
+\thm@style=\toks14
+\thm@bodyfont=\toks15
+\thm@headfont=\toks16
+\thm@notefont=\toks17
+\thm@headpunct=\toks18
+\thm@preskip=\skip43
+\thm@postskip=\skip44
+\thm@headsep=\skip45
+\dth@everypar=\toks19
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsmath.sty
+Package: amsmath 2013/01/14 v2.14 AMS math features
+\@mathmargin=\skip46
+
+For additional information on amsmath, use the `?' option.
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amstext.sty
+Package: amstext 2000/06/29 v2.01
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsgen.sty
+File: amsgen.sty 1999/11/30 v2.0
+\@emptytoks=\toks20
+\ex@=\dimen103
+))
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsbsy.sty
+Package: amsbsy 1999/11/29 v1.2d
+\pmbraise@=\dimen104
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsopn.sty
+Package: amsopn 1999/12/14 v2.01 operator names
+)
+\inf@bad=\count88
+LaTeX Info: Redefining \frac on input line 210.
+\uproot@=\count89
+\leftroot@=\count90
+LaTeX Info: Redefining \overline on input line 306.
+\classnum@=\count91
+\DOTSCASE@=\count92
+LaTeX Info: Redefining \ldots on input line 378.
+LaTeX Info: Redefining \dots on input line 381.
+LaTeX Info: Redefining \cdots on input line 466.
+\Mathstrutbox@=\box26
+\strutbox@=\box27
+\big@size=\dimen105
+LaTeX Font Info:    Redeclaring font encoding OML on input line 566.
+LaTeX Font Info:    Redeclaring font encoding OMS on input line 567.
+\macc@depth=\count93
+\c@MaxMatrixCols=\count94
+\dotsspace@=\muskip10
+\c@parentequation=\count95
+\dspbrk@lvl=\count96
+\tag@help=\toks21
+\row@=\count97
+\column@=\count98
+\maxfields@=\count99
+\andhelp@=\toks22
+\eqnshift@=\dimen106
+\alignsep@=\dimen107
+\tagshift@=\dimen108
+\tagwidth@=\dimen109
+\totwidth@=\dimen110
+\lineht@=\dimen111
+\@envbody=\toks23
+\multlinegap=\skip47
+\multlinetaggap=\skip48
+\mathdisplay@stack=\toks24
+LaTeX Info: Redefining \[ on input line 2665.
+LaTeX Info: Redefining \] on input line 2666.
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphicx.sty
+Package: graphicx 2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty
+Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
+\KV@toks@=\toks25
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphics.sty
+Package: graphics 2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/trig.sty
+Package: trig 1999/03/16 v1.09 sin cos tan (DPC)
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/graphics.cfg
+File: graphics.cfg 2010/04/23 v1.9 graphics configuration of TeX Live
+)
+Package graphics Info: Driver file: pdftex.def on input line 94.
+)
+\Gin@req@height=\dimen112
+\Gin@req@width=\dimen113
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/jknapltx/mathrsfs.sty
+Package: mathrsfs 1996/01/01 Math RSFS package v1.0 (jk)
+\symrsfs=\mathgroup4
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/amssymb.sty
+Package: amssymb 2013/01/14 v3.01 AMS font symbols
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/amsfonts.sty
+Package: amsfonts 2013/01/14 v3.01 Basic AMSFonts support
+\symAMSa=\mathgroup5
+\symAMSb=\mathgroup6
+LaTeX Font Info:    Overwriting math alphabet `\mathfrak' in version `bold'
+(Font)                  U/euf/m/n --> U/euf/b/n on input line 106.
+))
+(/usr/local/texlive/2015/texmf-dist/tex/latex/algorithmicx/algpseudocode.sty
+Package: algpseudocode 
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ifthen.sty
+Package: ifthen 2014/09/29 v1.1c Standard LaTeX ifthen package (DPC)
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/algorithmicx/algorithmicx.sty
+Package: algorithmicx 2005/04/27 v1.2 Algorithmicx
+
+Document Style algorithmicx 1.2 - a greatly improved `algorithmic' style
+\c@ALG@line=\count100
+\c@ALG@rem=\count101
+\c@ALG@nested=\count102
+\ALG@tlm=\skip49
+\ALG@thistlm=\skip50
+\c@ALG@Lnr=\count103
+\c@ALG@blocknr=\count104
+\c@ALG@storecount=\count105
+\c@ALG@tmpcounter=\count106
+\ALG@tmplength=\skip51
+)
+Document Style - pseudocode environments for use with the `algorithmicx' style
+) (/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xy.sty
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xy.tex Bootstrap'ing:
+catcodes, docmode,
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyrecat.tex)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyidioms.tex)
+
+ Xy-pic version 3.8.9 <2013/10/06>
+ Copyright (c) 1991-2013 by Kristoffer H. Rose <krisrose@tug.org> and others
+ Xy-pic is free software: see the User's Guide for details.
+
+Loading kernel: messages; fonts; allocations: state,
+\X@c=\dimen114
+\Y@c=\dimen115
+\U@c=\dimen116
+\D@c=\dimen117
+\L@c=\dimen118
+\R@c=\dimen119
+\Edge@c=\toks26
+\X@p=\dimen120
+\Y@p=\dimen121
+\U@p=\dimen122
+\D@p=\dimen123
+\L@p=\dimen124
+\R@p=\dimen125
+\Edge@p=\toks27
+\X@origin=\dimen126
+\Y@origin=\dimen127
+\X@xbase=\dimen128
+\Y@xbase=\dimen129
+\X@ybase=\dimen130
+\Y@ybase=\dimen131
+\X@min=\dimen132
+\Y@min=\dimen133
+\X@max=\dimen134
+\Y@max=\dimen135
+\lastobjectbox@=\box28
+\zerodotbox@=\box29
+\almostz@=\dimen136
+ direction,
+\d@X=\dimen137
+\d@Y=\dimen138
+\K@=\count107
+\KK@=\count108
+\Direction=\count109
+\K@dXdY=\dimen139
+\K@dYdX=\dimen140
+\xyread@=\read1
+\xywrite@=\write3
+\csp@=\count110
+\quotPTK@=\dimen141
+
+utility macros; pictures: \xy, positions,
+\swaptoks@@=\toks28
+\connectobjectbox@@=\box30
+ objects,
+\styletoks@=\toks29
+ decorations;
+kernel objects: directionals, circles, text; options; algorithms: directions,
+edges, connections;  Xy-pic loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/ifpdf.sty
+Package: ifpdf 2011/01/30 v2.3 Provides the ifpdf switch (HO)
+Package ifpdf Info: pdfTeX in PDF mode is detected.
+)
+Package: xy 2013/10/06 Xy-pic version 3.8.9
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyall.tex
+ Xy-pic option: All features v.3.8
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xycurve.tex
+ Xy-pic option: Curve and Spline extension v.3.12 curve,
+\crv@cnt@=\count111
+\crvpts@=\toks30
+\splinebox@=\box31
+\splineval@=\dimen142
+\splinedepth@=\dimen143
+\splinetol@=\dimen144
+\splinelength@=\dimen145
+ circles,
+\L@=\dimen146
+ loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyframe.tex
+ Xy-pic option: Frame and Bracket extension v.3.14 loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xycmtip.tex
+ Xy-pic option: Computer Modern tip extension v.3.7
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xytips.tex
+ Xy-pic option: More Tips extension v.3.11 loaded) loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyline.tex
+ Xy-pic option: Line styles extension v.3.10
+\xylinethick@=\dimen147
+ loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyrotate.tex
+ Xy-pic option: Rotate and Scale extension v.3.8 loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xycolor.tex
+ Xy-pic option: Colour extension v.3.11 loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xymatrix.tex
+ Xy-pic option: Matrix feature v.3.14
+\Row=\count112
+\Col=\count113
+\queue@=\toks31
+\queue@@=\toks32
+\qcount@=\count114
+\qcount@@=\count115
+\matrixsize@=\count116
+ loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xyarrow.tex
+ Xy-pic option: Arrow and Path feature v.3.9 path, \ar, loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xygraph.tex
+ Xy-pic option: Graph feature v.3.11 loaded) loaded)
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf.tex
+ Xy-pic option: PDF driver v.1.7 Xy-pic pdf driver: `color' extension support
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf-co.tex loaded)
+Xy-pic pdf driver: `curve' extension support
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf-cu.tex loaded)
+Xy-pic pdf driver: `frame' extension support
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf-fr.tex loaded)
+Xy-pic pdf driver: `line' extension support
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf-li.tex loaded)
+Xy-pic pdf driver: `rotate' extension support
+(/usr/local/texlive/2015/texmf-dist/tex/generic/xypic/xypdf-ro.tex loaded)
+loaded)) (/usr/local/texlive/2015/texmf-dist/tex/latex/varwidth/varwidth.sty
+Package: varwidth 2009/03/30 ver 0.92;  Variable-width minipages
+\@vwid@box=\box32
+\sift@deathcycles=\count117
+\@vwid@loff=\dimen148
+\@vwid@roff=\dimen149
+)
+\c@theorem=\count118
+\c@prop=\count119
+\c@lem=\count120
+\c@defn=\count121
+
+No file iotcloud.aux.
+\openout1 = `iotcloud.aux'.
+
+LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 21.
+LaTeX Font Info:    ... okay on input line 21.
+(/usr/local/texlive/2015/texmf-dist/tex/context/base/supp-pdf.mkii
+[Loading MPS to PDF converter (version 2006.09.02).]
+\scratchcounter=\count122
+\scratchdimen=\dimen150
+\scratchbox=\box33
+\nofMPsegments=\count123
+\nofMParguments=\count124
+\everyMPshowfont=\toks33
+\MPscratchCnt=\count125
+\MPscratchDim=\dimen151
+\MPnumerator=\count126
+\makeMPintoPDFobject=\count127
+\everyMPtoPDFconversion=\toks34
+) (/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty
+Package: pdftexcmds 2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO
+)
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/ifluatex.sty
+Package: ifluatex 2010/03/01 v1.3 Provides the ifluatex switch (HO)
+Package ifluatex Info: LuaTeX not detected.
+)
+Package pdftexcmds Info: LuaTeX not detected.
+Package pdftexcmds Info: \pdf@primitive is available.
+Package pdftexcmds Info: \pdf@ifprimitive is available.
+Package pdftexcmds Info: \pdfdraftmode found.
+)
+(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
+Package: epstopdf-base 2010/02/09 v2.5 Base part for package epstopdf
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/grfext.sty
+Package: grfext 2010/08/19 v1.1 Manage graphics extensions (HO)
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/kvdefinekeys.sty
+Package: kvdefinekeys 2011/04/07 v1.3 Define keys (HO)
+))
+(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/kvoptions.sty
+Package: kvoptions 2011/06/30 v3.11 Key value format for package options (HO)
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty
+Package: kvsetkeys 2012/04/25 v1.16 Key value parser (HO)
+
+(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/etexcmds.sty
+Package: etexcmds 2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
+Package etexcmds Info: Could not find \expanded.
+(etexcmds)             That can mean that you are not using pdfTeX 1.50 or
+(etexcmds)             that some package has redefined \expanded.
+(etexcmds)             In the latter case, load this package earlier.
+)))
+Package grfext Info: Graphics extension search list:
+(grfext)             [.png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPE
+G,.JBIG2,.JB2,.eps]
+(grfext)             \AppendGraphicsExtensions on input line 452.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg
+File: epstopdf-sys.cfg 2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Liv
+e
+))
+LaTeX Font Info:    Try loading font information for U+rsfs on input line 21.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/jknapltx/ursfs.fd
+File: ursfs.fd 1998/03/24 rsfs font definition file (jk)
+)
+LaTeX Font Info:    Try loading font information for U+msa on input line 21.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsa.fd
+File: umsa.fd 2013/01/14 v3.01 AMS symbols A
+)
+LaTeX Font Info:    Try loading font information for U+msb on input line 21.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsb.fd
+File: umsb.fd 2013/01/14 v3.01 AMS symbols B
+)
+Package xypdf Info: Line width: 0.43799pt on input line 21.
+LaTeX Font Info:    Try loading font information for OMS+cmr on input line 67.
+
+(/usr/local/texlive/2015/texmf-dist/tex/latex/base/omscmr.fd
+File: omscmr.fd 2014/09/29 v2.5h Standard LaTeX font definitions
+)
+LaTeX Font Info:    Font shape `OMS/cmr/m/n' in size <10.95> not available
+(Font)              Font shape `OMS/cmsy/m/n' tried instead on input line 67.
+ [1
+
+{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] [2] [3]
+
+Underfull \hbox (badness 10000) in paragraph at lines 145--159
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 145--159
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 145--159
+
+ []
+
+[4] [5]
+Underfull \hbox (badness 10000) in paragraph at lines 206--216
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 217--224
+
+ []
+
+[6]
+Underfull \hbox (badness 10000) in paragraph at lines 226--228
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 229--231
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 235--241
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 243--244
+
+ []
+
+[7]
+Underfull \hbox (badness 10000) in paragraph at lines 257--258
+
+ []
+
+
+Underfull \hbox (badness 10000) in paragraph at lines 259--261
+
+ []
+
+[8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22]
+[23] [24] [25]
+Underfull \hbox (badness 1603) in paragraph at lines 1227--1227
+[] $\OML/cmm/m/it/10.95 LSDelete \OMS/cmsy/m/n/10.95   fh\OML/cmm/m/it/10.95 se
+q[]; Dat[]\OMS/cmsy/m/n/10.95 ijh\OML/cmm/m/it/10.95 seq[]; Dat[]\OMS/cmsy/m/n/
+10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots; seq[] >
+ []
+
+
+Underfull \hbox (badness 1603) detected at line 1227
+[] $\OML/cmm/m/it/10.95 LSDelete \OMS/cmsy/m/n/10.95   fh\OML/cmm/m/it/10.95 se
+q[]; Dat[]\OMS/cmsy/m/n/10.95 ijh\OML/cmm/m/it/10.95 seq[]; Dat[]\OMS/cmsy/m/n/
+10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots; seq[] >
+ []
+
+[26]
+Underfull \hbox (badness 10000) in paragraph at lines 1301--1301
+[] $\OML/cmm/m/it/10.95 LstSlt[] \OMS/cmsy/m/n/10.95  $ \OT1/cmr/m/sc/10.95 Up-
+date-LastMes-
+ []
+
+
+Underfull \hbox (badness 10000) detected at line 1301
+[] $\OML/cmm/m/it/10.95 LstSlt[] \OMS/cmsy/m/n/10.95  $ \OT1/cmr/m/sc/10.95 Up-
+date-LastMes-
+ []
+
+[27]
+Overfull \vbox (27.29413pt too high) has occurred while \output is active []
+
+
+[28]
+Underfull \hbox (badness 2376) in paragraph at lines 1385--1385
+[] $\OML/cmm/m/it/10.95 smallestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 s
+eq$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq;
+ DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+
+ []
+
+
+Underfull \hbox (badness 2359) detected at line 1385
+[] $\OML/cmm/m/it/10.95 smallestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 s
+eq$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq;
+ DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+
+ []
+
+[29]
+Underfull \hbox (badness 2376) in paragraph at lines 1430--1430
+[] $\OML/cmm/m/it/10.95 smallestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 s
+eq$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq;
+ DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+
+ []
+
+
+Underfull \hbox (badness 3482) in paragraph at lines 1430--1430
+[] $\OML/cmm/m/it/10.95 largestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 se
+q$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq; 
+DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+ []
+
+
+Underfull \hbox (badness 3482) detected at line 1430
+[] $\OML/cmm/m/it/10.95 largestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 se
+q$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq; 
+DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+ []
+
+
+Underfull \hbox (badness 2376) detected at line 1430
+[] $\OML/cmm/m/it/10.95 smallestseq \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 s
+eq$ \OT1/cmr/m/n/10.95 such that $\OMS/cmsy/m/n/10.95 h\OML/cmm/m/it/10.95 seq;
+ DE\OMS/cmsy/m/n/10.95 i 2 \OML/cmm/m/it/10.95 LocalSlots \OMS/cmsy/m/n/10.95 ^
+
+ []
+
+[30]
+Overfull \vbox (13.69412pt too high) has occurred while \output is active []
+
+
+[31]
+Underfull \hbox (badness 10000) in paragraph at lines 1471--1471
+[] $\OML/cmm/m/it/10.95 DE[] \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 DE[]\OMS
+/cmsy/m/n/10.95 [$ \OT1/cmr/m/sc/10.95 Cre-ate-Col-
+ []
+
+
+Underfull \hbox (badness 10000) detected at line 1471
+[] $\OML/cmm/m/it/10.95 DE[] \OMS/cmsy/m/n/10.95   \OML/cmm/m/it/10.95 DE[]\OMS
+/cmsy/m/n/10.95 [$ \OT1/cmr/m/sc/10.95 Cre-ate-Col-
+ []
+
+[32]
+Overfull \vbox (149.69418pt too high) has occurred while \output is active []
+
+
+[33] [34]
+Overfull \vbox (95.29416pt too high) has occurred while \output is active []
+
+
+[35]
+Overfull \vbox (95.20291pt too high) has occurred while \output is active []
+
+
+[36] [37] [38] [39] [40] (./iotcloud.aux) ) 
+Here is how much of TeX's memory you used:
+ 5802 strings out of 493089
+ 75548 string characters out of 6134842
+ 202852 words of memory out of 5000000
+ 9157 multiletter control sequences out of 15000+600000
+ 13316 words of font info for 54 fonts, out of 8000000 for 9000
+ 1141 hyphenation exceptions out of 8191
+ 38i,11n,26p,3240b,312s stack positions out of 5000i,500n,10000p,200000b,80000s
+</usr/local/texlive/2015/texmf-dist/
+fonts/type1/public/amsfonts/cm/cmbx10.pfb></usr/local/texlive/2015/texmf-dist/f
+onts/type1/public/amsfonts/cm/cmbx12.pfb></usr/local/texlive/2015/texmf-dist/fo
+nts/type1/public/amsfonts/cm/cmcsc10.pfb></usr/local/texlive/2015/texmf-dist/fo
+nts/type1/public/amsfonts/cm/cmmi10.pfb></usr/local/texlive/2015/texmf-dist/fon
+ts/type1/public/amsfonts/cm/cmmi6.pfb></usr/local/texlive/2015/texmf-dist/fonts
+/type1/public/amsfonts/cm/cmmi8.pfb></usr/local/texlive/2015/texmf-dist/fonts/t
+ype1/public/amsfonts/cm/cmr10.pfb></usr/local/texlive/2015/texmf-dist/fonts/typ
+e1/public/amsfonts/cm/cmr6.pfb></usr/local/texlive/2015/texmf-dist/fonts/type1/
+public/amsfonts/cm/cmr8.pfb></usr/local/texlive/2015/texmf-dist/fonts/type1/pub
+lic/amsfonts/cm/cmr9.pfb></usr/local/texlive/2015/texmf-dist/fonts/type1/public
+/amsfonts/cm/cmsy10.pfb></usr/local/texlive/2015/texmf-dist/fonts/type1/public/
+amsfonts/cm/cmsy8.pfb></usr/local/texlive/2015/texmf-dist/fonts/type1/public/am
+sfonts/cm/cmti10.pfb>
+Output written on iotcloud.pdf (40 pages, 245596 bytes).
+PDF statistics:
+ 187 PDF objects out of 1000 (max. 8388607)
+ 130 compressed objects within 2 object streams
+ 0 named destinations out of 1000 (max. 500000)
+ 1 words of extra memory for PDF output out of 10000 (max. 10000000)
+
diff --git a/version2/backup/doc/iotcloud.pdf b/version2/backup/doc/iotcloud.pdf
new file mode 100644 (file)
index 0000000..6feabc2
Binary files /dev/null and b/version2/backup/doc/iotcloud.pdf differ
diff --git a/version2/backup/doc/iotcloud.tex b/version2/backup/doc/iotcloud.tex
new file mode 100644 (file)
index 0000000..1d760d7
--- /dev/null
@@ -0,0 +1,1860 @@
+\documentclass[11pt]{article}\r
+\newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}\r
+\usepackage{color}\r
+\usepackage{amsthm}\r
+\usepackage{amsmath}\r
+\usepackage{graphicx}\r
+\usepackage{mathrsfs}\r
+\usepackage{amssymb}\r
+\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx\r
+\usepackage[all]{xy}\r
+\usepackage{varwidth}\r
+\r
+\newtheorem{theorem}{Theorem}\r
+\newtheorem{prop}{Proposition}\r
+\newtheorem{lem}{Lemma}\r
+\newtheorem{defn}{Definition}\r
+\newcommand{\note}[1]{{\color{red} \bf [[#1]]}}\r
+\newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}\r
+\newcommand*\xor{\mathbin{\oplus}}\r
+\algnewcommand{\LeftComment}[1]{\Statex \(\triangleright\) #1}\r
+\begin{document}\r
+\r
+\r
+\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text\r
+\r
+\r
+\section{\textbf{Introduction}}\r
+\r
+\r
+\section{Approach}\r
+\r
+\subsection{Keys}\r
+\r
+Each device has: user id + password\r
+\r
+Server login is:\r
+hash1(user id), hash1(password)\r
+\r
+Symmetric Crypto keys is:\r
+hash2(user id | password)\r
+\r
+Server has finite length queue of entries + max\_entry\_identifier +\r
+server login key\r
+\r
+\subsection{Entry layout}\r
+Each entry has:\r
+\begin{enumerate}\r
+\item Sequence identifier\r
+\item Random IV (if needed by crypto algorithm)\r
+\item Encrypted payload\r
+\end{enumerate}\r
+\r
+Payload has:\r
+\begin{enumerate}\r
+\item Sequence identifier\r
+\item Machine id (most probably something like a 64-bit random number \r
+that is self-generated by client)\r
+\item HMAC of previous slot\r
+\item Data entries\r
+\item HMAC of current slot\r
+\end{enumerate}\r
+\r
+A data entry can be one of these:\r
+\begin{enumerate}\r
+    \item A transaction:\r
+        \begin{itemize}\r
+            \item Contains a sequence number, a set of key value pair updates and a guard condition that can be evaluated.\r
+            \item Must have the same arbitrator for all its key value pair updates and reads within the guard condition\r
+        \end{itemize}\r
+    \r
+    \item A Commit\r
+        \newline{Commits a transaction into the block chain.  Until a transaction is committed, no client can be sure if that transaction's key value updates will be used to update the state of the system.  Once an arbitrator commits a transaction then that transaction becomes a permanent state change in the system.  Transactions should be committed and aborted in order of their sequence numbers.}\r
+    \r
+    \item An Abort\r
+        \newline{An abort is used to show that a transactions key value update should not be used in the state change of the system.  This occurs when the guard of a transaction evaluates to false meaning that the conditions under-which this transaction should be committed no longer exists in the system (another transaction could have been committed first that would have changed the system in a way that makes the current transaction invalid).}\r
+    \r
+    \item New Key:\r
+        \newline{This creates a new key and assignes an arbitrator to that key.  Only the first new key message for a given key is valid.  Once a new key message is inserted into the block chain it is never removed and no other new key entries for the same key name can be inserted into the block chain.}\r
+        \r
+    \item Slot sequence entry: Machine id + last message identifier \r
+        \newline {The purpose of this is to keep the record of the last slot from a certain client if a client's update has to expunge that other client's last entry from the queue. This is kept in the slot until the entry owner inserts a newer update into the queue.}\r
+\r
+    \item Queue state entry: Includes queue size \r
+        \newline {The purpose of this is for the client to tell if the server lies about the number of slots in the queue, e.g. if there are 2 queue state entry in the queue, e.g. 50 and 70, the client knows that when it sees 50, it should expect at most 50 slots in the queue and after it sees 70, it should expect 50 slots before that queue state entry slot 50 and at most 70 slots. The queue state entry slot 70 is counted as slot number 51 in the queue.}\r
+\r
+    \item Collision resolution entry: message identifier + machine id of a collision winner\r
+        \newline {The purpose of this is to keep keep track of the winner of all the collisions until all clients have seen the particular entry.}\r
+\end{enumerate}\r
+\r
+\subsection{Live status}\r
+\r
+Live status of entries:\r
+\begin{enumerate}\r
+    \item Transaction is live if it has not been committed or aborted yet.\r
+    \r
+    \item Abort is live until the machine ID that created the transaction that is being aborted inserts into the block chain a message with a sequence number greater than the abort (that client sees the abort).\r
+        \r
+    \item Commit is dead if for all key value updates in the commit there is a commit with the same key value update that is newer (larger sequence number).  The committing client (arbitrator) will see those newer commits since it is the one that generates them.\r
+    \r
+    \item New Key messages are always kept alive.  Keys can not be deleted.  Deleted keys will cause arbitration to fail if a key is deleted then reassigned to a new client device for arbitration.\r
+    \r
+    \item Slot sequence number (of either a message version data or user-level data) is dead if there is a newer slot from the same machine.\r
+\r
+    \item Queue state entry is dead if there is a newer queue state entry.\r
+    {In the case of queue state entries 50 and 70, this means that queue state entry 50 is dead and 70 is live. However, not until the number of slots reaches 70 that queue state entry 50 will be expunged from the queue.  Further all entries before the 50 entry will also not be expunged until the queue size has reached 70}\r
+\r
+    \item Collision resolution entry is dead if this entry has been seen by all clients after a collision happens.\r
+\end{enumerate}\r
+\r
+When data is at the end of the queue ready to expunge, if:\r
+\begin{enumerate}\r
+    \item If any entry is not dead it must be reinserted into the queue.\r
+\r
+    \item If the slot sequence number is not dead, then a message sequence entry must be inserted.\r
+\end{enumerate}\r
+\r
+\paragraph{Validation procedure on client:}\r
+\begin{enumerate}\r
+    \item Decrypt each new slot in order.\r
+    \item For each slot:\r
+        (a) check its HMAC, and\r
+        (b) check that the previous entry HMAC field matches the previous entry (in case of a gap do not check for slots on gap margins).\r
+    \item That no slots are slots we have seen before (server trying to pass old slots).    \r
+    \r
+    \item For all other machines, check that the latest sequence number is at least as large (never goes backwards).\r
+    \r
+    \item That the queue has a current queue state entry.\r
+    \r
+    \item That the number of entries received is consistent with the size specified in the queue state entry and/or the queue is growing in size.\r
+\end{enumerate}\r
+\r
+\subsection{Resizing Queue}\r
+Client can make a request to resize the queue. This is done as a write that combines:\r
+  (a) a slot with the message, and (b) a request to the server. The queue can only be expanded, never contracted; attempting to decrease the size of the queue will cause future clients to throw an error.\r
+\r
+\r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
+\r
+\subsection{The Arbitrator}\r
+Each key has an arbitrator that makes the final decision when it comes to whether a specific transaction containing that key updates the state of the system or is aborted.  This ensures that clients can make offline updates and then push those updates to the server at a later time.  The arbitrator then tries to merge those updates and if possible will commit them into the current working state of the system.  If not possible then the arbitrator will abort that transaction.  The arbitrator arbitrates on transactions in order of transaction sequence number.\r
+\r
+\r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
+\section{Server Algorithm}\r
+$s \in SN$ is a sequence number\\\r
+$sv \in SV$ is a slot's value\\\r
+$slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$ \\ \\\r
+\textbf{State} \\\r
+\textit{SL = set of live slots on server} \\\r
+\textit{max = maximum number of slots (input only for resize message)} \\\r
+\textit{n = number of slots} \\ \\\r
+\textbf{Helper Function} \\\r
+$MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv}\r
+\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\\r
+$MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \r
+\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\\r
+$SeqN(slot_s = \tuple{s, sv})=s$ \\\r
+$SlotVal(slot_s = \tuple{s, sv})=sv$ \\\r
+\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Slot:}\\\r
+Returns to the client the slots that have a sequence number that is greater than or equal to the sequence number that is in the requese.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetSlot}{$s_g$}\r
+\State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Slot:}\\\r
+Puts a slot in the server memory if the slot has the correct sequence number.  Also resizes the server memory if needed.\\\r
+\begin{algorithmic}[1]\r
+\Function{PutSlot}{$s_p,sv_p,max'$}\r
+\If{$(max' \neq \emptyset)$}  \Comment{Resize}\r
+\State $max \gets max'$\r
+\EndIf\r
+\State $\tuple{s_n,sv_n} \gets MaxSlot(SL)$\Comment{Last sv}\r
+%\State $s_n \gets SeqN(\tuple{s_n,sv_n})$\r
+\If{$(s_p = s_n + 1)$}\r
+    \If{$n = max$}\r
+        \State $\tuple{s_m,sv_m} \gets MinSlot(SL)$\Comment{First sv}\r
+        \State $SL \gets SL - \{\tuple{s_m,sv_m}\}$\r
+    \Else \Comment{$n < max$}\r
+        \State $n \gets n + 1$\r
+    \EndIf\r
+    \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$\r
+    \State \Return{$(true,\emptyset)$}\r
+\Else\r
+    \State \Return{$(false,\{\tuple{s,sv}\in SL \mid \r
+    s \geq s_p\})$}\r
+\EndIf\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+\r
+\r
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
+\section{\textbf{Client}}\r
+\r
+\subsection{\textbf{Client Notation Conventions}}\r
+$k$ is key of entry \\\r
+$v$ is value of entry \\\r
+$size$ is a size (target size of the current block chain) \\\r
+$kv$ is a key-value pair $\tuple{k,v}$ \\\r
+$KV$ is a set of $kv$ \\\r
+$id$ is a machine ID \\\r
+$seq$ is a sequence number \\\r
+$hmac_p$ is the HMAC value of the previous slot \\\r
+$hmac_c$ is the HMAC value of the current slot \\\r
+$Guard$ is a set of$ \tuple{k,v,$logical operator$}$ which can be evaluated to a boolean \\\r
+\r
+$trans$ is a transaction entry , $\tuple{seq, id, KV, Guard}$  \\\r
+$lastmsg$ is a last message entry, $\tuple{seq, id}$ \\\r
+$qstate$ is a queue state entry, $\tuple{size}$ \\\r
+$colres$ is a collision resolution entry, $\tuple{id, seq_{old}, seq_{new}, true \lor false}$ \\\r
+$newkey$ is a new key entry, $\tuple{k, id}$, $id$ is ID of arbitrator \\\r
+$commit$ is a commit transaction entry, $\tuple{seq_{trans},KV}$, id is id of arbitrator \\\r
+$abort$ is an abort transaction entry, $\tuple{seq_{trans},id_{trans}}$ \\\r
+\r
+\r
+$de$ is a data entry that can one of: $trans$, $lastmsg$, $qstate$, $colres$, $newkey$, $commit$, $abort$ \\\r
+$DE$ is a set of all data entries, possibly of different types, in a single message, set of $de$\\\r
+\r
+$slotDat = \tuple{seq,id,DE,hmac_p,hmac_c}$ \\\r
+$slot = \tuple{seq, Encrpt(slotDat)}$\\\r
+\r
+\subsection{\textbf{Client State}}\r
+\r
+\subsubsection{Constants}\r
+$LOCAL\_ID$ = machine ID of this client.\\\r
+$RESIZE\_THRESH\_PERCENT$ = percent of slots that need to have live data to trigger a resize.\\\r
+$RESIZE\_PERCENT$ = percent that we should grow the block chain to.\\\r
+$DATA\_ENTRY\_SET\_MAX\_SIZE$ = max size that a data entry set can have (in bytes).\\\r
+$DEAD\_SLOT\_COUNT$ = number of slots to keep dead if possible at the end of the block chain.\\\r
+$MAX\_RESCUE\_SKIPS$ = number of skips that are allowed when saving data entries.\\\r
+\r
+\subsubsection{Primitive Variables}\r
+$max\_size$ = max size of the block chain\\\r
+\r
+\subsubsection{Sets and Lists}\r
+\r
+$PendingTransQueue$ = Queue of pending transactions that need to be pushed to the block chain, $\tuple{PendingTrans}$\\\r
+$PendingTrans= \tuple{KV, Guard} = \tuple{$set of key value pairs, set of guard conditions$}$.\\\r
+$Arbitrator$ = set of $\tuple{k,id}$ containing the key and its arbitrating device.\\\r
+$LastSlot$ = set of $\tuple{id, seq}$ containing the machine ID and the largest sequence number from that machine ID.\\\r
+$LocalSlots$ = set of slots that are in the clients local buffer (initially $\emptyset$), data is decrypted.\\\r
+$RejectedSlotList$ = ordered list of the sequence numbers of slots that this client tried to insert but were rejected.\\\r
+$CommittedKV$ = set of committed key value pairs (initially $\emptyset$).\\\r
+$SpeculatedKV$ = set of speculated key value pairs (initially $\emptyset$).\r
+\r
+\subsection{Helper Functions}\r
+The following helper functions are needed:\\\r
+\r
+$MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\\r
+$MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} \in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\\r
+\r
+\r
+% Get Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Byte Size:}\\\r
+Get the size in bytes of the thing that is passed in.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetSize}{$a$}\r
+    \State \Return{Size in bytes of $a$}\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+% Error\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Error:}\\\r
+Prints an error message and halts the execution of the client.\\\r
+\begin{algorithmic}[1]\r
+\Function{Error}{$msg$}\r
+    \State $Print(msg)$\r
+    \State $Halt()$\r
+\EndFunction\r
+\end{algorithmic}\end{varwidth}% \r
+}\r
+\r
+% Get Next Sequence Number\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Next Sequence Number:}\\\r
+Get the next sequence number for insertion into the block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetNextSeq}{$k$}\r
+    \LeftComment{Get the largest known sequence number}\r
+    \State $seq_{ret} \gets seq$ such that $\tuple{id, seq}\in LastSlo \land (\forall \tuple{id', seq'} \in LastSlo, seq \geq seq')$\\\r
+    \r
+    \LeftComment{Add one to the largest seq number to generate the new seq number}\r
+    \State \Return{$seq_{ret} + 1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get Arbitrator\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Arbitrator:}\\\r
+Get the arbitrator for a given key.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetArbitrator}{$k$}\r
+    \State $\tuple{k_1,id_1} \gets \tuple{k_2,id_2} $ \textit{such that} $ \tuple{k_2,id_2} \in Arbitrator \land k_2=k$\r
+    \State \Return{$id_1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get Arbitrator KV\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get Arbitrator for KV Set:}\\\r
+Get the arbitrator for a given key value set.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetArbitratorKV}{$KV$}\r
+    \State $\tuple{k,v} \gets \tuple{k',v'}$ such that $\tuple{k',v'} \in KV$\r
+    \State $\tuple{k_1,id_1} \gets \tuple{k_2,id_2} $ \textit{such that} $ \tuple{k_2,id_2} \in Arbitrator \land k_2=k$\r
+    \State \Return{$id_1$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Transaction arbitrator\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Arbitrator for a Transaction:}\\\r
+Check that the arbitrators for a given set are all the same arbitrator.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckArbitrator}{$PendingTrans_a$}\r
+    \State $id_{arb} \gets NULL$\\\r
+    \State $\tuple{KV_a, Guard_a} \gets PendingTrans_a$\r
+    \r
+    \ForAll{$\tuple{k',v'} \in KV_a$}\r
+        \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
+        \r
+        \If{$id_{arb} = NULL$}  \r
+            \State $id_{arb} \gets id'$\r
+        \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
+            \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \ForAll{$\tuple{k',v', lop'} \in Guard_a$}\r
+        \State $id' \gets$ \Call{GetArbitrator}{$k'$}\\\r
+        \r
+        \If{$id_{arb} = NULL$}  \r
+            \State $id_{arb} \gets id'$\r
+        \ElsIf{$id' \neq id_{arb}$} \Comment{Check all arbitrators are the same}\r
+            \State \Call{Error}{"Multiple arbitrators for key values in transaction."}\r
+        \EndIf\r
+    \EndFor\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Commits\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all Commits:}\\\r
+Get all commits that are currently in the local block chain.  Iterate over all the local slots and extract all the commits from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetCommits}{$ $}\r
+    \State $ComSet \gets \emptyset$ \Comment{Set of the commits}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $ComSet \gets ComSet \cup \{c |c \in DE',c$is a $commit\}$\r
+    \EndFor\r
+    \State \Return{$ComSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Transactions\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all Transactions:}\\\r
+Get all transactions that are currently in the local block chain.  Iterate over all the local slots and extract all the transactions from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetTrans}{$ $}\r
+    \State $TransSet \gets \emptyset$ \Comment{Set of the trans}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $TransSet \gets TransSet \cup \{c |c \in DE',c$is a $trans\}$\r
+    \EndFor\r
+    \State \Return{$TransSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all aborts\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all aborts:}\\\r
+Get all aborts that are currently in the local block chain.  Iterate over all the local slots and extract all the aborts from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetAborts}{$ $}\r
+    \State $AbrtSet \gets \emptyset$ \Comment{Set of the aborts}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $AbrtSet \gets AbrtSet \cup \{c |c \in DE',c$is a $abort\}$\r
+    \EndFor\r
+    \State \Return{$AbrtSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Queue States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all queue states:}\\\r
+Get all qstates that are currently in the local block chain.  Iterate over all the local slots and extract all the qstates from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetQStates}{$ $}\r
+    \State $QSet \gets \emptyset$ \Comment{Set of the qstates}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $QSet \gets QSet \cup \{c |c \in DE',c$is a $qstate\}$\r
+    \EndFor\r
+    \State \Return{$QSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get all Last Messages States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get all last message data entrues:}\\\r
+Get all last msg that are currently in the local block chain.  Iterate over all the local slots and extract all the last msg from each slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetLastMsg}{$ $}\r
+    \State $LMSet \gets \emptyset$ \Comment{Set of the last msg}\\\r
+        \r
+    \LeftComment{Iterate over all the slots saved locally}\r
+    \ForAll{$\tuple{s_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots$}\r
+        \State $LMSet \gets LMSet \cup \{c |c \in DE',c$is a $lastmsg\}$\r
+    \EndFor\r
+    \State \Return{$LMSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Queue State Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Queue State Live:}\\\r
+A queue state is dead if there is another queue state data entry that has a larger queue state.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckQStateLive}{$qstate_a$}\r
+    \State $\tuple{size_a} \gets qstate_a$\r
+    \State $AllQStates \gets$ \Call{GetQState}{} \Comment{Get all the qstates} \\\r
+    \r
+    \If{$\exists \tuple{size'} \in AllQStates, size' > size_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Commit Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Commit Live:}\\\r
+A commit is dead if for every key value pair in the commit there is a commit with a larger transaction sequence number that has a key value pair with the same key.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckCommitLive}{$commit_a$}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
+    \State $KSet \gets \{k|\tuple{k,v} \in KV\}$\r
+    \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits} \\\r
+    \r
+    \LeftComment{Iterate all commits that are newer in time}\r
+    \ForAll{$\tuple{seq_{trans}',KV'}\in AllCommits, seq_{trans}' > seq_{a_{trans}}$}\r
+        \State $KVSet \gets KVSet \setminus \{k|\tuple{k,v} \in KV'\}$\\\r
+        \r
+        \If{$KVSet = \emptyset$}\r
+            \State \Return{false} \Comment{All keys have a newer commit}\r
+        \EndIf\r
+    \EndFor\r
+    \State \Return{true} \Comment{If got here then some keys still live}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Last Message Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Last Message Live:}\\\r
+The last message is dead if the device in question pushed a slot that has a larger sequence number than the one recorded in the last message data entry. \\\r
+\begin{algorithmic}[1]\r
+\Function{CheckLastMsgLive}{$lastmsg_a$}\r
+    \State $\tuple{seq_a, id_a} \gets lastmsg_a$\\\r
+    \r
+    \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+%Check Collision Resolution Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Collision Resolution Live:}\\\r
+Check if a collision resolution data entry is live or not.  This done by checking if all clients that we know about have seen the collision resolution entry.  This is checked by seeing if all devices have inserted a message with a larger sequence number into the block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckColResLive}{$colres_a$}\r
+    \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, equal_a} \gets colres_a$\\\r
+    \r
+    \If{$\forall \tuple{id', seq'} \in LastSlot, seq' \geq seq_{a_{new}}$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check New Key Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check New Key Live:}\\\r
+A new key data entry is always live.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckNewkeyLive}{$newkey_a$}\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Abort Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Abort Live:}\\\r
+Check if an abort data entry is live or not.  Abort is dead if the device whos transaction was aborted sees the abort.  This is checked by seeing if that device inserted a slot into the block chain which has a sequence numberl that is larger than the aborts sequence number.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckAbortLive}{$abort_a, seq_a$}\r
+    \State $\tuple{seq_{a_{trans}},id_a} \gets abort_a$\\\r
+    \r
+    \LeftComment{The device whos transaction was aborted saw the abort}\r
+    \If{$\exists \tuple{id', seq'} \in LastSlot, id'=id_a \land seq' > seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{True}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Transaction Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Transaction Live:}\\\r
+A transaction is dead if there is an abort for that transaction or if there is a commit for that a transaction that came after this transaction.  Since transactions must be committed in order of there insertion, seeing a transaction that is committed and has a larger sequence number than the transaction in question means that the transaction in question was committed at some point.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckTransLive}{$trans_a$}\r
+    \State $\tuple{seq_a, id_a, KV_a, Guard_a} \gets trans_a$\r
+    \State $AllCommits \gets$ \Call{GetCommits}{} \Comment{Get all the commits}\r
+    \State $AllAborts \gets$ \Call{GetAborts}{} \Comment{Get all the aborts} \\\r
+    \r
+    \If{$\exists \tuple{seq_{abrt}',seq_{trans}',id'} \in AllAborts, seq_{trans}' = seq_a$}\r
+        \State \Return{false}\r
+    \ElsIf{$\exists \tuple{seq_{trans}',KV'} \in AllCommits, seq_{trans}' \geq seq_a$}\r
+        \State \Return{false}\r
+    \EndIf\r
+    \State \Return{true}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Live:}\\\r
+Checks if a data entry is live based on its type.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckLive}{$datentry, seq$}\r
+    \If{$datentry$ is a $commit$}\r
+        \State \Return{\Call{CheckCommitLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $abort$}\r
+        \State \Return{\Call{CheckAbortLive}{$datentry, seq$}}\\r
+    \ElsIf{$datentry$ is a $trans$}\r
+        \State \Return{\Call{CheckTransLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $lastmsg$}\r
+        \State \Return{\Call{CheckLastMsgLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $colres$}\r
+        \State \Return{\Call{CheckColResLive}{$datentry$}}\\r
+    \ElsIf{$datentry$ is a $qstate$}\r
+        \State \Return{\Call{CheckQStateLive}{$datentry$}}\r
+    \ElsIf{$datentry$ is a $newkey$}\r
+        \State \Return{\Call{CheckNewkeyLive}{$datentry$}}\r
+    \Else\r
+        \State \Call{Error}{"Unknown data entry type."}\r
+    \EndIf\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Slot Has Live\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Slot Has Live:}\\\r
+Check if the slot has any live data entries in it. Do this by looking at all the data entries in the slot and checking if they are live\\\r
+\begin{algorithmic}[1]\r
+\Function{SlotHasLive}{$slot_a$}\r
+    \State $\tuple{s_1, \tuple{seq_2,id,DE,hmac_p,hmac_c}} \in LocalSlots$\r
+    \r
+    \ForAll{$datentry \in DE$}\r
+        \If{\Call{CheckLive}{$datentry, s_1$}} \Comment{an entry is alive}\r
+            \State \Return{true}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State \Return{false} \Comment{All entries were dead}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Calculate Resize Threshold\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Calculate Resize Threshold:}\\\r
+Calculate a threshold for how many slots need to have live data entries in them for a resize to take place.\\\r
+\begin{algorithmic}[1]\r
+\Function{CalcResizeThresh}{$maxsize$}\r
+    \State \Return{$\left \lfloor {maxsize * RESIZE\_THRESH\_PERCENT} \right \rfloor$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Calculate Block Chain New Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Calculate Block Chain New Size:}\\\r
+Calculate the new size of the block chain which we need if we are to resize the data structure.\\\r
+\begin{algorithmic}[1]\r
+\Function{CalcNewSize}{$maxsize$}\r
+    \State \Return{$\left \lceil {maxsize * RESIZE\_THRESH\_PERCENT} \right \rceil$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Should Resize\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Should Resize:}\\\r
+Check if the block should resize based on some metric of how many slots in the block chain are filled with live data. \\\r
+\begin{algorithmic}[1]\r
+\Function{ShouldResize}{$ $}\r
+    \State $LiveSlots \gets \{slot_s|slot_s \in LocalSlots \land $\Call{SlotHasLive}{$slot_s$}$\}$\r
+    \State $resizethreshold \gets $ \Call{CalcResizeThresh}{$max\_size$}\r
+    \State \Return{$|LiveSlots| \geq resizethreshold$} \Comment{If passes threshold then resize}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Queue State \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Queue State:}\\\r
+Generate a queue state data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateQState}{$size_a$}\r
+    \State \Return{$\tuple{size_a}$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Abort\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Abort:}\\\r
+Generate a abort data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateAbort}{$seq_a, id_a$}\r
+    \State \Return{$\tuple{seq_a, id_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Collision\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create ColRes:}\\\r
+Generate a colres data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateColRes}{$is_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a$}\r
+    \State \Return{$\tuple{id_a, seq_{a_{old}}, seq_{a_{new}},}isequal_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Create Transaction\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Transaction:}\\\r
+Generate a transaction data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateTrans}{$pendingtrans_a, seq_a$}\r
+    \State $\tuple{KV_a, Guard_a} \gets pendingtrans_a$\r
+    \State \Return{$\tuple{seq_a, LOCAL\_ID, KV_a, Guard_a}$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Commit:}\\\r
+Generate a commit data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateCommit}{$seq_a,KV_a$}\r
+    \State \Return{$\tuple{seq_a,KV_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create New Key\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Key:}\\\r
+Generate a new key data entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateNewKey}{$k_a, id_a$}\r
+    \State \Return{$\tuple{k_a,id_a}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Data Entry Set Has Space \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Data Entry Set Has Space :}\\\r
+Checks if a data entry set has enough space for a new data entry to be inserted.\\\r
+\begin{algorithmic}[1]\r
+\Function{DEHasSpace}{$DE_a, de_a$}\r
+    \State $newsize \gets $ \Call{GetSize}{$DE_a$}\r
+    \State $newsize \gets newsize +$ \Call{GetSize}{$de_a$}\r
+    \State \Return{$newsize \leq DATA\_ENTRY\_SET\_MAX\_SIZE$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Rescued Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Rescued Date Entry:}\\\r
+For commits only the key-value pairs that are most recent (no newer commit that has those key values in it).\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateRescuedCommit}{$commit_a$}\r
+    \State $AllCommits \gets $ \Call{GetCommits}{}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets de_a$\r
+    \State $NewKV \gets KV_a$\\\r
+\r
+    \LeftComment{Get rid of all key values that have newer commits}\r
+    \ForAll{$\tuple{k_a, v_a} \in KV_a$}\r
+        \LeftComment{Iterate over all commits that are newer than the rescue commit}\r
+        \ForAll{$\tuple{seq', KV'} \in AllCommits, seq' > seq_{a_{trans}}$}\r
+            \If{$\exists \tuple{k', v'} \in KV', k' = k_a$}\r
+                \State $NewKV \gets NewKV \setminus \tuple{k_a, v_a}$\r
+                \State Break\r
+            \EndIf\r
+        \EndFor\r
+    \EndFor\r
+    \State \Return{$\tuple{seq_{a_{trans}}, NewKV}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Rescued Date Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Rescued Date Entry:}\\\r
+Generate the data entry rescued version of the entry.  For some data entry types such as commits, the entry is not rescued as is.  For commits only the key-value pairs that are most recent (no newer commit that has those key values in it).\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateRescuedEntry}{$de_a$}\r
+\r
+    \If{$de_a$is a $commit$}\r
+        \State \Return{\Call{CreateRescuedCommit}{$de_a$}}\r
+    \EndIf\r
+    \r
+    \State \Return{$de_a$} \Comment{No Modification needed}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Slot Hmacs\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Slot HMACs:}\\\r
+Check that each slot has not been tampered with by checking that the stored HMAC matches the calculated HMAC.  Also check thatthe slot number reported by the server matches the slot number of the actual slot.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSlotsHmacAndSeq}{$Slots_a$}\r
+    \ForAll{$slot_a \in Slots_a$}\r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
+        \State $calchmac \gets $ \Call{GenerateHmac}{$seq_{a_2}, id_a, DE_a, hmac_{a_p}$}\r
+    \r
+        \If{$seq_{a_1} \neq seq_{a_2}$}\r
+          \State \Call{Error}{"Slot sequence number mismatch"}\r
+        \ElsIf{$calchmac \neq hmac_{a_c}$}\r
+            \State \Call{Error}{"Slot HMAC mismatch"}\r
+        \EndIf\r
+    \EndFor\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check HMAC Chain\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check HMAC Chain:}\\\r
+Check that the HMAC chain has not been violated.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckHmacChain}{$Slots_a$}\r
+    \State $SlotsList \gets Slots_a$ sorted by sequence number\\  \r
+    \r
+    \r
+    \LeftComment{Check all new slots}\r
+    \ForAll{$index \in [2: |SlotsList|]$}\r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[i-1]$\r
+        \State $\tuple{seq_{b_1}, \tuple{seq_{b_2},id_b,DE_b,hmac_{b_p},hmac_{b_c}}} \gets SlotList[i]$\r
+        \r
+        \If{$hmac_{b_p} \neq hmac_{b_c}$}\r
+            \State \Call{Error}{"Invalid previous HMAC."}\r
+        \EndIf    \r
+    \EndFor\\\r
+    \r
+    \LeftComment{Check against slots that we already have in the block chain}\r
+    \If{$|LocalSlots| \neq 0$}\r
+        \State $\tuple{seq, SDE} \gets $\Call{MaxSlot}{$LocalSlots$}\r
+        \State $\tuple{seq{last_2},id_{last},DE_{last},hmac_{last_p},hmac_{last_c}} \gets SDE$\\\r
+        \r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets SlotList[1]$\\\r
+\r
+        \If{$(seq_{last_2} + 1) = seq_{a_1}$}\r
+            \If{$hmac_{a_p} \neq hmac_{last_c}$}\r
+                \State \Call{Error}{"Invalid previous HMAC."}\r
+            \EndIf\r
+        \EndIf\r
+    \EndIf\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Check For Old Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check For Old Slots:}\\\r
+Check if the slots are not new.  Checks if the "new" slots are actually new or if they are older than the most recent slot that we have.\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckOldSlots}{$Slots_a$}\r
+    \State $\tuple{seq_{new}, Dat_{new}} \gets$ \Call{MinSlot }{$Slots_a$} \Comment{Get the oldest new slot}\r
+    \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot }{$LocalSlots$} \Comment{Get the newest slot seen}\\\r
+    \r
+    \If{$seq_{new} \leq seq_{local}$} \Comment{The slots were not newer than what was already seen}\r
+        \State \Call{Error}{"Server sent old slots."}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Check if slots have the same sequence number but different data entries}\r
+    \ForAll{$\tuple{seq, Dat} \in Slots_a$}\r
+        \If{$\exists \tuple{seq', Dat'} \in (LocalSlots \cup Slots_a), seq'=seq \land Dat' \neq Dat$}\r
+            \State \Call{Error}{"Slot sequence number match but data does not"}\r
+        \EndIf\r
+    \EndFor\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get All Queue States with Sequence numbers\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Get All Queue States with Sequence numbers:}\\\r
+% Gets all the queue states with the sequence number of the slot that the queue state was inside.\r
+% \begin{algorithmic}[1]\r
+% \Function{GetQStateWithSeq}{$Slots_a$}\r
+%     \State $QSet \gets \emptyset$\\\r
+    \r
+%     \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
+%         \ForAll{$de' \in DE'$}\r
+%             \If{$de'$ is a  $qstate$}\r
+%                 \State $QSet \gets QSet \cup \{\tuple{seq_1', de'}\}$\r
+%             \EndIf\r
+%         \EndFor\r
+%     \EndFor\\\r
+    \r
+%     \State \Return{$QSet$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+% Get All Queue States\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get All Queue States:}\\\r
+Gets all the queue states from the slots that were passed in.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetQState}{$Slots_a$}\r
+    \State $QSet \gets \emptyset$\\\r
+    \r
+    \ForAll{$\tuple{seq_1', \tuple{seq_2',id',DE',hmac_p', hmac_c'}} \in Slots_a$}\r
+        \ForAll{$de' \in DE'$}\r
+            \If{$de'$ is a  $qstate$}\r
+                \State $QSet \gets QSet \cup \{de'\}$\r
+            \EndIf\r
+        \EndFor\r
+    \EndFor\\\r
+    \r
+    \State \Return{$QSet$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Check Size With Gap\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Size With Gap:}\\\r
+Checks that the block chain size is correct when there is a gap in the block chain.  This check makes sure that the server is not hiding any information from the client.  If there is a gap and there is only 1 queue state in the new slot entries then there must have at least that many slots since the old slot entry must have been purged.  If there is more than 1 queue state then the block chain is still growing check the smallest max size and there should be at least that many slots. \\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSizeWithGap}{$Slots_a$}    \r
+    %\State $QSSet \gets $ \Call{GetQStateWithSeq}{$Slots_a$}\r
+    %\State $\tuple{seq_{max}, size_{max}} \gets \tuple{seq, size}$ such that $\tuple{seq, size} \in QSSet \land \forall \tuple{seq', size'} \in QSSet, size \geq size'$ \Comment{Get largest size}\r
+    %\State $\tuple{seq_{min}, size_{min}} \gets \tuple{seq, size}$ such that $\tuple{seq, size} \in QSSet \land \forall \tuple{seq', size'} \in QSSet , size \leq size'$ \Comment{Get smallest size}\r
+    \r
+    \State $QSet \gets $ \Call{GetQState}{$Slots_a$}\r
+    \State $size_{max} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \geq size'$ \r
+    \State $size_{min} \gets size$ such that $size \in QSet \land \forall size' \in QSet, size \leq size'$     \r
+    \State $Slots_{oldmax} \gets \emptyset$\\\r
+\r
+    \r
+    \LeftComment{If only 1 max size then we must have all the slots for that size}\r
+    \If{$(|QSSet| = 1) \land (|Slots_a| \neq size_{max})$}\r
+        \State \Call{Error}{"Missing Slots"}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{We definitely have all the slots}\r
+    \If$|Slots_a| = size_{max}$\r
+        \State \Return{} \Comment{We have all the slots}\r
+    \EndIf\\\r
+    \r
+    \LeftComment{We must have at least this many slots}\r
+    \If$|Slots_a| < size_{min}$\r
+        \State \Call{Error}{"Missing Slots"}\r
+    \EndIf\\\r
+\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check Size\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check Size:}\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckSize}{$Slots_a$}    \r
+    \State $\tuple{seq_{old_{max}}, Dat_{old_{max}}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $\tuple{seq_{new_{max}}, Dat_{new_{max}}} \gets $ \Call{MinSlot}{$Slots_a$}\\\r
+    \r
+    \If{$(seq_{old_{max}} + 1) = seq_{new_{max}}$}\r
+        \LeftComment{No Gap so cannot say anything about the size}\r
+        \State \Return{} \r
+    \Else \r
+        \LeftComment{Has a gap so we need to do checks}\r
+        \State \Call{CheckSizeWithGap}{$Slots_a$}\r
+    \EndIf\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% % Initialize the expected size of the block chain\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Initialize the expected size of the block chain:}\\\r
+% Initialize the expected size of the block chain based on the size at the server.\\\r
+% \begin{algorithmic}[1]\r
+% \Function{InitExpSize}{$seq_a$}\r
+%     \State $startingsize \gets 0$\\\r
+\r
+%     \If{$seq_a < max\_size$} \Comment{Check whether saves slots are full on server}\r
+%         \State $startingsize \gets seq_a$\r
+%     \Else\r
+%         \State $startingsize \gets max\_size$\r
+%     \EndIf\\\r
+    \r
+%     \State \Return{$startingsize$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+% % Update the expected size of the block chain\r
+% \noindent\fbox{%\r
+% \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+% \textbf{Update the expected size of the block chain:}\\\r
+% Update the expected size of the block chain.\\\r
+% \begin{algorithmic}[1]\r
+% \Function{UpdateExpSize}{$size_a$}\r
+%     \State $size_a \gets size_a + 1$\\\r
+    \r
+%     \If{$size_a > max\_size$}\Comment{Expected size $\leq max\_size$}\r
+%         \State $ssize_a \gets max_\_size$\r
+%     \EndIf\\\r
+    \r
+%     \State \Return{$size_a$}\r
+% \EndFunction\r
+% \end{algorithmic}\r
+% \end{varwidth}% \r
+% }\r
+\r
+\r
+\r
+% Update Last Message\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Commit Data Entry:}\\\r
+Process a commit entry.  Updates the local copy of commits.\\\r
+\begin{algorithmic}[1]\r
+\Function{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, updateinglocal_a$}\r
+    \State $\tuple{id_{old}, seq_{old}} \gets \tuple{id', seq'}$ such that $\tuple{id', seq'} \in LastSlot \land id'=id$\\\r
+    \r
+    \If{$id_a = LOCAL\_ID$}\r
+        \If{$\lnot updateinglocal_a \land (seq_a \neq seq_{old})$}\r
+            \LeftComment{This client did not make any updates so its latest sequence number should not change}\r
+            \State \Call{Error}{"Mismatch on local machine sequence number"}\r
+        \EndIf\r
+    \Else\r
+        \If{$seq_{old} > seq_a$}\r
+            \State \Call{Error}{"Rollback on remote machine sequence number"}\r
+        \EndIf\r
+    \EndIf\\\r
+    \r
+    \State $LastSlot \gets LastSlot \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LastSlot, id=id_a\}$\r
+    \State $LastSlot \gets LastSlot \cup \{\tuple{id_a, seq_a}\}$\r
+    \r
+    \State \Return{$LstSlt_a \setminus \{\tuple{id, seq} | \tuple{id, seq} \in LstSlt_a, id=id_a\}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Commit Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Commit Data Entry:}\\\r
+Process a commit entry.  Updates the local copy of commits.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessCommit}{$commit_a$}\r
+    \State $\tuple{seq_{a_{trans}},KV_a} \gets commit_a$\r
+    \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CommittedKV \land \tuple{k',v'}\in KV_a \land k'=k\}$\r
+    \State $CommittedKV \gets (CommittedKV \setminus DKV) \cup KV_a$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Queue State Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Queue State Entry:}\\\r
+Process a queue state entry. Updates the max size of the block chain\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessQState}{$qstate_a$}\r
+    \State $\tuple{size_a} \gets qstate_a$\r
+    \State $max\_size \gets size_a$ \Comment{Update the max size we can have}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Collision Resolution Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Queue State Entry:}\\\r
+Process a collision resolution entry.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessColres}{$colres_a, NewSlots_a$}\r
+    \State $\tuple{id_a, seq_{a_{old}}, seq_{a_{new}}, isequal_a}$\r
+    \State $AllSlots \gets LocalSlots \cup NewSlots_a$\r
+    \State $index \gets seq_{a_{old}}$\\\r
+    \r
+    \While{$index <= seq_{a_{new}}$}\r
+        \State $slt \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
+        \r
+        \If{$\exists \tuple{seq' Dat'} \in AllSlots, seq' = index$}\r
+            \State $\tuple{seq, Dat} \gets \tuple{seq' Dat'}$ such that $\tuple{seq' Dat'} \in AllSlots \land seq'=index$\r
+            \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets Dat$\r
+            \If{$isequal_a \neq (id=id_a)$}\r
+                \State \Call{Error}{"Trying to insert rejected messages for slot"}\r
+            \EndIf\r
+        \EndIf\\\r
+        \State $index \gets index + 1$\r
+    \EndWhile\r
+    \r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process New Key Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process New Key Entry:}\\\r
+Process a queue state entry. Adds a key to the key arbitrator set\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessNewkey}{$newkey_a$}\r
+    \State $\tuple{seq_a, k_a, id_a} \gets newkey_a$\r
+    \State $Arbitrator \gets Arbitrator \cup \{\tuple{k_a,id_a}\}$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Process Process Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Process Data Entry:}\\\r
+Process the data entry based on what kind of entry it is.\\\r
+\begin{algorithmic}[1]\r
+\Function{ProcessDatEntry}{$slot_a, NewSlots_a,LstSlt_a$}\r
+    \If{$datentry_a$ is a $commit$}\r
+        \State \Call{ProcessCommit}{$dataentry_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $abort$}\r
+        \LeftComment{Do Nothing in this case}\r
+        \r
+    \ElsIf{$datentry_a$ is a $trans$}\r
+        \LeftComment{Do Nothing in this case}\r
+    \r
+    \ElsIf{$datentry_a$ is a $lastmsg$}\r
+        \State $\tuple{seq_a, id_a} \gets dataentry_a$\r
+        \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_a, id_a, LstSlt_a, false$}\r
+    \r
+    \ElsIf{$datentry_a$ is a $colres$}\r
+        \State \Call{ProcessColres}{$dataentry_a, NewSlots_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $qstate$}\r
+        \State \Call{ProcessQState}{$dataentry_a$}\r
+        \r
+    \ElsIf{$datentry_a$ is a $newkey$}\r
+        \State \Call{ProcessNewkey}{$dataentry_a$}\r
+        \r
+    \Else\r
+        \State \Call{Error}{"Unknown data entry type."}\r
+    \EndIf\r
+    \r
+    \State \Return{$LstSlt_a$}    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Delete Local Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Delete Local Slots:}\\\r
+Deletes local slots that are deleted at the server.  This keeps the size of the local block chain bounded.\\\r
+\begin{algorithmic}[1]\r
+\Function{DeleteLocalSlots}{$ $}\r
+    \State $\tuple{seq_{max}, Dat_{max}} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $seq_{min} \gets seq_{max} - max\_size$ \Comment{Min sequence number we should keep}\r
+    \State $LSDelete \gets \emptyset$\r
+        \r
+    \If{$|LocalSlots| \leq max\_size$}\r
+        \State \Return{} \Comment{Nothing to delete}\r
+    \EndIf\\\r
+    \r
+    \State $LSDelete \gets \{\tuple{seq', Dat'}|\tuple{seq', Dat'} \in LocalSlots, seq' > seq_{min}\}$\r
+    \State $LocalSlots \gets LocalSlots \setminus LSDelete$    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create Speculative KV\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create Speculative KV:}\\\r
+Speculates on what the most recent key value pairs will be based on the latest committed key value pairs and the uncommitted transactions.\\\r
+\begin{algorithmic}[1]\r
+\Function{SpeculateKV}{$ $}\r
+    \State $AllTrans \gets$ \Call{GetTrans}{}\r
+    \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
+    \State $CurrKV \gets CommittedKV$\r
+    \State $DKV \gets \emptyset$\r
+\r
+    \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
+        \If{\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
+            \State $CurrKV \gets (CurrKV \setminus DKV) \cup KV_t$\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State \Return{$CurrKV$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Validate and Update \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Validate Update:}\\\r
+Validate the block chain and insert into the local block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{ValidateUpdate}{$NewSlots_a, updatinglocal_a$}\r
+    \State $\tuple{seq_{oldest}, Dat_{oldest}} \gets$ \Call{MinSlot}{$NewSlots_a$}\r
+    \State $\tuple{seq_{newest}, Dat_{newest}} \gets$ \Call{MaxSlot}{$NewSlots_a$}\r
+    \State $\tuple{seq_{local}, Dat_{local}} \gets$ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $LastSlotTmp \gets LastSlot$\\\r
+    %\State $currsize \gets $\Call{InitExpSize}{$seq_{oldest}$}\\\r
+    \r
+    \State \Call{CheckSlotsHmacAndSeq}{$NewSlots_a$} \Comment{Check all the HMACs}\r
+    \State \Call{CheckHmacChain}{$NewSlots_a$} \Comment{Check HMAC Chain} \r
+    \State \Call{CheckOldSlots}{$NewSlots_a$} \Comment{Check if new slots are actually old slots} \r
+    \State \Call{CheckSize}{$NewSlots_a$} \Comment{Check if the size is correct}\\\r
+    \r
+    \ForAll{$slot_a \in NewSlots_a$ in order of sequence number}\r
+        \If{$slot_a \in LocalSlots$} \Comment{Client already has this slot}\r
+            \State $NewSlots_a \gets NewSlots_a \setminus \{slot_a\}$\r
+            \State Continue\r
+        \EndIf\\\r
+    \r
+        \State $\tuple{seq_{a_1}, \tuple{seq_{a_2},id_a,DE_a,hmac_{a_p},hmac_{a_c}}} \gets slot_a$\r
+        \State $LstSlt_a \gets$ \Call{UpdateLastMessage}{$seq_{a_1}, id_a, LstSlt_a, updatinglocal_a$}\\\r
+        \r
+        \ForAll{$de_a \in DE_a$} \Comment{Process each data entry}\r
+            \State $LstSlt_a \gets $ \Call{ProccessDatEntry}{$de_a, NewSlots_a,LstSlt_a$}\r
+        \EndFor\\\r
+    \r
+        %\State $currsize \gets $ \Call{UpdateExpSize}{$currsize$}\\\r
+        \State $LocalSlots \gets LocalSlots \cup \{slot_a\}$ \Comment{Add to local Chain}\r
+    \EndFor\\\r
+    \r
+    \If{$seq_{oldest} > (seq_{local} +1) \land LastSlotTmp \neq \emptyset$}\r
+        \LeftComment{There was a gap so there should be a complete set of information on each previously seen client}\r
+        \State \Call{Error}{"Missing records for machines"}\r
+    \EndIf\\\r
+    \r
+    \State \Call{DeleteLocalSlots}{ } \Comment{Delete old slots from local}\r
+    \State $SpeculatedKV \gets $\Call{SpeculateKV}{ } \Comment{Speculate on what will be latest KV set}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Decrypt Validate Insert Slots\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Decrypt Validate Insert Slots:}\\\r
+Decrypts slots, validates (checks for malicious activity) slots and inserts the slots into the local block chain.\\\r
+\begin{algorithmic}[1]\r
+\Function{DecryptValidateInsert}{$NewSlots_a, updatinglocal_a$}\r
+    \State $DecryptedSlots \gets \emptyset$\r
+    \State $DDat \gets NULL$\\\r
+    \r
+    \ForAll{$\tuple{seq', EDat'} \in NewSlots_a$}\r
+        \State $DDat \gets $ \Call{Decrypt}{$EDat'$}\r
+        \State $DecryptedSlots \gets DecryptedSlots \cup \tuple{seq',DDat}$\r
+    \EndFor\\\r
+    \State \Call{ValidateUpdate}{$DecryptedSlots, updatinglocal_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Check and Create Last Message Data Entry\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Check and Create Last Message Data Entry:}\\\r
+Check if a last message entry needs to be created for this slot and if so create it.  The check is done by checking if there are any newer slots with the same id or if there is already a last message slot with a newer sequence number\\\r
+\begin{algorithmic}[1]\r
+\Function{CheckCreateLastMsgEntry}{$seq_a, id_a$}\r
+    \State $AllLastMsg \gets$ \Call{GetLastMsg}{}\\\r
+    \r
+    \LeftComment{Already Has one}\r
+    \If{$\exists  \tuple{seq', id'} \in AllLastMsg, id_a=id' \land seq'=seq_a$}\r
+        \State \Return{$\{\}$}\\\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Not latest slot from that client}\r
+    \If{$\exists  \tuple{seq_1', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \in LocalSlots, id_a=id' \land seq_1'>seq_a$}\r
+        \State \Return{$\{\}$}\\\r
+    \EndIf\\\r
+    \r
+    \r
+    \State \Return{$\{\tuple{seq_a, id_a}\}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Mandatory Rescue\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Mandatory Rescue:}\\\r
+This rescue is mandatory before any types of data entries (excpet queue states) can be placed into the data entry section of the new slot.  Returns the data entry Set or null if the first slot could not be cleared (the live data in that slot could not fit in this current slot). \\\r
+\begin{algorithmic}[1]\r
+\Function{MandatoryRescue}{$DE_a$}\r
+    \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
+    \State $cseq \gets smallestseq$\\\r
+    \r
+    \LeftComment{Check the least slots to rescue and live entries}\r
+    \While{$cseq < (smallestseq + DEAD\_SLOT\_COUNT)$}\r
+        \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
+        \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\r
+        \State $DE' \gets DE' \cup$ \Call{CheckCreateLastMsgEntry}{$seq', id'$} \Comment{Get the last message too if we need it}\\\r
+        \r
+        \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
+            \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
+                \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\r
+                \If{\Call{DEHasSpace}{$DE_a, de$}}\r
+                    \State $DE_a \gets DE_a \cup de$ \Comment{Had enough space to add it}\r
+                \ElsIf{$currentseq = smallestseq$}\r
+                    \State \Return{$NULL$}\r
+                \Else\r
+                    \State \Return{$DE_a$}\r
+                \EndIf\r
+            \EndIf\r
+        \EndFor\\\r
+        \r
+        \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
+    \EndWhile\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Optional Rescue\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Optional Rescue:}\\\r
+This rescue is not mandatory.  This is trying to fill the remaining portion of the slot with rescued data so that no space is wasted. If we encounter a data entry that does not fit move on to the next, maybe that one will fit.  Do this until we skipped too many live data entries\\\r
+\begin{algorithmic}[1]\r
+\Function{OptionalRescue}{$DE_a$}\r
+    \State $smallestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \leq seq')$\r
+    \State $largestseq \gets seq$ such that $\tuple{seq, DE}\in LocalSlots \land (\forall \tuple{seq', DE'} \in LocalSlots, seq \geq seq')$\r
+\r
+    \State $numofskips \gets 0$\r
+    \State $cseq \gets smallestseq$\\\r
+    \r
+    \LeftComment{Check the least slots to rescue and live entries}\r
+    \While{$cseq < largestseq$}\r
+        \State $currentslot \gets s'$ such that $\tuple{s',DE'} \in LocalSlots \land s' = cseq$\r
+        \State $\tuple{seq', \tuple{seq_2',id',DE',hmac_p',hmac_c'}} \gets currentslot$\\\r
+        \r
+        \ForAll{$de \in DE'$} \Comment{Iterate over all the entries}\r
+            \If{\Call{CheckLive}{$de, cseq$}} \Comment{data entry is live}\r
+                \State $de \gets $ \Call{CreateRescuedEntry}{de} \Comment{Resize entry if needed}\\\r
+                \r
+                \If{$de \in DE_a$} \Comment{Already being rescued}\r
+                    \State Continue\r
+                \EndIf\\\r
+                \r
+                \If{\Call{DEHasSpace}{$DE_a, de$}}\r
+                    \State $DE_a \gets DE_a \cup de$ \Comment{Had enoug space to add it}\r
+                \ElsIf{$numofskips \geq MAX\_RESCUE\_SKIPS$}\r
+                    \State \Return{$DE_a$}\r
+                \Else\r
+                    $numofskips \gets numofskips +1$\r
+                \EndIf\r
+            \EndIf\r
+        \EndFor\\\r
+        \r
+        \State $cseq \gets cseq+1$ \Comment{Move onto the next slot}\r
+    \EndWhile\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Rejected Messages\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Rejected Messages:}\\\r
+\begin{algorithmic}[1]\r
+\Function{RejectedMessages}{$DE_a$}\r
+    \State $seq_{old} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \geq seq'$\r
+    \State $prev \gets -1$\\\r
+    \r
+    \r
+    \r
+    \If{$|RejectedSlotList| \geq REJECTED\_THRESH$}\r
+        \State $seq_{new} \gets seq$ such that $\tuple{seq} \in RejectedSlotList \land \forall \tuple{seq'} \in RejectedSlotList, seq \leq seq'$\\\r
+        \State $colres \gets $ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, seq_{new}, false$}    \r
+        \State \Return{$DE_a \cup \{colres\}$}\r
+    \EndIf\\\r
+    \r
+    \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
+        \If{$\exists \tuple{seq',Dat'} \in LocalSlots$}\r
+            \State Break\r
+        \EndIf\r
+        \State $prev \gets seq$\r
+    \EndFor\\\r
+    \r
+    \If{$prev \neq -1$}\r
+        \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq_{old}, prev, false$}\r
+    \EndIf\\\r
+    \r
+    \State $RejectedSlotList \gets \{\tuple{seq}| \tuple{seq} \in RejectedSlotList, seq > prev\}$\\\r
+    \r
+    \ForAll{$\tuple{seq} \in RejectedSlotList$ sorted by $seq$}\r
+        \State $DE_a \gets DE_a \cup$ \Call{CreateColRes}{$LOCAL\_ID, seq,seq, false$}\r
+    \EndFor\\\r
+    \r
+    \State \Return{$DE_a$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+\r
+% Arbitrate\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Arbitrate:}\\\r
+\begin{algorithmic}[1]\r
+\Function{Arbitrate}{$DE_a$}\r
+    \State $AllCommits \gets$ \Call{GetCommits}{}\r
+    \State $AllTrans \gets$ \Call{GetTrans}{}\r
+    \State $LiveCommits \gets \{c| c\in AllCommits, $\Call{CheckCommitLive}{$c$}$\}$\r
+    \State $LiveTrans \gets \{t| t\in AllTrans, $\Call{CheckTransLive}{$t$}$\}$\r
+    \State $KV \gets \emptyset$\r
+    \State $lastcomseq \gets -1$\r
+    \State $CurrKV \gets \emptyset$\r
+    \State $DKV \gets \emptyset$\r
+    \State $KVTmp \gets \emptyset$\\\r
+    \r
+    \LeftComment{Get all the latest commits}\r
+    \ForAll{$\tuple{seq_{trans}',KV'} \in LiveCommits$}\r
+        \State $CurrKV \gets CurrKV \cup KV'$\r
+    \EndFor\\\r
+    \r
+    \ForAll{$\tuple{seq_t, id_t, KV_t, Guard_t} \in LiveTrans$ ordered by $seq'$} \r
+        \If{\Call{GetArbitratorKV}{$KV_t$} $\neq LOCAL\_ID$}\r
+            \State Continue \Comment{Client not arbitrator for this transaction}\r
+        \EndIf\\\r
+    \r
+        \If{$\lnot$\Call{EvaluateGuard}{$Guard_t, CurrKV$}}\r
+            \State $abortde \gets $\Call{CreateAbort}{$seq_t, id_t$}\r
+            \LeftComment{No more space so we cant arbitrate any further}\r
+            \If($lnot$\Call{DeHasSpace}{$DE_a, abortde$})\r
+                \State \Return{$DE_a$}\r
+            \EndIf\r
+            \State $DE_a \gets DE_a \cup abortde$\r
+        \Else\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in KV \land \tuple{k',v'}\in KV_t \land k'=k\}$\r
+            \State $KVTmp \gets (KV \setminus DKV) \cup KV'$\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in CurrKV \land \tuple{k',v'}\in KVTmp \land k'=k\}$\r
+            \State $CurrKV \gets (CurrKV \setminus DKV) \cup KVTmp$\r
+            \State $commitde \gets $ \Call{CreateCommit}{$seq_t,KVTmp$}\r
+            \r
+            \If{$\lnot$ \Call{DeHasSpace}{$DE_a, commitde$}}\r
+                \If{$lastcomseq \neq -1$}\r
+                    \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
+                \EndIf\r
+                \State \Return{$DE_a$}\r
+            \Else\r
+                \State $KV \gets KVTmp$\r
+                \State $lastcomseq \gets seq_t$\r
+            \EndIf\r
+        \EndIf\r
+    \EndFor\r
+    \r
+    \State $DE_a \gets DE_a \cup$ \Call{CreateCommit}{$lastcomseq,KV$}\r
+    \State \Return{$DE_a$}\r
+\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Create New Slot\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Slot:}\\\r
+Create a slot and encrypt it.\\\r
+\begin{algorithmic}[1]\r
+\Function{CreateNewSlot}{$seq_a, DE_a$}\r
+    \State $\tuple{seq, SDE} \gets \tuple{seq', SDE'}$ such that $\tuple{seq', SDE'}\in LocalSlots \land (\forall \tuple{seq'', DE''} \in LocalSlots, seq' \geq seq'')$\r
+    \State $\tuple{seq,id,DE,hmac_p,hmac_c} \gets SDE$\\\r
+    \r
+    \State $newhmac \gets $ \Call{GenerateHmac}{$seq_a, LOCAL\_ID, DE_a, hmac_p$}\r
+    \State $newSDE \gets \tuple{seq,LOCAL\_ID,DE_a,hmac_c,newhmac}$\r
+    \State $encryptnewSDE \gets $\Call{Encrypt}{newSDE}\\\r
+    \r
+    \State \Return{$\tuple{seq_a, encryptnewSDE}$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Send Data to Server\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Send Data to Server:}\\\r
+Send the data to the server.  If this fails then new slots will be returned by the server.\\\r
+\begin{algorithmic}[1]\r
+\Function{SendToServer}{$seq_a, DE_a, newsize_a$}\r
+    \LeftComment{Make the slot and try to send to server}\r
+    \State $newslot \gets $ \Call{CreateNewSlot}{$seq_a, DE_a$}\r
+    \State $\tuple{success, newslots} \gets$ \Call{PutSlot}{$seq_a, newslot, newsize_a$}\\\r
+    \r
+    \If{$success$}\r
+        \State $RejectedSlotList \gets \emptyset$\r
+        \State \Return{$\tuple{true, \{newslot\}}$}\r
+    \Else\r
+        \If{$|newslots| = 0$}\r
+            \State \Call{Error}{"Server rejected but did not send any slots"}\r
+        \EndIf\r
+        \State $RejectedSlotList \gets RejectedSlotList \cup \{seq_a\}$\r
+        \State \Return{$\tuple{false, newslots}$}\r
+    \EndIf\\\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Try Insert Transaction\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Try Insert Transaction:}\\\r
+Try to insert a transaction into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
+\begin{algorithmic}[1]\r
+\Function{TryInsertTransaction}{$pendingtrans_a, forceresize$}\r
+    \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
+    \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
+    \State $newsize \gets 0$\r
+    \State $trans \gets$ \Call{CreateTrans}{$pendingtrans_a, seq$}\r
+    \State $transinserted \gets false$\r
+    \State $slotstoinsert \gets \emptyset$\\\r
+    \r
+    \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
+    \State $resize \gets resize \lor forceresize$\r
+    \If{$resize$}\r
+        \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
+        \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
+    \EndIf\\\r
+    \r
+    \If{$RejectedSlotList \neq \emptyset$}   \r
+        \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
+    \If{$DE = NULL$}\r
+        \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
+        \State \Return{\Call{TryInsertTransaction}{$trans_a, true$}}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
+    \r
+    \If{\Call{DEHasSpace}{$DE, trans$}} \Comment{transaction fits}\r
+        \State $DE \gets DE \cup trans$\r
+        \State $transinserted \gets true$\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Rescue data to fill slot data entry section}\r
+    \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
+    \r
+    \LeftComment{Send to server.}\r
+    \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
+    \r
+    \LeftComment{Insert the slots into the local bloakc chain}\r
+    \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
+    \r
+    \State \Return{$transinserted \land success$} \Comment{Return if  succeeded or not}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+% Try Insert New Key\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Try Insert New Key:}\\\r
+Try to insert a new key into the block chain. Does resizing, rescues and insertion of other data entry types as needed. \\\r
+\begin{algorithmic}[1]\r
+\Function{TryInsertNewKey}{$k_a, id_a, forceresize$}\r
+    \State $DE \gets \emptyset$ \Comment{The data entries for this slot}\r
+    \State $seq \gets $ \Call{GetNextSeq}{} \Comment{Get the sequence number for this slot}\r
+    \State $newsize \gets 0$\r
+    \r
+    \r
+    \State $newkey \gets$ \Call{CreateNewKey}{$k_a, id_a$}\r
+    \State $newkeyinserted \gets false$\r
+    \State $slotstoinsert \gets \emptyset$\\\r
+    \r
+    \State $resize \gets $ \Call{ShouldResize}{ } \Comment{Check if we should resize}\r
+    \State $resize \gets resize \lor forceresize$\r
+    \If{$resize$}\r
+        \State $newsize \gets$ \Call{CalcNewSize}{$max\_size$}\r
+        \State $DE \gets DE \cup \{$\Call{CreateQState}{$newsize$}$\}$\r
+    \EndIf\\\r
+    \r
+    \If{$RejectedSlotList \neq \emptyset$}   \r
+        \State $DE \gets$ \Call{RejectedMessages}{$DE$}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets$ \Call{MandatoryRescue}{$DE$} \Comment{Round 1 of rescue}\r
+    \If{$DE = NULL$}\r
+        \LeftComment{Data was going to fall off the end so try again with a forced resize}\r
+        \State \Return{\Call{TryInsertNewKey}{$k_a, id_a, true$}}\r
+    \EndIf\\\r
+    \r
+    \State $DE \gets $\Call{Arbitrate}{$DE$}\\\r
+    \r
+    \If{\Call{DEHasSpace}{$DE, newkey$}} \Comment{new key fits}\r
+        \State $DE \gets DE \cup newkey$\r
+        \State $newkeyinserted \gets true$\r
+    \EndIf\\\r
+    \r
+    \LeftComment{Rescue data to fill slot data entry section}\r
+    \State $DE \gets$ \Call{OptionalRescue}{$DE$}\\\r
+    \r
+    \LeftComment{Send to server.}\r
+    \State $\tuple{sendsuccess, newslots} \gets $ \Call{SendToServer}{$seq, DE, newsize$}\\\r
+    \r
+    \LeftComment{Insert the slots into the local block chain}\r
+    \State \Call{DecryptValidateInsert}{$newslots, true$}\\\r
+    \r
+    \State \Return{$newkeyinserted \land success$} \Comment{Return if  succeeded or not}\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+\r
+\subsection{Client Interfaces}\r
+\r
+% Put KV pair\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Put Key Value Pair:}\\\r
+Puts a key value pair into the key value pair buffer\\\r
+\begin{algorithmic}[1]\r
+\Function{PutKeyValue}{$k,v$}\r
+    \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
+    \r
+    \LeftComment{Check if KV already has a key value pair for the specified key}\r
+    \State $DSet \gets \{\tuple{k_1,v_1} | \tuple{k_1,v_1} \in KV \land k_1 = k\}$\\\r
+    \r
+    \If{$DSet \neq \emptyset$}\r
+        \State \Call{Error}{"Value for key already in most recent update"}\r
+    \EndIf\\\r
+        \r
+    \State $KV \gets KV \cup \{\tuple{k,v}\}$ \Comment{Add key value pair}\r
+    \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
+    \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get KV Pair Speculative\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get KV Pair Speculative:}\\\r
+Get the value for the key while speculating.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetValueSpeculate}{$k_a$}\r
+    %\State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
+    \r
+    \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in SpeculatedKV \land k = k_a$\r
+\State \Return{$v$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Update\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Update}\\\r
+Sync with the server and get all the latest slots.\\\r
+\begin{algorithmic}[1]\r
+\Function{Update}{$ $}\r
+    \State $\tuple{seq, Dat} \gets $ \Call{MaxSlot}{$LocalSlots$}\r
+    \State $NewSlots \gets$ \Call{GetSlots}{$seq$}\r
+    \State \Call{DecryptValidateInsert}{$NewSlots, false$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Get KV Pair Committed\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Get KV Pair Committed:}\\\r
+Get the value for the key which have been committed.\\\r
+\begin{algorithmic}[1]\r
+\Function{GetValueCommit}{$k_a$}\r
+    \State $\tuple{k,v} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} \in Committed \land k = k_a$\r
+    \State \Return{$v$}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Put guard condition\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Put Guard:}\\\r
+Puts a guard transaction into the key value update.  A guard is a key value with a logical operator ($lop$).\\\r
+\begin{algorithmic}[1]\r
+\Function{PutGuard}{$k,v, lop$}\r
+    \State $\tuple{seq, KV, Guard} \gets PendingTrans$\\\r
+    \r
+    \If{$\tuple{k,v, lop} \in Guard$}\r
+        \State \Return{} \Comment{Already have guard condition in update}\r
+    \EndIf\\\r
+    \r
+    \State $Guard \gets Guard \cup \{\tuple{k,v,lop}\}$\r
+    \State $PendingTrans \gets \tuple{seq, KV, Guard}$\r
+    \State \Call{CheckArbitrator}{$PendingTrans$} \Comment{Check that the transaction still valid}\r
+\EndFunction    \r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Transaction Start\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Transaction Start:}\\\r
+Starts a transaction.  Clears out the key value pair update buffer.\\\r
+\begin{algorithmic}[1]\r
+\Function{TransactionStart}{$ $}\r
+    % \LeftComment{Reset the key value update buffer}\r
+    % \State $KVUpdate \gets \tuple{\emptyset, \emptyset}$\r
+    \State $PendingTrans \gets NULL$\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+% Transaction Commit\r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{ Transaction Commit:}\\\r
+Commits the transaction into the block chain.  Keeps attempting to insert the transaction into the block chain until it succeeds.\\\r
+\begin{algorithmic}[1]\r
+\Function{Transaction Commit}{$ $}\r
+    \State $DKV \gets \emptyset$\r
+    \State $pt \gets NULL$\\\r
+    \r
+    \State $PendingTransQueue.$\Call{push}{$PendingTrans$}\\\r
+        \r
+    \While{\Call{HasConnectionToServer}{ } $\land PendingTransQueue \neq \emptyset$}\r
+        \State $pt \gets PendingTransQueue.$\Call{peak}{ }\\\r
+    \r
+        \If{\Call{TryInsertTransaction}{$pt, false$}}\r
+            \State $PendingTransQueue.$\Call{pop}{ }\r
+        \EndIf\r
+    \EndWhile\\    \r
+    \r
+    \LeftComment{Go Through local pending transactions and speculate}\r
+    \ForAll{$\tuple{KV, Guard} \in PendingTransQueue$}\r
+        \If{\Call{EvaluateGuard}{$Guard, SpeculatedKV$}}\r
+            \State $DKV \gets \{\tuple{k,v}| \tuple{k,v} \in SpeculatedKV \land \tuple{k',v'}\in KV \land k'=k\}$\r
+            \State $SpeculatedKV \gets (SpeculatedKV \setminus DKV) \cup KV$\r
+        \EndIf\r
+    \EndFor\r
+    \r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+%Create New Key \r
+\noindent\fbox{%\r
+\begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}\r
+\textbf{Create New Key:}\\\r
+Creates a new key and specifies which machine ID is the arbitrator. If there is already a new key entry in the block chain for this key name then do not insert into the chain, another client got there first. \\\r
+\begin{algorithmic}[1]\r
+\Function{Transaction Commit}{$k_a, id_a$}\r
+    \State $success \gets false$\\\r
+    \While{$\lnot success$}\r
+        \If{$\exists \tuple{k',id'} \in Arbitrator, k' = k_a$}\r
+            \State \Return{$false$} \Comment{Key already created}\r
+        \EndIf\\\r
+    \r
+        \State $success \gets$ \Call{TryInsertNewKey}{$k_a, id_a, false$}\r
+    \EndWhile\r
+    \r
+    \State \Return{$true$} \Comment{If got here then insertion was correct}\r
+\EndFunction\r
+\end{algorithmic}\r
+\end{varwidth}% \r
+}\r
+\r
+\r
+\end{document}\r
diff --git a/version2/backup/doc/makefile b/version2/backup/doc/makefile
new file mode 100644 (file)
index 0000000..cff4a15
--- /dev/null
@@ -0,0 +1,8 @@
+LATEX := pdflatex -halt-on-error
+
+default:
+       $(LATEX) iotcloud.tex
+
+clean:
+       rm -f *.dvi *.log *.aux *.blg *.bbl *~
+       rm -f iotcloud.ps iotcloud.pdf 
diff --git a/version2/backup/src/java/.dir-locals.el b/version2/backup/src/java/.dir-locals.el
new file mode 100644 (file)
index 0000000..e166a2e
--- /dev/null
@@ -0,0 +1,2 @@
+((nil . ((indent-tabs-mode . t))))
+
diff --git a/version2/backup/src/java/iotcloud/Abort.java b/version2/backup/src/java/iotcloud/Abort.java
new file mode 100644 (file)
index 0000000..327ce33
--- /dev/null
@@ -0,0 +1,63 @@
+package iotcloud;
+
+import java.nio.ByteBuffer;
+
+/**
+ * This Entry records the abort sent by a given machine.
+ * @author Ali Younis <ayounis@uci.edu>
+ * @version 1.0
+ */
+
+
+class Abort extends Entry {
+       private long seqnumtrans;
+       private long machineid;
+       private long transarbitrator;
+
+
+       public Abort(Slot slot, long _seqnumtrans,  long _machineid, long _transarbitrator) {
+               super(slot);
+               seqnumtrans = _seqnumtrans;
+               machineid = _machineid;
+               transarbitrator = _transarbitrator;
+       }
+
+       public long getMachineID() {
+               return machineid;
+       }
+
+       public long getTransSequenceNumber() {
+               return seqnumtrans;
+       }
+
+
+       public long getTransArbitrator() {
+               return transarbitrator;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               long seqnumtrans = bb.getLong();
+               long machineid = bb.getLong();
+               long transarbitrator = bb.getLong();
+               return new Abort(slot, seqnumtrans,  machineid, transarbitrator);
+       }
+
+       public void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeAbort);
+               bb.putLong(seqnumtrans);
+               bb.putLong(machineid);
+               bb.putLong(transarbitrator);
+       }
+
+       public int getSize() {
+               return (3 * Long.BYTES) + Byte.BYTES;
+       }
+
+       public byte getType() {
+               return Entry.TypeAbort;
+       }
+
+       public Entry getCopy(Slot s) {
+               return new Abort(s, seqnumtrans, machineid, transarbitrator);
+       }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/CloudComm.java b/version2/backup/src/java/iotcloud/CloudComm.java
new file mode 100644 (file)
index 0000000..6f548af
--- /dev/null
@@ -0,0 +1,261 @@
+package iotcloud;
+import java.io.*;
+import java.net.*;
+import java.util.Arrays;
+import javax.crypto.*;
+import javax.crypto.spec.*;
+import java.security.SecureRandom;
+
+/**
+ * This class provides a communication API to the webserver.  It also
+ * validates the HMACs on the slots and handles encryption.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+
+class CloudComm {
+       String hostname;
+       String baseurl;
+       Cipher encryptCipher;
+       Cipher decryptCipher;
+       Mac mac;
+       String password;
+       SecureRandom random;
+       static final int SALT_SIZE = 8;
+       static final int TIMEOUT_MILLIS = 100;
+       byte salt[];
+       Table table;
+
+       /**
+        * Empty Constructor needed for child class.
+        */
+       CloudComm() {
+       }
+
+       /**
+        * Constructor for actual use. Takes in the url and password.
+        */
+       CloudComm(Table _table, String _hostname, String _baseurl, String _password) {
+               this.table = _table;
+               this.hostname = _hostname;
+               this.baseurl = _baseurl;
+               this.password = _password;
+               this.random = new SecureRandom();
+       }
+
+       /**
+        * Generates Key from password.
+        */
+       private SecretKeySpec initKey() {
+               try {
+                       PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
+                       SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
+                       return new SecretKeySpec(tmpkey.getEncoded(), "AES");
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed generating key.");
+               }
+       }
+
+       /**
+        * Inits the HMAC generator.
+        */
+       private void initCrypt() {
+               try {
+                       SecretKeySpec key = initKey();
+                       password = null; // drop password
+                       mac = Mac.getInstance("HmacSHA256");
+                       mac.init(key);
+                       encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+                       encryptCipher.init(Cipher.ENCRYPT_MODE, key);
+                       decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+                       decryptCipher.init(Cipher.DECRYPT_MODE, key);
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed To Initialize Ciphers");
+               }
+       }
+
+       /*
+        * Builds the URL for the given request.
+        */
+       private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
+               String reqstring = isput ? "req=putslot" : "req=getslot";
+               String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
+               if (maxentries != 0)
+                       urlstr += "&max=" + maxentries;
+               return new URL(urlstr);
+       }
+
+       public void setSalt() throws ServerException {
+               try {
+                       byte[] saltTmp = new byte[SALT_SIZE];
+                       random.nextBytes(saltTmp);
+                       URL url = new URL(baseurl + "?req=setsalt");
+                       URLConnection con = url.openConnection();
+                       HttpURLConnection http = (HttpURLConnection) con;
+                       http.setRequestMethod("POST");
+                       http.setFixedLengthStreamingMode(saltTmp.length);
+                       http.setDoOutput(true);
+                       http.setConnectTimeout(TIMEOUT_MILLIS);
+                       http.connect();
+                       OutputStream os = http.getOutputStream();
+                       os.write(saltTmp);
+                       int responsecode = http.getResponseCode();
+                       if (responsecode != HttpURLConnection.HTTP_OK) {
+                               // TODO: Remove this print
+                               // System.out.println(responsecode);
+                               throw new Error("Invalid response");
+                       }
+
+                       salt = saltTmp;
+               } catch (Exception e) {
+                       throw new ServerException("Failed setting salt");
+               }
+               initCrypt();
+       }
+
+       private void getSalt() throws Exception {
+               URL url = new URL(baseurl + "?req=getsalt");
+               URLConnection con = url.openConnection();
+               HttpURLConnection http = (HttpURLConnection) con;
+               http.setRequestMethod("POST");
+               http.connect();
+
+               InputStream is = http.getInputStream();
+               DataInputStream dis = new DataInputStream(is);
+               int salt_length = dis.readInt();
+               byte [] tmp = new byte[salt_length];
+               dis.readFully(tmp);
+               salt = tmp;
+       }
+
+       /*
+        * API for putting a slot into the queue.  Returns null on success.
+        * On failure, the server will send slots with newer sequence
+        * numbers.
+        */
+
+       Slot[] putSlot(Slot slot, int max) throws ServerException {
+               try {
+                       if (salt == null) {
+                               getSalt();
+                               initCrypt();
+                       }
+
+                       long sequencenumber = slot.getSequenceNumber();
+                       byte[] bytes = slot.encode(mac);
+                       bytes = encryptCipher.doFinal(bytes);
+
+
+                       URL url = buildRequest(true, sequencenumber, max);
+                       URLConnection con = url.openConnection();
+                       HttpURLConnection http = (HttpURLConnection) con;
+
+                       http.setRequestMethod("POST");
+                       http.setFixedLengthStreamingMode(bytes.length);
+                       http.setDoOutput(true);
+                       http.setConnectTimeout(TIMEOUT_MILLIS);
+                       // http.setReadTimeout(TIMEOUT_MILLIS);
+                       http.connect();
+
+                       OutputStream os = http.getOutputStream();
+                       os.write(bytes);
+                       os.flush();
+
+
+                       InputStream is = http.getInputStream();
+                       DataInputStream dis = new DataInputStream(is);
+                       byte[] resptype = new byte[7];
+                       dis.readFully(resptype);
+
+                       if (Arrays.equals(resptype, "getslot".getBytes()))
+                               return processSlots(dis);
+                       else if (Arrays.equals(resptype, "putslot".getBytes()))
+                               return null;
+                       else
+                               throw new Error("Bad response to putslot");
+
+               } catch (Exception e) {
+                       throw new ServerException("putSlot failed");
+               }
+       }
+
+
+       /**
+        * Request the server to send all slots with the given
+        * sequencenumber or newer.
+        */
+       Slot[] getSlots(long sequencenumber) throws ServerException {
+               try {
+                       if (salt == null) {
+                               getSalt();
+                               initCrypt();
+                       }
+
+                       URL url = buildRequest(false, sequencenumber, 0);
+                       URLConnection con = url.openConnection();
+                       HttpURLConnection http = (HttpURLConnection) con;
+                       http.setRequestMethod("POST");
+                       http.setConnectTimeout(TIMEOUT_MILLIS);
+                       // http.setReadTimeout(TIMEOUT_MILLIS);
+                       http.connect();
+                       InputStream is = http.getInputStream();
+                       DataInputStream dis = new DataInputStream(is);
+
+                       int responsecode = http.getResponseCode();
+                       if (responsecode != HttpURLConnection.HTTP_OK) {
+                               // TODO: Remove this print
+                               // System.out.println("Code:  " + responsecode);
+                               throw new ServerException("getSlots failed");
+                       }
+
+                       byte[] resptype = new byte[7];
+                       dis.readFully(resptype);
+                       if (!Arrays.equals(resptype, "getslot".getBytes()))
+                               throw new Error("Bad Response: " + new String(resptype));
+                       else
+                               return processSlots(dis);
+               } catch (Exception e) {
+                       // e.printStackTrace();
+                       throw new ServerException("getSlots failed");
+               }
+       }
+
+       public boolean hasConnection() {
+               try {
+                       InetAddress address = InetAddress.getByName(hostname);
+                       return address.isReachable(TIMEOUT_MILLIS);
+               } catch (Exception e) {
+                       return false;
+               }
+       }
+
+       /**
+        * Method that actually handles building Slot objects from the
+        * server response.  Shared by both putSlot and getSlots.
+        */
+       private Slot[] processSlots(DataInputStream dis) throws Exception {
+               int numberofslots = dis.readInt();
+               int[] sizesofslots = new int[numberofslots];
+               Slot[] slots = new Slot[numberofslots];
+               for (int i = 0; i < numberofslots; i++)
+                       sizesofslots[i] = dis.readInt();
+
+               for (int i = 0; i < numberofslots; i++) {
+                       byte[] data = new byte[sizesofslots[i]];
+                       dis.readFully(data);
+
+                       data = decryptCipher.doFinal(data);
+
+                       slots[i] = Slot.decode(table, data, mac);
+               }
+               dis.close();
+               return slots;
+       }
+
+
+
+
+}
diff --git a/version2/backup/src/java/iotcloud/Commit.java b/version2/backup/src/java/iotcloud/Commit.java
new file mode 100644 (file)
index 0000000..fb52e67
--- /dev/null
@@ -0,0 +1,125 @@
+package iotcloud;
+
+import java.nio.ByteBuffer;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Iterator;
+
+/**
+ * This Entry records the commit of a transaction.
+ * @author Ali Younis <ayounis@uci.edu>
+ * @version 1.0
+ */
+
+
+class Commit extends Entry {
+       private long seqnumtrans;
+       private long seqnumcommit;
+       private long transarbitrator;
+
+       private Set<KeyValue> keyValueUpdateSet = null;
+
+
+       public Commit(Slot slot, long _seqnumtrans,  long _seqnumcommit, long _transarbitrator, Set<KeyValue> _keyValueUpdateSet) {
+               super(slot);
+               seqnumtrans = _seqnumtrans;
+               seqnumcommit = _seqnumcommit;
+               transarbitrator = _transarbitrator;
+
+               keyValueUpdateSet = new HashSet<KeyValue>();
+
+               for (KeyValue kv : _keyValueUpdateSet) {
+                       KeyValue kvCopy = kv.getCopy();
+                       keyValueUpdateSet.add(kvCopy);
+               }
+       }
+
+       public long getTransSequenceNumber() {
+               return seqnumtrans;
+       }
+       public long getSequenceNumber() {
+               return seqnumcommit;
+       }
+
+       public long getTransArbitrator() {
+               return transarbitrator;
+       }
+
+       public Set<KeyValue> getkeyValueUpdateSet() {
+               return keyValueUpdateSet;
+       }
+
+       public byte getType() {
+               return Entry.TypeCommit;
+       }
+
+       public int getSize() {
+               int size = 3 * Long.BYTES + Byte.BYTES; // seq id, entry type
+               size += Integer.BYTES; // number of KV's
+
+               // Size of each KV
+               for (KeyValue kv : keyValueUpdateSet) {
+                       size += kv.getSize();
+               }
+
+               return size;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               long seqnumtrans = bb.getLong();
+               long seqnumcommit = bb.getLong();
+               long transarbitrator = bb.getLong();
+               int numberOfKeys = bb.getInt();
+
+               Set<KeyValue> kvSet = new HashSet<KeyValue>();
+               for (int i = 0; i < numberOfKeys; i++) {
+                       KeyValue kv = KeyValue.decode(bb);
+                       kvSet.add(kv);
+               }
+
+               return new Commit(slot, seqnumtrans, seqnumcommit, transarbitrator, kvSet);
+       }
+
+       public void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeCommit);
+               bb.putLong(seqnumtrans);
+               bb.putLong(seqnumcommit);
+               bb.putLong(transarbitrator);
+
+               bb.putInt(keyValueUpdateSet.size());
+
+               for (KeyValue kv : keyValueUpdateSet) {
+                       kv.encode(bb);
+               }
+       }
+
+       public Entry getCopy(Slot s) {
+               return new Commit(s, seqnumtrans, seqnumcommit, transarbitrator, keyValueUpdateSet);
+       }
+
+       public Set<KeyValue> updateLiveKeys(Set<KeyValue> kvSet) {
+
+               if (!this.isLive())
+                       return new HashSet<KeyValue>();
+
+               Set<KeyValue> toDelete = new HashSet<KeyValue>();
+
+               for (KeyValue kv1 : kvSet) {
+                       for (Iterator<KeyValue> i = keyValueUpdateSet.iterator(); i.hasNext();) {
+                               KeyValue kv2 = i.next();
+
+                               if (kv1.getKey().equals(kv2.getKey())) {
+                                       toDelete.add(kv2);
+                                       i.remove();
+                                       break;
+                               }
+                       }
+               }
+
+               if (keyValueUpdateSet.size() == 0) {
+                       this.setDead();
+               }
+
+               return toDelete;
+       }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/Entry.java b/version2/backup/src/java/iotcloud/Entry.java
new file mode 100644 (file)
index 0000000..8395dec
--- /dev/null
@@ -0,0 +1,119 @@
+package iotcloud;
+import java.nio.ByteBuffer;
+
+/**
+ * Generic class that wraps all the different types of information
+ * that can be stored in a Slot.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+abstract class Entry implements Liveness {
+
+
+       static final byte TypeCommit = 1;
+       static final byte TypeAbort = 2;
+       static final byte TypeTransaction = 3;
+       static final byte TypeNewKey = 4;
+       static final byte TypeLastMessage = 5;
+       static final byte TypeRejectedMessage = 6;
+       static final byte TypeTableStatus = 7;
+
+
+
+       /* Records whether the information is still live or has been
+                superceded by a newer update.  */
+
+       private boolean islive = true;
+       private Slot parentslot;
+
+       public Entry(Slot _parentslot) {
+               parentslot = _parentslot;
+       }
+
+       /**
+        * Static method for decoding byte array into Entry objects.  First
+        * byte tells the type of entry.
+        */
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               byte type = bb.get();
+               switch (type) {
+
+               case TypeCommit:
+                       return Commit.decode(slot, bb);
+
+               case TypeAbort:
+                       return Abort.decode(slot, bb);
+
+               case TypeTransaction:
+                       return Transaction.decode(slot, bb);
+
+               case TypeNewKey:
+                       return NewKey.decode(slot, bb);
+
+               case TypeLastMessage:
+                       return LastMessage.decode(slot, bb);
+
+               case TypeRejectedMessage:
+                       return RejectedMessage.decode(slot, bb);
+
+               case TypeTableStatus:
+                       return TableStatus.decode(slot, bb);
+
+               default:
+                       throw new Error("Unrecognized Entry Type: " + type);
+               }
+       }
+
+       /**
+        * Returns true if the Entry object is still live.
+        */
+
+       public boolean isLive() {
+               return islive;
+       }
+
+       /**
+        * Flags the entry object as dead.  Also decrements the live count
+        * of the parent slot.
+        */
+
+       public void setDead() {
+
+               if (!islive ) {
+                       return; // already dead
+               }
+
+               islive = false;
+
+               if (parentslot != null) {
+                       parentslot.decrementLiveCount();
+               }
+       }
+
+       /**
+        * Serializes the Entry object into the byte buffer.
+        */
+
+       abstract void encode(ByteBuffer bb);
+
+       /**
+        * Returns the size in bytes the entry object will take in the byte
+        * array.
+        */
+
+       abstract int getSize();
+
+       /**
+        * Returns a byte encoding the type of the entry object.
+        */
+
+       abstract byte getType();
+
+       /**
+        * Returns a copy of the Entry that can be added to a different slot.
+        */
+       abstract Entry getCopy(Slot s);
+
+}
diff --git a/version2/backup/src/java/iotcloud/Guard.java_backup b/version2/backup/src/java/iotcloud/Guard.java_backup
new file mode 100644 (file)
index 0000000..f768b7f
--- /dev/null
@@ -0,0 +1,163 @@
+package iotcloud;
+
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collection;
+import java.util.List;
+import java.util.ArrayList;
+
+import java.nio.ByteBuffer;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import java.lang.NullPointerException;
+
+
+class Guard {
+
+    static final byte Equal = 1;
+    static final byte NotEqual = 2;
+    private IoTString booleanExpression;
+    private List<KeyValue> keyValsNeeded = null;
+
+    public Guard() {
+        booleanExpression = null;
+    }
+
+    public Guard(IoTString _booleanExpression) {
+        booleanExpression = _booleanExpression;
+    }
+
+    /**
+     * Create an equality expression for a key value.
+     *
+     */
+    public static String createExpression(IoTString keyName, IoTString keyValue, byte op) {
+        if (op == Equal) {
+            return keyName.toString() + "=='" + keyValue.toString() + "'";
+        } else if (op == NotEqual) {
+            return keyName.toString() + "!='" + keyValue.toString() + "'";
+        }
+
+        // Unrecognized op
+        return null;
+    }
+
+    /**
+     * Add a boolean expression to the guard.
+     *
+     */
+    public void setGuardExpression(String expr) {
+        booleanExpression = new IoTString(expr);
+    }
+
+    /**
+     * Evaluate the guard expression for a given set of key value pairs.
+     *
+     */
+    public boolean evaluate(Collection<KeyValue> kvSet) throws ScriptException, NullPointerException {
+
+        // There are no conditions to evaluate
+        if (booleanExpression == null) {
+            return true;
+        }
+
+        ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
+
+        if (keyValsNeeded == null) {
+            keyValsNeeded = new ArrayList<KeyValue>();
+
+            String booleanExprString = booleanExpression.toString();
+            for (KeyValue kv : kvSet) {
+                if (booleanExprString.contains(kv.getKey().toString())) {
+                    keyValsNeeded.add(kv);
+                }
+            }
+        }
+
+        // All the current key value pairs that we need to evaluate the condition
+        // String[] variables = new String[kvSet.size()];
+
+        // Fill the variables array
+        // int i = 0;
+        // for (KeyValue kv : kvSet) {
+        // for (KeyValue kv : keyValsNeeded) {
+        //     variables[i] = kv.getKey() + " ='" + kv.getValue() + "'";
+        //     i++;
+        // }
+
+        String varEval = "";
+        for (KeyValue kv : keyValsNeeded) {
+            varEval += "var " + kv.getKey() + " ='" + kv.getValue() + "'; \n";
+        }
+
+        varEval += booleanExpression.toString();
+
+        // Prep the evaluation engine (script engine)
+
+        // for (String s : variables) {
+        //     engine.eval(s);
+        // }
+        // engine.eval(varEval);
+
+
+
+        // boolean engineEval = (Boolean)engine.eval(booleanExpression.toString());
+        boolean engineEval = false;
+
+        try {
+            engineEval = (Boolean)engine.eval(varEval);
+        } catch (Exception e) {
+            // If there was an error then the script evaluated to false 
+            engineEval = false;
+        }
+
+        // Evaluate the guard condition
+        // return 1 == (Integer)engine.eval(booleanExpression.toString());
+        return engineEval;
+    }
+
+    /**
+     * Get the size of the guard condition
+     *
+     */
+    public int getSize() {
+
+        if (booleanExpression == null) {
+            return Integer.BYTES;
+        }
+
+        return Integer.BYTES + booleanExpression.length();
+    }
+
+    public void encode(ByteBuffer bb) {
+        if (booleanExpression == null) {
+            bb.putInt(0);
+        } else {
+            bb.putInt(booleanExpression.length());
+            bb.put(booleanExpression.internalBytes());
+        }
+    }
+
+    static Guard decode(ByteBuffer bb) {
+        int exprLength = bb.getInt();
+
+        if (exprLength != 0) {
+            byte[] expr = new byte[exprLength];
+            bb.get(expr);
+            return new Guard(IoTString.shallow(expr));
+        }
+        return new Guard(null);
+    }
+
+    public Guard getCopy() {
+
+        if (booleanExpression == null) {
+            return new Guard(null);
+        }
+
+        return new Guard(IoTString.shallow(booleanExpression.internalBytes()));
+    }
+
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/IoTString.java b/version2/backup/src/java/iotcloud/IoTString.java
new file mode 100644 (file)
index 0000000..83a3fa1
--- /dev/null
@@ -0,0 +1,105 @@
+package iotcloud;
+
+import java.util.Arrays;
+
+/**
+ * IoTString is wraps the underlying byte string.  We don't use the
+ * standard String class as we have bytes and not chars.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+
+final public class IoTString {
+       byte[] array;
+       int hashcode;
+
+       private IoTString() {
+       }
+
+       /**
+        * Builds an IoTString object around the byte array.  This
+        * constructor makes a copy, so the caller is free to modify the byte array.
+        */
+
+       public IoTString(byte[] _array) {
+               array=(byte[]) _array.clone();
+               hashcode=Arrays.hashCode(array);
+       }
+
+       /**
+        * Converts the String object to a byte representation and stores it
+        * into the IoTString object.
+        */
+
+       public IoTString(String str) {
+               array=str.getBytes();
+               hashcode=Arrays.hashCode(array);
+       }
+
+        /**
+        * Internal methods to build an IoTString using the byte[] passed
+        * in.  Caller is responsible for ensuring the byte[] is never
+        * modified.
+        */
+
+       static IoTString shallow(byte[] _array) {
+               IoTString i=new IoTString();
+               i.array = _array;
+               i.hashcode = Arrays.hashCode(_array);
+               return i;
+       }
+
+       /**
+        * Internal method to grab a reference to our byte array.  Caller
+        * must not modify it.
+        */
+
+       byte[] internalBytes() {
+               return array;
+       }
+
+       /**
+        * Returns the hashCode as computed by Arrays.hashcode(byte[]).
+        */
+
+       public int hashCode() {
+               return hashcode;
+       }
+
+       /**
+        * Returns a String representation of the IoTString.
+        */
+
+       public String toString() {
+               return new String(array);
+       }
+
+       /**
+        * Returns a copy of the underlying byte string.
+        */
+
+       public byte[] getBytes() {
+               return (byte[]) array.clone();
+       }
+
+       /**
+        * Returns true if two byte strings have the same content.
+        */
+
+       public boolean equals(Object o) {
+               if (o instanceof IoTString) {
+                       IoTString i=(IoTString)o;
+                       return Arrays.equals(array, i.array);
+               }
+               return false;
+       }
+
+       /**
+        * Returns the length in bytes of the IoTString.
+        */
+
+       public int length() {
+               return array.length;
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/KeyValue.java b/version2/backup/src/java/iotcloud/KeyValue.java
new file mode 100644 (file)
index 0000000..cd4821a
--- /dev/null
@@ -0,0 +1,76 @@
+package iotcloud;
+import java.nio.ByteBuffer;
+
+/**
+ * KeyValue entry for Slot.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+class KeyValue { /*extends Entry */
+       private IoTString key;
+       private IoTString value;
+
+       public KeyValue(IoTString _key, IoTString _value) {
+               key = _key;
+               value = _value;
+       }
+
+       public IoTString getKey() {
+               return key;
+       }
+
+       public IoTString getValue() {
+               return value;
+       }
+
+       static KeyValue decode(ByteBuffer bb) {
+               int keylength = bb.getInt();
+               int valuelength = bb.getInt();
+               byte[] key = new byte[keylength];
+               bb.get(key);
+
+               if (valuelength != 0) {
+                       byte[] value = new byte[valuelength];
+                       bb.get(value);
+                       return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
+               }
+
+               return new KeyValue(IoTString.shallow(key), null);
+       }
+
+       public void encode(ByteBuffer bb) {
+               bb.putInt(key.length());
+
+               if (value != null) {
+                       bb.putInt(value.length());
+               } else {
+                       bb.putInt(0);
+               }
+
+               bb.put(key.internalBytes());
+
+               if (value != null) {
+                       bb.put(value.internalBytes());
+               }
+       }
+
+       public int getSize() {
+               if (value != null) {
+                       return 2 * Integer.BYTES + key.length() + value.length();
+               }
+
+               return 2 * Integer.BYTES + key.length();
+       }
+
+       public String toString() {
+               if (value == null) {
+                       return "null";
+               }
+               return value.toString();
+       }
+
+       public KeyValue getCopy() {
+               return new KeyValue(key, value);
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/LastMessage.java b/version2/backup/src/java/iotcloud/LastMessage.java
new file mode 100644 (file)
index 0000000..738dff6
--- /dev/null
@@ -0,0 +1,55 @@
+package iotcloud;
+
+import java.nio.ByteBuffer;
+
+/**
+ * This Entry records the last message sent by a given machine.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+
+class LastMessage extends Entry {
+       private long machineid;
+       private long seqnum;
+
+       public LastMessage(Slot slot, long _machineid, long _seqnum) {
+               super(slot);
+               machineid=_machineid;
+               seqnum=_seqnum;
+       }
+
+       public long getMachineID() {
+               return machineid;
+       }
+
+       public long getSequenceNumber() {
+               return seqnum;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               long machineid=bb.getLong();
+               long seqnum=bb.getLong();
+               return new LastMessage(slot, machineid, seqnum);
+       }
+
+       public void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeLastMessage);
+               bb.putLong(machineid);
+               bb.putLong(seqnum);
+       }
+
+       public int getSize() {
+               return 2*Long.BYTES+Byte.BYTES;
+       }
+
+       public byte getType() {
+               return Entry.TypeLastMessage;
+       }
+
+       public Entry getCopy(Slot s) {
+               return new LastMessage(s, machineid, seqnum);
+       }
+}
+
+
diff --git a/version2/backup/src/java/iotcloud/Liveness.java b/version2/backup/src/java/iotcloud/Liveness.java
new file mode 100644 (file)
index 0000000..2c840e4
--- /dev/null
@@ -0,0 +1,11 @@
+package iotcloud;
+
+/**
+ * Interface common to both classes that record information about the
+ * last message sent by a machine.  (Either a Slot or a LastMessage.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+interface Liveness {
+}
diff --git a/version2/backup/src/java/iotcloud/LocalComm.java b/version2/backup/src/java/iotcloud/LocalComm.java
new file mode 100644 (file)
index 0000000..17e3c05
--- /dev/null
@@ -0,0 +1,23 @@
+package iotcloud;
+
+class LocalComm {
+    private Table t1;
+    private Table t2;
+
+    public LocalComm(Table _t1, Table _t2) {
+        t1 = _t1;
+        t2 = _t2;
+    }
+
+    public byte[] sendDataToLocalDevice(Long deviceId, byte[] data) throws InterruptedException{
+        System.out.println("Passing Locally");
+
+        if (deviceId == t1.getId()) {
+            return t1.localCommInput(data);
+        } else if (deviceId == t2.getId()) {
+            return t2.localCommInput(data);
+        } else {
+            throw new Error("Cannot send to " + deviceId + " using this local comm");
+        }
+    }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/Makefile b/version2/backup/src/java/iotcloud/Makefile
new file mode 100644 (file)
index 0000000..2d45b63
--- /dev/null
@@ -0,0 +1,17 @@
+all: server
+
+JAVAC = javac
+JAVADOC = javadoc
+BIN_DIR = bin
+DOCS_DIR = docs
+
+server:
+       $(JAVAC) -d $(BIN_DIR) *.java
+
+doc: server
+       $(JAVADOC) -private -d $(DOCS_DIR) *.java
+
+clean:
+       rm -r bin/*
+       rm -r docs/*
+       rm *~
diff --git a/version2/backup/src/java/iotcloud/NewKey.java b/version2/backup/src/java/iotcloud/NewKey.java
new file mode 100644 (file)
index 0000000..0970016
--- /dev/null
@@ -0,0 +1,57 @@
+package iotcloud;
+
+import java.nio.ByteBuffer;
+
+/**
+ * This Entry records the abort sent by a given machine.
+ * @author Ali Younis <ayounis@uci.edu>
+ * @version 1.0
+ */
+
+
+class NewKey extends Entry {
+       private IoTString key;
+       private long machineid;
+
+       public NewKey(Slot slot, IoTString _key, long _machineid) {
+               super(slot);
+               key = _key;
+               machineid = _machineid;
+       }
+
+       public long getMachineID() {
+               return machineid;
+       }
+
+       public IoTString getKey() {
+               return key;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               int keylength = bb.getInt();
+               byte[] key = new byte[keylength];
+               bb.get(key);
+               long machineid = bb.getLong();
+
+               return new NewKey(slot, IoTString.shallow(key), machineid);
+       }
+
+       public void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeNewKey);
+               bb.putInt(key.length());
+               bb.put(key.internalBytes());
+               bb.putLong(machineid);
+       }
+
+       public int getSize() {
+               return Long.BYTES + Byte.BYTES + Integer.BYTES + key.length();
+       }
+
+       public byte getType() {
+               return Entry.TypeNewKey;
+       }
+
+       public Entry getCopy(Slot s) {
+               return new NewKey(s, key, machineid);
+       }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/Pair.java b/version2/backup/src/java/iotcloud/Pair.java
new file mode 100644 (file)
index 0000000..73ed6bd
--- /dev/null
@@ -0,0 +1,23 @@
+package iotcloud;
+
+class Pair<A,B> {
+       private A a;
+       private B b;
+
+       Pair(A a, B b) {
+               this.a=a;
+               this.b=b;
+       }
+
+       A getFirst() {
+               return a;
+       }
+
+       B getSecond() {
+               return b;
+       }
+
+       public String toString() {
+               return "<"+a+","+b+">";
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/PendingTransaction.java b/version2/backup/src/java/iotcloud/PendingTransaction.java
new file mode 100644 (file)
index 0000000..1a14674
--- /dev/null
@@ -0,0 +1,138 @@
+package iotcloud;
+
+import java.util.Set;
+import java.util.Map;
+import java.util.HashSet;
+
+import javax.script.ScriptException;
+import java.lang.NullPointerException;
+
+
+class PendingTransaction {
+
+    private Set<KeyValue> keyValueUpdateSet = null;
+    private Set<KeyValue> keyValueGuardSet = null;
+    private long arbitrator = -1;
+    private long machineLocalTransSeqNum = -1;
+
+    public PendingTransaction() {
+        keyValueUpdateSet = new HashSet<KeyValue>();
+        keyValueGuardSet = new HashSet<KeyValue>();
+    }
+
+    /**
+     * Add a new key value to the updates
+     *
+     */
+    public void addKV(KeyValue newKV) {
+
+        KeyValue rmKV = null;
+
+        // Make sure there are no duplicates
+        for (KeyValue kv : keyValueUpdateSet) {
+            if (kv.getKey().equals(newKV.getKey())) {
+
+                // Remove key if we are adding a newer version of the same key
+                rmKV = kv;
+                break;
+            }
+        }
+
+        // Remove key if we are adding a newer version of the same key
+        if (rmKV != null) {
+            keyValueUpdateSet.remove(rmKV);
+        }
+
+        // Add the key to the hash set
+        keyValueUpdateSet.add(newKV);
+    }
+
+
+    /**
+     * Add a new key value to the guard set
+     *
+     */
+    public void addKVGuard(KeyValue newKV) {
+        // Add the key to the hash set
+        keyValueGuardSet.add(newKV);
+    }
+
+    /**
+     * Checks if the arbitrator is the same
+     *
+     */
+    public boolean checkArbitrator(long arb) {
+        if (arbitrator == -1) {
+            arbitrator = arb;
+            return true;
+        }
+
+        return arb == arbitrator;
+    }
+
+    /**
+     * Get the transaction arbitrator
+     *
+     */
+    public long getArbitrator() {
+        return arbitrator;
+    }
+
+    /**
+     * Get the key value update set
+     *
+     */
+    public Set<KeyValue> getKVUpdates() {
+        return keyValueUpdateSet;
+    }
+
+
+    /**
+       * Get the key value update set
+       *
+       */
+    public Set<KeyValue> getKVGuard() {
+        return keyValueGuardSet;
+    }
+
+    public void setMachineLocalTransSeqNum(long _machineLocalTransSeqNum) {
+        machineLocalTransSeqNum = _machineLocalTransSeqNum;
+    }
+
+    public long getMachineLocalTransSeqNum() {
+        return machineLocalTransSeqNum;
+    }
+
+    public boolean evaluateGuard(Map<IoTString, KeyValue> keyValTableCommitted, Map<IoTString, KeyValue> keyValTableSpeculative, Map<IoTString, KeyValue> keyValTablePendingTransSpeculative) {
+        for (KeyValue kvGuard : keyValueGuardSet) {
+
+            // First check if the key is in the speculative table, this is the value of the latest assumption
+            KeyValue kv = keyValTablePendingTransSpeculative.get(kvGuard.getKey());
+
+
+            if (kv == null) {
+                // if it is not in the pending trans table then check the speculative table and use that
+                // value as our latest assumption
+                kv = keyValTableSpeculative.get(kvGuard.getKey());
+            }
+
+
+            if (kv == null) {
+                // if it is not in the speculative table then check the committed table and use that
+                // value as our latest assumption
+                kv = keyValTableCommitted.get(kvGuard.getKey());
+            }
+
+            if (kvGuard.getValue() != null) {
+                if ((kv == null) || (!kvGuard.getValue().equals(kv.getValue()))) {
+                    return false;
+                }
+            } else {
+                if (kv != null) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/RejectedMessage.java b/version2/backup/src/java/iotcloud/RejectedMessage.java
new file mode 100644 (file)
index 0000000..9c84f18
--- /dev/null
@@ -0,0 +1,88 @@
+package iotcloud;
+import java.nio.ByteBuffer;
+import java.util.HashSet;
+
+/**
+ * Entry for tracking messages that the server rejected.  We have to
+ * make sure that all clients know that this message was rejected to
+ * prevent the server from reusing these messages in an attack.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+
+class RejectedMessage extends Entry {
+       /* Machine identifier */
+       private long machineid;
+       /* Oldest sequence number in range */
+       private long oldseqnum;
+       /* Newest sequence number in range */
+       private long newseqnum;
+       /* Is the machine identifier of the relevant slots equal to (or not
+        * equal to) the specified machine identifier. */
+       private boolean equalto;
+       /* Set of machines that have not received notification. */
+       private HashSet<Long> watchset;
+
+       RejectedMessage(Slot slot, long _machineid, long _oldseqnum, long _newseqnum, boolean _equalto) {
+               super(slot);
+               machineid=_machineid;
+               oldseqnum=_oldseqnum;
+               newseqnum=_newseqnum;
+               equalto=_equalto;
+       }
+
+       long getOldSeqNum() {
+               return oldseqnum;
+       }
+
+       long getNewSeqNum() {
+               return newseqnum;
+       }
+
+       boolean getEqual() {
+               return equalto;
+       }
+
+       long getMachineID() {
+               return machineid;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               long machineid=bb.getLong();
+               long oldseqnum=bb.getLong();
+               long newseqnum=bb.getLong();
+               byte equalto=bb.get();
+               return new RejectedMessage(slot, machineid, oldseqnum, newseqnum, equalto==1);
+       }
+
+       void setWatchSet(HashSet<Long> _watchset) {
+               watchset=_watchset;
+       }
+
+       void removeWatcher(long machineid) {
+               if (watchset.remove(machineid))
+                       if (watchset.isEmpty())
+                               setDead();
+       }
+
+       void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeRejectedMessage);
+               bb.putLong(machineid);
+               bb.putLong(oldseqnum);
+               bb.putLong(newseqnum);
+               bb.put(equalto?(byte)1:(byte)0);
+       }
+
+       int getSize() {
+               return 3*Long.BYTES + 2*Byte.BYTES;
+       }
+
+       byte getType() {
+               return Entry.TypeRejectedMessage;
+       }
+       
+       Entry getCopy(Slot s) {
+               return new RejectedMessage(s, machineid, oldseqnum, newseqnum, equalto);
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/ServerException.java b/version2/backup/src/java/iotcloud/ServerException.java
new file mode 100644 (file)
index 0000000..6d35c43
--- /dev/null
@@ -0,0 +1,8 @@
+package iotcloud;
+
+public class ServerException extends Exception {
+    
+    public ServerException(String message) {
+        super(message);
+    }
+}
diff --git a/version2/backup/src/java/iotcloud/Slot.java b/version2/backup/src/java/iotcloud/Slot.java
new file mode 100644 (file)
index 0000000..ab04359
--- /dev/null
@@ -0,0 +1,222 @@
+package iotcloud;
+import java.util.Vector;
+import java.nio.ByteBuffer;
+import javax.crypto.Mac;
+import java.util.Arrays;
+
+/**
+ * Data structuring for holding Slot information.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+class Slot implements Liveness {
+       /** Sets the slot size. */
+       static final int SLOT_SIZE = 2048;
+       /** Sets the size for the HMAC. */
+       static final int HMAC_SIZE = 32;
+
+       /** Sequence number of the slot. */
+       private long seqnum;
+       /** HMAC of previous slot. */
+       private byte[] prevhmac;
+       /** HMAC of this slot. */
+       private byte[] hmac;
+       /** Machine that sent this slot. */
+       private long machineid;
+       /** Vector of entries in this slot. */
+       private Vector<Entry> entries;
+       /** Pieces of information that are live. */
+       private int livecount;
+       /** Flag that indicates whether this slot is still live for
+        * recording the machine that sent it. */
+       private boolean seqnumlive;
+       /** Number of bytes of free space. */
+       private int freespace;
+       /** Reference to Table */
+       private Table table;
+
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
+               seqnum = _seqnum;
+               machineid = _machineid;
+               prevhmac = _prevhmac;
+               hmac = _hmac;
+               entries = new Vector<Entry>();
+               livecount = 1;
+               seqnumlive = true;
+               freespace = SLOT_SIZE - getBaseSize();
+               table = _table;
+       }
+
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac) {
+               this(_table, _seqnum, _machineid, _prevhmac, null);
+       }
+
+       Slot(Table _table, long _seqnum, long _machineid) {
+               this(_table, _seqnum, _machineid, new byte[HMAC_SIZE], null);
+       }
+
+       byte[] getHMAC() {
+               return hmac;
+       }
+
+       byte[] getPrevHMAC() {
+               return prevhmac;
+       }
+
+       Entry addEntry(Entry e) {
+               e = e.getCopy(this);
+               entries.add(e);
+               livecount++;
+               freespace -= e.getSize();
+               return e;
+       }
+
+       void removeEntry(Entry e) {
+               entries.remove(e);
+               livecount--;
+               freespace += e.getSize();
+       }
+
+       private void addShallowEntry(Entry e) {
+               entries.add(e);
+               livecount++;
+               freespace -= e.getSize();
+       }
+
+       /**
+        * Returns true if the slot has free space to hold the entry without
+        * using its reserved space. */
+
+       boolean hasSpace(Entry e) {
+               int newfreespace = freespace - e.getSize();
+               return newfreespace >= 0;
+       }
+
+       Vector<Entry> getEntries() {
+               return entries;
+       }
+
+       static Slot decode(Table table, byte[] array, Mac mac) {
+               mac.update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+               byte[] realmac = mac.doFinal();
+
+               ByteBuffer bb = ByteBuffer.wrap(array);
+               byte[] hmac = new byte[HMAC_SIZE];
+               byte[] prevhmac = new byte[HMAC_SIZE];
+               bb.get(hmac);
+               bb.get(prevhmac);
+               if (!Arrays.equals(realmac, hmac))
+                       throw new Error("Server Error: Invalid HMAC!  Potential Attack!");
+
+               long seqnum = bb.getLong();
+               long machineid = bb.getLong();
+               int numentries = bb.getInt();
+               Slot slot = new Slot(table, seqnum, machineid, prevhmac, hmac);
+
+               for (int i = 0; i < numentries; i++) {
+                       slot.addShallowEntry(Entry.decode(slot, bb));
+               }
+
+               return slot;
+       }
+
+       byte[] encode(Mac mac) {
+               byte[] array = new byte[SLOT_SIZE];
+               ByteBuffer bb = ByteBuffer.wrap(array);
+               /* Leave space for the slot HMAC.  */
+               bb.position(HMAC_SIZE);
+               bb.put(prevhmac);
+               bb.putLong(seqnum);
+               bb.putLong(machineid);
+               bb.putInt(entries.size());
+               for (Entry entry : entries) {
+                       entry.encode(bb);
+               }
+               /* Compute our HMAC */
+               mac.update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+               byte[] realmac = mac.doFinal();
+               hmac = realmac;
+               bb.position(0);
+               bb.put(realmac);
+               return array;
+       }
+
+       /**
+        * Returns the empty size of a Slot. Includes 2 HMACs, the machine
+        * identifier, the sequence number, and the number of entries.
+        */
+       int getBaseSize() {
+               return 2 * HMAC_SIZE + 2 * Long.BYTES + Integer.BYTES;
+       }
+
+       /**
+        * Returns the live set of entries for this Slot.  Generates a fake
+        * LastMessage entry to represent the information stored by the slot
+        * itself.
+        */
+
+       Vector<Entry> getLiveEntries(boolean resize) {
+               Vector<Entry> liveEntries = new Vector<Entry>();
+               for (Entry entry : entries) {
+                       if (entry.isLive()) {
+                               if (!resize || entry.getType() != Entry.TypeTableStatus)
+                                       liveEntries.add(entry);
+                       }
+               }
+
+               if (seqnumlive && !resize)
+                       liveEntries.add(new LastMessage(this, machineid, seqnum));
+
+               return liveEntries;
+       }
+
+       /**
+        * Returns the sequence number of the slot.
+        */
+
+       long getSequenceNumber() {
+               return seqnum;
+       }
+
+       /**
+        * Returns the machine that sent this slot.
+        */
+
+       long getMachineID() {
+               return machineid;
+       }
+
+       /**
+        * Records that a newer slot records the fact that this slot was
+        * sent by the relevant machine.
+        */
+
+       void setDead() {
+               seqnumlive = false;
+               decrementLiveCount();
+       }
+
+       /**
+        * Update the count of live entries.
+        */
+
+       void decrementLiveCount() {
+               livecount--;
+               if (livecount == 0) {
+                       table.decrementLiveCount();
+               }
+       }
+
+       /**
+        * Returns whether the slot stores any live information.
+        */
+
+       boolean isLive() {
+               return livecount > 0;
+       }
+
+       public String toString() {
+               return "<" + getSequenceNumber() + ">";
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/SlotBuffer.java b/version2/backup/src/java/iotcloud/SlotBuffer.java
new file mode 100644 (file)
index 0000000..716d0f2
--- /dev/null
@@ -0,0 +1,122 @@
+package iotcloud;
+
+/**
+ * Circular buffer that holds the live set of slots.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+class SlotBuffer {
+       static final int DEFAULT_SIZE = 128;
+
+       private Slot[] array;
+       private int head;
+       private int tail;
+       public long oldestseqn;
+
+       SlotBuffer() {
+               array = new Slot[DEFAULT_SIZE + 1];
+               head = tail = 0;
+               oldestseqn = 0;
+       }
+
+       int size() {
+               if (head >= tail)
+                       return head - tail;
+               return (array.length + head) - tail;
+       }
+
+       int capacity() {
+               return array.length - 1;
+       }
+
+       void resize(int newsize) {
+               if (newsize == (array.length - 1))
+                       return;
+
+               Slot[] newarray = new Slot[newsize + 1];
+               int currsize = size();
+               int index = tail;
+               for (int i = 0; i < currsize; i++) {
+                       newarray[i] = array[index];
+                       if ((++index) == array.length)
+                               index = 0;
+               }
+               array = newarray;
+               tail = 0;
+               head = currsize;
+       }
+
+       private void incrementHead() {
+               head++;
+               if (head >= array.length)
+                       head = 0;
+       }
+
+       private void incrementTail() {
+               tail++;
+               if (tail >= array.length)
+                       tail = 0;
+       }
+
+       void putSlot(Slot s) {
+
+               long checkNum = (getNewestSeqNum() + 1);
+
+               if (checkNum != s.getSequenceNumber()) {
+                       // We have a gap so expunge all our slots
+                       oldestseqn = s.getSequenceNumber();
+                       tail = 0;
+                       head = 1;
+                       array[0] = s;
+                       return;
+               }
+
+               array[head] = s;
+               incrementHead();
+
+               if (oldestseqn == 0) {
+                       oldestseqn = s.getSequenceNumber();
+               }
+
+               if (head == tail) {
+                       incrementTail();
+                       oldestseqn++;
+               }
+       }
+
+       Slot getSlot(long seqnum) {
+               int diff = (int) (seqnum - oldestseqn);
+               int index = diff + tail;
+
+               if (index < 0) {
+                       // Really old message so we dont have it anymore
+                       return null;
+               }
+
+               if (index >= array.length) {
+                       if (head >= tail) {
+                               return null;
+                       }
+                       index -= array.length;
+               }
+
+               if (index >= array.length) {
+
+                       return null;
+               }
+               if (head >= tail && index >= head) {
+                       return null;
+               }
+
+               return array[index];
+       }
+
+       long getOldestSeqNum() {
+               return oldestseqn;
+       }
+
+       long getNewestSeqNum() {
+               return oldestseqn + size() - 1;
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/SlotIndexer.java b/version2/backup/src/java/iotcloud/SlotIndexer.java
new file mode 100644 (file)
index 0000000..cecdf2d
--- /dev/null
@@ -0,0 +1,31 @@
+package iotcloud;
+
+/**
+ * Slot indexer allows slots in both the slot buffer and the new
+ * server response to looked up in a consistent fashion.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+class SlotIndexer {
+       private Slot[] updates;
+       private SlotBuffer buffer;
+       private long firstslotseqnum;
+
+       SlotIndexer(Slot[] _updates, SlotBuffer _buffer) {
+               buffer = _buffer;
+               updates = _updates;
+               firstslotseqnum = updates[0].getSequenceNumber();
+       }
+
+       Slot getSlot(long seqnum) {
+               if (seqnum >= firstslotseqnum) {
+                       int offset = (int) (seqnum - firstslotseqnum);
+                       if (offset >= updates.length)
+                               throw new Error("Invalid Slot Sequence Number Reference");
+                       else
+                               return updates[offset];
+               } else
+                       return buffer.getSlot(seqnum);
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/Table.java b/version2/backup/src/java/iotcloud/Table.java
new file mode 100644 (file)
index 0000000..f6d699b
--- /dev/null
@@ -0,0 +1,1728 @@
+package iotcloud;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.HashSet;
+import java.util.Arrays;
+import java.util.Vector;
+import java.util.Random;
+import java.util.Queue;
+import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.Collection;
+import java.util.Collections;
+import java.nio.ByteBuffer;
+import java.util.concurrent.Semaphore;
+
+
+/**
+ * IoTTable data structure.  Provides client inferface.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+final public class Table {
+       private int numslots;   //number of slots stored in buffer
+
+       // machine id -> (sequence number, Slot or LastMessage); records last message by each client
+       private HashMap<Long, Pair<Long, Liveness> > lastmessagetable = new HashMap<Long, Pair<Long, Liveness> >();
+       // machine id -> ...
+       private HashMap<Long, HashSet<RejectedMessage> > watchlist = new HashMap<Long, HashSet<RejectedMessage> >();
+       private Vector<Long> rejectedmessagelist = new Vector<Long>();
+       private SlotBuffer buffer;
+       private CloudComm cloud;
+       private long sequencenumber; //Largest sequence number a client has received
+       private long localmachineid;
+       private TableStatus lastTableStatus;
+       static final int FREE_SLOTS = 10; //number of slots that should be kept free
+       static final int SKIP_THRESHOLD = 10;
+       private long liveslotcount = 0;
+       private int chance;
+       static final double RESIZE_MULTIPLE = 1.2;
+       static final double RESIZE_THRESHOLD = 0.75;
+       static final int REJECTED_THRESHOLD = 5;
+       private int resizethreshold;
+       private long lastliveslotseqn;  //smallest sequence number with a live entry
+       private Random random = new Random();
+       private long lastUncommittedTransaction = 0;
+
+       private int smallestTableStatusSeen = -1;
+       private int largestTableStatusSeen = -1;
+       private int lastSeenPendingTransactionSpeculateIndex = 0;
+       private int commitSequenceNumber = 0;
+       private long localTransactionSequenceNumber = 0;
+
+       private PendingTransaction pendingTransBuild = null; // Pending Transaction used in building
+       private LinkedList<PendingTransaction> pendingTransQueue = null; // Queue of pending transactions
+       private Map<Long, Map<Long, Commit>> commitMap = null; // List of all the most recent live commits
+       private Map<Long, Abort> abortMap = null; // Set of the live aborts
+       private Map<IoTString, Commit> committedMapByKey = null; // Table of committed KV
+       private Map<IoTString, KeyValue> commitedTable = null; // Table of committed KV
+       private Map<IoTString, KeyValue> speculativeTable = null; // Table of speculative KV
+       private Map<Long, Transaction> uncommittedTransactionsMap = null;
+       private Map<IoTString, Long> arbitratorTable = null; // Table of arbitrators
+       private Map<IoTString, NewKey> newKeyTable = null; // Table of speculative KV
+       private Map<Long, Map<Long, Commit>> newCommitMap = null; // Map of all the new commits
+       private Map<Long, Long> lastCommitSeenSeqNumMap = null; // sequence number of the last commit that was seen grouped by arbitrator
+       private Map<Long, Long> lastCommitSeenTransSeqNumMap = null; // transaction sequence number of the last commit that was seen grouped by arbitrator
+       private Map<Long, Long> lastAbortSeenSeqNumMap = null; // sequence number of the last abort that was seen grouped by arbitrator
+       private Map<IoTString, KeyValue> pendingTransSpeculativeTable = null;
+       private List<Commit> pendingCommitsList = null;
+       private List<Commit> pendingCommitsToDelete = null;
+       private Map<Long, LocalComm> localCommunicationChannels;
+       private Map<Long, TransactionStatus> transactionStatusMap = null;
+       private Map<Long, TransactionStatus> transactionStatusNotSentMap = null;
+
+       private Semaphore mutex = null;
+
+
+
+       public Table(String hostname, String baseurl, String password, long _localmachineid) {
+               localmachineid = _localmachineid;
+               buffer = new SlotBuffer();
+               numslots = buffer.capacity();
+               setResizeThreshold();
+               sequencenumber = 0;
+               cloud = new CloudComm(this, hostname, baseurl, password);
+               lastliveslotseqn = 1;
+
+               setupDataStructs();
+       }
+
+       public Table(CloudComm _cloud, long _localmachineid) {
+               localmachineid = _localmachineid;
+               buffer = new SlotBuffer();
+               numslots = buffer.capacity();
+               setResizeThreshold();
+               sequencenumber = 0;
+               cloud = _cloud;
+
+               setupDataStructs();
+       }
+
+       private void setupDataStructs() {
+               pendingTransQueue = new LinkedList<PendingTransaction>();
+               commitMap = new HashMap<Long, Map<Long, Commit>>();
+               abortMap = new HashMap<Long, Abort>();
+               committedMapByKey = new HashMap<IoTString, Commit>();
+               commitedTable = new HashMap<IoTString, KeyValue>();
+               speculativeTable = new HashMap<IoTString, KeyValue>();
+               uncommittedTransactionsMap = new HashMap<Long, Transaction>();
+               arbitratorTable = new HashMap<IoTString, Long>();
+               newKeyTable = new HashMap<IoTString, NewKey>();
+               newCommitMap = new HashMap<Long, Map<Long, Commit>>();
+               lastCommitSeenSeqNumMap = new HashMap<Long, Long>();
+               lastCommitSeenTransSeqNumMap = new HashMap<Long, Long>();
+               lastAbortSeenSeqNumMap = new HashMap<Long, Long>();
+               pendingTransSpeculativeTable = new HashMap<IoTString, KeyValue>();
+               pendingCommitsList = new LinkedList<Commit>();
+               pendingCommitsToDelete = new LinkedList<Commit>();
+               localCommunicationChannels = new HashMap<Long, LocalComm>();
+               transactionStatusMap = new HashMap<Long, TransactionStatus>();
+               transactionStatusNotSentMap = new HashMap<Long, TransactionStatus>();
+               mutex = new Semaphore(1);
+       }
+
+       public void initTable() throws ServerException {
+               cloud.setSalt();//Set the salt
+               Slot s = new Slot(this, 1, localmachineid);
+               TableStatus status = new TableStatus(s, numslots);
+               s.addEntry(status);
+               Slot[] array = cloud.putSlot(s, numslots);
+               if (array == null) {
+                       array = new Slot[] {s};
+                       /* update data structure */
+                       validateandupdate(array, true);
+               } else {
+                       throw new Error("Error on initialization");
+               }
+       }
+
+       public void rebuild() throws ServerException, InterruptedException {
+               mutex.acquire();
+               Slot[] newslots = cloud.getSlots(sequencenumber + 1);
+               validateandupdate(newslots, true);
+               mutex.release();
+       }
+
+       // TODO: delete method
+       public void printSlots() {
+               long o = buffer.getOldestSeqNum();
+               long n = buffer.getNewestSeqNum();
+
+               int[] types = new int[10];
+
+               int num = 0;
+
+               int livec = 0;
+               int deadc = 0;
+               for (long i = o; i < (n + 1); i++) {
+                       Slot s = buffer.getSlot(i);
+
+                       Vector<Entry> entries = s.getEntries();
+
+                       for (Entry e : entries) {
+                               if (e.isLive()) {
+                                       int type = e.getType();
+                                       types[type] = types[type] + 1;
+                                       num++;
+                                       livec++;
+                               } else {
+                                       deadc++;
+                               }
+                       }
+               }
+
+               for (int i = 0; i < 10; i++) {
+                       System.out.println(i + "    " + types[i]);
+               }
+               System.out.println("Live count:   " + livec);
+               System.out.println("Dead count:   " + deadc);
+               System.out.println("Old:   " + o);
+               System.out.println("New:   " + n);
+               System.out.println("Size:   " + buffer.size());
+               System.out.println("Commits Key Map:   " + commitedTable.size());
+               // System.out.println("Commits Live Map:   " + commitMap.size());
+               System.out.println("Pending:   " + pendingTransQueue.size());
+
+               // List<IoTString> strList = new ArrayList<IoTString>();
+               // for (int i = 0; i < 100; i++) {
+               //      String keyA = "a" + i;
+               //      String keyB = "b" + i;
+               //      String keyC = "c" + i;
+               //      String keyD = "d" + i;
+
+               //      IoTString iKeyA = new IoTString(keyA);
+               //      IoTString iKeyB = new IoTString(keyB);
+               //      IoTString iKeyC = new IoTString(keyC);
+               //      IoTString iKeyD = new IoTString(keyD);
+
+               //      strList.add(iKeyA);
+               //      strList.add(iKeyB);
+               //      strList.add(iKeyC);
+               //      strList.add(iKeyD);
+               // }
+
+
+               // for (Long l : commitMap.keySet()) {
+               //      for (Long l2 : commitMap.get(l).keySet()) {
+               //              for (KeyValue kv : commitMap.get(l).get(l2).getkeyValueUpdateSet()) {
+               //                      strList.remove(kv.getKey());
+               //                      System.out.print(kv.getKey() + "    ");
+               //              }
+               //      }
+               // }
+
+               // System.out.println();
+               // System.out.println();
+
+               // for (IoTString s : strList) {
+               //      System.out.print(s + "    ");
+               // }
+               // System.out.println();
+               // System.out.println(strList.size());
+       }
+
+       public long getId() {
+               return localmachineid;
+       }
+
+       public boolean hasConnection() {
+               return cloud.hasConnection();
+       }
+
+       public String toString() {
+               String retString = " Committed Table: \n";
+               retString += "---------------------------\n";
+               retString += commitedTable.toString();
+
+               retString += "\n\n";
+
+               retString += " Speculative Table: \n";
+               retString += "---------------------------\n";
+               retString += speculativeTable.toString();
+
+               return retString;
+       }
+
+       public void addLocalComm(long machineId, LocalComm lc) {
+               localCommunicationChannels.put(machineId, lc);
+       }
+       public Long getArbitrator(IoTString key) throws InterruptedException {
+
+               mutex.acquire();
+               Long arb = arbitratorTable.get(key);
+               mutex.release();
+
+               return arb;
+       }
+
+       public IoTString getCommitted(IoTString key) throws InterruptedException {
+
+               mutex.acquire();
+               KeyValue kv = commitedTable.get(key);
+               mutex.release();
+
+
+               if (kv != null) {
+                       return kv.getValue();
+               } else {
+                       return null;
+               }
+       }
+
+       public IoTString getSpeculative(IoTString key) throws InterruptedException {
+
+               mutex.acquire();
+
+               KeyValue kv = pendingTransSpeculativeTable.get(key);
+
+               if (kv == null) {
+                       kv = speculativeTable.get(key);
+               }
+
+               if (kv == null) {
+                       kv = commitedTable.get(key);
+               }
+               mutex.release();
+
+
+               if (kv != null) {
+                       return kv.getValue();
+               } else {
+                       return null;
+               }
+       }
+
+       public IoTString getCommittedAtomic(IoTString key) throws InterruptedException {
+
+               mutex.acquire();
+
+               KeyValue kv = commitedTable.get(key);
+
+               if (arbitratorTable.get(key) == null) {
+                       throw new Error("Key not Found.");
+               }
+
+               // Make sure new key value pair matches the current arbitrator
+               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+                       // TODO: Maybe not throw en error
+                       throw new Error("Not all Key Values Match Arbitrator.");
+               }
+
+               mutex.release();
+
+               if (kv != null) {
+                       pendingTransBuild.addKVGuard(new KeyValue(key, kv.getValue()));
+                       return kv.getValue();
+               } else {
+                       pendingTransBuild.addKVGuard(new KeyValue(key, null));
+                       return null;
+               }
+       }
+
+       public IoTString getSpeculativeAtomic(IoTString key) throws InterruptedException {
+
+               mutex.acquire();
+
+               if (arbitratorTable.get(key) == null) {
+                       throw new Error("Key not Found.");
+               }
+
+               // Make sure new key value pair matches the current arbitrator
+               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+                       // TODO: Maybe not throw en error
+                       throw new Error("Not all Key Values Match Arbitrator.");
+               }
+
+               KeyValue kv = pendingTransSpeculativeTable.get(key);
+
+               if (kv == null) {
+                       kv = speculativeTable.get(key);
+               }
+
+               if (kv == null) {
+                       kv = commitedTable.get(key);
+               }
+
+               mutex.release();
+
+               if (kv != null) {
+                       pendingTransBuild.addKVGuard(new KeyValue(key, kv.getValue()));
+                       return kv.getValue();
+               } else {
+                       pendingTransBuild.addKVGuard(new KeyValue(key, null));
+                       return null;
+               }
+       }
+
+       public Pair<Boolean, Boolean> update() throws InterruptedException {
+
+               mutex.acquire();
+
+               boolean gotLatestFromServer = false;
+               boolean didSendLocal = false;
+
+               try {
+                       Slot[] newslots = cloud.getSlots(sequencenumber + 1);
+                       validateandupdate(newslots, false);
+                       gotLatestFromServer = true;
+
+                       if (!pendingTransQueue.isEmpty()) {
+
+                               // We have a pending transaction so do full insertion
+                               processPendingTrans();
+                       } else {
+
+                               // We dont have a pending transaction so do minimal effort
+                               updateWithNotPendingTrans();
+                       }
+
+                       didSendLocal = true;
+
+
+               } catch (Exception e) {
+                       // could not update so do nothing
+               }
+
+
+               mutex.release();
+               return new Pair<Boolean, Boolean>(gotLatestFromServer, didSendLocal);
+       }
+
+       public Boolean updateFromLocal(long arb) throws InterruptedException {
+               LocalComm lc = localCommunicationChannels.get(arb);
+               if (lc == null) {
+                       // Cant talk directly to arbitrator so cant do anything
+                       return false;
+               }
+
+               byte[] array = new byte[Long.BYTES ];
+               ByteBuffer bbEncode = ByteBuffer.wrap(array);
+               Long lastSeenCommit = lastCommitSeenSeqNumMap.get(arb);
+               if (lastSeenCommit != null) {
+                       bbEncode.putLong(lastSeenCommit);
+               } else {
+                       bbEncode.putLong(0);
+               }
+
+               mutex.acquire();
+               byte[] data = lc.sendDataToLocalDevice(arb, bbEncode.array());
+
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(data);
+               boolean didCommit = bbDecode.get() == 1;
+               int numberOfCommites = bbDecode.getInt();
+
+               List<Commit> newCommits = new LinkedList<Commit>();
+               for (int i = 0; i < numberOfCommites; i++ ) {
+                       bbDecode.get();
+                       Commit com = (Commit)Commit.decode(null, bbDecode);
+                       newCommits.add(com);
+               }
+
+               for (Commit commit : newCommits) {
+                       // Prepare to process the commit
+                       processEntry(commit);
+               }
+
+               boolean didCommitOrSpeculate = proccessAllNewCommits();
+
+               // Go through all uncommitted transactions and kill the ones that are dead
+               deleteDeadUncommittedTransactions();
+
+               // Speculate on key value pairs
+               didCommitOrSpeculate |= createSpeculativeTable();
+               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+
+
+               mutex.release();
+
+               return true;
+       }
+
+       public void startTransaction() throws InterruptedException {
+               // Create a new transaction, invalidates any old pending transactions.
+               pendingTransBuild = new PendingTransaction();
+       }
+
+       public void addKV(IoTString key, IoTString value) {
+
+               if (arbitratorTable.get(key) == null) {
+                       throw new Error("Key not Found.");
+               }
+
+               // Make sure new key value pair matches the current arbitrator
+               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+                       // TODO: Maybe not throw en error
+                       throw new Error("Not all Key Values Match Arbitrator.");
+               }
+
+               KeyValue kv = new KeyValue(key, value);
+               pendingTransBuild.addKV(kv);
+       }
+
+       public TransactionStatus commitTransaction() throws InterruptedException {
+
+               if (pendingTransBuild.getKVUpdates().size() == 0) {
+
+                       // transaction with no updates will have no effect on the system
+                       return new TransactionStatus(TransactionStatus.StatusNoEffect, -1);
+               }
+
+               mutex.acquire();
+
+               TransactionStatus transStatus = null;
+
+               if (pendingTransBuild.getArbitrator() != localmachineid) {
+
+                       // set the local sequence number so we can recognize this transaction later
+                       pendingTransBuild.setMachineLocalTransSeqNum(localTransactionSequenceNumber);
+                       localTransactionSequenceNumber++;
+
+                       transStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransBuild.getArbitrator());
+                       transactionStatusNotSentMap.put(pendingTransBuild.getMachineLocalTransSeqNum(), transStatus);
+
+                       // Add the pending transaction to the queue
+                       pendingTransQueue.add(pendingTransBuild);
+
+
+                       for (int i = lastSeenPendingTransactionSpeculateIndex; i < pendingTransQueue.size(); i++) {
+                               PendingTransaction pt = pendingTransQueue.get(i);
+
+                               if (pt.evaluateGuard(commitedTable, speculativeTable, pendingTransSpeculativeTable)) {
+
+                                       lastSeenPendingTransactionSpeculateIndex = i;
+
+                                       for (KeyValue kv : pt.getKVUpdates()) {
+                                               pendingTransSpeculativeTable.put(kv.getKey(), kv);
+                                       }
+
+                               }
+                       }
+               } else {
+                       Transaction ut = new Transaction(null,
+                                                        -1,
+                                                        localmachineid,
+                                                        pendingTransBuild.getArbitrator(),
+                                                        pendingTransBuild.getKVUpdates(),
+                                                        pendingTransBuild.getKVGuard());
+
+                       Pair<Boolean, List<Commit>> retData = doLocalUpdateAndArbitrate(ut, lastCommitSeenSeqNumMap.get(localmachineid));
+
+                       if (retData.getFirst()) {
+                               transStatus = new TransactionStatus(TransactionStatus.StatusCommitted, pendingTransBuild.getArbitrator());
+                       } else {
+                               transStatus = new TransactionStatus(TransactionStatus.StatusAborted, pendingTransBuild.getArbitrator());
+                       }
+               }
+
+               // Try to insert transactions if possible
+               if (!pendingTransQueue.isEmpty()) {
+                       // We have a pending transaction so do full insertion
+                       processPendingTrans();
+               } else {
+                       try {
+                               // We dont have a pending transaction so do minimal effort
+                               updateWithNotPendingTrans();
+                       } catch (Exception e) {
+                               // Do nothing
+                       }
+               }
+
+               // reset it so next time is fresh
+               pendingTransBuild = new PendingTransaction();
+
+
+               mutex.release();
+               return transStatus;
+       }
+
+       public boolean createNewKey(IoTString keyName, long machineId) throws ServerException, InterruptedException {
+               try {
+                       mutex.acquire();
+
+                       while (true) {
+                               if (arbitratorTable.get(keyName) != null) {
+                                       // There is already an arbitrator
+                                       mutex.release();
+                                       return false;
+                               }
+
+                               if (tryput(keyName, machineId, false)) {
+                                       // If successfully inserted
+                                       mutex.release();
+                                       return true;
+                               }
+                       }
+               } catch (ServerException e) {
+                       mutex.release();
+                       throw e;
+               }
+       }
+
+       private void processPendingTrans() throws InterruptedException {
+
+               boolean sentAllPending = false;
+               try {
+                       while (!pendingTransQueue.isEmpty()) {
+                               if (tryput( pendingTransQueue.peek(), false)) {
+                                       pendingTransQueue.poll();
+                               }
+                       }
+
+                       // if got here then all pending transactions were sent
+                       sentAllPending = true;
+               } catch (Exception e) {
+                       // There was a connection error
+                       sentAllPending = false;
+               }
+
+               if (!sentAllPending) {
+
+                       for (Iterator<PendingTransaction> i = pendingTransQueue.iterator(); i.hasNext(); ) {
+                               PendingTransaction pt = i.next();
+                               LocalComm lc = localCommunicationChannels.get(pt.getArbitrator());
+                               if (lc == null) {
+                                       // Cant talk directly to arbitrator so cant do anything
+                                       continue;
+                               }
+
+
+                               Transaction ut = new Transaction(null,
+                                                                -1,
+                                                                localmachineid,
+                                                                pendingTransBuild.getArbitrator(),
+                                                                pendingTransBuild.getKVUpdates(),
+                                                                pendingTransBuild.getKVGuard());
+
+
+                               Pair<Boolean, List<Commit>> retData = sendTransactionToLocal(ut, lc);
+
+                               for (Commit commit : retData.getSecond()) {
+                                       // Prepare to process the commit
+                                       processEntry(commit);
+                               }
+
+                               boolean didCommitOrSpeculate = proccessAllNewCommits();
+
+                               // Go through all uncommitted transactions and kill the ones that are dead
+                               deleteDeadUncommittedTransactions();
+
+                               // Speculate on key value pairs
+                               didCommitOrSpeculate |= createSpeculativeTable();
+                               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+
+                               if (retData.getFirst()) {
+                                       TransactionStatus transStatus = transactionStatusNotSentMap.remove(pendingTransBuild.getMachineLocalTransSeqNum());
+                                       if (transStatus != null) {
+                                               transStatus.setStatus(TransactionStatus.StatusCommitted);
+                                       }
+
+                               } else {
+                                       TransactionStatus transStatus = transactionStatusNotSentMap.remove(pendingTransBuild.getMachineLocalTransSeqNum());
+                                       if (transStatus != null) {
+                                               transStatus.setStatus(TransactionStatus.StatusAborted);
+                                       }
+                               }
+                               i.remove();
+                       }
+               }
+       }
+
+       private void updateWithNotPendingTrans() throws ServerException, InterruptedException {
+               boolean doEnd = false;
+               boolean needResize = false;
+               while (!doEnd && ((uncommittedTransactionsMap.keySet().size() > 0)  || (pendingCommitsList.size() > 0))   ) {
+                       boolean resize = needResize;
+                       needResize = false;
+
+                       Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
+                       int newsize = 0;
+                       if (liveslotcount > resizethreshold) {
+                               resize = true; //Resize is forced
+                       }
+
+                       if (resize) {
+                               newsize = (int) (numslots * RESIZE_MULTIPLE);
+                               TableStatus status = new TableStatus(s, newsize);
+                               s.addEntry(status);
+                       }
+
+                       doRejectedMessages(s);
+
+                       ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
+
+                       // Resize was needed so redo call
+                       if (retTup.getFirst()) {
+                               needResize = true;
+                               continue;
+                       }
+
+                       // Extract working variables
+                       boolean seenliveslot = retTup.getSecond();
+                       long seqn = retTup.getThird();
+
+                       // Did need to arbitrate
+                       doEnd = !doArbitration(s);
+
+                       doOptionalRescue(s, seenliveslot, seqn, resize);
+
+                       int max = 0;
+                       if (resize) {
+                               max = newsize;
+                       }
+
+                       Slot[] array = cloud.putSlot(s, max);
+                       if (array == null) {
+                               array = new Slot[] {s};
+                               rejectedmessagelist.clear();
+
+                               // Delete pending commits that were sent to the cloud
+                               deletePendingCommits();
+
+                       }       else {
+                               if (array.length == 0)
+                                       throw new Error("Server Error: Did not send any slots");
+                               rejectedmessagelist.add(s.getSequenceNumber());
+                               doEnd = false;
+                       }
+
+                       /* update data structure */
+                       validateandupdate(array, true);
+               }
+       }
+
+       private Pair<Boolean, List<Commit>> sendTransactionToLocal(Transaction ut, LocalComm lc) throws InterruptedException {
+
+               // encode the request
+               byte[] array = new byte[Long.BYTES + ut.getSize()];
+               ByteBuffer bbEncode = ByteBuffer.wrap(array);
+               Long lastSeenCommit = lastCommitSeenSeqNumMap.get(ut.getArbitrator());
+               if (lastSeenCommit != null) {
+                       bbEncode.putLong(lastSeenCommit);
+               } else {
+                       bbEncode.putLong(0);
+               }
+               ut.encode(bbEncode);
+
+               byte[] data = lc.sendDataToLocalDevice(ut.getArbitrator(), bbEncode.array());
+
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(data);
+               boolean didCommit = bbDecode.get() == 1;
+               int numberOfCommites = bbDecode.getInt();
+
+               List<Commit> newCommits = new LinkedList<Commit>();
+               for (int i = 0; i < numberOfCommites; i++ ) {
+                       bbDecode.get();
+                       Commit com = (Commit)Commit.decode(null, bbDecode);
+                       newCommits.add(com);
+               }
+
+               return new Pair<Boolean, List<Commit>>(didCommit, newCommits);
+       }
+
+       public byte[] localCommInput(byte[] data) throws InterruptedException {
+
+
+
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(data);
+               long lastSeenCommit = bbDecode.getLong();
+
+               Transaction ut = null;
+               if (data.length != Long.BYTES) {
+                       bbDecode.get();
+                       ut = (Transaction)Transaction.decode(null, bbDecode);
+               }
+
+               mutex.acquire();
+               // Do the local update and arbitrate
+               Pair<Boolean, List<Commit>> returnData = doLocalUpdateAndArbitrate(ut, lastSeenCommit);
+               mutex.release();
+
+
+               // Calculate the size of the response
+               int size = Byte.BYTES + Integer.BYTES;
+               for (Commit com : returnData.getSecond()) {
+                       size += com.getSize();
+               }
+
+               // encode the response
+               byte[] array = new byte[size];
+               ByteBuffer bbEncode = ByteBuffer.wrap(array);
+               if (returnData.getFirst()) {
+                       bbEncode.put((byte)1);
+               } else {
+                       bbEncode.put((byte)0);
+               }
+               bbEncode.putInt(returnData.getSecond().size());
+
+               for (Commit com : returnData.getSecond()) {
+                       com.encode(bbEncode);
+               }
+
+               return bbEncode.array();
+       }
+
+       private Pair<Boolean, List<Commit>> doLocalUpdateAndArbitrate(Transaction ut, Long lastCommitSeen) {
+
+               List<Commit> returnCommits = new ArrayList<Commit>();
+
+               if ((lastCommitSeenSeqNumMap.get(localmachineid) != null) && (lastCommitSeenSeqNumMap.get(localmachineid) > lastCommitSeen)) {
+                       // There is a commit that the other client has not seen yet
+
+                       Map<Long, Commit> cm = commitMap.get(localmachineid);
+                       if (cm != null) {
+
+                               List<Long> commitKeys = new ArrayList<Long>(cm.keySet());
+                               Collections.sort(commitKeys);
+
+
+                               for (int i = (commitKeys.size() - 1); i >= 0; i--) {
+                                       Commit com = cm.get(commitKeys.get(i));
+
+                                       if (com.getSequenceNumber() <= lastCommitSeen) {
+                                               break;
+                                       }
+                                       returnCommits.add((Commit)com.getCopy(null));
+                               }
+                       }
+               }
+
+
+               if ((ut == null) || (ut.getArbitrator() != localmachineid)) {
+                       // We are not the arbitrator for that transaction so the other device is talking to the wrong arbitrator
+                       // or there is no transaction to process
+                       return new Pair<Boolean, List<Commit>>(false, returnCommits);
+               }
+
+               if (!ut.evaluateGuard(commitedTable, null)) {
+                       // Guard evaluated as false so return only the commits that the other device has not seen yet
+                       return new Pair<Boolean, List<Commit>>(false, returnCommits);
+               }
+
+               // create the commit
+               Commit commit = new Commit(null,
+                                          -1,
+                                          commitSequenceNumber,
+                                          ut.getArbitrator(),
+                                          ut.getkeyValueUpdateSet());
+               commitSequenceNumber = commitSequenceNumber + 1;
+
+               // Add to the pending commits list
+               pendingCommitsList.add(commit);
+
+               // Add this commit so we can send it back
+               returnCommits.add(commit);
+
+               // Prepare to process the commit
+               processEntry(commit);
+
+               boolean didCommitOrSpeculate = proccessAllNewCommits();
+
+               // Go through all uncommitted transactions and kill the ones that are dead
+               deleteDeadUncommittedTransactions();
+
+               // Speculate on key value pairs
+               didCommitOrSpeculate |= createSpeculativeTable();
+               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+
+               return new Pair<Boolean, List<Commit>>(true, returnCommits);
+       }
+
+       public void decrementLiveCount() {
+               liveslotcount--;
+       }
+
+       private void setResizeThreshold() {
+               int resize_lower = (int) (RESIZE_THRESHOLD * numslots);
+               resizethreshold = resize_lower - 1 + random.nextInt(numslots - resize_lower);
+       }
+
+       private boolean tryput(PendingTransaction pendingTrans, boolean resize) throws ServerException {
+               Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
+
+               int newsize = 0;
+               if (liveslotcount > resizethreshold) {
+                       resize = true; //Resize is forced
+               }
+
+               if (resize) {
+                       newsize = (int) (numslots * RESIZE_MULTIPLE);
+                       TableStatus status = new TableStatus(s, newsize);
+                       s.addEntry(status);
+               }
+
+               doRejectedMessages(s);
+
+               ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
+
+               // Resize was needed so redo call
+               if (retTup.getFirst()) {
+                       return tryput(pendingTrans, true);
+               }
+
+               // Extract working variables
+               boolean seenliveslot = retTup.getSecond();
+               long seqn = retTup.getThird();
+
+               doArbitration(s);
+
+               Transaction trans = new Transaction(s,
+                                                   s.getSequenceNumber(),
+                                                   localmachineid,
+                                                   pendingTrans.getArbitrator(),
+                                                   pendingTrans.getKVUpdates(),
+                                                   pendingTrans.getKVGuard());
+               boolean insertedTrans = false;
+               if (s.hasSpace(trans)) {
+                       s.addEntry(trans);
+                       insertedTrans = true;
+               }
+
+               doOptionalRescue(s, seenliveslot, seqn, resize);
+               Pair<Boolean, Slot[]> sendRetData = doSendSlots(s, insertedTrans, resize, newsize);
+
+               if (sendRetData.getFirst()) {
+                       // update the status and change what the sequence number is for the
+                       TransactionStatus transStatus = transactionStatusNotSentMap.remove(pendingTrans.getMachineLocalTransSeqNum());
+                       transStatus.setStatus(TransactionStatus.StatusSent);
+                       transStatus.setSentTransaction();
+                       transactionStatusMap.put(trans.getSequenceNumber(), transStatus);
+               }
+
+
+               if (sendRetData.getSecond().length != 0) {
+                       // insert into the local block chain
+                       validateandupdate(sendRetData.getSecond(), true);
+               }
+
+               return sendRetData.getFirst();
+       }
+
+       private boolean tryput(IoTString keyName, long arbMachineid, boolean resize) throws ServerException {
+               Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
+               int newsize = 0;
+               if (liveslotcount > resizethreshold) {
+                       resize = true; //Resize is forced
+               }
+
+               if (resize) {
+                       newsize = (int) (numslots * RESIZE_MULTIPLE);
+                       TableStatus status = new TableStatus(s, newsize);
+                       s.addEntry(status);
+               }
+
+               doRejectedMessages(s);
+               ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
+
+               // Resize was needed so redo call
+               if (retTup.getFirst()) {
+                       return tryput(keyName, arbMachineid, true);
+               }
+
+               // Extract working variables
+               boolean seenliveslot = retTup.getSecond();
+               long seqn = retTup.getThird();
+
+               doArbitration(s);
+
+               NewKey newKey = new NewKey(s, keyName, arbMachineid);
+
+               boolean insertedNewKey = false;
+               if (s.hasSpace(newKey)) {
+                       s.addEntry(newKey);
+                       insertedNewKey = true;
+               }
+
+               doOptionalRescue(s, seenliveslot, seqn, resize);
+               Pair<Boolean, Slot[]> sendRetData = doSendSlots(s, insertedNewKey, resize, newsize);
+
+               if (sendRetData.getSecond().length != 0) {
+                       // insert into the local block chain
+                       validateandupdate(sendRetData.getSecond(), true);
+               }
+
+               return sendRetData.getFirst();
+       }
+
+       private void doRejectedMessages(Slot s) {
+               if (! rejectedmessagelist.isEmpty()) {
+                       /* TODO: We should avoid generating a rejected message entry if
+                        * there is already a sufficient entry in the queue (e.g.,
+                        * equalsto value of true and same sequence number).  */
+
+                       long old_seqn = rejectedmessagelist.firstElement();
+                       if (rejectedmessagelist.size() > REJECTED_THRESHOLD) {
+                               long new_seqn = rejectedmessagelist.lastElement();
+                               RejectedMessage rm = new RejectedMessage(s, localmachineid, old_seqn, new_seqn, false);
+                               s.addEntry(rm);
+                       } else {
+                               long prev_seqn = -1;
+                               int i = 0;
+                               /* Go through list of missing messages */
+                               for (; i < rejectedmessagelist.size(); i++) {
+                                       long curr_seqn = rejectedmessagelist.get(i);
+                                       Slot s_msg = buffer.getSlot(curr_seqn);
+                                       if (s_msg != null)
+                                               break;
+                                       prev_seqn = curr_seqn;
+                               }
+                               /* Generate rejected message entry for missing messages */
+                               if (prev_seqn != -1) {
+                                       RejectedMessage rm = new RejectedMessage(s, localmachineid, old_seqn, prev_seqn, false);
+                                       s.addEntry(rm);
+                               }
+                               /* Generate rejected message entries for present messages */
+                               for (; i < rejectedmessagelist.size(); i++) {
+                                       long curr_seqn = rejectedmessagelist.get(i);
+                                       Slot s_msg = buffer.getSlot(curr_seqn);
+                                       long machineid = s_msg.getMachineID();
+                                       RejectedMessage rm = new RejectedMessage(s, machineid, curr_seqn, curr_seqn, true);
+                                       s.addEntry(rm);
+                               }
+                       }
+               }
+       }
+
+       private ThreeTuple<Boolean, Boolean, Long> doMandatoryResuce(Slot s, boolean resize) {
+               long newestseqnum = buffer.getNewestSeqNum();
+               long oldestseqnum = buffer.getOldestSeqNum();
+               if (lastliveslotseqn < oldestseqnum)
+                       lastliveslotseqn = oldestseqnum;
+
+               long seqn = lastliveslotseqn;
+               boolean seenliveslot = false;
+               long firstiffull = newestseqnum + 1 - numslots; // smallest seq number in the buffer if it is full
+               long threshold = firstiffull + FREE_SLOTS;      // we want the buffer to be clear of live entries up to this point
+
+
+               // Mandatory Rescue
+               for (; seqn < threshold; seqn++) {
+                       Slot prevslot = buffer.getSlot(seqn);
+                       // Push slot number forward
+                       if (! seenliveslot)
+                               lastliveslotseqn = seqn;
+
+                       if (! prevslot.isLive())
+                               continue;
+                       seenliveslot = true;
+                       Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
+                       for (Entry liveentry : liveentries) {
+                               if (s.hasSpace(liveentry)) {
+                                       s.addEntry(liveentry);
+                               } else if (seqn == firstiffull) { //if there's no space but the entry is about to fall off the queue
+                                       if (!resize) {
+                                               System.out.println("B"); //?
+                                               return new ThreeTuple<Boolean, Boolean, Long>(true, seenliveslot, seqn);
+                                       }
+                               }
+                       }
+               }
+
+               // Did not resize
+               return new ThreeTuple<Boolean, Boolean, Long>(false, seenliveslot, seqn);
+       }
+
+       private boolean doArbitration(Slot s) {
+
+               // flag whether we have finished all arbitration
+               boolean stillHasArbitration = false;
+
+               pendingCommitsToDelete.clear();
+
+               // First add queue commits
+               for (Commit commit : pendingCommitsList) {
+                       if (s.hasSpace(commit)) {
+                               s.addEntry(commit);
+                               pendingCommitsToDelete.add(commit);
+                       } else {
+                               // Ran out of space so move on but still not done
+                               stillHasArbitration = true;
+                               return stillHasArbitration;
+                       }
+               }
+
+               // Arbitrate
+               Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
+               List<Long> transSeqNums = new ArrayList<Long>(uncommittedTransactionsMap.keySet());
+
+               // Sort from oldest to newest
+               Collections.sort(transSeqNums);
+
+               for (Long transNum : transSeqNums) {
+                       Transaction ut = uncommittedTransactionsMap.get(transNum);
+
+                       // Check if this machine arbitrates for this transaction
+                       if (ut.getArbitrator() != localmachineid ) {
+                               continue;
+                       }
+
+                       // we did have something to arbitrate on
+                       stillHasArbitration = true;
+
+                       Entry newEntry = null;
+
+                       if (ut.evaluateGuard(commitedTable, speculativeTableTmp)) {
+                               // Guard evaluated as true
+
+                               // update the local tmp current key set
+                               for (KeyValue kv : ut.getkeyValueUpdateSet()) {
+                                       speculativeTableTmp.put(kv.getKey(), kv);
+                               }
+
+                               // create the commit
+                               newEntry = new Commit(s,
+                                                     ut.getSequenceNumber(),
+                                                     commitSequenceNumber,
+                                                     ut.getArbitrator(),
+                                                     ut.getkeyValueUpdateSet());
+                               commitSequenceNumber = commitSequenceNumber + 1;
+                       } else {
+                               // Guard was false
+
+                               // create the abort
+                               newEntry = new Abort(s,
+                                                    ut.getSequenceNumber(),
+                                                    ut.getMachineID(),
+                                                    ut.getArbitrator());
+                       }
+
+                       if ((newEntry != null) && s.hasSpace(newEntry)) {
+                               s.addEntry(newEntry);
+                       } else {
+                               break;
+                       }
+               }
+
+               return stillHasArbitration;
+       }
+
+       private void deletePendingCommits() {
+               for (Commit com : pendingCommitsToDelete) {
+                       pendingCommitsList.remove(com);
+               }
+               pendingCommitsToDelete.clear();
+       }
+
+       private void  doOptionalRescue(Slot s, boolean seenliveslot, long seqn, boolean resize) {
+               /* now go through live entries from least to greatest sequence number until
+                * either all live slots added, or the slot doesn't have enough room
+                * for SKIP_THRESHOLD consecutive entries*/
+               int skipcount = 0;
+               long newestseqnum = buffer.getNewestSeqNum();
+               search:
+               for (; seqn <= newestseqnum; seqn++) {
+                       Slot prevslot = buffer.getSlot(seqn);
+                       //Push slot number forward
+                       if (!seenliveslot)
+                               lastliveslotseqn = seqn;
+
+                       if (!prevslot.isLive())
+                               continue;
+                       seenliveslot = true;
+                       Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
+                       for (Entry liveentry : liveentries) {
+                               if (s.hasSpace(liveentry))
+                                       s.addEntry(liveentry);
+                               else {
+                                       skipcount++;
+                                       if (skipcount > SKIP_THRESHOLD)
+                                               break search;
+                               }
+                       }
+               }
+       }
+
+       private Pair<Boolean, Slot[]> doSendSlots(Slot s, boolean inserted, boolean resize, int newsize)  throws ServerException {
+               int max = 0;
+               if (resize)
+                       max = newsize;
+
+               Slot[] array = cloud.putSlot(s, max);
+               if (array == null) {
+                       array = new Slot[] {s};
+                       rejectedmessagelist.clear();
+
+                       // Delete pending commits that were sent to the cloud
+                       deletePendingCommits();
+               }       else {
+                       // if (array.length == 0)
+                       // throw new Error("Server Error: Did not send any slots");
+                       rejectedmessagelist.add(s.getSequenceNumber());
+                       inserted = false;
+               }
+
+               return new Pair<Boolean, Slot[]>(inserted, array);
+       }
+
+       private void validateandupdate(Slot[] newslots, boolean acceptupdatestolocal) {
+               /* The cloud communication layer has checked slot HMACs already
+                        before decoding */
+               if (newslots.length == 0) return;
+
+               // Reset the table status declared sizes
+               smallestTableStatusSeen = -1;
+               largestTableStatusSeen = -1;
+
+               long firstseqnum = newslots[0].getSequenceNumber();
+               if (firstseqnum <= sequencenumber) {
+                       throw new Error("Server Error: Sent older slots!");
+               }
+
+               SlotIndexer indexer = new SlotIndexer(newslots, buffer);
+               checkHMACChain(indexer, newslots);
+
+               HashSet<Long> machineSet = new HashSet<Long>(lastmessagetable.keySet()); //
+
+               // initExpectedSize(firstseqnum);
+               for (Slot slot : newslots) {
+                       processSlot(indexer, slot, acceptupdatestolocal, machineSet);
+                       // updateExpectedSize();
+               }
+
+               /* If there is a gap, check to see if the server sent us everything. */
+               if (firstseqnum != (sequencenumber + 1)) {
+
+                       // TODO: Check size
+                       checkNumSlots(newslots.length);
+                       if (!machineSet.isEmpty()) {
+                               throw new Error("Missing record for machines: " + machineSet);
+                       }
+               }
+
+
+               commitNewMaxSize();
+
+               /* Commit new to slots. */
+               for (Slot slot : newslots) {
+                       buffer.putSlot(slot);
+                       liveslotcount++;
+               }
+               sequencenumber = newslots[newslots.length - 1].getSequenceNumber();
+
+               // Process all on key value pairs
+               boolean didCommitOrSpeculate = proccessAllNewCommits();
+
+               // Go through all uncommitted transactions and kill the ones that are dead
+               deleteDeadUncommittedTransactions();
+
+               // Speculate on key value pairs
+               didCommitOrSpeculate |= createSpeculativeTable();
+
+               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+       }
+
+       private boolean proccessAllNewCommits() {
+               // Process only if there are commit
+               if (newCommitMap.keySet().size() == 0) {
+                       return false;
+               }
+               boolean didProcessNewCommit = false;
+
+               for (Long arb : newCommitMap.keySet()) {
+
+                       List<Long> commitSeqNums = new ArrayList<Long>(newCommitMap.get(arb).keySet());
+
+                       // Sort from oldest to newest commit
+                       Collections.sort(commitSeqNums);
+
+                       // Go through each new commit one by one
+                       for (Long entrySeqNum : commitSeqNums) {
+                               Commit entry = newCommitMap.get(arb).get(entrySeqNum);
+
+                               long lastCommitSeenSeqNum = -1;
+                               if (lastCommitSeenSeqNumMap.get(entry.getTransArbitrator()) != null) {
+                                       lastCommitSeenSeqNum = lastCommitSeenSeqNumMap.get(entry.getTransArbitrator());
+                               }
+
+                               if (entry.getSequenceNumber() <= lastCommitSeenSeqNum) {
+                                       Map<Long, Commit> cm = commitMap.get(arb);
+                                       if (cm == null) {
+                                               cm = new HashMap<Long, Commit>();
+                                       }
+
+                                       Commit prevCommit = cm.put(entry.getSequenceNumber(), entry);
+                                       commitMap.put(arb, cm);
+
+                                       if (prevCommit != null) {
+                                               prevCommit.setDead();
+
+                                               for (KeyValue kv : prevCommit.getkeyValueUpdateSet()) {
+                                                       committedMapByKey.put(kv.getKey(), entry);
+                                               }
+                                       }
+
+                                       continue;
+                               }
+
+                               Set<Commit> commitsToEditSet = new HashSet<Commit>();
+
+                               for (KeyValue kv : entry.getkeyValueUpdateSet()) {
+                                       commitsToEditSet.add(committedMapByKey.get(kv.getKey()));
+                               }
+
+                               commitsToEditSet.remove(null);
+
+                               for (Commit prevCommit : commitsToEditSet) {
+
+                                       Set<KeyValue> deletedKV = prevCommit.updateLiveKeys(entry.getkeyValueUpdateSet());
+
+                                       if (!prevCommit.isLive()) {
+                                               Map<Long, Commit> cm = commitMap.get(arb);
+
+                                               // remove it from the map so that it can be set as dead
+                                               if (cm != null) {
+                                                       cm.remove(prevCommit.getSequenceNumber());
+                                                       commitMap.put(arb, cm);
+                                               }
+                                       }
+                               }
+
+                               // Add the new commit
+                               Map<Long, Commit> cm = commitMap.get(arb);
+                               if (cm == null) {
+                                       cm = new HashMap<Long, Commit>();
+                               }
+                               cm.put(entry.getSequenceNumber(), entry);
+                               commitMap.put(arb, cm);
+
+                               lastCommitSeenSeqNumMap.put(entry.getTransArbitrator(), entry.getSequenceNumber());
+
+                               // set the trans sequence number if we are able to
+                               if (entry.getTransSequenceNumber() != -1) {
+                                       lastCommitSeenTransSeqNumMap.put(entry.getTransArbitrator(), entry.getTransSequenceNumber());
+                               }
+
+                               didProcessNewCommit = true;
+
+                               // Update the committed table list
+                               for (KeyValue kv : entry.getkeyValueUpdateSet()) {
+                                       IoTString key = kv.getKey();
+                                       commitedTable.put(key, kv);
+                                       committedMapByKey.put(key, entry);
+                               }
+                       }
+               }
+               // Clear the new commits storage so we can use it later
+               newCommitMap.clear();
+
+
+               // go through all saved transactions and update the status of those that can be updated
+               for (Iterator<Map.Entry<Long, TransactionStatus>> i = transactionStatusMap.entrySet().iterator(); i.hasNext();) {
+                       Map.Entry<Long, TransactionStatus> entry = i.next();
+                       long seqnum = entry.getKey();
+                       TransactionStatus status = entry.getValue();
+
+                       if (status.getSentTransaction()) {
+
+                               Long commitSeqNum = lastCommitSeenTransSeqNumMap.get(status.getArbitrator());
+                               Long abortSeqNum = lastAbortSeenSeqNumMap.get(status.getArbitrator());
+
+                               if (((commitSeqNum != null) && (seqnum <= commitSeqNum)) ||
+                                       ((abortSeqNum != null) && (seqnum <= abortSeqNum))) {
+                                       status.setStatus(TransactionStatus.StatusCommitted);
+                                       i.remove();
+                               }
+                       }
+               }
+
+               return didProcessNewCommit;
+       }
+
+       private void deleteDeadUncommittedTransactions() {
+               // Make dead the transactions
+               for (Iterator<Map.Entry<Long, Transaction>> i = uncommittedTransactionsMap.entrySet().iterator(); i.hasNext();) {
+                       Transaction prevtrans = i.next().getValue();
+                       long transArb = prevtrans.getArbitrator();
+
+                       Long commitSeqNum = lastCommitSeenTransSeqNumMap.get(transArb);
+                       Long abortSeqNum = lastAbortSeenSeqNumMap.get(transArb);
+
+                       if (((commitSeqNum != null) && (prevtrans.getSequenceNumber() <= commitSeqNum)) ||
+                               ((abortSeqNum != null) && (prevtrans.getSequenceNumber() <= abortSeqNum))) {
+                               i.remove();
+                               prevtrans.setDead();
+                       }
+               }
+       }
+
+       private boolean createSpeculativeTable() {
+               if (uncommittedTransactionsMap.keySet().size() == 0) {
+                       return false;
+               }
+
+               Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
+               List<Long> utSeqNums = new ArrayList<Long>(uncommittedTransactionsMap.keySet());
+
+               // Sort from oldest to newest commit
+               Collections.sort(utSeqNums);
+
+               if (utSeqNums.get(0) > (lastUncommittedTransaction)) {
+
+                       speculativeTable.clear();
+                       lastUncommittedTransaction = -1;
+
+                       for (Long key : utSeqNums) {
+                               Transaction trans = uncommittedTransactionsMap.get(key);
+
+                               lastUncommittedTransaction = key;
+
+                               if (trans.evaluateGuard(commitedTable, speculativeTableTmp)) {
+                                       for (KeyValue kv : trans.getkeyValueUpdateSet()) {
+                                               speculativeTableTmp.put(kv.getKey(), kv);
+                                       }
+                               }
+
+                       }
+               } else {
+                       for (Long key : utSeqNums) {
+
+                               if (key <= lastUncommittedTransaction) {
+                                       continue;
+                               }
+
+                               lastUncommittedTransaction = key;
+
+                               Transaction trans = uncommittedTransactionsMap.get(key);
+
+                               if (trans.evaluateGuard(speculativeTable, speculativeTableTmp)) {
+                                       for (KeyValue kv : trans.getkeyValueUpdateSet()) {
+                                               speculativeTableTmp.put(kv.getKey(), kv);
+                                       }
+                               }
+                       }
+               }
+
+               for (IoTString key : speculativeTableTmp.keySet()) {
+                       speculativeTable.put(key, speculativeTableTmp.get(key));
+               }
+
+               return true;
+       }
+
+       private void createPendingTransactionSpeculativeTable(boolean didCommitOrSpeculate) {
+
+               if (didCommitOrSpeculate) {
+                       pendingTransSpeculativeTable.clear();
+                       lastSeenPendingTransactionSpeculateIndex = 0;
+
+                       int index = 0;
+                       for (PendingTransaction pt : pendingTransQueue) {
+                               if (pt.evaluateGuard(commitedTable, speculativeTable, pendingTransSpeculativeTable)) {
+
+                                       lastSeenPendingTransactionSpeculateIndex = index;
+                                       index++;
+
+                                       for (KeyValue kv : pt.getKVUpdates()) {
+                                               pendingTransSpeculativeTable.put(kv.getKey(), kv);
+                                       }
+
+                               }
+                       }
+               }
+       }
+
+       private int expectedsize, currmaxsize;
+
+       private void checkNumSlots(int numslots) {
+
+
+               // We only have 1 size so we must have this many slots
+               if (largestTableStatusSeen == smallestTableStatusSeen) {
+                       if (numslots != smallestTableStatusSeen) {
+                               throw new Error("Server Error: Server did not send all slots.  Expected: " + smallestTableStatusSeen + " Received:" + numslots);
+                       }
+               } else {
+                       // We have more than 1
+                       if (numslots < smallestTableStatusSeen) {
+                               throw new Error("Server Error: Server did not send all slots.  Expected at least: " + smallestTableStatusSeen + " Received:" + numslots);
+                       }
+               }
+
+               // if (numslots != expectedsize) {
+               // throw new Error("Server Error: Server did not send all slots.  Expected: " + expectedsize + " Received:" + numslots);
+               // }
+       }
+
+       private void initExpectedSize(long firstsequencenumber) {
+               long prevslots = firstsequencenumber;
+               expectedsize = (prevslots < ((long) numslots)) ? (int) prevslots : numslots;
+               currmaxsize = numslots;
+       }
+
+       private void updateExpectedSize() {
+               expectedsize++;
+               if (expectedsize > currmaxsize) {
+                       expectedsize = currmaxsize;
+               }
+       }
+
+       private void updateCurrMaxSize(int newmaxsize) {
+               currmaxsize = newmaxsize;
+       }
+
+       private void commitNewMaxSize() {
+
+               if (largestTableStatusSeen == -1) {
+                       currmaxsize = numslots;
+               } else {
+                       currmaxsize = largestTableStatusSeen;
+               }
+
+               if (numslots != currmaxsize) {
+                       buffer.resize(currmaxsize);
+               }
+
+               numslots = currmaxsize;
+               setResizeThreshold();
+       }
+
+       private void processEntry(LastMessage entry, HashSet<Long> machineSet) {
+               updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry, false, machineSet);
+       }
+
+       private void processEntry(RejectedMessage entry, SlotIndexer indexer) {
+               long oldseqnum = entry.getOldSeqNum();
+               long newseqnum = entry.getNewSeqNum();
+               boolean isequal = entry.getEqual();
+               long machineid = entry.getMachineID();
+               for (long seqnum = oldseqnum; seqnum <= newseqnum; seqnum++) {
+                       Slot slot = indexer.getSlot(seqnum);
+                       if (slot != null) {
+                               long slotmachineid = slot.getMachineID();
+                               if (isequal != (slotmachineid == machineid)) {
+                                       throw new Error("Server Error: Trying to insert rejected message for slot " + seqnum);
+                               }
+                       }
+               }
+
+               HashSet<Long> watchset = new HashSet<Long>();
+               for (Map.Entry<Long, Pair<Long, Liveness> > lastmsg_entry : lastmessagetable.entrySet()) {
+                       long entry_mid = lastmsg_entry.getKey();
+                       /* We've seen it, don't need to continue to watch.  Our next
+                        * message will implicitly acknowledge it. */
+                       if (entry_mid == localmachineid)
+                               continue;
+                       Pair<Long, Liveness> v = lastmsg_entry.getValue();
+                       long entry_seqn = v.getFirst();
+                       if (entry_seqn < newseqnum) {
+                               addWatchList(entry_mid, entry);
+                               watchset.add(entry_mid);
+                       }
+               }
+               if (watchset.isEmpty())
+                       entry.setDead();
+               else
+                       entry.setWatchSet(watchset);
+       }
+
+       private void processEntry(NewKey entry) {
+               arbitratorTable.put(entry.getKey(), entry.getMachineID());
+
+               NewKey oldNewKey = newKeyTable.put(entry.getKey(), entry);
+
+               if (oldNewKey != null) {
+                       oldNewKey.setDead();
+               }
+       }
+
+       private void processEntry(Transaction entry) {
+
+               long arb = entry.getArbitrator();
+               Long comLast = lastCommitSeenTransSeqNumMap.get(arb);
+               Long abLast = lastAbortSeenSeqNumMap.get(arb);
+
+               Transaction prevTrans = null;
+
+               if ((comLast != null) && (comLast >= entry.getSequenceNumber())) {
+                       prevTrans = uncommittedTransactionsMap.remove(entry.getSequenceNumber());
+               } else if ((abLast != null) && (abLast >= entry.getSequenceNumber())) {
+                       prevTrans = uncommittedTransactionsMap.remove(entry.getSequenceNumber());
+               } else {
+                       prevTrans = uncommittedTransactionsMap.put(entry.getSequenceNumber(), entry);
+               }
+
+               // Duplicate so delete old copy
+               if (prevTrans != null) {
+                       prevTrans.setDead();
+               }
+       }
+
+       private void processEntry(Abort entry) {
+               if (lastmessagetable.get(entry.getMachineID()).getFirst() < entry.getTransSequenceNumber()) {
+                       // Abort has not been seen yet so we need to keep track of it
+
+                       Abort prevAbort = abortMap.put(entry.getTransSequenceNumber(), entry);
+                       if (prevAbort != null) {
+                               prevAbort.setDead(); // delete old version of the duplicate
+                       }
+
+                       if ((lastAbortSeenSeqNumMap.get(entry.getTransArbitrator()) != null) &&   (entry.getTransSequenceNumber() > lastAbortSeenSeqNumMap.get(entry.getTransArbitrator()))) {
+                               lastAbortSeenSeqNumMap.put(entry.getTransArbitrator(), entry.getTransSequenceNumber());
+                       }
+               } else {
+                       // The machine already saw this so it is dead
+                       entry.setDead();
+               }
+
+               // Update the status of the transaction and remove it since we are done with this transaction
+               TransactionStatus status = transactionStatusMap.remove(entry.getTransSequenceNumber());
+               if (status != null) {
+                       status.setStatus(TransactionStatus.StatusAborted);
+               }
+       }
+
+       private void processEntry(Commit entry) {
+               Map<Long, Commit> arbMap = newCommitMap.get(entry.getTransArbitrator());
+
+               if (arbMap == null) {
+                       arbMap = new HashMap<Long, Commit>();
+               }
+
+               Commit prevCommit = arbMap.put(entry.getSequenceNumber(), entry);
+               newCommitMap.put(entry.getTransArbitrator(), arbMap);
+
+               if (prevCommit != null) {
+                       prevCommit.setDead();
+               }
+       }
+
+       private void processEntry(TableStatus entry) {
+               int newnumslots = entry.getMaxSlots();
+               // updateCurrMaxSize(newnumslots);
+               if (lastTableStatus != null)
+                       lastTableStatus.setDead();
+               lastTableStatus = entry;
+
+               if ((smallestTableStatusSeen == -1) || (newnumslots < smallestTableStatusSeen)) {
+                       smallestTableStatusSeen = newnumslots;
+               }
+
+               if ((largestTableStatusSeen == -1) || (newnumslots > largestTableStatusSeen)) {
+                       largestTableStatusSeen = newnumslots;
+               }
+       }
+
+       private void addWatchList(long machineid, RejectedMessage entry) {
+               HashSet<RejectedMessage> entries = watchlist.get(machineid);
+               if (entries == null)
+                       watchlist.put(machineid, entries = new HashSet<RejectedMessage>());
+               entries.add(entry);
+       }
+
+       private void updateLastMessage(long machineid, long seqnum, Liveness liveness, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
+               machineSet.remove(machineid);
+
+               HashSet<RejectedMessage> watchset = watchlist.get(machineid);
+               if (watchset != null) {
+                       for (Iterator<RejectedMessage> rmit = watchset.iterator(); rmit.hasNext(); ) {
+                               RejectedMessage rm = rmit.next();
+                               if (rm.getNewSeqNum() <= seqnum) {
+                                       /* Remove it from our watchlist */
+                                       rmit.remove();
+                                       /* Decrement machines that need to see this notification */
+                                       rm.removeWatcher(machineid);
+                               }
+                       }
+               }
+
+               if (machineid == localmachineid) {
+                       /* Our own messages are immediately dead. */
+                       if (liveness instanceof LastMessage) {
+                               ((LastMessage)liveness).setDead();
+                       } else if (liveness instanceof Slot) {
+                               ((Slot)liveness).setDead();
+                       } else {
+                               throw new Error("Unrecognized type");
+                       }
+               }
+
+               // Set dead the abort
+               for (Iterator<Map.Entry<Long, Abort>> i = abortMap.entrySet().iterator(); i.hasNext();) {
+                       Abort abort = i.next().getValue();
+
+                       if ((abort.getMachineID() == machineid) && (abort.getTransSequenceNumber() <= seqnum)) {
+                               abort.setDead();
+                               i.remove();
+                       }
+               }
+
+               Pair<Long, Liveness> lastmsgentry = lastmessagetable.put(machineid, new Pair<Long, Liveness>(seqnum, liveness));
+               if (lastmsgentry == null)
+                       return;
+
+               long lastmsgseqnum = lastmsgentry.getFirst();
+               Liveness lastentry = lastmsgentry.getSecond();
+               if (machineid != localmachineid) {
+                       if (lastentry instanceof LastMessage) {
+                               ((LastMessage)lastentry).setDead();
+                       } else if (lastentry instanceof Slot) {
+                               ((Slot)lastentry).setDead();
+                       } else {
+                               throw new Error("Unrecognized type");
+                       }
+               }
+
+               if (machineid == localmachineid) {
+                       if (lastmsgseqnum != seqnum && !acceptupdatestolocal)
+                               throw new Error("Server Error: Mismatch on local machine sequence number, needed: " +  seqnum + " got: " + lastmsgseqnum);
+               } else {
+                       if (lastmsgseqnum > seqnum)
+                               throw new Error("Server Error: Rollback on remote machine sequence number");
+               }
+       }
+
+       private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
+               updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet);
+               for (Entry entry : slot.getEntries()) {
+                       switch (entry.getType()) {
+
+                       case Entry.TypeNewKey:
+                               processEntry((NewKey)entry);
+                               break;
+
+                       case Entry.TypeCommit:
+                               processEntry((Commit)entry);
+                               break;
+
+                       case Entry.TypeAbort:
+                               processEntry((Abort)entry);
+                               break;
+
+                       case Entry.TypeTransaction:
+                               processEntry((Transaction)entry);
+                               break;
+
+                       case Entry.TypeLastMessage:
+                               processEntry((LastMessage)entry, machineSet);
+                               break;
+
+                       case Entry.TypeRejectedMessage:
+                               processEntry((RejectedMessage)entry, indexer);
+                               break;
+
+                       case Entry.TypeTableStatus:
+                               processEntry((TableStatus)entry);
+                               break;
+
+                       default:
+                               throw new Error("Unrecognized type: " + entry.getType());
+                       }
+               }
+       }
+
+       private void checkHMACChain(SlotIndexer indexer, Slot[] newslots) {
+               for (int i = 0; i < newslots.length; i++) {
+                       Slot currslot = newslots[i];
+                       Slot prevslot = indexer.getSlot(currslot.getSequenceNumber() - 1);
+                       if (prevslot != null &&
+                               !Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC()))
+                               throw new Error("Server Error: Invalid HMAC Chain" + currslot + " " + prevslot);
+               }
+       }
+}
\ No newline at end of file
diff --git a/version2/backup/src/java/iotcloud/TableStatus.java b/version2/backup/src/java/iotcloud/TableStatus.java
new file mode 100644 (file)
index 0000000..62f3a6d
--- /dev/null
@@ -0,0 +1,45 @@
+package iotcloud;
+import java.nio.ByteBuffer;
+
+/**
+ * TableStatus entries record the current size of the data structure
+ * in slots.  Used to remember the size and to perform resizes.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+
+class TableStatus extends Entry {
+       private int maxslots;
+
+       TableStatus(Slot slot, int _maxslots) {
+               super(slot);
+               maxslots=_maxslots;
+       }
+
+       int getMaxSlots() {
+               return maxslots;
+       }
+
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               int maxslots=bb.getInt();
+               return new TableStatus(slot, maxslots);
+       }
+
+       void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeTableStatus);
+               bb.putInt(maxslots);
+       }
+
+       int getSize() {
+               return Integer.BYTES+Byte.BYTES;
+       }
+
+       byte getType() {
+               return Entry.TypeTableStatus;
+       }
+
+       Entry getCopy(Slot s) {
+               return new TableStatus(s, maxslots);
+       }
+}
diff --git a/version2/backup/src/java/iotcloud/Test.java b/version2/backup/src/java/iotcloud/Test.java
new file mode 100644 (file)
index 0000000..0de2e5c
--- /dev/null
@@ -0,0 +1,1878 @@
+package iotcloud;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Test cases.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+public class Test {
+
+       public static final  int NUMBER_OF_TESTS = 100;
+
+       public static void main(String[] args)  throws ServerException, InterruptedException {
+               if (args[0].equals("2")) {
+                       test2();
+               } else if (args[0].equals("3")) {
+                       test3();
+               } else if (args[0].equals("4")) {
+                       test4();
+               } else if (args[0].equals("5")) {
+                       test5();
+               } else if (args[0].equals("6")) {
+                       test6();
+               } else if (args[0].equals("7")) {
+                       test7();
+               } else if (args[0].equals("8")) {
+                       test8();
+               } else if (args[0].equals("9")) {
+                       test9();
+               } else if (args[0].equals("10")) {
+                       test10();
+               } else if (args[0].equals("11")) {
+                       test11();
+               }
+       }
+
+
+       static void test11() throws InterruptedException {
+
+               boolean foundError = false;
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               while (true) {
+                       try {
+                               t1.initTable();
+                               break;
+                       } catch (Exception e) {}
+               }
+
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               while (t2.update().getFirst() == false) {}
+
+               LocalComm lc = new LocalComm(t1, t2);
+               t1.addLocalComm(t2.getId(), lc);
+               t2.addLocalComm(t1.getId(), lc);
+
+               // Make the Keys
+               System.out.println("Setting up keys");
+               for (int i = 0; i < 4; i++) {
+                       String a = "a" + i;
+                       String b = "b" + i;
+                       String c = "c" + i;
+                       String d = "d" + i;
+                       IoTString ia = new IoTString(a);
+                       IoTString ib = new IoTString(b);
+                       IoTString ic = new IoTString(c);
+                       IoTString id = new IoTString(d);
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ia, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ib, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(ic, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(id, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+               }
+
+               // Do Updates for the keys
+               System.out.println("Setting Key-Values...");
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyA = "a" + i;
+                               String valueA = "a" + (i + t);
+
+                               IoTString iKeyA = new IoTString(keyA);
+                               IoTString iValueA = new IoTString(valueA);
+
+                               t1.startTransaction();
+                               t1.addKV(iKeyA, iValueA);
+                               t1.commitTransaction();
+                       }
+               }
+
+               t2.updateFromLocal(321);
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < 4; i++) {
+
+                       String keyA = "a" + i;
+                       String valueA = "a" + (i + NUMBER_OF_TESTS - 1);
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iValueA = new IoTString(valueA);
+
+                       IoTString testValA1 = t1.getCommitted(iKeyA);
+                       IoTString testValA2 = t2.getCommitted(iKeyA);
+
+                       if ((testValA1 == null) || (testValA1.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyA + "    " + testValA1 + "    " + iValueA);
+                               foundError = true;
+                       }
+
+                       if ((testValA2 == null) || (testValA2.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyA + "    " + testValA2 + "    " + iValueA);
+                               foundError = true;
+                       }
+               }
+
+               if (foundError) {
+                       System.out.println("Found Errors...");
+               } else {
+                       System.out.println("No Errors Found...");
+               }
+       }
+
+       static void test10() throws InterruptedException {
+
+               boolean foundError = false;
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               while (true) {
+                       try {
+                               t1.rebuild();
+                               break;
+                       } catch (Exception e) {}
+               }
+
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               while (true) {
+                       try {
+                               t2.rebuild();
+                               break;
+                       } catch (Exception e) {}
+               }
+
+               LocalComm lc = new LocalComm(t1, t2);
+               t1.addLocalComm(t2.getId(), lc);
+               t2.addLocalComm(t1.getId(), lc);
+
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < 4; i++) {
+
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + (i + NUMBER_OF_TESTS - 1);
+                       String valueB = "b" + (i + NUMBER_OF_TESTS - 1);
+                       String valueC = "c" + (i + NUMBER_OF_TESTS - 1);
+                       String valueD = "d" + (i + NUMBER_OF_TESTS - 1);
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+
+                       IoTString testValA1 = t1.getCommitted(iKeyA);
+                       IoTString testValB1 = t1.getCommitted(iKeyB);
+                       IoTString testValC1 = t1.getCommitted(iKeyC);
+                       IoTString testValD1 = t1.getCommitted(iKeyD);
+
+                       IoTString testValA2 = t2.getCommitted(iKeyA);
+                       IoTString testValB2 = t2.getCommitted(iKeyB);
+                       IoTString testValC2 = t2.getCommitted(iKeyC);
+                       IoTString testValD2 = t2.getCommitted(iKeyD);
+
+                       if ((testValA1 == null) || (testValA1.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyA + "    " + testValA1 + "    " + iValueA);
+                               foundError = true;
+                       }
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB + "    " + testValB1 + "    " + iValueB);
+                               foundError = true;
+                       }
+
+                       if ((testValC1 == null) || (testValC1.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC + "    " + testValC1 + "    " + iValueC);
+                               foundError = true;
+                       }
+
+                       if ((testValD1 == null) || (testValD1.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyD + "    " + testValD1 + "    " + iValueD);
+                               foundError = true;
+                       }
+
+                       if ((testValA2 == null) || (testValA2.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyA + "    " + testValA2 + "    " + iValueA);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 == null) || (testValB2.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2 + "    " + iValueB);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "    " + testValC2 + "    " + iValueC);
+                               foundError = true;
+                       }
+
+                       if ((testValD2 == null) || (testValD2.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyD + "    " + testValD2 + "    " + iValueD);
+                               foundError = true;
+                       }
+               }
+
+               if (foundError) {
+                       System.out.println("Found Errors...");
+               } else {
+                       System.out.println("No Errors Found...");
+               }
+       }
+
+       static void test9() throws InterruptedException {
+
+               boolean foundError = false;
+               List<TransactionStatus> transStatusList = new ArrayList<TransactionStatus>();
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               while (true) {
+                       try {
+                               t1.initTable();
+                               break;
+                       } catch (Exception e) {}
+               }
+
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               while (t2.update().getFirst() == false) {}
+
+               // LocalComm lc = new LocalComm(t1, t2);
+               // t1.addLocalComm(t2.getId(), lc);
+               // t2.addLocalComm(t1.getId(), lc);
+
+
+
+               // Make the Keys
+               System.out.println("Setting up keys");
+               for (int i = 0; i < 4; i++) {
+                       String a = "a" + i;
+                       String b = "b" + i;
+                       String c = "c" + i;
+                       String d = "d" + i;
+                       IoTString ia = new IoTString(a);
+                       IoTString ib = new IoTString(b);
+                       IoTString ic = new IoTString(c);
+                       IoTString id = new IoTString(d);
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ia, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ib, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(ic, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(id, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+               }
+
+               // Do Updates for the keys
+               System.out.println("Setting Key-Values...");
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyB = "b" + i;
+                               String valueB = "b" + (i + t);
+
+                               IoTString iKeyB = new IoTString(keyB);
+                               IoTString iValueB = new IoTString(valueB);
+
+
+                               t1.startTransaction();
+                               t1.getSpeculativeAtomic(iKeyB);
+                               t1.addKV(iKeyB, iValueB);
+                               transStatusList.add(t1.commitTransaction());
+                       }
+               }
+
+               while (t1.update().getFirst() == false) {}
+
+
+               for (int i = 0; i < 4; i++) {
+
+                       String keyB = "b" + i;
+                       String valueB = "b" + (i + NUMBER_OF_TESTS - 1);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iValueB = new IoTString(valueB);
+
+                       IoTString testValB1 = t1.getSpeculative(iKeyB);
+                       IoTString testValB2 = t2.getSpeculative(iKeyB);
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB + "    " + testValB1);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 != null)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2);
+                               foundError = true;
+                       }
+               }
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyC = "c" + i;
+                               String valueC = "c" + (i + t);
+
+                               IoTString iKeyC = new IoTString(keyC);
+                               IoTString iValueC = new IoTString(valueC);
+
+                               t2.startTransaction();
+                               t2.getSpeculativeAtomic(iKeyC);
+                               t2.addKV(iKeyC, iValueC);
+                               transStatusList.add(t2.commitTransaction());
+                       }
+               }
+
+               while (t2.update().getFirst() == false) {}
+
+               for (int i = 0; i < 4; i++) {
+                       String keyC = "c" + i;
+                       String valueC = "c" + (i + NUMBER_OF_TESTS - 1);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iValueC = new IoTString(valueC);
+
+                       IoTString testValC1 = t1.getSpeculative(iKeyC);
+                       IoTString testValC2 = t2.getSpeculative(iKeyC);
+
+                       if ((testValC1 != null)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC + "   " + testValC1);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "   " + testValC2);
+                               foundError = true;
+                       }
+               }
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyA = "a" + i;
+                               String keyD = "d" + i;
+                               String valueA = "a" + (i + t);
+                               String valueD = "d" + (i + t);
+
+                               IoTString iKeyA = new IoTString(keyA);
+                               IoTString iKeyD = new IoTString(keyD);
+                               IoTString iValueA = new IoTString(valueA);
+                               IoTString iValueD = new IoTString(valueD);
+
+
+                               t1.startTransaction();
+                               t1.addKV(iKeyA, iValueA);
+                               transStatusList.add(t1.commitTransaction());
+
+                               t2.startTransaction();
+                               t2.addKV(iKeyD, iValueD);
+                               transStatusList.add(t2.commitTransaction());
+                       }
+               }
+
+               System.out.println("Updating Clients...");
+               while (t1.update().getFirst() == false) {}
+               while (t2.update().getFirst() == false) {}
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < 4; i++) {
+
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + (i + NUMBER_OF_TESTS - 1);
+                       String valueB = "b" + (i + NUMBER_OF_TESTS - 1);
+                       String valueC = "c" + (i + NUMBER_OF_TESTS - 1);
+                       String valueD = "d" + (i + NUMBER_OF_TESTS - 1);
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+
+                       IoTString testValA1 = t1.getCommitted(iKeyA);
+                       IoTString testValB1 = t1.getCommitted(iKeyB);
+                       IoTString testValC1 = t1.getCommitted(iKeyC);
+                       IoTString testValD1 = t1.getCommitted(iKeyD);
+
+                       IoTString testValA2 = t2.getCommitted(iKeyA);
+                       IoTString testValB2 = t2.getCommitted(iKeyB);
+                       IoTString testValC2 = t2.getCommitted(iKeyC);
+                       IoTString testValD2 = t2.getCommitted(iKeyD);
+
+                       if ((testValA1 == null) || (testValA1.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyA + "    " + testValA1 + "    " + iValueA);
+                               foundError = true;
+                       }
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB + "    " + testValB1 + "    " + iValueB);
+                               foundError = true;
+                       }
+
+                       if ((testValC1 == null) || (testValC1.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC + "    " + testValC1 + "    " + iValueC);
+                               foundError = true;
+                       }
+
+                       if ((testValD1 == null) || (testValD1.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyD + "    " + testValD1 + "    " + iValueD);
+                               foundError = true;
+                       }
+
+                       if ((testValA2 == null) || (testValA2.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyA + "    " + testValA2 + "    " + iValueA);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 == null) || (testValB2.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2 + "    " + iValueB);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "    " + testValC2 + "    " + iValueC);
+                               foundError = true;
+                       }
+
+                       if ((testValD2 == null) || (testValD2.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyD + "    " + testValD2 + "    " + iValueD);
+                               foundError = true;
+                       }
+               }
+
+               for (TransactionStatus status : transStatusList) {
+                       if (status.getStatus() != TransactionStatus.StatusCommitted) {
+                               foundError = true;
+                               System.out.println(status.getStatus());
+                       }
+               }
+
+               if (foundError) {
+                       System.out.println("Found Errors...");
+               } else {
+                       System.out.println("No Errors Found...");
+               }
+       }
+
+       static void test8() throws InterruptedException {
+
+               boolean foundError = false;
+               List<TransactionStatus> transStatusList = new ArrayList<TransactionStatus>();
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               while (true) {
+                       try {
+                               t1.initTable();
+                               break;
+                       } catch (Exception e) {}
+               }
+
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               while (t2.update().getFirst() == false) {}
+
+               LocalComm lc = new LocalComm(t1, t2);
+               t1.addLocalComm(t2.getId(), lc);
+               t2.addLocalComm(t1.getId(), lc);
+
+               // Make the Keys
+               System.out.println("Setting up keys");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+                       System.out.println(i);
+
+                       String a = "a" + i;
+                       String b = "b" + i;
+                       String c = "c" + i;
+                       String d = "d" + i;
+                       IoTString ia = new IoTString(a);
+                       IoTString ib = new IoTString(b);
+                       IoTString ic = new IoTString(c);
+                       IoTString id = new IoTString(d);
+
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ia, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t1.createNewKey(ib, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(ic, 321);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+
+                       while (true) {
+                               try {
+                                       t2.createNewKey(id, 351);
+                                       break;
+                               } catch (Exception e) {}
+                       }
+               }
+
+               // Do Updates for the keys
+               System.out.println("Setting Key-Values...");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+                       System.out.println(i);
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + i;
+                       String valueB = "b" + i;
+                       String valueC = "c" + i;
+                       String valueD = "d" + i;
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+
+                       String keyAPrev = "a" + (i - 1);
+                       String keyBPrev = "b" + (i - 1);
+                       String keyCPrev = "c" + (i - 1);
+                       String keyDPrev = "d" + (i - 1);
+                       String valueAPrev = "a" + (i - 1);
+                       String valueBPrev = "b" + (i - 1);
+                       String valueCPrev = "c" + (i - 1);
+                       String valueDPrev = "d" + (i - 1);
+
+                       IoTString iKeyAPrev = new IoTString(keyAPrev);
+                       IoTString iKeyBPrev = new IoTString(keyBPrev);
+                       IoTString iKeyCPrev = new IoTString(keyCPrev);
+                       IoTString iKeyDPrev = new IoTString(keyDPrev);
+                       IoTString iValueAPrev = new IoTString(valueAPrev);
+                       IoTString iValueBPrev = new IoTString(valueBPrev);
+                       IoTString iValueCPrev = new IoTString(valueCPrev);
+                       IoTString iValueDPrev = new IoTString(valueDPrev);
+
+                       t1.startTransaction();
+                       if (i != 0) {
+                               IoTString tmp = t1.getSpeculative(iKeyAPrev);
+                               if ((tmp == null) || !tmp.equals(iValueAPrev)) {
+                                       System.out.println("Key a Error: " + i);
+                                       foundError = true;
+                               }
+                       }
+                       t1.addKV(iKeyA, iValueA);
+                       transStatusList.add(t1.commitTransaction());
+
+
+
+                       t1.startTransaction();
+                       if (i != 0) {
+                               IoTString tmp = t1.getSpeculative(iKeyBPrev);
+                               if ((tmp == null) || !tmp.equals(iValueBPrev)) {
+                                       System.out.println("Key b Error: " + i);
+                                       foundError = true;
+                               }
+                       }
+                       t1.addKV(iKeyB, iValueB);
+                       transStatusList.add(t1.commitTransaction());
+
+
+                       t2.startTransaction();
+                       if (i != 0) {
+                               IoTString tmp = t2.getSpeculative(iKeyCPrev);
+                               if ((tmp == null) || !tmp.equals(iValueCPrev)) {
+                                       System.out.println("Key c Error: " + i);
+                                       foundError = true;
+                               }
+                       }
+                       t2.addKV(iKeyC, iValueC);
+                       transStatusList.add(t2.commitTransaction());
+
+                       t2.startTransaction();
+                       if (i != 0) {
+                               IoTString tmp = t2.getSpeculative(iKeyDPrev);
+                               if ((tmp == null) || !tmp.equals(iValueDPrev)) {
+                                       System.out.println("Key d Error: " + i);
+                                       foundError = true;
+                               }
+                       }
+                       t2.addKV(iKeyD, iValueD);
+                       transStatusList.add(t2.commitTransaction());
+               }
+
+
+               System.out.println("Updating...");
+               while (t1.update().getFirst() == false) {}
+               while (t2.update().getFirst() == false) {}
+
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + i;
+                       String valueB = "b" + i;
+                       String valueC = "c" + i;
+                       String valueD = "d" + i;
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+
+                       IoTString testValA1 = t1.getCommitted(iKeyA);
+                       IoTString testValB1 = t1.getCommitted(iKeyB);
+                       IoTString testValC1 = t1.getCommitted(iKeyC);
+                       IoTString testValD1 = t1.getCommitted(iKeyD);
+
+                       IoTString testValA2 = t2.getCommitted(iKeyA);
+                       IoTString testValB2 = t2.getCommitted(iKeyB);
+                       IoTString testValC2 = t2.getCommitted(iKeyC);
+                       IoTString testValD2 = t2.getCommitted(iKeyD);
+
+                       if ((testValA1 == null) || (testValA1.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyA);
+                               foundError = true;
+                       }
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB);
+                               foundError = true;
+                       }
+
+                       if ((testValC1 == null) || (testValC1.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC);
+                               foundError = true;
+                       }
+
+                       if ((testValD1 == null) || (testValD1.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyD);
+                               foundError = true;
+                       }
+
+
+                       if ((testValA2 == null) || (testValA2.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyA + "    " + testValA2);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 == null) || (testValB2.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "    " + testValC2);
+                               foundError = true;
+                       }
+
+                       if ((testValD2 == null) || (testValD2.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyD + "    " + testValD2);
+                               foundError = true;
+                       }
+               }
+
+               for (TransactionStatus status : transStatusList) {
+                       if (status.getStatus() != TransactionStatus.StatusCommitted) {
+                               foundError = true;
+                       }
+               }
+               if (foundError) {
+                       System.out.println("Found Errors...");
+               } else {
+                       System.out.println("No Errors Found...");
+               }
+       }
+
+       static void test7() throws ServerException, InterruptedException {
+
+               boolean foundError = false;
+               List<TransactionStatus> transStatusList = new ArrayList<TransactionStatus>();
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               t1.initTable();
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               t2.update();
+
+               // Make the Keys
+               System.out.println("Setting up keys");
+               for (int i = 0; i < 4; i++) {
+                       String a = "a" + i;
+                       String b = "b" + i;
+                       String c = "c" + i;
+                       String d = "d" + i;
+                       IoTString ia = new IoTString(a);
+                       IoTString ib = new IoTString(b);
+                       IoTString ic = new IoTString(c);
+                       IoTString id = new IoTString(d);
+                       t1.createNewKey(ia, 321);
+                       t1.createNewKey(ib, 351);
+                       t2.createNewKey(ic, 321);
+                       t2.createNewKey(id, 351);
+               }
+
+               // Do Updates for the keys
+               System.out.println("Setting Key-Values...");
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyB = "b" + i;
+                               String valueB = "b" + (i + t);
+
+                               IoTString iKeyB = new IoTString(keyB);
+                               IoTString iValueB = new IoTString(valueB);
+
+                               t1.startTransaction();
+                               t1.getSpeculativeAtomic(iKeyB);
+                               t1.addKV(iKeyB, iValueB);
+                               transStatusList.add(t1.commitTransaction());
+                       }
+               }
+
+               for (int i = 0; i < 4; i++) {
+
+                       String keyB = "b" + i;
+                       String valueB = "b" + (i + NUMBER_OF_TESTS - 1);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iValueB = new IoTString(valueB);
+
+                       IoTString testValB1 = t1.getSpeculative(iKeyB);
+                       IoTString testValB2 = t2.getSpeculative(iKeyB);
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB + "    " + testValB1);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 != null)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2);
+                               foundError = true;
+                       }
+               }
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyC = "c" + i;
+                               String valueC = "c" + (i + t);
+
+                               IoTString iKeyC = new IoTString(keyC);
+                               IoTString iValueC = new IoTString(valueC);
+
+                               t2.startTransaction();
+                               t2.getSpeculativeAtomic(iKeyC);
+                               t2.addKV(iKeyC, iValueC);
+                               transStatusList.add(t2.commitTransaction());
+                       }
+               }
+
+               for (int i = 0; i < 4; i++) {
+                       String keyC = "c" + i;
+                       String valueC = "c" + (i + NUMBER_OF_TESTS - 1);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iValueC = new IoTString(valueC);
+
+                       IoTString testValC1 = t1.getSpeculative(iKeyC);
+                       IoTString testValC2 = t2.getSpeculative(iKeyC);
+
+                       if ((testValC1 != null)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC + "   " + testValC1);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "   " + testValC2);
+                               foundError = true;
+                       }
+               }
+
+               for (int t = 0; t < NUMBER_OF_TESTS; t++) {
+                       for (int i = 0; i < 4; i++) {
+                               String keyA = "a" + i;
+                               String keyD = "d" + i;
+                               String valueA = "a" + (i + t);
+                               String valueD = "d" + (i + t);
+
+                               IoTString iKeyA = new IoTString(keyA);
+                               IoTString iKeyD = new IoTString(keyD);
+                               IoTString iValueA = new IoTString(valueA);
+                               IoTString iValueD = new IoTString(valueD);
+
+                               t1.startTransaction();
+                               t1.addKV(iKeyA, iValueA);
+                               transStatusList.add(t1.commitTransaction());
+
+                               t2.startTransaction();
+                               t2.addKV(iKeyD, iValueD);
+                               transStatusList.add(t2.commitTransaction());
+                       }
+               }
+
+               System.out.println("Updating Clients...");
+               t1.update();
+               t2.update();
+               t1.update();
+               t2.update();
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < 4; i++) {
+
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + (i + NUMBER_OF_TESTS - 1);
+                       String valueB = "b" + (i + NUMBER_OF_TESTS - 1);
+                       String valueC = "c" + (i + NUMBER_OF_TESTS - 1);
+                       String valueD = "d" + (i + NUMBER_OF_TESTS - 1);
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+
+                       IoTString testValA1 = t1.getCommitted(iKeyA);
+                       IoTString testValB1 = t1.getCommitted(iKeyB);
+                       IoTString testValC1 = t1.getCommitted(iKeyC);
+                       IoTString testValD1 = t1.getCommitted(iKeyD);
+
+                       IoTString testValA2 = t2.getCommitted(iKeyA);
+                       IoTString testValB2 = t2.getCommitted(iKeyB);
+                       IoTString testValC2 = t2.getCommitted(iKeyC);
+                       IoTString testValD2 = t2.getCommitted(iKeyD);
+
+                       if ((testValA1 == null) || (testValA1.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyA + "    " + testValA1);
+                               foundError = true;
+                       }
+
+                       if ((testValB1 == null) || (testValB1.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyB + "    " + testValB1);
+                               foundError = true;
+                       }
+
+                       if ((testValC1 == null) || (testValC1.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyC + "    " + testValC1);
+                               foundError = true;
+                       }
+
+                       if ((testValD1 == null) || (testValD1.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t1 incorrect: " + keyD + "    " + testValD1);
+                               foundError = true;
+                       }
+
+                       if ((testValA2 == null) || (testValA2.equals(iValueA) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyA + "    " + testValA2);
+                               foundError = true;
+                       }
+
+                       if ((testValB2 == null) || (testValB2.equals(iValueB) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyB + "    " + testValB2);
+                               foundError = true;
+                       }
+
+                       if ((testValC2 == null) || (testValC2.equals(iValueC) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyC + "    " + testValC2);
+                               foundError = true;
+                       }
+
+                       if ((testValD2 == null) || (testValD2.equals(iValueD) == false)) {
+                               System.out.println("Key-Value t2 incorrect: " + keyD + "    " + testValD2);
+                               foundError = true;
+                       }
+               }
+
+               for (TransactionStatus status : transStatusList) {
+                       if (status.getStatus() != TransactionStatus.StatusCommitted) {
+                               foundError = true;
+                       }
+               }
+
+               if (foundError) {
+                       System.out.println("Found Errors...");
+               } else {
+                       System.out.println("No Errors Found...");
+               }
+       }
+
+       static void test6() throws ServerException, InterruptedException {
+
+               boolean foundError = false;
+               List<TransactionStatus> transStatusList = new ArrayList<TransactionStatus>();
+
+               // Setup the 2 clients
+               Table t1 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 321);
+               t1.initTable();
+               Table t2 = new Table("127.0.0.1", "http://127.0.0.1/test.iotcloud/", "reallysecret", 351);
+               t2.update();
+
+               // Make the Keys
+               System.out.println("Setting up keys");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+                       String a = "a" + i;
+                       String b = "b" + i;
+                       String c = "c" + i;
+                       String d = "d" + i;
+                       IoTString ia = new IoTString(a);
+                       IoTString ib = new IoTString(b);
+                       IoTString ic = new IoTString(c);
+                       IoTString id = new IoTString(d);
+                       t1.createNewKey(ia, 321);
+                       t1.createNewKey(ib, 351);
+                       t2.createNewKey(ic, 321);
+                       t2.createNewKey(id, 351);
+               }
+
+               // Do Updates for the keys
+               System.out.println("Setting Key-Values...");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + i;
+                       String valueB = "b" + i;
+                       String valueC = "c" + i;
+                       String valueD = "d" + i;
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+                       IoTString iValueB = new IoTString(valueB);
+                       IoTString iValueC = new IoTString(valueC);
+                       IoTString iValueD = new IoTString(valueD);
+
+                       t1.startTransaction();
+                       t1.getCommittedAtomic(iKeyA);
+                       t1.addKV(iKeyA, iValueA);
+                       transStatusList.add(t1.commitTransaction());
+
+                       t1.startTransaction();
+                       t1.getCommittedAtomic(iKeyB);
+                       t1.addKV(iKeyB, iValueB);
+                       transStatusList.add(t1.commitTransaction());
+
+                       t2.startTransaction();
+                       t2.getCommittedAtomic(iKeyC);
+                       t2.addKV(iKeyC, iValueC);
+                       transStatusList.add(t2.commitTransaction());
+
+                       t2.startTransaction();
+                       t2.getCommittedAtomic(iKeyD);
+                       t2.addKV(iKeyD, iValueD);
+                       transStatusList.add(t2.commitTransaction());
+               }
+
+               System.out.println("Updating Clients...");
+               t1.update();
+               t2.update();
+
+               System.out.println("Checking Key-Values...");
+               for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+
+                       String keyA = "a" + i;
+                       String keyB = "b" + i;
+                       String keyC = "c" + i;
+                       String keyD = "d" + i;
+                       String valueA = "a" + i;
+                       String valueB = "b" + i;
+                       String valueC = "c" + i;
+                       String valueD = "d" + i;
+
+                       IoTString iKeyA = new IoTString(keyA);
+                       IoTString iKeyB = new IoTString(keyB);
+                       IoTString iKeyC = new IoTString(keyC);
+                       IoTString iKeyD = new IoTString(keyD);
+                       IoTString iValueA = new IoTString(valueA);
+           &nbs