@leofcoin/chain 1.9.22 → 1.9.24

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.
@@ -4947,6 +4947,42 @@ class State extends Contract {
4947
4947
  get resolving() {
4948
4948
  return this.#resolving;
4949
4949
  }
4950
+ async #resolveLastBlockMessage(result) {
4951
+ if (result instanceof Uint8Array) {
4952
+ try {
4953
+ let payload = result;
4954
+ for (let i = 0; i < 3; i += 1) {
4955
+ try {
4956
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
4957
+ if (wrapped?.decoded?.response === void 0) break;
4958
+ payload = wrapped.decoded.response;
4959
+ } catch {
4960
+ break;
4961
+ }
4962
+ }
4963
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
4964
+ return new LastBlockMessage(result);
4965
+ } catch {
4966
+ const candidate = new TextDecoder().decode(result);
4967
+ if (candidate) {
4968
+ const blockData = await globalThis.peernet.get(candidate, "block");
4969
+ if (blockData) return new BlockMessage(blockData);
4970
+ }
4971
+ }
4972
+ }
4973
+ if (typeof result === "string") {
4974
+ const blockData = await globalThis.peernet.get(result, "block");
4975
+ if (blockData) return new BlockMessage(blockData);
4976
+ }
4977
+ if (result && typeof result === "object") {
4978
+ const response = result.decoded?.response ?? result.response;
4979
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
4980
+ if ("hash" in result && "index" in result) {
4981
+ return new LastBlockMessage(result);
4982
+ }
4983
+ }
4984
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
4985
+ }
4950
4986
  get contracts() {
4951
4987
  return this.#machine.contracts;
4952
4988
  }
@@ -5386,7 +5422,7 @@ class State extends Contract {
5386
5422
  }
5387
5423
  }
5388
5424
  }
5389
- return { result: new LastBlockMessage(result), peer };
5425
+ return { result, peer };
5390
5426
  } catch (error) {
5391
5427
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
5392
5428
  debug$1(`lastBlock request failed: ${peerId}:`, error?.message ?? error);
@@ -5407,6 +5443,12 @@ class State extends Contract {
5407
5443
  throw new ResolveError("latestBlock: no responses from compatible peers");
5408
5444
  }
5409
5445
  console.log({ promises });
5446
+ promises = await Promise.all(
5447
+ promises.map(async (item) => ({
5448
+ value: (await this.#resolveLastBlockMessage(item.value)).decoded,
5449
+ peer: item.peer
5450
+ }))
5451
+ );
5410
5452
  let latest = { index: 0, hash: "0x0", previousHash: "0x0" };
5411
5453
  promises = promises.sort((a, b) => b.index - a.index);
5412
5454
  if (promises.length > 0) latest = promises[0].value;
@@ -6453,6 +6495,42 @@ class Chain extends VersionControl {
6453
6495
  throw error;
6454
6496
  }
6455
6497
  }
6498
+ async #resolveLastBlockMessage(result) {
6499
+ if (result instanceof Uint8Array) {
6500
+ try {
6501
+ let payload = result;
6502
+ for (let i = 0; i < 3; i += 1) {
6503
+ try {
6504
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
6505
+ if (wrapped?.decoded?.response === void 0) break;
6506
+ payload = wrapped.decoded.response;
6507
+ } catch {
6508
+ break;
6509
+ }
6510
+ }
6511
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
6512
+ return new LastBlockMessage(result);
6513
+ } catch {
6514
+ const candidate = new TextDecoder().decode(result);
6515
+ if (candidate) {
6516
+ const blockData = await globalThis.peernet.get(candidate, "block");
6517
+ if (blockData) return new BlockMessage(blockData);
6518
+ }
6519
+ }
6520
+ }
6521
+ if (typeof result === "string") {
6522
+ const blockData = await globalThis.peernet.get(result, "block");
6523
+ if (blockData) return new BlockMessage(blockData);
6524
+ }
6525
+ if (result && typeof result === "object") {
6526
+ const response = result.decoded?.response ?? result.response;
6527
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
6528
+ if ("hash" in result && "index" in result) {
6529
+ return new LastBlockMessage(result);
6530
+ }
6531
+ }
6532
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
6533
+ }
6456
6534
  async #decodeKnownBlocksResponse(response) {
6457
6535
  if (!response) return null;
6458
6536
  if (Array.isArray(response)) {
@@ -6578,11 +6656,12 @@ class Chain extends VersionControl {
6578
6656
  console.log(await this.lastBlock);
6579
6657
  const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
6580
6658
  console.log("raw last block response:", lastBlockRaw);
6581
- if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
6659
+ if (lastBlockRaw === void 0 || lastBlockRaw === null) {
6582
6660
  throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
6583
6661
  }
6584
- console.log(new LastBlockMessage(lastBlockRaw));
6585
- lastBlock = new LastBlockMessage(lastBlockRaw).decoded;
6662
+ const lastBlockMessage = await this.#resolveLastBlockMessage(lastBlockRaw);
6663
+ console.log(lastBlockMessage);
6664
+ lastBlock = lastBlockMessage.decoded;
6586
6665
  } catch (error) {
6587
6666
  const peerName = peer?.peerId || peer?.id || peer?.address || peerId || "unknown";
6588
6667
  debug(`lastBlock request failed: ${peerName}:`, error?.message ?? error);
package/exports/chain.js CHANGED
@@ -1154,6 +1154,42 @@ class State extends Contract {
1154
1154
  get resolving() {
1155
1155
  return this.#resolving;
1156
1156
  }
1157
+ async #resolveLastBlockMessage(result) {
1158
+ if (result instanceof Uint8Array) {
1159
+ try {
1160
+ let payload = result;
1161
+ for (let i = 0; i < 3; i += 1) {
1162
+ try {
1163
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
1164
+ if (wrapped?.decoded?.response === void 0) break;
1165
+ payload = wrapped.decoded.response;
1166
+ } catch {
1167
+ break;
1168
+ }
1169
+ }
1170
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
1171
+ return new LastBlockMessage(result);
1172
+ } catch {
1173
+ const candidate = new TextDecoder().decode(result);
1174
+ if (candidate) {
1175
+ const blockData = await globalThis.peernet.get(candidate, "block");
1176
+ if (blockData) return new BlockMessage(blockData);
1177
+ }
1178
+ }
1179
+ }
1180
+ if (typeof result === "string") {
1181
+ const blockData = await globalThis.peernet.get(result, "block");
1182
+ if (blockData) return new BlockMessage(blockData);
1183
+ }
1184
+ if (result && typeof result === "object") {
1185
+ const response = result.decoded?.response ?? result.response;
1186
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
1187
+ if ("hash" in result && "index" in result) {
1188
+ return new LastBlockMessage(result);
1189
+ }
1190
+ }
1191
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
1192
+ }
1157
1193
  get contracts() {
1158
1194
  return this.#machine.contracts;
1159
1195
  }
@@ -1593,7 +1629,7 @@ class State extends Contract {
1593
1629
  }
1594
1630
  }
1595
1631
  }
1596
- return { result: new LastBlockMessage(result), peer };
1632
+ return { result, peer };
1597
1633
  } catch (error) {
1598
1634
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
1599
1635
  debug$1(`lastBlock request failed: ${peerId}:`, error?.message ?? error);
@@ -1614,6 +1650,12 @@ class State extends Contract {
1614
1650
  throw new ResolveError("latestBlock: no responses from compatible peers");
1615
1651
  }
1616
1652
  console.log({ promises });
1653
+ promises = await Promise.all(
1654
+ promises.map(async (item) => ({
1655
+ value: (await this.#resolveLastBlockMessage(item.value)).decoded,
1656
+ peer: item.peer
1657
+ }))
1658
+ );
1617
1659
  let latest = { index: 0, hash: "0x0", previousHash: "0x0" };
1618
1660
  promises = promises.sort((a, b) => b.index - a.index);
1619
1661
  if (promises.length > 0) latest = promises[0].value;
@@ -2660,6 +2702,42 @@ class Chain extends VersionControl {
2660
2702
  throw error;
2661
2703
  }
2662
2704
  }
2705
+ async #resolveLastBlockMessage(result) {
2706
+ if (result instanceof Uint8Array) {
2707
+ try {
2708
+ let payload = result;
2709
+ for (let i = 0; i < 3; i += 1) {
2710
+ try {
2711
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](payload);
2712
+ if (wrapped?.decoded?.response === void 0) break;
2713
+ payload = wrapped.decoded.response;
2714
+ } catch {
2715
+ break;
2716
+ }
2717
+ }
2718
+ if (payload !== result) return this.#resolveLastBlockMessage(payload);
2719
+ return new LastBlockMessage(result);
2720
+ } catch {
2721
+ const candidate = new TextDecoder().decode(result);
2722
+ if (candidate) {
2723
+ const blockData = await globalThis.peernet.get(candidate, "block");
2724
+ if (blockData) return new BlockMessage(blockData);
2725
+ }
2726
+ }
2727
+ }
2728
+ if (typeof result === "string") {
2729
+ const blockData = await globalThis.peernet.get(result, "block");
2730
+ if (blockData) return new BlockMessage(blockData);
2731
+ }
2732
+ if (result && typeof result === "object") {
2733
+ const response = result.decoded?.response ?? result.response;
2734
+ if (response !== void 0) return this.#resolveLastBlockMessage(response);
2735
+ if ("hash" in result && "index" in result) {
2736
+ return new LastBlockMessage(result);
2737
+ }
2738
+ }
2739
+ throw new Error(`invalid lastBlock payload: ${typeof result}`);
2740
+ }
2663
2741
  async #decodeKnownBlocksResponse(response) {
2664
2742
  if (!response) return null;
2665
2743
  if (Array.isArray(response)) {
@@ -2785,11 +2863,12 @@ class Chain extends VersionControl {
2785
2863
  console.log(await this.lastBlock);
2786
2864
  const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
2787
2865
  console.log("raw last block response:", lastBlockRaw);
2788
- if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
2866
+ if (lastBlockRaw === void 0 || lastBlockRaw === null) {
2789
2867
  throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
2790
2868
  }
2791
- console.log(new LastBlockMessage(lastBlockRaw));
2792
- lastBlock = new LastBlockMessage(lastBlockRaw).decoded;
2869
+ const lastBlockMessage = await this.#resolveLastBlockMessage(lastBlockRaw);
2870
+ console.log(lastBlockMessage);
2871
+ lastBlock = lastBlockMessage.decoded;
2793
2872
  } catch (error) {
2794
2873
  const peerName = peer?.peerId || peer?.id || peer?.address || peerId || "unknown";
2795
2874
  debug(`lastBlock request failed: ${peerName}:`, error?.message ?? error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.9.22",
3
+ "version": "1.9.24",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {
@@ -62,19 +62,19 @@
62
62
  "tslib": "^2.8.1"
63
63
  },
64
64
  "dependencies": {
65
- "@leofcoin/addresses": "^1.0.58",
66
- "@leofcoin/codecs": "^1.1.2",
67
- "@leofcoin/contracts": "^0.1.19",
68
- "@leofcoin/crypto": "^0.2.39",
69
- "@leofcoin/errors": "^1.0.28",
70
- "@leofcoin/lib": "^1.2.77",
71
- "@leofcoin/messages": "^1.5.2",
65
+ "@leofcoin/addresses": "^1.0.59",
66
+ "@leofcoin/codecs": "^1.1.3",
67
+ "@leofcoin/contracts": "^0.1.20",
68
+ "@leofcoin/crypto": "^0.2.40",
69
+ "@leofcoin/errors": "^1.0.29",
70
+ "@leofcoin/lib": "^1.2.78",
71
+ "@leofcoin/messages": "^1.5.3",
72
72
  "@leofcoin/multi-wallet": "^3.1.9",
73
- "@leofcoin/networks": "^1.1.28",
73
+ "@leofcoin/networks": "^1.1.29",
74
74
  "@leofcoin/peernet": "^1.2.21",
75
75
  "@leofcoin/storage": "^3.5.38",
76
- "@leofcoin/utils": "^1.1.42",
77
- "@leofcoin/workers": "^1.5.30",
76
+ "@leofcoin/utils": "^1.1.43",
77
+ "@leofcoin/workers": "^1.5.31",
78
78
  "@vandeurenglenn/base58": "^1.1.9",
79
79
  "@vandeurenglenn/easy-worker": "^1.0.3",
80
80
  "semver": "^7.8.2"