@leofcoin/workers 1.1.1 → 1.2.1

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 (56) hide show
  1. package/dist/bundle/commonjs/block-worker-21ad8152.js +45 -0
  2. package/dist/bundle/commonjs/block-worker.js +4 -5
  3. package/dist/bundle/commonjs/{machine-worker-28ffe20b.js → machine-worker-e96e3082.js} +25 -20
  4. package/dist/bundle/commonjs/machine-worker.js +3 -4
  5. package/dist/bundle/commonjs/{pool-worker-61189fde.js → pool-worker-f450eeff.js} +6 -17
  6. package/dist/bundle/commonjs/pool-worker.js +3 -3
  7. package/dist/bundle/commonjs/{transaction-6a24c91e.js → transaction-d7c6a8ee.js} +2 -2
  8. package/dist/bundle/commonjs/transaction-worker-176a5aaa.js +27 -0
  9. package/dist/bundle/commonjs/transaction-worker.js +3 -3
  10. package/dist/bundle/commonjs/{utils-6a02b156.js → utils-6e881be4.js} +45 -2
  11. package/dist/bundle/commonjs/{index-f77ba3de.js → worker-6ace445f.js} +97 -0
  12. package/dist/bundle/commonjs/workers.js +7 -8
  13. package/dist/bundle/module/block-worker-414860e0.js +43 -0
  14. package/dist/bundle/module/block-worker.js +4 -5
  15. package/dist/bundle/module/{machine-worker-5f6457bd.js → machine-worker-e3020b69.js} +24 -19
  16. package/dist/bundle/module/machine-worker.js +3 -4
  17. package/dist/bundle/module/{pool-worker-012d12c9.js → pool-worker-0d927e3c.js} +6 -17
  18. package/dist/bundle/module/pool-worker.js +3 -3
  19. package/dist/bundle/module/{transaction-e9126d7b.js → transaction-cec98ce8.js} +1 -1
  20. package/dist/bundle/module/transaction-worker-548cd200.js +25 -0
  21. package/dist/bundle/module/transaction-worker.js +3 -3
  22. package/dist/bundle/module/{utils-d240ed61.js → utils-ac8c68e6.js} +44 -2
  23. package/dist/bundle/module/{index-6715242b.js → worker-def77c26.js} +79 -1
  24. package/dist/bundle/module/workers.js +7 -8
  25. package/dist/commonjs/block-worker-60e4cfa3.js +50 -0
  26. package/dist/commonjs/block-worker.js +3 -4
  27. package/dist/commonjs/{machine-worker-5e1073c1.js → machine-worker-16dad57f.js} +28 -18
  28. package/dist/commonjs/machine-worker.js +3 -3
  29. package/dist/commonjs/{pool-worker-3d581327.js → pool-worker-81e8b4c0.js} +9 -15
  30. package/dist/commonjs/pool-worker.js +2 -1
  31. package/dist/commonjs/transaction-worker-8eecc7a1.js +32 -0
  32. package/dist/commonjs/transaction-worker.js +2 -1
  33. package/dist/commonjs/{utils-def2fce8.js → utils-1dc37f72.js} +44 -0
  34. package/dist/commonjs/workers.js +6 -6
  35. package/dist/module/block-worker-f539b1b2.js +44 -0
  36. package/dist/module/block-worker.js +3 -4
  37. package/dist/module/{machine-worker-12c3eda2.js → machine-worker-a2897101.js} +24 -18
  38. package/dist/module/machine-worker.js +3 -3
  39. package/dist/module/{pool-worker-8d3369b4.js → pool-worker-f65671d7.js} +5 -15
  40. package/dist/module/pool-worker.js +2 -1
  41. package/dist/module/{transaction-worker-0f96397e.js → transaction-worker-105deaa8.js} +11 -18
  42. package/dist/module/transaction-worker.js +2 -1
  43. package/dist/module/{utils-96ad7b3e.js → utils-e87bc63d.js} +44 -1
  44. package/dist/module/workers.js +6 -6
  45. package/package.json +22 -22
  46. package/src/block-worker.js +13 -45
  47. package/src/machine-worker.js +25 -18
  48. package/src/pool-worker.js +7 -16
  49. package/src/transaction-worker.js +13 -19
  50. package/dist/bundle/commonjs/block-worker-81701b46.js +0 -116
  51. package/dist/bundle/commonjs/transaction-worker-90623101.js +0 -35
  52. package/dist/bundle/module/block-worker-6f72caf6.js +0 -114
  53. package/dist/bundle/module/transaction-worker-8afddd7e.js +0 -33
  54. package/dist/commonjs/block-worker-9ae351ea.js +0 -116
  55. package/dist/commonjs/transaction-worker-02476daf.js +0 -35
  56. package/dist/module/block-worker-d07e9a9f.js +0 -114
@@ -0,0 +1,44 @@
1
+ import '@leofcoin/codec-format-interface';
2
+ import { B as BigNumber, a as BlockMessage, f as formatBytes } from './utils-e87bc63d.js';
3
+ import EasyWorker from '@vandeurenglenn/easy-worker';
4
+
5
+ const worker = new EasyWorker();
6
+
7
+ globalThis.BigNumber = BigNumber;
8
+
9
+ globalThis.peernet = globalThis.peernet || {};
10
+ globalThis.contracts = {};
11
+
12
+ const run = async (blocks) => {
13
+ blocks = await Promise.all(blocks.map(block => new BlockMessage(block)));
14
+ blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp);
15
+
16
+ blocks = await Promise.all(blocks.map(block => new Promise(async (resolve, reject) => {
17
+ // todo: tx worker or nah?
18
+ const size = block.encoded.length || block.encoded.byteLength;
19
+ console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
20
+ resolve(block);
21
+ })));
22
+ return blocks
23
+ };
24
+
25
+ const tasks = async blocks => {
26
+ globalThis.peernet.codecs = {
27
+ 'block-message': {
28
+ codec: parseInt('626d', 16),
29
+ hashAlg: 'keccak-256'
30
+ }
31
+ };
32
+
33
+ blocks = await run(blocks);
34
+ worker.postMessage(blocks);
35
+ };
36
+
37
+
38
+ worker.onmessage(data => tasks(data));
39
+
40
+ var blockWorker = /*#__PURE__*/Object.freeze({
41
+ __proto__: null
42
+ });
43
+
44
+ export { blockWorker as b };
@@ -1,8 +1,7 @@
1
1
  import '@leofcoin/codec-format-interface';
2
- import './block-worker-d07e9a9f.js';
3
- import './utils-96ad7b3e.js';
4
- import 'child_process';
5
- import 'path';
2
+ import './utils-e87bc63d.js';
3
+ import '@vandeurenglenn/easy-worker';
4
+ import './block-worker-f539b1b2.js';
6
5
  import 'bn.js';
7
6
  import '@ethersproject/bytes';
8
7
  import '@ethersproject/logger';
@@ -1,7 +1,7 @@
1
1
  import { FormatInterface } from '@leofcoin/codec-format-interface';
2
- import { B as BigNumber, f as formatBytes } from './utils-96ad7b3e.js';
3
- import { fork } from 'child_process';
2
+ import { B as BigNumber, a as BlockMessage, f as formatBytes } from './utils-e87bc63d.js';
4
3
  import { join } from 'path';
4
+ import EasyWorker from '@vandeurenglenn/easy-worker';
5
5
 
6
6
  var proto = `
7
7
  message ContractMessage {
@@ -36,6 +36,8 @@ var bytecodes = {
36
36
  validators: validators
37
37
  };
38
38
 
39
+ const worker = new EasyWorker();
40
+
39
41
  const contractFactoryMessage = bytecodes.contractFactory;
40
42
  const nativeTokenMessage = bytecodes.nativeToken;
41
43
  const nameServiceMessage = bytecodes.nameService;
@@ -92,7 +94,6 @@ const execute = async (contract, method, params) => {
92
94
  } else {
93
95
  result = await contracts[contract][method](...params);
94
96
  }
95
-
96
97
  // state.put(result)
97
98
  return result
98
99
  } catch (e) {
@@ -140,24 +141,34 @@ const _init = async ({ contracts, blocks, peerid })=> {
140
141
  return contract
141
142
  }));
142
143
 
143
- const worker = fork(join(__dirname, './block-worker.js'), {serialization: 'advanced'});
144
+ const _worker = await new EasyWorker(join(__dirname, './block-worker.js'), {serialization: 'advanced', type: 'module' });
144
145
  // worker.on('message')
145
- worker.once('message', async (blocks) => {
146
+ _worker.once('message', async (blocks) => {
146
147
  for (const block of blocks) {
147
148
  await Promise.all(block.decoded.transactions.map(async message => {
148
- const {from, to, method, params} = message.decoded;
149
+ const {from, to, method, params} = message;
149
150
  globalThis.msg = createMessage(from);
150
151
 
151
152
  await execute(to, method, params);
152
153
  }));
153
154
  }
154
- process.send({type: 'machine-ready'});
155
- worker.kill();
155
+ let lastBlock;
156
+ if (blocks.length > 0) {
157
+ lastBlock = blocks[blocks.length - 1].decoded;
158
+
159
+ lastBlock = await new BlockMessage(lastBlock);
160
+ lastBlock = {
161
+ ...lastBlock.decoded,
162
+ hash: await lastBlock.hash
163
+ };
164
+ }
165
+ worker.postMessage({type: 'machine-ready', lastBlock });
166
+ _worker.terminate();
156
167
 
157
168
 
158
169
 
159
170
  });
160
- worker.send(blocks);
171
+ worker.postMessage(blocks);
161
172
  };
162
173
 
163
174
  const tasks = async (e) => {
@@ -165,7 +176,7 @@ const tasks = async (e) => {
165
176
  if (e.type === 'init') await _init(e.input);
166
177
  if (e.type === 'get') {
167
178
  const value = await get(e.input.contract, e.input.method, e.input.params);
168
- process.send({
179
+ worker.postMessage({
169
180
  type: 'response',
170
181
  id,
171
182
  value
@@ -174,13 +185,13 @@ const tasks = async (e) => {
174
185
  if (e.type === 'execute') {
175
186
  try {
176
187
  const value = await execute(e.input.contract, e.input.method, e.input.params);
177
- process.send({
188
+ worker.postMessage({
178
189
  type: 'response',
179
190
  id,
180
191
  value
181
192
  });
182
193
  } catch(e) {
183
- process.send({
194
+ worker.postMessage({
184
195
  type: 'executionError',
185
196
  message: e.message,
186
197
  id
@@ -190,12 +201,7 @@ const tasks = async (e) => {
190
201
  }
191
202
  };
192
203
 
193
-
194
- if (globalThis.process) {
195
- process.on('message', tasks);
196
- } else {
197
- onmessage = message => tasks(message.data);
198
- }
204
+ worker.onmessage(data => tasks(data));
199
205
 
200
206
  var machineWorker = /*#__PURE__*/Object.freeze({
201
207
  __proto__: null
@@ -1,8 +1,8 @@
1
- import './machine-worker-12c3eda2.js';
1
+ import './machine-worker-a2897101.js';
2
2
  import '@leofcoin/codec-format-interface';
3
- import './utils-96ad7b3e.js';
4
- import 'child_process';
3
+ import './utils-e87bc63d.js';
5
4
  import 'path';
5
+ import '@vandeurenglenn/easy-worker';
6
6
  import 'bn.js';
7
7
  import '@ethersproject/bytes';
8
8
  import '@ethersproject/logger';
@@ -1,23 +1,18 @@
1
1
  import '@leofcoin/codec-format-interface';
2
2
  import { T as TransactionMessage } from './transaction-d06dc6c4.js';
3
+ import EasyWorker from '@vandeurenglenn/easy-worker';
3
4
 
4
5
  globalThis.peernet = globalThis.peernet || {};
5
6
  globalThis.contracts = {};
6
7
 
8
+ const worker = new EasyWorker();
9
+
7
10
  const tasks = async transactions => {
8
11
 
9
12
  globalThis.peernet.codecs = {
10
- 'contract-message': {
11
- codec: parseInt('63636d', 16),
12
- hashAlg: 'keccak-256'
13
- },
14
13
  'transaction-message': {
15
14
  codec: parseInt('746d', 16),
16
15
  hashAlg: 'keccak-256'
17
- },
18
- 'block-message': {
19
- codec: parseInt('626d', 16),
20
- hashAlg: 'keccak-256'
21
16
  }
22
17
  };
23
18
 
@@ -27,15 +22,10 @@ const tasks = async transactions => {
27
22
  return {...message.decoded, hash: await message.hash, size: message.encoded.length}
28
23
  }));
29
24
 
30
- globalThis.process ? process.send(transactions) : postMessage(transactions);
25
+ worker.postMessage(transactions);
31
26
 
32
27
  };
33
-
34
- if (globalThis.process) {
35
- process.on('message', tasks);
36
- } else {
37
- onmessage = message => tasks(message.data);
38
- }
28
+ worker.onmessage(data => tasks(data));
39
29
 
40
30
  var poolWorker = /*#__PURE__*/Object.freeze({
41
31
  __proto__: null
@@ -1,3 +1,4 @@
1
1
  import '@leofcoin/codec-format-interface';
2
2
  import './transaction-d06dc6c4.js';
3
- import './pool-worker-8d3369b4.js';
3
+ import '@vandeurenglenn/easy-worker';
4
+ import './pool-worker-f65671d7.js';
@@ -1,29 +1,22 @@
1
1
  import '@leofcoin/codec-format-interface';
2
2
  import { T as TransactionMessage } from './transaction-d06dc6c4.js';
3
+ import EasyWorker from '@vandeurenglenn/easy-worker';
3
4
 
4
5
  globalThis.peernet = globalThis.peernet || {};
5
6
  globalThis.contracts = {};
6
-
7
7
 
8
- process.on('message', async (transactions) => {
9
-
10
- globalThis.peernet.codecs = {
11
- 'contract-message': {
12
- codec: parseInt('63636d', 16),
13
- hashAlg: 'keccak-256'
14
- },
15
- 'transaction-message': {
16
- codec: parseInt('746d', 16),
17
- hashAlg: 'keccak-256'
18
- },
19
- 'block-message': {
20
- codec: parseInt('626d', 16),
21
- hashAlg: 'keccak-256'
22
- }
23
- };
8
+ const worker = new EasyWorker();
9
+
10
+ worker.onmessage(async (transactions) => {
11
+ globalThis.peernet.codecs = {
12
+ 'transaction-message': {
13
+ codec: parseInt('746d', 16),
14
+ hashAlg: 'keccak-256'
15
+ }
16
+ };
24
17
  transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)));
25
18
 
26
- process.send(transactions);
19
+ worker.postMessage(transactions);
27
20
  });
28
21
 
29
22
  var transactionWorker = /*#__PURE__*/Object.freeze({
@@ -1,3 +1,4 @@
1
1
  import '@leofcoin/codec-format-interface';
2
2
  import './transaction-d06dc6c4.js';
3
- import './transaction-worker-0f96397e.js';
3
+ import '@vandeurenglenn/easy-worker';
4
+ import './transaction-worker-105deaa8.js';
@@ -1,8 +1,51 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
1
2
  import _BN from 'bn.js';
2
3
  import { isBytes, hexlify, isHexString } from '@ethersproject/bytes';
3
4
  import { Logger } from '@ethersproject/logger';
4
5
  import '@ethersproject/bignumber';
5
6
 
7
+ var proto = `
8
+ message ValidatorMessage {
9
+ required string address = 1;
10
+ required string reward = 2;
11
+ }
12
+
13
+ message Transaction {
14
+ required string hash = 1;
15
+ required uint64 timestamp = 2;
16
+ required string from = 3;
17
+ required string to = 4;
18
+ required uint64 nonce = 5;
19
+ required string method = 6;
20
+ repeated string params = 7;
21
+ }
22
+
23
+ message BlockMessage {
24
+ required uint64 index = 1;
25
+ required string previousHash = 3;
26
+ required uint64 timestamp = 4;
27
+ required uint64 reward = 5;
28
+ required string fees = 6;
29
+ repeated Transaction transactions = 7;
30
+ repeated ValidatorMessage validators = 8;
31
+ }
32
+ `;
33
+
34
+ class BlockMessage extends FormatInterface {
35
+ get keys() {
36
+ return ['index', 'previousHash', 'timestamp', 'reward', 'fees', 'transactions', 'validators']
37
+ }
38
+
39
+ get messageName() {
40
+ return 'BlockMessage'
41
+ }
42
+
43
+ constructor(buffer) {
44
+ const name = 'block-message';
45
+ super(buffer, proto, {name});
46
+ }
47
+ }
48
+
6
49
  const version$1 = "bignumber/5.6.2";
7
50
 
8
51
  var BN = _BN.BN;
@@ -298,4 +341,4 @@ const formatBytes = (bytes, decimals = 2) => {
298
341
  return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
299
342
  };
300
343
 
301
- export { BigNumber as B, formatBytes as f };
344
+ export { BigNumber as B, BlockMessage as a, formatBytes as f };
@@ -1,15 +1,15 @@
1
- import { t as transactionWorker } from './transaction-worker-0f96397e.js';
2
- import { b as blockWorker } from './block-worker-d07e9a9f.js';
3
- import { p as poolWorker } from './pool-worker-8d3369b4.js';
4
- import { m as machineWorker } from './machine-worker-12c3eda2.js';
1
+ import { t as transactionWorker } from './transaction-worker-105deaa8.js';
2
+ import { b as blockWorker } from './block-worker-f539b1b2.js';
3
+ import { p as poolWorker } from './pool-worker-f65671d7.js';
4
+ import { m as machineWorker } from './machine-worker-a2897101.js';
5
5
  import '@leofcoin/codec-format-interface';
6
6
  import './transaction-d06dc6c4.js';
7
- import './utils-96ad7b3e.js';
7
+ import '@vandeurenglenn/easy-worker';
8
+ import './utils-e87bc63d.js';
8
9
  import 'bn.js';
9
10
  import '@ethersproject/bytes';
10
11
  import '@ethersproject/logger';
11
12
  import '@ethersproject/bignumber';
12
- import 'child_process';
13
13
  import 'path';
14
14
 
15
15
  const TransactionWorker = transactionWorker;
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
- {
2
- "name": "@leofcoin/workers",
3
- "version": "1.1.1",
4
- "description": "",
5
- "module": "src/workers.js",
6
- "scripts": {
7
- "build": "rollup -c",
8
- "test": "echo \"Error: no test specified\" && exit 1"
9
- },
10
- "keywords": [],
11
- "author": "",
12
- "license": "MIT",
13
- "devDependencies": {
14
- "@rollup/plugin-commonjs": "^22.0.2",
15
- "@rollup/plugin-json": "^4.1.0",
16
- "@rollup/plugin-node-resolve": "^14.1.0",
17
- "rollup": "^2.79.1"
18
- },
19
- "dependencies": {
20
- "@leofcoin/messages": "^1.2.0"
21
- }
22
- }
1
+ {
2
+ "name": "@leofcoin/workers",
3
+ "version": "1.2.1",
4
+ "description": "",
5
+ "module": "src/workers.js",
6
+ "scripts": {
7
+ "build": "rollup -c",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "MIT",
13
+ "devDependencies": {
14
+ "@rollup/plugin-commonjs": "^22.0.2",
15
+ "@rollup/plugin-json": "^4.1.0",
16
+ "@rollup/plugin-node-resolve": "^14.1.0",
17
+ "rollup": "^2.79.1"
18
+ },
19
+ "dependencies": {
20
+ "@leofcoin/messages": "^1.2.0"
21
+ }
22
+ }
@@ -1,13 +1,9 @@
1
- import { BlockMessage, ContractMessage, TransactionMessage } from '../../messages/src/messages'
2
- import { formatBytes, BigNumber } from '../../utils/src/utils'
3
- import bytecodes from '../../lib/src/bytecodes.json'
4
- import { fork } from 'child_process'
5
- import { join } from 'path'
1
+ import { BlockMessage } from './../../messages/src/messages'
2
+ import { formatBytes, BigNumber } from './../../utils/src/utils'
6
3
 
7
- const contractFactoryMessage = bytecodes.contractFactory
8
- const nativeTokenMessage = bytecodes.nativeToken
9
- const nameServiceMessage = bytecodes.nameService
10
- const validatorsMessage = bytecodes.validators
4
+ import EasyWorker from '@vandeurenglenn/easy-worker'
5
+
6
+ const worker = new EasyWorker()
11
7
 
12
8
  globalThis.BigNumber = BigNumber
13
9
 
@@ -19,42 +15,16 @@ const run = async (blocks) => {
19
15
  blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp)
20
16
 
21
17
  blocks = await Promise.all(blocks.map(block => new Promise(async (resolve, reject) => {
22
- if (globalThis.process) {
23
- const worker = fork(join(__dirname, './transaction-worker.js'), {serialization: 'advanced'})
24
-
25
- worker.once('message', async transactions => {
26
- block.decoded.transactions = transactions
27
- const size = block.encoded.length || block.encoded.byteLength
28
- console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
29
- resolve(block)
30
- })
31
-
32
- worker.send(block.decoded.transactions)
33
- } else {
34
- const worker = new Worker('./workers/transaction-worker.js')
35
- worker.onmessage = async message => {
36
- const transactions = message.data
37
- block.decoded.transactions = transactions
38
- const size = block.encoded.length || block.encoded.byteLength
39
- console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
40
- resolve(block)
41
- }
42
- worker.postMessage(block.decoded.transactions)
43
- }
18
+ // todo: tx worker or nah?
19
+ const size = block.encoded.length || block.encoded.byteLength
20
+ console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
21
+ resolve(block)
44
22
  })))
45
23
  return blocks
46
24
  }
47
25
 
48
26
  const tasks = async blocks => {
49
27
  globalThis.peernet.codecs = {
50
- 'contract-message': {
51
- codec: parseInt('63636d', 16),
52
- hashAlg: 'keccak-256'
53
- },
54
- 'transaction-message': {
55
- codec: parseInt('746d', 16),
56
- hashAlg: 'keccak-256'
57
- },
58
28
  'block-message': {
59
29
  codec: parseInt('626d', 16),
60
30
  hashAlg: 'keccak-256'
@@ -62,11 +32,9 @@ const tasks = async blocks => {
62
32
  }
63
33
 
64
34
  blocks = await run(blocks)
65
- globalThis.process ? process.send(blocks) : postMessage(blocks)
35
+ worker.postMessage(blocks)
66
36
  }
67
37
 
68
- if (globalThis.process) {
69
- process.on('message', tasks)
70
- } else {
71
- onmessage = message => tasks(message.data)
72
- }
38
+
39
+ worker.onmessage(data => tasks(data))
40
+
@@ -1,8 +1,11 @@
1
- import { BlockMessage, ContractMessage, TransactionMessage } from './../../messages/src/messages'
1
+ import { BlockMessage, ContractMessage } from './../../messages/src/messages'
2
2
  import { formatBytes, BigNumber } from './../../utils/src/utils'
3
3
  import bytecodes from './../../lib/src/bytecodes.json'
4
- import { fork } from 'child_process'
5
4
  import { join } from 'path'
5
+
6
+ import EasyWorker from '@vandeurenglenn/easy-worker'
7
+ const worker = new EasyWorker()
8
+
6
9
  const contractFactoryMessage = bytecodes.contractFactory
7
10
  const nativeTokenMessage = bytecodes.nativeToken
8
11
  const nameServiceMessage = bytecodes.nameService
@@ -59,7 +62,6 @@ const execute = async (contract, method, params) => {
59
62
  } else {
60
63
  result = await contracts[contract][method](...params)
61
64
  }
62
-
63
65
  // state.put(result)
64
66
  return result
65
67
  } catch (e) {
@@ -107,24 +109,34 @@ const _init = async ({ contracts, blocks, peerid })=> {
107
109
  return contract
108
110
  }))
109
111
 
110
- const worker = fork(join(__dirname, './block-worker.js'), {serialization: 'advanced'})
112
+ const _worker = await new EasyWorker(join(__dirname, './block-worker.js'), {serialization: 'advanced', type: 'module' })
111
113
  // worker.on('message')
112
- worker.once('message', async (blocks) => {
114
+ _worker.once('message', async (blocks) => {
113
115
  for (const block of blocks) {
114
116
  await Promise.all(block.decoded.transactions.map(async message => {
115
- const {from, to, method, params} = message.decoded
117
+ const {from, to, method, params} = message
116
118
  globalThis.msg = createMessage(from)
117
119
 
118
120
  await execute(to, method, params)
119
121
  }))
120
122
  }
121
- process.send({type: 'machine-ready'})
122
- worker.kill()
123
+ let lastBlock
124
+ if (blocks.length > 0) {
125
+ lastBlock = blocks[blocks.length - 1].decoded
126
+
127
+ lastBlock = await new BlockMessage(lastBlock)
128
+ lastBlock = {
129
+ ...lastBlock.decoded,
130
+ hash: await lastBlock.hash
131
+ }
132
+ }
133
+ worker.postMessage({type: 'machine-ready', lastBlock })
134
+ _worker.terminate()
123
135
 
124
136
 
125
137
 
126
138
  })
127
- worker.send(blocks)
139
+ worker.postMessage(blocks)
128
140
  }
129
141
 
130
142
  const tasks = async (e) => {
@@ -132,7 +144,7 @@ const tasks = async (e) => {
132
144
  if (e.type === 'init') await _init(e.input)
133
145
  if (e.type === 'get') {
134
146
  const value = await get(e.input.contract, e.input.method, e.input.params)
135
- process.send({
147
+ worker.postMessage({
136
148
  type: 'response',
137
149
  id,
138
150
  value
@@ -141,13 +153,13 @@ const tasks = async (e) => {
141
153
  if (e.type === 'execute') {
142
154
  try {
143
155
  const value = await execute(e.input.contract, e.input.method, e.input.params)
144
- process.send({
156
+ worker.postMessage({
145
157
  type: 'response',
146
158
  id,
147
159
  value
148
160
  })
149
161
  } catch(e) {
150
- process.send({
162
+ worker.postMessage({
151
163
  type: 'executionError',
152
164
  message: e.message,
153
165
  id
@@ -157,9 +169,4 @@ const tasks = async (e) => {
157
169
  }
158
170
  }
159
171
 
160
-
161
- if (globalThis.process) {
162
- process.on('message', tasks)
163
- } else {
164
- onmessage = message => tasks(message.data)
165
- }
172
+ worker.onmessage(data => tasks(data))
@@ -1,22 +1,18 @@
1
- import { TransactionMessage } from '../../messages/src/messages'
1
+ import { TransactionMessage } from './../../messages/src/messages'
2
+
3
+ import EasyWorker from '@vandeurenglenn/easy-worker'
2
4
 
3
5
  globalThis.peernet = globalThis.peernet || {}
4
6
  globalThis.contracts = {}
5
7
 
8
+ const worker = new EasyWorker()
9
+
6
10
  const tasks = async transactions => {
7
11
 
8
12
  globalThis.peernet.codecs = {
9
- 'contract-message': {
10
- codec: parseInt('63636d', 16),
11
- hashAlg: 'keccak-256'
12
- },
13
13
  'transaction-message': {
14
14
  codec: parseInt('746d', 16),
15
15
  hashAlg: 'keccak-256'
16
- },
17
- 'block-message': {
18
- codec: parseInt('626d', 16),
19
- hashAlg: 'keccak-256'
20
16
  }
21
17
  }
22
18
 
@@ -26,12 +22,7 @@ const tasks = async transactions => {
26
22
  return {...message.decoded, hash: await message.hash, size: message.encoded.length}
27
23
  }))
28
24
 
29
- globalThis.process ? process.send(transactions) : postMessage(transactions)
25
+ worker.postMessage(transactions)
30
26
 
31
27
  }
32
-
33
- if (globalThis.process) {
34
- process.on('message', tasks)
35
- } else {
36
- onmessage = message => tasks(message.data)
37
- }
28
+ worker.onmessage(data => tasks(data))
@@ -1,26 +1,20 @@
1
- import { TransactionMessage } from '../../messages/src/messages'
1
+ import { TransactionMessage } from './../../messages/src/messages.js'
2
2
 
3
3
  globalThis.peernet = globalThis.peernet || {}
4
4
  globalThis.contracts = {}
5
-
6
5
 
7
- process.on('message', async (transactions) => {
8
-
9
- globalThis.peernet.codecs = {
10
- 'contract-message': {
11
- codec: parseInt('63636d', 16),
12
- hashAlg: 'keccak-256'
13
- },
14
- 'transaction-message': {
15
- codec: parseInt('746d', 16),
16
- hashAlg: 'keccak-256'
17
- },
18
- 'block-message': {
19
- codec: parseInt('626d', 16),
20
- hashAlg: 'keccak-256'
6
+ import EasyWorker from '@vandeurenglenn/easy-worker'
7
+
8
+ const worker = new EasyWorker()
9
+
10
+ worker.onmessage(async (transactions) => {
11
+ globalThis.peernet.codecs = {
12
+ 'transaction-message': {
13
+ codec: parseInt('746d', 16),
14
+ hashAlg: 'keccak-256'
15
+ }
21
16
  }
22
- }
23
17
  transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)))
24
18
 
25
- process.send(transactions)
26
- });
19
+ worker.postMessage(transactions)
20
+ })