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

Commit

Permalink
Merge branch '1.5.0_development_merge_branch' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejBaj committed Feb 14, 2019
2 parents 5db36a5 + 9c3dac7 commit 8d5c787
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 1,156 deletions.
240 changes: 153 additions & 87 deletions framework/src/modules/chain/api/controllers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@

'use strict';

const { promisify } = require('util');
const _ = require('lodash');
const checkIpInList = require('../../helpers/check_ip_in_list.js');
const apiCodes = require('../../helpers/api_codes');
const swaggerHelper = require('../../helpers/swagger');
const BlockReward = require('../../logic/block_reward.js');
const slots = require('../../helpers/slots.js');

const { EPOCH_TIME, FEES } = global.constants;

// Private Fields
let modules;
let config;
let library;
let blockReward;

// Promised functions
let getNetworkHeight;
let getTransactionsCount;
let updateForgingStatus;

/**
* Description of the function.
Expand All @@ -38,8 +48,17 @@ let config;
* @todo Add description of NodeController
*/
function NodeController(scope) {
modules = scope.modules;
config = scope.config;
library = {
modules: scope.modules,
storage: scope.storage,
config: scope.config,
build: scope.build,
lastCommit: scope.lastCommit,
};
blockReward = new BlockReward();
getNetworkHeight = promisify(library.modules.peers.networkHeight);
getTransactionsCount = promisify(library.modules.transactions.shared.getTransactionsCount);
updateForgingStatus = promisify(library.modules.delegates.updateForgingStatus);
}

/**
Expand All @@ -49,34 +68,39 @@ function NodeController(scope) {
* @param {function} next
* @todo Add description for the function and the params
*/
NodeController.getConstants = function(context, next) {
return modules.node.shared.getConstants(null, (err, data) => {
try {
if (err) {
return next(err);
}

data = _.cloneDeep(data);

// Perform required typecasts for integer
// or bignum properties when returning an API response
data.supply = data.supply.toString();
data.milestone = data.milestone.toString();
data.reward = data.reward.toString();
data.fees.dappDeposit = data.fees.dappDeposit.toString();
data.fees.dappWithdrawal = data.fees.dappWithdrawal.toString();
data.fees.dappRegistration = data.fees.dappRegistration.toString();
data.fees.multisignature = data.fees.multisignature.toString();
data.fees.delegate = data.fees.delegate.toString();
data.fees.secondSignature = data.fees.secondSignature.toString();
data.fees.vote = data.fees.vote.toString();
data.fees.send = data.fees.send.toString();

return next(null, data);
} catch (error) {
return next(error);
}
});
NodeController.getConstants = async (context, next) => {
try {
const [lastBlock] = await library.storage.entities.Block.get(
{},
{ sort: 'height:desc', limit: 1 }
);
const { height } = lastBlock;

return next(null, {
build: library.build,
commit: library.lastCommit,
epoch: EPOCH_TIME,
fees: {
send: FEES.SEND.toString(),
vote: FEES.VOTE.toString(),
secondSignature: FEES.SECOND_SIGNATURE.toString(),
delegate: FEES.DELEGATE.toString(),
multisignature: FEES.MULTISIGNATURE.toString(),
dappRegistration: FEES.DAPP_REGISTRATION.toString(),
dappWithdrawal: FEES.DAPP_WITHDRAWAL.toString(),
dappDeposit: FEES.DAPP_DEPOSIT.toString(),
},
nethash: library.config.nethash,
nonce: library.config.nonce,
milestone: blockReward.calcMilestone(height).toString(),
reward: blockReward.calcReward(height).toString(),
supply: blockReward.calcSupply(height).toString(),
version: library.config.version,
protocolVersion: library.config.protocolVersion,
});
} catch (error) {
return next(error);
}
};

/**
Expand All @@ -86,35 +110,35 @@ NodeController.getConstants = function(context, next) {
* @param {function} next
* @todo Add description for the function and the params
*/
NodeController.getStatus = function(context, next) {
return modules.node.shared.getStatus(null, (getStatusErr, data) => {
try {
if (getStatusErr) {
return next(getStatusErr);
}

data = _.cloneDeep(data);

// Check if properties are null, then set it to 0
// as per schema defined for these properties in swagger
data.networkHeight = data.networkHeight || 0;
data.consensus = data.consensus || 0;

return modules.transactions.shared.getTransactionsCount(
(getTransactionsCountErr, count) => {
if (getTransactionsCountErr) {
return next(getTransactionsCountErr);
}

data.transactions = count;

return next(null, data);
}
);
} catch (error) {
return next(error);
}
});
NodeController.getStatus = async (context, next) => {
try {
const networkHeight = await getNetworkHeight({
normalized: false,
});

const [lastBlock] = await library.storage.entities.Block.get(
{},
{ sort: 'height:desc', limit: 1 }
);
const { height } = lastBlock;

// TODO: Replace all library.modules calls after chain module extraction is done as part of https://github.com/LiskHQ/lisk/issues/2763.
const data = {
broadhash: library.modules.system.getBroadhash(),
consensus: library.modules.peers.getLastConsensus() || 0,
currentTime: Date.now(),
secondsSinceEpoch: slots.getTime(),
height,
loaded: library.modules.loader.loaded(),
networkHeight: networkHeight || 0,
syncing: library.modules.loader.syncing(),
};

data.transactions = await getTransactionsCount();
return next(null, data);
} catch (err) {
return next(err);
}
};

/**
Expand All @@ -124,21 +148,19 @@ NodeController.getStatus = function(context, next) {
* @param {function} next
* @todo Add description for the function and the params
*/
NodeController.getForgingStatus = function(context, next) {
if (!checkIpInList(config.forging.access.whiteList, context.request.ip)) {
NodeController.getForgingStatus = async (context, next) => {
if (!checkIpInList(library.config.forging.access.whiteList, context.request.ip)) {
context.statusCode = apiCodes.FORBIDDEN;
return next(new Error('Access Denied'));
}

const publicKey = context.request.swagger.params.publicKey.value;

return modules.node.internal.getForgingStatus(publicKey, (err, data) => {
if (err) {
return next(err);
}

return next(null, data);
});
try {
const forgingStatus = await _getForgingStatus(publicKey);
return next(null, forgingStatus);
} catch (err) {
return next(err);
}
};

/**
Expand All @@ -148,8 +170,8 @@ NodeController.getForgingStatus = function(context, next) {
* @param {function} next
* @todo Add description for the function and the params
*/
NodeController.updateForgingStatus = function(context, next) {
if (!checkIpInList(config.forging.access.whiteList, context.request.ip)) {
NodeController.updateForgingStatus = async (context, next) => {
if (!checkIpInList(library.config.forging.access.whiteList, context.request.ip)) {
context.statusCode = apiCodes.FORBIDDEN;
return next(new Error('Access Denied'));
}
Expand All @@ -158,19 +180,13 @@ NodeController.updateForgingStatus = function(context, next) {
const password = context.request.swagger.params.data.value.password;
const forging = context.request.swagger.params.data.value.forging;

return modules.node.internal.updateForgingStatus(
publicKey,
password,
forging,
(err, data) => {
if (err) {
context.statusCode = apiCodes.NOT_FOUND;
return next(err);
}

return next(null, [data]);
}
);
try {
const data = await _updateForgingStatus(publicKey, password, forging);
return next(null, [data]);
} catch (err) {
context.statusCode = apiCodes.NOT_FOUND;
return next(err);
}
};

/**
Expand Down Expand Up @@ -212,7 +228,7 @@ NodeController.getPooledTransactions = function(context, next) {
// Remove filters with null values
filters = _.pickBy(filters, v => !(v === undefined || v === null));

return modules.transactions.shared[stateMap[state]].call(
return library.modules.transactions.shared[stateMap[state]].call(
this,
_.clone(filters),
(err, data) => {
Expand Down Expand Up @@ -246,4 +262,54 @@ NodeController.getPooledTransactions = function(context, next) {
);
};

/**
* Get the forging status of a delegate.
*
* @param {string} publicKey - Public key of delegate
* @returns {Promise<object>}
* @private
*/
async function _getForgingStatus(publicKey) {
const keyPairs = library.modules.delegates.getForgersKeyPairs();
const forgingDelegates = library.config.forging.delegates;
const forgersPublicKeys = {};

Object.keys(keyPairs).forEach(key => {
forgersPublicKeys[keyPairs[key].publicKey.toString('hex')] = true;
});

const fullList = forgingDelegates.map(forger => ({
forging: !!forgersPublicKeys[forger.publicKey],
publicKey: forger.publicKey,
}));

if (publicKey && !_.find(fullList, { publicKey })) {
return [];
}

if (_.find(fullList, { publicKey })) {
return [
{
publicKey,
forging: !!forgersPublicKeys[publicKey],
},
];
}

return fullList;
}

/**
* Toggle the forging status of a delegate.
* @param {string} publicKey - Public key of a delegate
* @param {string} password - Password used to decrypt encrypted passphrase
* @param {boolean} forging - Forging status of a delegate to update
* @returns {Promise<object>}
* @todo Add description for the return value
* @private
*/
async function _updateForgingStatus(publicKey, password, forging) {
return updateForgingStatus(publicKey, password, forging);
}

module.exports = NodeController;
1 change: 0 additions & 1 deletion framework/src/modules/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const config = {
rounds: './modules/rounds.js',
loader: './modules/loader.js',
multisignatures: './modules/multisignatures.js',
node: './modules/node.js',
peers: './modules/peers.js',
system: './modules/system.js',
signatures: './modules/signatures.js',
Expand Down
4 changes: 0 additions & 4 deletions framework/src/modules/chain/helpers/newrelic_lisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ const modulesToInstrument = {
identifier: 'helpers.sequence',
callbackMethods: ['add'],
},
'./modules/node.js': {
identifier: 'modules.node',
callbackMethods: ['shared.getStatus', 'shared.getConstants'],
},
'./modules/blocks.js': {
identifier: 'modules.blocks',
callbackMethods: ['shared.getBlocks'],
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/chain/modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ __private.createSnapshot = height => {

const snapshotRound = library.config.loading.snapshotRound;
const totalRounds = Math.floor(height / ACTIVE_DELEGATES);
const targetRound = Number.isNaN(snapshotRound)
const targetRound = Number.isNaN(parseInt(snapshotRound))
? totalRounds
: Math.min(totalRounds, snapshotRound);
const targetHeight = targetRound * ACTIVE_DELEGATES;
Expand Down
Loading

0 comments on commit 8d5c787

Please sign in to comment.