From 01dd59cd5fdc6dbca5d5b614faaf45b291b67768 Mon Sep 17 00:00:00 2001 From: zeroxbt Date: Mon, 17 Jul 2023 22:16:02 +0200 Subject: [PATCH 1/2] only start libp2p after router init --- ot-node.js | 6 ++++++ src/modules/network/implementation/libp2p-service.js | 5 ++++- src/modules/network/network-module-manager.js | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ot-node.js b/ot-node.js index 2660f544ad..5e89a6b99f 100644 --- a/ot-node.js +++ b/ot-node.js @@ -69,6 +69,7 @@ class OTNode { await this.initializeCommandExecutor(); await this.initializeRouters(); + await this.startNetworkModule(); this.logger.info('Node is up and running!'); } @@ -254,6 +255,11 @@ class OTNode { } } + async startNetworkModule() { + const networkModuleManager = this.container.resolve('networkModuleManager'); + await networkModuleManager.start(); + } + async executePrivateAssetsMetadataMigration() { if ( process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT || diff --git a/src/modules/network/implementation/libp2p-service.js b/src/modules/network/implementation/libp2p-service.js index 1b99a1c873..1d3885debe 100644 --- a/src/modules/network/implementation/libp2p-service.js +++ b/src/modules/network/implementation/libp2p-service.js @@ -101,13 +101,16 @@ class Libp2pService { */ this.sessions = {}; this.node = await libp2p.create(initializationObject); - await this.node.start(); const port = parseInt(this.node.multiaddrs.toString().split('/')[4], 10); const peerId = this.node.peerId.toB58String(); this.config.id = peerId; this.logger.info(`Network ID is ${peerId}, connection port is ${port}`); } + async start() { + this.node.start(); + } + async onPeerConnected(listener) { this.node.connectionManager.on('peer:connect', listener); } diff --git a/src/modules/network/network-module-manager.js b/src/modules/network/network-module-manager.js index 758ca4244a..698c7f5697 100644 --- a/src/modules/network/network-module-manager.js +++ b/src/modules/network/network-module-manager.js @@ -5,6 +5,12 @@ class NetworkModuleManager extends BaseModuleManager { return 'network'; } + async start() { + if (this.initialized) { + return this.getImplementation().module.start(); + } + } + async onPeerConnected(listener) { if (this.initialized) { return this.getImplementation().module.onPeerConnected(listener); From 82fa8e84af4799b4c4bef7089280aa15f1e67595 Mon Sep 17 00:00:00 2001 From: zeroxbt Date: Mon, 17 Jul 2023 22:26:23 +0200 Subject: [PATCH 2/2] fix update peer last seen and dialed --- .../repositories/shard-repository.js | 4 ++-- src/service/sharding-table-service.js | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/modules/repository/implementation/sequelize/repositories/shard-repository.js b/src/modules/repository/implementation/sequelize/repositories/shard-repository.js index d2a00d74e2..08dc04df82 100644 --- a/src/modules/repository/implementation/sequelize/repositories/shard-repository.js +++ b/src/modules/repository/implementation/sequelize/repositories/shard-repository.js @@ -108,7 +108,7 @@ class ShardRepository { } async updatePeerRecordLastDialed(peerId, timestamp) { - await this.model.update( + return this.model.update( { lastDialed: timestamp, }, @@ -119,7 +119,7 @@ class ShardRepository { } async updatePeerRecordLastSeenAndLastDialed(peerId, timestamp) { - await this.model.update( + return this.model.update( { lastDialed: timestamp, lastSeen: timestamp, diff --git a/src/service/sharding-table-service.js b/src/service/sharding-table-service.js index 2a09e598ef..00bab83bcc 100644 --- a/src/service/sharding-table-service.js +++ b/src/service/sharding-table-service.js @@ -214,15 +214,21 @@ class ShardingTableService { }; } if (this.memoryCachedPeerIds[peerId].lastUpdated < timestampThreshold) { - await this.repositoryModuleManager.updatePeerRecordLastSeenAndLastDialed(peerId, now); - this.memoryCachedPeerIds[peerId].lastUpdated = now; + const [rowsUpdated] = + await this.repositoryModuleManager.updatePeerRecordLastSeenAndLastDialed( + peerId, + now, + ); + if (rowsUpdated) { + this.memoryCachedPeerIds[peerId].lastUpdated = now; + } } this.memoryCachedPeerIds[peerId].lastDialed = now; this.memoryCachedPeerIds[peerId].lastSeen = now; } async updatePeerRecordLastDialed(peerId) { - const now = new Date(); + const now = Date.now(); const timestampThreshold = now - PEER_RECORD_UPDATE_DELAY; if (!this.memoryCachedPeerIds[peerId]) { this.memoryCachedPeerIds[peerId] = { @@ -232,8 +238,13 @@ class ShardingTableService { }; } if (this.memoryCachedPeerIds[peerId].lastUpdated < timestampThreshold) { - await this.repositoryModuleManager.updatePeerRecordLastDialed(peerId, now); - this.memoryCachedPeerIds[peerId].lastUpdated = now; + const [rowsUpdated] = await this.repositoryModuleManager.updatePeerRecordLastDialed( + peerId, + now, + ); + if (rowsUpdated) { + this.memoryCachedPeerIds[peerId].lastUpdated = now; + } } this.memoryCachedPeerIds[peerId].lastDialed = now; }