@dignetwork/chia-block-listener 0.1.14 → 0.1.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-darwin-arm64",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/DIG-Network/chia-block-listener"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-darwin-x64",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/DIG-Network/chia-block-listener"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-linux-arm64-gnu",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/DIG-Network/chia-block-listener"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-linux-x64-gnu",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/DIG-Network/chia-block-listener"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-win32-x64-msvc",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/DIG-Network/chia-block-listener"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "repository": {
@@ -61,10 +61,10 @@
61
61
  "@dignetwork/datalayer-driver": "^0.1.35"
62
62
  },
63
63
  "optionalDependencies": {
64
- "@dignetwork/chia-block-listener-win32-x64-msvc": "0.1.14",
65
- "@dignetwork/chia-block-listener-darwin-x64": "0.1.14",
66
- "@dignetwork/chia-block-listener-linux-x64-gnu": "0.1.14",
67
- "@dignetwork/chia-block-listener-darwin-arm64": "0.1.14",
68
- "@dignetwork/chia-block-listener-linux-arm64-gnu": "0.1.14"
64
+ "@dignetwork/chia-block-listener-win32-x64-msvc": "0.1.15",
65
+ "@dignetwork/chia-block-listener-darwin-x64": "0.1.15",
66
+ "@dignetwork/chia-block-listener-linux-x64-gnu": "0.1.15",
67
+ "@dignetwork/chia-block-listener-darwin-arm64": "0.1.15",
68
+ "@dignetwork/chia-block-listener-linux-arm64-gnu": "0.1.15"
69
69
  }
70
70
  }
package/src/peer_pool.rs CHANGED
@@ -16,7 +16,7 @@ use tokio::time::timeout;
16
16
  use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
17
17
  use tracing::{debug, error, info, warn};
18
18
 
19
- const RATE_LIMIT_MS: u64 = 50; // Aggressive rate limiting for maximum performance
19
+ const RATE_LIMIT_MS: u64 = 500; // 500ms cooldown between peer usage
20
20
  const REQUEST_TIMEOUT_MS: u64 = 5000; // 5 second timeout for block requests (reduced from 10s)
21
21
  const CONNECTION_TIMEOUT_MS: u64 = 3000; // 3 second timeout for connections (reduced from 5s)
22
22
 
@@ -55,7 +55,8 @@ pub struct ChiaPeerPool {
55
55
 
56
56
  struct ChiaPeerPoolInner {
57
57
  peers: HashMap<String, PeerInfo>,
58
- peer_ids: Vec<String>, // For round-robin
58
+ peer_ids: Vec<String>, // For round-robin
59
+ round_robin_index: usize, // Track current position in round-robin
59
60
  highest_peak: Option<u32>,
60
61
  }
61
62
 
@@ -93,6 +94,7 @@ impl ChiaPeerPool {
93
94
  let inner = Arc::new(RwLock::new(ChiaPeerPoolInner {
94
95
  peers: HashMap::new(),
95
96
  peer_ids: Vec::new(),
97
+ round_robin_index: 0,
96
98
  highest_peak: None,
97
99
  }));
98
100
 
@@ -367,6 +369,10 @@ impl ChiaPeerPool {
367
369
  }
368
370
 
369
371
  guard.peer_ids.retain(|id| id != &peer_id);
372
+ // Adjust round_robin_index if needed
373
+ if guard.round_robin_index >= guard.peer_ids.len() && !guard.peer_ids.is_empty() {
374
+ guard.round_robin_index = 0;
375
+ }
370
376
  Ok(true)
371
377
  } else {
372
378
  Ok(false)
@@ -383,6 +389,7 @@ impl ChiaPeerPool {
383
389
  }
384
390
 
385
391
  guard.peer_ids.clear();
392
+ guard.round_robin_index = 0;
386
393
  Ok(())
387
394
  }
388
395
 
@@ -428,41 +435,43 @@ impl ChiaPeerPool {
428
435
  if let Some(request) = request_queue.front() {
429
436
  match request {
430
437
  PoolRequest::GetBlockByHeight { .. } => {
431
- // Find the best available peer (most recently successful)
438
+ // Round-robin peer selection with 500ms cooldown
432
439
  let now = Instant::now();
433
- let mut best_peer = None;
434
- let mut shortest_wait = Duration::from_secs(999);
435
-
436
- // Check if we have a preferred peer and if it's available
437
- // The preferred_peer logic is removed, so we just find the first available peer
438
- for peer_id in &guard.peer_ids {
439
- if let Some(peer_info) = guard.peers.get(peer_id) {
440
- if peer_info.is_connected {
441
- let time_since_last_use = now.duration_since(peer_info.last_used);
442
-
443
- // Prefer peers that are immediately available
444
- if time_since_last_use >= Duration::from_millis(RATE_LIMIT_MS) {
445
- best_peer = Some(peer_id.clone());
446
- break;
447
- }
448
-
449
- // Track the peer that will be available soonest
450
- let wait_time = Duration::from_millis(RATE_LIMIT_MS) - time_since_last_use;
451
- if wait_time < shortest_wait {
452
- shortest_wait = wait_time;
453
- best_peer = Some(peer_id.clone());
440
+ let mut selected_peer = None;
441
+ let mut attempts = 0;
442
+ let total_peers = guard.peer_ids.len();
443
+
444
+ // Try to find an available peer using round-robin
445
+ while attempts < total_peers {
446
+ if !guard.peer_ids.is_empty() {
447
+ let peer_id = &guard.peer_ids[guard.round_robin_index];
448
+
449
+ if let Some(peer_info) = guard.peers.get(peer_id) {
450
+ if peer_info.is_connected {
451
+ let time_since_last_use = now.duration_since(peer_info.last_used);
452
+
453
+ // Check if this peer is available (past cooldown)
454
+ if time_since_last_use >= Duration::from_millis(RATE_LIMIT_MS) {
455
+ selected_peer = Some(peer_id.clone());
456
+ // Move to next peer for next request
457
+ guard.round_robin_index = (guard.round_robin_index + 1) % total_peers;
458
+ break;
459
+ }
454
460
  }
455
461
  }
462
+
463
+ // Move to next peer and try again
464
+ guard.round_robin_index = (guard.round_robin_index + 1) % total_peers;
456
465
  }
466
+ attempts += 1;
457
467
  }
458
468
 
459
- if let Some(peer_id) = best_peer {
469
+ if let Some(peer_id) = selected_peer {
460
470
  if let Some(peer_info) = guard.peers.get(&peer_id) {
461
471
  let time_since_last_use = now.duration_since(peer_info.last_used);
462
472
 
463
- // Only proceed if peer is available or wait time is very short
464
- if time_since_last_use >= Duration::from_millis(RATE_LIMIT_MS) ||
465
- shortest_wait < Duration::from_millis(25) {
473
+ // Only proceed if peer is available (we already checked this in round-robin)
474
+ if time_since_last_use >= Duration::from_millis(RATE_LIMIT_MS) {
466
475
 
467
476
  if let Some(request) = request_queue.pop_front() {
468
477
  if let Some(peer_info) = guard.peers.get_mut(&peer_id) {
@@ -961,6 +970,10 @@ impl ChiaPeerPool {
961
970
  let _ = worker_tx.send(WorkerRequest::Shutdown).await;
962
971
  }
963
972
  guard.peer_ids.retain(|id| id != &params.peer_id);
973
+ // Adjust round_robin_index if needed
974
+ if guard.round_robin_index >= guard.peer_ids.len() && !guard.peer_ids.is_empty() {
975
+ guard.round_robin_index = 0;
976
+ }
964
977
  }
965
978
 
966
979
  // Emit disconnected event