@leofcoin/chain 1.9.14 → 1.9.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,6 @@
1
- import { j as getDefaultExportFromCjs } from './identity-B8_RBemH-Dq17MYzU.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-y2adT6_R.js';
2
+ import 'crypto';
3
+ import './constants-V2VjIc2r.js';
2
4
 
3
5
  function _mergeNamespaces$1(n, m) {
4
6
  m.forEach(function (e) {
@@ -1,4 +1,6 @@
1
- import { j as getDefaultExportFromCjs } from './identity-B8_RBemH-Dq17MYzU.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-y2adT6_R.js';
2
+ import 'crypto';
3
+ import './constants-V2VjIc2r.js';
2
4
 
3
5
  function _mergeNamespaces$1(n, m) {
4
6
  m.forEach(function (e) {
@@ -6302,6 +6302,36 @@ class Chain extends VersionControl {
6302
6302
  console.log("init");
6303
6303
  await super.init();
6304
6304
  console.log("super init done");
6305
+ const peernetAny = globalThis.peernet;
6306
+ if (typeof peernetAny.removePeer === "function" && !peernetAny.__chainRemovePeerGuardInstalled) {
6307
+ const originalRemovePeer = peernetAny.removePeer.bind(peernetAny);
6308
+ peernetAny.removePeer = async (peer) => {
6309
+ if (!peer || typeof peer !== "object") {
6310
+ debug("removePeer called with invalid peer (ignored)");
6311
+ return;
6312
+ }
6313
+ const peerId = peer?.id || peer?.peerId;
6314
+ if (!peerId || typeof peerId !== "string") {
6315
+ debug("removePeer called with peer missing id (ignored)");
6316
+ return;
6317
+ }
6318
+ const trackedPeers = Object.values(peernetAny.connections || {});
6319
+ const isStillTracked = trackedPeers.some((trackedPeer) => {
6320
+ const trackedId = trackedPeer?.id || trackedPeer?.peerId;
6321
+ return trackedPeer === peer || trackedId === peerId;
6322
+ });
6323
+ if (!isStillTracked) {
6324
+ debug(`removePeer called for unknown peer ${peerId} (ignored)`);
6325
+ return;
6326
+ }
6327
+ try {
6328
+ return await originalRemovePeer(peer);
6329
+ } catch (error) {
6330
+ debug("removePeer failed (ignored):", error?.message ?? error);
6331
+ }
6332
+ };
6333
+ peernetAny.__chainRemovePeerGuardInstalled = true;
6334
+ }
6305
6335
  this.#connectionMonitor.start(this.version);
6306
6336
  await globalThis.peernet.addRequestHandler("bw-request-message", () => {
6307
6337
  const bw = globalThis.peernet.client?.bw || { up: 0, down: 0 };
@@ -6309,9 +6339,11 @@ class Chain extends VersionControl {
6309
6339
  });
6310
6340
  await globalThis.peernet.addRequestHandler("transactionPool", this.#transactionPoolHandler.bind(this));
6311
6341
  await globalThis.peernet.addRequestHandler("version", this.#versionHandler.bind(this));
6312
- await globalThis.peernet.addRequestHandler("stateInfo", () => {
6342
+ await globalThis.peernet.addRequestHandler("stateInfo", async () => {
6343
+ const lastblock = await this.lastBlock || { index: 0, hash: "0x0", previousHash: "0x0" };
6344
+ const values = this.machine?.states?.info || {};
6313
6345
  return new globalThis.peernet.protos["peernet-response"]({
6314
- response: new StateMessage(this.machine.states.info).encoded
6346
+ response: new StateMessage({ lastblock, values }).encoded
6315
6347
  });
6316
6348
  });
6317
6349
  globalThis.peernet.subscribe("add-block", this.#addBlock.bind(this));
@@ -6451,6 +6483,16 @@ class Chain extends VersionControl {
6451
6483
  debug(`peer not found: ${peerId}`);
6452
6484
  return;
6453
6485
  }
6486
+ const peerAny = peer;
6487
+ if (typeof peerAny.on === "function" && !peerAny.__chainErrorListenerInstalled) {
6488
+ peerAny.on("error", async (error) => {
6489
+ const reason = error?.message ?? String(error);
6490
+ const peerName = peerAny?.peerId || peerAny?.id || peerAny?.address || peerId || "unknown";
6491
+ debug(`peer error: ${peerName}:`, reason);
6492
+ await this.#recordPeerFailure(peerId, `peer error: ${reason}`);
6493
+ });
6494
+ peerAny.__chainErrorListenerInstalled = true;
6495
+ }
6454
6496
  if (!peer.version) {
6455
6497
  try {
6456
6498
  let versionResponse = await this.#makeRequest(peer, "version");
@@ -6553,7 +6595,8 @@ class Chain extends VersionControl {
6553
6595
  try {
6554
6596
  let stateInfo = await this.#makeRequest(peer, "stateInfo");
6555
6597
  if (stateInfo instanceof Uint8Array) {
6556
- stateInfo = new StateMessage(stateInfo).decoded;
6598
+ const decodedStateInfo = new StateMessage(stateInfo).decoded;
6599
+ stateInfo = decodedStateInfo?.values ?? decodedStateInfo;
6557
6600
  }
6558
6601
  debug(
6559
6602
  `sync start with peer ${peerId}: local=${localBlock?.index ?? -1} remote=${lastBlock?.index ?? -1} hash=${lastBlock?.hash}`