@clonegod/ttd-bsc-common 1.0.11 → 1.0.13

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.
@@ -9,7 +9,7 @@ export declare class PoolEventListener {
9
9
  private appConfig;
10
10
  private wsProvider;
11
11
  private poolList;
12
- private endpoint;
12
+ private ws_endpoint;
13
13
  private isConnected;
14
14
  private isReconnecting;
15
15
  private isStarted;
@@ -19,7 +19,7 @@ export declare class PoolEventListener {
19
19
  private maxReconnectDelay;
20
20
  private lastProcessedBlockNumber;
21
21
  private isBlockListenerActive;
22
- constructor(appConfig: AppConfig);
22
+ constructor(appConfig: AppConfig, ws_endpoint: string);
23
23
  init(poolList: StandardPoolInfoType[]): Promise<void>;
24
24
  start(): Promise<void>;
25
25
  stop(): Promise<void>;
@@ -14,7 +14,7 @@ const ethers_1 = require("ethers");
14
14
  const dist_1 = require("@clonegod/ttd-core/dist");
15
15
  const common_1 = require("../../common");
16
16
  class PoolEventListener {
17
- constructor(appConfig) {
17
+ constructor(appConfig, ws_endpoint) {
18
18
  this.wsProvider = null;
19
19
  this.poolList = [];
20
20
  this.isConnected = false;
@@ -27,7 +27,7 @@ class PoolEventListener {
27
27
  this.lastProcessedBlockNumber = 0;
28
28
  this.isBlockListenerActive = false;
29
29
  this.appConfig = appConfig;
30
- this.endpoint = this.appConfig.env_args.ws_endpoint;
30
+ this.ws_endpoint = ws_endpoint;
31
31
  }
32
32
  init(poolList) {
33
33
  return __awaiter(this, void 0, void 0, function* () {
@@ -62,12 +62,12 @@ class PoolEventListener {
62
62
  connect() {
63
63
  return __awaiter(this, void 0, void 0, function* () {
64
64
  try {
65
- (0, dist_1.log_info)(`正在连接 WebSocket: ${this.endpoint}`);
66
- this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.endpoint);
65
+ (0, dist_1.log_info)(`正在连接 WebSocket: ${this.ws_endpoint}`);
66
+ this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.ws_endpoint);
67
67
  yield this.wsProvider.ready;
68
68
  this.isConnected = true;
69
69
  this.reconnectAttempts = 0;
70
- (0, dist_1.log_info)(`WebSocket 已连接: ${this.endpoint}`);
70
+ (0, dist_1.log_info)(`WebSocket 已连接: ${this.ws_endpoint}`);
71
71
  this.wsProvider.on('error', (err) => {
72
72
  (0, dist_1.log_error)(`WebSocket 错误:`, err, '');
73
73
  this.handleConnectionIssue();
@@ -81,8 +81,18 @@ class PoolEventListener {
81
81
  }
82
82
  }
83
83
  catch (error) {
84
- (0, dist_1.log_error)(`连接 WebSocket 失败: ${this.endpoint}`, error, '');
85
- this.handleConnectionIssue();
84
+ (0, dist_1.log_error)(`首次连接 WebSocket 失败: ${this.ws_endpoint}`, error, '');
85
+ if (this.reconnectAttempts > 0) {
86
+ this.handleConnectionIssue();
87
+ }
88
+ else {
89
+ this.isReconnecting = true;
90
+ this.isConnected = false;
91
+ this.cleanup();
92
+ yield (0, dist_1.sleep)(this.baseReconnectDelay);
93
+ yield this.connect();
94
+ this.isReconnecting = false;
95
+ }
86
96
  }
87
97
  });
88
98
  }
@@ -96,11 +106,11 @@ class PoolEventListener {
96
106
  this.cleanup();
97
107
  this.reconnectAttempts++;
98
108
  const delay = Math.min(this.baseReconnectDelay * Math.pow(2, this.reconnectAttempts), this.maxReconnectDelay);
99
- (0, dist_1.log_info)(`尝试重连 ${this.reconnectAttempts}/${this.maxReconnectAttempts}, 延迟 ${delay}ms, 节点: ${this.endpoint}`);
109
+ (0, dist_1.log_info)(`尝试重连 ${this.reconnectAttempts}/${this.maxReconnectAttempts}, 延迟 ${delay}ms, 节点: ${this.ws_endpoint}`);
100
110
  if (this.reconnectAttempts > this.maxReconnectAttempts) {
101
111
  (0, dist_1.log_error)(`重连失败,尝试次数达到 ${this.maxReconnectAttempts} 次,程序退出`, new Error('Max reconnect attempts failed'), '');
102
112
  this.appConfig.emit(common_1.EVENT_NAMES.WS_CONNECTION_FAILED, {
103
- endpoint: this.endpoint,
113
+ endpoint: this.ws_endpoint,
104
114
  attempts: this.maxReconnectAttempts,
105
115
  });
106
116
  process.exit(1);
@@ -151,7 +161,7 @@ class PoolEventListener {
151
161
  if (this.isBlockListenerActive && this.wsProvider) {
152
162
  this.wsProvider.removeListener('block', () => { });
153
163
  this.isBlockListenerActive = false;
154
- (0, dist_1.log_info)(`区块监听器已停止`);
164
+ (0, dist_1.log_info)(`区块监听已停止`);
155
165
  }
156
166
  }
157
167
  cleanup() {
@@ -167,9 +177,12 @@ class PoolEventListener {
167
177
  }
168
178
  this.wsProvider = null;
169
179
  }
180
+ this.isConnected = false;
181
+ this.isReconnecting = false;
170
182
  }
171
183
  isWSConnected() {
172
- return this.isConnected;
184
+ var _a, _b;
185
+ return this.isConnected && ((_b = (_a = this.wsProvider) === null || _a === void 0 ? void 0 : _a._websocket) === null || _b === void 0 ? void 0 : _b.readyState) === 1;
173
186
  }
174
187
  getConnectionDiagnostics() {
175
188
  return {
@@ -177,7 +190,7 @@ class PoolEventListener {
177
190
  isReconnecting: this.isReconnecting,
178
191
  isStarted: this.isStarted,
179
192
  reconnectAttempts: this.reconnectAttempts,
180
- endpoint: this.endpoint,
193
+ ws_endpoint: this.ws_endpoint,
181
194
  wsReadyState: this.wsProvider && this.wsProvider._websocket
182
195
  ? this.wsProvider._websocket.readyState
183
196
  : null,
@@ -10,7 +10,7 @@ export declare class SimpleRedisClient {
10
10
  private getLockKey;
11
11
  private acquireLock;
12
12
  private releaseLock;
13
- withLock<T>(lock_identifier: string, callback: () => Promise<T>): Promise<T>;
13
+ withLock<T>(lock_identifier: string, callback: () => Promise<T>, release_lock_delay_ms?: number): Promise<T>;
14
14
  getValue(key: string): Promise<string>;
15
15
  setValue(key: string, value: string, expireSeconds: number): Promise<any>;
16
16
  hsetValue(key: string, field: string, value: string, expireSeconds: number): Promise<any>;
@@ -69,8 +69,8 @@ class SimpleRedisClient {
69
69
  return success;
70
70
  });
71
71
  }
72
- withLock(lock_identifier, callback) {
73
- return __awaiter(this, void 0, void 0, function* () {
72
+ withLock(lock_identifier_1, callback_1) {
73
+ return __awaiter(this, arguments, void 0, function* (lock_identifier, callback, release_lock_delay_ms = 2000) {
74
74
  const lock_key = this.getLockKey(lock_identifier);
75
75
  const lock_value = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
76
76
  let retries = 0;
@@ -90,7 +90,7 @@ class SimpleRedisClient {
90
90
  }
91
91
  finally {
92
92
  if (acquired) {
93
- yield (0, dist_1.sleep)(parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || '2000'));
93
+ yield (0, dist_1.sleep)(parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || String(release_lock_delay_ms)));
94
94
  yield this.releaseLock(lock_key, lock_value);
95
95
  }
96
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",