Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -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'
]
}
6 changes: 5 additions & 1 deletion contracts/tree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
115 changes: 0 additions & 115 deletions contracts/utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solidity-patricia-tree",
"version": "1.0.1",
"version": "1.0.2",
"description": "Patricia Tree solidity implemenation",
"directories": {
"test": "test"
Expand Down
112 changes: 112 additions & 0 deletions test/TestUtils.sol
Original file line number Diff line number Diff line change
@@ -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");
}
}