Skip to content

Commit f6383f4

Browse files
committed
Rewrote Chriseth's patricia tree implementation
0 parents  commit f6383f4

17 files changed

+6704
-0
lines changed

.gitignore

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# truffle
2+
build/*
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
coverage.json
23+
.coveralls.yml
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# Optional npm cache directory
48+
.npm
49+
50+
# Optional eslint cache
51+
.eslintcache
52+
53+
# Optional REPL history
54+
.node_repl_history
55+
56+
# Output of 'npm pack'
57+
*.tgz
58+
59+
# Yarn Integrity file
60+
.yarn-integrity
61+
62+
# dotenv environment variables file
63+
.env
64+
65+
# next.js build output
66+
.next
67+
68+
# IDE
69+
.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 commitground
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Solidity Patricia Tree
2+
3+
## Credits
4+
5+
This is a rewritten version of [Christian Reitwießner](https://github.com/chriseth)'s [patricia-tree](https://github.com/chriseth/patricia-tree) to use his patricia tree implementation as a solidity library through npm.
6+
7+
8+
##### latest released version
9+
[![npm](https://img.shields.io/npm/v/solidity-patricia-tree/latest.svg)](https://www.npmjs.com/package/solidity-patricia-tree)
10+
[![Build Status](https://travis-ci.org/commitground/solidity-patricia-tree.svg?branch=master)](https://travis-ci.org/commitground/solidity-patricia-tree)
11+
[![Coverage Status](https://coveralls.io/repos/github/commitground/solidity-patricia-tree/badge.svg?branch=master)](https://coveralls.io/github/commitground/solidity-patricia-tree?branch=develop)
12+
13+
##### in progress
14+
[![npm](https://img.shields.io/npm/v/solidity-patricia-tree/next.svg)](https://www.npmjs.com/package/solidity-patricia-tree)
15+
[![Build Status](https://travis-ci.org/commitground/solidity-patricia-tree.svg?branch=develop)](https://travis-ci.org/commitground/solidity-patricia-tree)
16+
[![Coverage Status](https://coveralls.io/repos/github/commitground/solidity-patricia-tree/badge.svg?branch=develop)](https://coveralls.io/github/commitground/solidity-patricia-tree?branch=develop)
17+
18+
[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)
19+
20+
21+
22+
## Usage
23+
24+
```bash
25+
npm i solidity-patricia-tree
26+
```
27+
28+
```solidity
29+
pragma solidity ^0.4.25;
30+
31+
import {PatriciaTree} from "solidity-patricia-tree/contracts/tree.sol";
32+
contract YourContract {
33+
using PatriciaTree for PatriciaTree.Tree;
34+
35+
function test() {
36+
//testInsert();
37+
testProofs();
38+
}
39+
function testInsert() internal {
40+
PatriciaTree.Tree memory tree;
41+
tree.insert("one", "ONE");
42+
tree.insert("two", "ONE");
43+
tree.insert("three", "ONE");
44+
tree.insert("four", "ONE");
45+
tree.insert("five", "ONE");
46+
tree.insert("six", "ONE");
47+
tree.insert("seven", "ONE");
48+
// update
49+
tree.insert("one", "TWO");
50+
}
51+
function testProofs() internal {
52+
PatriciaTree.Tree memory tree;
53+
tree.insert("one", "ONE");
54+
uint branchMask;
55+
bytes32[] siblings;
56+
(branchMask, siblings) = tree.getProof("one");
57+
tree.verifyProof(root, "one", "ONE", branchMask, siblings);
58+
tree.insert("two", "TWO");
59+
(branchMask, siblings) = tree.getProof("one");
60+
tree.verifyProof(root, "one", "ONE", branchMask, siblings);
61+
(branchMask, siblings) = tree.getProof("two");
62+
tree.verifyProof(root, "two", "TWO", branchMask, siblings);
63+
}
64+
}
65+
```
66+
67+
68+
## Development
69+
70+
### Pre-requisites
71+
72+
```bash
73+
npm install -g truffle
74+
npm install -g ganache
75+
npm install
76+
```
77+
78+
### Tests
79+
80+
Test cases include the information about how the functions work, but also includes a demo scenario.
81+
Running and reading the test cases will help you understand how it works.
82+
83+
```bash
84+
npm run test
85+
```
86+
87+
88+
## Contributors
89+
- Original author: [Christian Reitwießner](https://github.com/chriseth)
90+
- [Wanseob Lim](https://github.com/james-lim)<[[email protected]](mailto:[email protected])>
91+
92+
## License
93+
94+
[MIT LICENSE](./LICENSE)

contracts/Migrations.sol

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity ^0.4.23;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
constructor() public {
8+
owner = msg.sender;
9+
}
10+
11+
modifier restricted() {
12+
if (msg.sender == owner) _;
13+
}
14+
15+
function setCompleted(uint completed) public restricted {
16+
last_completed_migration = completed;
17+
}
18+
19+
function upgrade(address new_address) public restricted {
20+
Migrations upgraded = Migrations(new_address);
21+
upgraded.setCompleted(last_completed_migration);
22+
}
23+
}

contracts/data.sol

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pragma solidity ^0.4.0;
2+
3+
/**
4+
MIT License
5+
Copyright (c) 2017 chriseth
6+
*/
7+
8+
library D {
9+
struct Label {
10+
bytes32 data;
11+
uint length;
12+
}
13+
14+
struct Edge {
15+
bytes32 node;
16+
Label label;
17+
}
18+
19+
struct Node {
20+
Edge[2] children;
21+
}
22+
}

contracts/implementation.sol

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pragma solidity ^0.4.24;
2+
3+
import {PatriciaTree} from "./tree.sol";
4+
5+
contract PatriciaTreeImplementation {
6+
using PatriciaTree for PatriciaTree.Tree;
7+
PatriciaTree.Tree tree;
8+
9+
constructor () public {
10+
}
11+
12+
function get(bytes key) public view returns (bytes) {
13+
return tree.get(key);
14+
}
15+
16+
function getRootHash() public view returns (bytes32) {
17+
return tree.getRootHash();
18+
}
19+
20+
function getNode(bytes32 hash) public view returns (uint, bytes32, bytes32, uint, bytes32, bytes32) {
21+
return tree.getNode(hash);
22+
}
23+
24+
function getRootEdge() public view returns (uint, bytes32, bytes32) {
25+
return tree.getRootEdge();
26+
}
27+
28+
function getProof(bytes key) public view returns (uint branchMask, bytes32[] _siblings) {
29+
return tree.getProof(key);
30+
}
31+
32+
function verifyProof(bytes32 rootHash, bytes key, bytes value, uint branchMask, bytes32[] siblings) public pure {
33+
PatriciaTree.verifyProof(rootHash, key, value, branchMask, siblings);
34+
}
35+
36+
function insert(bytes key, bytes value) public {
37+
tree.insert(key, value);
38+
}
39+
}

0 commit comments

Comments
 (0)