From 9357d7a4cceac349fe991975a39d5ffc38ea3b3b Mon Sep 17 00:00:00 2001 From: Ashis Date: Mon, 21 Oct 2024 20:05:57 +0530 Subject: [PATCH 1/2] fix: added condition to avoid shard for INIT_DID trx --- src/helpers/dbHelper.ts | 3 +++ src/services/messaging-common/BlockUtil.ts | 20 ++++++++++++------ src/services/messaging/IndexStorage.ts | 24 ++++++++++------------ src/services/messaging/storageNode.ts | 5 ++--- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/helpers/dbHelper.ts b/src/helpers/dbHelper.ts index 5022db3..cd82c75 100755 --- a/src/helpers/dbHelper.ts +++ b/src/helpers/dbHelper.ts @@ -357,6 +357,9 @@ END $$ LANGUAGE plpgsql; body: Transaction ) { const transactionObj = body.toObject() + if (ns == 'INIT_DID') { + shardId = -1 + } log.debug(`putValueInStorageTable() namespace=${ns}, namespaceShardId=${shardId} , skey=${skey}, jsonValue=${transactionObj}`) const sql = `INSERT INTO storage_node (namespace, namespace_shard_id, namespace_id, ts, skey, dataschema, payload) diff --git a/src/services/messaging-common/BlockUtil.ts b/src/services/messaging-common/BlockUtil.ts index aea087a..719ee76 100644 --- a/src/services/messaging-common/BlockUtil.ts +++ b/src/services/messaging-common/BlockUtil.ts @@ -160,13 +160,21 @@ export class BlockUtil { static calculateAffectedShards(block: Block, shardCount: number): Set { const shards = new Set() for (const txObj of block.getTxobjList()) { - const senderAndRecipients = [txObj.getTx().getSender(), ...txObj.getTx().getRecipientsList()] - for (const wallet of senderAndRecipients) { - const shardId = this.calculateAffectedShard(wallet, shardCount) - if (shardId == null) { - continue + const category = txObj.getTx().getCategory() + if (category === 'INIT_DID') { + shards.add(-1) + } else { + const senderAndRecipients = [ + txObj.getTx().getSender(), + ...txObj.getTx().getRecipientsList() + ] + for (const wallet of senderAndRecipients) { + const shardId = this.calculateAffectedShard(wallet, shardCount) + if (shardId == null) { + continue + } + shards.add(shardId) } - shards.add(shardId) } } return shards diff --git a/src/services/messaging/IndexStorage.ts b/src/services/messaging/IndexStorage.ts index f118113..ecae223 100644 --- a/src/services/messaging/IndexStorage.ts +++ b/src/services/messaging/IndexStorage.ts @@ -33,13 +33,13 @@ export class IndexStorage { This is POSTGRES */ - public async unpackBlockToInboxes(mb: Block, shardSet: Set) { + public async unpackBlockToTransactions(mb: Block, shardSet: Set) { // this is the list of shards that we support on this node const nodeShards = this.storageContractState.getNodeShards() this.log.debug('storage node supports %s shards: %o', nodeShards.size, nodeShards) const shardsToProcess = Coll.intersectSet(shardSet, nodeShards) - this.log.debug('block %s has %d inboxes to unpack', mb.getTs(), shardsToProcess) - if (shardsToProcess.size == 0) { + this.log.debug('block %s has %d transactions to unpack', mb.getTs(), shardsToProcess) + if (shardsToProcess.size == 0 && !shardSet.has(-1)) { this.log.debug('finished') return } @@ -51,26 +51,24 @@ export class IndexStorage { } const currentNodeId = this.valContractState.nodeId for (let i = 0; i < mb.getTxobjList().length; i++) { - const feedItem = mb.getTxobjList()[i] + const transaction = mb.getTxobjList()[i] const targetWallets: string[] = MessageBlockUtil.calculateRecipients(mb, i) for (let i1 = 0; i1 < targetWallets.length; i1++) { const targetAddr = targetWallets[i1] - const targetShard = BlockUtil.calculateAffectedShard( - targetAddr, - this.storageContractState.shardCount - ) - if (!shardsToProcess.has(targetShard)) { + const targetShard = + transaction.getTx().getCategory() == 'INIT_DID' + ? -1 + : BlockUtil.calculateAffectedShard(targetAddr, this.storageContractState.shardCount) + if (!shardsToProcess.has(targetShard) && transaction.getTx().getCategory() !== 'INIT_DID') { continue } - // const trx = feedItem.getTx() - // console.log(trx.toObject()) await this.putPayloadToInbox( - feedItem.getTx().getCategory(), + transaction.getTx().getCategory(), targetShard, targetAddr, tsString, currentNodeId, - feedItem.getTx() + transaction.getTx() ) } } diff --git a/src/services/messaging/storageNode.ts b/src/services/messaging/storageNode.ts index 24d6b98..088197b 100644 --- a/src/services/messaging/storageNode.ts +++ b/src/services/messaging/storageNode.ts @@ -72,7 +72,6 @@ export default class StorageNode implements Consumer, StorageContractList // check for validation const parsedBlock = BlockUtil.parseBlock(mb) - const blockObject = parsedBlock.toObject() const validatorSet = new Set(this.valContractState.getAllNodesMap().keys()) const checkResult = await BlockUtil.checkBlockFinalized( parsedBlock, @@ -99,7 +98,7 @@ export default class StorageNode implements Consumer, StorageContractList return false } // send block - await this.indexStorage.unpackBlockToInboxes(parsedBlock, shardSet) + await this.indexStorage.unpackBlockToTransactions(parsedBlock, shardSet) } public async handleReshard( @@ -147,7 +146,7 @@ export default class StorageNode implements Consumer, StorageContractList Coll.setToArray(shardsToAdd), Coll.setToArray(shardsToAddFromBlock) ) - await this.indexStorage.unpackBlockToInboxes(mb, shardsToAddFromBlock) + await this.indexStorage.unpackBlockToTransactions(mb, shardsToAddFromBlock) } ) await this.blockStorage.saveNodeShards(newShards) From ad455a2c2f0e13b749fd222c43569c7377d1f00e Mon Sep 17 00:00:00 2001 From: Ashis Date: Tue, 22 Oct 2024 18:16:23 +0530 Subject: [PATCH 2/2] fix: revert changes --- src/services/messaging-common/BlockUtil.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/services/messaging-common/BlockUtil.ts b/src/services/messaging-common/BlockUtil.ts index 719ee76..aea087a 100644 --- a/src/services/messaging-common/BlockUtil.ts +++ b/src/services/messaging-common/BlockUtil.ts @@ -160,21 +160,13 @@ export class BlockUtil { static calculateAffectedShards(block: Block, shardCount: number): Set { const shards = new Set() for (const txObj of block.getTxobjList()) { - const category = txObj.getTx().getCategory() - if (category === 'INIT_DID') { - shards.add(-1) - } else { - const senderAndRecipients = [ - txObj.getTx().getSender(), - ...txObj.getTx().getRecipientsList() - ] - for (const wallet of senderAndRecipients) { - const shardId = this.calculateAffectedShard(wallet, shardCount) - if (shardId == null) { - continue - } - shards.add(shardId) + const senderAndRecipients = [txObj.getTx().getSender(), ...txObj.getTx().getRecipientsList()] + for (const wallet of senderAndRecipients) { + const shardId = this.calculateAffectedShard(wallet, shardCount) + if (shardId == null) { + continue } + shards.add(shardId) } } return shards