Skip to content
This repository was archived by the owner on Feb 5, 2026. It is now read-only.

Commit ae60298

Browse files
authored
BE-876 Stop unnecessary discovery request (#255)
Explorer was making multiple `sendDiscoveryRequest` calls, 1 per block, which retrieves the same information. Signed-off-by: Atsushi Neki <nekiaiken@gmail.com>
1 parent fef5460 commit ae60298

File tree

6 files changed

+4506
-29
lines changed

6 files changed

+4506
-29
lines changed

app/platform/fabric/sync/FabricEvent.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export class FabricEvent {
6666
async event => {
6767
// Skip first block, it is process by peer event hub
6868
if (!(event.blockNumber.low === 0 && event.blockNumber.high === 0)) {
69-
await this.fabricServices.processBlockEvent(this.client, event.blockData);
69+
const noDiscovery = false;
70+
await this.fabricServices.processBlockEvent(
71+
this.client,
72+
event.blockData,
73+
noDiscovery
74+
);
7075
}
7176
},
7277
{

app/platform/fabric/sync/SyncPlatform.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ export class SyncPlatform {
117117
* Setting interval for validating any missing block from the current client ledger
118118
* Set blocksSyncTime property in platform config.json in minutes
119119
*/
120-
(function validateMissingBlocks(sync) {
121-
sync.isChannelEventHubConnected();
122-
setTimeout(validateMissingBlocks, sync.blocksSyncTime, sync);
123-
})(this);
120+
// During initial sync-up phase, disable discovery request
121+
(function validateMissingBlocks(sync: SyncPlatform, noDiscovery: boolean) {
122+
sync.isChannelEventHubConnected(noDiscovery);
123+
setTimeout(validateMissingBlocks, sync.blocksSyncTime, sync, false);
124+
})(this, true);
124125

125126
logger.debug(
126127
'******* Initialization end for child client process %s ******',
@@ -133,12 +134,12 @@ export class SyncPlatform {
133134
*
134135
* @memberof SyncPlatform
135136
*/
136-
async isChannelEventHubConnected() {
137+
async isChannelEventHubConnected(noDiscovery: boolean) {
137138
for (const channel_name of this.client.getChannels()) {
138139
// Validate channel event is connected
139140
const status = this.eventHub.isChannelEventHubConnected(channel_name);
140141
if (status) {
141-
await this.syncService.syncBlocks(this.client, channel_name);
142+
await this.syncService.syncBlocks(this.client, channel_name, noDiscovery);
142143
} else {
143144
// Channel client is not connected then it will reconnect
144145
this.eventHub.connectChannelEventHub(channel_name);

app/platform/fabric/sync/SyncService.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,16 @@ export class SyncServices {
357357
.saveChaincodPeerRef(network_id, chaincode_peer_row);
358358
}
359359

360-
async syncBlocks(client, channel_name) {
360+
async syncBlocks(client, channel_name, noDiscovery) {
361361
const network_id = client.getNetworkId();
362362

363-
// Get channel information from ledger
364-
const channelInfo = await client.fabricGateway.queryChainInfo(channel_name);
363+
// Get channel information from ledger
364+
const channelInfo = await client.fabricGateway.queryChainInfo(channel_name);
365365

366-
if (!channelInfo) {
367-
logger.info(
368-
`syncBlocks: Failed to retrieve channelInfo >> ${channel_name}`,
369-
);
370-
return;
371-
}
366+
if (!channelInfo) {
367+
logger.info(`syncBlocks: Failed to retrieve channelInfo >> ${channel_name}`);
368+
return;
369+
}
372370
const synch_key = `${network_id}_${channel_name}`;
373371
logger.info(`syncBlocks: Start >> ${synch_key}`);
374372
if (this.synchInProcess.includes(synch_key)) {
@@ -393,7 +391,7 @@ export class SyncServices {
393391
result.missing_id
394392
);
395393
if (block) {
396-
await this.processBlockEvent(client, block);
394+
await this.processBlockEvent(client, block, noDiscovery);
397395
}
398396
} catch {
399397
logger.error(`Failed to process Block # ${result.missing_id}`);
@@ -410,7 +408,6 @@ export class SyncServices {
410408
async updateDiscoveredChannel(client, channel_name, channel_genesis_hash) {
411409
const network_id = client.getNetworkId();
412410
// get discovery and insert new peer, orders details to db
413-
await client.initializeChannelFromDiscover(channel_name);
414411
await this.insertFromDiscoveryResults(
415412
client,
416413
channel_name,
@@ -459,7 +456,7 @@ export class SyncServices {
459456
* @returns
460457
* @memberof SyncServices
461458
*/
462-
async processBlockEvent(client, block) {
459+
async processBlockEvent(client, block, noDiscovery) {
463460
const network_id = client.getNetworkId();
464461
// Get the first transaction
465462
const first_tx = block.data.data[0];
@@ -492,7 +489,10 @@ export class SyncServices {
492489
}
493490
this.blocksInProcess.push(blockPro_key);
494491

495-
if (header.channel_header.typeString === fabric_const.BLOCK_TYPE_CONFIG) {
492+
if (
493+
!noDiscovery &&
494+
header.channel_header.typeString === fabric_const.BLOCK_TYPE_CONFIG
495+
) {
496496
setTimeout(
497497
async (cli, chName, chGenHash) => {
498498
await this.updateDiscoveredChannel(cli, chName, chGenHash);
@@ -629,9 +629,11 @@ export class SyncServices {
629629
const chaincode_id = String.fromCharCode.apply(null, chaincodeID);
630630
// checking new chaincode is deployed
631631
if (
632+
!noDiscovery &&
632633
header.channel_header.typeString ===
633634
fabric_const.BLOCK_TYPE_ENDORSER_TRANSACTION &&
634-
chaincode === fabric_const.CHAINCODE_LSCC
635+
(chaincode === fabric_const.CHAINCODE_LSCC ||
636+
chaincode === fabric_const.CHAINCODE_LIFECYCLE)
635637
) {
636638
setTimeout(
637639
async (cli, chName, chGenHash) => {

app/platform/fabric/utils/FabricConst.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const fabric = {
99
BLOCK_TYPE_CONFIG: 'CONFIG',
1010
BLOCK_TYPE_ENDORSER_TRANSACTION: 'ENDORSER_TRANSACTION',
1111
CHAINCODE_LSCC: 'lscc',
12+
CHAINCODE_LIFECYCLE: '_lifecycle',
1213
NOTITY_TYPE_NEWCHANNEL: '1',
1314
NOTITY_TYPE_UPDATECHANNEL: '2',
1415
NOTITY_TYPE_CHAINCODE: '3',

app/test/SyncService.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import sinon from 'sinon';
1010
import { SyncServices } from '../platform/fabric/sync/SyncService';
1111
import * as stubBlock from './block.json';
1212
import * as stubConfigBlock from './block_config.json';
13+
import * as stubLifecycleBlock from './block_lifecycle.json';
1314

1415
import { ExplorerError } from '../common/ExplorerError';
1516
import * as FabricConst from '../platform/fabric/utils/FabricConst';
@@ -128,8 +129,8 @@ describe('processBlockEvent', () => {
128129
it('should return without error', async () => {
129130
const stubClient = setupClient();
130131

131-
await expect(sync.processBlockEvent(stubClient, stubBlock)).eventually.to.be
132-
.true;
132+
await expect(sync.processBlockEvent(stubClient, stubBlock, false)).eventually
133+
.to.be.true;
133134
sinon.assert.calledOnce(stubPlatform.send);
134135
sinon.assert.calledWith(
135136
stubPlatform.send,
@@ -142,7 +143,7 @@ describe('processBlockEvent', () => {
142143
const stubClient = setupClient();
143144
sync.blocksInProcess = ['mychannel_9'];
144145

145-
await expect(sync.processBlockEvent(stubClient, stubBlock))
146+
await expect(sync.processBlockEvent(stubClient, stubBlock, false))
146147
.to.eventually.be.rejectedWith('Block already in processing')
147148
.and.be.an.instanceOf(ExplorerError);
148149
sinon.assert.notCalled(stubPlatform.send);
@@ -158,7 +159,7 @@ describe('processBlockEvent', () => {
158159
const spyInsertDiscoveredCH = sinon.spy(sync, 'insertDiscoveredChannel');
159160

160161
const clock = sinon.useFakeTimers();
161-
await expect(sync.processBlockEvent(stubClient, stubBlock))
162+
await expect(sync.processBlockEvent(stubClient, stubBlock, false))
162163
.to.eventually.be.rejectedWith('mychannel has not been inserted yet')
163164
.and.be.an.instanceOf(ExplorerError);
164165
clock.tick(20000);
@@ -174,7 +175,7 @@ describe('processBlockEvent', () => {
174175
const spyUpdateDiscoveredCH = sinon.spy(sync, 'updateDiscoveredChannel');
175176

176177
const clock = sinon.useFakeTimers();
177-
await expect(sync.processBlockEvent(stubClient, stubConfigBlock)).to
178+
await expect(sync.processBlockEvent(stubClient, stubConfigBlock, false)).to
178179
.eventually.to.be.true;
179180
clock.tick(20000);
180181

@@ -199,7 +200,14 @@ describe('processBlockEvent', () => {
199200
const stubClient = setupClient();
200201

201202
stubConfigBlock.data.data[0].payload.data.last_update.payload = null;
202-
await expect(sync.processBlockEvent(stubClient, stubConfigBlock)).to
203+
await expect(sync.processBlockEvent(stubClient, stubConfigBlock, false)).to
204+
.eventually.to.be.true;
205+
});
206+
207+
it('should be done without any errors when _lifecycle block is processed', async () => {
208+
const stubClient = setupClient();
209+
210+
await expect(sync.processBlockEvent(stubClient, stubLifecycleBlock, false)).to
203211
.eventually.to.be.true;
204212
});
205213
});
@@ -219,7 +227,7 @@ describe('syncBlocks', () => {
219227
const stubClient = setupClient();
220228
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');
221229

222-
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME);
230+
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME, false);
223231
expect(stubProcessBlockEvent.calledTwice).to.be.true;
224232
stubProcessBlockEvent.restore();
225233
});
@@ -230,7 +238,7 @@ describe('syncBlocks', () => {
230238
stubProcessBlockEvent.onFirstCall().throws('Block already in processing');
231239
stubError.reset();
232240

233-
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME);
241+
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME, false);
234242
expect(stubProcessBlockEvent.calledTwice).to.be.true;
235243
expect(stubError.calledWith('Failed to process Block # 1')).to.be.true;
236244
expect(stubError.calledWith('Failed to process Block # 2')).to.be.false;

0 commit comments

Comments
 (0)