Skip to content

Commit

Permalink
BE-855 Add try catch block to handle block in-process exception (#239)
Browse files Browse the repository at this point in the history
Signed-off-by: Atsushi Neki <nekiaiken@gmail.com>
  • Loading branch information
nekia authored May 20, 2021
1 parent a8869bc commit 3cf27da
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
16 changes: 10 additions & 6 deletions app/platform/fabric/sync/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,16 @@ export class SyncServices {
if (results) {
for (const result of results) {
// Get block by number
const block = await client.fabricGateway.queryBlock(
channel_name,
result.missing_id
);
if (block) {
await this.processBlockEvent(client, block);
try {
const block = await client.fabricGateway.queryBlock(
channel_name,
result.missing_id
);
if (block) {
await this.processBlockEvent(client, block);
}
} catch {
logger.error(`Failed to process Block # ${result.missing_id}`);
}
}
} else {
Expand Down
58 changes: 55 additions & 3 deletions app/test/SyncService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const stubDebug = sinon.stub();

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

const stubPlatform = {
send: sinon.spy()
Expand Down Expand Up @@ -58,10 +59,18 @@ function getSyncServicesInstance() {
const stubGetCrudService = sinon.stub();
stubGetCrudService.returns({
saveBlock: sinon.stub().resolves(true),
saveTransaction: spySaveTransaction
saveTransaction: spySaveTransaction,
getChannel: sinon.stub()
});
const stubGetMetricService = sinon.stub();
stubGetMetricService.returns({
findMissingBlockNumber: sinon
.stub()
.returns([{ missing_id: 1 }, { missing_id: 2 }])
});
const stubPersistence = {
getCrudService: stubGetCrudService
getCrudService: stubGetCrudService,
getMetricService: stubGetMetricService
};
const sync = new SyncServices(stubPlatform, stubPersistence);

Expand All @@ -87,10 +96,19 @@ function setupClient() {
getNetworkId: stubGetNetworkID,
getChannelGenHash: stubGetChannelGenHash,
initializeNewChannel: sinon.stub().resolves(true),
initializeChannelFromDiscover: sinon.stub(),
fabricGateway: {
fabricConfig: {
getRWSetEncoding: sinon.stub()
}
},
queryChainInfo: sinon.stub().returns({ height: { low: 10 } }),
queryBlock: sinon.fake((channel_name, missing_id) => {
if (channel_name === VALID_CHANNEL_NAME) {
return stubBlock;
} else {
return null;
}
})
}
};
return stubClient;
Expand Down Expand Up @@ -185,3 +203,37 @@ describe('processBlockEvent', () => {
.eventually.to.be.true;
});
});

describe('synchBlocks', () => {
let sync: SyncServices;

before(() => {
sync = getSyncServicesInstance();
});

beforeEach(() => {
resetAllStubs(sync);
});

it('should return without error', async () => {
const stubClient = setupClient();
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');

await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
expect(stubProcessBlockEvent.calledTwice).to.be.true;
stubProcessBlockEvent.restore();
});

it('should return without error when processBlockEvent throws exception', async () => {
const stubClient = setupClient();
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');
stubProcessBlockEvent.onFirstCall().throws('Block already in processing');
stubError.reset();

await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
expect(stubProcessBlockEvent.calledTwice).to.be.true;
expect(stubError.calledWith('Failed to process Block # 1')).to.be.true;
expect(stubError.calledWith('Failed to process Block # 2')).to.be.false;
stubProcessBlockEvent.restore();
});
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"convert-hex": "^0.1.0",
"ejs": "^2.5.6",
"enum": "^2.5.0",
"eslint": "^7.26.0",
"express": "^4.15.3",
"express-rate-limit": "^5.0.0",
"fabric-ca-client": "^2.2.4",
Expand Down Expand Up @@ -107,6 +108,8 @@
"build": "tsc",
"app-start": "./start.sh",
"app-stop": "./stop.sh",
"sync-start": "./syncstart.sh",
"sync-stop": "./syncstop.sh",
"start": "run-s build app-start",
"docker_build": "docker build --squash -t $npm_package_name .",
"docker_push": "docker push ${DOCKER_REGISTRY}/$npm_package_name:$npm_package_version",
Expand Down
2 changes: 1 addition & 1 deletion syncstop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
#

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



0 comments on commit 3cf27da

Please sign in to comment.