@@ -13,8 +13,6 @@ class BatchAVLProverSerializer[D <: Digest, HF <: CryptographicHash[D]](implicit
1313
1414 type SlicedTree = (BatchAVLProverManifest [D ], Seq [BatchAVLProverSubtree [D ]])
1515
16- def slice (tree : BatchAVLProver [D , HF ]): SlicedTree = slice(tree, tree.rootNodeHeight / 2 )
17-
1816 /**
1917 * Slice AVL tree to top subtree tree (BatchAVLProverManifest) and
2018 * bottom subtrees (BatchAVLProverSubtree) with height `subtreeDepth`
@@ -31,87 +29,88 @@ class BatchAVLProverSerializer[D <: Digest, HF <: CryptographicHash[D]](implicit
3129 currentNode match {
3230 case n : InternalProverNode [D ] if currentHeight > subtreeDepth =>
3331 val nextParent = ProxyInternalNode (n)
34- parent.mutate (nextParent)
32+ parent.setChild (nextParent)
3533 val leftSubtrees = getSubtrees(n.left, currentHeight - 1 , nextParent)
3634 val rightSubtrees = getSubtrees(n.right, currentHeight - 1 , nextParent)
3735 leftSubtrees ++ rightSubtrees
3836 case n : InternalProverNode [D ] =>
39- parent.mutate (ProxyInternalNode (n))
37+ parent.setChild (ProxyInternalNode (n))
4038 Seq (BatchAVLProverSubtree (n.left), BatchAVLProverSubtree (n.right))
4139 case l : ProverLeaf [D ] =>
42- parent.mutate (l)
40+ parent.setChild (l)
4341 Seq (BatchAVLProverSubtree (l))
4442 }
4543 }
4644
4745 val subtrees = getSubtrees(tn.left, height - 1 , rootProxyNode) ++ getSubtrees(tn.right, height - 1 , rootProxyNode)
48- val manifest = BatchAVLProverManifest [D ](tree.keyLength, tree.valueLengthOpt, ( rootProxyNode, height) )
46+ val manifest = BatchAVLProverManifest [D ](rootProxyNode, height)
4947 (manifest, subtrees)
5048 case l : ProverLeaf [D ] =>
51- (BatchAVLProverManifest [D ](tree.keyLength, tree.valueLengthOpt, ( l, tree.rootNodeHeight) ), Seq .empty)
49+ (BatchAVLProverManifest [D ](l, tree.rootNodeHeight), Seq .empty)
5250 }
5351
5452 /**
5553 * Combine tree pieces into one big tree
5654 */
57- def combine (sliced : SlicedTree ): Try [BatchAVLProver [D , HF ]] = Try {
58- sliced._1.rootAndHeight._1 match {
55+ def combine (sliced : SlicedTree ,
56+ keyLength : Int ,
57+ valueLengthOpt : Option [Int ]): Try [BatchAVLProver [D , HF ]] = Try {
58+ val manifest = sliced._1
59+ manifest.root match {
5960 case tn : InternalProverNode [D ] =>
61+
62+ // manifest being mutated here
6063 def mutateLoop (n : ProverNodes [D ]): Unit = n match {
6164 case n : ProxyInternalNode [D ] if n.isEmpty =>
62- val left = sliced._2.find(_.subtreeTop.label sameElements n.leftLabel).get.subtreeTop
63- val right = sliced._2.find(_.subtreeTop.label sameElements n.rightLabel).get.subtreeTop
64- n.mutate (left)
65- n.mutate (right)
65+ val left = sliced._2.find(_.id sameElements n.leftLabel).get.subtreeTop
66+ val right = sliced._2.find(_.id sameElements n.rightLabel).get.subtreeTop
67+ n.setChild (left)
68+ n.setChild (right)
6669 case n : InternalProverNode [D ] =>
6770 mutateLoop(n.left)
6871 mutateLoop(n.right)
6972 case _ =>
7073 }
7174
7275 mutateLoop(tn)
73- new BatchAVLProver [D , HF ](sliced._1.keyLength, sliced._1.valueLengthOpt, Some (sliced._1.rootAndHeight))
7476 case _ : ProverLeaf [D ] =>
75- new BatchAVLProver [D , HF ](sliced._1.keyLength, sliced._1.valueLengthOpt, Some (sliced._1.rootAndHeight))
7677 }
78+
79+ new BatchAVLProver [D , HF ](keyLength, valueLengthOpt, Some (manifest.root -> manifest.rootHeight))
7780 }
7881
7982 def manifestToBytes (manifest : BatchAVLProverManifest [D ]): Array [Byte ] = {
80- Bytes .concat(Ints .toByteArray(manifest.keyLength),
81- Ints .toByteArray(manifest.valueLengthOpt.getOrElse(- 1 )),
82- Ints .toByteArray(manifest.rootAndHeight._2),
83- nodesToBytes(manifest.rootAndHeight._1)
83+ Bytes .concat(
84+ Ints .toByteArray(manifest.rootHeight),
85+ nodesToBytes(manifest.root)
8486 )
8587 }
8688
87- def manifestFromBytes (bytes : Array [Byte ]): Try [BatchAVLProverManifest [D ]] = Try {
88- val keyLength = Ints .fromByteArray(bytes.slice(0 , 4 ))
89- val valueLength = Ints .fromByteArray(bytes.slice(4 , 8 ))
90- if (valueLength < - 1 ) throw new Error (s " Wrong valueLength: $valueLength" )
91- val valueLengthOpt = if (valueLength == - 1 ) None else Some (valueLength)
92- val oldHeight = Ints .fromByteArray(bytes.slice(8 , 12 ))
93- val oldTop = nodesFromBytes(bytes.slice(12 , bytes.length), keyLength).get
94- BatchAVLProverManifest [D ](keyLength, valueLengthOpt, (oldTop, oldHeight))
89+ def manifestFromBytes (bytes : Array [Byte ],
90+ keyLength : Int ): Try [BatchAVLProverManifest [D ]] = Try {
91+ val oldHeight = Ints .fromByteArray(bytes.slice(0 , 4 ))
92+ val oldTop = nodesFromBytes(bytes.slice(4 , bytes.length), keyLength).get
93+ BatchAVLProverManifest [D ](oldTop, oldHeight)
9594 }
9695
9796 def subtreeToBytes (t : BatchAVLProverSubtree [D ]): Array [Byte ] = nodesToBytes(t.subtreeTop)
9897
9998 def subtreeFromBytes (b : Array [Byte ], kl : Int ): Try [BatchAVLProverSubtree [D ]] = nodesFromBytes(b, kl).
10099 map(topNode => BatchAVLProverSubtree [D ](topNode))
101100
102- def nodesToBytes (obj : ProverNodes [D ]): Array [Byte ] = {
101+ def nodesToBytes (rootNode : ProverNodes [D ]): Array [Byte ] = {
103102 def loop (currentNode : ProverNodes [D ]): Array [Byte ] = currentNode match {
104103 case l : ProverLeaf [D ] =>
105104 Bytes .concat(Array (0 .toByte), l.key, l.nextLeafKey, l.value)
106105 case n : ProxyInternalNode [D ] if n.isEmpty =>
107- Bytes .concat(Array (2 .toByte, n.balance), n.key, n.leftLabel, n.rightLabel, n.label )
106+ Bytes .concat(Array (2 .toByte, n.balance), n.key, n.leftLabel, n.rightLabel)
108107 case n : InternalProverNode [D ] =>
109108 val leftBytes = loop(n.left)
110109 val rightBytes = loop(n.right)
111110 Bytes .concat(Array (1 .toByte, n.balance), n.key, Ints .toByteArray(leftBytes.length), leftBytes, rightBytes)
112111 }
113112
114- loop(obj )
113+ loop(rootNode )
115114 }
116115
117116 def nodesFromBytes (bytesIn : Array [Byte ], keyLength : Int ): Try [ProverNodes [D ]] = Try {
@@ -135,8 +134,7 @@ class BatchAVLProverSerializer[D <: Digest, HF <: CryptographicHash[D]](implicit
135134 val key = ADKey @@ bytes.slice(2 , keyLength + 2 )
136135 val leftLabel = hf.byteArrayToDigest(bytes.slice(keyLength + 2 , keyLength + 2 + labelLength)).get
137136 val rightLabel = hf.byteArrayToDigest(bytes.slice(keyLength + 2 + labelLength, keyLength + 2 + 2 * labelLength)).get
138- val selfLabel = hf.byteArrayToDigest(bytes.slice(keyLength + 2 + 2 * labelLength, keyLength + 2 + 3 * labelLength)).get
139- new ProxyInternalNode [D ](key, Some (selfLabel), leftLabel, rightLabel, balance)
137+ new ProxyInternalNode [D ](key, leftLabel, rightLabel, balance)
140138 case _ =>
141139 ???
142140 }
0 commit comments