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

Remove bus from chain module - Closes #3813 #3840

Merged
merged 4 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions framework/src/modules/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Sequence = require('./utils/sequence');
const { createStorageComponent } = require('../../components/storage');
const { createCacheComponent } = require('../../components/cache');
const { createLoggerComponent } = require('../../components/logger');
const { createBus, bootstrapStorage, bootstrapCache } = require('./init_steps');
const { bootstrapStorage, bootstrapCache } = require('./init_steps');
const jobQueue = require('./utils/jobs_queue');
const { Peers } = require('./peers');
const { TransactionInterfaceAdapter } = require('./interface_adapters');
Expand Down Expand Up @@ -152,19 +152,12 @@ module.exports = class Chain {
await bootstrapStorage(this.scope, global.constants.ACTIVE_DELEGATES);
await bootstrapCache(this.scope);

this.scope.bus = await createBus();

await this._initModules();

this.scope.bus.registerModules(this.scope.modules);

this.channel.subscribe('app:state:updated', event => {
Object.assign(this.scope.applicationState, event.data);
});

// Fire onBind event in every module
this.scope.bus.message('bind', this.scope);

this.logger.info('Modules ready and launched');
// After binding, it should immediately load blockchain
await this.blocks.loadBlockChain(this.options.loading.rebuildUpToRound);
Expand Down Expand Up @@ -312,7 +305,6 @@ module.exports = class Chain {
logger: this.logger,
storage: this.storage,
},
bus: this.scope.bus,
slots: this.slots,
config: {
exceptions: this.options.exceptions,
Expand Down
53 changes: 0 additions & 53 deletions framework/src/modules/chain/init_steps/create_bus.js

This file was deleted.

2 changes: 0 additions & 2 deletions framework/src/modules/chain/init_steps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

'use strict';

const createBus = require('./create_bus');
const bootstrapStorage = require('./bootstrap_storage');
const bootstrapCache = require('./bootstrap_cache');

module.exports = {
createBus,
bootstrapStorage,
bootstrapCache,
};
15 changes: 1 addition & 14 deletions framework/src/modules/chain/rounds/rounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Rounds {
library = {
channel: scope.channel,
logger: scope.components.logger,
bus: scope.bus,
storage: scope.components.storage,
slots: scope.slots,
exceptions: scope.config.exceptions,
Expand Down Expand Up @@ -214,7 +213,7 @@ class Rounds {
.then(() => {
if (scope.finishRound) {
return promised.land().then(() => {
library.bus.message('finishRound', round);
library.channel.publish('chain:rounds:change', { number: round });
2snEM6 marked this conversation as resolved.
Show resolved Hide resolved
});
}
return true;
Expand Down Expand Up @@ -347,18 +346,6 @@ class Rounds {
);
}

/**
* Clear all cache entries related to delegate and emits a 'rounds/change' socket message.
*
* @param {number} round
* @emits rounds/change
* @todo Add description for the params
*/
// eslint-disable-next-line class-methods-use-this
async onFinishRound(round) {
return library.channel.publish('chain:rounds:change', { number: round });
}

/**
* Sets private constant loaded to true.
*
Expand Down
11 changes: 1 addition & 10 deletions framework/test/mocha/common/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const Sequence = require('../../../src/modules/chain/utils/sequence');
const { BlockSlots } = require('../../../src/modules/chain/blocks/block_slots');
const { createCacheComponent } = require('../../../src/components/cache');
const { StorageSandbox } = require('./storage_sandbox');
const initSteps = require('../../../src/modules/chain/init_steps');

let currentAppScope;

Expand Down Expand Up @@ -162,12 +161,8 @@ async function __init(sandbox, initScope) {
await startStorage();
await cache.bootstrap();

scope.bus = await initSteps.createBus();
scope.modules = await initStepsForTest.initModules(scope);

// Fire onBind event in every module
scope.bus.message('bind', scope);

// Listen to websockets
// await scope.webSocket.listen();
// Listen to http, https servers
Expand All @@ -182,8 +177,7 @@ async function __init(sandbox, initScope) {
return false;
};

// If bus is overridden, then we just return the scope, without waiting for genesisBlock
if (!initScope.waitForGenesisBlock || initScope.bus) {
if (!initScope.waitForGenesisBlock) {
scope.modules.delegates.onBlockchainReady = function() {};
return scope;
}
Expand Down Expand Up @@ -257,7 +251,6 @@ const initStepsForTest = {
logger: scope.components.logger,
storage: scope.components.storage,
},
bus: scope.bus,
slots: scope.slots,
config: {
exceptions: __testContext.config.modules.chain.exceptions,
Expand Down Expand Up @@ -385,8 +378,6 @@ const initStepsForTest = {
__testContext.config.constants.MAX_SHARED_TRANSACTIONS,
});

scope.bus.registerModules(modules);

return modules;
},
};
Expand Down
18 changes: 0 additions & 18 deletions framework/test/mocha/unit/modules/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ describe('Chain', () => {
stubs.storage = {
cleanup: sinonSandbox.stub(),
};
stubs.bus = {
message: sinonSandbox.stub(),
registerModules: sinonSandbox.stub(),
};

stubs.modules = {
module1: {
cleanup: sinonSandbox.stub().resolves('module1cleanup'),
Expand Down Expand Up @@ -91,7 +86,6 @@ describe('Chain', () => {
stubs.createStorageComponent = sinonSandbox.stub().returns(stubs.storage);

stubs.initSteps = {
createBus: sinonSandbox.stub().resolves(stubs.bus),
bootstrapStorage: sinonSandbox.stub(),
bootstrapCache: sinonSandbox.stub(),
};
Expand All @@ -101,7 +95,6 @@ describe('Chain', () => {
Chain.__set__('createLoggerComponent', stubs.createLoggerComponent);
Chain.__set__('createCacheComponent', stubs.createCacheComponent);
Chain.__set__('createStorageComponent', stubs.createStorageComponent);
Chain.__set__('createBus', stubs.initSteps.createBus);
Chain.__set__('bootstrapStorage', stubs.initSteps.bootstrapStorage);
Chain.__set__('bootstrapCache', stubs.initSteps.bootstrapCache);

Expand Down Expand Up @@ -258,24 +251,13 @@ describe('Chain', () => {
chain.scope
);
});
it('should create bus object and assign to scope.bus', () => {
expect(stubs.initSteps.createBus).to.have.been.called;
return expect(chain.scope.bus).to.be.equal(stubs.bus);
});

it('should subscribe to "app:state:updated" event', () => {
return expect(chain.channel.subscribe).to.have.been.calledWith(
'app:state:updated'
);
});

it('should send bind message on the bus', () => {
return expect(chain.scope.bus.message).to.have.been.calledWith(
'bind',
chain.scope
);
});

it('should subscribe to "network:subscribe" event', () => {
return expect(chain.channel.subscribe).to.have.been.calledWith(
'network:event'
Expand Down
52 changes: 12 additions & 40 deletions framework/test/mocha/unit/modules/chain/submodules/rounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('rounds', () => {

const validScope = {
components: { logger, storage },
bus: { message: sinon.spy() },
channel: {
publish: sinonSandbox.stub(),
},
Expand Down Expand Up @@ -149,7 +148,6 @@ describe('rounds', () => {

expect(library.logger).to.eql(validScope.components.logger);
expect(library.storage).to.eql(validScope.components.storage);
expect(library.bus).to.eql(validScope.bus);
expect(library.channel).to.eql(validScope.channel);
});
});
Expand Down Expand Up @@ -188,31 +186,6 @@ describe('rounds', () => {
});
});

describe('onFinishRound', () => {
beforeEach(async () => {
validScope.channel.publish.resetHistory();
});

it('should call components.cache.removeByPattern once if cache is enabled', async () => {
const round = 123;
rounds.onFinishRound(round);

expect(validScope.channel.publish.called).to.be.true;
});

it('should call library.channel.publish once, with proper params', async () => {
const round = 124;
rounds.onFinishRound(round);

expect(validScope.channel.publish).to.be.calledWith(
'chain:rounds:change',
{
number: round,
}
);
});
});

describe('cleanup', () => {
it('should set __private.loaded = false and call a callback', done => {
const variable = '__private.loaded ';
Expand Down Expand Up @@ -544,11 +517,11 @@ describe('rounds', () => {
});

describe('scope.finishRound', () => {
let bus;
let channelPublish;

beforeEach(() => {
bus = get('library.bus.message');
return bus.resetHistory();
channelPublish = get('library.channel.publish');
return channelPublish.resetHistory();
});

describe('when true', () => {
Expand All @@ -562,7 +535,7 @@ describe('rounds', () => {
});
});

afterEach(() => bus.resetHistory());
afterEach(() => channelPublish.resetHistory());

it('scope.mergeBlockGenerator should be called once', async () =>
expect(mergeBlockGenerator_stub.calledOnce).to.be.true);
Expand All @@ -576,11 +549,11 @@ describe('rounds', () => {
it('scope.getOutsiders should be called once', async () =>
expect(getOutsiders_stub.calledOnce).to.be.true);

it('library.bus.message should be called once with proper params', async () => {
const busMessage = get('library.bus.message');
expect(busMessage.calledOnce).to.be.true;
return expect(busMessage.calledWith('finishRound', roundScope.round))
.to.be.true;
it('should call library.channel.publish once, with proper params', async () => {
expect(channelPublish.calledOnce).to.be.true;
expect(channelPublish).to.be.calledWith('chain:rounds:change', {
number: roundScope.round,
});
});
});

Expand All @@ -594,7 +567,7 @@ describe('rounds', () => {
});
});

after(() => bus.resetHistory());
after(() => channelPublish.resetHistory());

it('scope.mergeBlockGenerator should be called once', async () =>
expect(mergeBlockGenerator_stub.calledOnce).to.be.true);
Expand All @@ -608,9 +581,8 @@ describe('rounds', () => {
it('scope.getOutsiders should be not called', async () =>
expect(getOutsiders_stub.called).to.be.false);

it('library.bus.message should be not called', async () => {
const busMessage = get('library.bus.message');
return expect(busMessage.called).to.be.false;
it('library.channel.publish should be not called', async () => {
return expect(channelPublish.called).to.be.false;
});
});
});
Expand Down