1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ShaMapLeaf = exports.ShaMapNode = exports.ShaMap = void 0;
|
4 | const types_1 = require("./types");
|
5 | const hash_prefixes_1 = require("./hash-prefixes");
|
6 | const hashes_1 = require("./hashes");
|
7 |
|
8 |
|
9 |
|
10 | class ShaMapNode {
|
11 | }
|
12 | exports.ShaMapNode = ShaMapNode;
|
13 |
|
14 |
|
15 |
|
16 | class ShaMapLeaf extends ShaMapNode {
|
17 | constructor(index, item) {
|
18 | super();
|
19 | this.index = index;
|
20 | this.item = item;
|
21 | }
|
22 | |
23 |
|
24 |
|
25 | isLeaf() {
|
26 | return true;
|
27 | }
|
28 | |
29 |
|
30 |
|
31 | isInner() {
|
32 | return false;
|
33 | }
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 | hashPrefix() {
|
40 | return this.item === undefined ? new Uint8Array(0) : this.item.hashPrefix();
|
41 | }
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | hash() {
|
48 | const hash = hashes_1.Sha512Half.put(this.hashPrefix());
|
49 | this.toBytesSink(hash);
|
50 | return hash.finish();
|
51 | }
|
52 | |
53 |
|
54 |
|
55 |
|
56 | toBytesSink(list) {
|
57 | if (this.item !== undefined) {
|
58 | this.item.toBytesSink(list);
|
59 | }
|
60 | this.index.toBytesSink(list);
|
61 | }
|
62 | }
|
63 | exports.ShaMapLeaf = ShaMapLeaf;
|
64 |
|
65 |
|
66 |
|
67 | class ShaMapInner extends ShaMapNode {
|
68 | constructor(depth = 0) {
|
69 | super();
|
70 | this.depth = depth;
|
71 | this.slotBits = 0;
|
72 | this.branches = Array(16);
|
73 | }
|
74 | |
75 |
|
76 |
|
77 | isInner() {
|
78 | return true;
|
79 | }
|
80 | |
81 |
|
82 |
|
83 | isLeaf() {
|
84 | return false;
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | hashPrefix() {
|
92 | return hash_prefixes_1.HashPrefix.innerNode;
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | setBranch(slot, branch) {
|
101 | this.slotBits = this.slotBits | (1 << slot);
|
102 | this.branches[slot] = branch;
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | empty() {
|
108 | return this.slotBits === 0;
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | hash() {
|
116 | if (this.empty()) {
|
117 | return types_1.coreTypes.Hash256.ZERO_256;
|
118 | }
|
119 | const hash = hashes_1.Sha512Half.put(this.hashPrefix());
|
120 | this.toBytesSink(hash);
|
121 | return hash.finish();
|
122 | }
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 | toBytesSink(list) {
|
129 | for (let i = 0; i < this.branches.length; i++) {
|
130 | const branch = this.branches[i];
|
131 | const hash = branch
|
132 | ? branch.hash()
|
133 | : types_1.coreTypes.Hash256.ZERO_256;
|
134 | hash.toBytesSink(list);
|
135 | }
|
136 | }
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | addItem(index, item, leaf) {
|
145 | if (index === undefined) {
|
146 | throw new Error();
|
147 | }
|
148 | if (index !== undefined) {
|
149 | const nibble = index.nibblet(this.depth);
|
150 | const existing = this.branches[nibble];
|
151 | if (existing === undefined) {
|
152 | this.setBranch(nibble, leaf || new ShaMapLeaf(index, item));
|
153 | }
|
154 | else if (existing instanceof ShaMapLeaf) {
|
155 | const newInner = new ShaMapInner(this.depth + 1);
|
156 | newInner.addItem(existing.index, undefined, existing);
|
157 | newInner.addItem(index, item, leaf);
|
158 | this.setBranch(nibble, newInner);
|
159 | }
|
160 | else if (existing instanceof ShaMapInner) {
|
161 | existing.addItem(index, item, leaf);
|
162 | }
|
163 | else {
|
164 | throw new Error('invalid ShaMap.addItem call');
|
165 | }
|
166 | }
|
167 | }
|
168 | }
|
169 | class ShaMap extends ShaMapInner {
|
170 | }
|
171 | exports.ShaMap = ShaMap;
|
172 |
|
\ | No newline at end of file |