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