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

Commit

Permalink
Merge pull request #3658 from LiskHQ/3615-combine-all-the-rounds-rela…
Browse files Browse the repository at this point in the history
…ted-logic-into-one-module

Combine all the rounds related logic into one module and expose used functionality - Closes #3615
  • Loading branch information
shuse2 authored May 22, 2019
2 parents 9f60bbe + 8d47bed commit 008d6d2
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 173 deletions.
8 changes: 4 additions & 4 deletions framework/src/modules/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ module.exports = class Chain {
// TODO: Global variable forbits to require on top
const Loader = require('./loader');
const { Forger } = require('./forger');
const { Delegates } = require('./submodules/delegates');
const Transport = require('./transport');
const { Rounds } = require('./rounds');
this.loader = new Loader(this.scope);
this.forger = new Forger(this.scope);
this.transport = new Transport(this.scope);
// TODO: should not add to scope
this.scope.modules.delegates = new Delegates(this.scope);
this.rounds = new Rounds(this.scope);
this.scope.modules.loader = this.loader;
this.scope.modules.forger = this.forger;
this.scope.modules.transport = this.transport;
this.scope.modules.rounds = this.rounds;

this.scope.logic.block.bindModules(this.scope.modules);
this.scope.logic.account.bindModules(this.scope.modules);
Expand Down Expand Up @@ -224,7 +224,7 @@ module.exports = class Chain {
calculateReward: action =>
this.blockReward.calcReward(action.params.height),
generateDelegateList: async action =>
this.scope.modules.delegates.generateDelegateList(
this.scope.modules.rounds.generateDelegateList(
action.params.round,
action.params.source
),
Expand Down
9 changes: 4 additions & 5 deletions framework/src/modules/chain/forger.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const { ACTIVE_DELEGATES } = global.constants;
* @todo Add description for the params
*/
const getDelegateKeypairForCurrentSlot = async (
delegates,
rounds,
keypairs,
currentSlot,
round
) => {
const activeDelegates = await delegates.generateDelegateList(round);
const activeDelegates = await rounds.generateDelegateList(round);

const currentSlotIndex = currentSlot % ACTIVE_DELEGATES;
const currentSlotDelegate = activeDelegates[currentSlotIndex];
Expand Down Expand Up @@ -307,9 +307,8 @@ class Forger {

// We calculate round using height + 1, because we want the delegate keypair for next block to be forged
const round = slots.calcRound(lastBlock.height + 1);

return getDelegateKeypairForCurrentSlot(
modules.delegates,
modules.rounds,
this.keypairs,
currentSlot,
round
Expand Down Expand Up @@ -414,7 +413,7 @@ class Forger {
blocks: scope.modules.blocks,
peers: scope.modules.peers,
transactions: scope.modules.transactions,
delegates: scope.modules.delegates,
rounds: scope.modules.rounds,
};
}
}
Expand Down
1 change: 0 additions & 1 deletion framework/src/modules/chain/init_steps/init_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const domain = require('domain');

const modulesList = {
blocks: '../submodules/blocks',
rounds: '../submodules/rounds',
multisignatures: '../submodules/multisignatures',
peers: '../submodules/peers',
transactions: '../submodules/transactions',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ const validateBlockSlot = (block, activeDelegates) => {
class Delegates {
constructor(scope) {
this.delegatesListCache = {};
this.logger = scope.components.logger;
this.storage = scope.components.storage;
this.logger = scope.logger;
this.storage = scope.storage;
this.channel = scope.channel;
}

Expand Down
21 changes: 21 additions & 0 deletions framework/src/modules/chain/rounds/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

'use strict';

const Rounds = require('./rounds.js');

module.exports = {
Rounds,
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ const Bignumber = require('bignumber.js');
const async = require('async');
const cryptography = require('@liskhq/lisk-cryptography');

const Round = require('../logic/round');
const { Delegates } = require('./delegates');
const Round = require('./round');
const slots = require('../helpers/slots');

// Private fields
let modules;
let library;
let self;
const { ACTIVE_DELEGATES } = global.constants;
const __private = {};

Expand All @@ -46,17 +45,15 @@ __private.ticking = false;
* @todo Apply node pattern for callbacks: callback always at the end
*/
class Rounds {
constructor(cb, scope) {
constructor(scope) {
library = {
channel: scope.channel,
logic: scope.logic,
logger: scope.components.logger,
logic: scope.logic,
bus: scope.bus,
storage: scope.components.storage,
};
self = this;

setImmediate(cb, null, self);
library.delegates = new Delegates(library);
}

/**
Expand Down Expand Up @@ -164,7 +161,7 @@ class Rounds {
* delegate list.
* */
if (scope.finishRound) {
modules.delegates.clearDelegateListCache();
library.delegates.clearDelegateListCache();
}

return done();
Expand Down Expand Up @@ -348,17 +345,16 @@ class Rounds {
);
}

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

/**
Expand All @@ -371,18 +367,6 @@ class Rounds {
__private.loaded = true;
}

/**
* 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 false.
*
Expand All @@ -394,6 +378,28 @@ class Rounds {
cleanup() {
__private.loaded = false;
}

// Delegates Proxy

// eslint-disable-next-line class-methods-use-this
validateBlockSlot(block) {
return library.delegates.validateBlockSlot(block);
}

// eslint-disable-next-line class-methods-use-this
generateDelegateList(round, source, tx) {
return library.delegates.generateDelegateList(round, source, tx);
}

// eslint-disable-next-line class-methods-use-this
fork(block, cause) {
return library.delegates.fork(block, cause);
}

// eslint-disable-next-line class-methods-use-this
validateBlockSlotAgainstPreviousRound(block) {
return library.delegates.validateBlockSlotAgainstPreviousRound(block);
}
}

// Private methods
Expand All @@ -414,7 +420,7 @@ __private.getOutsiders = function(scope, cb, tx) {
if (scope.block.height === 1) {
return setImmediate(cb);
}
return modules.delegates
return library.delegates
.generateDelegateList(scope.round, null, tx)
.then(roundDelegates =>
async.eachSeries(
Expand Down
10 changes: 5 additions & 5 deletions framework/src/modules/chain/submodules/blocks/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class Process {
library.logger.trace('Blocks->Process: Shared modules bind.');
modules = {
blocks: scope.modules.blocks,
delegates: scope.modules.delegates,
rounds: scope.modules.rounds,
transactions: scope.modules.transactions,
processTransactions: scope.modules.processTransactions,
};
Expand Down Expand Up @@ -462,7 +462,7 @@ __private.receiveForkOne = function(block, lastBlock, cb) {
let tmpBlock = _.clone(block);

// Fork: Consecutive height but different previous block id
modules.delegates.fork(block, 1);
modules.rounds.fork(block, 1);

// Keep the oldest block, or if both have same age, keep block with lower id
if (
Expand Down Expand Up @@ -528,7 +528,7 @@ __private.receiveForkOne = function(block, lastBlock, cb) {
__private.receiveForkFive = function(block, lastBlock, cb) {
let tmpBlock = _.clone(block);
// Fork: Same height and previous block id, but different block id
modules.delegates.fork(block, 5);
modules.rounds.fork(block, 5);

// Check if delegate forged on more than one node
if (block.generatorPublicKey === lastBlock.generatorPublicKey) {
Expand Down Expand Up @@ -615,14 +615,14 @@ __private.validateBlockSlot = function(block, lastBlock, cb) {
) {
// Check if block was generated by the right active delagate from previous round.
// DATABASE: Read only to mem_accounts to extract active delegate list
modules.delegates
modules.rounds
.validateBlockSlotAgainstPreviousRound(block)
.then(() => setImmediate(cb))
.catch(err => setImmediate(cb, err));
} else {
// Check if block was generated by the right active delagate.
// DATABASE: Read only to mem_accounts to extract active delegate list
modules.delegates
modules.rounds
.validateBlockSlot(block)
.then(() => setImmediate(cb))
.catch(err => setImmediate(cb, err));
Expand Down
8 changes: 4 additions & 4 deletions framework/src/modules/chain/submodules/blocks/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class Verify {

modules = {
blocks: scope.modules.blocks,
delegates: scope.modules.delegates,
rounds: scope.modules.rounds,
transactions: scope.modules.transactions,
processTransactions: scope.modules.processTransactions,
};
Expand Down Expand Up @@ -663,7 +663,7 @@ __private.verifyPayload = function(block, result) {
*/
__private.verifyForkOne = function(block, lastBlock, result) {
if (block.previousBlock && block.previousBlock !== lastBlock.id) {
modules.delegates.fork(block, 1);
modules.rounds.fork(block, 1);
result.errors.push(
`Invalid previous block: ${block.previousBlock} expected: ${lastBlock.id}`
);
Expand Down Expand Up @@ -859,11 +859,11 @@ __private.checkExists = function(block, cb) {
__private.validateBlockSlot = function(block, cb) {
// Check if block was generated by the right active delagate. Otherwise, fork 3
// DATABASE: Read only to mem_accounts to extract active delegate list
modules.delegates
modules.rounds
.validateBlockSlot(block)
.then(() => setImmediate(cb))
.catch(err => {
modules.delegates.fork(block, 3);
modules.rounds.fork(block, 3);
setImmediate(cb, err);
});
};
Expand Down
10 changes: 4 additions & 6 deletions framework/test/mocha/common/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const modulesInit = {
blocks: '../../../src/modules/chain/submodules/blocks',
multisignatures: '../../../src/modules/chain/submodules/multisignatures',
peers: '../../../src/modules/chain/submodules/peers',
rounds: '../../../src/modules/chain/submodules/rounds',
transactions: '../../../src/modules/chain/submodules/transactions',
processTransactions:
'../../../src/modules/chain/submodules/process_transactions.js',
Expand Down Expand Up @@ -263,11 +262,10 @@ const initStepsForTest = {
const RewiredTransport = rewire('../../../src/modules/chain/transport');
scope.rewiredModules.transport = RewiredTransport;
modules.transport = new RewiredTransport(scope);
const { Delegates: RewiredDelegates } = rewire(
'../../../src/modules/chain/submodules/delegates'
);
scope.rewiredModules.delegates = RewiredDelegates;
modules.delegates = new RewiredDelegates(scope);
const {
Rounds: RewiredRounds,
} = require('../../../src/modules/chain/rounds');
modules.rounds = new RewiredRounds(scope);

scope.bus.registerModules(modules);

Expand Down
4 changes: 2 additions & 2 deletions framework/test/mocha/common/modules_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ const modulesLoader = new function() {
[
{ blocks: require('../../../src/modules/chain/submodules/blocks') },
{
delegates: require('../../../src/modules/chain/submodules/delegates'),
delegates: require('../../../src/modules/chain/rounds/delegates'),
},
{ loader: require('../../../src/modules/chain/loader') },
{
multisignatures: require('../../../src/modules/chain/submodules/multisignatures'),
},
{ peers: require('../../../src/modules/chain/submodules/peers') },
{ rounds: require('../../../src/modules/chain/submodules/rounds') },
{ rounds: require('../../../src/modules/chain/rounds/rounds') },
{
transactions: require('../../../src/modules/chain/submodules/transactions'),
},
Expand Down
2 changes: 1 addition & 1 deletion framework/test/mocha/integration/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ describe('app', () => {
let delegatesList;

before(() => {
return library.modules.delegates
return library.modules.rounds
.generateDelegateList(1, null)
.then(_delegatesList => {
delegatesList = _delegatesList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const genesisDelegates = require('../../../data/genesis_delegates.json')
const application = require('../../../common/application');
const {
getKeysSortByVote,
} = require('../../../../../src/modules/chain/submodules/delegates');
} = require('../../../../../src/modules/chain/rounds/delegates');

const { ACTIVE_DELEGATES, BLOCK_SLOT_WINDOW } = global.constants;

Expand Down Expand Up @@ -90,7 +90,7 @@ describe('integration test (blocks) - process receiveBlockFromNetwork()', () =>
function getNextForger(offset, seriesCb) {
offset = !offset ? 0 : offset;
const round = slots.calcRound(last_block.height + 1);
library.modules.delegates
library.modules.rounds
.generateDelegateList(round, getKeysSortByVote)
.then(delegateList => {
const nextForger = delegateList[(slot + offset) % ACTIVE_DELEGATES];
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('integration test (blocks) - process receiveBlockFromNetwork()', () =>
const lastBlock = library.modules.blocks.lastBlock.get();
const round = slots.calcRound(lastBlock.height);

return library.modules.delegates
return library.modules.rounds
.generateDelegateList(round, null)
.then(list => {
const delegatePublicKey = list[slot % ACTIVE_DELEGATES];
Expand Down
Loading

0 comments on commit 008d6d2

Please sign in to comment.