@leofcoin/chain 1.5.28 → 1.5.30

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.
@@ -1,2185 +0,0 @@
1
- import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, T as TransactionMessage, t as toBase58, C as ContractMessage, R as RawTransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-055d5584.js';
2
-
3
- const logger$1 = new Logger(version$1);
4
- const _constructorGuard = {};
5
- const Zero = BigNumber.from(0);
6
- const NegativeOne = BigNumber.from(-1);
7
- function throwFault(message, fault, operation, value) {
8
- const params = { fault: fault, operation: operation };
9
- if (value !== undefined) {
10
- params.value = value;
11
- }
12
- return logger$1.throwError(message, Logger.errors.NUMERIC_FAULT, params);
13
- }
14
- // Constant to pull zeros from for multipliers
15
- let zeros = "0";
16
- while (zeros.length < 256) {
17
- zeros += zeros;
18
- }
19
- // Returns a string "1" followed by decimal "0"s
20
- function getMultiplier(decimals) {
21
- if (typeof (decimals) !== "number") {
22
- try {
23
- decimals = BigNumber.from(decimals).toNumber();
24
- }
25
- catch (e) { }
26
- }
27
- if (typeof (decimals) === "number" && decimals >= 0 && decimals <= 256 && !(decimals % 1)) {
28
- return ("1" + zeros.substring(0, decimals));
29
- }
30
- return logger$1.throwArgumentError("invalid decimal size", "decimals", decimals);
31
- }
32
- function formatFixed(value, decimals) {
33
- if (decimals == null) {
34
- decimals = 0;
35
- }
36
- const multiplier = getMultiplier(decimals);
37
- // Make sure wei is a big number (convert as necessary)
38
- value = BigNumber.from(value);
39
- const negative = value.lt(Zero);
40
- if (negative) {
41
- value = value.mul(NegativeOne);
42
- }
43
- let fraction = value.mod(multiplier).toString();
44
- while (fraction.length < multiplier.length - 1) {
45
- fraction = "0" + fraction;
46
- }
47
- // Strip training 0
48
- fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
49
- const whole = value.div(multiplier).toString();
50
- if (multiplier.length === 1) {
51
- value = whole;
52
- }
53
- else {
54
- value = whole + "." + fraction;
55
- }
56
- if (negative) {
57
- value = "-" + value;
58
- }
59
- return value;
60
- }
61
- function parseFixed(value, decimals) {
62
- if (decimals == null) {
63
- decimals = 0;
64
- }
65
- const multiplier = getMultiplier(decimals);
66
- if (typeof (value) !== "string" || !value.match(/^-?[0-9.]+$/)) {
67
- logger$1.throwArgumentError("invalid decimal value", "value", value);
68
- }
69
- // Is it negative?
70
- const negative = (value.substring(0, 1) === "-");
71
- if (negative) {
72
- value = value.substring(1);
73
- }
74
- if (value === ".") {
75
- logger$1.throwArgumentError("missing value", "value", value);
76
- }
77
- // Split it into a whole and fractional part
78
- const comps = value.split(".");
79
- if (comps.length > 2) {
80
- logger$1.throwArgumentError("too many decimal points", "value", value);
81
- }
82
- let whole = comps[0], fraction = comps[1];
83
- if (!whole) {
84
- whole = "0";
85
- }
86
- if (!fraction) {
87
- fraction = "0";
88
- }
89
- // Trim trailing zeros
90
- while (fraction[fraction.length - 1] === "0") {
91
- fraction = fraction.substring(0, fraction.length - 1);
92
- }
93
- // Check the fraction doesn't exceed our decimals size
94
- if (fraction.length > multiplier.length - 1) {
95
- throwFault("fractional component exceeds decimals", "underflow", "parseFixed");
96
- }
97
- // If decimals is 0, we have an empty string for fraction
98
- if (fraction === "") {
99
- fraction = "0";
100
- }
101
- // Fully pad the string with zeros to get to wei
102
- while (fraction.length < multiplier.length - 1) {
103
- fraction += "0";
104
- }
105
- const wholeValue = BigNumber.from(whole);
106
- const fractionValue = BigNumber.from(fraction);
107
- let wei = (wholeValue.mul(multiplier)).add(fractionValue);
108
- if (negative) {
109
- wei = wei.mul(NegativeOne);
110
- }
111
- return wei;
112
- }
113
- class FixedFormat {
114
- constructor(constructorGuard, signed, width, decimals) {
115
- if (constructorGuard !== _constructorGuard) {
116
- logger$1.throwError("cannot use FixedFormat constructor; use FixedFormat.from", Logger.errors.UNSUPPORTED_OPERATION, {
117
- operation: "new FixedFormat"
118
- });
119
- }
120
- this.signed = signed;
121
- this.width = width;
122
- this.decimals = decimals;
123
- this.name = (signed ? "" : "u") + "fixed" + String(width) + "x" + String(decimals);
124
- this._multiplier = getMultiplier(decimals);
125
- Object.freeze(this);
126
- }
127
- static from(value) {
128
- if (value instanceof FixedFormat) {
129
- return value;
130
- }
131
- if (typeof (value) === "number") {
132
- value = `fixed128x${value}`;
133
- }
134
- let signed = true;
135
- let width = 128;
136
- let decimals = 18;
137
- if (typeof (value) === "string") {
138
- if (value === "fixed") ;
139
- else if (value === "ufixed") {
140
- signed = false;
141
- }
142
- else {
143
- const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
144
- if (!match) {
145
- logger$1.throwArgumentError("invalid fixed format", "format", value);
146
- }
147
- signed = (match[1] !== "u");
148
- width = parseInt(match[2]);
149
- decimals = parseInt(match[3]);
150
- }
151
- }
152
- else if (value) {
153
- const check = (key, type, defaultValue) => {
154
- if (value[key] == null) {
155
- return defaultValue;
156
- }
157
- if (typeof (value[key]) !== type) {
158
- logger$1.throwArgumentError("invalid fixed format (" + key + " not " + type + ")", "format." + key, value[key]);
159
- }
160
- return value[key];
161
- };
162
- signed = check("signed", "boolean", signed);
163
- width = check("width", "number", width);
164
- decimals = check("decimals", "number", decimals);
165
- }
166
- if (width % 8) {
167
- logger$1.throwArgumentError("invalid fixed format width (not byte aligned)", "format.width", width);
168
- }
169
- if (decimals > 80) {
170
- logger$1.throwArgumentError("invalid fixed format (decimals too large)", "format.decimals", decimals);
171
- }
172
- return new FixedFormat(_constructorGuard, signed, width, decimals);
173
- }
174
- }
175
- class FixedNumber {
176
- constructor(constructorGuard, hex, value, format) {
177
- if (constructorGuard !== _constructorGuard) {
178
- logger$1.throwError("cannot use FixedNumber constructor; use FixedNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
179
- operation: "new FixedFormat"
180
- });
181
- }
182
- this.format = format;
183
- this._hex = hex;
184
- this._value = value;
185
- this._isFixedNumber = true;
186
- Object.freeze(this);
187
- }
188
- _checkFormat(other) {
189
- if (this.format.name !== other.format.name) {
190
- logger$1.throwArgumentError("incompatible format; use fixedNumber.toFormat", "other", other);
191
- }
192
- }
193
- addUnsafe(other) {
194
- this._checkFormat(other);
195
- const a = parseFixed(this._value, this.format.decimals);
196
- const b = parseFixed(other._value, other.format.decimals);
197
- return FixedNumber.fromValue(a.add(b), this.format.decimals, this.format);
198
- }
199
- subUnsafe(other) {
200
- this._checkFormat(other);
201
- const a = parseFixed(this._value, this.format.decimals);
202
- const b = parseFixed(other._value, other.format.decimals);
203
- return FixedNumber.fromValue(a.sub(b), this.format.decimals, this.format);
204
- }
205
- mulUnsafe(other) {
206
- this._checkFormat(other);
207
- const a = parseFixed(this._value, this.format.decimals);
208
- const b = parseFixed(other._value, other.format.decimals);
209
- return FixedNumber.fromValue(a.mul(b).div(this.format._multiplier), this.format.decimals, this.format);
210
- }
211
- divUnsafe(other) {
212
- this._checkFormat(other);
213
- const a = parseFixed(this._value, this.format.decimals);
214
- const b = parseFixed(other._value, other.format.decimals);
215
- return FixedNumber.fromValue(a.mul(this.format._multiplier).div(b), this.format.decimals, this.format);
216
- }
217
- floor() {
218
- const comps = this.toString().split(".");
219
- if (comps.length === 1) {
220
- comps.push("0");
221
- }
222
- let result = FixedNumber.from(comps[0], this.format);
223
- const hasFraction = !comps[1].match(/^(0*)$/);
224
- if (this.isNegative() && hasFraction) {
225
- result = result.subUnsafe(ONE.toFormat(result.format));
226
- }
227
- return result;
228
- }
229
- ceiling() {
230
- const comps = this.toString().split(".");
231
- if (comps.length === 1) {
232
- comps.push("0");
233
- }
234
- let result = FixedNumber.from(comps[0], this.format);
235
- const hasFraction = !comps[1].match(/^(0*)$/);
236
- if (!this.isNegative() && hasFraction) {
237
- result = result.addUnsafe(ONE.toFormat(result.format));
238
- }
239
- return result;
240
- }
241
- // @TODO: Support other rounding algorithms
242
- round(decimals) {
243
- if (decimals == null) {
244
- decimals = 0;
245
- }
246
- // If we are already in range, we're done
247
- const comps = this.toString().split(".");
248
- if (comps.length === 1) {
249
- comps.push("0");
250
- }
251
- if (decimals < 0 || decimals > 80 || (decimals % 1)) {
252
- logger$1.throwArgumentError("invalid decimal count", "decimals", decimals);
253
- }
254
- if (comps[1].length <= decimals) {
255
- return this;
256
- }
257
- const factor = FixedNumber.from("1" + zeros.substring(0, decimals), this.format);
258
- const bump = BUMP.toFormat(this.format);
259
- return this.mulUnsafe(factor).addUnsafe(bump).floor().divUnsafe(factor);
260
- }
261
- isZero() {
262
- return (this._value === "0.0" || this._value === "0");
263
- }
264
- isNegative() {
265
- return (this._value[0] === "-");
266
- }
267
- toString() { return this._value; }
268
- toHexString(width) {
269
- if (width == null) {
270
- return this._hex;
271
- }
272
- if (width % 8) {
273
- logger$1.throwArgumentError("invalid byte width", "width", width);
274
- }
275
- const hex = BigNumber.from(this._hex).fromTwos(this.format.width).toTwos(width).toHexString();
276
- return hexZeroPad(hex, width / 8);
277
- }
278
- toUnsafeFloat() { return parseFloat(this.toString()); }
279
- toFormat(format) {
280
- return FixedNumber.fromString(this._value, format);
281
- }
282
- static fromValue(value, decimals, format) {
283
- // If decimals looks more like a format, and there is no format, shift the parameters
284
- if (format == null && decimals != null && !isBigNumberish(decimals)) {
285
- format = decimals;
286
- decimals = null;
287
- }
288
- if (decimals == null) {
289
- decimals = 0;
290
- }
291
- if (format == null) {
292
- format = "fixed";
293
- }
294
- return FixedNumber.fromString(formatFixed(value, decimals), FixedFormat.from(format));
295
- }
296
- static fromString(value, format) {
297
- if (format == null) {
298
- format = "fixed";
299
- }
300
- const fixedFormat = FixedFormat.from(format);
301
- const numeric = parseFixed(value, fixedFormat.decimals);
302
- if (!fixedFormat.signed && numeric.lt(Zero)) {
303
- throwFault("unsigned value cannot be negative", "overflow", "value", value);
304
- }
305
- let hex = null;
306
- if (fixedFormat.signed) {
307
- hex = numeric.toTwos(fixedFormat.width).toHexString();
308
- }
309
- else {
310
- hex = numeric.toHexString();
311
- hex = hexZeroPad(hex, fixedFormat.width / 8);
312
- }
313
- const decimal = formatFixed(numeric, fixedFormat.decimals);
314
- return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
315
- }
316
- static fromBytes(value, format) {
317
- if (format == null) {
318
- format = "fixed";
319
- }
320
- const fixedFormat = FixedFormat.from(format);
321
- if (arrayify(value).length > fixedFormat.width / 8) {
322
- throw new Error("overflow");
323
- }
324
- let numeric = BigNumber.from(value);
325
- if (fixedFormat.signed) {
326
- numeric = numeric.fromTwos(fixedFormat.width);
327
- }
328
- const hex = numeric.toTwos((fixedFormat.signed ? 0 : 1) + fixedFormat.width).toHexString();
329
- const decimal = formatFixed(numeric, fixedFormat.decimals);
330
- return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
331
- }
332
- static from(value, format) {
333
- if (typeof (value) === "string") {
334
- return FixedNumber.fromString(value, format);
335
- }
336
- if (isBytes(value)) {
337
- return FixedNumber.fromBytes(value, format);
338
- }
339
- try {
340
- return FixedNumber.fromValue(value, 0, format);
341
- }
342
- catch (error) {
343
- // Allow NUMERIC_FAULT to bubble up
344
- if (error.code !== Logger.errors.INVALID_ARGUMENT) {
345
- throw error;
346
- }
347
- }
348
- return logger$1.throwArgumentError("invalid FixedNumber value", "value", value);
349
- }
350
- static isFixedNumber(value) {
351
- return !!(value && value._isFixedNumber);
352
- }
353
- }
354
- const ONE = FixedNumber.from(1);
355
- const BUMP = FixedNumber.from("0.5");
356
-
357
- const version = "units/5.7.0";
358
-
359
- const logger = new Logger(version);
360
- const names = [
361
- "wei",
362
- "kwei",
363
- "mwei",
364
- "gwei",
365
- "szabo",
366
- "finney",
367
- "ether",
368
- ];
369
- function formatUnits(value, unitName) {
370
- if (typeof (unitName) === "string") {
371
- const index = names.indexOf(unitName);
372
- if (index !== -1) {
373
- unitName = 3 * index;
374
- }
375
- }
376
- return formatFixed(value, (unitName != null) ? unitName : 18);
377
- }
378
- function parseUnits(value, unitName) {
379
- if (typeof (value) !== "string") {
380
- logger.throwArgumentError("value must be a string", "value", value);
381
- }
382
- if (typeof (unitName) === "string") {
383
- const index = names.indexOf(unitName);
384
- if (index !== -1) {
385
- unitName = 3 * index;
386
- }
387
- }
388
- return parseFixed(value, (unitName != null) ? unitName : 18);
389
- }
390
-
391
- const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
392
- const formatBytes = (bytes, decimals = 2) => {
393
- if (bytes === 0)
394
- return '0 Bytes';
395
- if (decimals < 0)
396
- decimals = 0;
397
- const k = 1024;
398
- const i = Math.floor(Math.log(bytes) / Math.log(k));
399
- return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`;
400
- };
401
-
402
- var contractFactory$1 = "IHNY2GQGYO6BDXHHL2KHK3KQU353HPCARUVAP2UVWH4EUVV4JXT373KSVF2";
403
- var nativeToken$1 = "IHNY2GQH3C44RRUDM4XLG67KHHS3UER7L6NULDHH6ACVOAVTLAGBRLB5BSJ";
404
- var nameService$1 = "IHNY2GQH5BUXSQTSUP32R7TFOPN3HDWKL4TT2G7MJANULBRN7WHZ4ANG4DH";
405
- var validators$1 = "IHNY2GQHFKTVEBJGRT6XGHR434KWLGDHOVEI4OB6UK7QUKCNOICMVTXRWTS";
406
- var addresses$1 = {
407
- contractFactory: contractFactory$1,
408
- nativeToken: nativeToken$1,
409
- nameService: nameService$1,
410
- validators: validators$1
411
- };
412
-
413
- const contractFactory$2 = addresses$1.contractFactory;
414
- const nameService$2 = addresses$1.nameService;
415
- const nativeToken$2 = addresses$1.nativeToken;
416
- const validators$2 = addresses$1.validators;
417
- var addresses = {
418
- contractFactory: contractFactory$2,
419
- nameService: nameService$2,
420
- nativeToken: nativeToken$2,
421
- validators: validators$2
422
- };
423
-
424
- var contractFactory = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,173,5,99,108,97,115,115,32,70,97,99,116,111,114,121,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,67,111,110,116,114,97,99,116,70,97,99,116,111,114,121,34,59,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,48,59,35,99,111,110,116,114,97,99,116,115,61,91,93,59,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,116,97,116,101,38,38,40,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,99,111,110,116,114,97,99,116,115,44,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,116,111,116,97,108,67,111,110,116,114,97,99,116,115,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,116,111,116,97,108,67,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,44,99,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,125,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,99,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,91,46,46,46,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,93,125,103,101,116,32,116,111,116,97,108,67,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,125,105,115,82,101,103,105,115,116,101,114,101,100,40,97,100,100,114,101,115,115,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,125,97,115,121,110,99,32,114,101,103,105,115,116,101,114,67,111,110,116,114,97,99,116,40,97,100,100,114,101,115,115,41,123,105,102,40,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,97,100,100,114,101,115,115,44,34,104,97,115,82,111,108,101,34,44,91,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,93,41,44,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,108,114,101,97,100,121,32,114,101,103,105,115,116,101,114,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,43,61,49,44,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,125,114,101,116,117,114,110,32,70,97,99,116,111,114,121,59,2,91,93";
425
- var nativeToken = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,163,26,99,108,97,115,115,32,82,111,108,101,115,123,35,114,111,108,101,115,61,123,79,87,78,69,82,58,91,93,44,77,73,78,84,58,91,93,44,66,85,82,78,58,91,93,125,59,99,111,110,115,116,114,117,99,116,111,114,40,114,111,108,101,115,41,123,105,102,40,114,111,108,101,115,41,123,105,102,40,33,40,114,111,108,101,115,32,105,110,115,116,97,110,99,101,111,102,32,79,98,106,101,99,116,41,41,116,104,114,111,119,32,110,101,119,32,84,121,112,101,69,114,114,111,114,40,34,101,120,112,101,99,116,101,100,32,114,111,108,101,115,32,116,111,32,98,101,32,97,110,32,111,98,106,101,99,116,34,41,59,116,104,105,115,46,35,114,111,108,101,115,61,123,46,46,46,114,111,108,101,115,44,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,101,108,115,101,32,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,114,111,108,101,115,58,116,104,105,115,46,114,111,108,101,115,125,125,103,101,116,32,114,111,108,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,114,101,116,117,114,110,33,33,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,38,38,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,125,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,103,114,97,110,116,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,114,101,118,111,107,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,105,102,40,34,79,87,78,69,82,34,61,61,61,114,111,108,101,38,38,49,61,61,61,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,108,101,110,103,116,104,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,116,108,101,97,115,116,32,111,110,101,32,111,119,110,101,114,32,105,115,32,110,101,101,100,101,100,33,34,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,115,112,108,105,99,101,40,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,100,101,120,79,102,40,97,100,100,114,101,115,115,41,41,125,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,125,99,108,97,115,115,32,84,111,107,101,110,32,101,120,116,101,110,100,115,32,82,111,108,101,115,123,35,110,97,109,101,59,35,115,121,109,98,111,108,59,35,104,111,108,100,101,114,115,61,48,59,35,98,97,108,97,110,99,101,115,61,123,125,59,35,97,112,112,114,111,118,97,108,115,61,123,125,59,35,100,101,99,105,109,97,108,115,61,49,56,59,35,116,111,116,97,108,83,117,112,112,108,121,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,59,99,111,110,115,116,114,117,99,116,111,114,40,110,97,109,101,44,115,121,109,98,111,108,44,100,101,99,105,109,97,108,115,61,49,56,44,115,116,97,116,101,41,123,105,102,40,33,110,97,109,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,97,109,101,32,117,110,100,101,102,105,110,101,100,34,41,59,105,102,40,33,115,121,109,98,111,108,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,115,121,109,98,111,108,32,117,110,100,101,102,105,110,101,100,34,41,59,115,117,112,101,114,40,115,116,97,116,101,63,46,114,111,108,101,115,41,44,116,104,105,115,46,35,110,97,109,101,61,110,97,109,101,44,116,104,105,115,46,35,115,121,109,98,111,108,61,115,121,109,98,111,108,44,116,104,105,115,46,35,100,101,99,105,109,97,108,115,61,100,101,99,105,109,97,108,115,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,46,46,46,115,117,112,101,114,46,115,116,97,116,101,44,104,111,108,100,101,114,115,58,116,104,105,115,46,104,111,108,100,101,114,115,44,98,97,108,97,110,99,101,115,58,116,104,105,115,46,98,97,108,97,110,99,101,115,44,97,112,112,114,111,118,97,108,115,58,123,46,46,46,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,125,44,116,111,116,97,108,83,117,112,112,108,121,58,116,104,105,115,46,116,111,116,97,108,83,117,112,112,108,121,125,125,103,101,116,32,116,111,116,97,108,83,117,112,112,108,121,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,115,121,109,98,111,108,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,115,121,109,98,111,108,125,103,101,116,32,104,111,108,100,101,114,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,104,111,108,100,101,114,115,125,103,101,116,32,98,97,108,97,110,99,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,98,97,108,97,110,99,101,115,125,125,109,105,110,116,40,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,77,73,78,84,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,98,117,114,110,40,102,114,111,109,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,66,85,82,78,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,102,114,111,109,44,97,109,111,117,110,116,41,125,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,124,124,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,60,97,109,111,117,110,116,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,109,111,117,110,116,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,34,41,125,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,123,34,48,120,48,48,34,61,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,63,116,104,105,115,46,35,104,111,108,100,101,114,115,45,61,49,58,34,48,120,48,48,34,33,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,34,48,120,48,48,34,61,61,61,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,40,116,104,105,115,46,35,104,111,108,100,101,114,115,43,61,49,41,125,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,124,124,40,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,41,59,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,98,97,108,97,110,99,101,79,102,40,97,100,100,114,101,115,115,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,125,115,101,116,65,112,112,114,111,118,97,108,40,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,59,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,124,124,40,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,61,123,125,41,44,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,97,109,111,117,110,116,125,97,112,112,114,111,118,101,100,40,111,119,110,101,114,44,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,61,61,97,109,111,117,110,116,125,116,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,97,109,111,117,110,116,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,102,114,111,109,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,125,99,108,97,115,115,32,65,114,116,79,110,108,105,110,101,32,101,120,116,101,110,100,115,32,84,111,107,101,110,123,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,117,112,101,114,40,34,65,114,116,79,110,108,105,110,101,34,44,34,65,82,84,34,44,49,56,44,115,116,97,116,101,41,125,125,114,101,116,117,114,110,32,65,114,116,79,110,108,105,110,101,59,2,91,93";
426
- var nameService = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,131,13,99,108,97,115,115,32,78,97,109,101,83,101,114,118,105,99,101,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,78,97,109,101,83,101,114,118,105,99,101,34,59,35,111,119,110,101,114,59,35,112,114,105,99,101,61,48,59,35,114,101,103,105,115,116,114,121,61,123,125,59,35,99,117,114,114,101,110,99,121,59,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,114,101,103,105,115,116,114,121,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,101,103,105,115,116,114,121,125,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,111,119,110,101,114,58,116,104,105,115,46,35,111,119,110,101,114,44,114,101,103,105,115,116,114,121,58,116,104,105,115,46,35,114,101,103,105,115,116,114,121,44,99,117,114,114,101,110,99,121,58,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,112,114,105,99,101,58,116,104,105,115,46,35,112,114,105,99,101,125,125,99,111,110,115,116,114,117,99,116,111,114,40,102,97,99,116,111,114,121,65,100,100,114,101,115,115,44,99,117,114,114,101,110,99,121,44,118,97,108,105,100,97,116,111,114,65,100,100,114,101,115,115,44,112,114,105,99,101,44,115,116,97,116,101,41,123,115,116,97,116,101,63,40,116,104,105,115,46,35,111,119,110,101,114,61,115,116,97,116,101,46,111,119,110,101,114,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,61,115,116,97,116,101,46,114,101,103,105,115,116,114,121,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,115,116,97,116,101,46,99,117,114,114,101,110,99,121,44,116,104,105,115,46,35,112,114,105,99,101,61,115,116,97,116,101,46,112,114,105,99,101,41,58,40,116,104,105,115,46,35,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,44,116,104,105,115,46,35,112,114,105,99,101,61,112,114,105,99,101,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,67,111,110,116,114,97,99,116,70,97,99,116,111,114,121,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,102,97,99,116,111,114,121,65,100,100,114,101,115,115,125,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,84,111,107,101,110,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,99,117,114,114,101,110,99,121,125,44,116,104,105,115,46,35,114,101,103,105,115,116,114,121,46,65,114,116,79,110,108,105,110,101,86,97,108,105,100,97,116,111,114,115,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,118,97,108,105,100,97,116,111,114,65,100,100,114,101,115,115,125,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,41,125,99,104,97,110,103,101,79,119,110,101,114,40,111,119,110,101,114,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,111,119,110,101,114,61,111,119,110,101,114,125,99,104,97,110,103,101,80,114,105,99,101,40,112,114,105,99,101,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,112,114,105,99,101,61,112,114,105,99,101,125,99,104,97,110,103,101,67,117,114,114,101,110,99,121,40,99,117,114,114,101,110,99,121,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,125,97,115,121,110,99,32,112,117,114,99,104,97,115,101,78,97,109,101,40,110,97,109,101,44,97,100,100,114,101,115,115,41,123,105,102,40,97,119,97,105,116,32,109,115,103,46,99,97,108,108,40,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,109,115,103,46,115,101,110,100,101,114,93,41,60,116,104,105,115,46,35,112,114,105,99,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,112,114,105,99,101,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,34,41,59,116,114,121,123,97,119,97,105,116,32,109,115,103,46,99,97,108,108,40,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,34,116,114,97,110,115,102,101,114,34,44,91,109,115,103,46,115,101,110,100,101,114,44,116,104,105,115,46,35,111,119,110,101,114,44,116,104,105,115,46,35,112,114,105,99,101,93,41,125,99,97,116,99,104,40,101,114,114,111,114,41,123,116,104,114,111,119,32,101,114,114,111,114,125,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,61,123,111,119,110,101,114,58,109,115,103,46,115,101,110,100,101,114,44,97,100,100,114,101,115,115,58,97,100,100,114,101,115,115,125,125,108,111,111,107,117,112,40,110,97,109,101,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,125,116,114,97,110,115,102,101,114,79,119,110,101,114,115,104,105,112,40,110,97,109,101,44,116,111,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,111,119,110,101,114,61,116,111,125,99,104,97,110,103,101,65,100,100,114,101,115,115,40,110,97,109,101,44,97,100,100,114,101,115,115,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,114,101,103,105,115,116,114,121,91,110,97,109,101,93,46,97,100,100,114,101,115,115,61,97,100,100,114,101,115,115,125,125,114,101,116,117,114,110,32,78,97,109,101,83,101,114,118,105,99,101,59,212,1,91,34,73,72,78,89,50,71,81,71,89,79,54,66,68,88,72,72,76,50,75,72,75,51,75,81,85,51,53,51,72,80,67,65,82,85,86,65,80,50,85,86,87,72,52,69,85,86,86,52,74,88,84,51,55,51,75,83,86,70,50,34,44,34,73,72,78,89,50,71,81,72,51,67,52,52,82,82,85,68,77,52,88,76,71,54,55,75,72,72,83,51,85,69,82,55,76,54,78,85,76,68,72,72,54,65,67,86,79,65,86,84,76,65,71,66,82,76,66,53,66,83,74,34,44,34,73,72,78,89,50,71,81,72,70,75,84,86,69,66,74,71,82,84,54,88,71,72,82,52,51,52,75,87,76,71,68,72,79,86,69,73,52,79,66,54,85,75,55,81,85,75,67,78,79,73,67,77,86,84,88,82,87,84,83,34,44,34,49,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,34,93";
427
- var validators = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,164,28,99,108,97,115,115,32,82,111,108,101,115,123,35,114,111,108,101,115,61,123,79,87,78,69,82,58,91,93,44,77,73,78,84,58,91,93,44,66,85,82,78,58,91,93,125,59,99,111,110,115,116,114,117,99,116,111,114,40,114,111,108,101,115,41,123,105,102,40,114,111,108,101,115,41,123,105,102,40,33,40,114,111,108,101,115,32,105,110,115,116,97,110,99,101,111,102,32,79,98,106,101,99,116,41,41,116,104,114,111,119,32,110,101,119,32,84,121,112,101,69,114,114,111,114,40,34,101,120,112,101,99,116,101,100,32,114,111,108,101,115,32,116,111,32,98,101,32,97,110,32,111,98,106,101,99,116,34,41,59,116,104,105,115,46,35,114,111,108,101,115,61,123,46,46,46,114,111,108,101,115,44,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,101,108,115,101,32,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,114,111,108,101,115,58,116,104,105,115,46,114,111,108,101,115,125,125,103,101,116,32,114,111,108,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,114,101,116,117,114,110,33,33,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,38,38,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,125,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,103,114,97,110,116,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,114,101,118,111,107,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,105,102,40,34,79,87,78,69,82,34,61,61,61,114,111,108,101,38,38,49,61,61,61,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,108,101,110,103,116,104,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,116,108,101,97,115,116,32,111,110,101,32,111,119,110,101,114,32,105,115,32,110,101,101,100,101,100,33,34,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,115,112,108,105,99,101,40,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,100,101,120,79,102,40,97,100,100,114,101,115,115,41,41,125,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,125,99,108,97,115,115,32,86,97,108,105,100,97,116,111,114,115,32,101,120,116,101,110,100,115,32,82,111,108,101,115,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,86,97,108,105,100,97,116,111,114,115,34,59,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,61,48,59,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,61,48,59,35,118,97,108,105,100,97,116,111,114,115,61,123,125,59,35,99,117,114,114,101,110,99,121,59,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,59,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,46,46,46,115,117,112,101,114,46,115,116,97,116,101,44,109,105,110,105,109,117,109,66,97,108,97,110,99,101,58,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,44,99,117,114,114,101,110,99,121,58,116,104,105,115,46,35,99,117,114,114,101,110,99,121,44,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,58,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,44,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,58,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,44,118,97,108,105,100,97,116,111,114,115,58,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,125,125,99,111,110,115,116,114,117,99,116,111,114,40,116,111,107,101,110,65,100,100,114,101,115,115,44,115,116,97,116,101,41,123,115,117,112,101,114,40,115,116,97,116,101,63,46,114,111,108,101,115,41,44,115,116,97,116,101,63,40,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,61,115,116,97,116,101,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,115,116,97,116,101,46,99,117,114,114,101,110,99,121,44,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,61,115,116,97,116,101,46,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,44,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,61,115,116,97,116,101,46,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,61,115,116,97,116,101,46,118,97,108,105,100,97,116,111,114,115,41,58,40,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,61,53,101,52,44,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,116,111,107,101,110,65,100,100,114,101,115,115,44,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,109,115,103,46,115,101,110,100,101,114,93,61,123,102,105,114,115,116,83,101,101,110,58,68,97,116,101,46,110,111,119,40,41,44,108,97,115,116,83,101,101,110,58,68,97,116,101,46,110,111,119,40,41,44,97,99,116,105,118,101,58,33,48,125,41,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,99,117,114,114,101,110,99,121,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,99,117,114,114,101,110,99,121,125,103,101,116,32,118,97,108,105,100,97,116,111,114,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,125,125,103,101,116,32,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,125,103,101,116,32,109,105,110,105,109,117,109,66,97,108,97,110,99,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,99,104,97,110,103,101,67,117,114,114,101,110,99,121,40,99,117,114,114,101,110,99,121,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,110,32,111,119,110,101,114,34,41,59,116,104,105,115,46,35,99,117,114,114,101,110,99,121,61,99,117,114,114,101,110,99,121,125,104,97,115,40,118,97,108,105,100,97,116,111,114,41,123,114,101,116,117,114,110,32,66,111,111,108,101,97,110,40,118,111,105,100,32,48,33,61,61,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,41,125,35,105,115,65,108,108,111,119,101,100,40,97,100,100,114,101,115,115,41,123,105,102,40,109,115,103,46,115,101,110,100,101,114,33,61,61,97,100,100,114,101,115,115,38,38,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,115,101,110,100,101,114,32,105,115,32,110,111,116,32,116,104,101,32,118,97,108,105,100,97,116,111,114,32,111,114,32,111,119,110,101,114,34,41,59,114,101,116,117,114,110,33,48,125,97,115,121,110,99,32,97,100,100,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,41,123,105,102,40,116,104,105,115,46,35,105,115,65,108,108,111,119,101,100,40,118,97,108,105,100,97,116,111,114,41,44,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,108,114,101,97,100,121,32,97,32,118,97,108,105,100,97,116,111,114,34,41,59,99,111,110,115,116,32,98,97,108,97,110,99,101,61,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,116,104,105,115,46,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,118,97,108,105,100,97,116,111,114,93,41,59,105,102,40,98,97,108,97,110,99,101,60,116,104,105,115,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,98,97,108,97,110,99,101,32,116,111,32,108,111,119,33,32,103,111,116,58,32,36,123,98,97,108,97,110,99,101,125,32,110,101,101,100,58,32,36,123,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,96,41,59,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,43,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,61,123,102,105,114,115,116,83,101,101,110,58,68,97,116,101,46,110,111,119,40,41,44,108,97,115,116,83,101,101,110,58,68,97,116,101,46,110,111,119,40,41,44,97,99,116,105,118,101,58,33,48,125,125,114,101,109,111,118,101,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,41,123,105,102,40,116,104,105,115,46,35,105,115,65,108,108,111,119,101,100,40,118,97,108,105,100,97,116,111,114,41,44,33,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,86,97,108,105,100,97,116,111,114,115,45,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,38,38,40,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,45,61,49,41,44,100,101,108,101,116,101,32,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,125,97,115,121,110,99,32,117,112,100,97,116,101,86,97,108,105,100,97,116,111,114,40,118,97,108,105,100,97,116,111,114,44,97,99,116,105,118,101,41,123,105,102,40,116,104,105,115,46,35,105,115,65,108,108,111,119,101,100,40,118,97,108,105,100,97,116,111,114,41,44,33,116,104,105,115,46,104,97,115,40,118,97,108,105,100,97,116,111,114,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,34,41,59,99,111,110,115,116,32,98,97,108,97,110,99,101,61,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,116,104,105,115,46,99,117,114,114,101,110,99,121,44,34,98,97,108,97,110,99,101,79,102,34,44,91,118,97,108,105,100,97,116,111,114,93,41,59,105,102,40,98,97,108,97,110,99,101,60,116,104,105,115,46,109,105,110,105,109,117,109,66,97,108,97,110,99,101,38,38,97,99,116,105,118,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,98,97,108,97,110,99,101,32,116,111,32,108,111,119,33,32,103,111,116,58,32,36,123,98,97,108,97,110,99,101,125,32,110,101,101,100,58,32,36,123,116,104,105,115,46,35,109,105,110,105,109,117,109,66,97,108,97,110,99,101,125,96,41,59,105,102,40,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,61,61,61,97,99,116,105,118,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,108,114,101,97,100,121,32,34,43,40,97,99,116,105,118,101,63,34,97,99,116,105,118,97,116,101,100,34,58,34,100,101,97,99,116,105,118,97,116,101,100,34,41,41,59,97,99,116,105,118,101,63,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,43,61,49,58,116,104,105,115,46,35,97,99,116,105,118,101,86,97,108,105,100,97,116,111,114,115,45,61,49,44,116,104,105,115,46,35,118,97,108,105,100,97,116,111,114,115,91,118,97,108,105,100,97,116,111,114,93,46,97,99,116,105,118,101,61,97,99,116,105,118,101,125,125,114,101,116,117,114,110,32,86,97,108,105,100,97,116,111,114,115,59,63,91,34,73,72,78,89,50,71,81,72,51,67,52,52,82,82,85,68,77,52,88,76,71,54,55,75,72,72,83,51,85,69,82,55,76,54,78,85,76,68,72,72,54,65,67,86,79,65,86,84,76,65,71,66,82,76,66,53,66,83,74,34,93";
428
- var bytecodes = {
429
- contractFactory: contractFactory,
430
- nativeToken: nativeToken,
431
- nameService: nameService,
432
- validators: validators
433
- };
434
-
435
- const contractFactoryMessage = bytecodes.contractFactory;
436
- const nativeTokenMessage = bytecodes.nativeToken;
437
- const nameServiceMessage = bytecodes.nameService;
438
- const validatorsMessage = bytecodes.validators;
439
- const createContractMessage = async (creator, contract, constructorParameters = []) => {
440
- return new ContractMessage({
441
- creator,
442
- contract,
443
- constructorParameters
444
- });
445
- };
446
- const calculateFee = async (transaction, format = false) => {
447
- // excluded from fees
448
- if (transaction.to === validators$2)
449
- return 0;
450
- transaction = await new TransactionMessage(transaction);
451
- let fee = parseUnits(String(transaction.encoded.length));
452
- // fee per gb
453
- fee = fee.div(1073741824);
454
- // fee = fee.div(1000000)
455
- return format ? formatUnits(fee.toString()) : fee;
456
- };
457
- const createTransactionHash = async (transaction) => {
458
- const isRawTransactionMessage = transaction instanceof RawTransactionMessage;
459
- let message;
460
- if (!isRawTransactionMessage)
461
- message = await new RawTransactionMessage(transaction instanceof TransactionMessage ? transaction.decoded : transaction);
462
- else
463
- message = transaction;
464
- return (await message.peernetHash).digest;
465
- };
466
- const signTransaction = async (transaction, wallet) => {
467
- const signature = toBase58(await wallet.sign(await createTransactionHash(transaction)));
468
- return { ...transaction, signature };
469
- };
470
-
471
- const limit = 1800;
472
- const transactionLimit = 1800;
473
- const requestTimeout = 30000;
474
- const syncTimeout = 30000;
475
- class Protocol {
476
- constructor() {
477
- this.resolveTimeout = 10000;
478
- }
479
- get limit() {
480
- return limit;
481
- }
482
- get transactionLimit() {
483
- return transactionLimit;
484
- }
485
- get requestTimeout() {
486
- return requestTimeout;
487
- }
488
- get syncTimeout() {
489
- return syncTimeout;
490
- }
491
- }
492
-
493
- class Transaction extends Protocol {
494
- constructor() {
495
- super();
496
- }
497
- /**
498
- *
499
- * @param {Address[]} transactions
500
- * @returns transactions to include
501
- */
502
- async getTransactions(transactions) {
503
- return new Promise(async (resolve, reject) => {
504
- let size = 0;
505
- const _transactions = [];
506
- await Promise.all(transactions.map(async (tx) => {
507
- tx = await new TransactionMessage(tx);
508
- size += tx.encoded.length;
509
- if (!formatBytes(size).includes('MB') ||
510
- (formatBytes(size).includes('MB') && Number(formatBytes(size).split(' MB')[0]) <= 0.75))
511
- _transactions.push({ ...tx.decoded, hash: await tx.hash() });
512
- else
513
- resolve(_transactions);
514
- }));
515
- return resolve(_transactions);
516
- });
517
- }
518
- /**
519
- *
520
- * @param {Transaction[]} transactions An array containing Transactions
521
- * @returns {TransactionMessage}
522
- */
523
- async promiseTransactions(transactions) {
524
- transactions = await Promise.all(transactions.map((tx) => new TransactionMessage(tx.encoded || tx)));
525
- return transactions;
526
- }
527
- /**
528
- *
529
- * @param {Transaction[]} transactions An array containing Transactions
530
- * @returns {Object} {transaction.decoded, transaction.hash}
531
- */
532
- async promiseTransactionsContent(transactions) {
533
- transactions = await Promise.all(transactions.map((tx) => new Promise(async (resolve, reject) => {
534
- resolve({ ...tx.decoded, hash: await tx.hash() });
535
- })));
536
- return transactions;
537
- }
538
- /**
539
- * When a nonce isn't found for an address fallback to just checking the transactionnPoolStore
540
- * @param {Address} address
541
- * @returns {Number} nonce
542
- */
543
- async #getNonceFallback(address) {
544
- let transactions = await globalThis.transactionPoolStore.values();
545
- transactions = await this.promiseTransactions(transactions);
546
- transactions = transactions.filter((tx) => tx.decoded.from === address);
547
- transactions = await this.promiseTransactionsContent(transactions);
548
- // @ts-ignore
549
- if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
550
- // @ts-ignore
551
- let block = await peernet.get(this.lastBlock.hash, 'block');
552
- block = await new BlockMessage(block);
553
- // for (let tx of block.decoded?.transactions) {
554
- // tx = await peernet.get(tx, 'transaction')
555
- // transactions.push(new TransactionMessage(tx))
556
- // }
557
- transactions = transactions.filter((tx) => tx.from === address);
558
- while (transactions.length === 0 && block.decoded.index !== 0 && block.decoded.previousHash !== '0x0') {
559
- block = await globalThis.blockStore.get(block.decoded.previousHash);
560
- block = await new BlockMessage(block);
561
- transactions = block.decoded.transactions.filter((tx) => tx.from === address);
562
- }
563
- }
564
- if (transactions.length === 0)
565
- return 0;
566
- transactions = transactions.sort((a, b) => a.timestamp - b.timestamp);
567
- return transactions[transactions.length - 1].nonce;
568
- }
569
- /**
570
- * Get amount of transactions by address
571
- * @param {Address} address The address to get the nonce for
572
- * @returns {Number} nonce
573
- */
574
- async getNonce(address) {
575
- if (!(await globalThis.accountsStore.has(address))) {
576
- const nonce = await this.#getNonceFallback(address);
577
- await globalThis.accountsStore.put(address, new TextEncoder().encode(String(nonce)));
578
- }
579
- // todo: are those in the pool in cluded also ? they need to be included!!!
580
- let nonce = await globalThis.accountsStore.get(address);
581
- nonce = new TextDecoder().decode(nonce);
582
- let transactions = await globalThis.transactionPoolStore.values();
583
- transactions = await this.promiseTransactions(transactions);
584
- transactions = transactions.filter((tx) => tx.decoded.from === address);
585
- transactions = await this.promiseTransactionsContent(transactions);
586
- for (const transaction of transactions) {
587
- if (transaction.nonce > nonce)
588
- nonce = transaction.nonce;
589
- }
590
- return Number(nonce);
591
- }
592
- async validateNonce(address, nonce) {
593
- let previousNonce = await globalThis.accountsStore.get(address);
594
- previousNonce = Number(new TextDecoder().decode(previousNonce));
595
- if (previousNonce > nonce)
596
- throw new Error(`a transaction with a higher nonce already exists`);
597
- if (previousNonce === nonce)
598
- throw new Error(`a transaction with the same nonce already exists`);
599
- let transactions = await globalThis.transactionPoolStore.values();
600
- transactions = await this.promiseTransactions(transactions);
601
- transactions = transactions.filter((tx) => tx.decoded.from === address);
602
- for (const transaction of transactions) {
603
- if (transaction.decoded.nonce > nonce)
604
- throw new Error(`a transaction with a higher nonce already exists`);
605
- if (transaction.decoded.nonce === nonce)
606
- throw new Error(`a transaction with the same nonce already exists`);
607
- }
608
- }
609
- isTransactionMessage(message) {
610
- if (message instanceof TransactionMessage)
611
- return true;
612
- return false;
613
- }
614
- async createTransactionMessage(transaction, signature) {
615
- return new TransactionMessage({ ...transaction, signature });
616
- }
617
- async createTransaction(transaction) {
618
- return {
619
- from: transaction.from,
620
- to: transaction.to,
621
- method: transaction.method,
622
- params: transaction.params,
623
- timestamp: transaction.timestamp || Date.now(),
624
- nonce: transaction.nonce || (await this.getNonce(transaction.from)) + 1
625
- };
626
- }
627
- async sendTransaction(message) {
628
- if (!this.isTransactionMessage(message))
629
- message = await new TransactionMessage(message);
630
- if (!message.decoded.signature)
631
- throw new Error(`transaction not signed`);
632
- if (message.decoded.nonce === undefined)
633
- throw new Error(`nonce required`);
634
- await this.validateNonce(message.decoded.from, message.decoded.nonce);
635
- // todo check if signature is valid
636
- const hash = await message.hash();
637
- try {
638
- let data;
639
- const wait = new Promise(async (resolve, reject) => {
640
- if (pubsub.subscribers[`transaction.completed.${hash}`]) {
641
- const result = pubsub.subscribers[`transaction.completed.${hash}`].value;
642
- if (result.status !== 'fulfilled') {
643
- await transactionPoolStore.delete(hash);
644
- }
645
- result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
646
- }
647
- else {
648
- const completed = async (result) => {
649
- if (result.status !== 'fulfilled') {
650
- await transactionPoolStore.delete(hash);
651
- }
652
- result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
653
- setTimeout(async () => {
654
- pubsub.unsubscribe(`transaction.completed.${hash}`, completed);
655
- }, 10000);
656
- };
657
- pubsub.subscribe(`transaction.completed.${hash}`, completed);
658
- }
659
- });
660
- await globalThis.transactionPoolStore.put(hash, message.encoded);
661
- // debug(`Added ${hash} to the transaction pool`)
662
- peernet.publish('add-transaction', message.encoded);
663
- return { hash, data, fee: await calculateFee(message.decoded), wait, message };
664
- }
665
- catch (error) {
666
- console.log('remo');
667
- await transactionPoolStore.delete(hash);
668
- throw error;
669
- }
670
- }
671
- }
672
-
673
- /**
674
- * @extends {Transaction}
675
- */
676
- class Contract extends Transaction {
677
- constructor() {
678
- super();
679
- }
680
- async init() { }
681
- /**
682
- *
683
- * @param {Address} creator
684
- * @param {String} contract
685
- * @param {Array} constructorParameters
686
- * @returns lib.createContractMessage
687
- */
688
- async createContractMessage(creator, contract, constructorParameters = []) {
689
- return createContractMessage(creator, contract, constructorParameters);
690
- }
691
- /**
692
- *
693
- * @param {Address} creator
694
- * @param {String} contract
695
- * @param {Array} constructorParameters
696
- * @returns {Address}
697
- */
698
- async createContractAddress(creator, contract, constructorParameters = []) {
699
- contract = await this.createContractMessage(creator, contract, constructorParameters);
700
- return contract.hash();
701
- }
702
- /**
703
- *
704
- * @param {String} contract
705
- * @param {Array} parameters
706
- * @returns
707
- */
708
- async deployContract(signer, contract, constructorParameters = []) {
709
- const message = await createContractMessage(await signer.address, contract, constructorParameters);
710
- return this.deployContractMessage(signer, message);
711
- }
712
- async deployContractMessage(signer, message) {
713
- try {
714
- await globalThis.contractStore.put(await message.hash(), message.encoded);
715
- }
716
- catch (error) {
717
- throw error;
718
- }
719
- let transaction = {
720
- from: await signer.address,
721
- to: addresses.contractFactory,
722
- method: 'registerContract',
723
- params: [await message.hash()]
724
- };
725
- transaction = await signTransaction(await this.createTransaction(transaction), signer);
726
- return this.sendTransaction(transaction);
727
- }
728
- }
729
-
730
- const randombytes = (strength) => crypto.getRandomValues(new Uint8Array(strength));
731
-
732
- class EasyWorker {
733
- #messageEvent = 'message'
734
- #errorEvent = 'error'
735
- #isBrowser = false
736
- #isWorker = false
737
-
738
- get isWorker() {
739
- return this.#isWorker
740
- }
741
- constructor(url, options) {
742
- return this.#init(url, options)
743
- }
744
-
745
- #init(url, options = {}) {
746
- if (url) {
747
- if (globalThis.Worker) {
748
- this.#isBrowser = true;
749
- this.worker = new Worker(url, {...options});
750
- } else {
751
- return new Promise(async (resolve, reject) => {
752
- const {fork} = await import('child_process');
753
- this.worker = fork(url, ['easy-worker-child'], options);
754
- resolve(this);
755
- })
756
- }
757
- } else {
758
- this.#isWorker = true;
759
- if (globalThis.process?.argv[2] === 'easy-worker-child') {
760
- this.worker = process;
761
- } else {
762
- this.#isBrowser = true;
763
- this.worker = globalThis;
764
- }
765
- }
766
-
767
- return this
768
- }
769
-
770
- onmessage(fn) {
771
- if (this.#isBrowser) this.worker.onmessage = ({data}) => fn(data);
772
- else this.worker.on(this.#messageEvent, fn);
773
- }
774
-
775
- postMessage(message) {
776
- if (this.#isBrowser) this.worker.postMessage(message);
777
- else this.worker.send(message);
778
- }
779
-
780
- terminate() {
781
- if (this.#isBrowser) this.worker.terminate();
782
- else this.worker.kill();
783
- }
784
-
785
- onerror(fn) {
786
- if (this.#isBrowser) this.worker.onerror = fn;
787
- else this.worker.on(this.#errorEvent, fn);
788
- }
789
-
790
- /**
791
- *
792
- * @param {*} data
793
- * @returns {Promise} resolves result onmessage & rejects on error
794
- */
795
- once(data) {
796
- return new Promise((resolve, reject) => {
797
- this.onmessage(message => {
798
- resolve(message);
799
- this.terminate();
800
- });
801
- this.onerror(error => {
802
- reject(error);
803
- this.terminate();
804
- });
805
- this.postMessage(data);
806
- })
807
- }
808
- }
809
-
810
- class LeofcoinError extends Error {
811
- #message;
812
- constructor(message, options) {
813
- super(message, options);
814
- this.#message = message;
815
- }
816
- get message() {
817
- return `${this.name}: ${this.#message}`;
818
- }
819
- }
820
- class ResolveError extends LeofcoinError {
821
- name = 'ResolveError';
822
- }
823
- class ExecutionError extends LeofcoinError {
824
- name = 'ExecutionError';
825
- }
826
- class ContractDeploymentError extends LeofcoinError {
827
- name = 'ContractDeploymentError';
828
- }
829
- const isResolveError = (error) => error.name === 'ResolveError';
830
- const isExecutionError = (error) => error.name === 'ExecutionError';
831
-
832
- // import State from './state'
833
- class Machine {
834
- #contracts;
835
- #nonces;
836
- constructor(blocks) {
837
- this.#contracts = {};
838
- this.#nonces = {};
839
- this.lastBlock = { index: 0, hash: '0x0', previousHash: '0x0' };
840
- // @ts-ignore
841
- return this.#init(blocks);
842
- }
843
- #createMessage(sender = peernet.selectedAccount) {
844
- return {
845
- sender,
846
- call: this.execute,
847
- staticCall: this.get.bind(this)
848
- };
849
- }
850
- async #onmessage(data) {
851
- switch (data.type) {
852
- case 'contractError': {
853
- console.warn(`removing contract ${await data.hash()}`);
854
- // @ts-ignore
855
- await contractStore.delete(await data.hash());
856
- break;
857
- }
858
- case 'initError': {
859
- console.error(`init error: ${data.message}`);
860
- break;
861
- }
862
- case 'executionError': {
863
- // console.warn(`error executing transaction ${data.message}`);
864
- pubsub.publish(data.id, { error: data.message });
865
- break;
866
- }
867
- case 'debug': {
868
- for (const message of data.messages)
869
- globalThis.debug(message);
870
- break;
871
- }
872
- case 'machine-ready': {
873
- this.lastBlock = data.lastBlock;
874
- pubsub.publish('machine.ready', true);
875
- break;
876
- }
877
- case 'response': {
878
- pubsub.publish(data.id, data.value || false);
879
- break;
880
- }
881
- }
882
- }
883
- async #init(blocks) {
884
- return new Promise(async (resolve) => {
885
- const machineReady = () => {
886
- pubsub.unsubscribe('machine.ready', machineReady);
887
- resolve(this);
888
- };
889
- pubsub.subscribe('machine.ready', machineReady);
890
- let pre;
891
- try {
892
- const importee = await import('url');
893
- const url = importee.default;
894
- if (url)
895
- pre = url.fileURLToPath(new URL('.', import.meta.url));
896
- }
897
- catch {
898
- // browser env
899
- pre = './';
900
- }
901
- this.worker = await new EasyWorker(pre + 'workers/machine-worker.js', {
902
- serialization: 'advanced',
903
- type: 'module'
904
- });
905
- this.worker.onmessage(this.#onmessage.bind(this));
906
- // const blocks = await blockStore.values()
907
- const contracts = await Promise.all([
908
- globalThis.contractStore.get(contractFactory$2),
909
- globalThis.contractStore.get(nativeToken$2),
910
- globalThis.contractStore.get(validators$2),
911
- globalThis.contractStore.get(nameService$2)
912
- ]);
913
- const message = {
914
- type: 'init',
915
- input: {
916
- contracts,
917
- blocks,
918
- // @ts-ignore
919
- peerid: peernet.peerId
920
- }
921
- };
922
- this.worker.postMessage(message);
923
- });
924
- }
925
- async #runContract(contractMessage) {
926
- const hash = await contractMessage.hash();
927
- return new Promise((resolve, reject) => {
928
- // @ts-ignore
929
- const id = randombytes(20).toString('hex');
930
- const onmessage = (message) => {
931
- pubsub.unsubscribe(id, onmessage);
932
- if (message?.error)
933
- reject(message.error);
934
- else
935
- resolve(message);
936
- };
937
- pubsub.subscribe(id, onmessage);
938
- this.worker.postMessage({
939
- type: 'run',
940
- id,
941
- input: {
942
- decoded: contractMessage.decoded,
943
- encoded: contractMessage.encoded,
944
- hash
945
- }
946
- });
947
- });
948
- }
949
- /**
950
- *
951
- * @param {Address} contract
952
- * @param {String} method
953
- * @param {Array} parameters
954
- * @returns Promise<message>
955
- */
956
- async execute(contract, method, parameters) {
957
- try {
958
- if (contract === contractFactory$2 && method === 'registerContract') {
959
- if (await this.has(parameters[0]))
960
- throw new Error(`duplicate contract @${parameters[0]}`);
961
- let message;
962
- if (!(await globalThis.contractStore.has(parameters[0]))) {
963
- message = await peernet.get(parameters[0], 'contract');
964
- message = await new ContractMessage(message);
965
- await globalThis.contractStore.put(await message.hash(), message.encoded);
966
- }
967
- if (!message) {
968
- message = await globalThis.contractStore.get(parameters[0]);
969
- message = await new ContractMessage(message);
970
- }
971
- if (!(await this.has(await message.hash())))
972
- await this.#runContract(message);
973
- }
974
- }
975
- catch (error) {
976
- throw new ContractDeploymentError(`contract deployment failed for ${parameters[0]}\n${error.message}`);
977
- }
978
- return new Promise((resolve, reject) => {
979
- // @ts-ignore
980
- const id = randombytes(20).toString('hex');
981
- const onmessage = (message) => {
982
- pubsub.unsubscribe(id, onmessage);
983
- if (message?.error)
984
- reject(new ExecutionError(message.error));
985
- else
986
- resolve(message);
987
- };
988
- pubsub.subscribe(id, onmessage);
989
- this.worker.postMessage({
990
- type: 'execute',
991
- id,
992
- input: {
993
- contract,
994
- method,
995
- params: parameters
996
- }
997
- });
998
- });
999
- }
1000
- get(contract, method, parameters) {
1001
- return new Promise((resolve, reject) => {
1002
- const id = randombytes(20).toString();
1003
- const onmessage = (message) => {
1004
- pubsub.unsubscribe(id, onmessage);
1005
- resolve(message);
1006
- };
1007
- pubsub.subscribe(id, onmessage);
1008
- this.worker.postMessage({
1009
- type: 'get',
1010
- id,
1011
- input: {
1012
- contract,
1013
- method,
1014
- params: parameters
1015
- }
1016
- });
1017
- });
1018
- }
1019
- async has(address) {
1020
- return new Promise((resolve, reject) => {
1021
- // @ts-ignore
1022
- const id = randombytes(20).toString('hex');
1023
- const onmessage = (message) => {
1024
- pubsub.unsubscribe(id, onmessage);
1025
- if (message?.error)
1026
- reject(message.error);
1027
- else
1028
- resolve(message);
1029
- };
1030
- pubsub.subscribe(id, onmessage);
1031
- this.worker.postMessage({
1032
- type: 'has',
1033
- id,
1034
- input: {
1035
- address
1036
- }
1037
- });
1038
- });
1039
- }
1040
- async delete(hash) {
1041
- return globalThis.contractStore.delete(hash);
1042
- }
1043
- /**
1044
- *
1045
- * @returns Promise
1046
- */
1047
- async deleteAll() {
1048
- let hashes = await globalThis.contractStore.keys();
1049
- hashes = Object.keys(hashes).map((hash) => this.delete(hash));
1050
- return Promise.all(hashes);
1051
- }
1052
- }
1053
-
1054
- class Jobber {
1055
- constructor(timeout) {
1056
- this.busy = false;
1057
- this.timeout = timeout;
1058
- }
1059
- add(fn) {
1060
- this.busy = true;
1061
- return new Promise(async (resolve, reject) => {
1062
- const timeout = setTimeout(() => {
1063
- reject('timeout');
1064
- }, this.timeout);
1065
- this.destroy = () => {
1066
- clearTimeout(timeout);
1067
- this.busy = false;
1068
- resolve('stopped');
1069
- };
1070
- try {
1071
- const result = await fn();
1072
- clearTimeout(timeout);
1073
- this.busy = false;
1074
- resolve(result);
1075
- }
1076
- catch (error) {
1077
- clearTimeout(timeout);
1078
- reject(error);
1079
- }
1080
- });
1081
- }
1082
- }
1083
-
1084
- class State extends Contract {
1085
- #resolveErrored;
1086
- #lastResolvedTime;
1087
- #lastResolved;
1088
- #resolving;
1089
- #resolveErrorCount;
1090
- #syncState;
1091
- #chainState;
1092
- #lastBlockInQue;
1093
- #syncErrorCount;
1094
- #blockHashMap;
1095
- #chainSyncing;
1096
- #lastBlock;
1097
- #blocks;
1098
- #totalSize;
1099
- #machine;
1100
- #loaded;
1101
- get state() {
1102
- return {
1103
- sync: this.#syncState,
1104
- chain: this.#chainState
1105
- };
1106
- }
1107
- get blockHashMap() {
1108
- return this.#blockHashMap.entries();
1109
- }
1110
- get loaded() {
1111
- return this.#loaded;
1112
- }
1113
- get resolving() {
1114
- return this.#resolving;
1115
- }
1116
- /**
1117
- * amount the native token has been iteracted with
1118
- */
1119
- #nativeCalls;
1120
- /**
1121
- * amount the native token has been iteracted with
1122
- */
1123
- #nativeTransfers;
1124
- /**
1125
- * amount of native token burned
1126
- * {Number}
1127
- */
1128
- #nativeBurns;
1129
- /**
1130
- * amount of native tokens minted
1131
- * {Number}
1132
- */
1133
- #nativeMints;
1134
- /**
1135
- * total amount of transactions
1136
- * {Number}
1137
- */
1138
- #totalTransactions;
1139
- get nativeMints() {
1140
- return this.#nativeMints;
1141
- }
1142
- get nativeBurns() {
1143
- return this.#nativeBurns;
1144
- }
1145
- get nativeTransfers() {
1146
- return this.#nativeTransfers;
1147
- }
1148
- get totalTransactions() {
1149
- return this.#totalTransactions;
1150
- }
1151
- get nativeCalls() {
1152
- return this.#nativeCalls;
1153
- }
1154
- get blocks() {
1155
- return [...this.#blocks];
1156
- }
1157
- get lastBlock() {
1158
- return this.#lastBlock;
1159
- }
1160
- get totalSize() {
1161
- return this.#totalSize;
1162
- }
1163
- get machine() {
1164
- return this.#machine;
1165
- }
1166
- constructor() {
1167
- super();
1168
- this.#lastResolvedTime = 0;
1169
- this.#resolving = false;
1170
- this.#resolveErrorCount = 0;
1171
- this.#chainState = 'loading';
1172
- this.#syncErrorCount = 0;
1173
- this.#blockHashMap = new Map();
1174
- this.#chainSyncing = false;
1175
- this.#lastBlock = { index: 0, hash: '0x0', previousHash: '0x0' };
1176
- this.#blocks = [];
1177
- this.knownBlocks = [];
1178
- this.#totalSize = 0;
1179
- this.#loaded = false;
1180
- /**
1181
- * amount the native token has been iteracted with
1182
- */
1183
- this.#nativeCalls = 0;
1184
- /**
1185
- * amount the native token has been iteracted with
1186
- */
1187
- this.#nativeTransfers = 0;
1188
- /**
1189
- * amount of native token burned
1190
- * {Number}
1191
- */
1192
- this.#nativeBurns = 0;
1193
- /**
1194
- * amount of native tokens minted
1195
- * {Number}
1196
- */
1197
- this.#nativeMints = 0;
1198
- /**
1199
- * total amount of transactions
1200
- * {Number}
1201
- */
1202
- this.#totalTransactions = 0;
1203
- this.#chainStateHandler = () => {
1204
- return new globalThis.peernet.protos['peernet-response']({
1205
- response: this.#chainState
1206
- });
1207
- };
1208
- this.#lastBlockHandler = async () => {
1209
- return new globalThis.peernet.protos['peernet-response']({
1210
- response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index }
1211
- });
1212
- };
1213
- this.#knownBlocksHandler = async () => {
1214
- return new globalThis.peernet.protos['peernet-response']({
1215
- response: { blocks: this.#blocks.map((block) => block.hash) }
1216
- });
1217
- };
1218
- this.#loadBlockTransactions = (transactions) => Promise.all(transactions.map((transaction) => new TransactionMessage(transaction)));
1219
- this.#getLastTransactions = async () => {
1220
- let lastTransactions = (await Promise.all(this.#blocks
1221
- .filter((block) => block.loaded)
1222
- .slice(-128)
1223
- .map((block) => this.#loadBlockTransactions(block.transactions)))).reduce((all, transactions) => [...all, ...transactions], []);
1224
- return Promise.all(lastTransactions.map((transaction) => transaction.hash()));
1225
- };
1226
- }
1227
- async clearPool() {
1228
- await globalThis.transactionPoolStore.clear();
1229
- }
1230
- /**
1231
- * drastic measurement, removes everything!
1232
- */
1233
- async clearAll() {
1234
- await globalThis.accountsStore.clear();
1235
- await globalThis.chainStore.clear();
1236
- await globalThis.blockStore.clear();
1237
- await globalThis.transactionPoolStore.clear();
1238
- }
1239
- #chainStateHandler;
1240
- #lastBlockHandler;
1241
- #knownBlocksHandler;
1242
- async init() {
1243
- this.jobber = new Jobber(this.resolveTimeout);
1244
- if (super.init)
1245
- await super.init();
1246
- await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler);
1247
- await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler);
1248
- await globalThis.peernet.addRequestHandler('chainState', this.#chainStateHandler);
1249
- try {
1250
- let localBlock;
1251
- try {
1252
- localBlock = await globalThis.chainStore.get('lastBlock');
1253
- }
1254
- catch {
1255
- await globalThis.chainStore.put('lastBlock', '0x0');
1256
- localBlock = await globalThis.chainStore.get('lastBlock');
1257
- }
1258
- localBlock = new TextDecoder().decode(localBlock);
1259
- if (localBlock && localBlock !== '0x0') {
1260
- localBlock = await globalThis.peernet.get(localBlock, 'block');
1261
- localBlock = await new BlockMessage(localBlock);
1262
- this.#lastBlock = {
1263
- ...localBlock.decoded,
1264
- hash: await localBlock.hash()
1265
- };
1266
- }
1267
- else {
1268
- if (globalThis.peernet?.connections.length > 0) {
1269
- const latestBlock = await this.#getLatestBlock();
1270
- await this.#syncChain(latestBlock);
1271
- }
1272
- }
1273
- }
1274
- catch (error) {
1275
- console.log({ e: error });
1276
- console.log({ e: error });
1277
- }
1278
- globalThis.pubsub.publish('lastBlock', this.lastBlock);
1279
- // load local blocks
1280
- try {
1281
- this.knownBlocks = await blockStore.keys();
1282
- }
1283
- catch (error) {
1284
- console.error(error);
1285
- throw error;
1286
- }
1287
- try {
1288
- await this.resolveBlocks();
1289
- this.#machine = await new Machine(this.#blocks);
1290
- await this.#loadBlocks(this.#blocks);
1291
- }
1292
- catch (error) {
1293
- if (isResolveError(error)) {
1294
- console.error(error);
1295
- }
1296
- console.log(error);
1297
- }
1298
- }
1299
- async updateState(message) {
1300
- const hash = await message.hash();
1301
- this.#lastBlock = { hash, ...message.decoded };
1302
- // await this.state.updateState(message)
1303
- await globalThis.chainStore.put('lastBlock', hash);
1304
- globalThis.pubsub.publish('lastBlock', this.#lastBlock);
1305
- }
1306
- getLatestBlock() {
1307
- // @ts-ignore
1308
- return this.#getLatestBlock();
1309
- }
1310
- async getAndPutBlock(hash) {
1311
- // todo peernet resolves undefined blocks....
1312
- let block = await globalThis.peernet.get(hash, 'block');
1313
- if (block !== undefined) {
1314
- block = await new BlockMessage(block);
1315
- const { index } = block.decoded;
1316
- if (this.#blocks[index - 1] && this.#blocks[index - 1].hash !== block.hash)
1317
- throw `invalid block ${hash} @${index}`;
1318
- if (!(await globalThis.peernet.has(hash)))
1319
- await globalThis.peernet.put(hash, block.encoded, 'block');
1320
- }
1321
- return block;
1322
- }
1323
- async #resolveBlock(hash) {
1324
- let index = this.#blockHashMap.get(hash);
1325
- if (this.#blocks[index - 1]) {
1326
- if (this.#blocks[index - 1].previousHash !== '0x0') {
1327
- return this.resolveBlock(this.#blocks[index - 1].previousHash);
1328
- }
1329
- else {
1330
- return;
1331
- }
1332
- }
1333
- try {
1334
- const block = await this.getAndPutBlock(hash);
1335
- index = block.decoded.index;
1336
- const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength;
1337
- this.#totalSize += size;
1338
- this.#blocks[index - 1] = { hash, ...block.decoded };
1339
- this.#blockHashMap.set(hash, index);
1340
- globalThis.debug(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
1341
- globalThis.pubsub.publish('block-resolved', { hash, index });
1342
- this.#lastResolved = this.#blocks[index - 1];
1343
- this.#lastResolvedTime = Date.now();
1344
- }
1345
- catch (error) {
1346
- throw new ResolveError(`block: ${hash}@${index}`);
1347
- }
1348
- return;
1349
- }
1350
- async resolveBlock(hash) {
1351
- if (!hash)
1352
- throw new Error(`expected hash, got: ${hash}`);
1353
- if (hash === '0x0')
1354
- return;
1355
- if (this.#resolving)
1356
- return 'already resolving';
1357
- this.#resolving = true;
1358
- if (this.jobber.busy && this.jobber.destroy)
1359
- await this.jobber.destroy();
1360
- try {
1361
- await this.jobber.add(() => this.#resolveBlock(hash));
1362
- this.#resolving = false;
1363
- if (!this.#blockHashMap.has(this.#lastResolved.previousHash) && this.#lastResolved.previousHash !== '0x0')
1364
- return this.resolveBlock(this.#lastResolved.previousHash);
1365
- }
1366
- catch (error) {
1367
- console.log({ error });
1368
- this.#resolveErrorCount += 1;
1369
- this.#resolving = false;
1370
- if (this.#resolveErrorCount < 3)
1371
- return this.resolveBlock(hash);
1372
- this.#resolveErrorCount = 0;
1373
- throw new ResolveError(`block: ${hash}`, { cause: error });
1374
- }
1375
- }
1376
- async resolveBlocks() {
1377
- try {
1378
- if (this.jobber.busy && this.jobber.destroy) {
1379
- await this.jobber.destroy();
1380
- }
1381
- }
1382
- catch (error) {
1383
- console.error(error);
1384
- }
1385
- try {
1386
- const localBlock = await globalThis.chainStore.get('lastBlock');
1387
- const hash = new TextDecoder().decode(localBlock);
1388
- if (hash && hash !== '0x0') {
1389
- await this.resolveBlock(hash);
1390
- this.#lastBlock = this.#blocks[this.#blocks.length - 1];
1391
- }
1392
- }
1393
- catch (error) {
1394
- console.log(error);
1395
- this.#chainSyncing = false;
1396
- this.#syncState = 'errored';
1397
- this.#resolveErrored = true;
1398
- return this.restoreChain();
1399
- // console.log(e);
1400
- }
1401
- }
1402
- async restoreChain() {
1403
- try {
1404
- const { hash } = await this.#getLatestBlock();
1405
- await globalThis.chainStore.put('lastBlock', hash);
1406
- if (hash && hash !== '0x0') {
1407
- await this.resolveBlock(hash);
1408
- this.#lastBlock = this.#blocks[this.#blocks.length - 1];
1409
- }
1410
- }
1411
- catch (error) {
1412
- console.log(error);
1413
- this.#resolveErrored = true;
1414
- this.#resolveErrorCount += 1;
1415
- this.#resolving = false;
1416
- return this.restoreChain();
1417
- // console.log(e);
1418
- }
1419
- }
1420
- destroyResolveJob() { }
1421
- async syncChain(lastBlock) {
1422
- if (!this.shouldSync)
1423
- return;
1424
- this.#syncState = 'syncing';
1425
- this.#chainSyncing = true;
1426
- try {
1427
- if (this.jobber.busy && this.jobber.destroy) {
1428
- await this.jobber.destroy();
1429
- }
1430
- }
1431
- catch (error) {
1432
- console.error(error);
1433
- }
1434
- if (!lastBlock)
1435
- lastBlock = await this.#getLatestBlock();
1436
- console.log('starting sync');
1437
- if (globalThis.peernet.connections.length === 0)
1438
- return 'connectionless';
1439
- try {
1440
- await this.#syncChain(lastBlock);
1441
- }
1442
- catch (error) {
1443
- this.#syncErrorCount += 1;
1444
- if (this.#syncErrorCount < 3)
1445
- return this.syncChain(lastBlock);
1446
- this.#syncErrorCount = 0;
1447
- this.#chainSyncing = false;
1448
- this.#syncState = 'errored';
1449
- return this.#syncState;
1450
- }
1451
- if (lastBlock.index === this.#lastBlockInQue?.index)
1452
- this.#lastBlockInQue = undefined;
1453
- this.#syncErrorCount = 0;
1454
- this.#chainSyncing = false;
1455
- if (this.#lastBlockInQue)
1456
- return this.syncChain(this.#lastBlockInQue);
1457
- this.#syncState = 'synced';
1458
- return this.#syncState;
1459
- }
1460
- async #syncChain(lastBlock) {
1461
- try {
1462
- if (this.knownBlocks?.length === Number(lastBlock.index) + 1) {
1463
- let promises = [];
1464
- promises = await Promise.allSettled(this.knownBlocks.map(async (address) => {
1465
- const has = await globalThis.peernet.has(address);
1466
- return { has, address };
1467
- }));
1468
- promises = promises.filter(({ status, value }) => status === 'fulfilled' && !value.has);
1469
- await Promise.allSettled(promises.map(({ value }) => this.getAndPutBlock(value.address)));
1470
- }
1471
- if (!this.#lastBlock || Number(this.#lastBlock.index) < Number(lastBlock.index)) {
1472
- // TODO: check if valid
1473
- const localIndex = this.#lastBlock ? this.lastBlock.index : 0;
1474
- const index = lastBlock.index;
1475
- await this.resolveBlock(lastBlock.hash);
1476
- console.log('ok');
1477
- let blocksSynced = localIndex > 0 ? (localIndex > index ? localIndex - index : index + -localIndex) : index;
1478
- globalThis.debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
1479
- const start = this.#blocks.length - blocksSynced;
1480
- if (this.#machine)
1481
- await this.#loadBlocks(this.blocks.slice(start));
1482
- await this.updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
1483
- }
1484
- }
1485
- catch (error) {
1486
- console.log(error);
1487
- throw error;
1488
- }
1489
- }
1490
- async #getLatestBlock() {
1491
- let promises = [];
1492
- let data = await new globalThis.peernet.protos['peernet-request']({
1493
- request: 'lastBlock'
1494
- });
1495
- let node = await globalThis.peernet.prepareMessage(data);
1496
- for (const peer of globalThis.peernet?.connections) {
1497
- // @ts-ignore
1498
- if (peer.connected && peer.version === this.version) {
1499
- const task = async () => {
1500
- try {
1501
- const result = await peer.request(node.encoded);
1502
- return { result: Uint8Array.from(Object.values(result)), peer };
1503
- }
1504
- catch (error) {
1505
- throw error;
1506
- }
1507
- };
1508
- promises.push(task());
1509
- }
1510
- }
1511
- // @ts-ignore
1512
- promises = await this.promiseRequests(promises);
1513
- let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
1514
- promises = promises.sort((a, b) => b.index - a.index);
1515
- if (promises.length > 0)
1516
- latest = promises[0].value;
1517
- if (latest.hash && latest.hash !== '0x0') {
1518
- let message = await globalThis.peernet.get(latest.hash, 'block');
1519
- message = await new BlockMessage(message);
1520
- const hash = await message.hash();
1521
- if (hash !== latest.hash)
1522
- throw new Error('invalid block @getLatestBlock');
1523
- latest = { ...message.decoded, hash };
1524
- const peer = promises[0].peer;
1525
- if (peer.connected && peer.version === this.version) {
1526
- let data = await new globalThis.peernet.protos['peernet-request']({
1527
- request: 'knownBlocks'
1528
- });
1529
- let node = await globalThis.peernet.prepareMessage(data);
1530
- let message = await peer.request(node);
1531
- message = await new globalThis.peernet.protos['peernet-response'](message);
1532
- this.knownBlocks = message.decoded.response;
1533
- }
1534
- }
1535
- return latest;
1536
- }
1537
- #loadBlockTransactions;
1538
- #getLastTransactions;
1539
- /**
1540
- *
1541
- * @param {Block[]} blocks
1542
- */
1543
- async #loadBlocks(blocks) {
1544
- this.#chainState = 'loading';
1545
- let poolTransactionKeys = await globalThis.transactionPoolStore.keys();
1546
- for (const block of blocks) {
1547
- if (block && !block.loaded) {
1548
- if (block.index === 0)
1549
- this.#loaded = true;
1550
- const transactions = await this.#loadBlockTransactions([...block.transactions] || []);
1551
- const lastTransactions = await this.#getLastTransactions();
1552
- for (const transaction of transactions) {
1553
- const hash = await transaction.hash();
1554
- if (poolTransactionKeys.includes(hash))
1555
- await globalThis.transactionPoolStore.delete(hash);
1556
- if (lastTransactions.includes(hash)) {
1557
- console.log('removing invalid block');
1558
- await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
1559
- blocks.splice(block.index - 1, 1);
1560
- return this.#loadBlocks(blocks);
1561
- }
1562
- try {
1563
- await this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params);
1564
- await globalThis.accountsStore.put(transaction.decoded.from, String(transaction.decoded.nonce));
1565
- if (transaction.decoded.to === nativeToken$2) {
1566
- this.#nativeCalls += 1;
1567
- if (transaction.decoded.method === 'burn')
1568
- this.#nativeBurns += 1;
1569
- if (transaction.decoded.method === 'mint')
1570
- this.#nativeMints += 1;
1571
- if (transaction.decoded.method === 'transfer')
1572
- this.#nativeTransfers += 1;
1573
- }
1574
- this.#totalTransactions += 1;
1575
- }
1576
- catch (error) {
1577
- console.log(error);
1578
- await globalThis.transactionPoolStore.delete(hash);
1579
- console.log('removing invalid transaction');
1580
- if (isExecutionError(error)) {
1581
- console.log(`removing invalid block ${block.index}`);
1582
- await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
1583
- const deletedBlock = blocks.splice(block.index, 1);
1584
- console.log(`removed block ${deletedBlock[0].index}`);
1585
- return this.#loadBlocks(blocks);
1586
- }
1587
- console.log(error);
1588
- return false;
1589
- }
1590
- }
1591
- this.#blocks[block.index - 1].loaded = true;
1592
- // @ts-ignore
1593
- globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
1594
- globalThis.pubsub.publish('block-loaded', { ...block });
1595
- }
1596
- }
1597
- this.#chainState = 'loaded';
1598
- return true;
1599
- }
1600
- promiseRequests(promises) {
1601
- return new Promise(async (resolve, reject) => {
1602
- const timeout = setTimeout(() => {
1603
- resolve([{ index: 0, hash: '0x0' }]);
1604
- globalThis.debug('sync timed out');
1605
- }, this.requestTimeout);
1606
- promises = await Promise.allSettled(promises);
1607
- promises = promises.filter(({ status }) => status === 'fulfilled');
1608
- clearTimeout(timeout);
1609
- if (promises.length > 0) {
1610
- promises = promises.map(async ({ value }) => {
1611
- const node = await new globalThis.peernet.protos['peernet-response'](value.result);
1612
- return { value: node.decoded.response, peer: value.peer };
1613
- });
1614
- promises = await Promise.all(promises);
1615
- resolve(promises);
1616
- }
1617
- else {
1618
- resolve([]);
1619
- }
1620
- });
1621
- }
1622
- get canSync() {
1623
- if (this.#chainSyncing)
1624
- return false;
1625
- return true;
1626
- }
1627
- get shouldSync() {
1628
- if (this.#chainSyncing)
1629
- return false;
1630
- if (this.#resolveErrored ||
1631
- this.#syncState === 'errored' ||
1632
- this.#syncState === 'connectionless' ||
1633
- (!this.canSync && this.#lastResolvedTime + this.resolveTimeout > Date.now()))
1634
- return true;
1635
- return false;
1636
- }
1637
- async triggerSync() {
1638
- const latest = await this.#getLatestBlock();
1639
- return this.syncChain(latest);
1640
- }
1641
- async triggerLoad() {
1642
- if (this.#blocks.length > 0) {
1643
- this.#machine = await new Machine(this.#blocks);
1644
- }
1645
- }
1646
- }
1647
-
1648
- class VersionControl extends State {
1649
- constructor() {
1650
- super();
1651
- }
1652
- async init() {
1653
- super.init && (await super.init());
1654
- console.log('init');
1655
- try {
1656
- const version = await globalThis.chainStore.get('version');
1657
- this.version = new TextDecoder().decode(version);
1658
- console.log(this.version);
1659
- /**
1660
- * protocol version control!
1661
- * note v1 and 1.1 delete everything because of big changes, this is not what we want in the future
1662
- * in the future we want newer nodes to handle the new changes and still confirm old version transactions
1663
- * unless there is a security issue!
1664
- */
1665
- if (this.version !== '1.1.1') {
1666
- this.version = '1.1.1';
1667
- await this.clearAll();
1668
- await globalThis.chainStore.put('version', this.version);
1669
- }
1670
- // if (version)
1671
- }
1672
- catch (e) {
1673
- console.log(e);
1674
- this.version = '1.1.1';
1675
- await this.clearAll();
1676
- await globalThis.chainStore.put('version', this.version);
1677
- }
1678
- }
1679
- }
1680
-
1681
- globalThis.BigNumber = BigNumber;
1682
- const ignorelist = [];
1683
- // check if browser or local
1684
- class Chain extends VersionControl {
1685
- #state;
1686
- #slotTime;
1687
- /** {Address[]} */
1688
- #validators;
1689
- /** {Boolean} */
1690
- #runningEpoch;
1691
- #participants;
1692
- #participating;
1693
- #jail;
1694
- constructor() {
1695
- super();
1696
- this.#slotTime = 10000;
1697
- this.utils = {};
1698
- /** {Address[]} */
1699
- this.#validators = [];
1700
- /** {Boolean} */
1701
- this.#runningEpoch = false;
1702
- this.#participants = [];
1703
- this.#participating = false;
1704
- this.#jail = [];
1705
- // @ts-ignore
1706
- return this.#init();
1707
- }
1708
- get nativeToken() {
1709
- return addresses.nativeToken;
1710
- }
1711
- get validators() {
1712
- return [...this.#validators];
1713
- }
1714
- async hasTransactionToHandle() {
1715
- const size = await globalThis.transactionPoolStore.size();
1716
- if (size > 0)
1717
- return true;
1718
- return false;
1719
- }
1720
- async #runEpoch() {
1721
- this.#runningEpoch = true;
1722
- console.log('epoch');
1723
- const validators = await this.staticCall(addresses.validators, 'validators');
1724
- console.log({ validators });
1725
- if (!validators[peernet.selectedAccount]?.active)
1726
- return;
1727
- const start = Date.now();
1728
- try {
1729
- await this.#createBlock();
1730
- }
1731
- catch (error) {
1732
- console.error(error);
1733
- }
1734
- const end = Date.now();
1735
- console.log((end - start) / 1000 + ' s');
1736
- if (await this.hasTransactionToHandle())
1737
- return this.#runEpoch();
1738
- this.#runningEpoch = false;
1739
- // if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
1740
- }
1741
- async #setup() {
1742
- const contracts = [
1743
- {
1744
- address: addresses.contractFactory,
1745
- message: contractFactoryMessage
1746
- },
1747
- {
1748
- address: addresses.nativeToken,
1749
- message: nativeTokenMessage
1750
- },
1751
- {
1752
- address: addresses.validators,
1753
- message: validatorsMessage
1754
- },
1755
- {
1756
- address: addresses.nameService,
1757
- message: nameServiceMessage
1758
- }
1759
- ];
1760
- await Promise.all(contracts.map(async ({ address, message }) => {
1761
- // @ts-ignore
1762
- message = await new ContractMessage(Uint8Array.from(message.split(',').map((string) => Number(string))));
1763
- // @ts-ignore
1764
- await globalThis.contractStore.put(address, message.encoded);
1765
- }));
1766
- console.log('handle native contracts');
1767
- // handle native contracts
1768
- }
1769
- async #init() {
1770
- // this.node = await new Node()
1771
- this.#participants = [];
1772
- this.#participating = false;
1773
- const initialized = await globalThis.contractStore.has(addresses.contractFactory);
1774
- if (!initialized)
1775
- await this.#setup();
1776
- this.utils = { BigNumber, formatUnits, parseUnits };
1777
- // this.#state = new State()
1778
- // todo some functions rely on state
1779
- await super.init();
1780
- await globalThis.peernet.addRequestHandler('bw-request-message', () => {
1781
- return new BWMessage(globalThis.peernet.client.bw) || { up: 0, down: 0 };
1782
- });
1783
- // await globalThis.peernet.addRequestHandler('peerId', () => {
1784
- // let node =
1785
- // globalThis.peernet.protos['peernet-response']({response: node.encoded})
1786
- // })
1787
- await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
1788
- await globalThis.peernet.addRequestHandler('version', this.#versionHandler.bind(this));
1789
- globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
1790
- globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
1791
- globalThis.peernet.subscribe('send-transaction', this.#sendTransaction.bind(this));
1792
- globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
1793
- globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
1794
- globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
1795
- globalThis.pubsub.publish('chain:ready', true);
1796
- return this;
1797
- }
1798
- async #invalidTransaction(hash) {
1799
- await globalThis.transactionPoolStore.delete(hash);
1800
- console.log(`removed invalid transaction: ${hash}`);
1801
- }
1802
- async #validatorTimeout(validatorInfo) {
1803
- setTimeout(() => {
1804
- this.#jail.splice(this.#jail.indexOf(validatorInfo.address), 1);
1805
- }, validatorInfo.timeout);
1806
- this.#jail.push(validatorInfo.address);
1807
- }
1808
- #addTransaction(message) {
1809
- console.log({ message });
1810
- }
1811
- async #prepareRequest(request) {
1812
- let node = await new globalThis.peernet.protos['peernet-request']({ request });
1813
- return globalThis.peernet.prepareMessage(node);
1814
- }
1815
- async #makeRequest(peer, request) {
1816
- const node = await this.#prepareRequest(request);
1817
- let response = await peer.request(node.encoded);
1818
- response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
1819
- return response.decoded.response;
1820
- }
1821
- async getPeerTransactionPool(peer) {
1822
- const transactionsInPool = await this.#makeRequest(peer, 'transactionPool');
1823
- // todo iterate vs getting all keys?
1824
- const transactions = await globalThis.transactionPoolStore.keys();
1825
- const transactionsToGet = [];
1826
- for (const key of transactionsInPool) {
1827
- !transactions.includes(key) &&
1828
- !ignorelist.includes(key) &&
1829
- transactionsToGet.push(transactionPoolStore.put(key, await peernet.get(key, 'transaction')));
1830
- }
1831
- return Promise.all(transactionsToGet);
1832
- }
1833
- async #peerConnected(peer) {
1834
- // todo handle version changes
1835
- // for now just do nothing if version doesn't match
1836
- if (!peer.version || peer.version !== this.version)
1837
- return;
1838
- const lastBlock = await this.#makeRequest(peer, 'lastBlock');
1839
- const higherThenCurrentLocal = lastBlock.index > this.lastBlock?.index;
1840
- const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
1841
- if (Object.keys(lastBlock).length > 0) {
1842
- if (!this.lastBlock || higherThenCurrentLocal) {
1843
- this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
1844
- await this.syncChain(lastBlock);
1845
- }
1846
- else if (!this.knownBlocks)
1847
- this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
1848
- }
1849
- if (this.#participating && peerTransactionPool.length > 0)
1850
- return this.#runEpoch();
1851
- }
1852
- #epochTimeout;
1853
- async #transactionPoolHandler() {
1854
- const pool = await globalThis.transactionPoolStore.keys();
1855
- return new globalThis.peernet.protos['peernet-response']({ response: pool });
1856
- }
1857
- async #versionHandler() {
1858
- return new globalThis.peernet.protos['peernet-response']({ response: { version: this.version } });
1859
- }
1860
- async #executeTransaction({ hash, from, to, method, params, nonce }) {
1861
- try {
1862
- let result = await this.machine.execute(to, method, params);
1863
- // await accountsStore.put(to, nonce)
1864
- await transactionPoolStore.delete(hash);
1865
- // if (!result) result = this.machine.state
1866
- globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fulfilled', hash });
1867
- return result || 'no state change';
1868
- }
1869
- catch (error) {
1870
- await transactionPoolStore.delete(hash);
1871
- globalThis.peernet.publish('invalid-transaction', hash);
1872
- globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
1873
- throw { error, hash, from, to, params, nonce };
1874
- }
1875
- }
1876
- async #addBlock(block) {
1877
- const blockMessage = await new BlockMessage(block);
1878
- await Promise.all(blockMessage.decoded.transactions
1879
- // @ts-ignore
1880
- .map(async (transaction) => transactionPoolStore.delete(transaction.hash)));
1881
- const hash = await blockMessage.hash();
1882
- await globalThis.blockStore.put(hash, blockMessage.encoded);
1883
- if (this.lastBlock.index < Number(blockMessage.decoded.index))
1884
- await this.updateState(blockMessage);
1885
- globalThis.debug(`added block: ${hash}`);
1886
- let promises = [];
1887
- let contracts = [];
1888
- for (let transaction of blockMessage.decoded.transactions) {
1889
- // await transactionStore.put(transaction.hash, transaction.encoded)
1890
- // @ts-ignore
1891
- const index = contracts.indexOf(transaction.to);
1892
- // @ts-ignore
1893
- if (index === -1)
1894
- contracts.push(transaction.to);
1895
- // Todo: go trough all accounts
1896
- // @ts-ignore
1897
- promises.push(this.#executeTransaction(transaction));
1898
- }
1899
- try {
1900
- promises = await Promise.allSettled(promises);
1901
- for (let transaction of blockMessage.decoded.transactions) {
1902
- globalThis.pubsub.publish('transaction-processed', transaction);
1903
- if (transaction.to === globalThis.peernet.selectedAccount)
1904
- globalThis.pubsub.publish('account-transaction-processed', transaction);
1905
- await globalThis.accountsStore.put(transaction.from, String(transaction.nonce));
1906
- }
1907
- // todo finish state
1908
- // for (const contract of contracts) {
1909
- // const state = await this.machine.get(contract, 'state')
1910
- // // await stateStore.put(contract, state)
1911
- // console.log(state);
1912
- // }
1913
- globalThis.pubsub.publish('block-processed', blockMessage.decoded);
1914
- }
1915
- catch (error) {
1916
- console.log(error.hash);
1917
- console.log('errrrr');
1918
- await transactionPoolStore.delete(error.hash);
1919
- }
1920
- }
1921
- async participate(address) {
1922
- // TODO: validate participant
1923
- // hold min amount of 50k ART for 7 days
1924
- // lock the 50k
1925
- // introduce peer-reputation
1926
- // peerReputation(peerId)
1927
- // {bandwith: {up, down}, uptime}
1928
- this.#participating = true;
1929
- if (!(await this.staticCall(addresses.validators, 'has', [address]))) {
1930
- const rawTransaction = {
1931
- from: address,
1932
- to: addresses.validators,
1933
- method: 'addValidator',
1934
- params: [address],
1935
- nonce: (await this.getNonce(address)) + 1,
1936
- timestamp: Date.now()
1937
- };
1938
- const transaction = await signTransaction(rawTransaction, globalThis.peernet.identity);
1939
- await this.sendTransaction(transaction);
1940
- }
1941
- if ((await this.hasTransactionToHandle()) && !this.#runningEpoch && this.#participating)
1942
- await this.#runEpoch();
1943
- }
1944
- // todo filter tx that need to wait on prev nonce
1945
- async #createBlock(limit = this.transactionLimit) {
1946
- // vote for transactions
1947
- if ((await globalThis.transactionPoolStore.size()) === 0)
1948
- return;
1949
- let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
1950
- for (const hash of await globalThis.transactionPoolStore.keys()) {
1951
- if (ignorelist.includes(hash))
1952
- await globalThis.transactionPoolStore.delete(hash);
1953
- }
1954
- if (Object.keys(transactions)?.length === 0)
1955
- return;
1956
- const timestamp = Date.now();
1957
- let block = {
1958
- transactions: [],
1959
- validators: [],
1960
- fees: BigNumber.from(0),
1961
- timestamp,
1962
- previousHash: '',
1963
- reward: parseUnits('150'),
1964
- index: 0
1965
- };
1966
- // exclude failing tx
1967
- transactions = await this.promiseTransactions(transactions);
1968
- transactions = transactions.sort((a, b) => a.nonce - b.nonce);
1969
- for (let transaction of transactions) {
1970
- const hash = await transaction.hash();
1971
- const doubleTransactions = [];
1972
- for (const block of this.blocks) {
1973
- for (const transaction of block.transactions) {
1974
- if (transaction.hash === hash) {
1975
- doubleTransactions.push(hash);
1976
- }
1977
- }
1978
- }
1979
- if (doubleTransactions.length > 0) {
1980
- await globalThis.transactionPoolStore.delete(hash);
1981
- await globalThis.peernet.publish('invalid-transaction', hash);
1982
- return;
1983
- }
1984
- // if (timestamp + this.#slotTime > Date.now()) {
1985
- try {
1986
- const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1987
- block.transactions.push(transaction);
1988
- block.fees = block.fees.add(await calculateFee(transaction.decoded));
1989
- await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1990
- }
1991
- catch (e) {
1992
- console.log('vvvvvv');
1993
- console.log({ e });
1994
- console.log(hash);
1995
- peernet.publish('invalid-transaction', hash);
1996
- console.log(await globalThis.transactionPoolStore.keys());
1997
- console.log(await globalThis.transactionPoolStore.has(e.hash));
1998
- await globalThis.transactionPoolStore.delete(e.hash);
1999
- console.log(await globalThis.transactionPoolStore.has(e.hash));
2000
- }
2001
- }
2002
- // don't add empty block
2003
- if (block.transactions.length === 0)
2004
- return;
2005
- const validators = await this.staticCall(addresses.validators, 'validators');
2006
- // block.validators = Object.keys(block.validators).reduce((set, key) => {
2007
- // if (block.validators[key].active) {
2008
- // push({
2009
- // address: key
2010
- // })
2011
- // }
2012
- // }, [])
2013
- const peers = {};
2014
- for (const entry of globalThis.peernet.peerEntries) {
2015
- peers[entry[0]] = entry[1];
2016
- }
2017
- for (const validator of Object.keys(validators)) {
2018
- if (validators[validator].active) {
2019
- const peer = peers[validator];
2020
- if (peer && peer.connected && peer.version === this.version) {
2021
- let data = await new BWRequestMessage();
2022
- const node = await globalThis.peernet.prepareMessage(data.encoded);
2023
- try {
2024
- const bw = await peer.request(node.encoded);
2025
- block.validators.push({
2026
- address: validator,
2027
- bw: bw.up + bw.down
2028
- });
2029
- }
2030
- catch { }
2031
- }
2032
- else if (globalThis.peernet.selectedAccount === validator) {
2033
- block.validators.push({
2034
- address: globalThis.peernet.selectedAccount,
2035
- bw: globalThis.peernet.bw.up + globalThis.peernet.bw.down
2036
- });
2037
- }
2038
- }
2039
- }
2040
- block.validators = block.validators.map((validator) => {
2041
- validator.reward = block.fees;
2042
- validator.reward = validator.reward.add(block.reward);
2043
- validator.reward = validator.reward.div(block.validators.length);
2044
- delete validator.bw;
2045
- return validator;
2046
- });
2047
- // block.validators = calculateValidatorReward(block.validators, block.fees)
2048
- block.index = this.lastBlock?.index;
2049
- if (block.index === undefined)
2050
- block.index = 0;
2051
- else
2052
- block.index += 1;
2053
- block.previousHash = this.lastBlock?.hash || '0x0';
2054
- // block.timestamp = Date.now()
2055
- // block.reward = block.reward.toString()
2056
- // block.fees = block.fees.toString()
2057
- try {
2058
- block.transactions = await Promise.all(block.transactions.map(async (transaction) => {
2059
- await globalThis.transactionPoolStore.delete(await transaction.hash());
2060
- return transaction.decoded;
2061
- }));
2062
- let blockMessage = await new BlockMessage(block);
2063
- const hash = await blockMessage.hash();
2064
- await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
2065
- await this.updateState(blockMessage);
2066
- globalThis.debug(`created block: ${hash}`);
2067
- globalThis.peernet.publish('add-block', blockMessage.encoded);
2068
- globalThis.pubsub.publish('add-block', blockMessage.decoded);
2069
- }
2070
- catch (error) {
2071
- console.log(error);
2072
- console.log('eeeee');
2073
- throw new Error(`invalid block ${block}`);
2074
- }
2075
- // data = await this.machine.execute(to, method, params)
2076
- // transactionStore.put(message.hash, message.encoded)
2077
- }
2078
- async #sendTransaction(transaction) {
2079
- transaction = await new TransactionMessage(transaction.encoded || transaction);
2080
- const hash = await transaction.hash();
2081
- try {
2082
- const has = await globalThis.transactionPoolStore.has(hash);
2083
- if (!has) {
2084
- await globalThis.transactionPoolStore.put(hash, transaction.encoded);
2085
- }
2086
- if (this.#participating && !this.#runningEpoch)
2087
- this.#runEpoch();
2088
- }
2089
- catch (e) {
2090
- console.log(e);
2091
- console.log('rrrrr');
2092
- globalThis.peernet.publish('invalid-transaction', hash);
2093
- throw new Error('invalid transaction');
2094
- }
2095
- }
2096
- /**
2097
- * every tx done is trough contracts so no need for amount
2098
- * data is undefined when nothing is returned
2099
- * error is thrown on error so undefined data doesn't mean there is an error...
2100
- **/
2101
- async sendTransaction(transaction) {
2102
- const transactionMessage = await new TransactionMessage({ ...transaction });
2103
- const event = await super.sendTransaction(transactionMessage);
2104
- this.#sendTransaction(transactionMessage.encoded);
2105
- globalThis.peernet.publish('send-transaction', transactionMessage.encoded);
2106
- return event;
2107
- }
2108
- async addContract(transaction, contractMessage) {
2109
- const hash = await contractMessage.hash();
2110
- const has = await this.staticCall(addresses.contractFactory, 'isRegistered', [hash]);
2111
- if (has)
2112
- throw new Error('contract exists');
2113
- const tx = await this.sendTransaction(transaction);
2114
- await tx.wait;
2115
- return tx;
2116
- }
2117
- /**
2118
- *
2119
- * @param {Address} sender
2120
- * @returns {globalMessage}
2121
- */
2122
- #createMessage(sender = globalThis.peernet.selectedAccount) {
2123
- return {
2124
- sender,
2125
- call: this.call,
2126
- staticCall: this.staticCall
2127
- };
2128
- }
2129
- /**
2130
- *
2131
- * @param {Address} sender
2132
- * @param {Address} contract
2133
- * @param {String} method
2134
- * @param {Array} parameters
2135
- * @returns
2136
- */
2137
- internalCall(sender, contract, method, parameters) {
2138
- globalThis.msg = this.#createMessage(sender);
2139
- return this.machine.execute(contract, method, parameters);
2140
- }
2141
- /**
2142
- *
2143
- * @param {Address} contract
2144
- * @param {String} method
2145
- * @param {Array} parameters
2146
- * @returns
2147
- */
2148
- call(contract, method, parameters) {
2149
- globalThis.msg = this.#createMessage();
2150
- return this.machine.execute(contract, method, parameters);
2151
- }
2152
- staticCall(contract, method, parameters) {
2153
- globalThis.msg = this.#createMessage();
2154
- return this.machine.get(contract, method, parameters);
2155
- }
2156
- mint(to, amount) {
2157
- return this.call(addresses.nativeToken, 'mint', [to, amount]);
2158
- }
2159
- transfer(from, to, amount) {
2160
- return this.call(addresses.nativeToken, 'transfer', [from, to, amount]);
2161
- }
2162
- get balances() {
2163
- return this.staticCall(addresses.nativeToken, 'balances');
2164
- }
2165
- get contracts() {
2166
- return this.staticCall(addresses.contractFactory, 'contracts');
2167
- }
2168
- deleteAll() {
2169
- return this.machine.deleteAll();
2170
- }
2171
- /**
2172
- * lookup an address for a registered name using the builtin nameService
2173
- * @check nameService
2174
- *
2175
- * @param {String} - contractName
2176
- * @returns {String} - address
2177
- *
2178
- * @example chain.lookup('myCoolContractName') // qmqsfddfdgfg...
2179
- */
2180
- lookup(name) {
2181
- return this.call(addresses.nameService, 'lookup', [name]);
2182
- }
2183
- }
2184
-
2185
- export { Chain as default };