Skip to content

Commit d47e162

Browse files
committed
Update solidity 0.5.0 compatibility
1 parent 3ee9ab6 commit d47e162

File tree

7 files changed

+5966
-228
lines changed

7 files changed

+5966
-228
lines changed

contracts/Migrations.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.23;
1+
pragma solidity >=0.5.0 <0.6.0;
22

33
contract Migrations {
44
address public owner;

contracts/data.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity >=0.5.0 <0.6.0;
22

33
/**
44
MIT License
@@ -19,4 +19,5 @@ library D {
1919
struct Node {
2020
Edge[2] children;
2121
}
22-
}
22+
}
23+

contracts/implementation.sol

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.24;
1+
pragma solidity >=0.5.0 <0.6.0;
22

33
import {D} from "./data.sol";
44
import {PatriciaTree} from "./tree.sol";
@@ -10,23 +10,23 @@ contract PatriciaTreeImplementation {
1010
constructor () public {
1111
}
1212

13-
function insert(bytes key, bytes value) public {
13+
function insert(bytes memory key, bytes memory value) public {
1414
tree.insert(key, value);
1515
}
1616

17-
function get(bytes key) public view returns (bytes) {
17+
function get(bytes memory key) public view returns (bytes memory) {
1818
return tree.get(key);
1919
}
2020

21-
function safeGet(bytes key) public view returns (bytes) {
21+
function safeGet(bytes memory key) public view returns (bytes memory) {
2222
return tree.safeGet(key);
2323
}
2424

25-
function doesInclude(bytes key) public view returns (bool) {
25+
function doesInclude(bytes memory key) public view returns (bool) {
2626
return tree.doesInclude(key);
2727
}
2828

29-
function getValue(bytes32 hash) public view returns (bytes) {
29+
function getValue(bytes32 hash) public view returns (bytes memory) {
3030
return tree.values[hash];
3131
}
3232

@@ -42,24 +42,24 @@ contract PatriciaTreeImplementation {
4242
return tree.getRootEdge();
4343
}
4444

45-
function getProof(bytes key) public view returns (uint branchMask, bytes32[] _siblings) {
45+
function getProof(bytes memory key) public view returns (uint branchMask, bytes32[] memory _siblings) {
4646
return tree.getProof(key);
4747
}
4848

49-
function getNonInclusionProof(bytes key) public view returns (
49+
function getNonInclusionProof(bytes memory key) public view returns (
5050
bytes32 leafLabel,
5151
bytes32 leafNode,
5252
uint branchMask,
53-
bytes32[] _siblings
53+
bytes32[] memory _siblings
5454
) {
5555
return tree.getNonInclusionProof(key);
5656
}
5757

58-
function verifyProof(bytes32 rootHash, bytes key, bytes value, uint branchMask, bytes32[] siblings) public pure {
58+
function verifyProof(bytes32 rootHash, bytes memory key, bytes memory value, uint branchMask, bytes32[] memory siblings) public pure {
5959
PatriciaTree.verifyProof(rootHash, key, value, branchMask, siblings);
6060
}
6161

62-
function verifyNonInclusionProof(bytes32 rootHash, bytes key, bytes32 leafLabel, bytes32 leafNode, uint branchMask, bytes32[] siblings) public pure {
62+
function verifyNonInclusionProof(bytes32 rootHash, bytes memory key, bytes32 leafLabel, bytes32 leafNode, uint branchMask, bytes32[] memory siblings) public pure {
6363
PatriciaTree.verifyNonInclusionProof(rootHash, key, leafLabel, leafNode, branchMask, siblings);
6464
}
6565
}
@@ -112,7 +112,7 @@ contract PatriciaTreeMerkleProof {
112112
targetRoot = PatriciaTree.edgeHash(targetRootEdge);
113113
}
114114

115-
function insert(bytes key, bytes value) public {
115+
function insert(bytes memory key, bytes memory value) public {
116116
bytes32 k = keccak256(value);
117117
committedValues[k] = true;
118118
tree.insert(key, value);
@@ -140,7 +140,7 @@ contract PatriciaTreeMerkleProof {
140140
tree.nodes[nodeHash].children[1] = e1;
141141
}
142142

143-
function commitValue(bytes value) public onlyFor(Status.OPENED) {
143+
function commitValue(bytes memory value) public onlyFor(Status.OPENED) {
144144
bytes32 k = keccak256(value);
145145
committedValues[k] = true;
146146
tree.values[k] = value;
@@ -185,7 +185,7 @@ contract PatriciaTreeMerkleProof {
185185
return true;
186186
}
187187

188-
function _isLeaf(D.Edge _edge) internal view returns (bool) {
188+
function _isLeaf(D.Edge memory _edge) internal view returns (bool) {
189189
return (tree.nodes[_edge.node].children[0].node == 0 && tree.nodes[_edge.node].children[1].node == 0);
190190
}
191191

@@ -199,4 +199,4 @@ contract PatriciaTreeMerkleProof {
199199
status = _status;
200200
emit OnChangeStatus(status);
201201
}
202-
}
202+
}

contracts/tree.sol

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.24;
1+
pragma solidity >=0.5.0 <0.6.0;
22

33
import {D} from "./data.sol";
44
import {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

Comments
 (0)