Skip to content

Commit 231f1b5

Browse files
committed
Update solidity 0.5.0 compatibility
1 parent 84b6d3b commit 231f1b5

File tree

6 files changed

+197
-118
lines changed

6 files changed

+197
-118
lines changed

Diff for: contracts/Migrations.sol

+1-1
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;

Diff for: contracts/data.sol

+4-4
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
@@ -21,11 +21,11 @@ library D {
2121
Edge[2] children;
2222
}
2323

24-
function isEmpty(Edge edge) internal pure returns (bool) {
24+
function isEmpty(Edge memory edge) internal pure returns (bool) {
2525
return (edge.header == bytes32(0) && edge.node == bytes32(0));
2626
}
2727

28-
function hasNode(Edge edge) internal pure returns (bool) {
28+
function hasNode(Edge memory edge) internal pure returns (bool) {
2929
return (edge.node != bytes32(0));
3030
}
31-
}
31+
}

Diff for: contracts/implementation.sol

+14-14
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 {PartialMerkleTree} from "./tree.sol";
44

@@ -13,56 +13,56 @@ contract PartialMerkleTreeImplementation {
1313
tree.initialize(initialRoot);
1414
}
1515

16-
function insert(bytes key, bytes value) public {
16+
function insert(bytes memory key, bytes memory value) public {
1717
tree.insert(key, value);
1818
}
1919

20-
function commitBranch(bytes key, bytes value, uint branchMask, bytes32[] siblings) public {
20+
function commitBranch(bytes memory key, bytes memory value, uint branchMask, bytes32[] memory siblings) public {
2121
return tree.commitBranch(key, value, branchMask, siblings);
2222
}
2323

24-
function commitBranchOfNonInclusion(bytes key, bytes32 potentialSiblingLabel, bytes32 potentialSiblingValue, uint branchMask, bytes32[] siblings) public {
24+
function commitBranchOfNonInclusion(bytes memory key, bytes32 potentialSiblingLabel, bytes32 potentialSiblingValue, uint branchMask, bytes32[] memory siblings) public {
2525
return tree.commitBranchOfNonInclusion(key, potentialSiblingLabel, potentialSiblingValue, branchMask, siblings);
2626
}
2727

28-
function get(bytes key) public view returns (bytes) {
28+
function get(bytes memory key) public view returns (bytes memory) {
2929
return tree.get(key);
3030
}
3131

32-
function safeGet(bytes key) public view returns (bytes) {
32+
function safeGet(bytes memory key) public view returns (bytes memory) {
3333
return tree.safeGet(key);
3434
}
3535

36-
function doesInclude(bytes key) public view returns (bool) {
36+
function doesInclude(bytes memory key) public view returns (bool) {
3737
return tree.doesInclude(key);
3838
}
3939

40-
function getValue(bytes32 hash) public view returns (bytes) {
40+
function getValue(bytes32 hash) public view returns (bytes memory) {
4141
return tree.values[hash];
4242
}
4343

4444
function getRootHash() public view returns (bytes32) {
4545
return tree.getRootHash();
4646
}
4747

48-
function getProof(bytes key) public view returns (uint branchMask, bytes32[] _siblings) {
48+
function getProof(bytes memory key) public view returns (uint branchMask, bytes32[] memory _siblings) {
4949
return tree.getProof(key);
5050
}
5151

52-
function getNonInclusionProof(bytes key) public view returns (
52+
function getNonInclusionProof(bytes memory key) public view returns (
5353
bytes32 leafLabel,
5454
bytes32 leafNode,
5555
uint branchMask,
56-
bytes32[] _siblings
56+
bytes32[] memory _siblings
5757
) {
5858
return tree.getNonInclusionProof(key);
5959
}
6060

61-
function verifyProof(bytes32 rootHash, bytes key, bytes value, uint branchMask, bytes32[] siblings) public pure {
61+
function verifyProof(bytes32 rootHash, bytes memory key, bytes memory value, uint branchMask, bytes32[] memory siblings) public pure {
6262
PartialMerkleTree.verifyProof(rootHash, key, value, branchMask, siblings);
6363
}
6464

65-
function verifyNonInclusionProof(bytes32 rootHash, bytes key, bytes32 leafLabel, bytes32 leafNode, uint branchMask, bytes32[] siblings) public pure {
65+
function verifyNonInclusionProof(bytes32 rootHash, bytes memory key, bytes32 leafLabel, bytes32 leafNode, uint branchMask, bytes32[] memory siblings) public pure {
6666
PartialMerkleTree.verifyNonInclusionProof(rootHash, key, leafLabel, leafNode, branchMask, siblings);
6767
}
68-
}
68+
}

Diff for: contracts/tree.sol

+45-22
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 {Utils} from "./utils.sol";
44
import {D} from "./data.sol";
@@ -27,7 +27,13 @@ library PartialMerkleTree {
2727
tree.root = root;
2828
}
2929

30-
function commitBranch(Tree storage tree, bytes key, bytes value, uint branchMask, bytes32[] siblings) internal {
30+
function commitBranch(
31+
Tree storage tree,
32+
bytes memory key,
33+
bytes memory value,
34+
uint branchMask,
35+
bytes32[] memory siblings
36+
) internal {
3137
D.Label memory k = D.Label(keccak256(key), 256);
3238
D.Edge memory e;
3339
e.node = keccak256(value);
@@ -66,11 +72,11 @@ library PartialMerkleTree {
6672

6773
function commitBranchOfNonInclusion(
6874
Tree storage tree,
69-
bytes key,
75+
bytes memory key,
7076
bytes32 potentialSiblingLabel,
7177
bytes32 potentialSiblingValue,
7278
uint branchMask,
73-
bytes32[] siblings
79+
bytes32[] memory siblings
7480
) internal {
7581
D.Label memory k = D.Label(keccak256(key), 256);
7682
D.Edge memory e;
@@ -114,7 +120,11 @@ library PartialMerkleTree {
114120
tree.rootEdge = e;
115121
}
116122

117-
function insert(Tree storage tree, bytes key, bytes value) internal {
123+
function insert(
124+
Tree storage tree,
125+
bytes memory key,
126+
bytes memory value
127+
) internal {
118128
D.Label memory k = D.Label(keccak256(key), 256);
119129
bytes32 valueHash = keccak256(value);
120130
tree.values[valueHash] = value;
@@ -134,18 +144,18 @@ library PartialMerkleTree {
134144
tree.rootEdge = e;
135145
}
136146

137-
function get(Tree storage tree, bytes key) internal view returns (bytes) {
147+
function get(Tree storage tree, bytes memory key) internal view returns (bytes memory) {
138148
return getValue(tree, _findNode(tree, key));
139149
}
140150

141-
function safeGet(Tree storage tree, bytes key) internal view returns (bytes value) {
151+
function safeGet(Tree storage tree, bytes memory key) internal view returns (bytes memory value) {
142152
bytes32 valueHash = _findNode(tree, key);
143153
require(valueHash != bytes32(0));
144154
value = getValue(tree, valueHash);
145155
require(valueHash == keccak256(value));
146156
}
147157

148-
function doesInclude(Tree storage tree, bytes key) internal view returns (bool) {
158+
function doesInclude(Tree storage tree, bytes memory key) internal view returns (bool) {
149159
return doesIncludeHashedKey(tree, keccak256(key));
150160
}
151161

@@ -154,7 +164,7 @@ library PartialMerkleTree {
154164
return (valueHash != bytes32(0));
155165
}
156166

157-
function getValue(Tree storage tree, bytes32 valueHash) internal view returns (bytes) {
167+
function getValue(Tree storage tree, bytes32 valueHash) internal view returns (bytes memory) {
158168
return tree.values[valueHash];
159169
}
160170

@@ -181,11 +191,11 @@ library PartialMerkleTree {
181191
// - uint branchMask - bitmask with high bits at the positions in the key
182192
// where we have branch nodes (bit in key denotes direction)
183193
// - bytes32[] hashes - hashes of sibling edges
184-
function getProof(Tree storage tree, bytes key) internal view returns (uint branchMask, bytes32[] _siblings) {
194+
function getProof(Tree storage tree, bytes memory key) internal view returns (uint branchMask, bytes32[] memory _siblings) {
185195
return getProofWithHashedKey(tree, keccak256(key));
186196
}
187197

188-
function getProofWithHashedKey(Tree storage tree, bytes32 hashedKey) internal view returns (uint branchMask, bytes32[] _siblings) {
198+
function getProofWithHashedKey(Tree storage tree, bytes32 hashedKey) internal view returns (uint branchMask, bytes32[] memory _siblings) {
189199
D.Label memory k = D.Label(hashedKey, 256);
190200
D.Edge memory e = tree.rootEdge;
191201
bytes32[256] memory siblings;
@@ -218,11 +228,11 @@ library PartialMerkleTree {
218228
}
219229
}
220230

221-
function getNonInclusionProof(Tree storage tree, bytes key) internal view returns (
231+
function getNonInclusionProof(Tree storage tree, bytes memory key) internal view returns (
222232
bytes32 potentialSiblingLabel,
223233
bytes32 potentialSiblingValue,
224234
uint branchMask,
225-
bytes32[] _siblings
235+
bytes32[] memory _siblings
226236
) {
227237
return getNonInclusionProofWithHashedKey(tree, keccak256(key));
228238
}
@@ -231,7 +241,7 @@ library PartialMerkleTree {
231241
bytes32 potentialSiblingLabel,
232242
bytes32 potentialSiblingValue,
233243
uint branchMask,
234-
bytes32[] _siblings
244+
bytes32[] memory _siblings
235245
){
236246
uint length;
237247
uint numSiblings;
@@ -275,7 +285,13 @@ library PartialMerkleTree {
275285
}
276286
}
277287

278-
function verifyProof(bytes32 rootHash, bytes key, bytes value, uint branchMask, bytes32[] siblings) public pure {
288+
function verifyProof(
289+
bytes32 rootHash,
290+
bytes memory key,
291+
bytes memory value,
292+
uint branchMask,
293+
bytes32[] memory siblings
294+
) public pure {
279295
D.Label memory k = D.Label(keccak256(key), 256);
280296
D.Edge memory e;
281297
e.node = keccak256(value);
@@ -294,7 +310,14 @@ library PartialMerkleTree {
294310
require(rootHash == edgeHash(e));
295311
}
296312

297-
function verifyNonInclusionProof(bytes32 rootHash, bytes key, bytes32 potentialSiblingLabel, bytes32 potentialSiblingValue, uint branchMask, bytes32[] siblings) public pure {
313+
function verifyNonInclusionProof(
314+
bytes32 rootHash,
315+
bytes memory key,
316+
bytes32 potentialSiblingLabel,
317+
bytes32 potentialSiblingValue,
318+
uint branchMask,
319+
bytes32[] memory siblings
320+
) public pure {
298321
D.Label memory k = D.Label(keccak256(key), 256);
299322
D.Edge memory e;
300323
for (uint i = 0; branchMask != 0; i++) {
@@ -317,12 +340,12 @@ library PartialMerkleTree {
317340
require(rootHash == edgeHash(e));
318341
}
319342

320-
function newEdge(bytes32 node, D.Label label) internal pure returns (D.Edge memory e){
343+
function newEdge(bytes32 node, D.Label memory label) internal pure returns (D.Edge memory e){
321344
e.node = node;
322345
e.label = label;
323346
}
324347

325-
function _insertAtNode(Tree storage tree, bytes32 nodeHash, D.Label key, bytes32 value) private returns (bytes32) {
348+
function _insertAtNode(Tree storage tree, bytes32 nodeHash, D.Label memory key, bytes32 value) private returns (bytes32) {
326349
// require(key.length > 1);
327350
D.Node memory n = tree.nodes[nodeHash];
328351
uint head;
@@ -332,7 +355,7 @@ library PartialMerkleTree {
332355
return _replaceNode(tree, nodeHash, n);
333356
}
334357

335-
function _insertAtEdge(Tree storage tree, D.Edge e, D.Label key, bytes32 value) private returns (D.Edge) {
358+
function _insertAtEdge(Tree storage tree, D.Edge memory e, D.Label memory key, bytes32 value) private returns (D.Edge memory) {
336359
// require(e.hasNode());
337360
require(key.length >= e.label.length);
338361
D.Label memory prefix;
@@ -370,7 +393,7 @@ library PartialMerkleTree {
370393
return _insertNode(tree, n);
371394
}
372395

373-
function _findNode(Tree storage tree, bytes key) private view returns (bytes32) {
396+
function _findNode(Tree storage tree, bytes memory key) private view returns (bytes32) {
374397
return _findNodeWithHashedKey(tree, keccak256(key));
375398
}
376399

@@ -383,7 +406,7 @@ library PartialMerkleTree {
383406
}
384407
}
385408

386-
function _findAtNode(Tree storage tree, bytes32 nodeHash, D.Label key) private view returns (bytes32) {
409+
function _findAtNode(Tree storage tree, bytes32 nodeHash, D.Label memory key) private view returns (bytes32) {
387410
require(key.length > 1);
388411
D.Node memory n = tree.nodes[nodeHash];
389412
uint head;
@@ -392,7 +415,7 @@ library PartialMerkleTree {
392415
return _findAtEdge(tree, n.children[head], tail);
393416
}
394417

395-
function _findAtEdge(Tree storage tree, D.Edge e, D.Label key) private view returns (bytes32){
418+
function _findAtEdge(Tree storage tree, D.Edge memory e, D.Label memory key) private view returns (bytes32){
396419
require(key.length >= e.label.length);
397420
D.Label memory prefix;
398421
D.Label memory suffix;

Diff for: contracts/utils.sol

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
pragma solidity ^0.4.24;
1+
pragma solidity >=0.5.0 <0.6.0;
22

33
/**
44
MIT License
55
Original author: chriseth
6-
Refactored by Wanseob Lim
76
*/
87

98
import {D} from "./data.sol";
109

1110
library Utils {
1211
/// Returns a label containing the longest common prefix of `check` and `label`
1312
/// and a label consisting of the remaining part of `label`.
14-
function splitCommonPrefix(D.Label label, D.Label check) internal pure returns (D.Label prefix, D.Label labelSuffix) {
13+
function splitCommonPrefix(D.Label memory label, D.Label memory check) internal pure returns (D.Label memory prefix, D.Label memory labelSuffix) {
1514
return splitAt(label, commonPrefix(check, label));
1615
}
1716
/// Splits the label at the given position and returns prefix and suffix,
1817
/// i.e. prefix.length == pos and prefix.data . suffix.data == l.data.
19-
function splitAt(D.Label l, uint pos) internal pure returns (D.Label prefix, D.Label suffix) {
20-
require(pos <= l.length && pos <= 256);
18+
function splitAt(D.Label memory l, uint pos) internal pure returns (D.Label memory prefix, D.Label memory suffix) {
19+
require(pos <= l.length && pos <= 256, "Bad pos");
2120
prefix.length = pos;
2221
if (pos == 0) {
2322
prefix.data = bytes32(0);
@@ -28,7 +27,7 @@ library Utils {
2827
suffix.data = l.data << pos;
2928
}
3029
/// Returns the length of the longest common prefix of the two labels.
31-
function commonPrefix(D.Label a, D.Label b) internal pure returns (uint prefix) {
30+
function commonPrefix(D.Label memory a, D.Label memory b) internal pure returns (uint prefix) {
3231
uint length = a.length < b.length ? a.length : b.length;
3332
// TODO: This could actually use a "highestBitSet" helper
3433
uint diff = uint(a.data ^ b.data);
@@ -42,26 +41,27 @@ library Utils {
4241
}
4342
/// Returns the result of removing a prefix of length `prefix` bits from the
4443
/// given label (i.e. shifting its data to the left).
45-
function removePrefix(D.Label l, uint prefix) internal pure returns (D.Label r) {
46-
require(prefix <= l.length);
44+
function removePrefix(D.Label memory l, uint prefix) internal pure returns (D.Label memory r) {
45+
require(prefix <= l.length, "Bad lenght");
4746
r.length = l.length - prefix;
4847
r.data = l.data << prefix;
4948
}
5049
/// Removes the first bit from a label and returns the bit and a
5150
/// label containing the rest of the label (i.e. shifted to the left).
52-
function chopFirstBit(D.Label l) internal pure returns (uint firstBit, D.Label tail) {
53-
require(l.length > 0);
51+
function chopFirstBit(D.Label memory l) internal pure returns (uint firstBit, D.Label memory tail) {
52+
require(l.length > 0, "Empty element");
5453
return (uint(l.data >> 255), D.Label(l.data << 1, l.length - 1));
5554
}
5655
/// Returns the first bit set in the bitfield, where the 0th bit
5756
/// is the least significant.
5857
/// Throws if bitfield is zero.
5958
/// More efficient the smaller the result is.
6059
function lowestBitSet(uint bitfield) internal pure returns (uint bit) {
61-
require(bitfield != 0);
60+
require(bitfield != 0, "Bad bitfield");
6261
bytes32 bitfieldBytes = bytes32(bitfield);
6362
// First, find the lowest byte set
64-
for (uint byteSet = 0; byteSet < 32; byteSet++) {
63+
uint byteSet = 0;
64+
for (; byteSet < 32; byteSet++) {
6565
if (bitfieldBytes[31 - byteSet] != 0)
6666
break;
6767
}
@@ -82,5 +82,3 @@ library Utils {
8282
}
8383
}
8484

85-
86-

0 commit comments

Comments
 (0)