@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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dignetwork/chia-block-listener",
|
|
3
|
-
"version": "0.1.
|
|
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.
|
|
65
|
-
"@dignetwork/chia-block-listener-darwin-x64": "0.1.
|
|
66
|
-
"@dignetwork/chia-block-listener-linux-x64-gnu": "0.1.
|
|
67
|
-
"@dignetwork/chia-block-listener-darwin-arm64": "0.1.
|
|
68
|
-
"@dignetwork/chia-block-listener-linux-arm64-gnu": "0.1.
|
|
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 =
|
|
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>,
|
|
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
|
-
//
|
|
438
|
+
// Round-robin peer selection with 500ms cooldown
|
|
432
439
|
let now = Instant::now();
|
|
433
|
-
let mut
|
|
434
|
-
let mut
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
//
|
|
438
|
-
|
|
439
|
-
if
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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) =
|
|
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
|
|
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 != ¶ms.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
|