@dignetwork/chia-block-listener 0.1.12 → 0.1.14

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/README.md CHANGED
@@ -68,31 +68,6 @@ process.on('SIGINT', () => {
68
68
  process.exit(0)
69
69
  })
70
70
  ```
71
-
72
- ## Recent Enhancements 🚀
73
-
74
- This library includes several powerful enhancements for production use:
75
-
76
- ### Automatic Failover System
77
- - **Intelligent Retry Logic**: Automatically tries up to 3 different peers when block requests fail
78
- - **Smart Peer Selection**: Chooses the best available peer for each request based on response times
79
- - **Transparent Operation**: Failover happens automatically without user intervention
80
-
81
- ### Enhanced Error Handling
82
- - **Protocol Error Detection**: Automatically detects and disconnects peers that refuse blocks or send invalid data
83
- - **Connection Health Monitoring**: Tracks peer connection health and removes problematic peers
84
- - **Error Classification**: Distinguishes between temporary issues and permanent peer problems
85
-
86
- ### Performance Optimizations
87
- - **Aggressive Rate Limiting**: 50ms rate limiting per peer for maximum throughput
88
- - **Persistent Connections**: Reuses WebSocket connections for optimal performance
89
- - **Parallel Processing**: Efficient handling of multiple concurrent block requests
90
-
91
- ### Developer Experience
92
- - **camelCase API**: All JavaScript methods use proper camelCase naming conventions
93
- - **Comprehensive Events**: Detailed event system for monitoring peer lifecycle and errors
94
- - **TypeScript Support**: Full type safety with complete TypeScript definitions
95
-
96
71
  ## API Reference
97
72
 
98
73
  ### ChiaBlockListener Class
package/index.d.ts CHANGED
@@ -44,6 +44,11 @@ export interface CoinSpend {
44
44
  solution: string
45
45
  offset: number
46
46
  }
47
+ export interface NewPeakHeightEvent {
48
+ oldPeak?: number | null
49
+ newPeak: number
50
+ peerId: string
51
+ }
47
52
  export declare function initTracing(): void
48
53
  export declare class ChiaBlockListener {
49
54
  constructor()
@@ -51,7 +56,21 @@ export declare class ChiaBlockListener {
51
56
  disconnectPeer(peerId: string): boolean
52
57
  disconnectAllPeers(): void
53
58
  getConnectedPeers(): Array<string>
59
+ // Typed event method overloads
60
+
61
+ on(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
62
+
63
+ on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
64
+
65
+ on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
66
+
54
67
  on(event: string, callback: (...args: any[]) => any): void
68
+ off(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
69
+
70
+ off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
71
+
72
+ off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
73
+
55
74
  off(event: string, callback: (...args: any[]) => any): void
56
75
  getBlockByHeight(peerId: string, height: number): BlockReceivedEvent
57
76
  getBlocksRange(peerId: string, startHeight: number, endHeight: number): Array<BlockReceivedEvent>
@@ -64,6 +83,13 @@ export declare class ChiaPeerPool {
64
83
  shutdown(): Promise<void>
65
84
  getConnectedPeers(): Promise<Array<string>>
66
85
  getPeakHeight(): Promise<number | null>
86
+ // Typed event method overloads for ChiaPeerPool
87
+ on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
88
+ on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
89
+ on(event: 'newPeakHeight', callback: (event: NewPeakHeightEvent) => void): void
67
90
  on(event: string, callback: (...args: any[]) => any): void
91
+ off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
92
+ off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
93
+ off(event: 'newPeakHeight', callback: (event: NewPeakHeightEvent) => void): void
68
94
  off(event: string, callback: (...args: any[]) => any): void
69
95
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/chia-block-listener-darwin-arm64",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
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.12",
3
+ "version": "0.1.14",
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.12",
3
+ "version": "0.1.14",
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.12",
3
+ "version": "0.1.14",
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.12",
3
+ "version": "0.1.14",
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.12",
3
+ "version": "0.1.14",
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.12",
65
- "@dignetwork/chia-block-listener-darwin-x64": "0.1.12",
66
- "@dignetwork/chia-block-listener-linux-x64-gnu": "0.1.12",
67
- "@dignetwork/chia-block-listener-darwin-arm64": "0.1.12",
68
- "@dignetwork/chia-block-listener-linux-arm64-gnu": "0.1.12"
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"
69
69
  }
70
70
  }
@@ -28,43 +28,65 @@ function addTypedOverloads() {
28
28
  return;
29
29
  }
30
30
 
31
- // Find the on and off method declarations and add overloads before them
32
- const onMethodRegex = /(\s+)(on\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/;
33
- const offMethodRegex = /(\s+)(off\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/;
31
+ // Add NewPeakHeightEvent interface if not present
32
+ if (!content.includes('NewPeakHeightEvent')) {
33
+ const insertPosition = content.indexOf('export declare function initTracing(): void');
34
+ if (insertPosition !== -1) {
35
+ const newInterface = `export interface NewPeakHeightEvent {
36
+ oldPeak?: number | null
37
+ newPeak: number
38
+ peerId: string
39
+ }
40
+ `;
41
+ content = content.substring(0, insertPosition) + newInterface + content.substring(insertPosition);
42
+ console.log('✅ Added NewPeakHeightEvent interface');
43
+ }
44
+ }
34
45
 
35
- // Add typed overloads for 'on' method
36
- if (onMethodRegex.test(content)) {
46
+ // Handle ChiaBlockListener events - find the class and add typed overloads
47
+ let blockListenerMatch = content.match(/(export declare class ChiaBlockListener \{[\s\S]*?)(\s+)(on\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/);
48
+ if (blockListenerMatch) {
49
+ const replacement = `${blockListenerMatch[1]}${blockListenerMatch[2]}// Typed event method overloads for ChiaBlockListener
50
+ ${blockListenerMatch[2]}on(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
51
+ ${blockListenerMatch[2]}on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
52
+ ${blockListenerMatch[2]}on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
53
+ ${blockListenerMatch[2]}${blockListenerMatch[3]}`;
54
+ content = content.replace(blockListenerMatch[0], replacement);
55
+
56
+ // Also add off method overloads
37
57
  content = content.replace(
38
- onMethodRegex,
39
- `$1// Typed event method overloads
40
- $1on(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
41
- $1on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
42
- $1on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
43
- $1$2`
58
+ /(export declare class ChiaBlockListener \{[\s\S]*?)(\s+)(off\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/,
59
+ `$1$2off(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
60
+ $2off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
61
+ $2off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
62
+ $2$3`
44
63
  );
45
- } else {
46
- console.error('❌ Could not find on() method in index.d.ts');
47
- return;
48
64
  }
49
65
 
50
- // Add typed overloads for 'off' method
51
- if (offMethodRegex.test(content)) {
66
+ // Handle ChiaPeerPool events - find the class and add typed overloads
67
+ let peerPoolMatch = content.match(/(export declare class ChiaPeerPool \{[\s\S]*?)(\s+)(on\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/);
68
+ if (peerPoolMatch) {
69
+ const replacement = `${peerPoolMatch[1]}${peerPoolMatch[2]}// Typed event method overloads for ChiaPeerPool
70
+ ${peerPoolMatch[2]}on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
71
+ ${peerPoolMatch[2]}on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
72
+ ${peerPoolMatch[2]}on(event: 'newPeakHeight', callback: (event: NewPeakHeightEvent) => void): void
73
+ ${peerPoolMatch[2]}${peerPoolMatch[3]}`;
74
+ content = content.replace(peerPoolMatch[0], replacement);
75
+
76
+ // Also add off method overloads
52
77
  content = content.replace(
53
- offMethodRegex,
54
- `$1off(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
55
- $1off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
56
- $1off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
57
- $1$2`
78
+ /(export declare class ChiaPeerPool \{[\s\S]*?)(\s+)(off\(event: string, callback: \(\.\.\.args: any\[\]\) => any\): void)/,
79
+ `$1$2off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
80
+ $2off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
81
+ $2off(event: 'newPeakHeight', callback: (event: NewPeakHeightEvent) => void): void
82
+ $2$3`
58
83
  );
59
- } else {
60
- console.error('❌ Could not find off() method in index.d.ts');
61
- return;
62
84
  }
63
85
 
64
86
  // Write the updated content back to the file
65
87
  fs.writeFileSync(indexDtsPath, content, 'utf8');
66
88
 
67
- console.log('✅ Successfully added typed event method overloads to index.d.ts');
89
+ console.log('✅ Successfully added typed event method overloads for both ChiaBlockListener and ChiaPeerPool');
68
90
  } catch (error) {
69
91
  console.error('❌ Error adding typed overloads:', error.message);
70
92
  process.exit(1);
package/src/peer_pool.rs CHANGED
@@ -6,6 +6,7 @@ use crate::peer::PeerConnection;
6
6
  use chia_generator_parser::{BlockParser, ParsedBlock};
7
7
  use chia_protocol::FullBlock;
8
8
  use hex::encode as hex_encode;
9
+ use napi_derive::napi;
9
10
  use std::collections::{HashMap, VecDeque};
10
11
  use std::sync::Arc;
11
12
  use std::time::{Duration, Instant};
@@ -24,9 +25,13 @@ pub type PeerDisconnectedCallback = Box<dyn Fn(PeerDisconnectedEvent) + Send + S
24
25
  pub type NewPeakHeightCallback = Box<dyn Fn(NewPeakHeightEvent) + Send + Sync + 'static>;
25
26
 
26
27
  #[derive(Debug, Clone)]
28
+ #[napi(object)]
27
29
  pub struct NewPeakHeightEvent {
30
+ #[napi(js_name = "oldPeak")]
28
31
  pub old_peak: Option<u32>,
32
+ #[napi(js_name = "newPeak")]
29
33
  pub new_peak: u32,
34
+ #[napi(js_name = "peerId")]
30
35
  pub peer_id: String,
31
36
  }
32
37