@@ -4,115 +4,63 @@ pragma solidity ^0.8.28;
44import "./CborDecode.sol " ;
55import "./CidCbor.sol " ;
66import "./TreeNodeCbor.sol " ;
7- import "./RecordCbor.sol " ;
87
98library TreeCbor {
109 using CBORDecoder for bytes ;
1110
12- uint8 private constant CID_V1 = 0x01 ;
13- uint8 private constant MULTICODEC_DAG_CBOR = 0x71 ;
14- uint8 private constant MULTIHASH_SHA_256 = 0x12 ;
15- uint8 private constant MULTIHASH_SIZE_32 = 0x20 ;
16-
1711 struct Tree {
18- CidCbor.CidBytes32 root;
1912 TreeNodeCbor.TreeNode[] nodes;
20- RecordCbor.Record[] records;
2113 CidCbor.CidBytes32[] cids;
22- bool [] nodeOrRecord;
23- }
24-
25- function itemIsNode (bytes memory cborData ) internal pure returns (bool ) {
26- (uint len , uint byteIdx ) = cborData.readFixedMap (0 );
27-
28- string memory itemName;
29- (itemName, byteIdx) = cborData.readString (byteIdx);
30- return len == 2 ;
3114 }
3215
33- function readTree (bytes [] memory cborData , CidCbor.CidBytes32 root ) internal pure returns (Tree memory ) {
16+ function _readTree (bytes [] memory cborData ) internal pure returns (Tree memory ) {
3417 TreeNodeCbor.TreeNode[] memory nodes = new TreeNodeCbor.TreeNode [](cborData.length );
35- RecordCbor.Record[] memory records = new RecordCbor.Record [](cborData.length );
3618 CidCbor.CidBytes32[] memory cids = new CidCbor.CidBytes32 [](cborData.length );
37- bool [] memory nodeOrRecord = new bool [](cborData.length );
3819
3920 for (uint i = 0 ; i < cborData.length ; i++ ) {
4021 cids[i] = CidCbor.CidBytes32.wrap (sha256 (cborData[i]));
4122 uint byteIdx;
42- if (itemIsNode (cborData[i])) {
43- (nodes[i], byteIdx) = TreeNodeCbor.readTreeNode (cborData[i], 0 );
44- nodeOrRecord[i] = true ;
45- } else {
46- (records[i], byteIdx) = RecordCbor.readRecord (cborData[i], 0 );
47- nodeOrRecord[i] = false ;
48- }
23+ (nodes[i], byteIdx) = TreeNodeCbor.readTreeNode (cborData[i], 0 );
4924 require (byteIdx == cborData[i].length , "expected to read all bytes " );
5025 }
5126
52- return requireUniqueCidsAndRoot (Tree (root, nodes, records, cids, nodeOrRecord));
27+ return Tree (nodes, cids);
28+ }
29+
30+ function readTree (bytes [] memory cborData ) internal pure returns (Tree memory ) {
31+ return requireUniqueCids (_readTree (cborData));
5332 }
5433
55- function nodeByCid (Tree memory tree , CidCbor.CidBytes32 indexCid )
34+ function getCid (Tree memory tree , CidCbor.CidBytes32 indexCid )
5635 internal
5736 pure
5837 returns (TreeNodeCbor.TreeNode memory , uint index )
5938 {
60- return nodeByCid (tree, indexCid, 0 );
39+ return getCid (tree, indexCid, 0 );
6140 }
6241
63- function nodeByCid (Tree memory tree , CidCbor.CidBytes32 indexCid , uint startIdx )
42+ function getCid (Tree memory tree , CidCbor.CidBytes32 indexCid , uint startIdx )
6443 internal
6544 pure
6645 returns (TreeNodeCbor.TreeNode memory , uint index )
6746 {
6847 bytes32 indexBytes = CidCbor.CidBytes32.unwrap (indexCid);
6948 for (uint i = startIdx; i < tree.cids.length ; i++ ) {
7049 if (CidCbor.CidBytes32.unwrap (tree.cids[i]) == indexBytes) {
71- require (tree.nodeOrRecord[i], "expected node " );
7250 return (tree.nodes[i], i);
7351 }
7452 }
7553 revert ("node not found " );
7654 }
7755
78- function recordByCid (Tree memory tree , CidCbor.CidBytes32 indexCid )
79- internal
80- pure
81- returns (RecordCbor.Record memory , uint index )
82- {
83- return recordByCid (tree, indexCid, 0 );
84- }
85-
86- function recordByCid (Tree memory tree , CidCbor.CidBytes32 indexCid , uint startIdx )
87- internal
88- pure
89- returns (RecordCbor.Record memory , uint index )
90- {
91- bytes32 indexBytes = CidCbor.CidBytes32.unwrap (indexCid);
92-
93- for (uint i = startIdx; i < tree.cids.length ; i++ ) {
94- if (CidCbor.CidBytes32.unwrap (tree.cids[i]) == indexBytes) {
95- require (! tree.nodeOrRecord[i], "expected record " );
96- return (tree.records[i], i);
97- }
98- }
99- revert ("record not found " );
100- }
101-
102- function requireUniqueCidsAndRoot (Tree memory tree ) internal pure returns (Tree memory ) {
103- bool rootIncluded;
104- bytes32 treeRoot = CidCbor.CidBytes32.unwrap (tree.root);
56+ function requireUniqueCids (Tree memory tree ) internal pure returns (Tree memory ) {
10557 for (uint i = 0 ; i < tree.cids.length ; i++ ) {
10658 bytes32 thisCid = CidCbor.CidBytes32.unwrap (tree.cids[i]);
107- if (! rootIncluded) {
108- rootIncluded = thisCid == treeRoot;
109- }
11059 for (uint j = i + 1 ; j < tree.nodes.length ; j++ ) {
11160 bytes32 otherCid = CidCbor.CidBytes32.unwrap (tree.cids[j]);
11261 require (thisCid != otherCid, "node cids must be unique " );
11362 }
11463 }
115- require (rootIncluded, "root cid must be included " );
11664 return tree;
11765 }
11866}
0 commit comments