Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE-855 Add try catch block to handle block in-process exception #239

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions app/platform/fabric/sync/SyncService.ts
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
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
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
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}')