@leofcoin/chain 1.1.11 → 1.1.13

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.
Files changed (35) hide show
  1. package/{dist/workers/block-worker.js → block-worker.js} +0 -0
  2. package/demo/chain.browser.js +50 -51
  3. package/demo/node.browser.js +6 -13
  4. package/demo/pako.browser.js +670 -501
  5. package/demo/peernet-swarm.browser.js +8 -12
  6. package/demo/workers/block-worker.js +1 -2
  7. package/demo/workers/machine-worker.js +2 -4
  8. package/demo/workers/pool-worker.js +1 -2
  9. package/demo/workers/transaction-worker.js +1 -2
  10. package/dist/browser/workers/block-worker.js +1 -2
  11. package/dist/browser/workers/machine-worker.js +2 -4
  12. package/dist/browser/workers/pool-worker.js +1 -2
  13. package/dist/browser/workers/transaction-worker.js +1 -2
  14. package/dist/chain.browser.js +50 -51
  15. package/dist/chain.js +49 -49
  16. package/dist/module/chain.js +49 -49
  17. package/dist/module/workers/machine-worker.js +2 -2
  18. package/dist/node.browser.js +6 -13
  19. package/dist/pako.browser.js +670 -501
  20. package/dist/peernet-swarm.browser.js +8 -12
  21. package/dist/workers/machine-worker.js +1 -1
  22. package/package.json +4 -2
  23. package/rollup.config.js +1 -1
  24. package/src/chain.js +42 -46
  25. package/src/machine.js +6 -2
  26. package/test/chain.js +33 -18
  27. package/demo/865.machine-worker.js +0 -10
  28. package/demo/chain.js +0 -1209
  29. package/demo/machine-worker.js +0 -17624
  30. package/demo/node.js +0 -1
  31. package/demo/workers/workers/865.js +0 -10
  32. package/demo/workers/workers/block-worker.js +0 -13200
  33. package/demo/workers/workers/machine-worker.js +0 -13904
  34. package/demo/workers/workers/pool-worker.js +0 -8504
  35. package/demo/workers/workers/transaction-worker.js +0 -8496
package/demo/chain.js DELETED
@@ -1,1209 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('@leofcoin/utils');
4
- var _BN = require('bn.js');
5
- require('@ethersproject/bytes');
6
- var logger = require('@ethersproject/logger');
7
- require('@ethersproject/bignumber');
8
- var crypto = require('crypto');
9
- var path = require('path');
10
- var EasyWorker = require('@vandeurenglenn/easy-worker');
11
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
- var MultiWallet = require('@leofcoin/multi-wallet');
13
- var index = require('@leofcoin/codec-format-interface/dist/index');
14
- var bs32 = require('@vandeurenglenn/base32');
15
-
16
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
-
18
- var _BN__default = /*#__PURE__*/_interopDefaultLegacy(_BN);
19
- var EasyWorker__default = /*#__PURE__*/_interopDefaultLegacy(EasyWorker);
20
- var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
21
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
22
-
23
- var contractFactory$2 = "ihny2gqgorwofhvfgtr5dm2targnxrul5rr4wnyk4vl7akbmwox43nlvpim";
24
- var nativeToken$2 = "ihny2gqhe7gazkieey4o3rztfy757ia3as6snrmov4e3vkfdvt2olvx3ste";
25
- var nameService$2 = "ihny2gqgztuego4qkbcaqp5ttunly3j3f5vehe65txord7r7osv7xrqxbl6";
26
- var validators$2 = "ihny2gqhlmubrvkl3ixyfadompvfiinvwthi43wpt4qdfj767nhpyjlnwin";
27
- var addresses = {
28
- contractFactory: contractFactory$2,
29
- nativeToken: nativeToken$2,
30
- nameService: nameService$2,
31
- validators: validators$2
32
- };
33
-
34
- const contractFactory$1 = addresses.contractFactory;
35
- const nameService$1 = addresses.nameService;
36
- const nativeToken$1 = addresses.nativeToken;
37
- const validators$1 = addresses.validators;
38
-
39
- const version$1 = "bignumber/5.6.2";
40
-
41
- _BN__default["default"].BN;
42
- new logger.Logger(version$1);
43
-
44
- const version = "units/5.6.1";
45
-
46
- new logger.Logger(version);
47
-
48
- const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
49
-
50
- const formatBytes = (bytes, decimals = 2) => {
51
- if (bytes === 0) return '0 Bytes';
52
- if (decimals < 0) decimals = 0;
53
-
54
- const k = 1024;
55
- const i = Math.floor(Math.log(bytes) / Math.log(k));
56
-
57
- return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
58
- };
59
-
60
- // import State from './state'
61
-
62
- class Machine {
63
- #contracts = {}
64
- #nonces = {}
65
- lastBlock = {}
66
-
67
- constructor() {
68
- return this.#init()
69
- }
70
-
71
- #createMessage(sender = peernet.id) {
72
- return {
73
- sender,
74
- call: this.execute,
75
- staticCall: this.get.bind(this)
76
- }
77
- }
78
-
79
- async #onmessage(data) {
80
- switch (data.type) {
81
- case 'contractError':
82
- console.warn(`removing contract ${await data.hash}`);
83
- await contractStore.delete(await data.hash);
84
- break
85
-
86
- case 'executionError':
87
- // console.warn(`error executing transaction ${data.message}`);
88
- pubsub.publish(data.id, {error: data.message});
89
- break
90
-
91
- case 'debug':
92
- data.messages.forEach(message => debug(message));
93
- break
94
- case 'machine-ready':
95
- this.lastBlock = data.lastBlock;
96
- pubsub.publish('machine.ready');
97
- break
98
- case 'response':
99
- pubsub.publish(data.id, data.value);
100
- break
101
- }
102
-
103
- }
104
-
105
- async #init() {
106
- return new Promise(async (resolve) => {
107
- pubsub.subscribe('machine.ready', () => {
108
- resolve(this);
109
- });
110
-
111
- this.worker = await new EasyWorker__default["default"](path.join(__dirname, './workers/machine-worker.js'), {serialization: 'advanced', type:'module'});
112
- this.worker.onmessage(this.#onmessage.bind(this));
113
-
114
- const blocks = await blockStore.values();
115
- const contracts = await Promise.all([
116
- contractStore.get(contractFactory$1),
117
- contractStore.get(nativeToken$1),
118
- contractStore.get(validators$1),
119
- contractStore.get(nameService$1)
120
- ]);
121
-
122
- const message = {
123
- type: 'init',
124
- input: {
125
- contracts,
126
- blocks,
127
- peerid: peernet.peerId
128
- }
129
- };
130
- this.worker.postMessage(message);
131
- })
132
-
133
- }
134
-
135
- async #runContract(contractMessage) {
136
- const params = contractMessage.decoded.constructorParameters;
137
- try {
138
-
139
- const func = new Function(contractMessage.decoded.contract);
140
- const Contract = func();
141
-
142
- globalThis.msg = this.#createMessage(contractMessage.decoded.creator);
143
- // globalThis.msg = {sender: contractMessage.decoded.creator}
144
- this.#contracts[await contractMessage.hash] = await new Contract(...params);
145
- debug(`loaded contract: ${await contractMessage.hash}`);
146
- debug(`size: ${formatBytes(contractMessage.encoded.length)}`);
147
- } catch (e) {
148
- console.log(e);
149
- console.warn(`removing contract ${await contractMessage.hash}`);
150
- await contractStore.delete(await contractMessage.hash, contractMessage.encoded);
151
- }
152
- }
153
- /**
154
- * @params {ContractMessage} - contractMessage
155
- */
156
- async addContract(contractMessage) {
157
- if (!await contractStore.has(contractMessage.hash)) {
158
- await contractStore.put(contractMessage.hash, contractMessage.encoded);
159
- await this.#runContract(contractMessage);
160
- return contractMessage.hash
161
- }
162
- throw new Error('duplicate contract')
163
- }
164
-
165
- async execute(contract, method, params) {
166
- return new Promise((resolve, reject) => {
167
- const id = crypto.randomBytes(20).toString('hex');
168
- const message = message => {
169
- if (message?.error) reject(message.error);
170
- else resolve(message);
171
- };
172
- pubsub.subscribe(id, message);
173
- this.worker.postMessage({
174
- type: 'execute',
175
- id,
176
- input: {
177
- contract,
178
- method,
179
- params
180
- }
181
- });
182
- })
183
-
184
- }
185
-
186
- addJob(contract, method, params, from, nonce) {
187
- if (!this.#nonces[from]) this.#nonces[from] = nonce;
188
- if (nonce === this.#nonces[from] + 1) return this.#contracts[contract][method](...params)
189
- // return setTimeout(() => {
190
- // return this.addJob(contract, method, params, from, nonce)
191
- // }, 50)
192
- }
193
-
194
- get(contract, method, params) {
195
- return new Promise((resolve, reject) => {
196
- const id = crypto.randomBytes(20).toString();
197
- const message = message => {
198
- resolve(message);
199
- };
200
- pubsub.subscribe(id, message);
201
- this.worker.postMessage({
202
- type: 'get',
203
- id,
204
- input: {
205
- contract,
206
- method,
207
- params
208
- }
209
- });
210
- })
211
- }
212
-
213
- async delete(hash) {
214
- return contractStore.delete(hash)
215
- }
216
-
217
- async deleteAll() {
218
- let hashes = await contractStore.get();
219
- hashes = Object.keys(hashes).map(hash => this.delete(hash));
220
- return Promise.all(hashes)
221
- }
222
- }
223
-
224
- var proto$4 = `
225
- message ContractMessage {
226
- required string creator = 1;
227
- required bytes contract = 2;
228
- repeated string constructorParameters = 3;
229
- }
230
- `;
231
-
232
- class ContractMessage extends codecFormatInterface.FormatInterface {
233
- get keys() {
234
- return ['creator', 'contract', 'constructorParameters']
235
- }
236
-
237
- get messageName() {
238
- return 'ContractMessage'
239
- }
240
-
241
- constructor(buffer) {
242
- super(buffer, proto$4, {name: 'contract-message'});
243
- }
244
- }
245
-
246
- var proto$3 = `
247
-
248
- message TransactionMessage {
249
- required uint64 timestamp = 1;
250
- required string from = 2;
251
- required string to = 3;
252
- required uint64 nonce = 4;
253
- required string method = 5;
254
- repeated string params = 6;
255
- required string signature = 7;
256
- }
257
- `;
258
-
259
- class TransactionMessage extends codecFormatInterface.FormatInterface {
260
- get keys() {
261
- return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
262
- }
263
-
264
- get messageName() {
265
- return 'TransactionMessage'
266
- }
267
-
268
- constructor(buffer) {
269
- const name = 'transaction-message';
270
- super(buffer, proto$3, {name});
271
- }
272
- }
273
-
274
- var proto$2 = `
275
- message ValidatorMessage {
276
- required string address = 1;
277
- required string reward = 2;
278
- }
279
-
280
- message Transaction {
281
- required string hash = 1;
282
- required uint64 timestamp = 2;
283
- required string from = 3;
284
- required string to = 4;
285
- required uint64 nonce = 5;
286
- required string method = 6;
287
- repeated string params = 7;
288
- }
289
-
290
- message BlockMessage {
291
- required uint64 index = 1;
292
- required string previousHash = 3;
293
- required uint64 timestamp = 4;
294
- required uint64 reward = 5;
295
- required string fees = 6;
296
- repeated Transaction transactions = 7;
297
- repeated ValidatorMessage validators = 8;
298
- }
299
- `;
300
-
301
- class BlockMessage extends codecFormatInterface.FormatInterface {
302
- get keys() {
303
- return ['index', 'previousHash', 'timestamp', 'reward', 'fees', 'transactions', 'validators']
304
- }
305
-
306
- get messageName() {
307
- return 'BlockMessage'
308
- }
309
-
310
- constructor(buffer) {
311
- const name = 'block-message';
312
- super(buffer, proto$2, {name});
313
- }
314
- }
315
-
316
- var proto$1 = `
317
-
318
- message BWMessage {
319
- required uint64 up = 1;
320
- required uint64 down = 2;
321
- }
322
- `;
323
-
324
- class BWMessage extends codecFormatInterface.FormatInterface {
325
- get keys() {
326
- return ['up', 'down']
327
- }
328
-
329
- get messageName() {
330
- return 'BWMessage'
331
- }
332
-
333
- constructor(buffer) {
334
- const name = 'bw-message';
335
- super(buffer, proto$1, {name});
336
- }
337
- }
338
-
339
- var proto = `
340
-
341
- message BWRequestMessage {
342
- }
343
- `;
344
-
345
- class BWRequestMessage extends codecFormatInterface.FormatInterface {
346
- get keys() {
347
- return []
348
- }
349
-
350
- get messageName() {
351
- return 'BWRequestMessage'
352
- }
353
-
354
- constructor(buffer) {
355
- const name = 'bw-request-message';
356
- super(buffer, proto, {name});
357
- }
358
- }
359
-
360
- var contractFactory = "237,198,141,3,123,34,99,114,101,97,116,111,114,34,58,34,51,67,122,86,51,66,98,76,103,117,57,105,55,119,70,115,83,101,82,101,90,113,110,80,111,53,82,114,121,49,118,81,98,82,115,89,114,116,67,67,97,81,103,98,113,80,72,112,116,117,85,111,72,52,34,44,34,99,111,110,116,114,97,99,116,34,58,34,114,101,116,117,114,110,32,99,108,97,115,115,32,70,97,99,116,111,114,121,123,35,110,97,109,101,61,92,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,92,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,118,97,108,105,100,40,104,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,61,91,93,41,123,99,111,110,115,116,32,109,101,115,115,97,103,101,61,110,101,119,32,67,111,110,116,114,97,99,116,77,101,115,115,97,103,101,40,123,99,114,101,97,116,111,114,58,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,58,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,58,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,125,41,59,114,101,116,117,114,110,32,66,111,111,108,101,97,110,40,109,101,115,115,97,103,101,46,104,97,115,104,61,61,61,104,97,115,104,41,125,97,115,121,110,99,32,100,101,112,108,111,121,67,111,110,116,114,97,99,116,40,99,111,110,116,114,97,99,116,72,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,61,91,93,41,123,105,102,40,99,111,110,116,114,97,99,116,46,99,114,101,97,116,111,114,33,61,61,109,115,103,46,115,101,110,100,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,111,110,108,121,32,97,32,99,111,110,116,114,97,99,116,32,99,114,101,97,116,111,114,32,99,97,110,32,100,101,112,108,111,121,32,97,32,99,111,110,116,114,97,99,116,92,34,41,59,105,102,40,97,119,97,105,116,32,99,111,110,116,114,97,99,116,83,116,111,114,101,46,104,97,115,40,104,97,115,104,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,100,117,112,108,105,99,97,116,101,32,99,111,110,116,114,97,99,116,92,34,41,59,105,102,40,33,116,104,105,115,46,105,115,86,97,108,105,100,40,99,111,110,116,114,97,99,116,72,97,115,104,44,99,114,101,97,116,111,114,44,99,111,110,116,114,97,99,116,44,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,105,110,118,97,108,105,100,32,99,111,110,116,114,97,99,116,92,34,41,59,97,119,97,105,116,32,99,111,110,116,114,97,99,116,83,116,111,114,101,46,112,117,116,40,104,97,115,104,44,101,110,99,111,100,101,100,41,44,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,104,97,115,104,41,125,125,59,92,110,34,44,34,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,34,58,91,93,125";
361
- var nativeToken = "237,198,141,3,123,34,99,114,101,97,116,111,114,34,58,34,51,67,122,86,51,66,98,76,103,117,57,105,55,119,70,115,83,101,82,101,90,113,110,80,111,53,82,114,121,49,118,81,98,82,115,89,114,116,67,67,97,81,103,98,113,80,72,112,116,117,85,111,72,52,34,44,34,99,111,110,116,114,97,99,116,34,58,34,114,101,116,117,114,110,32,99,108,97,115,115,32,65,114,116,79,110,108,105,110,101,32,101,120,116,101,110,100,115,32,99,108,97,115,115,32,84,111,107,101,110,32,101,120,116,101,110,100,115,32,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,69,114,114,111,114,40,92,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,92,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,92,34,79,87,78,69,82,92,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,45,49,33,61,61,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,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,92,34,79,87,78,69,82,92,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,92,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,92,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,92,34,79,87,78,69,82,92,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,78,111,116,32,97,108,108,111,119,101,100,92,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,92,34,79,87,78,69,82,92,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,78,111,116,32,97,108,108,111,119,101,100,92,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,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,92,34,110,97,109,101,32,117,110,100,101,102,105,110,101,100,92,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,92,34,115,121,109,98,111,108,32,117,110,100,101,102,105,110,101,100,92,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,92,34,77,73,78,84,92,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,110,111,116,32,97,108,108,111,119,101,100,92,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,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,92,34,66,85,82,78,92,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,110,111,116,32,97,108,108,111,119,101,100,92,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,116,111,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,92,34,97,109,111,117,110,116,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,92,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,92,34,48,120,48,48,92,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,92,34,48,120,48,48,92,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,92,34,48,120,48,48,92,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,103,108,111,98,97,108,84,104,105,115,46,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,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,92,34,65,114,116,79,110,108,105,110,101,92,34,44,92,34,65,82,84,92,34,44,49,56,44,115,116,97,116,101,41,125,125,59,92,110,34,44,34,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,34,58,91,93,125";
362
- var nameService = "237,198,141,3,123,34,99,114,101,97,116,111,114,34,58,34,51,67,122,86,51,66,98,76,103,117,57,105,55,119,70,115,83,101,82,101,90,113,110,80,111,53,82,114,121,49,118,81,98,82,115,89,114,116,67,67,97,81,103,98,113,80,72,112,116,117,85,111,72,52,34,44,34,99,111,110,116,114,97,99,116,34,58,34,114,101,116,117,114,110,32,99,108,97,115,115,32,78,97,109,101,83,101,114,118,105,99,101,123,35,110,97,109,101,61,92,34,65,114,116,79,110,108,105,110,101,78,97,109,101,83,101,114,118,105,99,101,92,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,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,92,34,110,111,32,111,119,110,101,114,92,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,92,34,110,111,32,111,119,110,101,114,92,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,92,34,110,111,32,111,119,110,101,114,92,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,92,34,98,97,108,97,110,99,101,79,102,92,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,92,34,112,114,105,99,101,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,92,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,92,34,116,114,97,110,115,102,101,114,92,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,41,123,116,104,114,111,119,32,101,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,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,110,111,116,32,97,32,111,119,110,101,114,92,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,46,111,119,110,101,114,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,92,34,110,111,116,32,97,32,111,119,110,101,114,92,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,59,92,110,34,44,34,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,34,58,91,34,105,104,110,121,50,103,113,103,111,114,119,111,102,104,118,102,103,116,114,53,100,109,50,116,97,114,103,110,120,114,117,108,53,114,114,52,119,110,121,107,52,118,108,55,97,107,98,109,119,111,120,52,51,110,108,118,112,105,109,34,44,34,105,104,110,121,50,103,113,104,101,55,103,97,122,107,105,101,101,121,52,111,51,114,122,116,102,121,55,53,55,105,97,51,97,115,54,115,110,114,109,111,118,52,101,51,118,107,102,100,118,116,50,111,108,118,120,51,115,116,101,34,44,34,105,104,110,121,50,103,113,104,108,109,117,98,114,118,107,108,51,105,120,121,102,97,100,111,109,112,118,102,105,105,110,118,119,116,104,105,52,51,119,112,116,52,113,100,102,106,55,54,55,110,104,112,121,106,108,110,119,105,110,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,125";
363
- var validators = "237,198,141,3,123,34,99,114,101,97,116,111,114,34,58,34,51,67,122,86,51,66,98,76,103,117,57,105,55,119,70,115,83,101,82,101,90,113,110,80,111,53,82,114,121,49,118,81,98,82,115,89,114,116,67,67,97,81,103,98,113,80,72,112,116,117,85,111,72,52,34,44,34,99,111,110,116,114,97,99,116,34,58,34,114,101,116,117,114,110,32,99,108,97,115,115,32,86,97,108,105,100,97,116,111,114,115,123,35,110,97,109,101,61,92,34,65,114,116,79,110,108,105,110,101,86,97,108,105,100,97,116,111,114,115,92,34,59,35,116,111,116,97,108,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,111,119,110,101,114,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,111,119,110,101,114,58,116,104,105,115,46,35,111,119,110,101,114,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,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,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,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,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,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,44,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,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,40,110,101,119,32,68,97,116,101,41,46,103,101,116,84,105,109,101,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,111,119,110,101,114,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,111,119,110,101,114,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,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,92,34,110,111,116,32,97,110,32,111,119,110,101,114,92,34,41,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,92,34,110,111,116,32,97,110,32,111,119,110,101,114,92,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,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,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,92,34,97,108,114,101,97,100,121,32,97,32,118,97,108,105,100,97,116,111,114,92,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,92,34,98,97,108,97,110,99,101,79,102,92,34,44,91,109,115,103,46,115,101,110,100,101,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,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,40,110,101,119,32,68,97,116,101,41,46,103,101,116,84,105,109,101,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,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,92,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,92,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,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,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,92,34,118,97,108,105,100,97,116,111,114,32,110,111,116,32,102,111,117,110,100,92,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,92,34,98,97,108,97,110,99,101,79,102,92,34,44,91,109,115,103,46,115,101,110,100,101,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,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,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,33,49,41,44,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,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,59,92,110,34,44,34,99,111,110,115,116,114,117,99,116,111,114,80,97,114,97,109,101,116,101,114,115,34,58,91,34,105,104,110,121,50,103,113,104,101,55,103,97,122,107,105,101,101,121,52,111,51,114,122,116,102,121,55,53,55,105,97,51,97,115,54,115,110,114,109,111,118,52,101,51,118,107,102,100,118,116,50,111,108,118,120,51,115,116,101,34,93,125";
364
- var bytecodes = {
365
- contractFactory: contractFactory,
366
- nativeToken: nativeToken,
367
- nameService: nameService,
368
- validators: validators
369
- };
370
-
371
- const contractFactoryMessage = bytecodes.contractFactory;
372
- const nativeTokenMessage = bytecodes.nativeToken;
373
-
374
- const nameServiceMessage = bytecodes.nameService;
375
- const validatorsMessage = bytecodes.validators;
376
-
377
- const createContractMessage = async (creator, contract, constructorParameters = []) => {
378
- return new ContractMessage({
379
- creator,
380
- contract,
381
- constructorParameters
382
- })
383
- };
384
-
385
- const calculateFee = async transaction => {
386
- // excluded from fees
387
- if (transaction.to === validators$1) return 0
388
- // fee per gb
389
- transaction = await new TransactionMessage(transaction);
390
- let fee = transaction.encoded.length;
391
- fee = fee / 1024;
392
- fee = fee / 1000000;
393
- const parts = String(fee).split('.');
394
- let decimals = 0;
395
- if (parts[1]) {
396
- const potentional = parts[1].split('e');
397
- parts[1] = potentional[0];
398
- decimals = Number(potentional[1].replace(/\-|\+/g, '')) + Number(potentional[0].length);
399
- }
400
-
401
- return Number.parseFloat(fee.toString()).toFixed(decimals)
402
- };
403
-
404
- globalThis.BigNumber = utils.BigNumber;
405
-
406
- // check if browser or local
407
- class Chain {
408
- #validators = []
409
- #blocks = []
410
- #machine
411
- #runningEpoch = false
412
- #lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
413
-
414
- #jail = []
415
-
416
- constructor() {
417
- return this.#init()
418
- }
419
-
420
- get lib() {
421
- return lib
422
- }
423
-
424
- get lastBlock() {
425
- return this.#lastBlock
426
- }
427
-
428
- get nativeToken() {
429
- return addresses.nativeToken
430
- }
431
-
432
- get validators() {
433
- return [...this.#validators]
434
- }
435
-
436
- get blocks() {
437
- return [...this.#blocks]
438
- }
439
-
440
- async hasTransactionToHandle() {
441
- const size = await transactionPoolStore.size();
442
- if (size > 0) return true
443
- return false
444
- }
445
-
446
- async #runEpoch() {
447
- this.#runningEpoch = true;
448
- console.log('epoch');
449
- const validators = await this.staticCall(addresses.validators, 'validators');
450
- console.log(validators);
451
- if (!validators[peernet.id]?.active) return
452
-
453
- const start = new Date().getTime();
454
- try {
455
- await this.#createBlock();
456
- } catch (e) {
457
- console.error(e);
458
- }
459
-
460
- const end = new Date().getTime();
461
- console.log(((end - start) / 1000) + ' s');
462
-
463
- if (await this.hasTransactionToHandle()) return this.#runEpoch()
464
- this.#runningEpoch = false;
465
- // if (await this.hasTransactionToHandle() && !this.#runningEpoch) return this.#runEpoch()
466
- }
467
-
468
- async #setup() {
469
-
470
- const contracts = [{
471
- address: addresses.contractFactory,
472
- message: contractFactoryMessage
473
- }, {
474
- address: addresses.nativeToken,
475
- message: nativeTokenMessage
476
- }, {
477
- address: addresses.validators,
478
- message: validatorsMessage
479
- }, {
480
- address: addresses.nameService,
481
- message: nameServiceMessage
482
- }];
483
-
484
- await Promise.all(contracts.map(async ({address, message}) => {
485
- message = await new ContractMessage(message);
486
- await contractStore.put(address, message.encoded);
487
- }));
488
- console.log('handle native contracts');
489
- // handle native contracts
490
- }
491
-
492
- async #sync() {
493
- let promises = [];
494
- for (const peer of peernet.connections) {
495
- if (peer.peerId !== this.id) {
496
- let data = await new peernet.protos['peernet-request']({request: 'lastBlock'});
497
- const node = await peernet.prepareMessage(data);
498
- promises.push(peer.request(node.encoded));
499
- }
500
- }
501
- promises = await Promise.allSettled(promises);
502
- promises = promises.filter(({status}) => status === 'fulfilled');
503
- promises = promises.map(({value}) => new peernet.protos['peernet-response'](value));
504
- promises = await Promise.all(promises);
505
- promises = promises.map(node => node.decoded.response);
506
- promises = promises.reduce((set, value) => {
507
-
508
- if (value.index > set.index) {
509
- set.index = value.index;
510
- set.hash = value.hash;
511
- }
512
- return set
513
- }, {index: 0, hash: '0x0'});
514
- // get lastblock
515
- if (promises.hash && promises.hash !== '0x0') {
516
- let localBlock = await peernet.get(promises.hash);
517
- console.log({localBlock});
518
- }
519
-
520
-
521
- }
522
-
523
- async #init() {
524
- // this.node = await new Node()
525
- this.participants = [];
526
- this.participating = false;
527
- const initialized = await contractStore.has(addresses.contractFactory);
528
- if (!initialized) await this.#setup();
529
-
530
- this.#machine = await new Machine();
531
- this.utils = { BigNumber: utils.BigNumber, formatUnits: utils.formatUnits, parseUnits: utils.parseUnits };
532
-
533
- await peernet.addRequestHandler('bw-request-message', () => {
534
-
535
- return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
536
- });
537
-
538
- await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
539
-
540
- peernet.subscribe('add-block', this.#addBlock.bind(this));
541
-
542
- peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
543
-
544
- peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
545
-
546
- pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
547
-
548
- try {
549
- let localBlock;
550
- try {
551
- localBlock = await chainStore.get('lastBlock');
552
- } catch(e) {
553
- await chainStore.put('lastBlock', '0x0');
554
- localBlock = await chainStore.get('lastBlock');
555
- }
556
- localBlock = new TextDecoder().decode(localBlock);
557
-
558
- if (localBlock && localBlock !== '0x0') {
559
- localBlock = await peernet.get(localBlock);
560
- localBlock = await new BlockMessage(localBlock);
561
- this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
562
- } else if (this.#machine.lastBlock?.hash) {
563
- // todo remove when network is running
564
- // recovering chain (not needed if multiple peers are online)
565
- this.#lastBlock = this.#machine.lastBlock;
566
- await chainStore.put('lastBlock', this.#lastBlock.hash);
567
- } else {
568
- await this.#sync();
569
- }
570
- } catch (e) {
571
- console.log({e});
572
-
573
-
574
- // this.#setup()
575
- }
576
- // load local blocks
577
- if (peernet.connections?.length > 1) await this.resolveBlocks();
578
- return this
579
- }
580
-
581
- async #validatorTimeout(validatorInfo) {
582
- setTimeout(() => {
583
- this.#jail.splice(this.jail.indexOf(validatorInfo.address), 1);
584
- }, validatorInfo.timeout);
585
- this.#jail.push(validatorInfo.address);
586
- }
587
-
588
- async #peerConnected(peer) {
589
- let node = await new peernet.protos['peernet-request']({request: 'lastBlock'});
590
- node = await peernet.prepareMessage(node);
591
- let response = await peer.request(node.encoded);
592
- response = await new globalThis.peernet.protos['peernet-response'](response);
593
- let lastBlock = response.decoded.response;
594
-
595
- if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
596
- // TODO: check if valid
597
- const localIndex = this.lastBlock ? this.lastBlock.index : 0;
598
- const index = lastBlock.index;
599
- await this.resolveBlock(lastBlock.hash);
600
- let blocksSynced = localIndex > 0 ? localIndex > index ? localIndex - index : index - localIndex : index;
601
- debug(`synced ${blocksSynced} ${blocksSynced > 1 ? 'blocks' : 'block'}`);
602
-
603
- this.#blocks.length;
604
- (this.#blocks.length) - blocksSynced;
605
- await this.#loadBlocks(this.#blocks);
606
- this.#lastBlock = this.#blocks[this.#blocks.length - 1];
607
- const message = await new BlockMessage(this.lastBlock);
608
- await blockStore.put(await message.hash, message.encoded);
609
- await chainStore.put('lastBlock', this.lastBlock.hash);
610
- }
611
- }
612
-
613
- #epochTimeout
614
-
615
- async #addTransaction(transaction) {
616
- try {
617
- transaction = await new TransactionMessage(transaction);
618
- const has = await transactionPoolStore.has(await transaction.hash);
619
- if (!has) await transactionPoolStore.put(await transaction.hash, transaction.encoded);
620
- if (this.participating && !this.#runningEpoch) this.#runEpoch();
621
- } catch (e) {
622
- throw Error('invalid transaction')
623
- }
624
- }
625
-
626
- async #lastBlockHandler() {
627
- return new peernet.protos['peernet-response']({response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index }})
628
- }
629
-
630
- async resolveBlock(hash) {
631
- if (!hash) throw new Error(`expected hash, got: ${hash}`)
632
- let block = await peernet.get(hash, 'block');
633
- block = await new BlockMessage(block);
634
- if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block');
635
- const size = block.encoded.length || block.encoded.byteLength;
636
- block = {...block.decoded, hash};
637
- this.#blocks[block.index - 1] = block;
638
- console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
639
- if (block.previousHash !== '0x0') {
640
- return this.resolveBlock(block.previousHash)
641
- }
642
- }
643
-
644
- async resolveBlocks() {
645
- try {
646
-
647
- const localBlock = await chainStore.get('lastBlock');
648
- const hash = new TextDecoder().decode(localBlock);
649
- if (hash && hash !== '0x0')
650
- await this.resolveBlock(localBlock);
651
- this.#lastBlock = this.#blocks[this.#blocks.length - 1];
652
- await this.#loadBlocks(this.#blocks);
653
- } catch (e) {
654
- await chainStore.put('lastBlock', new TextEncoder().encode('0x0'));
655
- return this.resolveBlocks()
656
- // console.log(e);
657
- }
658
- }
659
-
660
- async #loadBlocks(blocks) {
661
- for (const block of blocks) {
662
- if (block && !block.loaded) {
663
- for (const transaction of block.transactions) {
664
- try {
665
- await this.#machine.execute(transaction.to, transaction.method, transaction.params);
666
-
667
- } catch (e) {
668
- console.log(e);
669
- }
670
- }
671
- block.loaded = true;
672
- // let message = await peernet.get(block.hash, 'block')
673
-
674
- // const compressed = pako.deflate(message);
675
- // const result = pako.inflate(compressed);
676
- // console.log(result.length, compressed.length);
677
- //
678
- // console.log(result.length - compressed.length);
679
-
680
- // message = new BlockMessage(message)
681
- // for (const transaction of message.decoded.transactions) {
682
- // try {
683
- // await this.#machine.execute(transaction.to, transaction.method, transaction.params)
684
- //
685
- // } catch (e) {
686
- // // console.log(e);
687
- // }
688
- // }
689
- // block.loaded = true
690
- }
691
- }
692
- }
693
-
694
- async #executeTransaction({hash, from, to, method, params, nonce}) {
695
- try {
696
- let result = await this.#machine.execute(to, method, params, from, nonce);
697
- // if (!result) result = this.#machine.state
698
- pubsub.publish(`transaction.completed.${hash}`, {status: 'fulfilled', hash});
699
- return result ? result : 'no state change'
700
- } catch (e) {
701
- pubsub.publish(`transaction.completed.${hash}`, {status: 'fail', hash, error: e});
702
- throw e
703
- }
704
- }
705
-
706
- async #addBlock(block) {
707
- const blockMessage = await new BlockMessage(block);
708
- // if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
709
- // const transactionJob = async transaction => {
710
- // try {
711
- // transaction = await transactionPoolStore.get(transaction)
712
- // } catch (e) {
713
- // try {
714
- // transaction = await peernet.get(transaction, 'transaction')
715
- // } catch (e) {
716
- // console.warn(`couldn't resolve ${transaction}`);
717
- // }
718
- // }
719
- // transaction = new TransactionMessage(transaction)
720
- // return transaction
721
- // }
722
- await Promise.all(blockMessage.decoded.transactions
723
- .map(async transaction => transactionPoolStore.delete(await transaction.hash)));
724
- const hash = await blockMessage.hash;
725
- // let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
726
- // transactions = await Promise.all(transactions)
727
- this.#lastBlock = { hash, ...blockMessage.decoded };
728
- await blockStore.put(hash, blockMessage.encoded);
729
- await chainStore.put('lastBlock', hash);
730
- debug(`added block: ${hash}`);
731
- let promises = [];
732
- let contracts = [];
733
- for (let transaction of blockMessage.decoded.transactions) {
734
- // await transactionStore.put(transaction.hash, transaction.encoded)
735
- const index = contracts.indexOf(transaction.to);
736
- if (index === -1) contracts.push(transaction.to);
737
- promises.push(this.#executeTransaction(transaction));
738
- }
739
- try {
740
- promises = await Promise.allSettled(promises);
741
- for (let transaction of blockMessage.decoded.transactions) {
742
- await accountsStore.put(transaction.from, String(transaction.nonce));
743
- }
744
-
745
- // todo finish state
746
- // for (const contract of contracts) {
747
- // const state = await this.#machine.get(contract, 'state')
748
- // // await stateStore.put(contract, state)
749
- // console.log(state);
750
- // }
751
- pubsub.publish('block-processed', blockMessage.decoded);
752
- } catch (e) {
753
- console.log({e});
754
- }
755
-
756
- }
757
-
758
- async #updateState() {
759
-
760
- }
761
-
762
-
763
-
764
- async participate() {
765
- // TODO: validate participant
766
- // hold min amount of 50k ART for 7 days
767
- // lock the 50k
768
- // introduce peer-reputation
769
- // peerReputation(peerId)
770
- // {bandwith: {up, down}, uptime}
771
- this.participating = true;
772
- if (!await this.staticCall(addresses.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, addresses.validators, 'addValidator', [peernet.id]);
773
- if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch();
774
-
775
- // const runEpoch = () => setTimeout(async () => {
776
- // if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch()
777
- // runEpoch()
778
- // }, 5000)
779
- // runEpoch()
780
- }
781
-
782
- calculateFee(transaction) {
783
- // excluded from fees
784
- // if (transaction.decoded.to === addresses.validators) return 0
785
- // fee per gb
786
- return (transaction.encoded.length / 1024) / 1e-6
787
- }
788
-
789
- async getTransactions (transactions) {
790
- return new Promise(async (resolve, reject) => {
791
- let size = 0;
792
- const _transactions = [];
793
-
794
-
795
- await Promise.all(transactions
796
- .map(async tx => {
797
- tx = await new TransactionMessage(tx);
798
- size += tx.encoded.length;
799
- if (!formatBytes(size).includes('MB') || formatBytes(size).includes('MB') && Number(formatBytes(size).split(' MB')[0]) <= 0.75) _transactions.push({...tx.decoded, hash: await tx.hash});
800
- else resolve(_transactions);
801
- }));
802
-
803
- return resolve(_transactions)
804
- })
805
-
806
- }
807
-
808
- // todo filter tx that need to wait on prev nonce
809
- async #createBlock(limit = 1800) {
810
- // vote for transactions
811
- if (await transactionPoolStore.size() === 0) return;
812
-
813
- let transactions = await transactionPoolStore.values(limit);
814
-
815
- if (Object.keys(transactions)?.length === 0 ) return
816
-
817
- let block = {
818
- transactions: [],
819
- validators: [],
820
- fees: 0
821
- };
822
-
823
- // exclude failing tx
824
- transactions = await this.getTransactions(transactions.slice(0, transactions.length < 1800 ? transactions.length : 1800));
825
-
826
- transactions = transactions.sort((a, b) => a.nonce - b.nonce);
827
- for (let transaction of transactions) {
828
- try {
829
- await this.#executeTransaction(transaction);
830
- block.transactions.push(transaction);
831
- block.fees += Number(calculateFee(transaction));
832
- await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)));
833
- } catch (e) {
834
- transaction = await new TransactionMessage(transaction);
835
- await transactionPoolStore.delete(await transaction.hash);
836
- }
837
- }
838
- // don't add empty block
839
- if (block.transactions.length === 0) return
840
- const validators = await this.staticCall(addresses.validators, 'validators');
841
- console.log({validators});
842
- // block.validators = Object.keys(block.validators).reduce((set, key) => {
843
- // if (block.validators[key].active) {
844
- // push({
845
- // address: key
846
- // })
847
- // }
848
- // }, [])
849
- const peers = {};
850
- for (const entry of peernet.peerEntries) {
851
- peers[entry[0]] = entry[1];
852
- }
853
- for (const validator of Object.keys(validators)) {
854
- if (validators[validator].active) {
855
- const peer = peers[validator];
856
- if (peer && peer.connected) {
857
- let data = await new BWRequestMessage();
858
- const node = await peernet.prepareMessage(validator, data.encoded);
859
- try {
860
- const bw = await peer.request(node.encoded);
861
- console.log(bw);
862
- block.validators.push({
863
- address: validator,
864
- bw: bw.up + bw.down
865
- });
866
- } catch(e) {
867
-
868
- }
869
-
870
- } else if (peernet.id === validator) {
871
- block.validators.push({
872
- address: peernet.id,
873
- bw: peernet.bw.up + peernet.bw.down
874
- });
875
-
876
- }
877
- }
878
-
879
- }
880
-
881
- console.log({validators: block.validators});
882
-
883
- block.reward = 150;
884
- block.validators = block.validators.map(validator => {
885
- validator.reward = String(Number(block.fees) + block.reward / block.validators.length);
886
- delete validator.bw;
887
- return validator
888
- });
889
- // block.validators = calculateValidatorReward(block.validators, block.fees)
890
-
891
- block.index = this.lastBlock?.index;
892
- if (block.index === undefined) block.index = 0;
893
- else block.index += 1;
894
-
895
- block.previousHash = this.lastBlock?.hash || '0x0';
896
- block.timestamp = new Date().getTime();
897
-
898
- const parts = String(block.fees).split('.');
899
- let decimals = 0;
900
- if (parts[1]) {
901
- const potentional = parts[1].split('e');
902
- if (potentional[0] !== parts[1]) {
903
- parts[1] = potentional[0];
904
- decimals = Number(potentional[1]?.replace(/\-|\+/g, '')) + Number(potentional[0].length);
905
- } else {
906
- decimals = parts[1].length;
907
- }
908
-
909
- }
910
- block.fees = Number.parseFloat(String(block.fees)).toFixed(decimals);
911
-
912
- try {
913
- await Promise.all(block.transactions
914
- .map(async transaction => transactionPoolStore.delete(await transaction.hash)));
915
-
916
-
917
- let blockMessage = await new BlockMessage(block);
918
- const hash = await blockMessage.hash;
919
-
920
-
921
- this.#lastBlock = { hash, ...blockMessage.decoded };
922
- await blockStore.put(hash, blockMessage.encoded);
923
- await chainStore.put('lastBlock', hash);
924
- debug(`created block: ${hash}`);
925
-
926
- peernet.publish('add-block', blockMessage.encoded);
927
- this.#updateState(blockMessage);
928
- } catch (e) {
929
- console.log(e);
930
- throw Error(`invalid block ${block}`)
931
- }
932
- // data = await this.#machine.execute(to, method, params)
933
- // transactionStore.put(message.hash, message.encoded)
934
- }
935
-
936
- async promiseTransactions(transactions) {
937
- transactions = await Promise.all(transactions.map(tx => new TransactionMessage(tx)));
938
- return transactions
939
- }
940
-
941
- async promiseTransactionsContent(transactions) {
942
- transactions = await Promise.all(transactions.map(tx => new Promise(async (resolve, reject) => {
943
- resolve({ ...tx.decoded, hash: await tx.hash });
944
- })));
945
-
946
- return transactions
947
- }
948
-
949
- async #getNonceFallback(address) {
950
- let transactions = await transactionPoolStore.values();
951
- transactions = await this.promiseTransactions(transactions);
952
- transactions = transactions.filter(tx => tx.decoded.from === address);
953
- transactions = await this.promiseTransactionsContent(transactions);
954
-
955
- if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
956
-
957
- let block = await peernet.get(this.lastBlock.hash);
958
- block = await new BlockMessage(block);
959
-
960
- // for (let tx of block.decoded?.transactions) {
961
- // tx = await peernet.get(tx, 'transaction')
962
- // transactions.push(new TransactionMessage(tx))
963
- // }
964
- transactions = transactions.filter(tx => tx.from === address);
965
- while (transactions.length === 0 && block.decoded.index !== 0 && block.decoded.previousHash !== '0x0') {
966
- block = await blockStore.get(block.decoded.previousHash);
967
- block = await new BlockMessage(block);
968
- transactions = block.decoded.transactions.filter(tx => tx.from === address);
969
- }
970
-
971
- }
972
- if (transactions.length === 0) return 0
973
-
974
- transactions = transactions.sort((a, b) => a.timestamp - b.timestamp);
975
- return transactions[transactions.length - 1].nonce
976
- }
977
-
978
- async getNonce(address) {
979
- if (!await accountsStore.has(address)) {
980
- const nonce = await this.#getNonceFallback(address);
981
- await accountsStore.put(address, new TextEncoder().encode(String(nonce)));
982
- }
983
- let nonce = await accountsStore.get(address);
984
- nonce = new TextDecoder().decode(nonce);
985
- return Number(nonce)
986
- }
987
-
988
- /**
989
- * whenever method = createContract params should hold the contract hash
990
- *
991
- * example: [hash]
992
- * createTransaction('0x0', 'createContract', [hash])
993
- *
994
- * @param {String} to - the contract address for the contract to interact with
995
- * @param {String} method - the method/function to run
996
- * @param {Array} params - array of paramters to apply to the contract method
997
- * @param {Number} nonce - total transaction count [optional]
998
- */
999
- async createTransaction(to, method, params, nonce, signature) {
1000
- return this.createTransactionFrom(peernet.id, to, method, params, nonce)
1001
- }
1002
-
1003
-
1004
-
1005
- /**
1006
- *
1007
- * @param {Object} transaction {}
1008
- * @param {String} transaction.from address
1009
- * @param {String} transaction.to address
1010
- * @param {Object} transaction.params {}
1011
- * @param {String} transaction.params.method get, call
1012
- * @param {Buffer} transaction.params.data
1013
- * @returns
1014
- */
1015
- async createTransactionHash(transaction) {
1016
- // todo: validate
1017
- const peernetHash = await new index.CodecHash(transaction, {name: 'transaction-message'});
1018
- return peernetHash.digest
1019
- }
1020
-
1021
- /**
1022
- * @params {object} transaction -
1023
- * @params {object} wallet - any wallet/signer that supports sign(RAWtransaction)
1024
- */
1025
- async #signTransaction (transaction, wallet) {
1026
- return wallet.sign(await this.createTransactionHash(transaction))
1027
- }
1028
-
1029
- async signTransaction(transaction, signer) {
1030
- let identity = await walletStore.get('identity');
1031
- identity = JSON.parse(new TextDecoder().decode(identity));
1032
- const wallet = new MultiWallet__default["default"](peernet.network);
1033
- wallet.recover(identity.mnemonic);
1034
- const account = wallet.account(0).external(0);
1035
- transaction.signature = await this.#signTransaction(transaction, account);
1036
- transaction.signature = bs32__default["default"].encode(transaction.signature);
1037
- return transaction
1038
- }
1039
-
1040
- /**
1041
- *
1042
- * @param {Object} transaction
1043
- * @param {Address} transaction.from
1044
- * @param {Address} transaction.to
1045
- * @param {String} transaction.method
1046
- * @param {Array} transaction.params
1047
- * @param {Number} transaction.nonce
1048
- *
1049
- * @returns {Object} transaction
1050
- */
1051
- async createRawTransaction(transaction) {
1052
- if (!transaction.from) transaction.from = peernet.id;
1053
- transaction.timestamp = Date.now();
1054
-
1055
- if (transaction.nonce === undefined) {
1056
- transaction.nonce = await this.getNonce(transaction.from);
1057
- } else {
1058
- let nonce = await accountsStore.get(transaction.from);
1059
- nonce = new TextDecoder().decode(nonce);
1060
- if (transaction.nonce < nonce) throw Error(`a transaction with a higher nonce already exists`)
1061
- if (transaction.nonce === nonce) throw Error(`a transaction with the same nonce already exists`)
1062
- }
1063
- return transaction
1064
- }
1065
- /**
1066
- * every tx done is trough contracts so no need for amount
1067
- * data is undefined when nothing is returned
1068
- * error is thrown on error so undefined data doesn't mean there is an error...
1069
- *
1070
- * @param {String} from - the sender address
1071
- * @param {String} to - the contract address for the contract to interact with
1072
- * @param {String} method - the method/function to run
1073
- * @param {Array} params - array of paramters to apply to the contract method
1074
- * @param {Number} nonce - total transaction count [optional]
1075
- */
1076
- async createTransactionFrom(from, to, method, params, nonce) {
1077
- try {
1078
-
1079
- const rawTransaction = await this.createRawTransaction({from, to, nonce, method, params});
1080
- const transaction = await this.signTransaction(rawTransaction, from);
1081
- const message = await new TransactionMessage(transaction);
1082
-
1083
- let data;
1084
- // await transactionPoolStore.put(message.hash, new TextEncoder().encode(JSON.stringify({signature, message: message.encoded})))
1085
- const wait = () => new Promise(async (resolve, reject) => {
1086
- if (pubsub.subscribers[`transaction.completed.${await message.hash}`]) {
1087
- const result = pubsub.subscribers[`transaction.completed.${await message.hash}`].value;
1088
- result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error});
1089
- } else {
1090
- const completed = async result => {
1091
- result.status === 'fulfilled' ? resolve(await result.hash) : reject({hash: await result.hash, error: result.error});
1092
-
1093
- setTimeout(async () => {
1094
- pubsub.unsubscribe(`transaction.completed.${await message.hash}`, completed);
1095
- }, 10000);
1096
- };
1097
- pubsub.subscribe(`transaction.completed.${await message.hash}`, completed);
1098
- }
1099
-
1100
-
1101
- });
1102
-
1103
- await transactionPoolStore.put(await message.hash, message.encoded);
1104
- peernet.publish('add-transaction', message.encoded);
1105
- this.#addTransaction(message.encoded);
1106
- return {hash: await message.hash, data, fee: await calculateFee(message.decoded), wait}
1107
- } catch (e) {
1108
- console.log(e);
1109
- throw e
1110
- }
1111
-
1112
- }
1113
-
1114
- async createContractMessage(creator, contract, constructorParameters = []) {
1115
- return createContractMessage(creator, contract, constructorParameters = [])
1116
- }
1117
-
1118
- async createContractAddress(creator, contract, constructorParameters = []) {
1119
- contract = await this.createContractMessage(creator, contract, constructorParameters);
1120
- return contract.hash
1121
- }
1122
-
1123
- /**
1124
- *
1125
- * @param {String} contract - a contract string (see plugins/deployContract)
1126
- */
1127
- async deployContract(contract, params = []) {
1128
- globalThis.msg = {sender: peernet.id, call: this.call};
1129
-
1130
- const hash = await this.createContractAddress(creator, contract, params);
1131
- console.log(hash);
1132
- try {
1133
- const tx = await this.createTransactionFrom(peernet.id, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
1134
- } catch (e) {
1135
- throw e
1136
- }
1137
- return this.#machine.addContract(message)
1138
- }
1139
-
1140
- #createMessage(sender = peernet.id) {
1141
- return {
1142
- sender: peernet.id,
1143
- call: this.call,
1144
- staticCall: this.staticCall,
1145
- delegate: this.delegate,
1146
- staticDelegate: this.staticDelegate
1147
- }
1148
- }
1149
-
1150
- internalCall(sender, contract, method, params) {
1151
- globalThis.msg = this.#createMessage(sender);
1152
-
1153
- return this.#machine.execute(contract, method, params)
1154
- }
1155
-
1156
- call(contract, method, params) {
1157
- globalThis.msg = this.#createMessage();
1158
-
1159
- return this.#machine.execute(contract, method, params)
1160
- }
1161
-
1162
- staticCall(contract, method, params) {
1163
- globalThis.msg = this.#createMessage();
1164
- return this.#machine.get(contract, method, params)
1165
- }
1166
-
1167
- delegate(contract, method, params) {
1168
- globalThis.msg = this.#createMessage();
1169
-
1170
- return this.#machine.execute(contract, method, params)
1171
- }
1172
-
1173
- staticDelegate(contract, method, params) {
1174
- globalThis.msg = this.#createMessage();
1175
-
1176
- return this.#machine.get(contract, method, params)
1177
- }
1178
-
1179
- mint(to, amount) {
1180
- return this.call(addresses.nativeToken, 'mint', [to, amount])
1181
- }
1182
-
1183
- transfer(from, to, amount) {
1184
- return this.call(addresses.nativeToken, 'transfer', [from, to, amount])
1185
- }
1186
-
1187
- get balances() {
1188
- return this.staticCall(addresses.nativeToken, 'balances')
1189
- }
1190
-
1191
- deleteAll() {
1192
- return this.#machine.deleteAll()
1193
- }
1194
-
1195
- /**
1196
- * lookup an address for a registered name using the builtin nameService
1197
- * @check nameService
1198
- *
1199
- * @param {String} - contractName
1200
- * @returns {String} - address
1201
- *
1202
- * @example chain.lookup('myCoolContractName') // qmqsfddfdgfg...
1203
- */
1204
- lookup(name) {
1205
- return this.call(addresses.nameService, 'lookup', [name])
1206
- }
1207
- }
1208
-
1209
- module.exports = Chain;