@leofcoin/chain 1.4.51 → 1.4.52

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.
@@ -7646,9 +7646,23 @@ class State {
7646
7646
  }
7647
7647
  }
7648
7648
 
7649
+ const limit = 1800;
7650
+ const transactionLimit = 1800;
7651
+ const requestTimeout = 30_000;
7652
+ const syncTimeout = 30_000;
7649
7653
  class Protocol {
7650
- limit = 1800;
7651
- transactionLimit = 10_000;
7654
+ get limit() {
7655
+ return limit;
7656
+ }
7657
+ get transactionLimit() {
7658
+ return transactionLimit;
7659
+ }
7660
+ get requestTimeout() {
7661
+ return requestTimeout;
7662
+ }
7663
+ get syncTimeout() {
7664
+ return syncTimeout;
7665
+ }
7652
7666
  }
7653
7667
 
7654
7668
  class Transaction extends Protocol {
@@ -8009,7 +8023,7 @@ class Chain extends Contract {
8009
8023
  const timeout = setTimeout(() => {
8010
8024
  resolve([{ index: 0, hash: '0x0' }]);
8011
8025
  globalThis.debug('sync timed out');
8012
- }, 10_000);
8026
+ }, this.requestTimeout);
8013
8027
  promises = await Promise.allSettled(promises);
8014
8028
  promises = promises.filter(({ status }) => status === 'fulfilled');
8015
8029
  clearTimeout(timeout);
@@ -8057,11 +8071,11 @@ class Chain extends Contract {
8057
8071
  const hash = await message.hash();
8058
8072
  if (hash !== latest.hash)
8059
8073
  throw new Error('invalid block @getLatestBlock');
8060
- let data = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
8061
- let node = await globalThis.peernet.prepareMessage(data);
8062
- const peer = promises[0].peer;
8063
8074
  latest = { ...message.decoded, hash };
8075
+ const peer = promises[0].peer;
8064
8076
  if (peer.connected && peer.version === this.version) {
8077
+ let data = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
8078
+ let node = await globalThis.peernet.prepareMessage(data);
8065
8079
  let message = await peer.request(node);
8066
8080
  message = await new globalThis.peernet.protos['peernet-response'](message);
8067
8081
  this.#knownBlocks = message.decoded.response;
@@ -8171,7 +8185,7 @@ class Chain extends Contract {
8171
8185
  let current;
8172
8186
  const timeout = () => current = setTimeout(() => {
8173
8187
  if (this.#chainSyncing) {
8174
- if (this.#lastResolved + 10000 > Date.now())
8188
+ if (this.#lastResolved + this.syncTimeout > Date.now())
8175
8189
  timeout();
8176
8190
  else {
8177
8191
  this.#chainSyncing = false;
@@ -8179,7 +8193,7 @@ class Chain extends Contract {
8179
8193
  this.#syncChain(lastBlock);
8180
8194
  }
8181
8195
  }
8182
- }, 10000);
8196
+ }, this.syncTimeout);
8183
8197
  timeout();
8184
8198
  if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
8185
8199
  return;
@@ -8219,28 +8233,36 @@ class Chain extends Contract {
8219
8233
  return response.decoded.response;
8220
8234
  }
8221
8235
  async #peerConnected(peer) {
8222
- console.log(peer);
8223
8236
  if (!peer.version || peer.version !== this.version)
8224
8237
  return;
8225
8238
  const lastBlock = await this.#makeRequest(peer, 'lastBlock');
8226
- if (!this.#lastBlock || lastBlock && lastBlock.index > this.#lastBlock.index && lastBlock.hash === this.#lastBlock?.hash) {
8227
- this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
8228
- let pool = await this.#makeRequest(peer, 'transactionPool');
8229
- pool = await Promise.all(pool.map(async (hash) => {
8230
- const has = await globalThis.peernet.has(hash);
8231
- return { has, hash };
8232
- }));
8233
- pool = pool.filter(item => !item.has);
8234
- await Promise.all(pool.map(async ({ hash }) => {
8235
- const result = await globalThis.peernet.get(hash);
8236
- // result could be undefined cause invalid/double transactions could be deleted already
8237
- if (!result)
8238
- console.log(result);
8239
- if (result) {
8240
- const node = await new TransactionMessage(result);
8241
- await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
8242
- }
8243
- }));
8239
+ if (lastBlock.index > this.#lastBlock?.index) {
8240
+ // this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks')
8241
+ try {
8242
+ console.log('getting pool');
8243
+ let pool = await this.#makeRequest(peer, 'transactionPool');
8244
+ console.log('got pool');
8245
+ pool = await Promise.all(pool.map(async (hash) => {
8246
+ const has = await globalThis.peernet.has(hash);
8247
+ return { has, hash };
8248
+ }));
8249
+ pool = pool.filter(item => !item.has);
8250
+ await Promise.all(pool.map(async ({ hash }) => {
8251
+ const result = await globalThis.peernet.get(hash);
8252
+ // result could be undefined cause invalid/double transactions could be deleted already
8253
+ if (!result)
8254
+ console.log(result);
8255
+ if (result) {
8256
+ const node = await new TransactionMessage(result);
8257
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
8258
+ }
8259
+ }));
8260
+ }
8261
+ catch (error) {
8262
+ console.log(error);
8263
+ console.log('error fetching transactionPool');
8264
+ }
8265
+ console.log(lastBlock);
8244
8266
  if (lastBlock)
8245
8267
  await this.#syncChain(lastBlock);
8246
8268
  if (await this.hasTransactionToHandle())
@@ -8262,13 +8284,16 @@ class Chain extends Contract {
8262
8284
  return new globalThis.peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
8263
8285
  }
8264
8286
  async getAndPutBlock(hash) {
8287
+ // todo peernet resolves undefined blocks....
8265
8288
  let block = await globalThis.peernet.get(hash, 'block');
8266
- block = await new BlockMessage(block);
8267
- const { index } = block.decoded;
8268
- if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
8269
- throw `invalid block ${hash} @${index}`;
8270
- if (!await globalThis.peernet.has(hash, 'block'))
8271
- await globalThis.peernet.put(hash, block.encoded, 'block');
8289
+ if (block !== undefined) {
8290
+ block = await new BlockMessage(block);
8291
+ const { index } = block.decoded;
8292
+ if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
8293
+ throw `invalid block ${hash} @${index}`;
8294
+ if (!await globalThis.peernet.has(hash, 'block'))
8295
+ await globalThis.peernet.put(hash, block.encoded, 'block');
8296
+ }
8272
8297
  return block;
8273
8298
  }
8274
8299
  async resolveBlock(hash) {
@@ -8342,12 +8367,14 @@ class Chain extends Contract {
8342
8367
  }
8343
8368
  catch (error) {
8344
8369
  console.log(error);
8370
+ return false;
8345
8371
  }
8346
8372
  }
8347
8373
  this.#blocks[block.index].loaded = true;
8348
8374
  globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
8349
8375
  }
8350
8376
  }
8377
+ return true;
8351
8378
  }
8352
8379
  async #executeTransaction({ hash, from, to, method, params, nonce }) {
8353
8380
  try {
@@ -8439,7 +8466,6 @@ class Chain extends Contract {
8439
8466
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
8440
8467
  if (Object.keys(transactions)?.length === 0)
8441
8468
  return;
8442
- await globalThis.transactionPoolStore.keys();
8443
8469
  const timestamp = Date.now();
8444
8470
  let block = {
8445
8471
  transactions: [],
package/exports/chain.js CHANGED
@@ -232,9 +232,23 @@ class State {
232
232
  }
233
233
  }
234
234
 
235
+ const limit = 1800;
236
+ const transactionLimit = 1800;
237
+ const requestTimeout = 30_000;
238
+ const syncTimeout = 30_000;
235
239
  class Protocol {
236
- limit = 1800;
237
- transactionLimit = 10_000;
240
+ get limit() {
241
+ return limit;
242
+ }
243
+ get transactionLimit() {
244
+ return transactionLimit;
245
+ }
246
+ get requestTimeout() {
247
+ return requestTimeout;
248
+ }
249
+ get syncTimeout() {
250
+ return syncTimeout;
251
+ }
238
252
  }
239
253
 
240
254
  class Transaction extends Protocol {
@@ -595,7 +609,7 @@ class Chain extends Contract {
595
609
  const timeout = setTimeout(() => {
596
610
  resolve([{ index: 0, hash: '0x0' }]);
597
611
  globalThis.debug('sync timed out');
598
- }, 10_000);
612
+ }, this.requestTimeout);
599
613
  promises = await Promise.allSettled(promises);
600
614
  promises = promises.filter(({ status }) => status === 'fulfilled');
601
615
  clearTimeout(timeout);
@@ -643,11 +657,11 @@ class Chain extends Contract {
643
657
  const hash = await message.hash();
644
658
  if (hash !== latest.hash)
645
659
  throw new Error('invalid block @getLatestBlock');
646
- let data = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
647
- let node = await globalThis.peernet.prepareMessage(data);
648
- const peer = promises[0].peer;
649
660
  latest = { ...message.decoded, hash };
661
+ const peer = promises[0].peer;
650
662
  if (peer.connected && peer.version === this.version) {
663
+ let data = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
664
+ let node = await globalThis.peernet.prepareMessage(data);
651
665
  let message = await peer.request(node);
652
666
  message = await new globalThis.peernet.protos['peernet-response'](message);
653
667
  this.#knownBlocks = message.decoded.response;
@@ -757,7 +771,7 @@ class Chain extends Contract {
757
771
  let current;
758
772
  const timeout = () => current = setTimeout(() => {
759
773
  if (this.#chainSyncing) {
760
- if (this.#lastResolved + 10000 > Date.now())
774
+ if (this.#lastResolved + this.syncTimeout > Date.now())
761
775
  timeout();
762
776
  else {
763
777
  this.#chainSyncing = false;
@@ -765,7 +779,7 @@ class Chain extends Contract {
765
779
  this.#syncChain(lastBlock);
766
780
  }
767
781
  }
768
- }, 10000);
782
+ }, this.syncTimeout);
769
783
  timeout();
770
784
  if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
771
785
  return;
@@ -805,28 +819,36 @@ class Chain extends Contract {
805
819
  return response.decoded.response;
806
820
  }
807
821
  async #peerConnected(peer) {
808
- console.log(peer);
809
822
  if (!peer.version || peer.version !== this.version)
810
823
  return;
811
824
  const lastBlock = await this.#makeRequest(peer, 'lastBlock');
812
- if (!this.#lastBlock || lastBlock && lastBlock.index > this.#lastBlock.index && lastBlock.hash === this.#lastBlock?.hash) {
813
- this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
814
- let pool = await this.#makeRequest(peer, 'transactionPool');
815
- pool = await Promise.all(pool.map(async (hash) => {
816
- const has = await globalThis.peernet.has(hash);
817
- return { has, hash };
818
- }));
819
- pool = pool.filter(item => !item.has);
820
- await Promise.all(pool.map(async ({ hash }) => {
821
- const result = await globalThis.peernet.get(hash);
822
- // result could be undefined cause invalid/double transactions could be deleted already
823
- if (!result)
824
- console.log(result);
825
- if (result) {
826
- const node = await new TransactionMessage(result);
827
- await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
828
- }
829
- }));
825
+ if (lastBlock.index > this.#lastBlock?.index) {
826
+ // this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks')
827
+ try {
828
+ console.log('getting pool');
829
+ let pool = await this.#makeRequest(peer, 'transactionPool');
830
+ console.log('got pool');
831
+ pool = await Promise.all(pool.map(async (hash) => {
832
+ const has = await globalThis.peernet.has(hash);
833
+ return { has, hash };
834
+ }));
835
+ pool = pool.filter(item => !item.has);
836
+ await Promise.all(pool.map(async ({ hash }) => {
837
+ const result = await globalThis.peernet.get(hash);
838
+ // result could be undefined cause invalid/double transactions could be deleted already
839
+ if (!result)
840
+ console.log(result);
841
+ if (result) {
842
+ const node = await new TransactionMessage(result);
843
+ await globalThis.transactionPoolStore.put(await node.hash(), node.encoded);
844
+ }
845
+ }));
846
+ }
847
+ catch (error) {
848
+ console.log(error);
849
+ console.log('error fetching transactionPool');
850
+ }
851
+ console.log(lastBlock);
830
852
  if (lastBlock)
831
853
  await this.#syncChain(lastBlock);
832
854
  if (await this.hasTransactionToHandle())
@@ -848,13 +870,16 @@ class Chain extends Contract {
848
870
  return new globalThis.peernet.protos['peernet-response']({ response: { blocks: this.#blocks.map((block) => block.hash) } });
849
871
  }
850
872
  async getAndPutBlock(hash) {
873
+ // todo peernet resolves undefined blocks....
851
874
  let block = await globalThis.peernet.get(hash, 'block');
852
- block = await new BlockMessage(block);
853
- const { index } = block.decoded;
854
- if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
855
- throw `invalid block ${hash} @${index}`;
856
- if (!await globalThis.peernet.has(hash, 'block'))
857
- await globalThis.peernet.put(hash, block.encoded, 'block');
875
+ if (block !== undefined) {
876
+ block = await new BlockMessage(block);
877
+ const { index } = block.decoded;
878
+ if (this.#blocks[index] && this.#blocks[index].hash !== block.hash)
879
+ throw `invalid block ${hash} @${index}`;
880
+ if (!await globalThis.peernet.has(hash, 'block'))
881
+ await globalThis.peernet.put(hash, block.encoded, 'block');
882
+ }
858
883
  return block;
859
884
  }
860
885
  async resolveBlock(hash) {
@@ -928,12 +953,14 @@ class Chain extends Contract {
928
953
  }
929
954
  catch (error) {
930
955
  console.log(error);
956
+ return false;
931
957
  }
932
958
  }
933
959
  this.#blocks[block.index].loaded = true;
934
960
  globalThis.debug(`loaded block: ${block.hash} @${block.index}`);
935
961
  }
936
962
  }
963
+ return true;
937
964
  }
938
965
  async #executeTransaction({ hash, from, to, method, params, nonce }) {
939
966
  try {
@@ -1025,7 +1052,6 @@ class Chain extends Contract {
1025
1052
  let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
1026
1053
  if (Object.keys(transactions)?.length === 0)
1027
1054
  return;
1028
- await globalThis.transactionPoolStore.keys();
1029
1055
  const timestamp = Date.now();
1030
1056
  let block = {
1031
1057
  transactions: [],
@@ -1,4 +1,11 @@
1
- export default class Protocol {
2
- limit: number;
3
- transactionLimit: number;
1
+ export declare const limit = 1800;
2
+ export declare const transactionLimit = 1800;
3
+ export declare const requestTimeout = 30000;
4
+ export declare const syncTimeout = 30000;
5
+ export declare class Protocol {
6
+ get limit(): number;
7
+ get transactionLimit(): number;
8
+ get requestTimeout(): number;
9
+ get syncTimeout(): number;
4
10
  }
11
+ export { Protocol as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.51",
3
+ "version": "1.4.52",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": "./exports/node.js",