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