@leofcoin/chain 1.9.20 → 1.9.22

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.
@@ -1,6 +1,6 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-CYsu-WiL.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  function _mergeNamespaces$1(n, m) {
6
6
  m.forEach(function (e) {
@@ -1,6 +1,6 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-CYsu-WiL.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  function _mergeNamespaces$1(n, m) {
6
6
  m.forEach(function (e) {
@@ -1,4 +1,4 @@
1
- import { t as toBase58, T as TransactionMessage, C as ContractMessage, R as RawTransactionMessage, B as BlockMessage, L as LastBlockMessage, P as PROTOCOL_VERSION, a as REACHED_ONE_ZERO_ZERO, b as PrevoteMessage, c as PrecommitMessage, d as ProposalMessage, e as BWMessage, S as StateMessage, f as BWRequestMessage } from './constants-BCaq9RBn.js';
1
+ import { t as toBase58, T as TransactionMessage, C as ContractMessage, R as RawTransactionMessage, B as BlockMessage, L as LastBlockMessage, P as PROTOCOL_VERSION, a as REACHED_ONE_ZERO_ZERO, b as PrevoteMessage, c as PrecommitMessage, d as ProposalMessage, e as BWMessage, S as StateMessage, f as Codec, g as BWRequestMessage } from './constants-COpvMqFX.js';
2
2
 
3
3
  const getTargets = () => Array.from([]);
4
4
  const isEnabled = (target) => {
@@ -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`);
@@ -5815,7 +5833,7 @@ class ConnectionMonitor {
5815
5833
  try {
5816
5834
  const networkName = globalThis.peernet?.network;
5817
5835
  if (networkName && typeof networkName === "string") {
5818
- const { default: networks } = await import('./constants-BCaq9RBn.js').then(function (n) { return n.n; });
5836
+ const { default: networks } = await import('./constants-COpvMqFX.js').then(function (n) { return n.n; });
5819
5837
  const [mainKey, subKey] = networkName.split(":");
5820
5838
  const networkConfig = networks?.[mainKey]?.[subKey];
5821
5839
  if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
@@ -6385,22 +6403,23 @@ 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 codec = new Codec(bytes);
6411
+ if (codec.name !== "peernet-response") break;
6412
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
6413
+ if (wrapped?.decoded?.response === void 0) break;
6414
+ const next = wrapped.decoded.response;
6415
+ if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
6416
+ bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
6417
+ continue;
6418
+ }
6419
+ return normalizeResponse(next);
6420
+ } catch {
6421
+ break;
6402
6422
  }
6403
- } catch {
6404
6423
  }
6405
6424
  try {
6406
6425
  return JSON.parse(new TextDecoder().decode(bytes));
@@ -6408,6 +6427,16 @@ class Chain extends VersionControl {
6408
6427
  return bytes;
6409
6428
  }
6410
6429
  }
6430
+ if (payload && typeof payload === "object") {
6431
+ const objectPayload = payload;
6432
+ const decoded = objectPayload.decoded;
6433
+ if ("decoded" in objectPayload && decoded && "response" in decoded) {
6434
+ return normalizeResponse(decoded.response);
6435
+ }
6436
+ if ("response" in objectPayload) {
6437
+ return normalizeResponse(objectPayload.response);
6438
+ }
6439
+ }
6411
6440
  if (typeof payload === "string") {
6412
6441
  try {
6413
6442
  return JSON.parse(payload);
@@ -6475,8 +6504,19 @@ class Chain extends VersionControl {
6475
6504
  let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
6476
6505
  console.log("raw response for request transactionPool:", transactionsInPool);
6477
6506
  if (transactionsInPool instanceof Uint8Array) {
6478
- debug("transactionPool response must be decoded array payload");
6479
- return [];
6507
+ try {
6508
+ transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
6509
+ } catch {
6510
+ debug("transactionPool response must be decoded array payload");
6511
+ return [];
6512
+ }
6513
+ }
6514
+ if (typeof transactionsInPool === "string") {
6515
+ try {
6516
+ transactionsInPool = JSON.parse(transactionsInPool);
6517
+ } catch {
6518
+ return [];
6519
+ }
6480
6520
  }
6481
6521
  if (!Array.isArray(transactionsInPool)) return [];
6482
6522
  const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
@@ -6561,9 +6601,9 @@ class Chain extends VersionControl {
6561
6601
  debug(`peer has no lastBlock: ${peerId}`);
6562
6602
  return;
6563
6603
  }
6564
- const higherThenCurrentLocal = !localBlock?.index ? true : lastBlock.index > localBlock.index;
6604
+ const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
6565
6605
  if (lastBlock) {
6566
- if (!this.lastBlock || higherThenCurrentLocal) {
6606
+ if (!localBlock || higherThenCurrentLocal) {
6567
6607
  try {
6568
6608
  const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
6569
6609
  const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
@@ -6591,9 +6631,8 @@ class Chain extends VersionControl {
6591
6631
  }
6592
6632
  if (this.wantList.length > 0) {
6593
6633
  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);
6634
+ for (let i = promises.length - 1; i >= 0; i -= 1) {
6635
+ if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
6597
6636
  }
6598
6637
  if (this.wantList.length === 0) await this.triggerSync();
6599
6638
  }
@@ -1,6 +1,6 @@
1
- import { i as inflate_1, c as createDebugger, d as deflate_1, L as LittlePubSub } from './node-browser-CYsu-WiL.js';
1
+ import { i as inflate_1, c as createDebugger, d as deflate_1, L as LittlePubSub } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  class Api {
6
6
  _pubsub;
@@ -231,7 +231,7 @@ class SocketRequestClient {
231
231
  const init = async () => {
232
232
  // @ts-ignore
233
233
  if (!globalThis.WebSocket && !this.#experimentalWebsocket)
234
- globalThis.WebSocket = (await import('./browser-8BFql6K9-Cfm2axSE.js').then(function (n) { return n.b; })).default.w3cwebsocket;
234
+ globalThis.WebSocket = (await import('./browser-8BFql6K9-BMXXABIY.js').then(function (n) { return n.b; })).default.w3cwebsocket;
235
235
  const client = new WebSocket(this.#url, this.#protocol);
236
236
  if (this.#experimentalWebsocket) {
237
237
  client.addEventListener('error', this.onerror);
@@ -1302,7 +1302,7 @@ class Client {
1302
1302
  }
1303
1303
  }
1304
1304
  async #loadNodeWebrtcImplementation() {
1305
- const koushWrtcModule = await import('./browser-FVp_QbaL-DShoEW2_.js').then(function (n) { return n.b; });
1305
+ const koushWrtcModule = await import('./browser-FVp_QbaL-C-GWIuv6.js').then(function (n) { return n.b; });
1306
1306
  const koushWrtc = koushWrtcModule.default;
1307
1307
  if (!isWrtcImplementation(koushWrtc)) {
1308
1308
  throw new Error('@koush/wrtc does not match required wrtc contract');
@@ -7802,7 +7802,7 @@ const utils = {
7802
7802
  codecs: registry
7803
7803
  };
7804
7804
 
7805
- class Codec extends BasicInterface {
7805
+ let Codec$1 = class Codec extends BasicInterface {
7806
7806
  codecBuffer;
7807
7807
  codec;
7808
7808
  hashAlg;
@@ -7902,7 +7902,7 @@ class Codec extends BasicInterface {
7902
7902
  this.encoded = index$4.encode(codec);
7903
7903
  return this.encoded;
7904
7904
  }
7905
- }
7905
+ };
7906
7906
 
7907
7907
  /*!
7908
7908
  * hash-wasm (https://www.npmjs.com/package/hash-wasm)
@@ -8517,7 +8517,7 @@ class CodecHash extends BasicInterface {
8517
8517
  // For large buffers, only check first bytes for codec prefix
8518
8518
  // Codec prefixes are small (varint encoded), so 100 bytes is more than enough
8519
8519
  const checkBuffer = uint8Array.length > 100 ? uint8Array.subarray(0, 100) : uint8Array;
8520
- this.discoCodec = new Codec(checkBuffer);
8520
+ this.discoCodec = new Codec$1(checkBuffer);
8521
8521
  const name = this.discoCodec.name;
8522
8522
  if (name) {
8523
8523
  this.name = name;
@@ -8550,7 +8550,7 @@ class CodecHash extends BasicInterface {
8550
8550
  this.name = name;
8551
8551
  if (!buffer)
8552
8552
  throw new Error('buffer is required for encoding');
8553
- this.discoCodec = new Codec(this.name);
8553
+ this.discoCodec = new Codec$1(this.name);
8554
8554
  let hashAlg = this.discoCodec.hashAlg;
8555
8555
  const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
8556
8556
  if (hashAlg.includes('dbl')) {
@@ -8582,7 +8582,7 @@ class CodecHash extends BasicInterface {
8582
8582
  decode(buffer) {
8583
8583
  this.encoded = buffer;
8584
8584
  const codec = index$4.decode(buffer);
8585
- this.discoCodec = new Codec(codec, this.codecs);
8585
+ this.discoCodec = new Codec$1(codec, this.codecs);
8586
8586
  // TODO: validate codec
8587
8587
  buffer = buffer.slice(index$4.decode.bytes);
8588
8588
  this.size = index$4.decode(buffer);
@@ -8622,13 +8622,13 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8622
8622
  hasCodec() {
8623
8623
  if (!this.encoded)
8624
8624
  return false;
8625
- const codec = new Codec(this.encoded);
8625
+ const codec = new Codec$1(this.encoded);
8626
8626
  if (codec.name)
8627
8627
  return true;
8628
8628
  }
8629
8629
  decode(encoded) {
8630
8630
  encoded = encoded || this.encoded;
8631
- const codec = new Codec(encoded);
8631
+ const codec = new Codec$1(encoded);
8632
8632
  if (codec.codecBuffer) {
8633
8633
  encoded = encoded.slice(codec.codecBuffer.length);
8634
8634
  this.name = codec.name;
@@ -8646,7 +8646,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8646
8646
  encode(decoded) {
8647
8647
  let encoded;
8648
8648
  decoded = decoded || this.decoded;
8649
- const codec = new Codec(this.name);
8649
+ const codec = new Codec$1(this.name);
8650
8650
  if (decoded instanceof Uint8Array)
8651
8651
  encoded = decoded;
8652
8652
  else
@@ -8685,7 +8685,7 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8685
8685
  get contentBytes() {
8686
8686
  if (!this.encoded)
8687
8687
  return new Uint8Array(0);
8688
- const codec = new Codec(this.encoded);
8688
+ const codec = new Codec$1(this.encoded);
8689
8689
  if (codec.codecBuffer) {
8690
8690
  return this.encoded.slice(codec.codecBuffer.length);
8691
8691
  }
@@ -8765,6 +8765,10 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8765
8765
  * Interface for defining codec formats
8766
8766
  */
8767
8767
  const FormatInterface = FormatInterface$1;
8768
+ /**
8769
+ * Codec utility class
8770
+ */
8771
+ const Codec = Codec$1;
8768
8772
 
8769
8773
  var proto$b = {
8770
8774
  address: String(),
@@ -9040,4 +9044,4 @@ const DEFAULT_NODE_OPTIONS = {
9040
9044
  stars: networks.leofcoin.peach.stars
9041
9045
  };
9042
9046
 
9043
- export { BlockMessage as B, ContractMessage as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, PROTOCOL_VERSION as P, RawTransactionMessage as R, StateMessage as S, TransactionMessage as T, ValidatorMessage as V, REACHED_ONE_ZERO_ZERO as a, PrevoteMessage as b, PrecommitMessage as c, ProposalMessage as d, BWMessage as e, BWRequestMessage as f, networks$1 as n, toBase58 as t };
9047
+ export { BlockMessage as B, ContractMessage as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, PROTOCOL_VERSION as P, RawTransactionMessage as R, StateMessage as S, TransactionMessage as T, ValidatorMessage as V, REACHED_ONE_ZERO_ZERO as a, PrevoteMessage as b, PrecommitMessage as c, ProposalMessage as d, BWMessage as e, Codec as f, BWRequestMessage as g, networks$1 as n, toBase58 as t };
@@ -1,6 +1,6 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-CYsu-WiL.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  /**
6
6
  * @params {String} network
@@ -1,6 +1,6 @@
1
- import { F as FormatInterface } from './node-browser-CYsu-WiL.js';
1
+ import { F as FormatInterface } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
4
4
 
5
5
  var proto$b = {
6
6
  data: new Uint8Array(),
@@ -1,5 +1,5 @@
1
1
  import { createHash as createHash$1 } from 'crypto';
2
- import { T as TransactionMessage, C as ContractMessage, B as BlockMessage, e as BWMessage, f as BWRequestMessage, V as ValidatorMessage, S as StateMessage, L as LastBlockMessage, D as DEFAULT_NODE_OPTIONS } from './constants-BCaq9RBn.js';
2
+ import { T as TransactionMessage, C as ContractMessage, B as BlockMessage, e as BWMessage, g as BWRequestMessage, V as ValidatorMessage, S as StateMessage, L as LastBlockMessage, D as DEFAULT_NODE_OPTIONS } from './constants-COpvMqFX.js';
3
3
 
4
4
  var nodeConfig = async (config = {
5
5
  network: "leofcoin:peach",
@@ -8337,7 +8337,7 @@ class Identity {
8337
8337
  this.selectedAccount = new TextDecoder().decode(selected);
8338
8338
  }
8339
8339
  else {
8340
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-D6qd-AUn-q2EQ9wfH.js');
8340
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-D6qd-AUn-cJS9xh8w.js');
8341
8341
  const { identity, accounts } = await importee.default(password, this.network);
8342
8342
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
8343
8343
  await globalThis.walletStore.put('version', String(1));
@@ -16998,7 +16998,7 @@ class Peernet {
16998
16998
  await getAddress();
16999
16999
  this.storePrefix = options.storePrefix;
17000
17000
  this.root = options.root;
17001
- const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BfDvXW-x-CNuQ6tVL.js');
17001
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BfDvXW-x-BaHit_or.js');
17002
17002
  /**
17003
17003
  * proto Object containing protos
17004
17004
  * @type {Object}
@@ -17045,7 +17045,7 @@ class Peernet {
17045
17045
  if (this.#starting || this.#started)
17046
17046
  return;
17047
17047
  this.#starting = true;
17048
- const importee = await import('./client-lPe0SUWx-Cm8Ph6Z5.js');
17048
+ const importee = await import('./client-lPe0SUWx-DwxXCP5p.js');
17049
17049
  /**
17050
17050
  * @access public
17051
17051
  * @type {PeernetClient}
@@ -1,3 +1,3 @@
1
- export { N as default } from './node-browser-CYsu-WiL.js';
1
+ export { N as default } from './node-browser-Dc4HZiX0.js';
2
2
  import 'crypto';
3
- import './constants-BCaq9RBn.js';
3
+ import './constants-COpvMqFX.js';
package/exports/chain.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createDebugger } from '@vandeurenglenn/debug';
2
+ import { Codec } from '@leofcoin/codec-format-interface';
2
3
  import { jsonStringifyBigInt, jsonParseBigInt, formatBytes, parseUnits, formatUnits } from '@leofcoin/utils';
3
4
  import { TransactionMessage, BlockMessage, ContractMessage, LastBlockMessage, PrevoteMessage, PrecommitMessage, ProposalMessage, BWMessage, StateMessage, BWRequestMessage } from '@leofcoin/messages';
4
5
  import addresses, { contractFactory } from '@leofcoin/addresses';
@@ -1343,7 +1344,11 @@ class State extends Contract {
1343
1344
  let index = this.#blockHashMap.get(hash);
1344
1345
  let localHash = "0x0";
1345
1346
  try {
1346
- localHash = await globalThis.stateStore.get("lastBlock");
1347
+ const rawLocalHash = await globalThis.stateStore.get("lastBlock");
1348
+ if (rawLocalHash) {
1349
+ const decoded = JSON.parse(new TextDecoder().decode(rawLocalHash));
1350
+ localHash = typeof decoded === "string" ? decoded : decoded?.hash ?? "0x0";
1351
+ }
1347
1352
  } catch (error) {
1348
1353
  debug$1("no local state found");
1349
1354
  }
@@ -1573,10 +1578,21 @@ class State extends Contract {
1573
1578
  compatiblePeerCount += 1;
1574
1579
  const task = async () => {
1575
1580
  try {
1576
- const result = await peer.request(node.encoded);
1581
+ let result = await peer.request(node.encoded);
1577
1582
  const resultType = result instanceof Uint8Array ? `bytes:${result.length}` : typeof result;
1578
1583
  debug$1(`lastBlock result type: ${resultType}`);
1579
1584
  console.log({ result });
1585
+ if (result instanceof Uint8Array) {
1586
+ for (let i = 0; i < 3; i += 1) {
1587
+ try {
1588
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](result);
1589
+ if (wrapped?.decoded?.response === void 0) break;
1590
+ result = wrapped.decoded.response;
1591
+ } catch {
1592
+ break;
1593
+ }
1594
+ }
1595
+ }
1580
1596
  return { result: new LastBlockMessage(result), peer };
1581
1597
  } catch (error) {
1582
1598
  const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
@@ -1653,6 +1669,9 @@ class State extends Contract {
1653
1669
  */
1654
1670
  async #loadBlocks(blocks) {
1655
1671
  this.#chainState = "loading";
1672
+ if (blocks.some((block) => !block)) {
1673
+ throw new Error("missing block data during load; chain resolution incomplete");
1674
+ }
1656
1675
  const poolTransactionKeys = new Set(await globalThis.transactionPoolStore.keys());
1657
1676
  debug$1(`pool transactions: ${poolTransactionKeys.size}`);
1658
1677
  debug$1(`loading ${blocks.length} blocks`);
@@ -2591,22 +2610,23 @@ class Chain extends VersionControl {
2591
2610
  if (response === void 0 || response === null) return response;
2592
2611
  const normalizeResponse = async (payload) => {
2593
2612
  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
2613
  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;
2614
+ let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
2615
+ for (let i = 0; i < 3; i += 1) {
2616
+ try {
2617
+ const codec = new Codec(bytes);
2618
+ if (codec.name !== "peernet-response") break;
2619
+ const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
2620
+ if (wrapped?.decoded?.response === void 0) break;
2621
+ const next = wrapped.decoded.response;
2622
+ if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
2623
+ bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
2624
+ continue;
2625
+ }
2626
+ return normalizeResponse(next);
2627
+ } catch {
2628
+ break;
2608
2629
  }
2609
- } catch {
2610
2630
  }
2611
2631
  try {
2612
2632
  return JSON.parse(new TextDecoder().decode(bytes));
@@ -2614,6 +2634,16 @@ class Chain extends VersionControl {
2614
2634
  return bytes;
2615
2635
  }
2616
2636
  }
2637
+ if (payload && typeof payload === "object") {
2638
+ const objectPayload = payload;
2639
+ const decoded = objectPayload.decoded;
2640
+ if ("decoded" in objectPayload && decoded && "response" in decoded) {
2641
+ return normalizeResponse(decoded.response);
2642
+ }
2643
+ if ("response" in objectPayload) {
2644
+ return normalizeResponse(objectPayload.response);
2645
+ }
2646
+ }
2617
2647
  if (typeof payload === "string") {
2618
2648
  try {
2619
2649
  return JSON.parse(payload);
@@ -2681,8 +2711,19 @@ class Chain extends VersionControl {
2681
2711
  let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
2682
2712
  console.log("raw response for request transactionPool:", transactionsInPool);
2683
2713
  if (transactionsInPool instanceof Uint8Array) {
2684
- debug("transactionPool response must be decoded array payload");
2685
- return [];
2714
+ try {
2715
+ transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
2716
+ } catch {
2717
+ debug("transactionPool response must be decoded array payload");
2718
+ return [];
2719
+ }
2720
+ }
2721
+ if (typeof transactionsInPool === "string") {
2722
+ try {
2723
+ transactionsInPool = JSON.parse(transactionsInPool);
2724
+ } catch {
2725
+ return [];
2726
+ }
2686
2727
  }
2687
2728
  if (!Array.isArray(transactionsInPool)) return [];
2688
2729
  const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
@@ -2767,9 +2808,9 @@ class Chain extends VersionControl {
2767
2808
  debug(`peer has no lastBlock: ${peerId}`);
2768
2809
  return;
2769
2810
  }
2770
- const higherThenCurrentLocal = !localBlock?.index ? true : lastBlock.index > localBlock.index;
2811
+ const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
2771
2812
  if (lastBlock) {
2772
- if (!this.lastBlock || higherThenCurrentLocal) {
2813
+ if (!localBlock || higherThenCurrentLocal) {
2773
2814
  try {
2774
2815
  const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
2775
2816
  const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
@@ -2797,9 +2838,8 @@ class Chain extends VersionControl {
2797
2838
  }
2798
2839
  if (this.wantList.length > 0) {
2799
2840
  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);
2841
+ for (let i = promises.length - 1; i >= 0; i -= 1) {
2842
+ if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
2803
2843
  }
2804
2844
  if (this.wantList.length === 0) await this.triggerSync();
2805
2845
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.9.20",
3
+ "version": "1.9.22",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {