@leofcoin/chain 1.9.20 → 1.9.21

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.
@@ -5137,7 +5137,11 @@ class State extends Contract {
5137
5137
  let index = this.#blockHashMap.get(hash);
5138
5138
  let localHash = "0x0";
5139
5139
  try {
5140
- localHash = await globalThis.stateStore.get("lastBlock");
5140
+ const rawLocalHash = await globalThis.stateStore.get("lastBlock");
5141
+ if (rawLocalHash) {
5142
+ const decoded = JSON.parse(new TextDecoder().decode(rawLocalHash));
5143
+ localHash = typeof decoded === "string" ? decoded : decoded?.hash ?? "0x0";
5144
+ }
5141
5145
  } catch (error) {
5142
5146
  debug$1("no local state found");
5143
5147
  }
@@ -5367,10 +5371,21 @@ class State extends Contract {
5367
5371
  compatiblePeerCount += 1;
5368
5372
  const task = async () => {
5369
5373
  try {
5370
- const result = await peer.request(node.encoded);
5374
+ let result = await peer.request(node.encoded);
5371
5375
  const resultType = result instanceof Uint8Array ? `bytes:${result.length}` : typeof result;
5372
5376
  debug$1(`lastBlock result type: ${resultType}`);
5373
5377
  console.log({ result });
5378
+ if (result instanceof Uint8Array) {
5379
+ for (let i = 0; i < 3; i += 1) {
5380
+ try {
5381
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](result);
5382
+ if (wrapped?.decoded?.response === void 0) break;
5383
+ result = wrapped.decoded.response;
5384
+ } catch {
5385
+ break;
5386
+ }
5387
+ }
5388
+ }
5374
5389
  return { result: new LastBlockMessage(result), peer };
5375
5390
  } catch (error) {
5376
5391
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
@@ -5447,6 +5462,9 @@ class State extends Contract {
5447
5462
  */
5448
5463
  async #loadBlocks(blocks) {
5449
5464
  this.#chainState = "loading";
5465
+ if (blocks.some((block) => !block)) {
5466
+ throw new Error("missing block data during load; chain resolution incomplete");
5467
+ }
5450
5468
  const poolTransactionKeys = new Set(await globalThis.transactionPoolStore.keys());
5451
5469
  debug$1(`pool transactions: ${poolTransactionKeys.size}`);
5452
5470
  debug$1(`loading ${blocks.length} blocks`);
@@ -6385,22 +6403,21 @@ class Chain extends VersionControl {
6385
6403
  if (response === void 0 || response === null) return response;
6386
6404
  const normalizeResponse = async (payload) => {
6387
6405
  if (payload === void 0 || payload === null) return payload;
6388
- if (payload && typeof payload === "object") {
6389
- if ("decoded" in payload && payload?.decoded && "response" in payload.decoded) {
6390
- return normalizeResponse(payload.decoded.response);
6391
- }
6392
- if ("response" in payload) {
6393
- return normalizeResponse(payload.response);
6394
- }
6395
- }
6396
6406
  if (payload instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(payload)) {
6397
- const bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
6398
- try {
6399
- const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
6400
- if (wrapped?.decoded?.response !== void 0) {
6401
- return wrapped.decoded.response;
6407
+ let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
6408
+ for (let i = 0; i < 3; i += 1) {
6409
+ try {
6410
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
6411
+ if (wrapped?.decoded?.response === void 0) break;
6412
+ const next = wrapped.decoded.response;
6413
+ if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
6414
+ bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
6415
+ continue;
6416
+ }
6417
+ return normalizeResponse(next);
6418
+ } catch {
6419
+ break;
6402
6420
  }
6403
- } catch {
6404
6421
  }
6405
6422
  try {
6406
6423
  return JSON.parse(new TextDecoder().decode(bytes));
@@ -6408,6 +6425,16 @@ class Chain extends VersionControl {
6408
6425
  return bytes;
6409
6426
  }
6410
6427
  }
6428
+ if (payload && typeof payload === "object") {
6429
+ const objectPayload = payload;
6430
+ const decoded = objectPayload.decoded;
6431
+ if ("decoded" in objectPayload && decoded && "response" in decoded) {
6432
+ return normalizeResponse(decoded.response);
6433
+ }
6434
+ if ("response" in objectPayload) {
6435
+ return normalizeResponse(objectPayload.response);
6436
+ }
6437
+ }
6411
6438
  if (typeof payload === "string") {
6412
6439
  try {
6413
6440
  return JSON.parse(payload);
@@ -6475,8 +6502,19 @@ class Chain extends VersionControl {
6475
6502
  let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
6476
6503
  console.log("raw response for request transactionPool:", transactionsInPool);
6477
6504
  if (transactionsInPool instanceof Uint8Array) {
6478
- debug("transactionPool response must be decoded array payload");
6479
- return [];
6505
+ try {
6506
+ transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
6507
+ } catch {
6508
+ debug("transactionPool response must be decoded array payload");
6509
+ return [];
6510
+ }
6511
+ }
6512
+ if (typeof transactionsInPool === "string") {
6513
+ try {
6514
+ transactionsInPool = JSON.parse(transactionsInPool);
6515
+ } catch {
6516
+ return [];
6517
+ }
6480
6518
  }
6481
6519
  if (!Array.isArray(transactionsInPool)) return [];
6482
6520
  const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
@@ -6561,9 +6599,9 @@ class Chain extends VersionControl {
6561
6599
  debug(`peer has no lastBlock: ${peerId}`);
6562
6600
  return;
6563
6601
  }
6564
- const higherThenCurrentLocal = !localBlock?.index ? true : lastBlock.index > localBlock.index;
6602
+ const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
6565
6603
  if (lastBlock) {
6566
- if (!this.lastBlock || higherThenCurrentLocal) {
6604
+ if (!localBlock || higherThenCurrentLocal) {
6567
6605
  try {
6568
6606
  const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
6569
6607
  const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
@@ -6591,9 +6629,8 @@ class Chain extends VersionControl {
6591
6629
  }
6592
6630
  if (this.wantList.length > 0) {
6593
6631
  const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash, "block")));
6594
- for (let i = 0; i < promises.length; i++) {
6595
- const result = promises[i];
6596
- if (result.status === "fulfilled") this.wantList.splice(i, 1);
6632
+ for (let i = promises.length - 1; i >= 0; i -= 1) {
6633
+ if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
6597
6634
  }
6598
6635
  if (this.wantList.length === 0) await this.triggerSync();
6599
6636
  }
package/exports/chain.js CHANGED
@@ -1343,7 +1343,11 @@ class State extends Contract {
1343
1343
  let index = this.#blockHashMap.get(hash);
1344
1344
  let localHash = "0x0";
1345
1345
  try {
1346
- localHash = await globalThis.stateStore.get("lastBlock");
1346
+ const rawLocalHash = await globalThis.stateStore.get("lastBlock");
1347
+ if (rawLocalHash) {
1348
+ const decoded = JSON.parse(new TextDecoder().decode(rawLocalHash));
1349
+ localHash = typeof decoded === "string" ? decoded : decoded?.hash ?? "0x0";
1350
+ }
1347
1351
  } catch (error) {
1348
1352
  debug$1("no local state found");
1349
1353
  }
@@ -1573,10 +1577,21 @@ class State extends Contract {
1573
1577
  compatiblePeerCount += 1;
1574
1578
  const task = async () => {
1575
1579
  try {
1576
- const result = await peer.request(node.encoded);
1580
+ let result = await peer.request(node.encoded);
1577
1581
  const resultType = result instanceof Uint8Array ? `bytes:${result.length}` : typeof result;
1578
1582
  debug$1(`lastBlock result type: ${resultType}`);
1579
1583
  console.log({ result });
1584
+ if (result instanceof Uint8Array) {
1585
+ for (let i = 0; i < 3; i += 1) {
1586
+ try {
1587
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](result);
1588
+ if (wrapped?.decoded?.response === void 0) break;
1589
+ result = wrapped.decoded.response;
1590
+ } catch {
1591
+ break;
1592
+ }
1593
+ }
1594
+ }
1580
1595
  return { result: new LastBlockMessage(result), peer };
1581
1596
  } catch (error) {
1582
1597
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
@@ -1653,6 +1668,9 @@ class State extends Contract {
1653
1668
  */
1654
1669
  async #loadBlocks(blocks) {
1655
1670
  this.#chainState = "loading";
1671
+ if (blocks.some((block) => !block)) {
1672
+ throw new Error("missing block data during load; chain resolution incomplete");
1673
+ }
1656
1674
  const poolTransactionKeys = new Set(await globalThis.transactionPoolStore.keys());
1657
1675
  debug$1(`pool transactions: ${poolTransactionKeys.size}`);
1658
1676
  debug$1(`loading ${blocks.length} blocks`);
@@ -2591,22 +2609,21 @@ class Chain extends VersionControl {
2591
2609
  if (response === void 0 || response === null) return response;
2592
2610
  const normalizeResponse = async (payload) => {
2593
2611
  if (payload === void 0 || payload === null) return payload;
2594
- if (payload && typeof payload === "object") {
2595
- if ("decoded" in payload && payload?.decoded && "response" in payload.decoded) {
2596
- return normalizeResponse(payload.decoded.response);
2597
- }
2598
- if ("response" in payload) {
2599
- return normalizeResponse(payload.response);
2600
- }
2601
- }
2602
2612
  if (payload instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(payload)) {
2603
- const bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
2604
- try {
2605
- const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
2606
- if (wrapped?.decoded?.response !== void 0) {
2607
- return wrapped.decoded.response;
2613
+ let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
2614
+ for (let i = 0; i < 3; i += 1) {
2615
+ try {
2616
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
2617
+ if (wrapped?.decoded?.response === void 0) break;
2618
+ const next = wrapped.decoded.response;
2619
+ if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
2620
+ bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
2621
+ continue;
2622
+ }
2623
+ return normalizeResponse(next);
2624
+ } catch {
2625
+ break;
2608
2626
  }
2609
- } catch {
2610
2627
  }
2611
2628
  try {
2612
2629
  return JSON.parse(new TextDecoder().decode(bytes));
@@ -2614,6 +2631,16 @@ class Chain extends VersionControl {
2614
2631
  return bytes;
2615
2632
  }
2616
2633
  }
2634
+ if (payload && typeof payload === "object") {
2635
+ const objectPayload = payload;
2636
+ const decoded = objectPayload.decoded;
2637
+ if ("decoded" in objectPayload && decoded && "response" in decoded) {
2638
+ return normalizeResponse(decoded.response);
2639
+ }
2640
+ if ("response" in objectPayload) {
2641
+ return normalizeResponse(objectPayload.response);
2642
+ }
2643
+ }
2617
2644
  if (typeof payload === "string") {
2618
2645
  try {
2619
2646
  return JSON.parse(payload);
@@ -2681,8 +2708,19 @@ class Chain extends VersionControl {
2681
2708
  let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
2682
2709
  console.log("raw response for request transactionPool:", transactionsInPool);
2683
2710
  if (transactionsInPool instanceof Uint8Array) {
2684
- debug("transactionPool response must be decoded array payload");
2685
- return [];
2711
+ try {
2712
+ transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
2713
+ } catch {
2714
+ debug("transactionPool response must be decoded array payload");
2715
+ return [];
2716
+ }
2717
+ }
2718
+ if (typeof transactionsInPool === "string") {
2719
+ try {
2720
+ transactionsInPool = JSON.parse(transactionsInPool);
2721
+ } catch {
2722
+ return [];
2723
+ }
2686
2724
  }
2687
2725
  if (!Array.isArray(transactionsInPool)) return [];
2688
2726
  const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
@@ -2767,9 +2805,9 @@ class Chain extends VersionControl {
2767
2805
  debug(`peer has no lastBlock: ${peerId}`);
2768
2806
  return;
2769
2807
  }
2770
- const higherThenCurrentLocal = !localBlock?.index ? true : lastBlock.index > localBlock.index;
2808
+ const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
2771
2809
  if (lastBlock) {
2772
- if (!this.lastBlock || higherThenCurrentLocal) {
2810
+ if (!localBlock || higherThenCurrentLocal) {
2773
2811
  try {
2774
2812
  const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
2775
2813
  const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
@@ -2797,9 +2835,8 @@ class Chain extends VersionControl {
2797
2835
  }
2798
2836
  if (this.wantList.length > 0) {
2799
2837
  const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash, "block")));
2800
- for (let i = 0; i < promises.length; i++) {
2801
- const result = promises[i];
2802
- if (result.status === "fulfilled") this.wantList.splice(i, 1);
2838
+ for (let i = promises.length - 1; i >= 0; i -= 1) {
2839
+ if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
2803
2840
  }
2804
2841
  if (this.wantList.length === 0) await this.triggerSync();
2805
2842
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.9.20",
3
+ "version": "1.9.21",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {