@@ -84,10 +84,10 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
84
84
def get (key : K ): Option [V ] = {
85
85
pathTraverse[Option [V ]](None , mkKeyNibbles(key)) { case (_, node) =>
86
86
node match {
87
- case LeafNode (_, value, _, _, _) =>
87
+ case Some ( LeafNode (_, value, _, _, _) ) =>
88
88
Some (vSerializer.fromBytes(value.toArray[Byte ]))
89
89
90
- case BranchNode (_, terminator, _, _, _) =>
90
+ case Some ( BranchNode (_, terminator, _, _, _) ) =>
91
91
terminator.map(term => vSerializer.fromBytes(term.toArray[Byte ]))
92
92
93
93
case _ => None
@@ -103,9 +103,9 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
103
103
* @throws io.iohk.ethereum.mpt.MerklePatriciaTrie.MPTException if there is any inconsistency in how the trie is build.
104
104
*/
105
105
def getProof (key : K ): Option [Vector [MptNode ]] = {
106
- pathTraverseWithProof [Vector [MptNode ]](Vector .empty, mkKeyNibbles(key)) { case (acc, node) =>
106
+ pathTraverse [Vector [MptNode ]](Vector .empty, mkKeyNibbles(key)) { case (acc, node) =>
107
107
node match {
108
- case nextNodeOnExt @ (_ : BranchNode | _ : ExtensionNode | _ : LeafNode ) => acc :+ nextNodeOnExt
108
+ case Some ( nextNodeOnExt @ (_ : BranchNode | _ : ExtensionNode | _ : LeafNode ) ) => acc :+ nextNodeOnExt
109
109
case _ => acc
110
110
}
111
111
}
@@ -121,25 +121,25 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
121
121
* @tparam T accumulator type
122
122
* @return accumulated data or None if key doesn't exist
123
123
*/
124
- private def pathTraverse [T ](acc : T , searchKey : Array [Byte ])(op : (T , MptNode ) => T ): Option [T ] = {
124
+ private def pathTraverse [T ](acc : T , searchKey : Array [Byte ])(op : (T , Option [ MptNode ] ) => T ): Option [T ] = {
125
125
126
126
@ tailrec
127
- def pathTraverse (acc : T , node : MptNode , searchKey : Array [Byte ], op : (T , MptNode ) => T ): Option [T ] = {
127
+ def pathTraverse (acc : T , node : MptNode , searchKey : Array [Byte ], op : (T , Option [ MptNode ] ) => T ): Option [T ] = {
128
128
node match {
129
129
case LeafNode (key, _, _, _, _) =>
130
- if (key.toArray[Byte ] sameElements searchKey) Some (op(acc, node)) else None
130
+ if (key.toArray[Byte ] sameElements searchKey) Some (op(acc, Some ( node))) else Some (op(acc, None ))
131
131
132
132
case extNode @ ExtensionNode (sharedKey, _, _, _, _) =>
133
133
val (commonKey, remainingKey) = searchKey.splitAt(sharedKey.length)
134
134
if (searchKey.length >= sharedKey.length && (sharedKey.toArray[Byte ] sameElements commonKey)) {
135
- pathTraverse(op(acc, node), extNode.next, remainingKey, op)
136
- } else None
135
+ pathTraverse(op(acc, Some ( node) ), extNode.next, remainingKey, op)
136
+ } else Some (op(acc, None ))
137
137
138
138
case branch : BranchNode =>
139
- if (searchKey.isEmpty) Some (op(acc, node))
139
+ if (searchKey.isEmpty) Some (op(acc, Some ( node) ))
140
140
else
141
141
pathTraverse(
142
- op(acc, node),
142
+ op(acc, Some ( node) ),
143
143
branch.children(searchKey(0 )),
144
144
searchKey.slice(1 , searchKey.length),
145
145
op
@@ -149,7 +149,7 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
149
149
pathTraverse(acc, getFromHash(bytes, nodeStorage), searchKey, op)
150
150
151
151
case NullNode =>
152
- None
152
+ Some (op(acc, None ))
153
153
}
154
154
}
155
155
@@ -161,46 +161,6 @@ class MerklePatriciaTrie[K, V] private (private[mpt] val rootNode: Option[MptNod
161
161
}
162
162
}
163
163
164
- private def pathTraverseWithProof [T ](acc : T , searchKey : Array [Byte ])(op : (T , MptNode ) => T ): Option [T ] = {
165
-
166
- @ tailrec
167
- def pathTraverseWithProof (acc : T , node : MptNode , searchKey : Array [Byte ], op : (T , MptNode ) => T ): Option [T ] = {
168
- node match {
169
- case LeafNode (key, _, _, _, _) =>
170
- if (key.toArray[Byte ] sameElements searchKey) Some (op(acc, node)) else Some (acc)
171
-
172
- case extNode @ ExtensionNode (sharedKey, _, _, _, _) =>
173
- val (commonKey, remainingKey) = searchKey.splitAt(sharedKey.length)
174
- if (searchKey.length >= sharedKey.length && (sharedKey.toArray[Byte ] sameElements commonKey)) {
175
- pathTraverseWithProof(op(acc, node), extNode.next, remainingKey, op)
176
- } else Some (acc)
177
-
178
- case branch : BranchNode =>
179
- if (searchKey.isEmpty) Some (op(acc, node))
180
- else
181
- pathTraverseWithProof(
182
- op(acc, node),
183
- branch.children(searchKey(0 )),
184
- searchKey.slice(1 , searchKey.length),
185
- op
186
- )
187
-
188
- case HashNode (bytes) =>
189
- pathTraverseWithProof(acc, getFromHash(bytes, nodeStorage), searchKey, op)
190
-
191
- case NullNode =>
192
- Some (acc)
193
- }
194
- }
195
-
196
- rootNode match {
197
- case Some (root) =>
198
- pathTraverseWithProof(acc, root, searchKey, op)
199
- case None =>
200
- None
201
- }
202
- }
203
-
204
164
private def getFromHash (nodeId : Array [Byte ], source : MptStorage ): MptNode = {
205
165
val nodeEncoded = source.get(nodeId).encode
206
166
MptTraversals
0 commit comments