@leofcoin/chain 1.1.11 → 1.1.13
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 +50 -51
- package/demo/node.browser.js +6 -13
- package/demo/pako.browser.js +670 -501
- package/demo/peernet-swarm.browser.js +8 -12
- package/demo/workers/block-worker.js +1 -2
- package/demo/workers/machine-worker.js +2 -4
- package/demo/workers/pool-worker.js +1 -2
- package/demo/workers/transaction-worker.js +1 -2
- package/dist/browser/workers/block-worker.js +1 -2
- package/dist/browser/workers/machine-worker.js +2 -4
- package/dist/browser/workers/pool-worker.js +1 -2
- package/dist/browser/workers/transaction-worker.js +1 -2
- package/dist/chain.browser.js +50 -51
- package/dist/chain.js +49 -49
- package/dist/module/chain.js +49 -49
- package/dist/module/workers/machine-worker.js +2 -2
- package/dist/node.browser.js +6 -13
- package/dist/pako.browser.js +670 -501
- package/dist/peernet-swarm.browser.js +8 -12
- package/dist/workers/machine-worker.js +1 -1
- package/package.json +4 -2
- package/rollup.config.js +1 -1
- package/src/chain.js +42 -46
- package/src/machine.js +6 -2
- package/test/chain.js +33 -18
- package/demo/865.machine-worker.js +0 -10
- package/demo/chain.js +0 -1209
- package/demo/machine-worker.js +0 -17624
- package/demo/node.js +0 -1
- package/demo/workers/workers/865.js +0 -10
- package/demo/workers/workers/block-worker.js +0 -13200
- package/demo/workers/workers/machine-worker.js +0 -13904
- package/demo/workers/workers/pool-worker.js +0 -8504
- package/demo/workers/workers/transaction-worker.js +0 -8496
package/dist/chain.js
CHANGED
|
@@ -68,7 +68,7 @@ class Machine {
|
|
|
68
68
|
return this.#init()
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
#createMessage(sender = peernet.
|
|
71
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
72
72
|
return {
|
|
73
73
|
sender,
|
|
74
74
|
call: this.execute,
|
|
@@ -93,7 +93,7 @@ class Machine {
|
|
|
93
93
|
break
|
|
94
94
|
case 'machine-ready':
|
|
95
95
|
this.lastBlock = data.lastBlock;
|
|
96
|
-
pubsub.publish('machine.ready');
|
|
96
|
+
pubsub.publish('machine.ready', true);
|
|
97
97
|
break
|
|
98
98
|
case 'response':
|
|
99
99
|
pubsub.publish(data.id, data.value);
|
|
@@ -214,6 +214,10 @@ class Machine {
|
|
|
214
214
|
return contractStore.delete(hash)
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
/**
|
|
218
|
+
*
|
|
219
|
+
* @returns Promise
|
|
220
|
+
*/
|
|
217
221
|
async deleteAll() {
|
|
218
222
|
let hashes = await contractStore.get();
|
|
219
223
|
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
@@ -448,7 +452,7 @@ class Chain {
|
|
|
448
452
|
console.log('epoch');
|
|
449
453
|
const validators = await this.staticCall(addresses.validators, 'validators');
|
|
450
454
|
console.log(validators);
|
|
451
|
-
if (!validators[peernet.
|
|
455
|
+
if (!validators[peernet.selectedAccount]?.active) return
|
|
452
456
|
|
|
453
457
|
const start = new Date().getTime();
|
|
454
458
|
try {
|
|
@@ -534,8 +538,7 @@ class Chain {
|
|
|
534
538
|
}, {index: 0, hash: '0x0'});
|
|
535
539
|
// get lastblock
|
|
536
540
|
if (promises.hash && promises.hash !== '0x0') {
|
|
537
|
-
|
|
538
|
-
console.log({localBlock});
|
|
541
|
+
await peernet.get(promises.hash);
|
|
539
542
|
}
|
|
540
543
|
|
|
541
544
|
|
|
@@ -550,22 +553,7 @@ class Chain {
|
|
|
550
553
|
|
|
551
554
|
this.#machine = await new Machine();
|
|
552
555
|
this.utils = { BigNumber: utils.BigNumber, formatUnits: utils.formatUnits, parseUnits: utils.parseUnits };
|
|
553
|
-
|
|
554
|
-
await peernet.addRequestHandler('bw-request-message', () => {
|
|
555
|
-
|
|
556
|
-
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
560
|
-
|
|
561
|
-
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
562
|
-
|
|
563
|
-
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
564
|
-
|
|
565
|
-
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
566
|
-
|
|
567
|
-
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
568
|
-
|
|
556
|
+
|
|
569
557
|
try {
|
|
570
558
|
let localBlock;
|
|
571
559
|
try {
|
|
@@ -575,17 +563,11 @@ class Chain {
|
|
|
575
563
|
localBlock = await chainStore.get('lastBlock');
|
|
576
564
|
}
|
|
577
565
|
localBlock = new TextDecoder().decode(localBlock);
|
|
578
|
-
|
|
579
566
|
if (localBlock && localBlock !== '0x0') {
|
|
580
|
-
localBlock = await peernet.get(localBlock);
|
|
567
|
+
localBlock = await peernet.get(localBlock, 'block');
|
|
581
568
|
localBlock = await new BlockMessage(localBlock);
|
|
582
569
|
this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
|
|
583
|
-
} else
|
|
584
|
-
// todo remove when network is running
|
|
585
|
-
// recovering chain (not needed if multiple peers are online)
|
|
586
|
-
this.#lastBlock = this.#machine.lastBlock;
|
|
587
|
-
await chainStore.put('lastBlock', this.#lastBlock.hash);
|
|
588
|
-
} else {
|
|
570
|
+
} else {
|
|
589
571
|
await this.#sync();
|
|
590
572
|
}
|
|
591
573
|
} catch (e) {
|
|
@@ -594,8 +576,25 @@ class Chain {
|
|
|
594
576
|
|
|
595
577
|
// this.#setup()
|
|
596
578
|
}
|
|
579
|
+
|
|
580
|
+
await peernet.addRequestHandler('bw-request-message', () => {
|
|
581
|
+
|
|
582
|
+
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
586
|
+
|
|
587
|
+
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
588
|
+
|
|
589
|
+
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
590
|
+
|
|
591
|
+
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
592
|
+
|
|
593
|
+
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
594
|
+
|
|
595
|
+
|
|
597
596
|
// load local blocks
|
|
598
|
-
|
|
597
|
+
await this.resolveBlocks();
|
|
599
598
|
return this
|
|
600
599
|
}
|
|
601
600
|
|
|
@@ -613,7 +612,7 @@ class Chain {
|
|
|
613
612
|
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
614
613
|
let lastBlock = response.decoded.response;
|
|
615
614
|
|
|
616
|
-
if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
|
|
615
|
+
if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
|
|
617
616
|
// TODO: check if valid
|
|
618
617
|
const localIndex = this.lastBlock ? this.lastBlock.index : 0;
|
|
619
618
|
const index = lastBlock.index;
|
|
@@ -655,7 +654,8 @@ async resolveBlock(hash) {
|
|
|
655
654
|
if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block');
|
|
656
655
|
const size = block.encoded.length || block.encoded.byteLength;
|
|
657
656
|
block = {...block.decoded, hash};
|
|
658
|
-
this.#blocks[block.index
|
|
657
|
+
if (this.#blocks[block.index]) throw `invalid block ${hash} @${block.index}`
|
|
658
|
+
this.#blocks[block.index] = block;
|
|
659
659
|
console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
|
|
660
660
|
if (block.previousHash !== '0x0') {
|
|
661
661
|
return this.resolveBlock(block.previousHash)
|
|
@@ -664,11 +664,11 @@ async resolveBlock(hash) {
|
|
|
664
664
|
|
|
665
665
|
async resolveBlocks() {
|
|
666
666
|
try {
|
|
667
|
-
|
|
668
667
|
const localBlock = await chainStore.get('lastBlock');
|
|
669
668
|
const hash = new TextDecoder().decode(localBlock);
|
|
669
|
+
|
|
670
670
|
if (hash && hash !== '0x0')
|
|
671
|
-
await this.resolveBlock(
|
|
671
|
+
await this.resolveBlock(hash);
|
|
672
672
|
this.#lastBlock = this.#blocks[this.#blocks.length - 1];
|
|
673
673
|
await this.#loadBlocks(this.#blocks);
|
|
674
674
|
} catch (e) {
|
|
@@ -725,7 +725,7 @@ async resolveBlock(hash) {
|
|
|
725
725
|
}
|
|
726
726
|
|
|
727
727
|
async #addBlock(block) {
|
|
728
|
-
console.log(block);
|
|
728
|
+
// console.log(block);
|
|
729
729
|
const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)));
|
|
730
730
|
// if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
|
|
731
731
|
// const transactionJob = async transaction => {
|
|
@@ -751,7 +751,7 @@ async resolveBlock(hash) {
|
|
|
751
751
|
|
|
752
752
|
await blockStore.put(hash, blockMessage.encoded);
|
|
753
753
|
|
|
754
|
-
if (this.lastBlock.index < blockMessage.decoded.index) this.#updateState(blockMessage);
|
|
754
|
+
if (this.lastBlock.index < blockMessage.decoded.index) await this.#updateState(blockMessage);
|
|
755
755
|
debug(`added block: ${hash}`);
|
|
756
756
|
let promises = [];
|
|
757
757
|
let contracts = [];
|
|
@@ -790,7 +790,7 @@ async resolveBlock(hash) {
|
|
|
790
790
|
|
|
791
791
|
|
|
792
792
|
|
|
793
|
-
async participate() {
|
|
793
|
+
async participate(address) {
|
|
794
794
|
// TODO: validate participant
|
|
795
795
|
// hold min amount of 50k ART for 7 days
|
|
796
796
|
// lock the 50k
|
|
@@ -798,7 +798,7 @@ async resolveBlock(hash) {
|
|
|
798
798
|
// peerReputation(peerId)
|
|
799
799
|
// {bandwith: {up, down}, uptime}
|
|
800
800
|
this.participating = true;
|
|
801
|
-
if (!await this.staticCall(addresses.validators, 'has', [
|
|
801
|
+
if (!await this.staticCall(addresses.validators, 'has', [address])) await this.createTransactionFrom(address, addresses.validators, 'addValidator', [address]);
|
|
802
802
|
if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch();
|
|
803
803
|
|
|
804
804
|
// const runEpoch = () => setTimeout(async () => {
|
|
@@ -887,7 +887,7 @@ async resolveBlock(hash) {
|
|
|
887
887
|
const node = await peernet.prepareMessage(validator, data.encoded);
|
|
888
888
|
try {
|
|
889
889
|
const bw = await peer.request(node.encoded);
|
|
890
|
-
console.log(bw);
|
|
890
|
+
console.log({bw});
|
|
891
891
|
block.validators.push({
|
|
892
892
|
address: validator,
|
|
893
893
|
bw: bw.up + bw.down
|
|
@@ -896,9 +896,9 @@ async resolveBlock(hash) {
|
|
|
896
896
|
|
|
897
897
|
}
|
|
898
898
|
|
|
899
|
-
} else if (peernet.
|
|
899
|
+
} else if (peernet.selectedAccount === validator) {
|
|
900
900
|
block.validators.push({
|
|
901
|
-
address: peernet.
|
|
901
|
+
address: peernet.selectedAccount,
|
|
902
902
|
bw: peernet.bw.up + peernet.bw.down
|
|
903
903
|
});
|
|
904
904
|
|
|
@@ -947,8 +947,8 @@ async resolveBlock(hash) {
|
|
|
947
947
|
const hash = await blockMessage.hash;
|
|
948
948
|
|
|
949
949
|
|
|
950
|
-
await
|
|
951
|
-
this.#updateState(blockMessage);
|
|
950
|
+
await peernet.put(hash, blockMessage.encoded, 'block');
|
|
951
|
+
await this.#updateState(blockMessage);
|
|
952
952
|
debug(`created block: ${hash}`);
|
|
953
953
|
|
|
954
954
|
peernet.publish('add-block', blockMessage.encoded);
|
|
@@ -1024,7 +1024,7 @@ async resolveBlock(hash) {
|
|
|
1024
1024
|
* @param {Number} nonce - total transaction count [optional]
|
|
1025
1025
|
*/
|
|
1026
1026
|
async createTransaction(to, method, params, nonce, signature) {
|
|
1027
|
-
return this.createTransactionFrom(peernet.
|
|
1027
|
+
return this.createTransactionFrom(peernet.selectedAccount, to, method, params, nonce)
|
|
1028
1028
|
}
|
|
1029
1029
|
|
|
1030
1030
|
|
|
@@ -1076,7 +1076,7 @@ async #signTransaction (transaction, wallet) {
|
|
|
1076
1076
|
* @returns {Object} transaction
|
|
1077
1077
|
*/
|
|
1078
1078
|
async createRawTransaction(transaction) {
|
|
1079
|
-
if (!transaction.from) transaction.from = peernet.
|
|
1079
|
+
if (!transaction.from) transaction.from = peernet.selectedAccount;
|
|
1080
1080
|
transaction.timestamp = Date.now();
|
|
1081
1081
|
|
|
1082
1082
|
if (transaction.nonce === undefined) {
|
|
@@ -1152,21 +1152,21 @@ async #signTransaction (transaction, wallet) {
|
|
|
1152
1152
|
* @param {String} contract - a contract string (see plugins/deployContract)
|
|
1153
1153
|
*/
|
|
1154
1154
|
async deployContract(contract, params = []) {
|
|
1155
|
-
globalThis.msg = {sender: peernet.
|
|
1155
|
+
globalThis.msg = {sender: peernet.selectedAccount, call: this.call};
|
|
1156
1156
|
|
|
1157
1157
|
const hash = await this.createContractAddress(creator, contract, params);
|
|
1158
1158
|
console.log(hash);
|
|
1159
1159
|
try {
|
|
1160
|
-
const tx = await this.createTransactionFrom(peernet.
|
|
1160
|
+
const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
|
|
1161
1161
|
} catch (e) {
|
|
1162
1162
|
throw e
|
|
1163
1163
|
}
|
|
1164
1164
|
return this.#machine.addContract(message)
|
|
1165
1165
|
}
|
|
1166
1166
|
|
|
1167
|
-
#createMessage(sender = peernet.
|
|
1167
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
1168
1168
|
return {
|
|
1169
|
-
sender
|
|
1169
|
+
sender,
|
|
1170
1170
|
call: this.call,
|
|
1171
1171
|
staticCall: this.staticCall,
|
|
1172
1172
|
delegate: this.delegate,
|
package/dist/module/chain.js
CHANGED
|
@@ -59,7 +59,7 @@ class Machine {
|
|
|
59
59
|
return this.#init()
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
#createMessage(sender = peernet.
|
|
62
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
63
63
|
return {
|
|
64
64
|
sender,
|
|
65
65
|
call: this.execute,
|
|
@@ -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));
|
|
@@ -439,7 +443,7 @@ class Chain {
|
|
|
439
443
|
console.log('epoch');
|
|
440
444
|
const validators = await this.staticCall(addresses.validators, 'validators');
|
|
441
445
|
console.log(validators);
|
|
442
|
-
if (!validators[peernet.
|
|
446
|
+
if (!validators[peernet.selectedAccount]?.active) return
|
|
443
447
|
|
|
444
448
|
const start = new Date().getTime();
|
|
445
449
|
try {
|
|
@@ -525,8 +529,7 @@ class Chain {
|
|
|
525
529
|
}, {index: 0, hash: '0x0'});
|
|
526
530
|
// get lastblock
|
|
527
531
|
if (promises.hash && promises.hash !== '0x0') {
|
|
528
|
-
|
|
529
|
-
console.log({localBlock});
|
|
532
|
+
await peernet.get(promises.hash);
|
|
530
533
|
}
|
|
531
534
|
|
|
532
535
|
|
|
@@ -541,22 +544,7 @@ class Chain {
|
|
|
541
544
|
|
|
542
545
|
this.#machine = await new Machine();
|
|
543
546
|
this.utils = { BigNumber, formatUnits, parseUnits };
|
|
544
|
-
|
|
545
|
-
await peernet.addRequestHandler('bw-request-message', () => {
|
|
546
|
-
|
|
547
|
-
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
551
|
-
|
|
552
|
-
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
553
|
-
|
|
554
|
-
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
555
|
-
|
|
556
|
-
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
557
|
-
|
|
558
|
-
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
559
|
-
|
|
547
|
+
|
|
560
548
|
try {
|
|
561
549
|
let localBlock;
|
|
562
550
|
try {
|
|
@@ -566,17 +554,11 @@ class Chain {
|
|
|
566
554
|
localBlock = await chainStore.get('lastBlock');
|
|
567
555
|
}
|
|
568
556
|
localBlock = new TextDecoder().decode(localBlock);
|
|
569
|
-
|
|
570
557
|
if (localBlock && localBlock !== '0x0') {
|
|
571
|
-
localBlock = await peernet.get(localBlock);
|
|
558
|
+
localBlock = await peernet.get(localBlock, 'block');
|
|
572
559
|
localBlock = await new BlockMessage(localBlock);
|
|
573
560
|
this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
|
|
574
|
-
} else
|
|
575
|
-
// todo remove when network is running
|
|
576
|
-
// recovering chain (not needed if multiple peers are online)
|
|
577
|
-
this.#lastBlock = this.#machine.lastBlock;
|
|
578
|
-
await chainStore.put('lastBlock', this.#lastBlock.hash);
|
|
579
|
-
} else {
|
|
561
|
+
} else {
|
|
580
562
|
await this.#sync();
|
|
581
563
|
}
|
|
582
564
|
} catch (e) {
|
|
@@ -585,8 +567,25 @@ class Chain {
|
|
|
585
567
|
|
|
586
568
|
// this.#setup()
|
|
587
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
|
+
|
|
588
587
|
// load local blocks
|
|
589
|
-
|
|
588
|
+
await this.resolveBlocks();
|
|
590
589
|
return this
|
|
591
590
|
}
|
|
592
591
|
|
|
@@ -604,7 +603,7 @@ class Chain {
|
|
|
604
603
|
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
605
604
|
let lastBlock = response.decoded.response;
|
|
606
605
|
|
|
607
|
-
if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
|
|
606
|
+
if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
|
|
608
607
|
// TODO: check if valid
|
|
609
608
|
const localIndex = this.lastBlock ? this.lastBlock.index : 0;
|
|
610
609
|
const index = lastBlock.index;
|
|
@@ -646,7 +645,8 @@ async resolveBlock(hash) {
|
|
|
646
645
|
if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block');
|
|
647
646
|
const size = block.encoded.length || block.encoded.byteLength;
|
|
648
647
|
block = {...block.decoded, hash};
|
|
649
|
-
this.#blocks[block.index
|
|
648
|
+
if (this.#blocks[block.index]) throw `invalid block ${hash} @${block.index}`
|
|
649
|
+
this.#blocks[block.index] = block;
|
|
650
650
|
console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
|
|
651
651
|
if (block.previousHash !== '0x0') {
|
|
652
652
|
return this.resolveBlock(block.previousHash)
|
|
@@ -655,11 +655,11 @@ async resolveBlock(hash) {
|
|
|
655
655
|
|
|
656
656
|
async resolveBlocks() {
|
|
657
657
|
try {
|
|
658
|
-
|
|
659
658
|
const localBlock = await chainStore.get('lastBlock');
|
|
660
659
|
const hash = new TextDecoder().decode(localBlock);
|
|
660
|
+
|
|
661
661
|
if (hash && hash !== '0x0')
|
|
662
|
-
await this.resolveBlock(
|
|
662
|
+
await this.resolveBlock(hash);
|
|
663
663
|
this.#lastBlock = this.#blocks[this.#blocks.length - 1];
|
|
664
664
|
await this.#loadBlocks(this.#blocks);
|
|
665
665
|
} catch (e) {
|
|
@@ -716,7 +716,7 @@ async resolveBlock(hash) {
|
|
|
716
716
|
}
|
|
717
717
|
|
|
718
718
|
async #addBlock(block) {
|
|
719
|
-
console.log(block);
|
|
719
|
+
// console.log(block);
|
|
720
720
|
const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)));
|
|
721
721
|
// if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
|
|
722
722
|
// const transactionJob = async transaction => {
|
|
@@ -742,7 +742,7 @@ async resolveBlock(hash) {
|
|
|
742
742
|
|
|
743
743
|
await blockStore.put(hash, blockMessage.encoded);
|
|
744
744
|
|
|
745
|
-
if (this.lastBlock.index < blockMessage.decoded.index) this.#updateState(blockMessage);
|
|
745
|
+
if (this.lastBlock.index < blockMessage.decoded.index) await this.#updateState(blockMessage);
|
|
746
746
|
debug(`added block: ${hash}`);
|
|
747
747
|
let promises = [];
|
|
748
748
|
let contracts = [];
|
|
@@ -781,7 +781,7 @@ async resolveBlock(hash) {
|
|
|
781
781
|
|
|
782
782
|
|
|
783
783
|
|
|
784
|
-
async participate() {
|
|
784
|
+
async participate(address) {
|
|
785
785
|
// TODO: validate participant
|
|
786
786
|
// hold min amount of 50k ART for 7 days
|
|
787
787
|
// lock the 50k
|
|
@@ -789,7 +789,7 @@ async resolveBlock(hash) {
|
|
|
789
789
|
// peerReputation(peerId)
|
|
790
790
|
// {bandwith: {up, down}, uptime}
|
|
791
791
|
this.participating = true;
|
|
792
|
-
if (!await this.staticCall(addresses.validators, 'has', [
|
|
792
|
+
if (!await this.staticCall(addresses.validators, 'has', [address])) await this.createTransactionFrom(address, addresses.validators, 'addValidator', [address]);
|
|
793
793
|
if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch();
|
|
794
794
|
|
|
795
795
|
// const runEpoch = () => setTimeout(async () => {
|
|
@@ -878,7 +878,7 @@ async resolveBlock(hash) {
|
|
|
878
878
|
const node = await peernet.prepareMessage(validator, data.encoded);
|
|
879
879
|
try {
|
|
880
880
|
const bw = await peer.request(node.encoded);
|
|
881
|
-
console.log(bw);
|
|
881
|
+
console.log({bw});
|
|
882
882
|
block.validators.push({
|
|
883
883
|
address: validator,
|
|
884
884
|
bw: bw.up + bw.down
|
|
@@ -887,9 +887,9 @@ async resolveBlock(hash) {
|
|
|
887
887
|
|
|
888
888
|
}
|
|
889
889
|
|
|
890
|
-
} else if (peernet.
|
|
890
|
+
} else if (peernet.selectedAccount === validator) {
|
|
891
891
|
block.validators.push({
|
|
892
|
-
address: peernet.
|
|
892
|
+
address: peernet.selectedAccount,
|
|
893
893
|
bw: peernet.bw.up + peernet.bw.down
|
|
894
894
|
});
|
|
895
895
|
|
|
@@ -938,8 +938,8 @@ async resolveBlock(hash) {
|
|
|
938
938
|
const hash = await blockMessage.hash;
|
|
939
939
|
|
|
940
940
|
|
|
941
|
-
await
|
|
942
|
-
this.#updateState(blockMessage);
|
|
941
|
+
await peernet.put(hash, blockMessage.encoded, 'block');
|
|
942
|
+
await this.#updateState(blockMessage);
|
|
943
943
|
debug(`created block: ${hash}`);
|
|
944
944
|
|
|
945
945
|
peernet.publish('add-block', blockMessage.encoded);
|
|
@@ -1015,7 +1015,7 @@ async resolveBlock(hash) {
|
|
|
1015
1015
|
* @param {Number} nonce - total transaction count [optional]
|
|
1016
1016
|
*/
|
|
1017
1017
|
async createTransaction(to, method, params, nonce, signature) {
|
|
1018
|
-
return this.createTransactionFrom(peernet.
|
|
1018
|
+
return this.createTransactionFrom(peernet.selectedAccount, to, method, params, nonce)
|
|
1019
1019
|
}
|
|
1020
1020
|
|
|
1021
1021
|
|
|
@@ -1067,7 +1067,7 @@ async #signTransaction (transaction, wallet) {
|
|
|
1067
1067
|
* @returns {Object} transaction
|
|
1068
1068
|
*/
|
|
1069
1069
|
async createRawTransaction(transaction) {
|
|
1070
|
-
if (!transaction.from) transaction.from = peernet.
|
|
1070
|
+
if (!transaction.from) transaction.from = peernet.selectedAccount;
|
|
1071
1071
|
transaction.timestamp = Date.now();
|
|
1072
1072
|
|
|
1073
1073
|
if (transaction.nonce === undefined) {
|
|
@@ -1143,21 +1143,21 @@ async #signTransaction (transaction, wallet) {
|
|
|
1143
1143
|
* @param {String} contract - a contract string (see plugins/deployContract)
|
|
1144
1144
|
*/
|
|
1145
1145
|
async deployContract(contract, params = []) {
|
|
1146
|
-
globalThis.msg = {sender: peernet.
|
|
1146
|
+
globalThis.msg = {sender: peernet.selectedAccount, call: this.call};
|
|
1147
1147
|
|
|
1148
1148
|
const hash = await this.createContractAddress(creator, contract, params);
|
|
1149
1149
|
console.log(hash);
|
|
1150
1150
|
try {
|
|
1151
|
-
const tx = await this.createTransactionFrom(peernet.
|
|
1151
|
+
const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
|
|
1152
1152
|
} catch (e) {
|
|
1153
1153
|
throw e
|
|
1154
1154
|
}
|
|
1155
1155
|
return this.#machine.addContract(message)
|
|
1156
1156
|
}
|
|
1157
1157
|
|
|
1158
|
-
#createMessage(sender = peernet.
|
|
1158
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
1159
1159
|
return {
|
|
1160
|
-
sender
|
|
1160
|
+
sender,
|
|
1161
1161
|
call: this.call,
|
|
1162
1162
|
staticCall: this.staticCall,
|
|
1163
1163
|
delegate: this.delegate,
|
|
@@ -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 = `
|
|
@@ -199,7 +199,7 @@ const _init = async ({ contracts, blocks, peerid })=> {
|
|
|
199
199
|
let lastBlock = {hash: '0x0'};
|
|
200
200
|
|
|
201
201
|
if (blocks?.length > 0) {
|
|
202
|
-
const _worker = await new EasyWorker(
|
|
202
|
+
const _worker = await new EasyWorker('./block-worker.js', {serialization: 'advanced', type: 'module' });
|
|
203
203
|
blocks = await _worker.once(blocks);
|
|
204
204
|
|
|
205
205
|
for (const block of blocks) {
|
package/dist/node.browser.js
CHANGED
|
@@ -8341,8 +8341,7 @@ module.exports = function (value) {
|
|
|
8341
8341
|
/******/ script.parentNode && script.parentNode.removeChild(script);
|
|
8342
8342
|
/******/ doneFns && doneFns.forEach(function(fn) { return fn(event); });
|
|
8343
8343
|
/******/ if(prev) return prev(event);
|
|
8344
|
-
/******/ }
|
|
8345
|
-
/******/ ;
|
|
8344
|
+
/******/ };
|
|
8346
8345
|
/******/ var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);
|
|
8347
8346
|
/******/ script.onerror = onScriptComplete.bind(null, script.onerror);
|
|
8348
8347
|
/******/ script.onload = onScriptComplete.bind(null, script.onload);
|
|
@@ -9133,7 +9132,8 @@ class Peernet {
|
|
|
9133
9132
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
9134
9133
|
let accounts = await walletStore.get('accounts');
|
|
9135
9134
|
accounts = new TextDecoder().decode(accounts);
|
|
9136
|
-
|
|
9135
|
+
const selected = await walletStore.get('selected-account');
|
|
9136
|
+
globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
|
|
9137
9137
|
|
|
9138
9138
|
// fixing account issue (string while needs to be a JSON)
|
|
9139
9139
|
// TODO: remove when on mainnet
|
|
@@ -9151,15 +9151,17 @@ class Peernet {
|
|
|
9151
9151
|
|
|
9152
9152
|
await walletStore.put('version', String(1));
|
|
9153
9153
|
await walletStore.put('accounts', JSON.stringify(accounts));
|
|
9154
|
+
await walletStore.put('selected-account', accounts[0][1]);
|
|
9154
9155
|
await walletStore.put('identity', JSON.stringify(identity));
|
|
9155
9156
|
|
|
9157
|
+
globalThis.peernet.selectedAccount = accounts[0][1];
|
|
9156
9158
|
this.id = identity.walletId;
|
|
9157
9159
|
}
|
|
9158
9160
|
this._peerHandler = new PeerDiscovery(this.id);
|
|
9159
9161
|
this.peerId = this.id;
|
|
9160
9162
|
|
|
9161
9163
|
pubsub.subscribe('peer:connected', async (peer) => {
|
|
9162
|
-
console.log(peer);
|
|
9164
|
+
// console.log(peer);
|
|
9163
9165
|
// console.log({connected: peer.id, as: this._getPeerId(peer.id) });
|
|
9164
9166
|
// peer.on('peernet.data', async (message) => {
|
|
9165
9167
|
// const id = message.id
|
|
@@ -9269,7 +9271,6 @@ class Peernet {
|
|
|
9269
9271
|
const walk = async peer => {
|
|
9270
9272
|
const node = await this.prepareMessage(data);
|
|
9271
9273
|
let result = await peer.request(node.encoded);
|
|
9272
|
-
console.log({result});
|
|
9273
9274
|
result = new Uint8Array(Object.values(result));
|
|
9274
9275
|
const proto = await protoFor(result);
|
|
9275
9276
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
@@ -9616,14 +9617,6 @@ class Peernet {
|
|
|
9616
9617
|
get Buffer() {
|
|
9617
9618
|
return Buffer
|
|
9618
9619
|
}
|
|
9619
|
-
// async block(index) {
|
|
9620
|
-
// const _values = []
|
|
9621
|
-
// for (const peer of this.peers) {
|
|
9622
|
-
// const value = await peer.request({type: 'block', index})
|
|
9623
|
-
// console.log(value);
|
|
9624
|
-
// }
|
|
9625
|
-
//
|
|
9626
|
-
// }
|
|
9627
9620
|
}
|
|
9628
9621
|
globalThis.Peernet = Peernet;
|
|
9629
9622
|
|