11pragma solidity ^ 0.4.24 ;
22
3- import {D} from "./data.sol " ;
4- import {PatriciaTree} from "./tree.sol " ;
3+ import {SparseTree} from "./tree.sol " ;
54
6- contract PatriciaTreeImplementation {
7- using PatriciaTree for PatriciaTree .Tree;
8- PatriciaTree .Tree tree;
5+ contract SparseTreeImplementation {
6+ using SparseTree for SparseTree .Tree;
7+ SparseTree .Tree tree;
98
109 constructor () public {
1110 }
1211
13- function get (bytes key ) public view returns (bytes ) {
14- return tree.get (key);
15- }
16-
17- function getValue (bytes32 hash ) public view returns (bytes ) {
18- return tree.values[hash];
19- }
20-
21- function getRootHash () public view returns (bytes32 ) {
22- return tree.getRootHash ();
23- }
24-
25- function getNode (bytes32 hash ) public view returns (uint , bytes32 , bytes32 , uint , bytes32 , bytes32 ) {
26- return tree.getNode (hash);
27- }
28-
29- function getRootEdge () public view returns (uint , bytes32 , bytes32 ) {
30- return tree.getRootEdge ();
31- }
32-
33- function getProof (bytes key ) public view returns (uint branchMask , bytes32 [] _siblings ) {
34- return tree.getProof (key);
35- }
36-
37- function verifyProof (bytes32 rootHash , bytes key , bytes value , uint branchMask , bytes32 [] siblings ) public pure {
38- PatriciaTree.verifyProof (rootHash, key, value, branchMask, siblings);
39- }
40-
41- function insert (bytes key , bytes value ) public {
42- tree.insert (key, value);
43- }
44- }
45-
46- contract PatriciaTreeMerkleProof {
47- using PatriciaTree for PatriciaTree.Tree;
48- PatriciaTree.Tree tree;
49-
50- enum Status {OPENED, ONGOING, SUCCESS, FAILURE}
51-
52- event OnChangeStatus (Status s );
53-
54- modifier onlyFor (Status _status ) {
55- require (status == _status);
56- _;
12+ function initialize (bytes32 initialRoot ) public {
13+ tree.initialize (initialRoot);
5714 }
58-
59- mapping (bytes32 => bool ) committedValues;
60-
61- Status public status;
62- D.Edge originalRootEdge;
63- bytes32 originalRoot;
64- D.Edge targetRootEdge;
65- bytes32 targetRoot;
66-
67- constructor () public {
68- // Init status
69- status = Status.OPENED;
70- }
71-
72- function commitOriginalEdge (
73- uint _originalLabelLength ,
74- bytes32 _originalLabel ,
75- bytes32 _originalValue
76- ) public onlyFor (Status.OPENED) {
77- // Init original root edge
78- originalRootEdge.label = D.Label (_originalLabel, _originalLabelLength);
79- originalRootEdge.node = _originalValue;
80- originalRoot = PatriciaTree.edgeHash (originalRootEdge);
81- }
82-
83- function commitTargetEdge (
84- uint _targetLabelLength ,
85- bytes32 _targetLabel ,
86- bytes32 _targetValue
87- ) public onlyFor (Status.OPENED) {
88- // Init target root edge
89- targetRootEdge.label = D.Label (_targetLabel, _targetLabelLength);
90- targetRootEdge.node = _targetValue;
91- targetRoot = PatriciaTree.edgeHash (targetRootEdge);
92- }
93-
9415 function insert (bytes key , bytes value ) public {
95- bytes32 k = keccak256 (value);
96- committedValues[k] = true ;
9716 tree.insert (key, value);
9817 }
9918
100- function commitNode (
101- bytes32 nodeHash ,
102- uint firstEdgeLabelLength ,
103- bytes32 firstEdgeLabel ,
104- bytes32 firstEdgeValue ,
105- uint secondEdgeLabelLength ,
106- bytes32 secondEdgeLabel ,
107- bytes32 secondEdgeValue
108- ) public onlyFor (Status.OPENED) {
109- D.Label memory k0 = D.Label (firstEdgeLabel, firstEdgeLabelLength);
110- D.Edge memory e0 = D.Edge (firstEdgeValue, k0);
111- D.Label memory k1 = D.Label (secondEdgeLabel, secondEdgeLabelLength);
112- D.Edge memory e1 = D.Edge (secondEdgeValue, k1);
113- require (tree.nodes[nodeHash].children[0 ].node == 0 );
114- require (tree.nodes[nodeHash].children[1 ].node == 0 );
115- require (nodeHash == keccak256 (
116- abi.encodePacked (PatriciaTree.edgeHash (e0), PatriciaTree.edgeHash (e1)))
117- );
118- tree.nodes[nodeHash].children[0 ] = e0;
119- tree.nodes[nodeHash].children[1 ] = e1;
19+ function commitBranch (bytes key , bytes value , uint branchMask , bytes32 [] siblings ) public {
20+ return tree.commitBranch (key, value, branchMask, siblings);
12021 }
12122
122- function commitValue (bytes value ) public onlyFor (Status.OPENED) {
123- bytes32 k = keccak256 (value);
124- committedValues[k] = true ;
125- tree.values[k] = value;
126- }
127-
128- function seal () public onlyFor (Status.OPENED) {
129- // require(_verifyEdge(originalRootEdge));
130- tree.rootEdge = originalRootEdge;
131- tree.root = PatriciaTree.edgeHash (tree.rootEdge);
132- _changeStatus (Status.ONGOING);
23+ function get (bytes key ) public view returns (bytes ) {
24+ return tree.get (key);
13325 }
13426
135- function proof () public onlyFor (Status.ONGOING) {
136- require (targetRootEdge.node == tree.rootEdge.node);
137- require (targetRootEdge.label.length == tree.rootEdge.label.length );
138- require (targetRootEdge.label.data == tree.rootEdge.label.data);
139- require (_verifyEdge (tree.rootEdge));
140- _changeStatus (Status.SUCCESS);
27+ function getValue (bytes32 hash ) public view returns (bytes ) {
28+ return tree.values[hash];
14129 }
14230
14331 function getRootHash () public view returns (bytes32 ) {
14432 return tree.getRootHash ();
14533 }
14634
147- function _verifyEdge (D.Edge memory _edge ) internal view returns (bool ) {
148- if (_edge.node == 0 ) {
149- // Empty. Return true because there is nothing to verify
150- return true ;
151- } else if (_isLeaf (_edge)) {
152- // check stored value of the leaf node
153- require (_hasValue (_edge.node));
154- } else {
155- D.Edge[2 ] memory children = tree.nodes[_edge.node].children;
156- // its node value should be the hashed value of its child nodes
157- require (_edge.node == keccak256 (
158- abi.encodePacked (PatriciaTree.edgeHash (children[0 ]), PatriciaTree.edgeHash (children[1 ]))
159- ));
160- // check children recursively
161- require (_verifyEdge (children[0 ]));
162- require (_verifyEdge (children[1 ]));
163- }
164- return true ;
165- }
166-
167- function _isLeaf (D.Edge _edge ) internal view returns (bool ) {
168- return (tree.nodes[_edge.node].children[0 ].node == 0 && tree.nodes[_edge.node].children[1 ].node == 0 );
169- }
170-
171- function _hasValue (bytes32 valHash ) internal view returns (bool ) {
172- return committedValues[valHash];
35+ function getProof (bytes key ) public view returns (uint branchMask , bytes32 [] _siblings ) {
36+ return tree.getProof (key);
17337 }
17438
175- function _changeStatus (Status _status ) internal {
176- require (status < _status);
177- // unidirectional
178- status = _status;
179- emit OnChangeStatus (status);
39+ function verifyProof (bytes32 rootHash , bytes key , bytes value , uint branchMask , bytes32 [] siblings ) public pure {
40+ SparseTree.verifyProof (rootHash, key, value, branchMask, siblings);
18041 }
18142}
0 commit comments