@leofcoin/chain 1.7.113 → 1.7.115

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.
@@ -5334,9 +5334,10 @@ class ConnectionMonitor {
5334
5334
  #peerReconnectAttempts = {};
5335
5335
  #maxReconnectAttempts = 10;
5336
5336
  #reconnectDelay = 5000;
5337
- #healthCheckInterval = 60000;
5337
+ #healthCheckInterval = 15000; // Check every 15 seconds for faster recovery
5338
5338
  #version;
5339
5339
  #lastHealthCheckAt = 0;
5340
+ #reconnecting = false;
5340
5341
  // event handlers to remove later
5341
5342
  #onOnline = null;
5342
5343
  #onVisibilityChange = null;
@@ -5369,8 +5370,11 @@ class ConnectionMonitor {
5369
5370
  window.addEventListener('online', this.#onOnline);
5370
5371
  this.#onVisibilityChange = () => {
5371
5372
  if (document.visibilityState === 'visible') {
5372
- console.log('💡 Visibility regained — attempting restore');
5373
- void this.#restoreNetwork();
5373
+ console.log('💡 Visibility regained');
5374
+ if (this.connectedPeers.length === 0) {
5375
+ console.log('💡 Visibility regained — attempting restore');
5376
+ void this.#restoreNetwork();
5377
+ }
5374
5378
  }
5375
5379
  };
5376
5380
  document.addEventListener('visibilitychange', this.#onVisibilityChange);
@@ -5442,7 +5446,7 @@ class ConnectionMonitor {
5442
5446
  console.warn('⚠️ No peer connections detected — attempting reconnection');
5443
5447
  await this.#attemptReconnection();
5444
5448
  }
5445
- else if (compatiblePeers.length === 0) {
5449
+ else if (compatiblePeers.length === 0 && connectedPeers.length > 0) {
5446
5450
  console.warn('⚠️ No compatible peers found — attempting reconnection');
5447
5451
  await this.#attemptReconnection();
5448
5452
  }
@@ -5492,11 +5496,17 @@ class ConnectionMonitor {
5492
5496
  }
5493
5497
  // Called on visibility/online/resume events
5494
5498
  async #restoreNetwork() {
5499
+ if (this.#reconnecting) {
5500
+ console.log('🔁 Reconnection already in progress, skipping');
5501
+ return;
5502
+ }
5503
+ this.#reconnecting = true;
5495
5504
  console.log('🔁 Restoring network');
5496
5505
  try {
5497
5506
  const online = await this.#isOnLine(1500);
5498
5507
  if (!online) {
5499
5508
  console.warn('⚠️ No internet detected, skipping restore');
5509
+ this.#reconnecting = false;
5500
5510
  return;
5501
5511
  }
5502
5512
  }
@@ -5505,18 +5515,85 @@ class ConnectionMonitor {
5505
5515
  console.warn('⚠️ Online probe failed, proceeding with restore', e?.message || e);
5506
5516
  }
5507
5517
  try {
5508
- // prefer safe client reinit if available
5509
- console.log('🔄 Attempting to reinitialize peernet client');
5510
- await globalThis.peernet.client.reinit();
5511
- }
5512
- catch (e) {
5513
- console.warn('⚠️ peernet.client.reinit failed, falling back to peernet.start if available', e?.message || e);
5518
+ // Try multiple restoration approaches
5519
+ console.log('🔄 Attempting network restoration...');
5520
+ // Approach 1: Try client.reinit if available
5521
+ if (globalThis.peernet?.client?.reinit) {
5522
+ console.log(' → Trying client.reinit()');
5523
+ try {
5524
+ await globalThis.peernet.client.reinit();
5525
+ console.log(' ✅ client.reinit() succeeded');
5526
+ }
5527
+ catch (e) {
5528
+ console.warn(' ⚠️ client.reinit() failed:', e?.message || e);
5529
+ }
5530
+ }
5531
+ // Approach 2: Try peernet.start if available
5532
+ if (globalThis.peernet?.start) {
5533
+ console.log(' → Trying peernet.start()');
5534
+ try {
5535
+ await globalThis.peernet.start();
5536
+ console.log(' ✅ peernet.start() succeeded');
5537
+ }
5538
+ catch (e) {
5539
+ console.warn(' ⚠️ peernet.start() failed:', e?.message || e);
5540
+ }
5541
+ }
5542
+ // Approach 3: Try client.connect if available
5543
+ if (globalThis.peernet?.client &&
5544
+ 'connect' in globalThis.peernet.client &&
5545
+ typeof globalThis.peernet.client.connect === 'function') {
5546
+ console.log(' → Trying client.connect()');
5547
+ try {
5548
+ await globalThis.peernet.client.connect();
5549
+ console.log(' ✅ client.connect() succeeded');
5550
+ }
5551
+ catch (e) {
5552
+ console.warn(' ⚠️ client.connect() failed:', e?.message || e);
5553
+ }
5554
+ }
5555
+ // Approach 4: Explicitly dial star servers if available
5514
5556
  try {
5515
- await globalThis.peernet.start();
5557
+ const networkName = globalThis.peernet?.network;
5558
+ if (networkName && typeof networkName === 'string') {
5559
+ // Try to import network config
5560
+ const { default: networks } = await import('./networks-BdMfU1ag.js');
5561
+ const [mainKey, subKey] = networkName.split(':');
5562
+ const networkConfig = networks?.[mainKey]?.[subKey];
5563
+ if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
5564
+ console.log(' → Attempting to dial star servers:', networkConfig.stars.join(', '));
5565
+ for (const star of networkConfig.stars) {
5566
+ try {
5567
+ if (globalThis.peernet?.client && 'dial' in globalThis.peernet.client) {
5568
+ await globalThis.peernet.client.dial(star);
5569
+ console.log(` ✅ Connected to star server: ${star}`);
5570
+ }
5571
+ else if (globalThis.peernet?.client && 'connect' in globalThis.peernet.client) {
5572
+ // Try connect with the star URL
5573
+ await globalThis.peernet.client.connect(star);
5574
+ console.log(` ✅ Connected to star server: ${star}`);
5575
+ }
5576
+ }
5577
+ catch (e) {
5578
+ console.warn(` ⚠️ Failed to dial ${star}:`, e?.message || e);
5579
+ }
5580
+ }
5581
+ }
5582
+ }
5516
5583
  }
5517
- catch (err) {
5518
- console.error(' peernet.start also failed during restore', err?.message || err);
5584
+ catch (e) {
5585
+ console.warn(' ⚠️ Could not load or dial star servers:', e?.message || e);
5519
5586
  }
5587
+ // Give it a moment to establish connections
5588
+ await new Promise((resolve) => setTimeout(resolve, 2000));
5589
+ const connectedAfter = this.connectedPeers.length;
5590
+ console.log(`🔄 Restoration complete. Connected peers: ${connectedAfter}`);
5591
+ }
5592
+ catch (error) {
5593
+ console.error('❌ Network restoration failed:', error?.message || error);
5594
+ }
5595
+ finally {
5596
+ this.#reconnecting = false;
5520
5597
  }
5521
5598
  }
5522
5599
  async waitForPeers(timeoutMs = 30000) {
@@ -5537,19 +5614,41 @@ class ConnectionMonitor {
5537
5614
  });
5538
5615
  }
5539
5616
  async #attemptReconnection() {
5617
+ if (this.#reconnecting) {
5618
+ console.log('⏭️ Reconnection already in progress');
5619
+ return;
5620
+ }
5540
5621
  try {
5541
5622
  await this.#restoreNetwork();
5623
+ // Check if reconnection was successful
5624
+ const hasConnections = this.connectedPeers.length > 0;
5625
+ if (hasConnections) {
5626
+ console.log('✅ Reconnection successful, resetting backoff delay');
5627
+ this.#reconnectDelay = 5000;
5628
+ }
5629
+ else {
5630
+ console.warn('⚠️ Reconnection attempt completed but no peers connected');
5631
+ // Schedule retry with backoff
5632
+ if (this.#reconnectDelay >= 30000) {
5633
+ console.warn('⚠️ Reconnection delay reached maximum, resetting to 5 seconds');
5634
+ this.#reconnectDelay = 5000;
5635
+ }
5636
+ else {
5637
+ // exponential-ish backoff
5638
+ this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, 30000);
5639
+ console.warn(`⚠️ Increasing reconnection delay to ${this.#reconnectDelay} ms`);
5640
+ }
5641
+ setTimeout(() => this.#attemptReconnection(), this.#reconnectDelay);
5642
+ }
5542
5643
  }
5543
5644
  catch (error) {
5544
5645
  console.error('❌ Reconnection failed:', error?.message || error);
5646
+ // Schedule retry with backoff
5545
5647
  if (this.#reconnectDelay >= 30000) {
5546
- console.warn('⚠️ Reconnection delay reached maximum, resetting to 5 seconds');
5547
5648
  this.#reconnectDelay = 5000;
5548
5649
  }
5549
5650
  else {
5550
- // exponential-ish backoff
5551
5651
  this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, 30000);
5552
- console.warn(`⚠️ Increasing reconnection delay to ${this.#reconnectDelay} ms`);
5553
5652
  }
5554
5653
  setTimeout(() => this.#attemptReconnection(), this.#reconnectDelay);
5555
5654
  }
@@ -1,6 +1,7 @@
1
- import { L as LittlePubSub } from './node-browser-B7Z9OIT2.js';
1
+ import { L as LittlePubSub } from './node-browser-B11ddGmL.js';
2
2
  import './identity-Cn0iQbY3-CeW0giQS.js';
3
3
  import './index-DUfUgiQY.js';
4
+ import './networks-BdMfU1ag.js';
4
5
 
5
6
  class Api {
6
7
  _pubsub;
@@ -1,6 +1,7 @@
1
- import { F as FormatInterface } from './node-browser-B7Z9OIT2.js';
1
+ import { F as FormatInterface } from './node-browser-B11ddGmL.js';
2
2
  import './identity-Cn0iQbY3-CeW0giQS.js';
3
3
  import './index-DUfUgiQY.js';
4
+ import './networks-BdMfU1ag.js';
4
5
 
5
6
  var proto$b = {
6
7
  data: new Uint8Array(),
@@ -0,0 +1,25 @@
1
+ // todo only one star needed, no need to have one for each network
2
+ // unless we change anything to the star protocoll
3
+ // version diferences should be handled in the chain
4
+ // maybe a good way to handle could be in p2pt-swarm
5
+ var networks = {
6
+ leofcoin: {
7
+ mainnet: {
8
+ // ports don't really matter since it is favorable to have it begind a ngninx proxy but if we change something to the proto it's easier maybe?
9
+ port: 44444,
10
+ // todo a versionhash would be nice to have as a double check?
11
+ versionHash: '0',
12
+ // a short description identifying the version
13
+ description: 'Main net current version',
14
+ stars: ['wss://star.leofcoin.org'] // todo webrtc and bittorent stars
15
+ },
16
+ peach: {
17
+ port: 44444,
18
+ description: 'Main testnet: latest step before merging into main',
19
+ versionHash: '1',
20
+ stars: ['wss://star.leofcoin.org'] // todo webrtc and bittorent stars
21
+ }
22
+ }
23
+ };
24
+
25
+ export { networks as default };
@@ -1,5 +1,6 @@
1
1
  import { b as base$1, I as Identity, i as index$4, a as index$5, c as base58$1, d as index$3, e as index$2 } from './identity-Cn0iQbY3-CeW0giQS.js';
2
2
  import { F as FormatInterface$2, T as TransactionMessage, C as ContractMessage, B as BlockMessage, a as BWMessage, b as BWRequestMessage, V as ValidatorMessage } from './index-DUfUgiQY.js';
3
+ import networks from './networks-BdMfU1ag.js';
3
4
 
4
5
  var proto = {
5
6
  lastblock: Object(),
@@ -8490,7 +8491,7 @@ class Peernet {
8490
8491
  this.root = options.root;
8491
8492
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
8492
8493
  // FolderMessageResponse
8493
- } = await import(/* webpackChunkName: "messages" */ './messages-C3VxYRj3-pcgSwlQ_.js');
8494
+ } = await import(/* webpackChunkName: "messages" */ './messages-C3VxYRj3-CAcEAWj1.js');
8494
8495
  /**
8495
8496
  * proto Object containing protos
8496
8497
  * @type {Object}
@@ -8584,7 +8585,7 @@ class Peernet {
8584
8585
  if (this.#starting || this.#started)
8585
8586
  return;
8586
8587
  this.#starting = true;
8587
- const importee = await import('./client-bgSiSUVr-WviWWZtO.js');
8588
+ const importee = await import('./client-bgSiSUVr-CGsjlhKP.js');
8588
8589
  /**
8589
8590
  * @access public
8590
8591
  * @type {PeernetClient}
@@ -9104,18 +9105,6 @@ class Peernet {
9104
9105
  }
9105
9106
  globalThis.Peernet = Peernet;
9106
9107
 
9107
- // todo only one star needed, no need to have one for each network
9108
- // unless we change anything to the star protocoll
9109
- // version diferences should be handled in the chain
9110
- // maybe a good way to handle could be in p2pt-swarm
9111
- var networks = {
9112
- leofcoin: {
9113
- peach: {
9114
- stars: ['wss://star.leofcoin.org'] // todo webrtc and bittorent stars
9115
- }
9116
- }
9117
- };
9118
-
9119
9108
  const DEFAULT_NODE_OPTIONS = {
9120
9109
  network: 'leofcoin:peach',
9121
9110
  networkVersion: 'peach',
@@ -1,3 +1,4 @@
1
- export { N as default } from './node-browser-B7Z9OIT2.js';
1
+ export { N as default } from './node-browser-B11ddGmL.js';
2
2
  import './identity-Cn0iQbY3-CeW0giQS.js';
3
3
  import './index-DUfUgiQY.js';
4
+ import './networks-BdMfU1ag.js';
package/exports/chain.js CHANGED
@@ -1461,9 +1461,10 @@ class ConnectionMonitor {
1461
1461
  #peerReconnectAttempts = {};
1462
1462
  #maxReconnectAttempts = 10;
1463
1463
  #reconnectDelay = 5000;
1464
- #healthCheckInterval = 60000;
1464
+ #healthCheckInterval = 15000; // Check every 15 seconds for faster recovery
1465
1465
  #version;
1466
1466
  #lastHealthCheckAt = 0;
1467
+ #reconnecting = false;
1467
1468
  // event handlers to remove later
1468
1469
  #onOnline = null;
1469
1470
  #onVisibilityChange = null;
@@ -1496,8 +1497,11 @@ class ConnectionMonitor {
1496
1497
  window.addEventListener('online', this.#onOnline);
1497
1498
  this.#onVisibilityChange = () => {
1498
1499
  if (document.visibilityState === 'visible') {
1499
- console.log('💡 Visibility regained — attempting restore');
1500
- void this.#restoreNetwork();
1500
+ console.log('💡 Visibility regained');
1501
+ if (this.connectedPeers.length === 0) {
1502
+ console.log('💡 Visibility regained — attempting restore');
1503
+ void this.#restoreNetwork();
1504
+ }
1501
1505
  }
1502
1506
  };
1503
1507
  document.addEventListener('visibilitychange', this.#onVisibilityChange);
@@ -1569,7 +1573,7 @@ class ConnectionMonitor {
1569
1573
  console.warn('⚠️ No peer connections detected — attempting reconnection');
1570
1574
  await this.#attemptReconnection();
1571
1575
  }
1572
- else if (compatiblePeers.length === 0) {
1576
+ else if (compatiblePeers.length === 0 && connectedPeers.length > 0) {
1573
1577
  console.warn('⚠️ No compatible peers found — attempting reconnection');
1574
1578
  await this.#attemptReconnection();
1575
1579
  }
@@ -1619,11 +1623,17 @@ class ConnectionMonitor {
1619
1623
  }
1620
1624
  // Called on visibility/online/resume events
1621
1625
  async #restoreNetwork() {
1626
+ if (this.#reconnecting) {
1627
+ console.log('🔁 Reconnection already in progress, skipping');
1628
+ return;
1629
+ }
1630
+ this.#reconnecting = true;
1622
1631
  console.log('🔁 Restoring network');
1623
1632
  try {
1624
1633
  const online = await this.#isOnLine(1500);
1625
1634
  if (!online) {
1626
1635
  console.warn('⚠️ No internet detected, skipping restore');
1636
+ this.#reconnecting = false;
1627
1637
  return;
1628
1638
  }
1629
1639
  }
@@ -1632,18 +1642,85 @@ class ConnectionMonitor {
1632
1642
  console.warn('⚠️ Online probe failed, proceeding with restore', e?.message || e);
1633
1643
  }
1634
1644
  try {
1635
- // prefer safe client reinit if available
1636
- console.log('🔄 Attempting to reinitialize peernet client');
1637
- await globalThis.peernet.client.reinit();
1638
- }
1639
- catch (e) {
1640
- console.warn('⚠️ peernet.client.reinit failed, falling back to peernet.start if available', e?.message || e);
1645
+ // Try multiple restoration approaches
1646
+ console.log('🔄 Attempting network restoration...');
1647
+ // Approach 1: Try client.reinit if available
1648
+ if (globalThis.peernet?.client?.reinit) {
1649
+ console.log(' → Trying client.reinit()');
1650
+ try {
1651
+ await globalThis.peernet.client.reinit();
1652
+ console.log(' ✅ client.reinit() succeeded');
1653
+ }
1654
+ catch (e) {
1655
+ console.warn(' ⚠️ client.reinit() failed:', e?.message || e);
1656
+ }
1657
+ }
1658
+ // Approach 2: Try peernet.start if available
1659
+ if (globalThis.peernet?.start) {
1660
+ console.log(' → Trying peernet.start()');
1661
+ try {
1662
+ await globalThis.peernet.start();
1663
+ console.log(' ✅ peernet.start() succeeded');
1664
+ }
1665
+ catch (e) {
1666
+ console.warn(' ⚠️ peernet.start() failed:', e?.message || e);
1667
+ }
1668
+ }
1669
+ // Approach 3: Try client.connect if available
1670
+ if (globalThis.peernet?.client &&
1671
+ 'connect' in globalThis.peernet.client &&
1672
+ typeof globalThis.peernet.client.connect === 'function') {
1673
+ console.log(' → Trying client.connect()');
1674
+ try {
1675
+ await globalThis.peernet.client.connect();
1676
+ console.log(' ✅ client.connect() succeeded');
1677
+ }
1678
+ catch (e) {
1679
+ console.warn(' ⚠️ client.connect() failed:', e?.message || e);
1680
+ }
1681
+ }
1682
+ // Approach 4: Explicitly dial star servers if available
1641
1683
  try {
1642
- await globalThis.peernet.start();
1684
+ const networkName = globalThis.peernet?.network;
1685
+ if (networkName && typeof networkName === 'string') {
1686
+ // Try to import network config
1687
+ const { default: networks } = await import('@leofcoin/networks');
1688
+ const [mainKey, subKey] = networkName.split(':');
1689
+ const networkConfig = networks?.[mainKey]?.[subKey];
1690
+ if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
1691
+ console.log(' → Attempting to dial star servers:', networkConfig.stars.join(', '));
1692
+ for (const star of networkConfig.stars) {
1693
+ try {
1694
+ if (globalThis.peernet?.client && 'dial' in globalThis.peernet.client) {
1695
+ await globalThis.peernet.client.dial(star);
1696
+ console.log(` ✅ Connected to star server: ${star}`);
1697
+ }
1698
+ else if (globalThis.peernet?.client && 'connect' in globalThis.peernet.client) {
1699
+ // Try connect with the star URL
1700
+ await globalThis.peernet.client.connect(star);
1701
+ console.log(` ✅ Connected to star server: ${star}`);
1702
+ }
1703
+ }
1704
+ catch (e) {
1705
+ console.warn(` ⚠️ Failed to dial ${star}:`, e?.message || e);
1706
+ }
1707
+ }
1708
+ }
1709
+ }
1643
1710
  }
1644
- catch (err) {
1645
- console.error(' peernet.start also failed during restore', err?.message || err);
1711
+ catch (e) {
1712
+ console.warn(' ⚠️ Could not load or dial star servers:', e?.message || e);
1646
1713
  }
1714
+ // Give it a moment to establish connections
1715
+ await new Promise((resolve) => setTimeout(resolve, 2000));
1716
+ const connectedAfter = this.connectedPeers.length;
1717
+ console.log(`🔄 Restoration complete. Connected peers: ${connectedAfter}`);
1718
+ }
1719
+ catch (error) {
1720
+ console.error('❌ Network restoration failed:', error?.message || error);
1721
+ }
1722
+ finally {
1723
+ this.#reconnecting = false;
1647
1724
  }
1648
1725
  }
1649
1726
  async waitForPeers(timeoutMs = 30000) {
@@ -1664,19 +1741,41 @@ class ConnectionMonitor {
1664
1741
  });
1665
1742
  }
1666
1743
  async #attemptReconnection() {
1744
+ if (this.#reconnecting) {
1745
+ console.log('⏭️ Reconnection already in progress');
1746
+ return;
1747
+ }
1667
1748
  try {
1668
1749
  await this.#restoreNetwork();
1750
+ // Check if reconnection was successful
1751
+ const hasConnections = this.connectedPeers.length > 0;
1752
+ if (hasConnections) {
1753
+ console.log('✅ Reconnection successful, resetting backoff delay');
1754
+ this.#reconnectDelay = 5000;
1755
+ }
1756
+ else {
1757
+ console.warn('⚠️ Reconnection attempt completed but no peers connected');
1758
+ // Schedule retry with backoff
1759
+ if (this.#reconnectDelay >= 30000) {
1760
+ console.warn('⚠️ Reconnection delay reached maximum, resetting to 5 seconds');
1761
+ this.#reconnectDelay = 5000;
1762
+ }
1763
+ else {
1764
+ // exponential-ish backoff
1765
+ this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, 30000);
1766
+ console.warn(`⚠️ Increasing reconnection delay to ${this.#reconnectDelay} ms`);
1767
+ }
1768
+ setTimeout(() => this.#attemptReconnection(), this.#reconnectDelay);
1769
+ }
1669
1770
  }
1670
1771
  catch (error) {
1671
1772
  console.error('❌ Reconnection failed:', error?.message || error);
1773
+ // Schedule retry with backoff
1672
1774
  if (this.#reconnectDelay >= 30000) {
1673
- console.warn('⚠️ Reconnection delay reached maximum, resetting to 5 seconds');
1674
1775
  this.#reconnectDelay = 5000;
1675
1776
  }
1676
1777
  else {
1677
- // exponential-ish backoff
1678
1778
  this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, 30000);
1679
- console.warn(`⚠️ Increasing reconnection delay to ${this.#reconnectDelay} ms`);
1680
1779
  }
1681
1780
  setTimeout(() => this.#attemptReconnection(), this.#reconnectDelay);
1682
1781
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.113",
3
+ "version": "1.7.115",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {