From 4f7c2f69043be0fefb722e11dadcebbbc12b0854 Mon Sep 17 00:00:00 2001 From: James Lim Date: Thu, 8 Nov 2018 14:22:02 +0900 Subject: [PATCH 1/3] Add solcover configuration --- .solcover.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .solcover.js diff --git a/.solcover.js b/.solcover.js new file mode 100644 index 0000000..44aa295 --- /dev/null +++ b/.solcover.js @@ -0,0 +1,9 @@ +module.exports = { + norpc: true, + testCommand: 'node --max-old-space-size=4096 ../node_modules/.bin/truffle test --network coverage', + compileCommand: 'node --max-old-space-size=4096 ../node_modules/.bin/truffle compile --network coverage', + skipFiles: [ + 'contracts/Migrations.sol', + 'contracts/implementation.sol' + ] +} From 67786b2dd60f76b49bba8ef9b3c8f8d0800b6533 Mon Sep 17 00:00:00 2001 From: James Lim Date: Thu, 8 Nov 2018 14:23:04 +0900 Subject: [PATCH 2/3] Add a new function getProofWithHashedKey() --- contracts/tree.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contracts/tree.sol b/contracts/tree.sol index dc7de9b..c5c32dd 100644 --- a/contracts/tree.sol +++ b/contracts/tree.sol @@ -61,7 +61,11 @@ library PatriciaTree { // where we have branch nodes (bit in key denotes direction) // - bytes32[] hashes - hashes of sibling edges function getProof(Tree storage tree, bytes key) internal view returns (uint branchMask, bytes32[] _siblings) { - D.Label memory k = D.Label(keccak256(key), 256); + return getProofWithHashedKey(tree, keccak256(key)); + } + + function getProofWithHashedKey(Tree storage tree, bytes32 hashedKey) internal view returns (uint branchMask, bytes32[] _siblings) { + D.Label memory k = D.Label(hashedKey, 256); D.Edge memory e = tree.rootEdge; bytes32[256] memory siblings; uint length; From d05cead5eeb0f3d84baf8359ebd0c43abe26db9e Mon Sep 17 00:00:00 2001 From: James Lim Date: Thu, 8 Nov 2018 14:48:33 +0900 Subject: [PATCH 3/3] Divide test file --- contracts/utils.sol | 115 -------------------------------------------- package-lock.json | 2 +- package.json | 2 +- test/TestUtils.sol | 112 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 117 deletions(-) create mode 100644 test/TestUtils.sol diff --git a/contracts/utils.sol b/contracts/utils.sol index 5de9464..38049c0 100644 --- a/contracts/utils.sol +++ b/contracts/utils.sol @@ -83,119 +83,4 @@ library Utils { } -contract UtilsTest { - function test() public pure { - testLowestBitSet(); - testChopFirstBit(); - testRemovePrefix(); - testCommonPrefix(); - testSplitAt(); - testSplitCommonPrefix(); - } - - function testLowestBitSet() internal pure { - require(Utils.lowestBitSet(0x123) == 0); - require(Utils.lowestBitSet(0x124) == 2); - require(Utils.lowestBitSet(0x11 << 30) == 30); - require(Utils.lowestBitSet(1 << 255) == 255); - } - - function testChopFirstBit() internal pure { - D.Label memory l; - l.data = hex"ef1230"; - l.length = 20; - uint bit1; - uint bit2; - uint bit3; - uint bit4; - (bit1, l) = Utils.chopFirstBit(l); - (bit2, l) = Utils.chopFirstBit(l); - (bit3, l) = Utils.chopFirstBit(l); - (bit4, l) = Utils.chopFirstBit(l); - require(bit1 == 1); - require(bit2 == 1); - require(bit3 == 1); - require(bit4 == 0); - require(l.length == 16); - require(l.data == hex"F123"); - - l.data = hex"80"; - l.length = 1; - (bit1, l) = Utils.chopFirstBit(l); - require(bit1 == 1); - require(l.length == 0); - require(l.data == 0); - } - - function testRemovePrefix() internal pure { - D.Label memory l; - l.data = hex"ef1230"; - l.length = 20; - l = Utils.removePrefix(l, 4); - require(l.length == 16); - require(l.data == hex"f123"); - l = Utils.removePrefix(l, 15); - require(l.length == 1); - require(l.data == hex"80"); - l = Utils.removePrefix(l, 1); - require(l.length == 0); - require(l.data == 0); - } - - function testCommonPrefix() internal pure { - D.Label memory a; - D.Label memory b; - a.data = hex"abcd"; - a.length = 16; - b.data = hex"a000"; - b.length = 16; - require(Utils.commonPrefix(a, b) == 4); - b.length = 0; - require(Utils.commonPrefix(a, b) == 0); - - b.data = hex"bbcd"; - b.length = 16; - require(Utils.commonPrefix(a, b) == 3); - require(Utils.commonPrefix(b, b) == b.length); - } - - function testSplitAt() internal pure { - D.Label memory a; - a.data = hex"abcd"; - a.length = 16; - D.Label memory x; - D.Label memory y; - (x, y) = Utils.splitAt(a, 0); - require(x.length == 0); - require(y.length == a.length); - require(y.data == a.data); - - (x, y) = Utils.splitAt(a, 4); - require(x.length == 4); - require(x.data == hex"a0"); - require(y.length == 12); - require(y.data == hex"bcd0"); - - (x, y) = Utils.splitAt(a, 16); - require(y.length == 0); - require(x.length == a.length); - require(x.data == a.data); - } - - function testSplitCommonPrefix() internal pure { - D.Label memory a; - D.Label memory b; - a.data = hex"abcd"; - a.length = 16; - b.data = hex"a0f570"; - b.length = 20; - D.Label memory prefix; - D.Label memory suffix; - (prefix, suffix) = Utils.splitCommonPrefix(b, a); - require(prefix.length == 4); - require(prefix.data == hex"a0"); - require(suffix.length == 16); - require(suffix.data == hex"0f57"); - } -} diff --git a/package-lock.json b/package-lock.json index b7fca99..7c95b1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "solidity-patricia-tree", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5d22b82..39fb57b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "solidity-patricia-tree", - "version": "1.0.1", + "version": "1.0.2", "description": "Patricia Tree solidity implemenation", "directories": { "test": "test" diff --git a/test/TestUtils.sol b/test/TestUtils.sol new file mode 100644 index 0000000..859effb --- /dev/null +++ b/test/TestUtils.sol @@ -0,0 +1,112 @@ +pragma solidity ^0.4.0; + +import {D} from "../contracts/data.sol"; +import {Utils} from "../contracts/utils.sol"; + +contract TestUtils { + function testLowestBitSet() internal pure { + require(Utils.lowestBitSet(0x123) == 0); + require(Utils.lowestBitSet(0x124) == 2); + require(Utils.lowestBitSet(0x11 << 30) == 30); + require(Utils.lowestBitSet(1 << 255) == 255); + } + + function testChopFirstBit() internal pure { + D.Label memory l; + l.data = hex"ef1230"; + l.length = 20; + uint bit1; + uint bit2; + uint bit3; + uint bit4; + (bit1, l) = Utils.chopFirstBit(l); + (bit2, l) = Utils.chopFirstBit(l); + (bit3, l) = Utils.chopFirstBit(l); + (bit4, l) = Utils.chopFirstBit(l); + require(bit1 == 1); + require(bit2 == 1); + require(bit3 == 1); + require(bit4 == 0); + require(l.length == 16); + require(l.data == hex"F123"); + + l.data = hex"80"; + l.length = 1; + (bit1, l) = Utils.chopFirstBit(l); + require(bit1 == 1); + require(l.length == 0); + require(l.data == 0); + } + + function testRemovePrefix() internal pure { + D.Label memory l; + l.data = hex"ef1230"; + l.length = 20; + l = Utils.removePrefix(l, 4); + require(l.length == 16); + require(l.data == hex"f123"); + l = Utils.removePrefix(l, 15); + require(l.length == 1); + require(l.data == hex"80"); + l = Utils.removePrefix(l, 1); + require(l.length == 0); + require(l.data == 0); + } + + function testCommonPrefix() internal pure { + D.Label memory a; + D.Label memory b; + a.data = hex"abcd"; + a.length = 16; + b.data = hex"a000"; + b.length = 16; + require(Utils.commonPrefix(a, b) == 4); + + b.length = 0; + require(Utils.commonPrefix(a, b) == 0); + + b.data = hex"bbcd"; + b.length = 16; + require(Utils.commonPrefix(a, b) == 3); + require(Utils.commonPrefix(b, b) == b.length); + } + + function testSplitAt() internal pure { + D.Label memory a; + a.data = hex"abcd"; + a.length = 16; + D.Label memory x; + D.Label memory y; + (x, y) = Utils.splitAt(a, 0); + require(x.length == 0); + require(y.length == a.length); + require(y.data == a.data); + + (x, y) = Utils.splitAt(a, 4); + require(x.length == 4); + require(x.data == hex"a0"); + require(y.length == 12); + require(y.data == hex"bcd0"); + + (x, y) = Utils.splitAt(a, 16); + require(y.length == 0); + require(x.length == a.length); + require(x.data == a.data); + } + + function testSplitCommonPrefix() internal pure { + D.Label memory a; + D.Label memory b; + a.data = hex"abcd"; + a.length = 16; + b.data = hex"a0f570"; + b.length = 20; + D.Label memory prefix; + D.Label memory suffix; + (prefix, suffix) = Utils.splitCommonPrefix(b, a); + require(prefix.length == 4); + require(prefix.data == hex"a0"); + require(suffix.length == 16); + require(suffix.data == hex"0f57"); + } +}