zkjson 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/circomlibjs/SMT.js +481 -0
- package/cjs/circomlibjs/SMTMemDb.js +173 -0
- package/cjs/circomlibjs/buildPoseidon.js +162 -0
- package/cjs/circomlibjs/buildPoseidonWasm.js +124 -0
- package/cjs/circomlibjs/getHashes.js +220 -0
- package/cjs/circomlibjs/poseidonConstants$1.js +1 -0
- package/cjs/circomlibjs.js +6 -6
- package/cjs/collection.js +2 -2
- package/cjs/collection_tree.js +148 -0
- package/cjs/db.js +2 -2
- package/cjs/db_tree.js +398 -0
- package/cjs/index.js +16 -0
- package/cjs/newMemEmptyTrie.js +42 -0
- package/cjs/nft.js +3 -4
- package/esm/circomlibjs/SMT.js +349 -0
- package/esm/circomlibjs/SMTMemDb.js +69 -0
- package/esm/circomlibjs/buildPoseidon.js +113 -0
- package/esm/circomlibjs/buildPoseidonWasm.js +445 -0
- package/esm/circomlibjs/getHashes.js +515 -0
- package/esm/circomlibjs/poseidonConstants$1.js +24806 -0
- package/esm/circomlibjs.js +70 -25385
- package/esm/collection.js +1 -1
- package/esm/collection_tree.js +43 -0
- package/esm/db.js +1 -1
- package/esm/db_tree.js +122 -0
- package/esm/index.js +3 -1
- package/esm/newMemEmptyTrie.js +11 -0
- package/esm/nft.js +5 -3
- package/package.json +1 -1
@@ -0,0 +1,481 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports["default"] = void 0;
|
7
|
+
var ffjavascript = _interopRequireWildcard(require("ffjavascript"));
|
8
|
+
var _buildPoseidon = _interopRequireDefault(require("./buildPoseidon.js"));
|
9
|
+
var _ramda = require("ramda");
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
11
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
12
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
13
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
14
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
15
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
16
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
17
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
18
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
19
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
20
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
21
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
22
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
23
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
24
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
25
|
+
var SMT = exports["default"] = /*#__PURE__*/function () {
|
26
|
+
function SMT(db, root, hash0, hash1, F) {
|
27
|
+
_classCallCheck(this, SMT);
|
28
|
+
this.db = db;
|
29
|
+
this.root = root;
|
30
|
+
this.hash0 = hash0;
|
31
|
+
this.hash1 = hash1;
|
32
|
+
this.F = F;
|
33
|
+
}
|
34
|
+
return _createClass(SMT, [{
|
35
|
+
key: "_splitBits",
|
36
|
+
value: function _splitBits(_key) {
|
37
|
+
var F = this.F;
|
38
|
+
var res = ffjavascript.Scalar.bits(F.toObject(_key));
|
39
|
+
while (res.length < 256) res.push(false);
|
40
|
+
return res;
|
41
|
+
}
|
42
|
+
}, {
|
43
|
+
key: "update",
|
44
|
+
value: function () {
|
45
|
+
var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_key, _newValue) {
|
46
|
+
var _this$poseidon, _this$zero;
|
47
|
+
var _hash_value, _iterator, _step, v, value, newValue, F, key, resFind, res, ins, dels, rtOld, rtNew, keyBits, level, oldNode, newNode, sibling;
|
48
|
+
return _regenerator().w(function (_context) {
|
49
|
+
while (1) switch (_context.n) {
|
50
|
+
case 0:
|
51
|
+
if (!((_this$poseidon = this.poseidon) !== null && _this$poseidon !== void 0)) {
|
52
|
+
_context.n = 1;
|
53
|
+
break;
|
54
|
+
}
|
55
|
+
_this$poseidon;
|
56
|
+
_context.n = 3;
|
57
|
+
break;
|
58
|
+
case 1:
|
59
|
+
_context.n = 2;
|
60
|
+
return (0, _buildPoseidon["default"])();
|
61
|
+
case 2:
|
62
|
+
this.poseidon = _context.v;
|
63
|
+
case 3:
|
64
|
+
(_this$zero = this.zero) !== null && _this$zero !== void 0 ? _this$zero : this.zero = this.poseidon(["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]);
|
65
|
+
_hash_value = _newValue;
|
66
|
+
if (_newValue.length === 256) {
|
67
|
+
_hash_value = [];
|
68
|
+
_iterator = _createForOfIteratorHelper((0, _ramda.splitEvery)(16, _newValue));
|
69
|
+
try {
|
70
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
71
|
+
v = _step.value;
|
72
|
+
value = v.join("") === "0000000000000000" ? this.zero : this.poseidon(v);
|
73
|
+
_hash_value.push(value);
|
74
|
+
}
|
75
|
+
} catch (err) {
|
76
|
+
_iterator.e(err);
|
77
|
+
} finally {
|
78
|
+
_iterator.f();
|
79
|
+
}
|
80
|
+
}
|
81
|
+
newValue = this.poseidon(_hash_value);
|
82
|
+
F = this.F;
|
83
|
+
key = F.e(_key);
|
84
|
+
_context.n = 4;
|
85
|
+
return this.find(key);
|
86
|
+
case 4:
|
87
|
+
resFind = _context.v;
|
88
|
+
res = {};
|
89
|
+
res.oldRoot = this.root;
|
90
|
+
res.oldKey = key;
|
91
|
+
res.oldValue = resFind.foundValue;
|
92
|
+
res.newKey = key;
|
93
|
+
res.newValue = newValue;
|
94
|
+
res.siblings = resFind.siblings;
|
95
|
+
ins = [];
|
96
|
+
dels = [];
|
97
|
+
rtOld = this.hash1(key, resFind.foundValue);
|
98
|
+
rtNew = this.hash1(key, newValue);
|
99
|
+
ins.push([rtNew, [1, key, newValue]]);
|
100
|
+
dels.push(rtOld);
|
101
|
+
keyBits = this._splitBits(key);
|
102
|
+
for (level = resFind.siblings.length - 1; level >= 0; level--) {
|
103
|
+
oldNode = void 0, newNode = void 0;
|
104
|
+
sibling = resFind.siblings[level];
|
105
|
+
if (keyBits[level]) {
|
106
|
+
oldNode = [sibling, rtOld];
|
107
|
+
newNode = [sibling, rtNew];
|
108
|
+
} else {
|
109
|
+
oldNode = [rtOld, sibling];
|
110
|
+
newNode = [rtNew, sibling];
|
111
|
+
}
|
112
|
+
rtOld = this.hash0(oldNode[0], oldNode[1]);
|
113
|
+
rtNew = this.hash0(newNode[0], newNode[1]);
|
114
|
+
dels.push(rtOld);
|
115
|
+
ins.push([rtNew, newNode]);
|
116
|
+
}
|
117
|
+
res.newRoot = rtNew;
|
118
|
+
_context.n = 5;
|
119
|
+
return this.db.multiDel(dels);
|
120
|
+
case 5:
|
121
|
+
_context.n = 6;
|
122
|
+
return this.db.multiIns(ins);
|
123
|
+
case 6:
|
124
|
+
_context.n = 7;
|
125
|
+
return this.db.setRoot(rtNew);
|
126
|
+
case 7:
|
127
|
+
this.root = rtNew;
|
128
|
+
return _context.a(2, res);
|
129
|
+
}
|
130
|
+
}, _callee, this);
|
131
|
+
}));
|
132
|
+
function update(_x, _x2) {
|
133
|
+
return _update.apply(this, arguments);
|
134
|
+
}
|
135
|
+
return update;
|
136
|
+
}()
|
137
|
+
}, {
|
138
|
+
key: "delete",
|
139
|
+
value: function () {
|
140
|
+
var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_key) {
|
141
|
+
var F, key, resFind, res, dels, ins, rtOld, rtNew, mixed, record, keyBits, level, newSibling, oldSibling, newNode;
|
142
|
+
return _regenerator().w(function (_context2) {
|
143
|
+
while (1) switch (_context2.n) {
|
144
|
+
case 0:
|
145
|
+
F = this.F;
|
146
|
+
key = F.e(_key);
|
147
|
+
_context2.n = 1;
|
148
|
+
return this.find(key);
|
149
|
+
case 1:
|
150
|
+
resFind = _context2.v;
|
151
|
+
if (resFind.found) {
|
152
|
+
_context2.n = 2;
|
153
|
+
break;
|
154
|
+
}
|
155
|
+
throw new Error("Key does not exists");
|
156
|
+
case 2:
|
157
|
+
res = {
|
158
|
+
siblings: [],
|
159
|
+
delKey: key,
|
160
|
+
delValue: resFind.foundValue
|
161
|
+
};
|
162
|
+
dels = [];
|
163
|
+
ins = [];
|
164
|
+
rtOld = this.hash1(key, resFind.foundValue);
|
165
|
+
dels.push(rtOld);
|
166
|
+
if (!(resFind.siblings.length > 0)) {
|
167
|
+
_context2.n = 7;
|
168
|
+
break;
|
169
|
+
}
|
170
|
+
_context2.n = 3;
|
171
|
+
return this.db.get(resFind.siblings[resFind.siblings.length - 1]);
|
172
|
+
case 3:
|
173
|
+
record = _context2.v;
|
174
|
+
if (!(record.length == 3 && F.eq(record[0], F.one))) {
|
175
|
+
_context2.n = 4;
|
176
|
+
break;
|
177
|
+
}
|
178
|
+
mixed = false;
|
179
|
+
res.oldKey = record[1];
|
180
|
+
res.oldValue = record[2];
|
181
|
+
res.isOld0 = false;
|
182
|
+
rtNew = resFind.siblings[resFind.siblings.length - 1];
|
183
|
+
_context2.n = 6;
|
184
|
+
break;
|
185
|
+
case 4:
|
186
|
+
if (!(record.length == 2)) {
|
187
|
+
_context2.n = 5;
|
188
|
+
break;
|
189
|
+
}
|
190
|
+
mixed = true;
|
191
|
+
res.oldKey = key;
|
192
|
+
res.oldValue = F.zero;
|
193
|
+
res.isOld0 = true;
|
194
|
+
rtNew = F.zero;
|
195
|
+
_context2.n = 6;
|
196
|
+
break;
|
197
|
+
case 5:
|
198
|
+
throw new Error("Invalid node. Database corrupted");
|
199
|
+
case 6:
|
200
|
+
_context2.n = 8;
|
201
|
+
break;
|
202
|
+
case 7:
|
203
|
+
rtNew = F.zero;
|
204
|
+
res.oldKey = key;
|
205
|
+
res.oldValue = F.zero;
|
206
|
+
res.isOld0 = true;
|
207
|
+
case 8:
|
208
|
+
keyBits = this._splitBits(key);
|
209
|
+
for (level = resFind.siblings.length - 1; level >= 0; level--) {
|
210
|
+
newSibling = resFind.siblings[level];
|
211
|
+
if (level == resFind.siblings.length - 1 && !res.isOld0) {
|
212
|
+
newSibling = F.zero;
|
213
|
+
}
|
214
|
+
oldSibling = resFind.siblings[level];
|
215
|
+
if (keyBits[level]) {
|
216
|
+
rtOld = this.hash0(oldSibling, rtOld);
|
217
|
+
} else {
|
218
|
+
rtOld = this.hash0(rtOld, oldSibling);
|
219
|
+
}
|
220
|
+
dels.push(rtOld);
|
221
|
+
if (!F.isZero(newSibling)) {
|
222
|
+
mixed = true;
|
223
|
+
}
|
224
|
+
if (mixed) {
|
225
|
+
res.siblings.unshift(resFind.siblings[level]);
|
226
|
+
newNode = void 0;
|
227
|
+
if (keyBits[level]) {
|
228
|
+
newNode = [newSibling, rtNew];
|
229
|
+
} else {
|
230
|
+
newNode = [rtNew, newSibling];
|
231
|
+
}
|
232
|
+
rtNew = this.hash0(newNode[0], newNode[1]);
|
233
|
+
ins.push([rtNew, newNode]);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
_context2.n = 9;
|
237
|
+
return this.db.multiIns(ins);
|
238
|
+
case 9:
|
239
|
+
_context2.n = 10;
|
240
|
+
return this.db.setRoot(rtNew);
|
241
|
+
case 10:
|
242
|
+
this.root = rtNew;
|
243
|
+
_context2.n = 11;
|
244
|
+
return this.db.multiDel(dels);
|
245
|
+
case 11:
|
246
|
+
res.newRoot = rtNew;
|
247
|
+
res.oldRoot = rtOld;
|
248
|
+
return _context2.a(2, res);
|
249
|
+
}
|
250
|
+
}, _callee2, this);
|
251
|
+
}));
|
252
|
+
function _delete(_x3) {
|
253
|
+
return _delete2.apply(this, arguments);
|
254
|
+
}
|
255
|
+
return _delete;
|
256
|
+
}()
|
257
|
+
}, {
|
258
|
+
key: "insert",
|
259
|
+
value: function () {
|
260
|
+
var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_key, _value) {
|
261
|
+
var _this$poseidon2, _this$zero2;
|
262
|
+
var _hash_value, _iterator2, _step2, v, _value2, value, F, key, addedOne, res, newKeyBits, rtOld, resFind, mixed, oldKeyits, i, inserts, dels, rt, _i, oldSibling, newRt;
|
263
|
+
return _regenerator().w(function (_context3) {
|
264
|
+
while (1) switch (_context3.n) {
|
265
|
+
case 0:
|
266
|
+
if (!((_this$poseidon2 = this.poseidon) !== null && _this$poseidon2 !== void 0)) {
|
267
|
+
_context3.n = 1;
|
268
|
+
break;
|
269
|
+
}
|
270
|
+
_this$poseidon2;
|
271
|
+
_context3.n = 3;
|
272
|
+
break;
|
273
|
+
case 1:
|
274
|
+
_context3.n = 2;
|
275
|
+
return (0, _buildPoseidon["default"])();
|
276
|
+
case 2:
|
277
|
+
this.poseidon = _context3.v;
|
278
|
+
case 3:
|
279
|
+
(_this$zero2 = this.zero) !== null && _this$zero2 !== void 0 ? _this$zero2 : this.zero = this.poseidon(["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]);
|
280
|
+
_hash_value = _value;
|
281
|
+
if (_value.length === 256) {
|
282
|
+
_hash_value = [];
|
283
|
+
_iterator2 = _createForOfIteratorHelper((0, _ramda.splitEvery)(16, _value));
|
284
|
+
try {
|
285
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
286
|
+
v = _step2.value;
|
287
|
+
_value2 = v.join("") === "0000000000000000" ? this.zero : this.poseidon(v);
|
288
|
+
_hash_value.push(_value2);
|
289
|
+
}
|
290
|
+
} catch (err) {
|
291
|
+
_iterator2.e(err);
|
292
|
+
} finally {
|
293
|
+
_iterator2.f();
|
294
|
+
}
|
295
|
+
}
|
296
|
+
value = this.poseidon(_hash_value);
|
297
|
+
F = this.F;
|
298
|
+
key = F.e(_key);
|
299
|
+
addedOne = false;
|
300
|
+
res = {};
|
301
|
+
res.oldRoot = this.root;
|
302
|
+
newKeyBits = this._splitBits(key);
|
303
|
+
_context3.n = 4;
|
304
|
+
return this.find(key);
|
305
|
+
case 4:
|
306
|
+
resFind = _context3.v;
|
307
|
+
if (!resFind.found) {
|
308
|
+
_context3.n = 5;
|
309
|
+
break;
|
310
|
+
}
|
311
|
+
throw new Error("Key already exists");
|
312
|
+
case 5:
|
313
|
+
res.siblings = resFind.siblings;
|
314
|
+
if (!resFind.isOld0) {
|
315
|
+
oldKeyits = this._splitBits(resFind.notFoundKey);
|
316
|
+
for (i = res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) {
|
317
|
+
res.siblings.push(F.zero);
|
318
|
+
}
|
319
|
+
rtOld = this.hash1(resFind.notFoundKey, resFind.notFoundValue);
|
320
|
+
res.siblings.push(rtOld);
|
321
|
+
addedOne = true;
|
322
|
+
mixed = false;
|
323
|
+
} else if (res.siblings.length > 0) {
|
324
|
+
mixed = true;
|
325
|
+
rtOld = F.zero;
|
326
|
+
}
|
327
|
+
inserts = [];
|
328
|
+
dels = [];
|
329
|
+
rt = this.hash1(key, value);
|
330
|
+
inserts.push([rt, [1, key, value]]);
|
331
|
+
for (_i = res.siblings.length - 1; _i >= 0; _i--) {
|
332
|
+
if (_i < res.siblings.length - 1 && !F.isZero(res.siblings[_i])) {
|
333
|
+
mixed = true;
|
334
|
+
}
|
335
|
+
if (mixed) {
|
336
|
+
oldSibling = resFind.siblings[_i];
|
337
|
+
if (newKeyBits[_i]) {
|
338
|
+
rtOld = this.hash0(oldSibling, rtOld);
|
339
|
+
} else {
|
340
|
+
rtOld = this.hash0(rtOld, oldSibling);
|
341
|
+
}
|
342
|
+
dels.push(rtOld);
|
343
|
+
}
|
344
|
+
newRt = void 0;
|
345
|
+
if (newKeyBits[_i]) {
|
346
|
+
newRt = this.hash0(res.siblings[_i], rt);
|
347
|
+
inserts.push([newRt, [res.siblings[_i], rt]]);
|
348
|
+
} else {
|
349
|
+
newRt = this.hash0(rt, res.siblings[_i]);
|
350
|
+
inserts.push([newRt, [rt, res.siblings[_i]]]);
|
351
|
+
}
|
352
|
+
rt = newRt;
|
353
|
+
}
|
354
|
+
if (addedOne) res.siblings.pop();
|
355
|
+
while (res.siblings.length > 0 && F.isZero(res.siblings[res.siblings.length - 1])) {
|
356
|
+
res.siblings.pop();
|
357
|
+
}
|
358
|
+
res.oldKey = resFind.notFoundKey;
|
359
|
+
res.oldValue = resFind.notFoundValue;
|
360
|
+
res.newRoot = rt;
|
361
|
+
res.isOld0 = resFind.isOld0;
|
362
|
+
_context3.n = 6;
|
363
|
+
return this.db.multiIns(inserts);
|
364
|
+
case 6:
|
365
|
+
_context3.n = 7;
|
366
|
+
return this.db.setRoot(rt);
|
367
|
+
case 7:
|
368
|
+
this.root = rt;
|
369
|
+
_context3.n = 8;
|
370
|
+
return this.db.multiDel(dels);
|
371
|
+
case 8:
|
372
|
+
return _context3.a(2, res);
|
373
|
+
}
|
374
|
+
}, _callee3, this);
|
375
|
+
}));
|
376
|
+
function insert(_x4, _x5) {
|
377
|
+
return _insert.apply(this, arguments);
|
378
|
+
}
|
379
|
+
return insert;
|
380
|
+
}()
|
381
|
+
}, {
|
382
|
+
key: "find",
|
383
|
+
value: function () {
|
384
|
+
var _find2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_key) {
|
385
|
+
var key, keyBits;
|
386
|
+
return _regenerator().w(function (_context4) {
|
387
|
+
while (1) switch (_context4.n) {
|
388
|
+
case 0:
|
389
|
+
key = this.F.e(_key);
|
390
|
+
keyBits = this._splitBits(key);
|
391
|
+
_context4.n = 1;
|
392
|
+
return this._find(key, keyBits, this.root, 0);
|
393
|
+
case 1:
|
394
|
+
return _context4.a(2, _context4.v);
|
395
|
+
}
|
396
|
+
}, _callee4, this);
|
397
|
+
}));
|
398
|
+
function find(_x6) {
|
399
|
+
return _find2.apply(this, arguments);
|
400
|
+
}
|
401
|
+
return find;
|
402
|
+
}()
|
403
|
+
}, {
|
404
|
+
key: "_find",
|
405
|
+
value: function () {
|
406
|
+
var _find3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(key, keyBits, root, level) {
|
407
|
+
var F, res, record;
|
408
|
+
return _regenerator().w(function (_context5) {
|
409
|
+
while (1) switch (_context5.n) {
|
410
|
+
case 0:
|
411
|
+
F = this.F;
|
412
|
+
if (typeof root === "undefined") root = this.root;
|
413
|
+
if (!F.isZero(root)) {
|
414
|
+
_context5.n = 1;
|
415
|
+
break;
|
416
|
+
}
|
417
|
+
res = {
|
418
|
+
found: false,
|
419
|
+
siblings: [],
|
420
|
+
notFoundKey: key,
|
421
|
+
notFoundValue: F.zero,
|
422
|
+
isOld0: true
|
423
|
+
};
|
424
|
+
return _context5.a(2, res);
|
425
|
+
case 1:
|
426
|
+
_context5.n = 2;
|
427
|
+
return this.db.get(root);
|
428
|
+
case 2:
|
429
|
+
record = _context5.v;
|
430
|
+
if (!(record.length == 3 && F.eq(record[0], F.one))) {
|
431
|
+
_context5.n = 3;
|
432
|
+
break;
|
433
|
+
}
|
434
|
+
if (F.eq(record[1], key)) {
|
435
|
+
res = {
|
436
|
+
found: true,
|
437
|
+
siblings: [],
|
438
|
+
foundValue: record[2],
|
439
|
+
isOld0: false
|
440
|
+
};
|
441
|
+
} else {
|
442
|
+
res = {
|
443
|
+
found: false,
|
444
|
+
siblings: [],
|
445
|
+
notFoundKey: record[1],
|
446
|
+
notFoundValue: record[2],
|
447
|
+
isOld0: false
|
448
|
+
};
|
449
|
+
}
|
450
|
+
_context5.n = 7;
|
451
|
+
break;
|
452
|
+
case 3:
|
453
|
+
if (!(keyBits[level] == 0)) {
|
454
|
+
_context5.n = 5;
|
455
|
+
break;
|
456
|
+
}
|
457
|
+
_context5.n = 4;
|
458
|
+
return this._find(key, keyBits, record[0], level + 1);
|
459
|
+
case 4:
|
460
|
+
res = _context5.v;
|
461
|
+
res.siblings.unshift(record[1]);
|
462
|
+
_context5.n = 7;
|
463
|
+
break;
|
464
|
+
case 5:
|
465
|
+
_context5.n = 6;
|
466
|
+
return this._find(key, keyBits, record[1], level + 1);
|
467
|
+
case 6:
|
468
|
+
res = _context5.v;
|
469
|
+
res.siblings.unshift(record[0]);
|
470
|
+
case 7:
|
471
|
+
return _context5.a(2, res);
|
472
|
+
}
|
473
|
+
}, _callee5, this);
|
474
|
+
}));
|
475
|
+
function _find(_x7, _x8, _x9, _x0) {
|
476
|
+
return _find3.apply(this, arguments);
|
477
|
+
}
|
478
|
+
return _find;
|
479
|
+
}()
|
480
|
+
}]);
|
481
|
+
}();
|
@@ -0,0 +1,173 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports["default"] = void 0;
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
8
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
9
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
10
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
11
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
12
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
13
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
14
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
17
|
+
var SMTMemDb = exports["default"] = /*#__PURE__*/function () {
|
18
|
+
function SMTMemDb(F, kv) {
|
19
|
+
_classCallCheck(this, SMTMemDb);
|
20
|
+
this.kv = kv;
|
21
|
+
this.nodes = {};
|
22
|
+
this.root = F.zero;
|
23
|
+
if (this.kv) {
|
24
|
+
var root = this.kv.get("__root__");
|
25
|
+
if (root) this.root = root;else this.kv.put("__root__", this.root);
|
26
|
+
}
|
27
|
+
this.F = F;
|
28
|
+
}
|
29
|
+
return _createClass(SMTMemDb, [{
|
30
|
+
key: "getRoot",
|
31
|
+
value: function () {
|
32
|
+
var _getRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
33
|
+
return _regenerator().w(function (_context) {
|
34
|
+
while (1) switch (_context.n) {
|
35
|
+
case 0:
|
36
|
+
return _context.a(2, this.root);
|
37
|
+
}
|
38
|
+
}, _callee, this);
|
39
|
+
}));
|
40
|
+
function getRoot() {
|
41
|
+
return _getRoot.apply(this, arguments);
|
42
|
+
}
|
43
|
+
return getRoot;
|
44
|
+
}()
|
45
|
+
}, {
|
46
|
+
key: "_key2str",
|
47
|
+
value: function _key2str(k) {
|
48
|
+
this.F;
|
49
|
+
var keyS = this.F.toString(k);
|
50
|
+
return keyS;
|
51
|
+
}
|
52
|
+
}, {
|
53
|
+
key: "_normalize",
|
54
|
+
value: function _normalize(n) {
|
55
|
+
this.F;
|
56
|
+
for (var i = 0; i < n.length; i++) {
|
57
|
+
n[i] = this.F.e(n[i]);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}, {
|
61
|
+
key: "get",
|
62
|
+
value: function () {
|
63
|
+
var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(key) {
|
64
|
+
var keyS, node;
|
65
|
+
return _regenerator().w(function (_context2) {
|
66
|
+
while (1) switch (_context2.n) {
|
67
|
+
case 0:
|
68
|
+
keyS = this._key2str(key);
|
69
|
+
if (!this.nodes[keyS]) {
|
70
|
+
node = this.kv.get("__nodes__/".concat(keyS));
|
71
|
+
if (node) this.nodes[keyS] = node;
|
72
|
+
}
|
73
|
+
return _context2.a(2, this.nodes[keyS]);
|
74
|
+
}
|
75
|
+
}, _callee2, this);
|
76
|
+
}));
|
77
|
+
function get(_x) {
|
78
|
+
return _get.apply(this, arguments);
|
79
|
+
}
|
80
|
+
return get;
|
81
|
+
}()
|
82
|
+
}, {
|
83
|
+
key: "multiGet",
|
84
|
+
value: function () {
|
85
|
+
var _multiGet = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(keys) {
|
86
|
+
var promises, i;
|
87
|
+
return _regenerator().w(function (_context3) {
|
88
|
+
while (1) switch (_context3.n) {
|
89
|
+
case 0:
|
90
|
+
promises = [];
|
91
|
+
for (i = 0; i < keys.length; i++) {
|
92
|
+
promises.push(this.get(keys[i]));
|
93
|
+
}
|
94
|
+
_context3.n = 1;
|
95
|
+
return Promise.all(promises);
|
96
|
+
case 1:
|
97
|
+
return _context3.a(2, _context3.v);
|
98
|
+
}
|
99
|
+
}, _callee3, this);
|
100
|
+
}));
|
101
|
+
function multiGet(_x2) {
|
102
|
+
return _multiGet.apply(this, arguments);
|
103
|
+
}
|
104
|
+
return multiGet;
|
105
|
+
}()
|
106
|
+
}, {
|
107
|
+
key: "setRoot",
|
108
|
+
value: function () {
|
109
|
+
var _setRoot = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(rt) {
|
110
|
+
return _regenerator().w(function (_context4) {
|
111
|
+
while (1) switch (_context4.n) {
|
112
|
+
case 0:
|
113
|
+
this.root = rt;
|
114
|
+
if (this.kv) this.kv.put("__root__", this.root);
|
115
|
+
case 1:
|
116
|
+
return _context4.a(2);
|
117
|
+
}
|
118
|
+
}, _callee4, this);
|
119
|
+
}));
|
120
|
+
function setRoot(_x3) {
|
121
|
+
return _setRoot.apply(this, arguments);
|
122
|
+
}
|
123
|
+
return setRoot;
|
124
|
+
}()
|
125
|
+
}, {
|
126
|
+
key: "multiIns",
|
127
|
+
value: function () {
|
128
|
+
var _multiIns = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(inserts) {
|
129
|
+
var i, keyS;
|
130
|
+
return _regenerator().w(function (_context5) {
|
131
|
+
while (1) switch (_context5.n) {
|
132
|
+
case 0:
|
133
|
+
for (i = 0; i < inserts.length; i++) {
|
134
|
+
keyS = this._key2str(inserts[i][0]);
|
135
|
+
this._normalize(inserts[i][1]);
|
136
|
+
this.nodes[keyS] = inserts[i][1];
|
137
|
+
if (this.kv) this.kv.put("__nodes__/".concat(keyS), this.nodes[keyS]);
|
138
|
+
}
|
139
|
+
case 1:
|
140
|
+
return _context5.a(2);
|
141
|
+
}
|
142
|
+
}, _callee5, this);
|
143
|
+
}));
|
144
|
+
function multiIns(_x4) {
|
145
|
+
return _multiIns.apply(this, arguments);
|
146
|
+
}
|
147
|
+
return multiIns;
|
148
|
+
}()
|
149
|
+
}, {
|
150
|
+
key: "multiDel",
|
151
|
+
value: function () {
|
152
|
+
var _multiDel = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(dels) {
|
153
|
+
var i, keyS;
|
154
|
+
return _regenerator().w(function (_context6) {
|
155
|
+
while (1) switch (_context6.n) {
|
156
|
+
case 0:
|
157
|
+
for (i = 0; i < dels.length; i++) {
|
158
|
+
keyS = this._key2str(dels[i]);
|
159
|
+
delete this.nodes[keyS];
|
160
|
+
if (this.kv) this.kv.del("__nodes__/".concat(keyS));
|
161
|
+
}
|
162
|
+
case 1:
|
163
|
+
return _context6.a(2);
|
164
|
+
}
|
165
|
+
}, _callee6, this);
|
166
|
+
}));
|
167
|
+
function multiDel(_x5) {
|
168
|
+
return _multiDel.apply(this, arguments);
|
169
|
+
}
|
170
|
+
return multiDel;
|
171
|
+
}()
|
172
|
+
}]);
|
173
|
+
}();
|