Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Check the error exists or not and then return with proper message #3924

Merged
merged 6 commits into from
Jul 9, 2019
19 changes: 13 additions & 6 deletions framework/src/modules/chain/submodules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,21 +813,28 @@ __private.loadBlocksFromNetwork = function(cb) {
modules.blocks.process.loadBlocksFromNetwork(
(loadBlocksFromNetworkErr, lastValidBlock) => {
if (loadBlocksFromNetworkErr) {
library.logger.debug(
loadBlocksFromNetworkErr instanceof Error
ishantiw marked this conversation as resolved.
Show resolved Hide resolved
? loadBlocksFromNetworkErr
: new Error(loadBlocksFromNetworkErr),
'Chain recovery failed after failing to load blocks from the network'
);
// If comparison failed and current consensus is low - perform chain recovery
if (modules.peers.isPoorConsensus()) {
library.logger.debug(
'Perform chain recovery due to poor consensus'
);
return modules.blocks.chain.recoverChain(recoveryError => {
waterCb(
`Failed chain recovery after failing to load blocks while network consensus was low. ${recoveryError}`
if (recoveryError) {
return waterCb(
`Chain recovery failed after failing to load blocks while network consensus was low. ${recoveryError}`
);
}
return waterCb(
`Chain recovery failed chain recovery after failing to load blocks ${loadBlocksFromNetworkErr}`
);
});
}
library.logger.error(
'Failed to process block from network',
loadBlocksFromNetworkErr
);
return waterCb(
`Failed to load blocks from the network. ${loadBlocksFromNetworkErr}`
);
Expand Down
99 changes: 99 additions & 0 deletions framework/test/mocha/unit/modules/chain/submodules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,105 @@ describe('loader', () => {
});
});

describe('__private.loadBlocksFromNetwork', () => {
let loggerStub;
let poorConsensusStub;
let restoreLogger;
const loadBlockModulesError = new Error(
'Error occurred while loading blocks'
);
const chainRecoveryFailError =
'Chain recovery failed chain recovery after failing to load blocks';

beforeEach(async () => {
loggerStub = {
error: sinonSandbox.stub(),
debug: sinonSandbox.stub(),
};
poorConsensusStub = sinonSandbox
.stub(library.modules.peers, 'isPoorConsensus')
.resolves(true);
restoreLogger = library.rewiredModules.loader.__set__(
'library.logger',
loggerStub
);
});

afterEach(async () => {
poorConsensusStub.restore();
restoreLogger();
});

describe('when modules.blocks.process.loadBlocksFromNetwork throws an error', () => {
let loadBlocksFromNetworkBlocksModule;
let recoverChainStub;
let lastBlockGetStub;

beforeEach(async () => {
lastBlockGetStub = sinonSandbox
.stub(library.modules.blocks.lastBlock, 'get')
.resolves({});
loadBlocksFromNetworkBlocksModule = sinonSandbox
.stub(library.modules.blocks.process, 'loadBlocksFromNetwork')
.callsArgWith(0, loadBlockModulesError, {});
recoverChainStub = sinonSandbox
.stub(library.modules.blocks.chain, 'recoverChain')
.callsArgWith(0, null, true);
});

afterEach(async () => {
loadBlocksFromNetworkBlocksModule.restore();
recoverChainStub.restore();
lastBlockGetStub.restore();
});

it('should throw error when modules.blocks.process.loadBlocksFromNetwork fails and logs it', done => {
__private.loadBlocksFromNetwork(() => {
expect(loggerStub.error).to.be.calledWith(
`${chainRecoveryFailError} Error: ${loadBlockModulesError.message}`
);
done();
});
});
});

describe('when modules.blocks.chain.recoverChain throws an error', () => {
const chainRecoveryErrorMessage =
'Chain recovery failed after failing to load blocks while network consensus was low.';
const recoverChainError = new Error('Error While Recovery');
let loadBlocksFromNetworkBlocksModule;
let recoverChainStub;
let lastBlockGetStub;

beforeEach(async () => {
lastBlockGetStub = sinonSandbox
.stub(library.modules.blocks.lastBlock, 'get')
.resolves({});
loadBlocksFromNetworkBlocksModule = sinonSandbox
.stub(library.modules.blocks.process, 'loadBlocksFromNetwork')
.callsArgWith(0, loadBlockModulesError, {});
recoverChainStub = sinonSandbox
.stub(library.modules.blocks.chain, 'recoverChain')
.callsArgWith(0, recoverChainError);
});

afterEach(async () => {
loadBlocksFromNetworkBlocksModule.restore();
recoverChainStub.restore();
lastBlockGetStub.restore();
});

it('should throw error returned by modules.blocks.chain.recoverChain', done => {
__private.loadBlocksFromNetwork(() => {
expect(loggerStub.error).to.be.calledWith(
`${chainRecoveryErrorMessage} Error: ${recoverChainError.message}`
);
done();
});
});
});
});

describe('__private.rebuildAccounts', () => {
let __privateVar;
let libraryVar;
Expand Down