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.0.0' into 561-rewrite-tx-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Beddows committed Nov 25, 2017
2 parents 3385128 + 02149fe commit 1b7478d
Show file tree
Hide file tree
Showing 92 changed files with 4,555 additions and 2,408 deletions.
2 changes: 2 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ lock(resource: "Lisk-Core-Nodes", inversePrecedence: true) {
"Build cached dependencies" : {
node('master-01'){
try {
deleteDir()
checkout scm
sh """
if [ ${params.JENKINS_PROFILE} = "jenkins-extensive" ]; then
rm -Rf "${env.WORKSPACE}/node_modules/"
Expand Down
68 changes: 68 additions & 0 deletions api/controllers/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

var _ = require('lodash');

// Private Fields
var modules;

/**
* Initializes with scope content and private variables:
* - modules
* @class AccountsController
* @classdesc Main System methods.
* @param {scope} scope - App instance.
*/
function AccountsController (scope) {
modules = scope.modules;
}

AccountsController.getAccounts = function (context, next) {
var params = context.request.swagger.params;

var filters = {
address: params.address.value,
publicKey: params.publicKey.value,
secondPublicKey: params.secondPublicKey.value,
username: params.username.value,
limit: params.limit.value,
offset: params.offset.value,
sort: params.sort.value
};

// Remove filters with null values
filters = _.pickBy(filters, function (v) {
return !(v === undefined || v === null);
});

modules.accounts.shared.getAccounts(_.clone(filters), function (err, data) {
if (err) { return next(err); }

data = _.cloneDeep(data);

data = _.map(data, function (account) {
if (_.isEmpty(account.delegate)) {
delete account.delegate;
} else {
account.delegate.rank = parseInt(account.delegate.rank);
account.delegate.missedBlocks = parseInt(account.delegate.missedBlocks);
account.delegate.producedBlocks = parseInt(account.delegate.producedBlocks);
}
if (_.isNull(account.secondPublicKey)) {
account.secondPublicKey = '';
}
delete account.secondSignature;
delete account.unconfirmedSignature;
return account;
});

next(null, {
data: data,
meta: {
offset: filters.offset,
limit: filters.limit
}
});
});
};

module.exports = AccountsController;
53 changes: 53 additions & 0 deletions api/controllers/dapps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var _ = require('lodash');

// Private Fields
var modules;

/**
* Initializes with scope content and private variables:
* - modules
* @class DappsController
* @classdesc Main System methods.
* @param {scope} scope - App instance.
*/
function DappsController (scope) {
modules = scope.modules;
}

DappsController.getDapps = function (context, next) {
var params = context.request.swagger.params;

var filters = {
transactionId: params.transactionId.value,
name: params.name.value,
sort: params.sort.value,
limit: params.limit.value,
offset: params.offset.value
};

// Remove filters with null values
filters = _.pickBy(filters, function (v) {
return !(v === undefined || v === null);
});

modules.dapps.shared.getDapps(_.clone(filters), function (err, data) {
try {
if (err) { return next(err); }

next(null, {
data: data,
meta: {
offset: filters.offset,
limit: filters.limit
}
});

} catch (error) {
next(error);
}
});
};

module.exports = DappsController;
2 changes: 1 addition & 1 deletion api/controllers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PeersController.getPeers = function (context, next) {
height: params.height.value,
limit: params.limit.value,
offset: params.offset.value,
orderBy: params.sort.value
sort: params.sort.value
};

// Remove filters with null values
Expand Down
31 changes: 0 additions & 31 deletions api/http/accounts.js

This file was deleted.

29 changes: 0 additions & 29 deletions api/http/dapps.js

This file was deleted.

42 changes: 10 additions & 32 deletions api/http/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,35 @@ var httpApi = require('../../helpers/httpApi');
* - End point: `/api/delegates`
* - Public API:
* - get /
* - get /count
* - get /search
* - get /voters
* - get /get
* - get /fee
* - get /forging/getForgedByAccount
* - get /getNextForgers
* - get /forgers
* - Private API:
* - post /forging/enable
* - post /forging/disable
* - get /forging/status
* - Debug API:
* - get /forging/disableAll
* - get /forging/enableAll
* - put /forging
* - get /forging
* @memberof module:delegates
* @requires helpers/Router
* @requires helpers/httpApi
* @constructor
* @param {Object} delegatesModule - Module delegate instance.
* @param {scope} app - Network app.
* @param {Logger} logger - Application logger.
* @param {Cache} cache - Cache module.
*/
// Constructor
function DelegatesHttpApi (delegatesModule, app, logger, cache) {

var router = new Router();

// attach a middlware to endpoints
// Attach a middleware to endpoints
router.attachMiddlwareForUrls(httpApi.middleware.useCache.bind(null, logger, cache), ['get /']);

router.map(delegatesModule.shared, {
'get /': 'getDelegates',
'get /count': 'count',
'get /search': 'search',
'get /voters': 'getVoters',
'get /get': 'getDelegate',
'get /fee': 'getFee',
'get /forging/getForgedByAccount': 'getForgedByAccount',
'get /getNextForgers': 'getNextForgers'
});
'get /forgers': 'getForgers'
}, {responseWithCode: true});

router.map(delegatesModule.internal, {
'put /forging': 'forgingToggle',
'get /forging/status': 'forgingStatus'
});

if (process.env.DEBUG) {
router.map(delegatesModule.internal, {
'get /forging/disableAll': 'forgingDisableAll',
'get /forging/enableAll': 'forgingEnableAll'
});
}
'get /forging': 'forgingStatus'
}, {responseWithCode: true});

httpApi.registerEndpoint('/api/delegates', app, router, delegatesModule.isLoaded);
}
Expand Down
2 changes: 0 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ var config = {
voters: './modules/voters'
},
api: {
accounts: { http: './api/http/accounts.js' },
blocks: { http: './api/http/blocks.js' },
dapps: { http: './api/http/dapps.js' },
delegates: { http: './api/http/delegates.js' },
multisignatures: { http: './api/http/multisignatures.js' },
signatures: { http: './api/http/signatures.js' },
Expand Down
1 change: 1 addition & 0 deletions helpers/apiCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
NO_CONTENT: 204,
INTERNAL_SERVER_ERROR: 500,
BAD_REQUEST: 400,
FORBIDDEN: 403,
TOO_MANY_REQUESTS: 429
};
5 changes: 3 additions & 2 deletions helpers/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ module.exports.connect = function (cacheEnabled, config, logger, cb) {

client.on('error', function (err) {
logger.error('Redis:', err);
// Only throw an error if cache was enabled in config but were unable to load it properly
// Returns redis client so application can continue to try to connect with the redis server,
// and modules/cache can have client reference once it's connected
if (!isRedisLoaded) {
isRedisLoaded = true;
return cb(null, { cacheEnabled: cacheEnabled, client: null });
return cb(null, { cacheEnabled: cacheEnabled, client: client });
}
});
};
4 changes: 2 additions & 2 deletions helpers/httpApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ var middleware = {
res.json = function (response) {
// ToDo: Remove response.success check when API refactor is done (#225)
if (response.success || (response.success === undefined && res.statusCode === apiCodes.OK)) {
logger.debug('cached response for key: ', req.url);
logger.debug('Cache - Response for key:', req.url);
cache.setJsonForKey(key, response);
}
expressSendJson.call(res, response);
};
next();
} else {
logger.debug(['serving response for url:', req.url, 'from cache'].join(' '));
logger.debug('Cache - Response for url:', req.url);
res.json(cachedValue);
}
});
Expand Down
29 changes: 18 additions & 11 deletions helpers/orderBy.js → helpers/sort_by.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
* Validates sort options, methods and fields.
* @memberof module:helpers
* @function
* @param {Array} orderBy
* @param {string} options
* @param {Array} sort
* @param {Object} options
* @param {string} options.fieldPrefix
* @param {string} options.sortField
* @param {string} options.sortMethod - asc / desc
* @param {Array} options.sortFields
* @return {Object} error | {sortField, sortMethod}.
*/
function OrderBy (orderBy, options) {
function sortBy (sort, options) {
options = (typeof options === 'object') ? options : {};
options.sortField = options.sortField || null;
options.sortMethod = options.sortMethod || null;
Expand All @@ -22,12 +26,12 @@ function OrderBy (orderBy, options) {

var sortField, sortMethod;

if (orderBy) {
var sort = String(orderBy).split(':');
sortField = sort[0].replace(/[^\w\s]/gi, '');
if (sort) {
var sortBy = String(sort).split(':');
sortField = sortBy[0].replace(/[^\w\s]/gi, '');

if (sort.length === 2) {
sortMethod = sort[1] === 'desc' ? 'DESC' : 'ASC';
if (sortBy.length === 2) {
sortMethod = sortBy[1] === 'desc' ? 'DESC' : 'ASC';
}
}

Expand Down Expand Up @@ -78,7 +82,7 @@ function OrderBy (orderBy, options) {
}

/**
* Converts orderBy queries from string format like "field:asc"
* Converts sortBy queries from string format like "field:asc"
* to format accepted by "json-sql" library: {field: 1}.
* Ascending sort method number equivalent is 1.
* Descending sort method number equivalent is -1.
Expand All @@ -87,7 +91,7 @@ function OrderBy (orderBy, options) {
* @param {Array} sortableFields
* @returns {Object}[={}] returns {} if incorrect format of sortQuery given or if field
*/
OrderBy.sortQueryToJsonSqlFormat = function (sortQuery, sortableFields) {
function sortQueryToJsonSqlFormat (sortQuery, sortableFields) {
if (sortableFields.indexOf(sortQuery) !== -1) {
sortQuery = sortQuery + ':asc';
}
Expand All @@ -106,4 +110,7 @@ OrderBy.sortQueryToJsonSqlFormat = function (sortQuery, sortableFields) {
return result;
};

module.exports = OrderBy;
module.exports = {
sortQueryToJsonSqlFormat: sortQueryToJsonSqlFormat,
sortBy: sortBy
};
Loading

0 comments on commit 1b7478d

Please sign in to comment.