Skip to content

Commit 6c5bff0

Browse files
committed
Fix: initialization & shutdown
version: 0.34.2
1 parent 8030b10 commit 6c5bff0

File tree

3 files changed

+58
-65
lines changed

3 files changed

+58
-65
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@diva.exchange/divachain",
3-
"version": "0.34.1",
3+
"version": "0.34.2",
44
"description": "diva - Distributed value exchange upholding security, reliability and privacy",
55
"bin": "dist/main.js",
66
"keywords": [

src/net/network.ts

+55-62
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ export class Network extends EventEmitter {
9393

9494
shutdown() {
9595
this.isClosing = true;
96-
this.agent && this.agent.destroy();
97-
this.samForward && this.samForward.close();
98-
this.samUDP && this.samUDP.close();
96+
typeof this.agent.destroy === 'function' && this.agent.destroy();
97+
typeof this.samForward.close === 'function' && this.samForward.close();
98+
typeof this.samUDP.close === 'function' && this.samUDP.close();
9999
clearTimeout(this.timeoutP2P);
100100
clearTimeout(this.timeoutClean);
101101
}
@@ -105,16 +105,16 @@ export class Network extends EventEmitter {
105105

106106
let retry = 0;
107107
let started = false;
108-
const i = setInterval(() => {
108+
const i = setInterval(async () => {
109109
retry++;
110-
if (retry > 60) {
110+
if (retry > 10) {
111111
throw new Error(`P2P failed on ${toB32(this.server.config.udp)}.b32.i2p`);
112112
}
113113

114114
if (started) {
115115
if (this.hasP2PNetwork()) {
116-
this.emit('ready');
117116
clearInterval(i);
117+
this.emit('ready');
118118
Logger.info(`P2P ready on ${toB32(this.server.config.udp)}.b32.i2p`);
119119
}
120120
return;
@@ -123,57 +123,50 @@ export class Network extends EventEmitter {
123123
started = true;
124124
this.p2pNetwork();
125125

126-
(async () => {
127-
const _c = this.server.config;
128-
this.samForward = (
129-
await createForward({
130-
sam: {
131-
host: _c.i2p_sam_http_host,
132-
portTCP: _c.i2p_sam_http_port_tcp,
133-
publicKey: _c.i2p_public_key_http,
134-
privateKey: _c.i2p_private_key_http,
135-
},
136-
forward: {
137-
host: _c.i2p_sam_forward_http_host,
138-
port: _c.i2p_sam_forward_http_port,
139-
silent: true,
140-
},
141-
})
142-
).on('error', (error: any) => {
143-
Logger.warn('SAM HTTP ' + error.toString());
126+
const _c = this.server.config;
127+
this.samForward = (
128+
await createForward({
129+
sam: {
130+
host: _c.i2p_sam_http_host,
131+
portTCP: _c.i2p_sam_http_port_tcp,
132+
publicKey: _c.i2p_public_key_http,
133+
privateKey: _c.i2p_private_key_http,
134+
},
135+
forward: {
136+
host: _c.i2p_sam_forward_http_host,
137+
port: _c.i2p_sam_forward_http_port,
138+
silent: true,
139+
},
140+
})
141+
).on('error', (error: any) => {
142+
Logger.warn('SAM HTTP ' + error.toString());
143+
});
144+
Logger.info(`HTTP ${toB32(_c.http)}.b32.i2p to ${_c.i2p_sam_forward_http_host}:${_c.i2p_sam_forward_http_port}`);
145+
146+
this.samUDP = (
147+
await createDatagram({
148+
sam: {
149+
host: _c.i2p_sam_udp_host,
150+
portTCP: _c.i2p_sam_udp_port_tcp,
151+
publicKey: _c.i2p_public_key_udp,
152+
privateKey: _c.i2p_private_key_udp,
153+
},
154+
listen: {
155+
address: _c.i2p_sam_listen_udp_host,
156+
port: _c.i2p_sam_listen_udp_port,
157+
hostForward: _c.i2p_sam_forward_udp_host,
158+
portForward: _c.i2p_sam_forward_udp_port,
159+
},
160+
})
161+
)
162+
.on('data', (data: Buffer, from: string) => {
163+
this.incomingData(data, from);
164+
})
165+
.on('error', (error: any) => {
166+
Logger.warn('SAM UDP ' + error.toString());
144167
});
145-
Logger.info(
146-
`HTTP ${toB32(_c.http)}.b32.i2p to ${_c.i2p_sam_forward_http_host}:${_c.i2p_sam_forward_http_port}`
147-
);
148-
})();
149-
150-
(async () => {
151-
const _c = this.server.config;
152-
this.samUDP = (
153-
await createDatagram({
154-
sam: {
155-
host: _c.i2p_sam_udp_host,
156-
portTCP: _c.i2p_sam_udp_port_tcp,
157-
publicKey: _c.i2p_public_key_udp,
158-
privateKey: _c.i2p_private_key_udp,
159-
},
160-
listen: {
161-
address: _c.i2p_sam_listen_udp_host,
162-
port: _c.i2p_sam_listen_udp_port,
163-
hostForward: _c.i2p_sam_forward_udp_host,
164-
portForward: _c.i2p_sam_forward_udp_port,
165-
},
166-
})
167-
)
168-
.on('data', (data: Buffer, from: string) => {
169-
this.incomingData(data, from);
170-
})
171-
.on('error', (error: any) => {
172-
Logger.warn('SAM UDP ' + error.toString());
173-
});
174-
Logger.info(`UDP ${toB32(_c.udp)}.b32.i2p to ${_c.i2p_sam_forward_udp_host}:${_c.i2p_sam_forward_udp_port}`);
175-
})();
176-
}, 1000);
168+
Logger.info(`UDP ${toB32(_c.udp)}.b32.i2p to ${_c.i2p_sam_forward_udp_host}:${_c.i2p_sam_forward_udp_port}`);
169+
}, 10000);
177170
}
178171

179172
private hasP2PNetwork(): Boolean {
@@ -286,16 +279,16 @@ export class Network extends EventEmitter {
286279

287280
const aNetwork = Util.shuffleArray(this.arrayNetwork.filter((v) => v.http !== this.server.config.http));
288281
let urlApi = '';
289-
do {
290-
urlApi = `http://${toB32(aNetwork.pop().http)}.b32.i2p/${endpoint}`;
282+
let n = aNetwork.pop();
283+
while (n) {
284+
urlApi = `http://${toB32(n.http)}.b32.i2p/${endpoint}`;
291285
try {
292-
const json = await this.fetch(urlApi, timeout);
293-
return JSON.parse(json);
286+
return JSON.parse(await this.fetch(urlApi, timeout));
294287
} catch (error: any) {
295288
Logger.warn(`Network.fetchFromApi() ${urlApi} - ${error.toString()}`);
296289
}
297-
} while (aNetwork.length > 0);
298-
290+
n = aNetwork.pop();
291+
}
299292
throw new Error('fetchFromApi failed');
300293
}
301294

0 commit comments

Comments
 (0)