Skip to content

Commit

Permalink
fixed: startup bug (P2P hangs)
Browse files Browse the repository at this point in the history
  • Loading branch information
diva-exchange committed Mar 30, 2022
1 parent 061537f commit 43289d5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2021 diva.exchange
# Copyright (C) 2021-2022 diva.exchange
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down
8 changes: 4 additions & 4 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#
# Copyright (C) 2021 diva.exchange
# Copyright (C) 2021-2022 diva.exchange
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -52,10 +52,10 @@ cp ${PROJECT_PATH}build/version ${PROJECT_PATH}dist/version
if command_exists pkg; then
info "Packaging..."

#leveldown prebuilds
[[ -d ${PROJECT_PATH}node_modules/leveldown/prebuilds/ ]] &&
#classic-level prebuilds
[[ -d ${PROJECT_PATH}node_modules/classic-level/prebuilds/ ]] &&
mkdir -p ${PROJECT_PATH}build/prebuilds &&
cp -r ${PROJECT_PATH}node_modules/leveldown/prebuilds/linux-x64 ${PROJECT_PATH}build/prebuilds/linux-x64
cp -r ${PROJECT_PATH}node_modules/classic-level/prebuilds/linux-x64 ${PROJECT_PATH}build/prebuilds/linux-x64

pkg --no-bytecode \
--public \
Expand Down
17 changes: 13 additions & 4 deletions src/net/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,30 @@ export class Network extends EventEmitter {
}

private init() {
Logger.info(`P2P starting on ${toB32(this.server.config.udp)}.b32.i2p`);

let retry = 0;
let started = false;
const i = setInterval(() => {
const _c = this.server.config;
retry++;
if (retry > 60) {
throw new Error(`P2P failed on ${toB32(this.server.config.udp)}.b32.i2p`);
}

if (started) {
if (this.hasP2PNetwork()) {
this.emit('ready');
clearInterval(i);
Logger.info(`P2P ready on ${toB32(this.server.config.udp)}.b32.i2p`);
}
return;
}

started = true;
Logger.info(`P2P starting on ${toB32(_c.udp)}.b32.i2p`);
this.p2pNetwork();

(async () => {
const _c = this.server.config;
this.samForward = (
await createForward({
sam: {
Expand All @@ -134,6 +141,7 @@ export class Network extends EventEmitter {
})();

(async () => {
const _c = this.server.config;
this.samUDP = (
await createDatagram({
sam: {
Expand Down Expand Up @@ -191,14 +199,15 @@ export class Network extends EventEmitter {
}

private p2pNetwork() {
this.timeoutP2P = setTimeout(async () => {
this.timeoutP2P = setTimeout(() => {
this.p2pNetwork();
}, this.server.config.network_p2p_interval_ms);

const aNetwork = [...this.server.getBlockchain().getMapPeer().values()];
if (!aNetwork.length || !Object.keys(this.samUDP).length) {
if (!aNetwork.length || !Object.keys(this.samForward).length || !Object.keys(this.samUDP).length) {
return;
}

this.arrayNetwork = aNetwork;

this.arrayBroadcast = Util.shuffleArray(
Expand Down
2 changes: 0 additions & 2 deletions src/net/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ export class Server {

return new Promise((resolve) => {
this.network.once('ready', async () => {
Logger.info('Network ready');

this.bootstrap = Bootstrap.make(this);
if (this.config.bootstrap) {
await this.bootstrap.syncWithNetwork();
Expand Down

0 comments on commit 43289d5

Please sign in to comment.