@leofcoin/chain 1.8.12 → 1.8.15

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,4 +1,4 @@
1
- import { t as toBase58, T as TransactionMessage, C as ContractMessage, R as RawTransactionMessage, B as BlockMessage, P as PROTOCOL_VERSION, a as REACHED_ONE_ZERO_ZERO, b as BWMessage, c as BWRequestMessage } from './constants-ByuwyBVy.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, u as utils, b as BWMessage, c as BWRequestMessage } from './constants-BKKQytjd.js';
2
2
  import { log } from 'console';
3
3
  import { log as log$1 } from 'node:console';
4
4
 
@@ -3977,7 +3977,12 @@ class Transaction extends Protocol {
3977
3977
  });
3978
3978
  await globalThis.transactionPoolStore.put(hash, message.encoded);
3979
3979
  // debug(`Added ${hash} to the transaction pool`)
3980
- peernet.publish('add-transaction', message.encoded);
3980
+ try {
3981
+ peernet.publish('add-transaction', message.encoded);
3982
+ }
3983
+ catch (publishError) {
3984
+ console.warn('peernet publish failed: add-transaction', publishError?.message ?? publishError);
3985
+ }
3981
3986
  const fee = await calculateFee(message.decoded);
3982
3987
  return { hash, data, fee, wait, message };
3983
3988
  }
@@ -4919,10 +4924,16 @@ class Machine {
4919
4924
  return this.getBlocks();
4920
4925
  }
4921
4926
  get lastBlock() {
4922
- return this.#askWorker('lastBlock');
4927
+ return this.#askWorker('lastBlock').catch((error) => {
4928
+ debug$2('lastBlock fallback after worker timeout:', error?.message ?? error);
4929
+ return this.states.lastBlock;
4930
+ });
4923
4931
  }
4924
4932
  get lastBlockHeight() {
4925
- return this.#askWorker('lastBlockHeight');
4933
+ return this.#askWorker('lastBlockHeight').catch((error) => {
4934
+ debug$2('lastBlockHeight fallback after worker timeout:', error?.message ?? error);
4935
+ return Number(this.states.lastBlock?.index ?? 0);
4936
+ });
4926
4937
  }
4927
4938
  getBlocks(from, to) {
4928
4939
  return this.#askWorker('blocks', { from, to });
@@ -5096,7 +5107,7 @@ class State extends Contract {
5096
5107
  };
5097
5108
  this.#lastBlockHandler = async () => {
5098
5109
  return new globalThis.peernet.protos['peernet-response']({
5099
- response: await this.lastBlock
5110
+ response: new Uint8Array(new LastBlockMessage(await this.lastBlock).encoded)
5100
5111
  });
5101
5112
  };
5102
5113
  this.#knownBlocksHandler = async () => {
@@ -5496,6 +5507,8 @@ class State extends Contract {
5496
5507
  return { result: Uint8Array.from(Object.values(result)), peer };
5497
5508
  }
5498
5509
  catch (error) {
5510
+ const peerId = peer?.peerId || peer?.id || peer?.address || 'unknown';
5511
+ debug$1(`lastBlock request failed: ${peerId}:`, error?.message ?? error);
5499
5512
  throw error;
5500
5513
  }
5501
5514
  };
@@ -5523,9 +5536,16 @@ class State extends Contract {
5523
5536
  request: 'knownBlocks'
5524
5537
  });
5525
5538
  let node = await globalThis.peernet.prepareMessage(data);
5526
- let message = await peer.request(node.encode());
5527
- message = await new globalThis.peernet.protos['peernet-response'](message);
5528
- this.wantList.push(...message.decoded.response.blocks.filter((block) => !this.knownBlocks.includes(block)));
5539
+ try {
5540
+ let message = await peer.request(node.encode());
5541
+ message = await new globalThis.peernet.protos['peernet-response'](message);
5542
+ this.wantList.push(...message.decoded.response.blocks.filter((block) => !this.knownBlocks.includes(block)));
5543
+ }
5544
+ catch (error) {
5545
+ const peerId = peer?.peerId || peer?.id || peer?.address || 'unknown';
5546
+ debug$1(`knownBlocks request failed: ${peerId}:`, error?.message ?? error);
5547
+ throw error;
5548
+ }
5529
5549
  }
5530
5550
  }
5531
5551
  return latest;
@@ -5996,7 +6016,7 @@ class ConnectionMonitor {
5996
6016
  const networkName = globalThis.peernet?.network;
5997
6017
  if (networkName && typeof networkName === 'string') {
5998
6018
  // Try to import network config
5999
- const { default: networks } = await import('./constants-ByuwyBVy.js').then(function (n) { return n.n; });
6019
+ const { default: networks } = await import('./constants-BKKQytjd.js').then(function (n) { return n.n; });
6000
6020
  const [mainKey, subKey] = networkName.split(':');
6001
6021
  const networkConfig = networks?.[mainKey]?.[subKey];
6002
6022
  if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
@@ -6094,6 +6114,16 @@ class ConnectionMonitor {
6094
6114
  }
6095
6115
  }
6096
6116
 
6117
+ utils.addCodec({
6118
+ name: 'last-block-message',
6119
+ codec: 0x6c626d,
6120
+ hashAlg: 'keccak-256'
6121
+ });
6122
+ utils.addCodec({
6123
+ name: 'last-block-request-message',
6124
+ codec: 0x6c62726d,
6125
+ hashAlg: 'keccak-256'
6126
+ });
6097
6127
  const debug = createDebugger('leofcoin/chain');
6098
6128
  // check if browser or local
6099
6129
  class Chain extends VersionControl {
@@ -6274,9 +6304,16 @@ class Chain extends VersionControl {
6274
6304
  }
6275
6305
  async #makeRequest(peer, request) {
6276
6306
  const node = await this.#prepareRequest(request);
6277
- let response = await peer.request(node.encoded);
6278
- response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
6279
- return response.decoded.response;
6307
+ try {
6308
+ let response = await peer.request(node.encoded);
6309
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
6310
+ return response.decoded.response;
6311
+ }
6312
+ catch (error) {
6313
+ const peerId = peer?.peerId || peer?.id || peer?.address || 'unknown';
6314
+ debug(`peernet request failed: ${request} -> ${peerId}:`, error?.message ?? error);
6315
+ throw error;
6316
+ }
6280
6317
  }
6281
6318
  async getPeerTransactionPool(peer) {
6282
6319
  const transactionsInPool = await this.#makeRequest(peer, 'transactionPool');
@@ -6315,18 +6352,35 @@ class Chain extends VersionControl {
6315
6352
  debug(`versions don't match`);
6316
6353
  return;
6317
6354
  }
6318
- const lastBlock = await this.#makeRequest(peer, 'lastBlock');
6355
+ let lastBlock;
6356
+ try {
6357
+ lastBlock = new LastBlockMessage(await this.#makeRequest(peer, 'lastBlock')).decoded;
6358
+ }
6359
+ catch (error) {
6360
+ const peerName = peer?.peerId || peer?.id || peer?.address || peerId || 'unknown';
6361
+ debug(`lastBlock request failed: ${peerName}:`, error?.message ?? error);
6362
+ return;
6363
+ }
6319
6364
  const localBlock = await this.lastBlock;
6320
- if (lastBlock.hash === '0x0')
6365
+ if (!lastBlock || !lastBlock.hash || lastBlock.hash === '0x0') {
6366
+ debug(`peer has no lastBlock: ${peerId}`);
6321
6367
  return;
6368
+ }
6322
6369
  const higherThenCurrentLocal = !localBlock?.index ? true : lastBlock.index > localBlock.index;
6323
6370
  if (lastBlock) {
6324
6371
  if (!this.lastBlock || higherThenCurrentLocal) {
6325
- const knownBlocksResponse = await this.#makeRequest(peer, 'knownBlocks');
6326
- if (knownBlocksResponse.blocks)
6327
- for (const hash of knownBlocksResponse.blocks) {
6328
- this.wantList.push(hash);
6329
- }
6372
+ try {
6373
+ const knownBlocksResponse = await this.#makeRequest(peer, 'knownBlocks');
6374
+ if (knownBlocksResponse.blocks)
6375
+ for (const hash of knownBlocksResponse.blocks) {
6376
+ this.wantList.push(hash);
6377
+ }
6378
+ }
6379
+ catch (error) {
6380
+ const peerName = peer?.peerId || peer?.id || peer?.address || peerId || 'unknown';
6381
+ debug(`knownBlocks request failed: ${peerName}:`, error?.message ?? error);
6382
+ return;
6383
+ }
6330
6384
  }
6331
6385
  }
6332
6386
  if (this.wantList.length > 0) {
@@ -6341,13 +6395,26 @@ class Chain extends VersionControl {
6341
6395
  await this.triggerSync();
6342
6396
  }
6343
6397
  setTimeout(async () => {
6344
- const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
6345
- if (this.#participating && peerTransactionPool.length > 0)
6346
- return this.#runEpoch();
6398
+ try {
6399
+ const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
6400
+ if (this.#participating && peerTransactionPool.length > 0)
6401
+ return this.#runEpoch();
6402
+ }
6403
+ catch (error) {
6404
+ const peerName = peer?.peerId || peer?.id || peer?.address || peerId || 'unknown';
6405
+ debug(`transactionPool request failed: ${peerName}:`, error?.message ?? error);
6406
+ }
6347
6407
  }, 3000);
6348
- const stateInfo = await this.#makeRequest(peer, 'stateInfo');
6349
- await this.syncChain(lastBlock);
6350
- this.machine.states.info = stateInfo;
6408
+ try {
6409
+ const stateInfo = await this.#makeRequest(peer, 'stateInfo');
6410
+ await this.syncChain(lastBlock);
6411
+ this.machine.states.info = stateInfo;
6412
+ }
6413
+ catch (error) {
6414
+ const peerName = peer?.peerId || peer?.id || peer?.address || peerId || 'unknown';
6415
+ debug(`stateInfo/syncChain failed: ${peerName}:`, error?.message ?? error);
6416
+ return;
6417
+ }
6351
6418
  }
6352
6419
  #epochTimeout;
6353
6420
  async #transactionPoolHandler() {
@@ -6367,7 +6434,12 @@ class Chain extends VersionControl {
6367
6434
  }
6368
6435
  catch (error) {
6369
6436
  await transactionPoolStore.delete(hash);
6370
- globalThis.peernet.publish('invalid-transaction', hash);
6437
+ try {
6438
+ globalThis.peernet.publish('invalid-transaction', hash);
6439
+ }
6440
+ catch (publishError) {
6441
+ debug('peernet publish failed: invalid-transaction', publishError?.message ?? publishError);
6442
+ }
6371
6443
  globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
6372
6444
  throw { error, hash, from, to, params, nonce };
6373
6445
  }
@@ -6582,7 +6654,10 @@ class Chain extends VersionControl {
6582
6654
  bw: bw.up + bw.down
6583
6655
  });
6584
6656
  }
6585
- catch { }
6657
+ catch (error) {
6658
+ const peerId = peer?.peerId || peer?.id || peer?.address || 'unknown';
6659
+ debug(`bw request failed: ${peerId}:`, error?.message ?? error);
6660
+ }
6586
6661
  }
6587
6662
  else if (globalThis.peernet.selectedAccount === validator) {
6588
6663
  block.validators.push({
@@ -6622,7 +6697,12 @@ class Chain extends VersionControl {
6622
6697
  debug(`created block: ${hash} @${block.index}`);
6623
6698
  // Publish canonical encoded form via codec interface
6624
6699
  console.log(`[chain] 📤 Publishing block #${block.index} | hash: ${hash} | encoded bytes: ${blockMessage.encoded.length}`);
6625
- globalThis.peernet.publish('add-block', blockMessage.encoded);
6700
+ try {
6701
+ globalThis.peernet.publish('add-block', blockMessage.encoded);
6702
+ }
6703
+ catch (publishError) {
6704
+ debug('peernet publish failed: add-block', publishError?.message ?? publishError);
6705
+ }
6626
6706
  globalThis.pubsub.publish('add-block', blockMessage.encoded);
6627
6707
  }
6628
6708
  catch (error) {
@@ -6644,7 +6724,12 @@ class Chain extends VersionControl {
6644
6724
  this.#runEpoch();
6645
6725
  }
6646
6726
  catch (e) {
6647
- globalThis.peernet.publish('invalid-transaction', hash);
6727
+ try {
6728
+ globalThis.peernet.publish('invalid-transaction', hash);
6729
+ }
6730
+ catch (publishError) {
6731
+ debug('peernet publish failed: invalid-transaction', publishError?.message ?? publishError);
6732
+ }
6648
6733
  throw new Error('invalid transaction');
6649
6734
  }
6650
6735
  }
@@ -6657,7 +6742,12 @@ class Chain extends VersionControl {
6657
6742
  const transactionMessage = await new TransactionMessage({ ...transaction });
6658
6743
  const event = await super.sendTransaction(transactionMessage);
6659
6744
  this.#sendTransaction(transactionMessage.encoded);
6660
- globalThis.peernet.publish('send-transaction', transactionMessage.encoded);
6745
+ try {
6746
+ globalThis.peernet.publish('send-transaction', transactionMessage.encoded);
6747
+ }
6748
+ catch (publishError) {
6749
+ debug('peernet publish failed: send-transaction', publishError?.message ?? publishError);
6750
+ }
6661
6751
  return event;
6662
6752
  }
6663
6753
  async addContract(transaction, contractMessage) {