1- pragma solidity ^ 0.4.24 ;
1+ pragma solidity >= 0.5.0 < 0.6.0 ;
22
33import {D} from "./data.sol " ;
44import {Utils} from "./utils.sol " ;
@@ -21,18 +21,18 @@ library PatriciaTree {
2121 D.Edge rootEdge;
2222 }
2323
24- function get (Tree storage tree , bytes key ) internal view returns (bytes ) {
24+ function get (Tree storage tree , bytes memory key ) internal view returns (bytes memory ) {
2525 return getValue (tree, _findNode (tree, key));
2626 }
2727
28- function safeGet (Tree storage tree , bytes key ) internal view returns (bytes value ) {
28+ function safeGet (Tree storage tree , bytes memory key ) internal view returns (bytes memory value ) {
2929 bytes32 valueHash = _findNode (tree, key);
3030 require (valueHash != bytes32 (0 ));
3131 value = getValue (tree, valueHash);
3232 require (valueHash == keccak256 (value));
3333 }
3434
35- function doesInclude (Tree storage tree , bytes key ) internal view returns (bool ) {
35+ function doesInclude (Tree storage tree , bytes memory key ) internal view returns (bool ) {
3636 return doesIncludeHashedKey (tree, keccak256 (key));
3737 }
3838
@@ -41,7 +41,7 @@ library PatriciaTree {
4141 return (valueHash != bytes32 (0 ));
4242 }
4343
44- function getValue (Tree storage tree , bytes32 valueHash ) internal view returns (bytes ) {
44+ function getValue (Tree storage tree , bytes32 valueHash ) internal view returns (bytes memory ) {
4545 return tree.values[valueHash];
4646 }
4747
@@ -76,11 +76,11 @@ library PatriciaTree {
7676 // - uint branchMask - bitmask with high bits at the positions in the key
7777 // where we have branch nodes (bit in key denotes direction)
7878 // - bytes32[] hashes - hashes of sibling edges
79- function getProof (Tree storage tree , bytes key ) internal view returns (uint branchMask , bytes32 [] _siblings ) {
79+ function getProof (Tree storage tree , bytes memory key ) internal view returns (uint branchMask , bytes32 [] memory _siblings ) {
8080 return getProofWithHashedKey (tree, keccak256 (key));
8181 }
8282
83- function getProofWithHashedKey (Tree storage tree , bytes32 hashedKey ) internal view returns (uint branchMask , bytes32 [] _siblings ) {
83+ function getProofWithHashedKey (Tree storage tree , bytes32 hashedKey ) internal view returns (uint branchMask , bytes32 [] memory _siblings ) {
8484 D.Label memory k = D.Label (hashedKey, 256 );
8585 D.Edge memory e = tree.rootEdge;
8686 bytes32 [256 ] memory siblings;
@@ -113,11 +113,11 @@ library PatriciaTree {
113113 }
114114 }
115115
116- function getNonInclusionProof (Tree storage tree , bytes key ) internal view returns (
116+ function getNonInclusionProof (Tree storage tree , bytes memory key ) internal view returns (
117117 bytes32 potentialSiblingLabel ,
118118 bytes32 potentialSiblingValue ,
119119 uint branchMask ,
120- bytes32 [] _siblings
120+ bytes32 [] memory _siblings
121121 ) {
122122 return getNonInclusionProofWithHashedKey (tree, keccak256 (key));
123123 }
@@ -126,7 +126,7 @@ library PatriciaTree {
126126 bytes32 potentialSiblingLabel ,
127127 bytes32 potentialSiblingValue ,
128128 uint branchMask ,
129- bytes32 [] _siblings
129+ bytes32 [] memory _siblings
130130 ){
131131 uint length;
132132 uint numSiblings;
@@ -170,7 +170,7 @@ library PatriciaTree {
170170 }
171171 }
172172
173- function verifyProof (bytes32 rootHash , bytes key , bytes value , uint branchMask , bytes32 [] siblings ) public pure {
173+ function verifyProof (bytes32 rootHash , bytes memory key , bytes memory value , uint branchMask , bytes32 [] memory siblings ) public pure {
174174 D.Label memory k = D.Label (keccak256 (key), 256 );
175175 D.Edge memory e;
176176 e.node = keccak256 (value);
@@ -189,7 +189,7 @@ library PatriciaTree {
189189 require (rootHash == edgeHash (e));
190190 }
191191
192- function verifyNonInclusionProof (bytes32 rootHash , bytes key , bytes32 potentialSiblingLabel , bytes32 potentialSiblingValue , uint branchMask , bytes32 [] siblings ) public pure {
192+ function verifyNonInclusionProof (bytes32 rootHash , bytes memory key , bytes32 potentialSiblingLabel , bytes32 potentialSiblingValue , uint branchMask , bytes32 [] memory siblings ) public pure {
193193 D.Label memory k = D.Label (keccak256 (key), 256 );
194194 D.Edge memory e;
195195 for (uint i = 0 ; branchMask != 0 ; i++ ) {
@@ -213,7 +213,7 @@ library PatriciaTree {
213213 }
214214
215215 // TODO also return the proof
216- function insert (Tree storage tree , bytes key , bytes value ) internal {
216+ function insert (Tree storage tree , bytes memory key , bytes memory value ) internal {
217217 D.Label memory k = D.Label (keccak256 (key), 256 );
218218 bytes32 valueHash = keccak256 (value);
219219 tree.values[valueHash] = value;
@@ -233,21 +233,26 @@ library PatriciaTree {
233233 tree.rootEdge = e;
234234 }
235235
236- function _insertAtNode (Tree storage tree , bytes32 nodeHash , D.Label key , bytes32 value ) private returns (bytes32 ) {
237- require (key.length > 1 );
236+ function _insertAtNode (
237+ Tree storage tree ,
238+ bytes32 nodeHash ,
239+ D.Label memory key ,
240+ bytes32 value
241+ ) private returns (bytes32 ) {
242+ require (key.length > 1 , "Bad key " );
238243 D.Node memory n = tree.nodes[nodeHash];
239- uint head;
240- D.Label memory tail;
241- (head, tail) = Utils.chopFirstBit (key);
244+ (uint256 head , D.Label memory tail ) = Utils.chopFirstBit (key);
242245 n.children[head] = _insertAtEdge (tree, n.children[head], tail, value);
243246 return _replaceNode (tree, nodeHash, n);
244247 }
245248
246- function _insertAtEdge (Tree storage tree , D.Edge e , D.Label key , bytes32 value ) private returns (D.Edge) {
247- require (key.length >= e.label.length );
248- D.Label memory prefix;
249- D.Label memory suffix;
250- (prefix, suffix) = Utils.splitCommonPrefix (key, e.label);
249+ function _insertAtEdge (
250+ Tree storage tree ,
251+ D.Edge memory e ,
252+ D.Label memory key , bytes32 value
253+ ) private returns (D.Edge memory ) {
254+ require (key.length >= e.label.length , "Key lenght mismatch label lenght " );
255+ (D.Label memory prefix , D.Label memory suffix ) = Utils.splitCommonPrefix (key, e.label);
251256 bytes32 newNodeHash;
252257 if (suffix.length == 0 ) {
253258 // Full match with the key, update operation
@@ -257,9 +262,7 @@ library PatriciaTree {
257262 newNodeHash = _insertAtNode (tree, e.node, suffix, value);
258263 } else {
259264 // Mismatch, so let us create a new branch node.
260- uint head;
261- D.Label memory tail;
262- (head, tail) = Utils.chopFirstBit (suffix);
265+ (uint256 head , D.Label memory tail ) = Utils.chopFirstBit (suffix);
263266 D.Node memory branchNode;
264267 branchNode.children[head] = D.Edge (value, tail);
265268 branchNode.children[1 - head] = D.Edge (e.node, Utils.removePrefix (e.label, prefix.length + 1 ));
@@ -275,12 +278,16 @@ library PatriciaTree {
275278 return h;
276279 }
277280
278- function _replaceNode (Tree storage tree , bytes32 oldHash , D.Node memory n ) private returns (bytes32 newHash ) {
281+ function _replaceNode (
282+ Tree storage tree ,
283+ bytes32 oldHash ,
284+ D.Node memory n
285+ ) private returns (bytes32 newHash ) {
279286 delete tree.nodes[oldHash];
280287 return _insertNode (tree, n);
281288 }
282289
283- function _findNode (Tree storage tree , bytes key ) private view returns (bytes32 ) {
290+ function _findNode (Tree storage tree , bytes memory key ) private view returns (bytes32 ) {
284291 return _findNodeWithHashedKey (tree, keccak256 (key));
285292 }
286293
@@ -293,20 +300,16 @@ library PatriciaTree {
293300 }
294301 }
295302
296- function _findAtNode (Tree storage tree , bytes32 nodeHash , D.Label key ) private view returns (bytes32 ) {
303+ function _findAtNode (Tree storage tree , bytes32 nodeHash , D.Label memory key ) private view returns (bytes32 ) {
297304 require (key.length > 1 );
298305 D.Node memory n = tree.nodes[nodeHash];
299- uint head;
300- D.Label memory tail;
301- (head, tail) = Utils.chopFirstBit (key);
306+ (uint head , D.Label memory tail ) = Utils.chopFirstBit (key);
302307 return _findAtEdge (tree, n.children[head], tail);
303308 }
304309
305- function _findAtEdge (Tree storage tree , D.Edge e , D.Label key ) private view returns (bytes32 ){
310+ function _findAtEdge (Tree storage tree , D.Edge memory e , D.Label memory key ) private view returns (bytes32 ){
306311 require (key.length >= e.label.length );
307- D.Label memory prefix;
308- D.Label memory suffix;
309- (prefix, suffix) = Utils.splitCommonPrefix (key, e.label);
312+ (D.Label memory prefix , D.Label memory suffix ) = Utils.splitCommonPrefix (key, e.label);
310313 if (suffix.length == 0 ) {
311314 // Full match with the key, update operation
312315 return e.node;
0 commit comments