@leofcoin/chain 1.4.30 → 1.4.32
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/exports/chain.js +19 -11
- package/exports/node.js +5 -1
- package/exports/typings/node.d.ts +1 -0
- package/package.json +1 -1
package/exports/chain.js
CHANGED
|
@@ -656,6 +656,16 @@ class Chain extends Contract {
|
|
|
656
656
|
await this.#setup();
|
|
657
657
|
this.utils = { BigNumber, formatUnits, parseUnits };
|
|
658
658
|
this.state = new State();
|
|
659
|
+
await peernet.addRequestHandler('bw-request-message', () => {
|
|
660
|
+
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 };
|
|
661
|
+
});
|
|
662
|
+
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
663
|
+
await peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
664
|
+
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
665
|
+
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
666
|
+
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
667
|
+
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
668
|
+
// todo some functions rely on state
|
|
659
669
|
try {
|
|
660
670
|
let localBlock;
|
|
661
671
|
try {
|
|
@@ -679,19 +689,11 @@ class Chain extends Contract {
|
|
|
679
689
|
catch (error) {
|
|
680
690
|
console.log({ e: error });
|
|
681
691
|
}
|
|
682
|
-
await peernet.addRequestHandler('bw-request-message', () => {
|
|
683
|
-
return new BWMessage(peernet.client.bw) || { up: 0, down: 0 };
|
|
684
|
-
});
|
|
685
|
-
await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
686
|
-
await peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
687
|
-
peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
688
|
-
peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
689
|
-
peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
690
|
-
pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
691
692
|
// load local blocks
|
|
692
693
|
await this.resolveBlocks();
|
|
693
694
|
this.#machine = await new Machine(this.#blocks);
|
|
694
695
|
await this.#loadBlocks(this.#blocks);
|
|
696
|
+
globalThis.pubsub.publish('chain:ready', true);
|
|
695
697
|
return this;
|
|
696
698
|
}
|
|
697
699
|
async #validatorTimeout(validatorInfo) {
|
|
@@ -703,6 +705,7 @@ class Chain extends Contract {
|
|
|
703
705
|
async #syncChain(lastBlock) {
|
|
704
706
|
if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
|
|
705
707
|
return;
|
|
708
|
+
this.#chainSyncing = true;
|
|
706
709
|
if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
|
|
707
710
|
let promises = [];
|
|
708
711
|
promises = await Promise.allSettled(this.#knownBlocks.map(async (address) => {
|
|
@@ -713,7 +716,6 @@ class Chain extends Contract {
|
|
|
713
716
|
await Promise.allSettled(promises.map(({ value }) => peernet.get(value.address, 'block')));
|
|
714
717
|
}
|
|
715
718
|
if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
|
|
716
|
-
this.#chainSyncing = true;
|
|
717
719
|
// TODO: check if valid
|
|
718
720
|
const localIndex = this.lastBlock ? this.lastBlock.index : 0;
|
|
719
721
|
const index = lastBlock.index;
|
|
@@ -724,8 +726,8 @@ class Chain extends Contract {
|
|
|
724
726
|
const start = (this.#blocks.length - blocksSynced) - 1;
|
|
725
727
|
await this.#loadBlocks(this.blocks.slice(start));
|
|
726
728
|
await this.#updateState(new BlockMessage(this.#blocks[this.#blocks.length - 1]));
|
|
727
|
-
this.#chainSyncing = false;
|
|
728
729
|
}
|
|
730
|
+
this.#chainSyncing = false;
|
|
729
731
|
}
|
|
730
732
|
async #peerConnected(peer) {
|
|
731
733
|
let node = await new peernet.protos['peernet-request']({ request: 'lastBlock' });
|
|
@@ -733,6 +735,12 @@ class Chain extends Contract {
|
|
|
733
735
|
let response = await peer.request(node.encoded);
|
|
734
736
|
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
735
737
|
let lastBlock = response.decoded.response;
|
|
738
|
+
// try catch known blocks
|
|
739
|
+
node = await new peernet.protos['peernet-request']({ request: 'knownBlocks' });
|
|
740
|
+
node = await peernet.prepareMessage(node);
|
|
741
|
+
response = await peer.request(node.encoded);
|
|
742
|
+
response = await new globalThis.peernet.protos['peernet-response'](response);
|
|
743
|
+
this.#knownBlocks = response.decoded.response;
|
|
736
744
|
this.#syncChain(lastBlock);
|
|
737
745
|
}
|
|
738
746
|
#epochTimeout;
|
package/exports/node.js
CHANGED
|
@@ -4,6 +4,7 @@ import networks from '@leofcoin/networks';
|
|
|
4
4
|
|
|
5
5
|
// import config from './config/config'
|
|
6
6
|
class Node {
|
|
7
|
+
#node;
|
|
7
8
|
constructor(config, password) {
|
|
8
9
|
return this._init(config, password);
|
|
9
10
|
}
|
|
@@ -13,8 +14,11 @@ class Node {
|
|
|
13
14
|
networkVersion: 'peach',
|
|
14
15
|
stars: networks.leofcoin.peach.stars
|
|
15
16
|
}, password) {
|
|
16
|
-
globalThis.Peernet ? await new globalThis.Peernet(config, password) : await new Peernet(config, password);
|
|
17
|
+
this.#node = globalThis.Peernet ? await new globalThis.Peernet(config, password) : await new Peernet(config, password);
|
|
17
18
|
await nodeConfig(config);
|
|
19
|
+
globalThis.pubsub.subscribe('chain:ready', () => {
|
|
20
|
+
this.#node.start();
|
|
21
|
+
});
|
|
18
22
|
return this;
|
|
19
23
|
// this.config = await config()
|
|
20
24
|
}
|