@clonegod/ttd-bsc-common 1.0.11 → 1.0.12
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
|
|
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.
|
|
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.
|
|
66
|
-
this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.
|
|
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.
|
|
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)(
|
|
85
|
-
this.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
193
|
+
ws_endpoint: this.ws_endpoint,
|
|
181
194
|
wsReadyState: this.wsProvider && this.wsProvider._websocket
|
|
182
195
|
? this.wsProvider._websocket.readyState
|
|
183
196
|
: null,
|