@leofcoin/chain 1.4.38 → 1.4.39

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.
@@ -8078,6 +8078,7 @@ class Chain extends Contract {
8078
8078
  // let node =
8079
8079
  // globalThis.peernet.protos['peernet-response']({response: node.encoded})
8080
8080
  // })
8081
+ await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
8081
8082
  await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
8082
8083
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
8083
8084
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
@@ -8157,21 +8158,38 @@ class Chain extends Contract {
8157
8158
  }
8158
8159
  this.#chainSyncing = false;
8159
8160
  }
8160
- async #peerConnected(peer) {
8161
- let node = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
8162
- node = await globalThis.peernet.prepareMessage(node);
8161
+ async #prepareRequest(request) {
8162
+ let node = await new globalThis.peernet.protos['peernet-request']({ request });
8163
+ return globalThis.peernet.prepareMessage(node);
8164
+ }
8165
+ async #makeRequest(peer, request) {
8166
+ const node = await this.#prepareRequest(request);
8163
8167
  let response = await peer.request(node.encoded);
8164
- response = await new globalThis.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
8165
- let lastBlock = response.decoded.response;
8166
- // try catch known blocks
8167
- node = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
8168
- node = await globalThis.peernet.prepareMessage(node);
8169
- response = await peer.request(node.encoded);
8170
- response = await new globalThis.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
8171
- this.#knownBlocks = response.decoded.response;
8172
- this.#syncChain(lastBlock);
8168
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
8169
+ return response.decoded.response;
8170
+ }
8171
+ async #peerConnected(peer) {
8172
+ const lastBlock = await this.#makeRequest(peer, 'lastBlock');
8173
+ this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
8174
+ let pool = await this.#makeRequest(peer, 'transactionPool');
8175
+ pool = await Promise.all(pool.map(async (hash) => {
8176
+ const has = await globalThis.peernet.has(hash);
8177
+ return { has, hash };
8178
+ }));
8179
+ pool = pool.filter(item => !item.has);
8180
+ await Promise.all(pool.map(async ({ hash }) => {
8181
+ const result = await globalThis.peernet.get(hash);
8182
+ await globalThis.peernet.put(hash, result);
8183
+ }));
8184
+ console.log(pool);
8185
+ if (lastBlock)
8186
+ this.#syncChain(lastBlock);
8173
8187
  }
8174
8188
  #epochTimeout;
8189
+ async #transactionPoolHandler() {
8190
+ const pool = await globalThis.transactionPoolStore.keys();
8191
+ return new globalThis.peernet.protos['peernet-response']({ response: pool });
8192
+ }
8175
8193
  async #lastBlockHandler() {
8176
8194
  return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
8177
8195
  }
package/exports/chain.js CHANGED
@@ -673,6 +673,7 @@ class Chain extends Contract {
673
673
  // let node =
674
674
  // globalThis.peernet.protos['peernet-response']({response: node.encoded})
675
675
  // })
676
+ await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
676
677
  await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
677
678
  await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
678
679
  globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
@@ -752,21 +753,38 @@ class Chain extends Contract {
752
753
  }
753
754
  this.#chainSyncing = false;
754
755
  }
755
- async #peerConnected(peer) {
756
- let node = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
757
- node = await globalThis.peernet.prepareMessage(node);
756
+ async #prepareRequest(request) {
757
+ let node = await new globalThis.peernet.protos['peernet-request']({ request });
758
+ return globalThis.peernet.prepareMessage(node);
759
+ }
760
+ async #makeRequest(peer, request) {
761
+ const node = await this.#prepareRequest(request);
758
762
  let response = await peer.request(node.encoded);
759
- response = await new globalThis.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
760
- let lastBlock = response.decoded.response;
761
- // try catch known blocks
762
- node = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
763
- node = await globalThis.peernet.prepareMessage(node);
764
- response = await peer.request(node.encoded);
765
- response = await new globalThis.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
766
- this.#knownBlocks = response.decoded.response;
767
- this.#syncChain(lastBlock);
763
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
764
+ return response.decoded.response;
765
+ }
766
+ async #peerConnected(peer) {
767
+ const lastBlock = await this.#makeRequest(peer, 'lastBlock');
768
+ this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
769
+ let pool = await this.#makeRequest(peer, 'transactionPool');
770
+ pool = await Promise.all(pool.map(async (hash) => {
771
+ const has = await globalThis.peernet.has(hash);
772
+ return { has, hash };
773
+ }));
774
+ pool = pool.filter(item => !item.has);
775
+ await Promise.all(pool.map(async ({ hash }) => {
776
+ const result = await globalThis.peernet.get(hash);
777
+ await globalThis.peernet.put(hash, result);
778
+ }));
779
+ console.log(pool);
780
+ if (lastBlock)
781
+ this.#syncChain(lastBlock);
768
782
  }
769
783
  #epochTimeout;
784
+ async #transactionPoolHandler() {
785
+ const pool = await globalThis.transactionPoolStore.keys();
786
+ return new globalThis.peernet.protos['peernet-response']({ response: pool });
787
+ }
770
788
  async #lastBlockHandler() {
771
789
  return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
772
790
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.38",
3
+ "version": "1.4.39",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",