From b772353284e52caa8535bb1130b8ad1396b7f6a5 Mon Sep 17 00:00:00 2001 From: zeroxbt <89495162+zeroxbt@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:42:08 +0200 Subject: [PATCH 1/2] fix bulk update stake and ask events (#2636) --- .../repositories/shard-repository.js | 46 ++++++++++--------- .../repository/repository-module-manager.js | 8 ++-- .../blockchain-event-listener-service.js | 33 ++++++------- 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/modules/repository/implementation/sequelize/repositories/shard-repository.js b/src/modules/repository/implementation/sequelize/repositories/shard-repository.js index 3b5afb1d66..d2a00d74e2 100644 --- a/src/modules/repository/implementation/sequelize/repositories/shard-repository.js +++ b/src/modules/repository/implementation/sequelize/repositories/shard-repository.js @@ -6,23 +6,11 @@ class ShardRepository { this.model = models.shard; } - async createManyPeerRecords(peers) { - return this._bulkUpdatePeerRecords(peers, ['ask', 'stake', 'sha256']); - } - - async _bulkUpdatePeerRecords(peerRecords, updateColumns) { - return this.model.bulkCreate( - peerRecords.map((peerRecord) => ({ - ask: 0, - stake: 0, - sha256: '', - ...peerRecord, - })), - { - validate: true, - updateOnDuplicate: updateColumns, - }, - ); + async createManyPeerRecords(peerRecords) { + return this.model.bulkCreate(peerRecords, { + validate: true, + updateOnDuplicate: ['ask', 'stake', 'sha256'], + }); } async removeShardingTablePeerRecords(blockchainId) { @@ -95,12 +83,28 @@ class ShardRepository { return (result ?? []).map((record) => ({ peerId: record.peer_id })); } - async updatePeersAsk(peerRecords) { - return this._bulkUpdatePeerRecords(peerRecords, ['ask']); + async updatePeerAsk(peerId, blockchainId, ask) { + return this.model.update( + { ask }, + { + where: { + peerId, + blockchainId, + }, + }, + ); } - async updatePeersStake(peerRecords) { - return this._bulkUpdatePeerRecords(peerRecords, ['stake']); + async updatePeerStake(peerId, blockchainId, stake) { + return this.model.update( + { stake }, + { + where: { + peerId, + blockchainId, + }, + }, + ); } async updatePeerRecordLastDialed(peerId, timestamp) { diff --git a/src/modules/repository/repository-module-manager.js b/src/modules/repository/repository-module-manager.js index fd1cc4617f..74f832901b 100644 --- a/src/modules/repository/repository-module-manager.js +++ b/src/modules/repository/repository-module-manager.js @@ -188,12 +188,12 @@ class RepositoryModuleManager extends BaseModuleManager { return this.getRepository('shard').updatePeerRecordLastSeenAndLastDialed(peerId, timestamp); } - async updatePeersAsk(peerRecords) { - return this.getRepository('shard').updatePeersAsk(peerRecords); + async updatePeerAsk(peerId, blockchainId, ask) { + return this.getRepository('shard').updatePeerAsk(peerId, blockchainId, ask); } - async updatePeersStake(peerRecords) { - return this.getRepository('shard').updatePeersStake(peerRecords); + async updatePeerStake(peerId, blockchainId, stake) { + return this.getRepository('shard').updatePeerStake(peerId, blockchainId, stake); } async getNeighbourhood(assertionId, r2) { diff --git a/src/service/blockchain-event-listener-service.js b/src/service/blockchain-event-listener-service.js index 6c3c0721ce..c5a252be41 100644 --- a/src/service/blockchain-event-listener-service.js +++ b/src/service/blockchain-event-listener-service.js @@ -313,13 +313,13 @@ class BlockchainEventListenerService { this.logger.trace(`Removing peer id: ${nodeId} from sharding table.`); - this.repositoryModuleManager.removePeerRecord(event.blockchainId, nodeId); + await this.repositoryModuleManager.removePeerRecord(event.blockchainId, nodeId); }), ); } async handleStakeIncreasedEvents(blockEvents) { - const peerRecords = await Promise.all( + await Promise.all( blockEvents.map(async (event) => { const eventData = JSON.parse(event.data); @@ -330,18 +330,16 @@ class BlockchainEventListenerService { this.logger.trace(`Updating stake value for peer id: ${nodeId} in sharding table.`); - return { - peerId: nodeId, - blockchainId: event.blockchainId, - stake: this.blockchainModuleManager.convertFromWei( + await this.repositoryModuleManager.updatePeerStake( + nodeId, + event.blockchainId, + this.blockchainModuleManager.convertFromWei( event.blockchainId, eventData.newStake, ), - }; + ); }), ); - - await this.repositoryModuleManager.updatePeersStake(peerRecords); } async handleStakeWithdrawalStartedEvents(blockEvents) { @@ -349,7 +347,7 @@ class BlockchainEventListenerService { } async handleAskUpdatedEvents(blockEvents) { - const peerRecords = await Promise.all( + await Promise.all( blockEvents.map(async (event) => { const eventData = JSON.parse(event.data); @@ -360,18 +358,13 @@ class BlockchainEventListenerService { this.logger.trace(`Updating ask value for peer id: ${nodeId} in sharding table.`); - return { - peerId: nodeId, - blockchainId: event.blockchainId, - ask: this.blockchainModuleManager.convertFromWei( - event.blockchainId, - eventData.ask, - ), - }; + await this.repositoryModuleManager.updatePeerAsk( + nodeId, + event.blockchainId, + this.blockchainModuleManager.convertFromWei(event.blockchainId, eventData.ask), + ); }), ); - - await this.repositoryModuleManager.updatePeersAsk(peerRecords); } async handleServiceAgreementV1ExtendedEvents(blockEvents) { From 12a4e6f19b63ca8f2b3a8575090d5aef8e555f7e Mon Sep 17 00:00:00 2001 From: zeroxbt <89495162+zeroxbt@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:54:57 +0200 Subject: [PATCH 2/2] add new pull sharding table migration (#2637) * add new pull sharding table migration * bump version --- ot-node.js | 2 +- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ot-node.js b/ot-node.js index 9ac39cf525..2660f544ad 100644 --- a/ot-node.js +++ b/ot-node.js @@ -315,7 +315,7 @@ class OTNode { const validationModuleManager = this.container.resolve('validationModuleManager'); const migration = new PullBlockchainShardingTableMigration( - 'pullShardingTableMigrationV604', + 'pullShardingTableMigrationV612', this.logger, this.config, repositoryModuleManager, diff --git a/package-lock.json b/package-lock.json index 7ad232345b..1e48c518dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "origintrail_node", - "version": "6.0.11", + "version": "6.0.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "origintrail_node", - "version": "6.0.11", + "version": "6.0.12", "license": "ISC", "dependencies": { "@comunica/query-sparql": "^2.4.3", diff --git a/package.json b/package.json index aaeca8c038..c43d91afb6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "origintrail_node", - "version": "6.0.11", + "version": "6.0.12", "description": "OTNode V6", "main": "index.js", "type": "module",