Skip to content

Commit 3cf27da

Browse files
authored
BE-855 Add try catch block to handle block in-process exception (#239)
Signed-off-by: Atsushi Neki <[email protected]>
1 parent a8869bc commit 3cf27da

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

app/platform/fabric/sync/SyncService.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,16 @@ export class SyncServices {
376376
if (results) {
377377
for (const result of results) {
378378
// Get block by number
379-
const block = await client.fabricGateway.queryBlock(
380-
channel_name,
381-
result.missing_id
382-
);
383-
if (block) {
384-
await this.processBlockEvent(client, block);
379+
try {
380+
const block = await client.fabricGateway.queryBlock(
381+
channel_name,
382+
result.missing_id
383+
);
384+
if (block) {
385+
await this.processBlockEvent(client, block);
386+
}
387+
} catch {
388+
logger.error(`Failed to process Block # ${result.missing_id}`);
385389
}
386390
}
387391
} else {

app/test/SyncService.test.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const stubDebug = sinon.stub();
2222

2323
const VALID_GENESIS_HASH = '8A+HyzS4sqZynD06BfNW7T1Vtv2SOXAOUJQK4itulus=';
2424
const VALID_NETWORK_ID = 'test-network-id';
25+
const VALID_CHANNEL_NAME = 'testchannel';
2526

2627
const stubPlatform = {
2728
send: sinon.spy()
@@ -58,10 +59,18 @@ function getSyncServicesInstance() {
5859
const stubGetCrudService = sinon.stub();
5960
stubGetCrudService.returns({
6061
saveBlock: sinon.stub().resolves(true),
61-
saveTransaction: spySaveTransaction
62+
saveTransaction: spySaveTransaction,
63+
getChannel: sinon.stub()
64+
});
65+
const stubGetMetricService = sinon.stub();
66+
stubGetMetricService.returns({
67+
findMissingBlockNumber: sinon
68+
.stub()
69+
.returns([{ missing_id: 1 }, { missing_id: 2 }])
6270
});
6371
const stubPersistence = {
64-
getCrudService: stubGetCrudService
72+
getCrudService: stubGetCrudService,
73+
getMetricService: stubGetMetricService
6574
};
6675
const sync = new SyncServices(stubPlatform, stubPersistence);
6776

@@ -87,10 +96,19 @@ function setupClient() {
8796
getNetworkId: stubGetNetworkID,
8897
getChannelGenHash: stubGetChannelGenHash,
8998
initializeNewChannel: sinon.stub().resolves(true),
99+
initializeChannelFromDiscover: sinon.stub(),
90100
fabricGateway: {
91101
fabricConfig: {
92102
getRWSetEncoding: sinon.stub()
93-
}
103+
},
104+
queryChainInfo: sinon.stub().returns({ height: { low: 10 } }),
105+
queryBlock: sinon.fake((channel_name, missing_id) => {
106+
if (channel_name === VALID_CHANNEL_NAME) {
107+
return stubBlock;
108+
} else {
109+
return null;
110+
}
111+
})
94112
}
95113
};
96114
return stubClient;
@@ -185,3 +203,37 @@ describe('processBlockEvent', () => {
185203
.eventually.to.be.true;
186204
});
187205
});
206+
207+
describe('synchBlocks', () => {
208+
let sync: SyncServices;
209+
210+
before(() => {
211+
sync = getSyncServicesInstance();
212+
});
213+
214+
beforeEach(() => {
215+
resetAllStubs(sync);
216+
});
217+
218+
it('should return without error', async () => {
219+
const stubClient = setupClient();
220+
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');
221+
222+
await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
223+
expect(stubProcessBlockEvent.calledTwice).to.be.true;
224+
stubProcessBlockEvent.restore();
225+
});
226+
227+
it('should return without error when processBlockEvent throws exception', async () => {
228+
const stubClient = setupClient();
229+
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');
230+
stubProcessBlockEvent.onFirstCall().throws('Block already in processing');
231+
stubError.reset();
232+
233+
await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
234+
expect(stubProcessBlockEvent.calledTwice).to.be.true;
235+
expect(stubError.calledWith('Failed to process Block # 1')).to.be.true;
236+
expect(stubError.calledWith('Failed to process Block # 2')).to.be.false;
237+
stubProcessBlockEvent.restore();
238+
});
239+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"convert-hex": "^0.1.0",
2121
"ejs": "^2.5.6",
2222
"enum": "^2.5.0",
23+
"eslint": "^7.26.0",
2324
"express": "^4.15.3",
2425
"express-rate-limit": "^5.0.0",
2526
"fabric-ca-client": "^2.2.4",
@@ -107,6 +108,8 @@
107108
"build": "tsc",
108109
"app-start": "./start.sh",
109110
"app-stop": "./stop.sh",
111+
"sync-start": "./syncstart.sh",
112+
"sync-stop": "./syncstop.sh",
110113
"start": "run-s build app-start",
111114
"docker_build": "docker build --squash -t $npm_package_name .",
112115
"docker_push": "docker push ${DOCKER_REGISTRY}/$npm_package_name:$npm_package_version",

syncstop.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
#
44

5-
kill -SIGTERM $(ps aux | grep 'sync.js' | grep -v grep | awk '{print $2}')
5+
kill -15 $(ps aux | grep 'sync.js' | grep -v grep | awk '{print $2}')
66

77

88

0 commit comments

Comments
 (0)