@leofcoin/chain 1.7.29 → 1.7.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/browser/chain.js +29 -20
- package/exports/browser/workers/block-worker.js +1 -1
- package/exports/browser/workers/machine-worker.js +380 -410
- package/exports/browser/workers/{worker-Botf--mj.js → worker-Botf--mj-BUK2CMT9.js} +0 -2
- package/exports/chain.js +29 -20
- package/exports/machine.d.ts +10 -6
- package/exports/workers/block-worker.js +1 -1
- package/exports/workers/machine-worker.js +380 -410
- package/exports/workers/{worker-Botf--mj.js → worker-Botf--mj-BUK2CMT9.js} +0 -2
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as BigNumber, E as EasyWorker, C as ContractMessage, T as TransactionMessage } from './worker-Botf--mj-BUK2CMT9.js';
|
|
2
2
|
|
|
3
3
|
const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
4
4
|
const formatBytes = (bytes, decimals = 2) => {
|
|
@@ -107,413 +107,383 @@ class LittlePubSub {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
const contractFactoryMessage = bytecodes.contractFactory;
|
|
114
|
-
const nativeTokenMessage = bytecodes.nativeToken;
|
|
115
|
-
const nameServiceMessage = bytecodes.nameService;
|
|
116
|
-
const validatorsMessage = bytecodes.validators;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let
|
|
120
|
-
let
|
|
121
|
-
let
|
|
122
|
-
let
|
|
123
|
-
let
|
|
124
|
-
|
|
125
|
-
let
|
|
126
|
-
let
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
result
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
pubsub.
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
respond(id, totalTransfers);
|
|
490
|
-
break
|
|
491
|
-
case 'totalBlocks':
|
|
492
|
-
respond(id, blocks.length);
|
|
493
|
-
break
|
|
494
|
-
case 'blocks':
|
|
495
|
-
respond(id, input ? blocks.slice(input.from, input.to) : blocks);
|
|
496
|
-
break
|
|
497
|
-
case 'block':
|
|
498
|
-
respond(id, blocks[input - 1]);
|
|
499
|
-
break
|
|
500
|
-
case 'lastBlock':
|
|
501
|
-
respond(id, lastBlock);
|
|
502
|
-
break
|
|
503
|
-
case 'latestTransactions':
|
|
504
|
-
respond(id, latestTransactions);
|
|
505
|
-
break
|
|
506
|
-
case 'totalTransactions':
|
|
507
|
-
respond(id, totalTransactions);
|
|
508
|
-
break
|
|
509
|
-
case 'has':
|
|
510
|
-
respond(id, has(input.address));
|
|
511
|
-
break
|
|
512
|
-
case 'get':
|
|
513
|
-
respond(id, get(input));
|
|
514
|
-
break
|
|
515
|
-
default:
|
|
516
|
-
console.log(`machine-worker: unsupported taskType: ${type}`);
|
|
517
|
-
break
|
|
518
|
-
}
|
|
110
|
+
globalThis.BigNumber = BigNumber;
|
|
111
|
+
const pubsub = new LittlePubSub();
|
|
112
|
+
const worker = new EasyWorker();
|
|
113
|
+
const contractFactoryMessage = bytecodes.contractFactory;
|
|
114
|
+
const nativeTokenMessage = bytecodes.nativeToken;
|
|
115
|
+
const nameServiceMessage = bytecodes.nameService;
|
|
116
|
+
const validatorsMessage = bytecodes.validators;
|
|
117
|
+
const latestTransactions = [];
|
|
118
|
+
let nativeCalls;
|
|
119
|
+
let nativeBurns;
|
|
120
|
+
let nativeMints;
|
|
121
|
+
let nativeTransfers;
|
|
122
|
+
let totalTransactions;
|
|
123
|
+
let totalBurnAmount;
|
|
124
|
+
let totalMintAmount;
|
|
125
|
+
let totalTransferAmount;
|
|
126
|
+
let blocks = [];
|
|
127
|
+
let contracts = {};
|
|
128
|
+
let lastBlock = { index: -1, hash: '0x0', previousHash: '0x0' };
|
|
129
|
+
const createMessage = (sender = globalThis.peerid, contract) => {
|
|
130
|
+
return {
|
|
131
|
+
contract,
|
|
132
|
+
sender,
|
|
133
|
+
call: (params) => {
|
|
134
|
+
// make sure sender is set to the actual caller (iow contracts need approval to access tokens ...)
|
|
135
|
+
globalThis.msg = createMessage(contract, params.contract);
|
|
136
|
+
return _.execute(params);
|
|
137
|
+
},
|
|
138
|
+
staticCall: get
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
const debug = (message) => {
|
|
142
|
+
worker.postMessage({
|
|
143
|
+
type: 'debug',
|
|
144
|
+
message
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
const has = (address) => {
|
|
148
|
+
return contracts[address] ? true : false;
|
|
149
|
+
};
|
|
150
|
+
const get = ({ contract, method, params }) => {
|
|
151
|
+
let result;
|
|
152
|
+
if (params?.length > 0) {
|
|
153
|
+
result = contracts[contract][method](...params);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
result = contracts[contract][method];
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
};
|
|
160
|
+
const respond = (id, value) => {
|
|
161
|
+
worker.postMessage({
|
|
162
|
+
type: 'response',
|
|
163
|
+
value,
|
|
164
|
+
id
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
const askFor = (question, input) => new Promise((resolve) => {
|
|
168
|
+
const id = globalThis.crypto.randomUUID();
|
|
169
|
+
pubsub.subscribe(id, resolve);
|
|
170
|
+
worker.postMessage({
|
|
171
|
+
type: 'ask',
|
|
172
|
+
question,
|
|
173
|
+
input,
|
|
174
|
+
id
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
const resolveContract = (hash) => askFor('contract', hash);
|
|
178
|
+
const resolveTransaction = (hash) => askFor('transaction', hash);
|
|
179
|
+
const runTask = async (id, taskName, input) => {
|
|
180
|
+
try {
|
|
181
|
+
const result = await _[taskName](input);
|
|
182
|
+
respond(id, result);
|
|
183
|
+
}
|
|
184
|
+
catch (e) {
|
|
185
|
+
worker.postMessage({
|
|
186
|
+
type: `${taskName}Error`,
|
|
187
|
+
message: e.message,
|
|
188
|
+
id
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
const _executeTransaction = async (transaction) => {
|
|
193
|
+
const hash = await new TransactionMessage(transaction).hash();
|
|
194
|
+
if (latestTransactions.includes(hash)) {
|
|
195
|
+
throw new Error(`double transaction found: ${hash}`);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
latestTransactions.push(hash);
|
|
199
|
+
const { from, to, method, params, nonce } = transaction;
|
|
200
|
+
globalThis.msg = createMessage(from, to);
|
|
201
|
+
await _.execute({ contract: to, method, params });
|
|
202
|
+
worker.postMessage({
|
|
203
|
+
type: 'transactionLoaded',
|
|
204
|
+
result: {
|
|
205
|
+
hash,
|
|
206
|
+
from,
|
|
207
|
+
nonce: String(nonce)
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
const addToWantList = (hash) => {
|
|
213
|
+
worker.postMessage({
|
|
214
|
+
type: 'addToWantList',
|
|
215
|
+
hash
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
const _ = {
|
|
219
|
+
runContract: async ({ decoded, hash, encoded }, state) => {
|
|
220
|
+
const params = decoded.constructorParameters;
|
|
221
|
+
try {
|
|
222
|
+
const func = new Function(new TextDecoder().decode(decoded.contract));
|
|
223
|
+
const Contract = func();
|
|
224
|
+
if (state)
|
|
225
|
+
params.push(state);
|
|
226
|
+
globalThis.msg = createMessage(decoded.creator, hash);
|
|
227
|
+
contracts[hash] = await new Contract(...params);
|
|
228
|
+
debug(`loaded contract: ${hash} size: ${formatBytes(encoded.length)}`);
|
|
229
|
+
}
|
|
230
|
+
catch (e) {
|
|
231
|
+
console.log(e);
|
|
232
|
+
worker.postMessage({
|
|
233
|
+
type: 'contractError',
|
|
234
|
+
message: e.message,
|
|
235
|
+
hash
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
execute: async ({ contract, method, params }) => {
|
|
240
|
+
try {
|
|
241
|
+
let result;
|
|
242
|
+
// don't execute the method on a proxy
|
|
243
|
+
if (contracts[contract].fallback) {
|
|
244
|
+
result = await contracts[contract].fallback(method, params);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
result = await contracts[contract][method](...params);
|
|
248
|
+
}
|
|
249
|
+
if (contract === nativeToken$2) {
|
|
250
|
+
nativeCalls.add(1);
|
|
251
|
+
if (method === 'burn') {
|
|
252
|
+
nativeBurns = nativeBurns.add(1);
|
|
253
|
+
totalBurnAmount = totalBurnAmount.add(params[1]);
|
|
254
|
+
}
|
|
255
|
+
if (method === 'mint') {
|
|
256
|
+
nativeMints = nativeMints.add(1);
|
|
257
|
+
totalMintAmount = totalMintAmount.add(params[1]);
|
|
258
|
+
}
|
|
259
|
+
if (method === 'transfer') {
|
|
260
|
+
nativeTransfers = nativeTransfers.add(1);
|
|
261
|
+
totalTransferAmount = totalTransferAmount.add(params[2]);
|
|
262
|
+
}
|
|
263
|
+
if (method === 'transferFrom') {
|
|
264
|
+
nativeTransfers = nativeTransfers.add(1);
|
|
265
|
+
totalTransferAmount = totalTransferAmount.add(params[1]);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
totalTransactions = totalTransactions.add(1);
|
|
269
|
+
// state.put(result)
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
catch (e) {
|
|
273
|
+
console.log({ e });
|
|
274
|
+
throw new Error(`error: ${e.message}
|
|
275
|
+
contract: ${contract}
|
|
276
|
+
method: ${method}
|
|
277
|
+
params: ${JSON.stringify(params, null, '\t')}
|
|
278
|
+
`);
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
init: async (message) => {
|
|
282
|
+
let { peerid, fromState, state, info } = message;
|
|
283
|
+
globalThis.peerid = peerid;
|
|
284
|
+
console.log({ fromState, info });
|
|
285
|
+
nativeCalls = BigNumber.from(info?.nativeCalls ?? 0);
|
|
286
|
+
nativeMints = BigNumber.from(info?.nativeMints ?? 0);
|
|
287
|
+
nativeBurns = BigNumber.from(info?.nativeBurns ?? 0);
|
|
288
|
+
nativeTransfers = BigNumber.from(info?.nativeTransfers ?? 0);
|
|
289
|
+
totalTransactions = BigNumber.from(info?.totalTransactions ?? 0);
|
|
290
|
+
totalBurnAmount = BigNumber.from(info?.totalBurnAmount ?? 0);
|
|
291
|
+
totalMintAmount = BigNumber.from(info?.totalMintAmount ?? 0);
|
|
292
|
+
totalTransferAmount = BigNumber.from(info?.totalTransferAmount ?? 0);
|
|
293
|
+
if (fromState) {
|
|
294
|
+
lastBlock = message.lastBlock;
|
|
295
|
+
const setState = async (address, state) => {
|
|
296
|
+
const contractBytes = await resolveContract(address);
|
|
297
|
+
if (contractBytes === address) {
|
|
298
|
+
addToWantList(address);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const contract = await new ContractMessage(contractBytes);
|
|
302
|
+
await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state);
|
|
303
|
+
};
|
|
304
|
+
const entries = Object.entries(state);
|
|
305
|
+
if (entries.length > 0) {
|
|
306
|
+
const promises = [];
|
|
307
|
+
for (const [address, value] of entries) {
|
|
308
|
+
promises.push(setState(address, value));
|
|
309
|
+
}
|
|
310
|
+
await Promise.all(promises);
|
|
311
|
+
}
|
|
312
|
+
const promises = [];
|
|
313
|
+
if (!contracts[addresses.contractFactory])
|
|
314
|
+
promises.push(setState(addresses.contractFactory));
|
|
315
|
+
if (!contracts[addresses.nameService])
|
|
316
|
+
promises.push(setState(addresses.nameService));
|
|
317
|
+
if (!contracts[addresses.validators])
|
|
318
|
+
promises.push(setState(addresses.validators));
|
|
319
|
+
if (!contracts[addresses.nativeToken])
|
|
320
|
+
promises.push(setState(addresses.nativeToken));
|
|
321
|
+
// contracts = await Promise.all(
|
|
322
|
+
// contracts.map(async (contract) => {
|
|
323
|
+
// contract = await new ContractMessage(new Uint8Array(contract.split(',')))
|
|
324
|
+
// await _.runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() })
|
|
325
|
+
// return contract
|
|
326
|
+
// })
|
|
327
|
+
// )
|
|
328
|
+
await Promise.all(promises);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
await Promise.all([contractFactoryMessage, nativeTokenMessage, nameServiceMessage, validatorsMessage].map(async (contract) => {
|
|
332
|
+
contract = await new ContractMessage(new Uint8Array(contract.split(',')));
|
|
333
|
+
return _.runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() });
|
|
334
|
+
}));
|
|
335
|
+
console.log({ blocks: message.blocks });
|
|
336
|
+
if (message.blocks?.length > 0) {
|
|
337
|
+
// let pre
|
|
338
|
+
// try {
|
|
339
|
+
// const importee = await import('url')
|
|
340
|
+
// const url = importee.default
|
|
341
|
+
// if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
|
|
342
|
+
// } catch {
|
|
343
|
+
// // browser env
|
|
344
|
+
// pre = './'
|
|
345
|
+
// }
|
|
346
|
+
// let _worker = await new EasyWorker(pre + './block-worker.js', {
|
|
347
|
+
// serialization: 'advanced',
|
|
348
|
+
// type: 'module'
|
|
349
|
+
// })
|
|
350
|
+
// blocks = await _worker.once(message.blocks)
|
|
351
|
+
// _worker = null
|
|
352
|
+
// blocks = unique(globalThis.blocks ? globalThis : [], blocks)
|
|
353
|
+
// for (let i = 0; i < blocks.length; i++) {
|
|
354
|
+
// }
|
|
355
|
+
blocks = message.blocks;
|
|
356
|
+
for (const block of blocks) {
|
|
357
|
+
// we only revalidate the latest 24 blocks
|
|
358
|
+
// every 24 blocks a snapshot is taken and stored in state
|
|
359
|
+
// this means contracts will be restored from this state
|
|
360
|
+
// this also means devs NEED to make sure the state can be restored
|
|
361
|
+
// on contract deploy an error will be thrown if state wasn't recoverable
|
|
362
|
+
if (block.index >= blocks.length - 24) {
|
|
363
|
+
const transactionCount = blocks[block.index].transactions.length;
|
|
364
|
+
latestTransactions.splice(-transactionCount, latestTransactions.length);
|
|
365
|
+
}
|
|
366
|
+
if (!block.loaded && !fromState) {
|
|
367
|
+
try {
|
|
368
|
+
const transactions = await Promise.all(block.transactions.map(async (transaction) => {
|
|
369
|
+
const message = new TransactionMessage(await resolveTransaction(transaction)).decode();
|
|
370
|
+
if (message === transaction) {
|
|
371
|
+
throw new Error(`nothing found for ${transaction}`);
|
|
372
|
+
}
|
|
373
|
+
return message;
|
|
374
|
+
}));
|
|
375
|
+
const priority = transactions
|
|
376
|
+
.filter((transaction) => transaction.priority)
|
|
377
|
+
?.sort((a, b) => a.nonce - b.nonce);
|
|
378
|
+
if (priority.length > 0)
|
|
379
|
+
for (const transaction of priority) {
|
|
380
|
+
await _executeTransaction(transaction);
|
|
381
|
+
}
|
|
382
|
+
await Promise.all(transactions
|
|
383
|
+
.filter((transaction) => !transaction.priority)
|
|
384
|
+
.map(async (transaction) => _executeTransaction(transaction)));
|
|
385
|
+
block.loaded = true;
|
|
386
|
+
worker.postMessage({
|
|
387
|
+
type: 'debug',
|
|
388
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
// just tell chain it's ready so we can get this node sync
|
|
393
|
+
// when a node connects this node will try to resolve the wantList
|
|
394
|
+
// this should result in the node beeing sync
|
|
395
|
+
if (error.message.includes('nothing found for'))
|
|
396
|
+
worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
397
|
+
else
|
|
398
|
+
console.error(error);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (blocks.length > 0) {
|
|
403
|
+
lastBlock = blocks[blocks.length - 1];
|
|
404
|
+
}
|
|
405
|
+
globalThis.blocks = blocks;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
409
|
+
},
|
|
410
|
+
addLoadedBlock: (block) => {
|
|
411
|
+
blocks[block.index - 1] = block;
|
|
412
|
+
lastBlock = blocks[blocks.length - 1];
|
|
413
|
+
return true;
|
|
414
|
+
},
|
|
415
|
+
loadBlock: (block) => {
|
|
416
|
+
// todo validate here and deprecate addLoadedBlock
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
worker.onmessage(({ id, type, input }) => {
|
|
420
|
+
if (pubsub.hasSubscribers(id)) {
|
|
421
|
+
pubsub.publish(id, input);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
switch (type) {
|
|
425
|
+
case 'init':
|
|
426
|
+
runTask(id, 'init', input);
|
|
427
|
+
break;
|
|
428
|
+
case 'run':
|
|
429
|
+
runTask(id, 'runContract', input);
|
|
430
|
+
break;
|
|
431
|
+
case 'execute':
|
|
432
|
+
runTask(id, 'execute', input);
|
|
433
|
+
break;
|
|
434
|
+
case 'addLoadedBlock':
|
|
435
|
+
runTask(id, 'addLoadedBlock', input);
|
|
436
|
+
break;
|
|
437
|
+
case 'nativeCalls':
|
|
438
|
+
respond(id, nativeCalls);
|
|
439
|
+
break;
|
|
440
|
+
case 'contracts':
|
|
441
|
+
respond(id, contracts);
|
|
442
|
+
break;
|
|
443
|
+
case 'nativeMints':
|
|
444
|
+
respond(id, nativeMints);
|
|
445
|
+
break;
|
|
446
|
+
case 'nativeBurns':
|
|
447
|
+
respond(id, nativeBurns);
|
|
448
|
+
break;
|
|
449
|
+
case 'nativeTransfers':
|
|
450
|
+
respond(id, nativeTransfers);
|
|
451
|
+
break;
|
|
452
|
+
case 'totalBurnAmount':
|
|
453
|
+
respond(id, totalBurnAmount);
|
|
454
|
+
break;
|
|
455
|
+
case 'totalMintAmount':
|
|
456
|
+
respond(id, totalMintAmount);
|
|
457
|
+
break;
|
|
458
|
+
case 'totalTransferAmount':
|
|
459
|
+
respond(id, totalTransferAmount);
|
|
460
|
+
break;
|
|
461
|
+
case 'totalBlocks':
|
|
462
|
+
respond(id, blocks.length);
|
|
463
|
+
break;
|
|
464
|
+
case 'blocks':
|
|
465
|
+
respond(id, input ? blocks.slice(input.from, input.to) : blocks);
|
|
466
|
+
break;
|
|
467
|
+
case 'block':
|
|
468
|
+
respond(id, blocks[input - 1]);
|
|
469
|
+
break;
|
|
470
|
+
case 'lastBlock':
|
|
471
|
+
respond(id, lastBlock);
|
|
472
|
+
break;
|
|
473
|
+
case 'latestTransactions':
|
|
474
|
+
respond(id, latestTransactions);
|
|
475
|
+
break;
|
|
476
|
+
case 'totalTransactions':
|
|
477
|
+
respond(id, totalTransactions);
|
|
478
|
+
break;
|
|
479
|
+
case 'has':
|
|
480
|
+
respond(id, has(input.address));
|
|
481
|
+
break;
|
|
482
|
+
case 'get':
|
|
483
|
+
respond(id, get(input));
|
|
484
|
+
break;
|
|
485
|
+
default:
|
|
486
|
+
console.log(`machine-worker: unsupported taskType: ${type}`);
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
519
489
|
});
|