@leofcoin/workers 1.0.0

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 (53) hide show
  1. package/dist/bundle/commonjs/block-worker-42d9ae8e.js +115 -0
  2. package/dist/bundle/commonjs/block-worker.js +15 -0
  3. package/dist/bundle/commonjs/index-f77ba3de.js +4794 -0
  4. package/dist/bundle/commonjs/machine-worker-8fdd8b95.js +201 -0
  5. package/dist/bundle/commonjs/machine-worker.js +15 -0
  6. package/dist/bundle/commonjs/pool-worker-61189fde.js +46 -0
  7. package/dist/bundle/commonjs/pool-worker.js +14 -0
  8. package/dist/bundle/commonjs/transaction-6a24c91e.js +33 -0
  9. package/dist/bundle/commonjs/transaction-worker-90623101.js +35 -0
  10. package/dist/bundle/commonjs/transaction-worker.js +14 -0
  11. package/dist/bundle/commonjs/utils-6a02b156.js +4096 -0
  12. package/dist/bundle/commonjs/workers.js +30 -0
  13. package/dist/bundle/module/block-worker-efe837c4.js +113 -0
  14. package/dist/bundle/module/block-worker.js +12 -0
  15. package/dist/bundle/module/index-6715242b.js +4780 -0
  16. package/dist/bundle/module/machine-worker-bdb2ec57.js +199 -0
  17. package/dist/bundle/module/machine-worker.js +12 -0
  18. package/dist/bundle/module/pool-worker-012d12c9.js +44 -0
  19. package/dist/bundle/module/pool-worker.js +11 -0
  20. package/dist/bundle/module/transaction-e9126d7b.js +31 -0
  21. package/dist/bundle/module/transaction-worker-8afddd7e.js +33 -0
  22. package/dist/bundle/module/transaction-worker.js +11 -0
  23. package/dist/bundle/module/utils-d240ed61.js +4093 -0
  24. package/dist/bundle/module/workers.js +23 -0
  25. package/dist/commonjs/block-worker-d849f917.js +115 -0
  26. package/dist/commonjs/block-worker.js +11 -0
  27. package/dist/commonjs/machine-worker-550c3abe.js +201 -0
  28. package/dist/commonjs/machine-worker.js +11 -0
  29. package/dist/commonjs/pool-worker-3d581327.js +46 -0
  30. package/dist/commonjs/pool-worker.js +6 -0
  31. package/dist/commonjs/transaction-220a3a15.js +33 -0
  32. package/dist/commonjs/transaction-worker-02476daf.js +35 -0
  33. package/dist/commonjs/transaction-worker.js +6 -0
  34. package/dist/commonjs/utils-def2fce8.js +308 -0
  35. package/dist/commonjs/workers.js +26 -0
  36. package/dist/module/block-worker-f5b28240.js +113 -0
  37. package/dist/module/block-worker.js +8 -0
  38. package/dist/module/machine-worker-8054fa84.js +199 -0
  39. package/dist/module/machine-worker.js +8 -0
  40. package/dist/module/pool-worker-8d3369b4.js +44 -0
  41. package/dist/module/pool-worker.js +3 -0
  42. package/dist/module/transaction-d06dc6c4.js +31 -0
  43. package/dist/module/transaction-worker-0f96397e.js +33 -0
  44. package/dist/module/transaction-worker.js +3 -0
  45. package/dist/module/utils-96ad7b3e.js +301 -0
  46. package/dist/module/workers.js +19 -0
  47. package/package.json +22 -0
  48. package/rollup.config.js +40 -0
  49. package/src/block-worker.js +71 -0
  50. package/src/machine-worker.js +160 -0
  51. package/src/pool-worker.js +37 -0
  52. package/src/transaction-worker.js +26 -0
  53. package/src/workers.js +9 -0
@@ -0,0 +1,301 @@
1
+ import _BN from 'bn.js';
2
+ import { isBytes, hexlify, isHexString } from '@ethersproject/bytes';
3
+ import { Logger } from '@ethersproject/logger';
4
+ import '@ethersproject/bignumber';
5
+
6
+ const version$1 = "bignumber/5.6.2";
7
+
8
+ var BN = _BN.BN;
9
+ const logger = new Logger(version$1);
10
+ const _constructorGuard = {};
11
+ const MAX_SAFE = 0x1fffffffffffff;
12
+ // Only warn about passing 10 into radix once
13
+ let _warnedToStringRadix = false;
14
+ class BigNumber {
15
+ constructor(constructorGuard, hex) {
16
+ if (constructorGuard !== _constructorGuard) {
17
+ logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
18
+ operation: "new (BigNumber)"
19
+ });
20
+ }
21
+ this._hex = hex;
22
+ this._isBigNumber = true;
23
+ Object.freeze(this);
24
+ }
25
+ fromTwos(value) {
26
+ return toBigNumber(toBN(this).fromTwos(value));
27
+ }
28
+ toTwos(value) {
29
+ return toBigNumber(toBN(this).toTwos(value));
30
+ }
31
+ abs() {
32
+ if (this._hex[0] === "-") {
33
+ return BigNumber.from(this._hex.substring(1));
34
+ }
35
+ return this;
36
+ }
37
+ add(other) {
38
+ return toBigNumber(toBN(this).add(toBN(other)));
39
+ }
40
+ sub(other) {
41
+ return toBigNumber(toBN(this).sub(toBN(other)));
42
+ }
43
+ div(other) {
44
+ const o = BigNumber.from(other);
45
+ if (o.isZero()) {
46
+ throwFault("division-by-zero", "div");
47
+ }
48
+ return toBigNumber(toBN(this).div(toBN(other)));
49
+ }
50
+ mul(other) {
51
+ return toBigNumber(toBN(this).mul(toBN(other)));
52
+ }
53
+ mod(other) {
54
+ const value = toBN(other);
55
+ if (value.isNeg()) {
56
+ throwFault("division-by-zero", "mod");
57
+ }
58
+ return toBigNumber(toBN(this).umod(value));
59
+ }
60
+ pow(other) {
61
+ const value = toBN(other);
62
+ if (value.isNeg()) {
63
+ throwFault("negative-power", "pow");
64
+ }
65
+ return toBigNumber(toBN(this).pow(value));
66
+ }
67
+ and(other) {
68
+ const value = toBN(other);
69
+ if (this.isNegative() || value.isNeg()) {
70
+ throwFault("unbound-bitwise-result", "and");
71
+ }
72
+ return toBigNumber(toBN(this).and(value));
73
+ }
74
+ or(other) {
75
+ const value = toBN(other);
76
+ if (this.isNegative() || value.isNeg()) {
77
+ throwFault("unbound-bitwise-result", "or");
78
+ }
79
+ return toBigNumber(toBN(this).or(value));
80
+ }
81
+ xor(other) {
82
+ const value = toBN(other);
83
+ if (this.isNegative() || value.isNeg()) {
84
+ throwFault("unbound-bitwise-result", "xor");
85
+ }
86
+ return toBigNumber(toBN(this).xor(value));
87
+ }
88
+ mask(value) {
89
+ if (this.isNegative() || value < 0) {
90
+ throwFault("negative-width", "mask");
91
+ }
92
+ return toBigNumber(toBN(this).maskn(value));
93
+ }
94
+ shl(value) {
95
+ if (this.isNegative() || value < 0) {
96
+ throwFault("negative-width", "shl");
97
+ }
98
+ return toBigNumber(toBN(this).shln(value));
99
+ }
100
+ shr(value) {
101
+ if (this.isNegative() || value < 0) {
102
+ throwFault("negative-width", "shr");
103
+ }
104
+ return toBigNumber(toBN(this).shrn(value));
105
+ }
106
+ eq(other) {
107
+ return toBN(this).eq(toBN(other));
108
+ }
109
+ lt(other) {
110
+ return toBN(this).lt(toBN(other));
111
+ }
112
+ lte(other) {
113
+ return toBN(this).lte(toBN(other));
114
+ }
115
+ gt(other) {
116
+ return toBN(this).gt(toBN(other));
117
+ }
118
+ gte(other) {
119
+ return toBN(this).gte(toBN(other));
120
+ }
121
+ isNegative() {
122
+ return (this._hex[0] === "-");
123
+ }
124
+ isZero() {
125
+ return toBN(this).isZero();
126
+ }
127
+ toNumber() {
128
+ try {
129
+ return toBN(this).toNumber();
130
+ }
131
+ catch (error) {
132
+ throwFault("overflow", "toNumber", this.toString());
133
+ }
134
+ return null;
135
+ }
136
+ toBigInt() {
137
+ try {
138
+ return BigInt(this.toString());
139
+ }
140
+ catch (e) { }
141
+ return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
142
+ value: this.toString()
143
+ });
144
+ }
145
+ toString() {
146
+ // Lots of people expect this, which we do not support, so check (See: #889)
147
+ if (arguments.length > 0) {
148
+ if (arguments[0] === 10) {
149
+ if (!_warnedToStringRadix) {
150
+ _warnedToStringRadix = true;
151
+ logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
152
+ }
153
+ }
154
+ else if (arguments[0] === 16) {
155
+ logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
156
+ }
157
+ else {
158
+ logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
159
+ }
160
+ }
161
+ return toBN(this).toString(10);
162
+ }
163
+ toHexString() {
164
+ return this._hex;
165
+ }
166
+ toJSON(key) {
167
+ return { type: "BigNumber", hex: this.toHexString() };
168
+ }
169
+ static from(value) {
170
+ if (value instanceof BigNumber) {
171
+ return value;
172
+ }
173
+ if (typeof (value) === "string") {
174
+ if (value.match(/^-?0x[0-9a-f]+$/i)) {
175
+ return new BigNumber(_constructorGuard, toHex(value));
176
+ }
177
+ if (value.match(/^-?[0-9]+$/)) {
178
+ return new BigNumber(_constructorGuard, toHex(new BN(value)));
179
+ }
180
+ return logger.throwArgumentError("invalid BigNumber string", "value", value);
181
+ }
182
+ if (typeof (value) === "number") {
183
+ if (value % 1) {
184
+ throwFault("underflow", "BigNumber.from", value);
185
+ }
186
+ if (value >= MAX_SAFE || value <= -MAX_SAFE) {
187
+ throwFault("overflow", "BigNumber.from", value);
188
+ }
189
+ return BigNumber.from(String(value));
190
+ }
191
+ const anyValue = value;
192
+ if (typeof (anyValue) === "bigint") {
193
+ return BigNumber.from(anyValue.toString());
194
+ }
195
+ if (isBytes(anyValue)) {
196
+ return BigNumber.from(hexlify(anyValue));
197
+ }
198
+ if (anyValue) {
199
+ // Hexable interface (takes priority)
200
+ if (anyValue.toHexString) {
201
+ const hex = anyValue.toHexString();
202
+ if (typeof (hex) === "string") {
203
+ return BigNumber.from(hex);
204
+ }
205
+ }
206
+ else {
207
+ // For now, handle legacy JSON-ified values (goes away in v6)
208
+ let hex = anyValue._hex;
209
+ // New-form JSON
210
+ if (hex == null && anyValue.type === "BigNumber") {
211
+ hex = anyValue.hex;
212
+ }
213
+ if (typeof (hex) === "string") {
214
+ if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
215
+ return BigNumber.from(hex);
216
+ }
217
+ }
218
+ }
219
+ }
220
+ return logger.throwArgumentError("invalid BigNumber value", "value", value);
221
+ }
222
+ static isBigNumber(value) {
223
+ return !!(value && value._isBigNumber);
224
+ }
225
+ }
226
+ // Normalize the hex string
227
+ function toHex(value) {
228
+ // For BN, call on the hex string
229
+ if (typeof (value) !== "string") {
230
+ return toHex(value.toString(16));
231
+ }
232
+ // If negative, prepend the negative sign to the normalized positive value
233
+ if (value[0] === "-") {
234
+ // Strip off the negative sign
235
+ value = value.substring(1);
236
+ // Cannot have multiple negative signs (e.g. "--0x04")
237
+ if (value[0] === "-") {
238
+ logger.throwArgumentError("invalid hex", "value", value);
239
+ }
240
+ // Call toHex on the positive component
241
+ value = toHex(value);
242
+ // Do not allow "-0x00"
243
+ if (value === "0x00") {
244
+ return value;
245
+ }
246
+ // Negate the value
247
+ return "-" + value;
248
+ }
249
+ // Add a "0x" prefix if missing
250
+ if (value.substring(0, 2) !== "0x") {
251
+ value = "0x" + value;
252
+ }
253
+ // Normalize zero
254
+ if (value === "0x") {
255
+ return "0x00";
256
+ }
257
+ // Make the string even length
258
+ if (value.length % 2) {
259
+ value = "0x0" + value.substring(2);
260
+ }
261
+ // Trim to smallest even-length string
262
+ while (value.length > 4 && value.substring(0, 4) === "0x00") {
263
+ value = "0x" + value.substring(4);
264
+ }
265
+ return value;
266
+ }
267
+ function toBigNumber(value) {
268
+ return BigNumber.from(toHex(value));
269
+ }
270
+ function toBN(value) {
271
+ const hex = BigNumber.from(value).toHexString();
272
+ if (hex[0] === "-") {
273
+ return (new BN("-" + hex.substring(3), 16));
274
+ }
275
+ return new BN(hex.substring(2), 16);
276
+ }
277
+ function throwFault(fault, operation, value) {
278
+ const params = { fault: fault, operation: operation };
279
+ if (value != null) {
280
+ params.value = value;
281
+ }
282
+ return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
283
+ }
284
+
285
+ const version = "units/5.6.1";
286
+
287
+ new Logger(version);
288
+
289
+ const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
290
+
291
+ const formatBytes = (bytes, decimals = 2) => {
292
+ if (bytes === 0) return '0 Bytes';
293
+ if (decimals < 0) decimals = 0;
294
+
295
+ const k = 1024;
296
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
297
+
298
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
299
+ };
300
+
301
+ export { BigNumber as B, formatBytes as f };
@@ -0,0 +1,19 @@
1
+ import { t as transactionWorker } from './transaction-worker-0f96397e.js';
2
+ import { b as blockWorker } from './block-worker-f5b28240.js';
3
+ import { p as poolWorker } from './pool-worker-8d3369b4.js';
4
+ import { m as machineWorker } from './machine-worker-8054fa84.js';
5
+ import '@leofcoin/codec-format-interface';
6
+ import './transaction-d06dc6c4.js';
7
+ import './utils-96ad7b3e.js';
8
+ import 'bn.js';
9
+ import '@ethersproject/bytes';
10
+ import '@ethersproject/logger';
11
+ import '@ethersproject/bignumber';
12
+ import 'child_process';
13
+
14
+ const TransactionWorker = transactionWorker;
15
+ const BlockWorker = blockWorker;
16
+ const PoolWorker = poolWorker;
17
+ const MachineWorker = machineWorker;
18
+
19
+ export { BlockWorker, MachineWorker, PoolWorker, TransactionWorker };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@leofcoin/workers",
3
+ "version": "1.0.0",
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
+ }
@@ -0,0 +1,40 @@
1
+ import { execSync } from 'child_process'
2
+ import json from '@rollup/plugin-json'
3
+ import resolve from '@rollup/plugin-node-resolve'
4
+
5
+ import commonjs from '@rollup/plugin-commonjs'
6
+ try {
7
+ execSync(`rm -rf ./dist/**`)
8
+
9
+ } catch (e) {
10
+
11
+ }
12
+ export default [{
13
+ input: ['./src/workers.js', './src/transaction-worker.js', './src/block-worker.js', './src/pool-worker.js', './src/machine-worker.js'],
14
+
15
+ plugins: [
16
+ json()
17
+ ],
18
+ output: [{
19
+ dir: './dist/commonjs',
20
+ format: 'cjs'
21
+ }, {
22
+ dir: './dist/module',
23
+ format: 'es'
24
+ }]
25
+ }, {
26
+ input: ['./src/workers.js', './src/transaction-worker.js', './src/block-worker.js', './src/pool-worker.js', './src/machine-worker.js'],
27
+
28
+ plugins: [
29
+ json(),
30
+ commonjs(),
31
+ resolve()
32
+ ],
33
+ output: [{
34
+ dir: './dist/bundle/commonjs',
35
+ format: 'cjs'
36
+ }, {
37
+ dir: './dist/bundle/module',
38
+ format: 'es'
39
+ }]
40
+ }]
@@ -0,0 +1,71 @@
1
+ import { BlockMessage } 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
+
6
+ const contractFactoryMessage = bytecodes.contractFactory
7
+ const nativeTokenMessage = bytecodes.nativeToken
8
+ const nameServiceMessage = bytecodes.nameService
9
+ const validatorsMessage = bytecodes.validators
10
+
11
+ globalThis.BigNumber = BigNumber
12
+
13
+ globalThis.peernet = globalThis.peernet || {}
14
+ globalThis.contracts = {}
15
+
16
+ const run = async (blocks) => {
17
+ blocks = await Promise.all(blocks.map(block => new BlockMessage(block)))
18
+ blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp)
19
+
20
+ blocks = await Promise.all(blocks.map(block => new Promise(async (resolve, reject) => {
21
+ if (globalThis.process) {
22
+ const worker = fork('./workers/transaction-worker.js', {serialization: 'advanced'})
23
+
24
+ worker.once('message', async transactions => {
25
+ block.decoded.transactions = transactions
26
+ const size = block.encoded.length || block.encoded.byteLength
27
+ console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
28
+ resolve(block)
29
+ })
30
+
31
+ worker.send(block.decoded.transactions)
32
+ } else {
33
+ const worker = new Worker('./workers/transaction-worker.js')
34
+ worker.onmessage = async message => {
35
+ const transactions = message.data
36
+ block.decoded.transactions = transactions
37
+ const size = block.encoded.length || block.encoded.byteLength
38
+ console.log(`loaded block: ${await block.hash} @${block.decoded.index} ${formatBytes(size)}`);
39
+ resolve(block)
40
+ }
41
+ worker.postMessage(block.decoded.transactions)
42
+ }
43
+ })))
44
+ return blocks
45
+ }
46
+
47
+ const tasks = async blocks => {
48
+ globalThis.peernet.codecs = {
49
+ 'contract-message': {
50
+ codec: parseInt('63636d', 16),
51
+ hashAlg: 'keccak-256'
52
+ },
53
+ 'transaction-message': {
54
+ codec: parseInt('746d', 16),
55
+ hashAlg: 'keccak-256'
56
+ },
57
+ 'block-message': {
58
+ codec: parseInt('626d', 16),
59
+ hashAlg: 'keccak-256'
60
+ }
61
+ }
62
+
63
+ blocks = await run(blocks)
64
+ globalThis.process ? process.send(blocks) : postMessage(blocks)
65
+ }
66
+
67
+ if (globalThis.process) {
68
+ process.on('message', tasks)
69
+ } else {
70
+ onmessage = message => tasks(message.data)
71
+ }
@@ -0,0 +1,160 @@
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
+ const contractFactoryMessage = bytecodes.contractFactory
6
+ const nativeTokenMessage = bytecodes.nativeToken
7
+ const nameServiceMessage = bytecodes.nameService
8
+ const validatorsMessage = bytecodes.validators
9
+
10
+ globalThis.BigNumber = BigNumber
11
+
12
+ globalThis.peernet = globalThis.peernet || {}
13
+ globalThis.contracts = {}
14
+
15
+ const get = (contract, method, params) => {
16
+ let result
17
+ if (params?.length > 0) {
18
+ result = contracts[contract][method](...params)
19
+ } else {
20
+ result = contracts[contract][method]
21
+ }
22
+ return result
23
+ }
24
+
25
+ const runContract = async (contractMessage) => {
26
+ const params = contractMessage.decoded.constructorParameters
27
+ try {
28
+
29
+ const func = new Function(contractMessage.decoded.contract)
30
+ const Contract = func()
31
+
32
+ globalThis.msg = createMessage(contractMessage.decoded.creator)
33
+ // globalThis.msg = {sender: contractMessage.decoded.creator}
34
+ contracts[await contractMessage.hash] = await new Contract(...params)
35
+ process.send({
36
+ type: 'debug',
37
+ messages: [
38
+ `loaded contract: ${await contractMessage.hash}`,
39
+ `size: ${formatBytes(contractMessage.encoded.length)}`
40
+ ]
41
+ })
42
+ } catch (e) {
43
+ console.log(e);
44
+ process.send({
45
+ type: 'contractError',
46
+ hash: await contractMessage.hash
47
+ })
48
+ }
49
+ }
50
+
51
+ const execute = async (contract, method, params) => {
52
+ try {
53
+ let result
54
+ // don't execute the method on a proxy
55
+ if (contracts[contract].fallback) {
56
+ result = await contracts[contract].fallback(method, params)
57
+ } else {
58
+ result = await contracts[contract][method](...params)
59
+ }
60
+
61
+ // state.put(result)
62
+ return result
63
+ } catch (e) {
64
+ throw e
65
+ }
66
+ }
67
+
68
+
69
+ const createMessage = (sender = globalThis.peerid) => {
70
+ return {
71
+ sender,
72
+ call: execute,
73
+ staticCall: get
74
+ }
75
+ }
76
+
77
+ const _init = async ({ contracts, blocks, peerid })=> {
78
+ globalThis.peerid = peerid
79
+ contracts = [
80
+ contractFactoryMessage,
81
+ nativeTokenMessage,
82
+ nameServiceMessage,
83
+ validatorsMessage
84
+ ]
85
+
86
+ contracts = await Promise.all(contracts.map(async contract => {
87
+ contract = await new ContractMessage(new Uint8Array(contract.split(',')))
88
+ await runContract(contract)
89
+ return contract
90
+ }))
91
+
92
+ const worker = fork('./workers/block-worker.js', {serialization: 'advanced'})
93
+ // worker.on('message')
94
+ worker.once('message', async (blocks) => {
95
+ for (const block of blocks) {
96
+ await Promise.all(block.decoded.transactions.map(async message => {
97
+ const {from, to, method, params} = message.decoded
98
+ globalThis.msg = createMessage(from)
99
+
100
+ await execute(to, method, params)
101
+ }))
102
+ }
103
+ process.send({type: 'machine-ready'})
104
+ worker.kill()
105
+
106
+
107
+
108
+ })
109
+ worker.send(blocks)
110
+ }
111
+
112
+ const tasks = async (e) => {
113
+ globalThis.peernet.codecs = {
114
+ 'contract-message': {
115
+ codec: parseInt('63636d', 16),
116
+ hashAlg: 'keccak-256'
117
+ },
118
+ 'transaction-message': {
119
+ codec: parseInt('746d', 16),
120
+ hashAlg: 'keccak-256'
121
+ },
122
+ 'block-message': {
123
+ codec: parseInt('626d', 16),
124
+ hashAlg: 'keccak-256'
125
+ }
126
+ }
127
+ if (e.type === 'init') await _init(e.input)
128
+ if (e.type === 'get') {
129
+ const value = await get(e.input.contract, e.input.method, e.input.params)
130
+ process.send({
131
+ type: 'response',
132
+ id: e.id,
133
+ value
134
+ })
135
+ }
136
+ if (e.type === 'execute') {
137
+ try {
138
+ const value = await execute(e.input.contract, e.input.method, e.input.params)
139
+ process.send({
140
+ type: 'response',
141
+ id: e.id,
142
+ value
143
+ })
144
+ } catch(e) {
145
+ process.send({
146
+ type: 'executionError',
147
+ message: e,
148
+ id: e.id
149
+
150
+ })
151
+ }
152
+ }
153
+ }
154
+
155
+
156
+ if (globalThis.process) {
157
+ process.on('message', tasks)
158
+ } else {
159
+ onmessage = message => tasks(message.data)
160
+ }
@@ -0,0 +1,37 @@
1
+ import { TransactionMessage } from '../../messages/src/messages'
2
+
3
+ globalThis.peernet = globalThis.peernet || {}
4
+ globalThis.contracts = {}
5
+
6
+ const tasks = async transactions => {
7
+
8
+ globalThis.peernet.codecs = {
9
+ 'contract-message': {
10
+ codec: parseInt('63636d', 16),
11
+ hashAlg: 'keccak-256'
12
+ },
13
+ 'transaction-message': {
14
+ codec: parseInt('746d', 16),
15
+ hashAlg: 'keccak-256'
16
+ },
17
+ 'block-message': {
18
+ codec: parseInt('626d', 16),
19
+ hashAlg: 'keccak-256'
20
+ }
21
+ }
22
+
23
+ transactions = await Promise.all(transactions.map(async message => {
24
+ message = await new TransactionMessage(message)
25
+
26
+ return {...message.decoded, hash: await message.hash, size: message.encoded.length}
27
+ }))
28
+
29
+ globalThis.process ? process.send(transactions) : postMessage(transactions)
30
+
31
+ }
32
+
33
+ if (globalThis.process) {
34
+ process.on('message', tasks)
35
+ } else {
36
+ onmessage = message => tasks(message.data)
37
+ }
@@ -0,0 +1,26 @@
1
+ import { TransactionMessage } from '../../messages/src/messages'
2
+
3
+ globalThis.peernet = globalThis.peernet || {}
4
+ globalThis.contracts = {}
5
+
6
+
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'
21
+ }
22
+ }
23
+ transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)))
24
+
25
+ process.send(transactions)
26
+ });
package/src/workers.js ADDED
@@ -0,0 +1,9 @@
1
+ import * as transactionWorker from './transaction-worker'
2
+ import * as blockWorker from './block-worker'
3
+ import * as poolWorker from './pool-worker'
4
+ import * as machineWorker from './machine-worker'
5
+
6
+ export const TransactionWorker = transactionWorker
7
+ export const BlockWorker = blockWorker
8
+ export const PoolWorker = poolWorker
9
+ export const MachineWorker = machineWorker