@leofcoin/chain 1.1.10 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist/workers/block-worker.js → block-worker.js} +0 -0
- package/demo/chain.browser.js +26998 -21730
- package/demo/generate-account.browser.js +50 -0
- package/demo/index.html +8 -1
- package/demo/messages.browser.js +328 -0
- package/demo/multi-wallet.browser.js +15 -0
- package/demo/node.browser.js +7957 -29565
- package/demo/pako.browser.js +670 -501
- package/demo/peernet-swarm.browser.js +828 -0
- package/demo/storage.browser.js +3724 -0
- package/demo/{865.machine-worker.js → workers/865.js} +0 -0
- package/demo/{machine-worker.js → workers/block-worker.js} +1231 -5655
- package/demo/workers/machine-worker.js +2501 -6190
- package/demo/workers/pool-worker.js +8503 -0
- package/demo/workers/transaction-worker.js +8495 -0
- package/dist/{865.machine-worker.js → browser/workers/865.js} +0 -0
- package/dist/browser/workers/block-worker.js +13199 -0
- package/dist/{machine-worker.js → browser/workers/machine-worker.js} +2501 -6190
- package/dist/browser/workers/pool-worker.js +8503 -0
- package/dist/browser/workers/transaction-worker.js +8495 -0
- package/dist/chain.browser.js +26998 -21730
- package/dist/chain.js +78 -51
- package/dist/generate-account.browser.js +50 -0
- package/dist/messages.browser.js +328 -0
- package/dist/module/chain.js +78 -51
- package/dist/module/workers/block-worker.js +93 -0
- package/dist/module/workers/machine-worker.js +55 -29
- package/dist/module/workers/pool-worker.js +55 -0
- package/dist/module/workers/transaction-worker.js +47 -0
- package/dist/multi-wallet.browser.js +15 -0
- package/dist/node.browser.js +7957 -29565
- package/dist/pako.browser.js +6900 -0
- package/dist/peernet-swarm.browser.js +828 -0
- package/dist/storage.browser.js +3724 -0
- package/dist/workers/machine-worker.js +1 -1
- package/dist/wrtc.browser.js +28 -0
- package/package.json +3 -3
- package/rollup.config.js +31 -1
- package/src/chain.js +73 -47
- package/src/machine.js +6 -2
- package/test/chain.js +18 -3
- package/webpack.config.js +8 -3
- package/demo/chain.js +0 -1209
- package/demo/node.js +0 -1
package/dist/module/chain.js
CHANGED
|
@@ -53,7 +53,7 @@ const formatBytes = (bytes, decimals = 2) => {
|
|
|
53
53
|
class Machine {
|
|
54
54
|
#contracts = {}
|
|
55
55
|
#nonces = {}
|
|
56
|
-
lastBlock = {}
|
|
56
|
+
lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
|
|
57
57
|
|
|
58
58
|
constructor() {
|
|
59
59
|
return this.#init()
|
|
@@ -84,7 +84,7 @@ class Machine {
|
|
|
84
84
|
break
|
|
85
85
|
case 'machine-ready':
|
|
86
86
|
this.lastBlock = data.lastBlock;
|
|
87
|
-
pubsub.publish('machine.ready');
|
|
87
|
+
pubsub.publish('machine.ready', true);
|
|
88
88
|
break
|
|
89
89
|
case 'response':
|
|
90
90
|
pubsub.publish(data.id, data.value);
|
|
@@ -205,6 +205,10 @@ class Machine {
|
|
|
205
205
|
return contractStore.delete(hash)
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
/**
|
|
209
|
+
*
|
|
210
|
+
* @returns Promise
|
|
211
|
+
*/
|
|
208
212
|
async deleteAll() {
|
|
209
213
|
let hashes = await contractStore.get();
|
|
210
214
|
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
@@ -480,20 +484,41 @@ class Chain {
|
|
|
480
484
|
// handle native contracts
|
|
481
485
|
}
|
|
482
486
|
|
|
487
|
+
promiseRequests(promises) {
|
|
488
|
+
return new Promise(async (resolve, reject) => {
|
|
489
|
+
const timeout = setTimeout(() => {
|
|
490
|
+
resolve([{index: 0, hash: '0x0'}]);
|
|
491
|
+
debug('sync timed out');
|
|
492
|
+
}, 10000);
|
|
493
|
+
|
|
494
|
+
promises = await Promise.allSettled(promises);
|
|
495
|
+
promises = promises.filter(({status}) => status === 'fulfilled');
|
|
496
|
+
clearTimeout(timeout);
|
|
497
|
+
|
|
498
|
+
promises = promises.map(({value}) => new peernet.protos['peernet-response'](value));
|
|
499
|
+
promises = await Promise.all(promises);
|
|
500
|
+
promises = promises.map(node => node.decoded.response);
|
|
501
|
+
resolve(promises);
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
sync() {
|
|
507
|
+
return this.#sync()
|
|
508
|
+
}
|
|
509
|
+
|
|
483
510
|
async #sync() {
|
|
484
511
|
let promises = [];
|
|
512
|
+
|
|
513
|
+
let data = await new peernet.protos['peernet-request']({request: 'lastBlock'});
|
|
514
|
+
const node = await peernet.prepareMessage(data);
|
|
515
|
+
|
|
485
516
|
for (const peer of peernet.connections) {
|
|
486
|
-
if (peer.peerId !== this.id) {
|
|
487
|
-
let data = await new peernet.protos['peernet-request']({request: 'lastBlock'});
|
|
488
|
-
const node = await peernet.prepareMessage(data);
|
|
517
|
+
if (peer.connected && peer.readyState === 'open' && peer.peerId !== this.id) {
|
|
489
518
|
promises.push(peer.request(node.encoded));
|
|
490
|
-
}
|
|
519
|
+
} else if (!peer.connected || peer.readyState !== 'open') ;
|
|
491
520
|
}
|
|
492
|
-
promises = await
|
|
493
|
-
promises = promises.filter(({status}) => status === 'fulfilled');
|
|
494
|
-
promises = promises.map(({value}) => new peernet.protos['peernet-response'](value));
|
|
495
|
-
promises = await Promise.all(promises);
|
|
496
|
-
promises = promises.map(node => node.decoded.response);
|
|
521
|
+
promises = await this.promiseRequests(promises);
|
|
497
522
|
promises = promises.reduce((set, value) => {
|
|
498
523
|
|
|
499
524
|
if (value.index > set.index) {
|
|
@@ -504,8 +529,7 @@ class Chain {
|
|
|
504
529
|
}, {index: 0, hash: '0x0'});
|
|
505
530
|
// get lastblock
|
|
506
531
|
if (promises.hash && promises.hash !== '0x0') {
|
|
507
|
-
|
|
508
|
-
console.log({localBlock});
|
|
532
|
+
await peernet.get(promises.hash);
|
|
509
533
|
}
|
|
510
534
|
|
|
511
535
|
|
|
@@ -520,22 +544,7 @@ class Chain {
|
|
|
520
544
|
|
|
521
545
|
this.#machine = await new Machine();
|
|
522
546
|
this.utils = { BigNumber, formatUnits, parseUnits };
|
|
523
|
-
|
|
524
|
-
await peernet.addRequestHandler('bw-request-message', () => {
|
|
525
|
-
|
|
526
|
-
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
530
|
-
|
|
531
|
-
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
532
|
-
|
|
533
|
-
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
534
|
-
|
|
535
|
-
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
536
|
-
|
|
537
|
-
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
538
|
-
|
|
547
|
+
|
|
539
548
|
try {
|
|
540
549
|
let localBlock;
|
|
541
550
|
try {
|
|
@@ -545,17 +554,11 @@ class Chain {
|
|
|
545
554
|
localBlock = await chainStore.get('lastBlock');
|
|
546
555
|
}
|
|
547
556
|
localBlock = new TextDecoder().decode(localBlock);
|
|
548
|
-
|
|
549
557
|
if (localBlock && localBlock !== '0x0') {
|
|
550
|
-
localBlock = await peernet.get(localBlock);
|
|
558
|
+
localBlock = await peernet.get(localBlock, 'block');
|
|
551
559
|
localBlock = await new BlockMessage(localBlock);
|
|
552
560
|
this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
|
|
553
|
-
} else
|
|
554
|
-
// todo remove when network is running
|
|
555
|
-
// recovering chain (not needed if multiple peers are online)
|
|
556
|
-
this.#lastBlock = this.#machine.lastBlock;
|
|
557
|
-
await chainStore.put('lastBlock', this.#lastBlock.hash);
|
|
558
|
-
} else {
|
|
561
|
+
} else {
|
|
559
562
|
await this.#sync();
|
|
560
563
|
}
|
|
561
564
|
} catch (e) {
|
|
@@ -564,8 +567,25 @@ class Chain {
|
|
|
564
567
|
|
|
565
568
|
// this.#setup()
|
|
566
569
|
}
|
|
570
|
+
|
|
571
|
+
await peernet.addRequestHandler('bw-request-message', () => {
|
|
572
|
+
|
|
573
|
+
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
577
|
+
|
|
578
|
+
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
579
|
+
|
|
580
|
+
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
581
|
+
|
|
582
|
+
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
583
|
+
|
|
584
|
+
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
585
|
+
|
|
586
|
+
|
|
567
587
|
// load local blocks
|
|
568
|
-
|
|
588
|
+
await this.resolveBlocks();
|
|
569
589
|
return this
|
|
570
590
|
}
|
|
571
591
|
|
|
@@ -583,7 +603,7 @@ class Chain {
|
|
|
583
603
|
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
584
604
|
let lastBlock = response.decoded.response;
|
|
585
605
|
|
|
586
|
-
if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
|
|
606
|
+
if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
|
|
587
607
|
// TODO: check if valid
|
|
588
608
|
const localIndex = this.lastBlock ? this.lastBlock.index : 0;
|
|
589
609
|
const index = lastBlock.index;
|
|
@@ -625,7 +645,8 @@ async resolveBlock(hash) {
|
|
|
625
645
|
if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block');
|
|
626
646
|
const size = block.encoded.length || block.encoded.byteLength;
|
|
627
647
|
block = {...block.decoded, hash};
|
|
628
|
-
this.#blocks[block.index
|
|
648
|
+
if (this.#blocks[block.index]) throw `invalid block ${hash} @${block.index}`
|
|
649
|
+
this.#blocks[block.index] = block;
|
|
629
650
|
console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
|
|
630
651
|
if (block.previousHash !== '0x0') {
|
|
631
652
|
return this.resolveBlock(block.previousHash)
|
|
@@ -634,11 +655,11 @@ async resolveBlock(hash) {
|
|
|
634
655
|
|
|
635
656
|
async resolveBlocks() {
|
|
636
657
|
try {
|
|
637
|
-
|
|
638
658
|
const localBlock = await chainStore.get('lastBlock');
|
|
639
659
|
const hash = new TextDecoder().decode(localBlock);
|
|
660
|
+
|
|
640
661
|
if (hash && hash !== '0x0')
|
|
641
|
-
await this.resolveBlock(
|
|
662
|
+
await this.resolveBlock(hash);
|
|
642
663
|
this.#lastBlock = this.#blocks[this.#blocks.length - 1];
|
|
643
664
|
await this.#loadBlocks(this.#blocks);
|
|
644
665
|
} catch (e) {
|
|
@@ -695,7 +716,8 @@ async resolveBlock(hash) {
|
|
|
695
716
|
}
|
|
696
717
|
|
|
697
718
|
async #addBlock(block) {
|
|
698
|
-
|
|
719
|
+
// console.log(block);
|
|
720
|
+
const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)));
|
|
699
721
|
// if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
|
|
700
722
|
// const transactionJob = async transaction => {
|
|
701
723
|
// try {
|
|
@@ -710,14 +732,17 @@ async resolveBlock(hash) {
|
|
|
710
732
|
// transaction = new TransactionMessage(transaction)
|
|
711
733
|
// return transaction
|
|
712
734
|
// }
|
|
735
|
+
|
|
736
|
+
console.log(blockMessage);
|
|
713
737
|
await Promise.all(blockMessage.decoded.transactions
|
|
714
738
|
.map(async transaction => transactionPoolStore.delete(await transaction.hash)));
|
|
715
739
|
const hash = await blockMessage.hash;
|
|
716
740
|
// let transactions = blockMessage.decoded.transactions.map(tx => transactionJob(tx))
|
|
717
741
|
// transactions = await Promise.all(transactions)
|
|
718
|
-
|
|
742
|
+
|
|
719
743
|
await blockStore.put(hash, blockMessage.encoded);
|
|
720
|
-
|
|
744
|
+
|
|
745
|
+
if (this.lastBlock.index < blockMessage.decoded.index) await this.#updateState(blockMessage);
|
|
721
746
|
debug(`added block: ${hash}`);
|
|
722
747
|
let promises = [];
|
|
723
748
|
let contracts = [];
|
|
@@ -739,6 +764,8 @@ async resolveBlock(hash) {
|
|
|
739
764
|
// // await stateStore.put(contract, state)
|
|
740
765
|
// console.log(state);
|
|
741
766
|
// }
|
|
767
|
+
|
|
768
|
+
|
|
742
769
|
pubsub.publish('block-processed', blockMessage.decoded);
|
|
743
770
|
} catch (e) {
|
|
744
771
|
console.log({e});
|
|
@@ -746,8 +773,10 @@ async resolveBlock(hash) {
|
|
|
746
773
|
|
|
747
774
|
}
|
|
748
775
|
|
|
749
|
-
async #updateState() {
|
|
750
|
-
|
|
776
|
+
async #updateState(message) {
|
|
777
|
+
const hash = await message.hash;
|
|
778
|
+
this.#lastBlock = { hash, ...message.decoded };
|
|
779
|
+
await chainStore.put('lastBlock', hash);
|
|
751
780
|
}
|
|
752
781
|
|
|
753
782
|
|
|
@@ -909,13 +938,11 @@ async resolveBlock(hash) {
|
|
|
909
938
|
const hash = await blockMessage.hash;
|
|
910
939
|
|
|
911
940
|
|
|
912
|
-
|
|
913
|
-
await
|
|
914
|
-
await chainStore.put('lastBlock', hash);
|
|
941
|
+
await peernet.put(hash, blockMessage.encoded, 'block');
|
|
942
|
+
await this.#updateState(blockMessage);
|
|
915
943
|
debug(`created block: ${hash}`);
|
|
916
944
|
|
|
917
945
|
peernet.publish('add-block', blockMessage.encoded);
|
|
918
|
-
this.#updateState(blockMessage);
|
|
919
946
|
} catch (e) {
|
|
920
947
|
console.log(e);
|
|
921
948
|
throw Error(`invalid block ${block}`)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
+
import '@ethersproject/units';
|
|
4
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
5
|
+
|
|
6
|
+
var proto = `
|
|
7
|
+
message ValidatorMessage {
|
|
8
|
+
required string address = 1;
|
|
9
|
+
required string reward = 2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
message Transaction {
|
|
13
|
+
required string hash = 1;
|
|
14
|
+
required uint64 timestamp = 2;
|
|
15
|
+
required string from = 3;
|
|
16
|
+
required string to = 4;
|
|
17
|
+
required uint64 nonce = 5;
|
|
18
|
+
required string method = 6;
|
|
19
|
+
repeated string params = 7;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
message BlockMessage {
|
|
23
|
+
required uint64 index = 1;
|
|
24
|
+
required string previousHash = 3;
|
|
25
|
+
required uint64 timestamp = 4;
|
|
26
|
+
required uint64 reward = 5;
|
|
27
|
+
required string fees = 6;
|
|
28
|
+
repeated Transaction transactions = 7;
|
|
29
|
+
repeated ValidatorMessage validators = 8;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
class BlockMessage extends FormatInterface {
|
|
34
|
+
get keys() {
|
|
35
|
+
return ['index', 'previousHash', 'timestamp', 'reward', 'fees', 'transactions', 'validators']
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get messageName() {
|
|
39
|
+
return 'BlockMessage'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor(buffer) {
|
|
43
|
+
const name = 'block-message';
|
|
44
|
+
super(buffer, proto, {name});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
49
|
+
|
|
50
|
+
const formatBytes = (bytes, decimals = 2) => {
|
|
51
|
+
if (bytes === 0) return '0 Bytes';
|
|
52
|
+
if (decimals < 0) decimals = 0;
|
|
53
|
+
|
|
54
|
+
const k = 1024;
|
|
55
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
56
|
+
|
|
57
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${byteFormats[i]}`
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const worker = new EasyWorker();
|
|
61
|
+
|
|
62
|
+
globalThis.BigNumber = BigNumber;
|
|
63
|
+
|
|
64
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
65
|
+
globalThis.contracts = {};
|
|
66
|
+
|
|
67
|
+
const run = async (blocks) => {
|
|
68
|
+
blocks = await Promise.all(blocks.map(block => new BlockMessage(block)));
|
|
69
|
+
blocks = blocks.sort((a, b) => a.decoded.timestamp - b.decoded.timestamp);
|
|
70
|
+
|
|
71
|
+
blocks = await Promise.all(blocks.map(block => new Promise(async (resolve, reject) => {
|
|
72
|
+
// todo: tx worker or nah?
|
|
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
|
+
return blocks
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const tasks = async blocks => {
|
|
81
|
+
globalThis.peernet.codecs = {
|
|
82
|
+
'block-message': {
|
|
83
|
+
codec: parseInt('626d', 16),
|
|
84
|
+
hashAlg: 'keccak-256'
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
blocks = await run(blocks);
|
|
89
|
+
worker.postMessage(blocks);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
worker.onmessage(data => tasks(data));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
2
|
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
3
|
import '@ethersproject/units';
|
|
4
|
-
import
|
|
4
|
+
import 'path';
|
|
5
5
|
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
6
6
|
|
|
7
7
|
var proto$1 = `
|
|
@@ -124,7 +124,7 @@ const runContract = async (contractMessage) => {
|
|
|
124
124
|
globalThis.msg = createMessage(contractMessage.decoded.creator);
|
|
125
125
|
// globalThis.msg = {sender: contractMessage.decoded.creator}
|
|
126
126
|
contracts[await contractMessage.hash] = await new Contract(...params);
|
|
127
|
-
|
|
127
|
+
worker.postMessage({
|
|
128
128
|
type: 'debug',
|
|
129
129
|
messages: [
|
|
130
130
|
`loaded contract: ${await contractMessage.hash}`,
|
|
@@ -133,7 +133,7 @@ const runContract = async (contractMessage) => {
|
|
|
133
133
|
});
|
|
134
134
|
} catch (e) {
|
|
135
135
|
console.log(e);
|
|
136
|
-
|
|
136
|
+
worker.postMessage({
|
|
137
137
|
type: 'contractError',
|
|
138
138
|
hash: await contractMessage.hash
|
|
139
139
|
});
|
|
@@ -196,29 +196,36 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
196
196
|
return contract
|
|
197
197
|
}));
|
|
198
198
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
await
|
|
204
|
-
|
|
205
|
-
|
|
199
|
+
let lastBlock = {hash: '0x0'};
|
|
200
|
+
|
|
201
|
+
if (blocks?.length > 0) {
|
|
202
|
+
const _worker = await new EasyWorker('./block-worker.js', {serialization: 'advanced', type: 'module' });
|
|
203
|
+
blocks = await _worker.once(blocks);
|
|
204
|
+
|
|
205
|
+
for (const block of blocks) {
|
|
206
|
+
await Promise.all(block.decoded.transactions.map(async message => {
|
|
207
|
+
const {from, to, method, params} = message;
|
|
208
|
+
globalThis.msg = createMessage(from);
|
|
209
|
+
|
|
210
|
+
await execute(to, method, params);
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
206
213
|
|
|
207
|
-
|
|
208
|
-
|
|
214
|
+
if (blocks.length > 0) {
|
|
215
|
+
lastBlock = blocks[blocks.length - 1].decoded;
|
|
216
|
+
lastBlock = await new BlockMessage(lastBlock);
|
|
217
|
+
|
|
218
|
+
lastBlock = {
|
|
219
|
+
...lastBlock.decoded,
|
|
220
|
+
hash: await lastBlock.hash
|
|
221
|
+
};
|
|
222
|
+
}
|
|
209
223
|
}
|
|
210
224
|
|
|
211
|
-
|
|
212
|
-
if (blocks.length > 0) {
|
|
213
|
-
lastBlock = blocks[blocks.length - 1].decoded;
|
|
214
|
-
lastBlock = await new BlockMessage(lastBlock);
|
|
225
|
+
|
|
215
226
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
hash: await lastBlock.hash
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
worker.postMessage({type: 'machine-ready', lastBlock });
|
|
227
|
+
|
|
228
|
+
worker.postMessage({type: 'machine-ready', lastBlock});
|
|
222
229
|
_worker.terminate();
|
|
223
230
|
|
|
224
231
|
worker.postMessage(blocks);
|
|
@@ -226,14 +233,33 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
226
233
|
|
|
227
234
|
const tasks = async (e) => {
|
|
228
235
|
const id = e.id;
|
|
229
|
-
if (e.type === 'init')
|
|
236
|
+
if (e.type === 'init') {
|
|
237
|
+
try {
|
|
238
|
+
await _init(e.input);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
worker.postMessage({
|
|
241
|
+
type: 'initError',
|
|
242
|
+
message: e.message,
|
|
243
|
+
id
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
230
247
|
if (e.type === 'get') {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
248
|
+
try {
|
|
249
|
+
const value = await get(e.input.contract, e.input.method, e.input.params);
|
|
250
|
+
worker.postMessage({
|
|
251
|
+
type: 'response',
|
|
252
|
+
id,
|
|
253
|
+
value
|
|
254
|
+
});
|
|
255
|
+
} catch (e) {
|
|
256
|
+
worker.postMessage({
|
|
257
|
+
type: 'fetchError',
|
|
258
|
+
message: e.message,
|
|
259
|
+
id
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
237
263
|
}
|
|
238
264
|
if (e.type === 'execute') {
|
|
239
265
|
try {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
3
|
+
|
|
4
|
+
var proto = `
|
|
5
|
+
|
|
6
|
+
message TransactionMessage {
|
|
7
|
+
required uint64 timestamp = 1;
|
|
8
|
+
required string from = 2;
|
|
9
|
+
required string to = 3;
|
|
10
|
+
required uint64 nonce = 4;
|
|
11
|
+
required string method = 5;
|
|
12
|
+
repeated string params = 6;
|
|
13
|
+
required string signature = 7;
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
class TransactionMessage extends FormatInterface {
|
|
18
|
+
get keys() {
|
|
19
|
+
return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get messageName() {
|
|
23
|
+
return 'TransactionMessage'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
constructor(buffer) {
|
|
27
|
+
const name = 'transaction-message';
|
|
28
|
+
super(buffer, proto, {name});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
33
|
+
globalThis.contracts = {};
|
|
34
|
+
|
|
35
|
+
const worker = new EasyWorker();
|
|
36
|
+
|
|
37
|
+
const tasks = async transactions => {
|
|
38
|
+
|
|
39
|
+
globalThis.peernet.codecs = {
|
|
40
|
+
'transaction-message': {
|
|
41
|
+
codec: parseInt('746d', 16),
|
|
42
|
+
hashAlg: 'keccak-256'
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
transactions = await Promise.all(transactions.map(async message => {
|
|
47
|
+
message = await new TransactionMessage(message);
|
|
48
|
+
|
|
49
|
+
return {...message.decoded, hash: await message.hash, size: message.encoded.length}
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
worker.postMessage(transactions);
|
|
53
|
+
|
|
54
|
+
};
|
|
55
|
+
worker.onmessage(data => tasks(data));
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
+
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
3
|
+
|
|
4
|
+
var proto = `
|
|
5
|
+
|
|
6
|
+
message TransactionMessage {
|
|
7
|
+
required uint64 timestamp = 1;
|
|
8
|
+
required string from = 2;
|
|
9
|
+
required string to = 3;
|
|
10
|
+
required uint64 nonce = 4;
|
|
11
|
+
required string method = 5;
|
|
12
|
+
repeated string params = 6;
|
|
13
|
+
required string signature = 7;
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
class TransactionMessage extends FormatInterface {
|
|
18
|
+
get keys() {
|
|
19
|
+
return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get messageName() {
|
|
23
|
+
return 'TransactionMessage'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
constructor(buffer) {
|
|
27
|
+
const name = 'transaction-message';
|
|
28
|
+
super(buffer, proto, {name});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
33
|
+
globalThis.contracts = {};
|
|
34
|
+
|
|
35
|
+
const worker = new EasyWorker();
|
|
36
|
+
|
|
37
|
+
worker.onmessage(async (transactions) => {
|
|
38
|
+
globalThis.peernet.codecs = {
|
|
39
|
+
'transaction-message': {
|
|
40
|
+
codec: parseInt('746d', 16),
|
|
41
|
+
hashAlg: 'keccak-256'
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
transactions = await Promise.all(transactions.map(async message => new TransactionMessage(message)));
|
|
45
|
+
|
|
46
|
+
worker.postMessage(transactions);
|
|
47
|
+
});
|