From daad3f696fe1050b1e926c75c28883b5ca13eff8 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:55:26 +0100 Subject: [PATCH 01/18] Add swagger definitions into app scope --- app.js | 8 ++++++-- config/swagger.js | 32 ++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 3ccad3fe405..2bc6cfde2c7 100644 --- a/app.js +++ b/app.js @@ -42,6 +42,7 @@ var httpApi = require('./helpers/httpApi.js'); var Sequence = require('./helpers/sequence.js'); var z_schema = require('./helpers/z_schema.js'); var swagger = require('./config/swagger'); +var swaggerHelper = require('./helpers/swagger'); process.stdin.resume(); @@ -172,7 +173,7 @@ d.run(function () { }, schema: function (cb) { - cb(null, new z_schema()); + cb(null, swaggerHelper.getValidator()); }, /** @@ -528,7 +529,10 @@ d.run(function () { cb(); }], - ready: ['modules', 'bus', 'logic', function (scope, cb) { + ready: ['swagger', 'modules', 'bus', 'logic', function (scope, cb) { + + scope.modules.swagger = scope.swagger; + // Fire onBind event in every module scope.bus.message('bind', scope.modules); diff --git a/config/swagger.js b/config/swagger.js index d6a708738d7..07b64bd642d 100644 --- a/config/swagger.js +++ b/config/swagger.js @@ -4,6 +4,7 @@ var SwaggerRunner = require('swagger-node-runner'); var path = require('path'); var fs = require('fs'); var YAML = require('js-yaml'); +var _ = require('lodash'); // Its necessary to require this file to extend swagger validator with our custom formats var validator = require('../helpers/swagger').getValidator(); @@ -41,22 +42,33 @@ function bootstrapSwagger (app, config, logger, scope, cb) { swagger: swagger, enforceUniqueOperationId: true, startWithErrors: false, - startWithWarnings: false + startWithWarnings: true }; - SwaggerRunner.create(swaggerConfig, function (err, runner) { - if (err) { + // Swagger Express Middleware + SwaggerRunner.create(swaggerConfig, function (errors, runner) { + if (errors) { + + // Ignore unused definition warning + errors.validationWarnings = _.filter(errors.validationWarnings, function (error) { + return error.code !== 'UNUSED_DEFINITION'; + }); + // Some error occurred in configuring the swagger - if (err.validationErrors) { + if (!_.isEmpty(errors.validationErrors)) { logger.error('Swagger Validation Errors:'); - logger.error(err.validationErrors); + logger.error(errors.validationErrors); } - if (err.validationWarnings) { + + if (!_.isEmpty(errors.validationWarnings)) { logger.error('Swagger Validation Warnings:'); - logger.error(err.validationWarnings); + logger.error(errors.validationWarnings); + } + + if (!_.isEmpty(errors.validationErrors) || !_.isEmpty(errors.validationWarnings) ) { + cb(errors); + return; } - cb(err); - return; } // Swagger Express Middleware @@ -78,7 +90,7 @@ function bootstrapSwagger (app, config, logger, scope, cb) { app.swaggerRunner = runner; // Successfully mounted the swagger runner - cb(null, {swaggerRunner: runner}); + cb(null, {swaggerRunner: runner, definitions: runner.swagger.definitions}); }); } From 1aa6b27271baa92b15e50aa896b0442406d88043 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:56:42 +0100 Subject: [PATCH 02/18] Migrate schema/peers to swagger definitions --- modules/peers.js | 10 ++++---- schema/peers.js | 57 ---------------------------------------------- schema/swagger.yml | 11 +++++++++ 3 files changed, 16 insertions(+), 62 deletions(-) delete mode 100644 schema/peers.js diff --git a/modules/peers.js b/modules/peers.js index 7101f59ef79..33db67e7fa5 100644 --- a/modules/peers.js +++ b/modules/peers.js @@ -14,12 +14,11 @@ var ApiError = require('../helpers/apiError.js'); var constants = require('../helpers/constants.js'); var failureCodes = require('../api/ws/rpc/failureCodes.js'); var jobsQueue = require('../helpers/jobsQueue.js'); -var schema = require('../schema/peers.js'); var Peer = require('../logic/peer.js'); var sql = require('../sql/peers.js'); // Private fields -var modules, library, self, __private = {}; +var modules, library, self, __private = {}, definitions; /** * Initializes library with scope content. @@ -379,7 +378,7 @@ Peers.prototype.discover = function (cb) { } function validatePeersList (result, waterCb) { - library.schema.validate(result, schema.discover.peers, function (err) { + library.schema.validate(result, definitions.PeersList, function (err) { return setImmediate(waterCb, err, result.peers); }); } @@ -393,7 +392,7 @@ Peers.prototype.discover = function (cb) { function updatePeers (peers, waterCb) { async.each(peers, function (peer, eachCb) { peer = library.logic.peers.create(peer); - library.schema.validate(peer, schema.discover.peer, function (err) { + library.schema.validate(peer, definitions.Peer, function (err) { if (err) { library.logger.warn(['Rejecting invalid peer:', peer.string].join(' '), {err: err}); return setImmediate(eachCb); @@ -519,6 +518,7 @@ Peers.prototype.onBind = function (scope) { modules = { system: scope.system }; + definitions = scope.swagger.definitions; }; /** @@ -597,7 +597,7 @@ Peers.prototype.onPeersReady = function () { }); } // Loop in 10sec intervals (5sec + 5sec connect timeout from pingPeer) - jobsQueue.register('peersDiscoveryAndUpdate', peersDiscoveryAndUpdate, 60000); + jobsQueue.register('peersDiscoveryAndUpdate', peersDiscoveryAndUpdate, 5000); }; /** diff --git a/schema/peers.js b/schema/peers.js deleted file mode 100644 index 9ffedc1f1a9..00000000000 --- a/schema/peers.js +++ /dev/null @@ -1,57 +0,0 @@ -'use strict'; - -module.exports = { - discover: { - peers: { - id: 'peers.discover.peers', - type: 'object', - properties: { - peers: { - type: 'array' - } - }, - required: ['peers'] - }, - peer: { - id: 'peers.discover.peer', - type: 'object', - properties: { - ip: { - type: 'string', - format: 'ip' - }, - wsPort: { - type: 'integer', - minimum: 1, - maximum: 65535 - }, - state: { - type: 'integer', - minimum: 0, - maximum: 2 - }, - os: { - type: 'string', - format: 'os', - minLength: 1, - maxLength: 64 - }, - version: { - type: 'string', - format: 'version', - minLength: 5, - maxLength: 12 - }, - broadhash: { - type: 'string', - format: 'hex' - }, - height: { - type: 'integer', - minimum: 1 - } - }, - required: ['ip', 'wsPort'] - } - } -}; diff --git a/schema/swagger.yml b/schema/swagger.yml index c1a9fefee7a..50a812e986d 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -1770,6 +1770,17 @@ definitions: example: sYHEDBKcScaAAAYg minLength: 1 + PeersList: + x-private: true + type: object + required: + - peers + properties: + peers: + type: array + items: + $ref: '#/definitions/Peer' + Fees: type: object required: From 68c4b524e9cb0a33d0a8f0c70a8354462e5b0c8e Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:57:32 +0100 Subject: [PATCH 03/18] Migrate schema/transactions to swagger definitions --- modules/transactions.js | 37 ------------------------------------- schema/transactions.js | 20 -------------------- 2 files changed, 57 deletions(-) delete mode 100644 schema/transactions.js diff --git a/modules/transactions.js b/modules/transactions.js index 108b9646cae..7eb5bbc6d00 100644 --- a/modules/transactions.js +++ b/modules/transactions.js @@ -8,7 +8,6 @@ var extend = require('extend'); var apiCodes = require('../helpers/apiCodes.js'); var ApiError = require('../helpers/apiError.js'); var sortBy = require('../helpers/sort_by.js').sortBy; -var schema = require('../schema/transactions.js'); var sql = require('../sql/transactions.js'); var TransactionPool = require('../logic/transactionPool.js'); var transactionTypes = require('../helpers/transactionTypes.js'); @@ -251,30 +250,6 @@ __private.getAssetForIdsBasedOnType = function (ids, type) { return library.db.query(sql[queryName], {id: ids}); }; -/** - * Gets transaction by calling parameter method. - * @private - * @param {Object} method - * @param {Object} req - * @param {function} cb - Callback function. - * @returns {setImmediateCallback} error | data: {transaction} - */ -__private.getPooledTransaction = function (method, req, cb) { - library.schema.validate(req.body, schema.getPooledTransaction, function (err) { - if (err) { - return setImmediate(cb, err[0].message); - } - - var transaction = self[method](req.body.id); - - if (!transaction) { - return setImmediate(cb, 'Transaction not found'); - } - - return setImmediate(cb, null, {transaction: transaction}); - }); -}; - /** * Gets transactions by calling parameter method. * Filters by senderPublicKey or address if they are present. @@ -622,26 +597,14 @@ Transactions.prototype.shared = { }); }, - getQueuedTransaction: function (req, cb) { - return __private.getPooledTransaction('getQueuedTransaction', req, cb); - }, - getUnProcessedTransactions: function (filters, cb) { return __private.getPooledTransactions('getQueuedTransactionList', filters, cb); }, - getMultisignatureTransaction: function (req, cb) { - return __private.getPooledTransaction('getMultisignatureTransaction', req, cb); - }, - getMultisignatureTransactions: function (req, cb) { return __private.getPooledTransactions('getMultisignatureTransactionList', req, cb); }, - getUnconfirmedTransaction: function (req, cb) { - return __private.getPooledTransaction('getUnconfirmedTransaction', req, cb); - }, - getUnconfirmedTransactions: function (req, cb) { return __private.getPooledTransactions('getUnconfirmedTransactionList', req, cb); }, diff --git a/schema/transactions.js b/schema/transactions.js deleted file mode 100644 index 35d77d6cfee..00000000000 --- a/schema/transactions.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -var constants = require('../helpers/constants.js'); -var transactionTypes = require('../helpers/transactionTypes.js'); - -module.exports = { - getPooledTransaction: { - id: 'transactions.getPooledTransaction', - type: 'object', - properties: { - id: { - type: 'string', - format: 'id', - minLength: 1, - maxLength: 20 - } - }, - required: ['id'] - } -}; From 42561ef94e9764cd751c809df7050d567d904347 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:58:28 +0100 Subject: [PATCH 04/18] Migrate schema/blocks to swagger definitions --- modules/blocks/process.js | 9 +++---- schema/blocks.js | 33 -------------------------- schema/swagger.yml | 29 ++++++++++++++++++++++ test/functional/ws/transport.blocks.js | 2 +- 4 files changed, 35 insertions(+), 38 deletions(-) delete mode 100644 schema/blocks.js diff --git a/modules/blocks/process.js b/modules/blocks/process.js index cbe2986438d..549cda7b287 100644 --- a/modules/blocks/process.js +++ b/modules/blocks/process.js @@ -4,11 +4,10 @@ var _ = require('lodash'); var async = require('async'); var constants = require('../../helpers/constants.js'); var Peer = require('../../logic/peer.js'); -var schema = require('../../schema/blocks.js'); var slots = require('../../helpers/slots.js'); var sql = require('../../sql/blocks.js'); -var modules, library, self, __private = {}; +var modules, definitions, library, self, __private = {}; /** * Initializes library. @@ -231,7 +230,7 @@ Process.prototype.getCommonBlock = function (peer, height, cb) { }, function (common, waterCb) { // Validate remote peer response via schema - library.schema.validate(common, schema.getCommonBlock, function (err) { + library.schema.validate(common, definitions.CommonBlock, function (err) { if (err) { return setImmediate(waterCb, err[0].message); } else { @@ -397,7 +396,7 @@ Process.prototype.loadBlocksFromPeer = function (peer, cb) { } function validateBlocks (blocks, seriesCb) { - var report = library.schema.validate(blocks, schema.loadBlocksFromPeer); + var report = library.schema.validate(blocks, definitions.BlocksList); if (!report) { return setImmediate(seriesCb, 'Received invalid blocks data'); @@ -583,6 +582,8 @@ Process.prototype.onBind = function (scope) { transport: scope.transport, }; + definitions = scope.swagger.definitions; + // Set module as loaded __private.loaded = true; }; diff --git a/schema/blocks.js b/schema/blocks.js deleted file mode 100644 index bf09d609ed9..00000000000 --- a/schema/blocks.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var constants = require('../helpers/constants.js'); - -module.exports = { - loadBlocksFromPeer: { - id: 'blocks.loadBlocksFromPeer', - type: 'array' - }, - getCommonBlock: { - id: 'blocks.getCommonBlock', - type: 'object', - properties: { - id: { - type: 'string', - format: 'id', - minLength: 1, - maxLength: 20 - }, - previousBlock: { - type: 'string', - format: 'id', - minLength: 1, - maxLength: 20 - }, - height: { - type: 'integer', - minimum: 1 - } - }, - required: ['id', 'previousBlock', 'height'] - } -}; diff --git a/schema/swagger.yml b/schema/swagger.yml index 50a812e986d..8a9d486419a 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -1586,6 +1586,35 @@ definitions: type: string example: "65000000" + CommonBlock: + x-private: true + type: object + required: + - id + - height + - previousBlock + properties: + id: + type: string + format: id + minLength: 1 + maxLength: 20 + example: "6258354802676165798" + height: + type: integer + example: 123 + minimum: 1 + previousBlock: + type: string + format: id + example: "15918760246746894806" + + BlocksList: + x-private: true + type: array + items: + $ref: '#/definitions/Block' + ForgingStatus: type: object required: diff --git a/test/functional/ws/transport.blocks.js b/test/functional/ws/transport.blocks.js index 8fba045b17f..d0811bc6840 100644 --- a/test/functional/ws/transport.blocks.js +++ b/test/functional/ws/transport.blocks.js @@ -93,7 +93,7 @@ describe('WS transport blocks', function () { ws.call('blocksCommon', function (err, res) { test.debug('> Error / Response:'.grey, JSON.stringify(err), JSON.stringify(res)); expect(res).to.be.undefined; - expect(err).to.equal('Missing required property: ids: #/'); + expect(err).to.equal('Missing required property: ids: '); done(); }); }); From 3b54722fcc12830ac859d151efb6adbc080c4afd Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:59:31 +0100 Subject: [PATCH 05/18] Migrate schema/transport.headers to swagger definitions --- helpers/wsApi.js | 8 ++-- schema/swagger.yml | 49 +++++++++++++++++++++++ schema/transport.js | 46 --------------------- test/functional/ws/transport.client.js | 14 +++---- test/functional/ws/transport.handshake.js | 2 +- 5 files changed, 61 insertions(+), 58 deletions(-) diff --git a/helpers/wsApi.js b/helpers/wsApi.js index 54a1fb7714c..a7abe7e26e6 100644 --- a/helpers/wsApi.js +++ b/helpers/wsApi.js @@ -3,18 +3,18 @@ var _ = require('lodash'); var url = require('url'); var failureCodes = require('../api/ws/rpc/failureCodes.js'); -var Z_schema = require('../helpers/z_schema.js'); -var schema = require('../schema/transport.js'); +var swaggerHelper = require('../helpers/swagger'); +var definitions = swaggerHelper.getSwaggerSpec().definitions; var Peer = require('../logic/peer.js'); var constants = require('./constants'); -var z_schema = new Z_schema(); +var z_schema = swaggerHelper.getValidator(); var middleware = { Handshake: function (system) { return function (headers, cb) { - z_schema.validate(headers, schema.headers, function (error) { + z_schema.validate(headers, definitions.WSPeerHeaders, function (error) { if (error) { return setImmediate(cb, { code: failureCodes.INVALID_HEADERS, diff --git a/schema/swagger.yml b/schema/swagger.yml index 8a9d486419a..df09133d022 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2344,3 +2344,52 @@ definitions: balance: type: string example: 1081560729258 + + WSPeerHeaders: + x-private: true + type: object + required: + - httpPort + - wsPort + - os + - version + - height + - nethash + - nonce + properties: + httpPort: + type: integer + example: 8000 + format: int32 + minimum: 1 + maximum: 65535 + wsPort: + type: integer + example: 8001 + format: int32 + minimum: 1 + maximum: 65535 + os: + type: string + example: debian + version: + type: string + example: v0.8.0 + format: version + minLength: 5 + maxLength: 12 + height: + type: integer + example: 123 + nethash: + type: string + maxLength: 64 + broadhash: + type: string + example: 258974416d58533227c6a3da1b6333f0541b06c65b41e45cf31926847a3db1ea + format: hex + nonce: + type: string + example: sYHEDBKcScaAAAYg + minLength: 16 + maxLength: 16 diff --git a/schema/transport.js b/schema/transport.js index 1267ad75d63..1462afc77dc 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -3,52 +3,6 @@ var constants = require('../helpers/constants.js'); module.exports = { - headers: { - id: 'transport.headers', - type: 'object', - properties: { - wsPort: { - type: 'integer', - minimum: 1, - maximum: 65535 - }, - httpPort: { - type: 'integer', - minimum: 1, - maximum: 65535 - }, - os: { - type: 'string', - format: 'os', - minLength: 1, - maxLength: 64 - }, - version: { - type: 'string', - format: 'version', - minLength: 5, - maxLength: 12 - }, - nethash: { - type: 'string', - maxLength: 64 - }, - broadhash: { - type: 'string', - format: 'hex' - }, - height: { - type: 'integer', - minimum: 1 - }, - nonce: { - type: 'string', - minLength: 16, - maxLength: 16 - } - }, - required: ['wsPort', 'httpPort', 'version', 'nonce', 'nethash', 'height', 'os'] - }, commonBlock: { id: 'transport.commonBlock', type: 'object', diff --git a/test/functional/ws/transport.client.js b/test/functional/ws/transport.client.js index d785758175f..d77b943e632 100644 --- a/test/functional/ws/transport.client.js +++ b/test/functional/ws/transport.client.js @@ -118,7 +118,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/wsPort: Expected type integer but found type not-a-number'); + expect(err).to.have.property('description').equal('wsPort: Expected type integer but found type not-a-number'); done(); }); }); @@ -137,7 +137,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/nonce: String is too short (9 chars), minimum 16'); + expect(err).to.have.property('description').equal('nonce: String is too short (9 chars), minimum 16'); done(); }); }); @@ -147,7 +147,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/nonce: String is too long (26 chars), maximum 16'); + expect(err).to.have.property('description').equal('nonce: String is too long (26 chars), maximum 16'); done(); }); }); @@ -157,7 +157,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/: Missing required property: nonce'); + expect(err).to.have.property('description').equal(': Missing required property: nonce'); done(); }); }); @@ -167,7 +167,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/: Missing required property: nethash'); + expect(err).to.have.property('description').equal(': Missing required property: nethash'); done(); }); }); @@ -177,7 +177,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/height: Expected type integer but found type not-a-number'); + expect(err).to.have.property('description').equal('height: Expected type integer but found type not-a-number'); done(); }); }); @@ -187,7 +187,7 @@ describe('ClientRPCStub', function () { System.setHeaders(validHeaders); validClientRPCStub.status(function (err) { expect(err).to.have.property('code').equal(failureCodes.INVALID_HEADERS); - expect(err).to.have.property('description').equal('#/: Missing required property: version'); + expect(err).to.have.property('description').equal(': Missing required property: version'); done(); }); }); diff --git a/test/functional/ws/transport.handshake.js b/test/functional/ws/transport.handshake.js index 40a14004986..7fbcb1c7076 100644 --- a/test/functional/ws/transport.handshake.js +++ b/test/functional/ws/transport.handshake.js @@ -125,7 +125,7 @@ describe('handshake', function () { connect(); expectDisconnect(this, function (code, description) { expect(code).equal(failureCodes.INVALID_HEADERS); - expect(description).contain('#/height: Expected type integer but found type not-a-number'); + expect(description).contain('height: Expected type integer but found type not-a-number'); done(); }); }); From c1af242896b5bbb0915d920d645ccac08620e02d Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 12:59:52 +0100 Subject: [PATCH 06/18] Add swagger definitions into app scope for test cases --- test/common/application.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/common/application.js b/test/common/application.js index cec37565abe..854354e77c7 100644 --- a/test/common/application.js +++ b/test/common/application.js @@ -267,7 +267,9 @@ function __init (initScope, done) { cb(err, results); }); }], - ready: ['modules', 'bus', 'logic', function (scope, cb) { + ready: ['swagger', 'modules', 'bus', 'logic', function (scope, cb) { + scope.modules.swagger = scope.swagger; + // Fire onBind event in every module scope.bus.message('bind', scope.modules); scope.logic.peers.bindModules(scope.modules); From bc98678178ca3e2bfd04b5502b391c117edb92b5 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 14:17:55 +0100 Subject: [PATCH 07/18] Migrate schema/transport.update to swagger definitions --- api/ws/workers/peersUpdateRules.js | 8 ++++--- schema/swagger.yml | 29 +++++++++++++++++++++++++ schema/transport.js | 34 ------------------------------ 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/api/ws/workers/peersUpdateRules.js b/api/ws/workers/peersUpdateRules.js index 5493bb1a7e9..aadca4a704f 100644 --- a/api/ws/workers/peersUpdateRules.js +++ b/api/ws/workers/peersUpdateRules.js @@ -3,13 +3,15 @@ var connectionsTable = require('./connectionsTable'); var SlaveToMasterSender = require('./slaveToMasterSender'); var Rules = require('./rules'); -var schema = require('../../../schema/transport'); var failureCodes = require('../../../api/ws/rpc/failureCodes'); var PeerUpdateError = require('../../../api/ws/rpc/failureCodes').PeerUpdateError; +var swaggerHelper = require('../../../helpers/swagger'); +var definitions = swaggerHelper.getSwaggerSpec().definitions; +var z_schema = swaggerHelper.getValidator(); var Peer = require('../../../logic/peer'); var Z_schema = require('../../../helpers/z_schema'); -var self, z_schema = new Z_schema(); +var self; /** * Secures peers updates. Used only by workers. @@ -107,7 +109,7 @@ PeersUpdateRules.prototype.external = { * @param {function} cb */ update: function (request, cb) { - z_schema.validate(request, schema.update, function (err) { + z_schema.validate(request, definitions.WSPeerUpdateRequest, function (err) { if (err) { return setImmediate(cb, err[0].message); } diff --git a/schema/swagger.yml b/schema/swagger.yml index df09133d022..a04bfb5a774 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2393,3 +2393,32 @@ definitions: example: sYHEDBKcScaAAAYg minLength: 16 maxLength: 16 + + WSPeerUpdateRequest: + type: object + required: + - data + - socketId + properties: + data: + type: object + required: + - nonce + properties: + nethash: + type: string + maxLength: 64 + broadhash: + type: string + format: hex + height: + type: integer + minimum: 1 + nonce: + type: string + minLength: 16 + maxLength: 16 + socketId: + type: string + workerId: + type: number diff --git a/schema/transport.js b/schema/transport.js index 1462afc77dc..8bd2092bb60 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -90,39 +90,5 @@ module.exports = { } }, required: ['authKey', 'peer', 'updateType'] - }, - update: { - id: 'transport.update', - type: 'object', - properties: { - data: { - type: 'object', - properties: { - nethash: { - type: 'string', - maxLength: 64 - }, - broadhash: { - type: 'string', - format: 'hex' - }, - height: { - type: 'integer', - minimum: 1 - }, - nonce: { - type: 'string' - } - }, - required: ['nonce'] - }, - socketId: { - type: 'string' - }, - workerId: { - type: 'number' - } - }, - required: ['socketId', 'data'] } }; From ed3969d74729e2fd77a38e21f1f83113d3ab247f Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:05:06 +0100 Subject: [PATCH 08/18] Fix swagger constructor to return fully resolved schema definitions --- config/swagger.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/config/swagger.js b/config/swagger.js index 07b64bd642d..d5e4c689096 100644 --- a/config/swagger.js +++ b/config/swagger.js @@ -5,6 +5,7 @@ var path = require('path'); var fs = require('fs'); var YAML = require('js-yaml'); var _ = require('lodash'); +var swaggerHelper = require('../helpers/swagger'); // Its necessary to require this file to extend swagger validator with our custom formats var validator = require('../helpers/swagger').getValidator(); @@ -34,12 +35,10 @@ function bootstrapSwagger (app, config, logger, scope, cb) { require(config.root + controllerFolder + file)(scope); }); - var swagger = YAML.safeLoad(fs.readFileSync(path.join(config.root + '/schema/swagger.yml')).toString()); - var swaggerConfig = { appRoot: config.root, configDir: config.root + '/config/swagger', - swagger: swagger, + swaggerFile: path.join(config.root + '/schema/swagger.yml'), enforceUniqueOperationId: true, startWithErrors: false, startWithWarnings: true @@ -89,8 +88,12 @@ function bootstrapSwagger (app, config, logger, scope, cb) { // To be used in test cases or getting configuration runtime app.swaggerRunner = runner; - // Successfully mounted the swagger runner - cb(null, {swaggerRunner: runner, definitions: runner.swagger.definitions}); + swaggerHelper.getResolvedSwaggerSpec().then(function (resolvedSchema) { + // Successfully mounted the swagger runner + cb(null, {swaggerRunner: runner, definitions: resolvedSchema.definitions}); + }).catch(function (reason) { + cb(reason); + }); }); } From f57ab5d57a423f4c75d8c2afb49dd07a80e0794f Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:06:00 +0100 Subject: [PATCH 09/18] Migrate schema/transport.signatures to swagger definitions --- modules/transport.js | 8 +++++--- schema/swagger.yml | 27 +++++++++++++++++++++++++++ schema/transport.js | 35 ----------------------------------- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/modules/transport.js b/modules/transport.js index 55c82bd2e5e..dcc65172ebd 100644 --- a/modules/transport.js +++ b/modules/transport.js @@ -20,7 +20,7 @@ var System = require('../modules/system'); var wsRPC = require('../api/ws/rpc/wsRPC').wsRPC; // Private fields -var modules, library, self, __private = {}; +var modules, definitions, library, self, __private = {}; __private.headers = {}; __private.loaded = false; @@ -121,7 +121,7 @@ __private.receiveSignatures = function (query, cb) { async.series({ validateSchema: function (seriesCb) { - library.schema.validate(query, schema.signatures, function (err) { + library.schema.validate(query, definitions.WSSignaturesList, function (err) { if (err) { return setImmediate(seriesCb, 'Invalid signatures body'); } else { @@ -158,7 +158,7 @@ __private.receiveSignatures = function (query, cb) { * @return {setImmediateCallback} cb | error messages */ __private.receiveSignature = function (query, cb) { - library.schema.validate({signature: query}, schema.signature, function (err) { + library.schema.validate(query, definitions.WSSignature, function (err) { if (err) { return setImmediate(cb, 'Invalid signature body ' + err[0].message); } @@ -312,6 +312,8 @@ Transport.prototype.onBind = function (scope) { transactions: scope.transactions }; + definitions = scope.swagger.definitions; + __private.headers = System.getHeaders(); __private.broadcaster.bind( scope.peers, diff --git a/schema/swagger.yml b/schema/swagger.yml index a04bfb5a774..4f10c887168 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2422,3 +2422,30 @@ definitions: type: string workerId: type: number + + WSSignaturesList: + type: object + required: + - signatures + properties: + signatures: + type: array + items: + $ref: '#/definitions/WSSignature' + minItems: 1 + maxItems: 25 + + WSSignature: + type: object + required: + - transaction + - signature + properties: + transaction: + type: string + format: id + minLength: 1 + maxLength: 20 + signature: + type: string + format: signature diff --git a/schema/transport.js b/schema/transport.js index 8bd2092bb60..ca2a72ce44a 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -38,41 +38,6 @@ module.exports = { }, required: ['transactions'] }, - signatures: { - id: 'transport.signatures', - type: 'object', - properties: { - signatures: { - type: 'array', - minItems: 1, - maxItems: 25 - } - }, - required: ['signatures'] - }, - signature: { - id: 'transport.signature', - type: 'object', - properties: { - signature: { - type: 'object', - properties: { - transaction: { - type: 'string', - format: 'id', - minLength: 1, - maxLength: 20 - }, - signature: { - type: 'string', - format: 'signature' - } - }, - required: ['transaction', 'signature'] - } - }, - required: ['signature'] - }, internalAccess: { id: 'transport.internalAccess', type: 'object', From 39c146090a444b70141dcc527e0d9f89eb28f754 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:10:11 +0100 Subject: [PATCH 10/18] Migrate schema/transport.commonBlock to swagger definitions --- modules/transport.js | 2 +- schema/swagger.yml | 9 +++++++++ schema/transport.js | 11 ----------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/modules/transport.js b/modules/transport.js index dcc65172ebd..6cf83d82fe6 100644 --- a/modules/transport.js +++ b/modules/transport.js @@ -427,7 +427,7 @@ Transport.prototype.isLoaded = function () { Transport.prototype.shared = { blocksCommon: function (query, cb) { query = query || {}; - return library.schema.validate(query, schema.commonBlock, function (err, valid) { + return library.schema.validate(query, definitions.WSBlocksCommonRequest, function (err, valid) { if (err) { err = err[0].message + ': ' + err[0].path; library.logger.debug('Common block request validation failed', {err: err.toString(), req: query}); diff --git a/schema/swagger.yml b/schema/swagger.yml index 4f10c887168..012154a51e5 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2449,3 +2449,12 @@ definitions: signature: type: string format: signature + + WSBlocksCommonRequest: + type: object + required: + - ids + properties: + ids: + type: string + format: csv diff --git a/schema/transport.js b/schema/transport.js index ca2a72ce44a..a2676ad75c1 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -3,17 +3,6 @@ var constants = require('../helpers/constants.js'); module.exports = { - commonBlock: { - id: 'transport.commonBlock', - type: 'object', - properties: { - ids: { - type: 'string', - format: 'csv' - } - }, - required: ['ids'] - }, blocks: { id: 'transport.blocks', type: 'object', From 34badbeeaba04c88f56b9a4182f63ab40c563b0c Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:20:01 +0100 Subject: [PATCH 11/18] Migrate schema/transport.transactions to swagger definitions --- modules/transport.js | 2 +- schema/swagger.yml | 101 +++++++++++++++++++++++++++++++++++++++++++ schema/transport.js | 12 ----- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/modules/transport.js b/modules/transport.js index 6cf83d82fe6..ebc483ae709 100644 --- a/modules/transport.js +++ b/modules/transport.js @@ -562,7 +562,7 @@ Transport.prototype.shared = { }, postTransactions: function (query, cb) { - library.schema.validate(query, schema.transactions, function (err) { + library.schema.validate(query, definitions.WSTransactionsReqeuest, function (err) { if (err) { return setImmediate(cb, null, {success: false, message: err}); } diff --git a/schema/swagger.yml b/schema/swagger.yml index 012154a51e5..b767d7a56cb 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2458,3 +2458,104 @@ definitions: ids: type: string format: csv + + WSTransaction: + type: object + required: + - id + - type + - amount + - fee + - senderPublicKey + - recipientId + - timestamp + - asset + - signature + properties: + id: + type: string + format: id + example: "222675625422353767" + minLength: 1 + maxLength: 20 + amount: + type: integer + example: '150000000' + fee: + type: integer + example: '1000000' + type: + type: integer + minimum: 0 + maximum: 7 + height: + type: integer + minimum: 1 + blockId: + type: string + format: id + minLength: 1 + maxLength: 20 + example: "6258354802676165798" + timestamp: + type: integer + example: 28227090 + senderId: + type: string + format: address + example: 12668885769632475474L + senderPublicKey: + type: string + format: publicKey + example: 2ca9a7143fc721fdc540fef893b27e8d648d2288efa61e56264edf01a2c23079 + senderSecondPublicKey: + type: string + format: publicKey + example: 2ca9a7143fc721fdc540fef893b27e8d648d2288efa61e56264edf01a2c23079 + recipientId: + type: string + format: address + example: 12668885769632475474L + recipientPublicKey: + type: string + format: publicKey + example: 2ca9a7143fc721fdc540fef893b27e8d648d2288efa61e56264edf01a2c23079 + signature: + type: string + format: signature + example: 2821d93a742c4edf5fd960efad41a4def7bf0fd0f7c09869aed524f6f52bf9c97a617095e2c712bd28b4279078a29509b339ac55187854006591aa759784c205 + secondSignature: + type: string + format: signature + example: 2821d93a742c4edf5fd960efad41a4def7bf0fd0f7c09869aed524f6f52bf9c97a617095e2c712bd28b4279078a29509b339ac55187854006591aa759784c205 + multisignatures: + type: array + items: + type: string + format: signature + example: 72c9b2aa734ec1b97549718ddf0d4737fd38a7f0fd105ea28486f2d989e9b3e399238d81a93aa45c27309d91ce604a5db9d25c9c90a138821f2011bc6636c60a + confirmations: + type: integer + minimum: 0 + asset: + type: object + receivedAt: + type: string + format: 'date-time' + relays: + type: integer + signSignature: + type: string + format: signature + + WSTransactionsReqeuest: + type: object + required: + - transactions + properties: + transactions: + type: array + items: + $ref: '#/definitions/WSTransaction' + minItems: 1 + maxItems: 25 diff --git a/schema/transport.js b/schema/transport.js index a2676ad75c1..1f0347ad375 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -15,18 +15,6 @@ module.exports = { } }, }, - transactions: { - id: 'transport.transactions', - type: 'object', - properties: { - transactions: { - type: 'array', - minItems: 1, - maxItems: 25 - } - }, - required: ['transactions'] - }, internalAccess: { id: 'transport.internalAccess', type: 'object', From 29add3dc8f598e788119c6ba4331106b293f11d1 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:25:08 +0100 Subject: [PATCH 12/18] Migrate schema/transport.internalAccess to swagger definitions --- modules/transport.js | 3 +-- schema/swagger.yml | 16 ++++++++++++++++ schema/transport.js | 18 ------------------ 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/modules/transport.js b/modules/transport.js index ebc483ae709..d0781bdc729 100644 --- a/modules/transport.js +++ b/modules/transport.js @@ -14,7 +14,6 @@ var failureCodes = require('../api/ws/rpc/failureCodes'); var Peer = require('../logic/peer'); var PeerUpdateError = require('../api/ws/rpc/failureCodes').PeerUpdateError; var Rules = require('../api/ws/workers/rules'); -var schema = require('../schema/transport.js'); var sql = require('../sql/transport.js'); var System = require('../modules/system'); var wsRPC = require('../api/ws/rpc/wsRPC').wsRPC; @@ -596,7 +595,7 @@ Transport.prototype.shared = { * @param {function} cb */ __private.checkInternalAccess = function (query, cb) { - library.schema.validate(query, schema.internalAccess, function (err) { + library.schema.validate(query, definitions.WSAccessObject, function (err) { if (err) { return setImmediate(cb, err[0].message); } diff --git a/schema/swagger.yml b/schema/swagger.yml index b767d7a56cb..d2baa5874be 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2559,3 +2559,19 @@ definitions: $ref: '#/definitions/WSTransaction' minItems: 1 maxItems: 25 + + WSAccessObject: + type: object + required: + - peer + - authKey + - updateType + properties: + peer: + type: object + authKey: + type: string + updateType: + type: integer + minimum: 0 + maximum: 1 diff --git a/schema/transport.js b/schema/transport.js index 1f0347ad375..2f1c8ea7f80 100644 --- a/schema/transport.js +++ b/schema/transport.js @@ -14,23 +14,5 @@ module.exports = { maxLength: 20 } }, - }, - internalAccess: { - id: 'transport.internalAccess', - type: 'object', - properties: { - peer: { - type: 'object' - }, - authKey: { - type: 'string' - }, - updateType: { - type: 'integer', - minimum: 0, - maximum: 1 - } - }, - required: ['authKey', 'peer', 'updateType'] } }; From 7d73bbe6efc4027054313b6e4ae435f734358565 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:50:50 +0100 Subject: [PATCH 13/18] Migrate schema/loader to swagger definitions --- modules/loader.js | 9 +++++---- schema/loader.js | 28 ---------------------------- schema/swagger.yml | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 schema/loader.js diff --git a/modules/loader.js b/modules/loader.js index a091a1778d6..60ce7e9b9b2 100644 --- a/modules/loader.js +++ b/modules/loader.js @@ -6,14 +6,13 @@ var exceptions = require('../helpers/exceptions'); var ip = require('ip'); var jobsQueue = require('../helpers/jobsQueue.js'); var Peer = require('../logic/peer.js'); -var schema = require('../schema/loader.js'); var slots = require('../helpers/slots.js'); var sql = require('../sql/loader.js'); require('colors'); // Private fields -var modules, library, self, __private = {}, shared = {}; +var modules, definitions, library, self, __private = {}, shared = {}; __private.loaded = false; __private.isActive = false; @@ -172,7 +171,7 @@ __private.loadSignatures = function (cb) { peer.applyHeaders({state: Peer.STATE.DISCONNECTED}); return setImmediate(waterCb, err); } else { - library.schema.validate(res, schema.loadSignatures, function (err) { + library.schema.validate(res, definitions.WSSignaturesResponse, function (err) { return setImmediate(waterCb, err, res.signatures); }); } @@ -232,7 +231,7 @@ __private.loadTransactions = function (cb) { peer.applyHeaders({state: Peer.STATE.DISCONNECTED}); return setImmediate(waterCb, err); } - library.schema.validate(res, schema.loadTransactions, function (err) { + library.schema.validate(res, definitions.WSTransactionsResponse, function (err) { if (err) { return setImmediate(waterCb, err[0].message); } else { @@ -846,6 +845,8 @@ Loader.prototype.onBind = function (scope) { system: scope.system, }; + definitions = scope.swagger.definitions; + __private.loadBlockChain(); }; diff --git a/schema/loader.js b/schema/loader.js deleted file mode 100644 index 1495f517a03..00000000000 --- a/schema/loader.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -module.exports = { - loadSignatures: { - id: 'loader.loadSignatures', - type: 'object', - properties: { - signatures: { - type: 'array', - uniqueItems: true, - maxItems: 100 - } - }, - required: ['signatures'] - }, - loadTransactions: { - id: 'loader.loadTransactions', - type: 'object', - properties: { - transactions: { - type: 'array', - uniqueItems: true, - maxItems: 100 - } - }, - required: ['transactions'] - } -}; diff --git a/schema/swagger.yml b/schema/swagger.yml index d2baa5874be..32d64429ed6 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2575,3 +2575,26 @@ definitions: type: integer minimum: 0 maximum: 1 + + WSTransactionsResponse: + type: object + required: + - transactions + properties: + transactions: + type: array + uniqueItems: true + maxItems: 100 + items: + type: object + + WSSignaturesResponse: + required: + - signatures + properties: + signatures: + type: array + uniqueItems: true + maxItems: 100 + items: + type: object From 2f81146773139324793c91929976ea61a64a00ba Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 15:52:54 +0100 Subject: [PATCH 14/18] Add x-private to internally used swagger definitions --- schema/swagger.yml | 9 +++++++++ schema/transport.js | 18 ------------------ 2 files changed, 9 insertions(+), 18 deletions(-) delete mode 100644 schema/transport.js diff --git a/schema/swagger.yml b/schema/swagger.yml index 32d64429ed6..0537cdfeb35 100755 --- a/schema/swagger.yml +++ b/schema/swagger.yml @@ -2395,6 +2395,7 @@ definitions: maxLength: 16 WSPeerUpdateRequest: + x-private: true type: object required: - data @@ -2424,6 +2425,7 @@ definitions: type: number WSSignaturesList: + x-private: true type: object required: - signatures @@ -2436,6 +2438,7 @@ definitions: maxItems: 25 WSSignature: + x-private: true type: object required: - transaction @@ -2451,6 +2454,7 @@ definitions: format: signature WSBlocksCommonRequest: + x-private: true type: object required: - ids @@ -2460,6 +2464,7 @@ definitions: format: csv WSTransaction: + x-private: true type: object required: - id @@ -2549,6 +2554,7 @@ definitions: format: signature WSTransactionsReqeuest: + x-private: true type: object required: - transactions @@ -2561,6 +2567,7 @@ definitions: maxItems: 25 WSAccessObject: + x-private: true type: object required: - peer @@ -2577,6 +2584,7 @@ definitions: maximum: 1 WSTransactionsResponse: + x-private: true type: object required: - transactions @@ -2589,6 +2597,7 @@ definitions: type: object WSSignaturesResponse: + x-private: true required: - signatures properties: diff --git a/schema/transport.js b/schema/transport.js deleted file mode 100644 index 2f1c8ea7f80..00000000000 --- a/schema/transport.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -var constants = require('../helpers/constants.js'); - -module.exports = { - blocks: { - id: 'transport.blocks', - type: 'object', - properties: { - lastBlockId: { - type: 'string', - format: 'id', - minLength: 1, - maxLength: 20 - } - }, - } -}; From adfa78416e5afdd22db5abba1b29a76c60486691 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 16:26:10 +0100 Subject: [PATCH 15/18] Fix unit-tests failed due to swagger definition injection into modules --- helpers/z_schema.js | 5 ++++ test/unit/helpers/wsApi.js | 12 +++++----- test/unit/modules/loader.js | 47 +++++++++++++++++++++---------------- test/unit/modules/peers.js | 10 +++++++- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/helpers/z_schema.js b/helpers/z_schema.js index 0c60a005ace..1374f03ef48 100644 --- a/helpers/z_schema.js +++ b/helpers/z_schema.js @@ -123,6 +123,11 @@ var liskFormats = { }, version: function (str) { + + if (str === null || str === undefined) { + return false; + } + if (str.length === 0) { return true; } diff --git a/test/unit/helpers/wsApi.js b/test/unit/helpers/wsApi.js index 963cf62717b..cfd10a28fe4 100644 --- a/test/unit/helpers/wsApi.js +++ b/test/unit/helpers/wsApi.js @@ -105,7 +105,7 @@ describe('handshake', function () { it('should call callback with error.description when input is: ' + type.description, function (done) { handshake(type.input, function (err) { - expect(err.description).to.equal('#/: Expected type object but found type ' + type.expectation); + expect(err.description).to.equal(': Expected type object but found type ' + type.expectation); done(); }); }); @@ -133,7 +133,7 @@ describe('handshake', function () { it('should call callback with error.description when input is: ' + type.description, function (done) { headers.nonce = type.input; handshake(headers, function (err) { - expect(err.description).to.equal('#/nonce: Expected type string but found type ' + type.expectation); + expect(err.description).to.equal('nonce: Expected type string but found type ' + type.expectation); done(); }); }); @@ -181,7 +181,7 @@ describe('handshake', function () { it('should call callback with error.description when input is: ' + type.description, function (done) { headers.height = type.input; handshake(headers, function (err) { - expect(err.description).to.equal('#/height: Expected type integer but found type ' + type.expectation); + expect(err.description).to.equal('height: Expected type integer but found type ' + type.expectation); done(); }); }); @@ -222,7 +222,7 @@ describe('handshake', function () { it('should call callback with error.description when input is: ' + type.description, function (done) { headers.nethash = type.input; handshake(headers, function (err) { - expect(err.description).to.equal('#/nethash: Expected type string but found type ' + type.expectation); + expect(err.description).to.equal('nethash: Expected type string but found type ' + type.expectation); done(); }); }); @@ -248,7 +248,7 @@ describe('handshake', function () { it('should call callback with error.description when input is: ' + type.description, function (done) { headers.version = type.input; handshake(headers, function (err) { - expect(err.description).to.equal('#/version: Expected type string but found type ' + type.expectation); + expect(err.description).to.equal('version: Expected type string but found type ' + type.expectation); done(); }); }); @@ -268,7 +268,7 @@ describe('handshake', function () { it('should call callback with error for required property: ' + property, function (done) { headers[property] = undefined; handshake(headers, function (err) { - expect(err.description).to.equal('#/: Missing required property: ' + property); + expect(err.description).to.equal(': Missing required property: ' + property); done(); }); }); diff --git a/test/unit/modules/loader.js b/test/unit/modules/loader.js index 9583711ccc8..4411508813d 100644 --- a/test/unit/modules/loader.js +++ b/test/unit/modules/loader.js @@ -9,6 +9,7 @@ var sinon = require('sinon'); var jobsQueue = require('../../../helpers/jobsQueue'); var modulesLoader = require('../../common/modulesLoader'); +var swaggerHelper = require('../../../helpers/swagger'); describe('loader', function () { @@ -23,28 +24,34 @@ describe('loader', function () { get: function () {} } }; - modulesLoader.initModule( - loaderModuleRewired, - _.assign({}, modulesLoader.scope, { - logic: { - transaction: sinon.mock(), - account: sinon.mock(), - peers: { - create: sinon.stub.returnsArg(0) + + swaggerHelper.getResolvedSwaggerSpec().then(function (resolvedSwaggerSpec) { + modulesLoader.initModule( + loaderModuleRewired, + _.assign({}, modulesLoader.scope, { + logic: { + transaction: sinon.mock(), + account: sinon.mock(), + peers: { + create: sinon.stub.returnsArg(0) + } } - } - }), - function (err, __loaderModule) { - if (err) { - return done(err); - } - loaderModule = __loaderModule; - loadBlockChainStub = sinon.stub(loaderModuleRewired.__get__('__private'), 'loadBlockChain'); - loaderModule.onBind({ - blocks: blocksModuleMock + }), + function (err, __loaderModule) { + if (err) { + return done(err); + } + loaderModule = __loaderModule; + loadBlockChainStub = sinon.stub(loaderModuleRewired.__get__('__private'), 'loadBlockChain'); + loaderModule.onBind({ + blocks: blocksModuleMock, + swagger: { + definitions: resolvedSwaggerSpec.definitions + } + }); + done(); }); - done(); - }); + }); after(function () { loadBlockChainStub.restore(); diff --git a/test/unit/modules/peers.js b/test/unit/modules/peers.js index 3d376f7aae7..58a22b22c27 100644 --- a/test/unit/modules/peers.js +++ b/test/unit/modules/peers.js @@ -17,6 +17,7 @@ var constants = require('../../../helpers/constants'); var generateMatchedAndUnmatchedBroadhashes = require('../common/helpers/peers').generateMatchedAndUnmatchedBroadhashes; var modulesLoader = require('../../common/modulesLoader'); var random = require('../../common/utils/random'); +var swagerHelper = require('../../../helpers/swagger'); describe('peers', function () { @@ -31,7 +32,7 @@ describe('peers', function () { var NONCE = randomstring.generate(16); - before(function () { + before(function (done) { dbMock = { any: sinon.stub().resolves() }; @@ -50,6 +51,13 @@ describe('peers', function () { system: systemModuleMock, transport: transportModuleMock }; + + swagerHelper.getResolvedSwaggerSpec().then(function (resolvedSpec) { + modules.swagger = { + definitions: resolvedSpec.definitions + }; + done(); + }); }); before(function (done) { From ef5962acd223314ee065d3008a6c8c037218c446 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 20 Dec 2017 16:56:05 +0100 Subject: [PATCH 16/18] Fix http-post-tests failed due to swagger definition injection into modules --- test/functional/http/post/6.dapps.inTransfer.js | 4 ++-- test/functional/http/post/7.dapps.outTransfer.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/functional/http/post/6.dapps.inTransfer.js b/test/functional/http/post/6.dapps.inTransfer.js index 4af58f9c655..ef3fa83818b 100644 --- a/test/functional/http/post/6.dapps.inTransfer.js +++ b/test/functional/http/post/6.dapps.inTransfer.js @@ -103,7 +103,7 @@ describe('POST /api/transactions (type 6) inTransfer dapp', function () { transaction.asset.inTransfer.dappId = 1.2; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate inTransfer schema: Expected type string but found type number'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate inTransfer schema: Expected type string but found type number, Object didn\'t pass validation for format id: 1.2'); badTransactions.push(transaction); }); }); @@ -123,7 +123,7 @@ describe('POST /api/transactions (type 6) inTransfer dapp', function () { transaction.asset.inTransfer.dappId = {}; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate inTransfer schema: Expected type string but found type object'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate inTransfer schema: Expected type string but found type object, Object didn\'t pass validation for format id: {}'); badTransactions.push(transaction); }); }); diff --git a/test/functional/http/post/7.dapps.outTransfer.js b/test/functional/http/post/7.dapps.outTransfer.js index 6e0ae5f2d17..58db0fd8888 100644 --- a/test/functional/http/post/7.dapps.outTransfer.js +++ b/test/functional/http/post/7.dapps.outTransfer.js @@ -104,7 +104,7 @@ describe('POST /api/transactions (type 7) outTransfer dapp', function () { transaction.asset.outTransfer.dappId = 1.2; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type number'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type number, Object didn\'t pass validation for format id: 1.2'); badTransactions.push(transaction); }); }); @@ -124,7 +124,7 @@ describe('POST /api/transactions (type 7) outTransfer dapp', function () { transaction.asset.outTransfer.dappId = {}; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type object'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type object, Object didn\'t pass validation for format id: {}'); badTransactions.push(transaction); }); }); @@ -176,7 +176,7 @@ describe('POST /api/transactions (type 7) outTransfer dapp', function () { transaction.asset.outTransfer.transactionId = 1.2; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type number'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type number, Object didn\'t pass validation for format id: 1.2'); badTransactions.push(transaction); }); }); @@ -196,7 +196,7 @@ describe('POST /api/transactions (type 7) outTransfer dapp', function () { transaction.asset.outTransfer.transactionId = {}; return sendTransactionPromise(transaction, errorCodes.PROCESSING_ERROR).then(function (res) { - res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type object'); + res.body.message.should.be.equal('Invalid transaction body - Failed to validate outTransfer schema: Expected type string but found type object, Object didn\'t pass validation for format id: {}'); badTransactions.push(transaction); }); }); From c8f9ba273342ceb4eaae38aff63e0e558dec2a76 Mon Sep 17 00:00:00 2001 From: Oliver Beddows Date: Tue, 2 Jan 2018 21:06:48 +0100 Subject: [PATCH 17/18] Apply standards --- app.js | 1 - config/swagger.js | 1 - helpers/z_schema.js | 1 - modules/peers.js | 1 + 4 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app.js b/app.js index 76a3389976c..0b6d0ff0964 100644 --- a/app.js +++ b/app.js @@ -530,7 +530,6 @@ d.run(function () { }], ready: ['swagger', 'modules', 'bus', 'logic', function (scope, cb) { - scope.modules.swagger = scope.swagger; // Fire onBind event in every module diff --git a/config/swagger.js b/config/swagger.js index d5e4c689096..ec48a712e2a 100644 --- a/config/swagger.js +++ b/config/swagger.js @@ -47,7 +47,6 @@ function bootstrapSwagger (app, config, logger, scope, cb) { // Swagger Express Middleware SwaggerRunner.create(swaggerConfig, function (errors, runner) { if (errors) { - // Ignore unused definition warning errors.validationWarnings = _.filter(errors.validationWarnings, function (error) { return error.code !== 'UNUSED_DEFINITION'; diff --git a/helpers/z_schema.js b/helpers/z_schema.js index 1374f03ef48..644df56673c 100644 --- a/helpers/z_schema.js +++ b/helpers/z_schema.js @@ -123,7 +123,6 @@ var liskFormats = { }, version: function (str) { - if (str === null || str === undefined) { return false; } diff --git a/modules/peers.js b/modules/peers.js index 33db67e7fa5..a2837631e15 100644 --- a/modules/peers.js +++ b/modules/peers.js @@ -518,6 +518,7 @@ Peers.prototype.onBind = function (scope) { modules = { system: scope.system }; + definitions = scope.swagger.definitions; }; From 51d6b492b236faea740a7b02842b4e4f7f5d6ecc Mon Sep 17 00:00:00 2001 From: Oliver Beddows Date: Tue, 2 Jan 2018 21:09:02 +0100 Subject: [PATCH 18/18] Review comments --- config/swagger.js | 8 ++++---- modules/peers.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/swagger.js b/config/swagger.js index ec48a712e2a..16cf005f255 100644 --- a/config/swagger.js +++ b/config/swagger.js @@ -44,7 +44,7 @@ function bootstrapSwagger (app, config, logger, scope, cb) { startWithWarnings: true }; - // Swagger Express Middleware + // Swagger express middleware SwaggerRunner.create(swaggerConfig, function (errors, runner) { if (errors) { // Ignore unused definition warning @@ -69,12 +69,12 @@ function bootstrapSwagger (app, config, logger, scope, cb) { } } - // Swagger Express Middleware + // Swagger express middleware var swaggerExpress = runner.expressMiddleware(); - // Check the response and do appropriate on error + // Check the response and act appropriately on error runner.on('responseValidationError', function (validationResponse, request, response) { - // TODO: Troubleshoot why default validation hook consider json response as string response + // TODO: Troubleshoot why default validation hook considers json response as string response if (validationResponse.errors[0].code !== 'INVALID_RESPONSE_BODY') { logger.error('Swagger Response Validation Errors:'); logger.error(validationResponse.errors[0].errors); diff --git a/modules/peers.js b/modules/peers.js index a2837631e15..298817bc7e3 100644 --- a/modules/peers.js +++ b/modules/peers.js @@ -551,7 +551,7 @@ Peers.prototype.onBlockchainReady = function () { }; /** - * Discovers peers and updates them in 10sec intervals loop. + * Periodically discovers and updates peers. */ Peers.prototype.onPeersReady = function () { library.logger.trace('Peers ready'); @@ -597,7 +597,7 @@ Peers.prototype.onPeersReady = function () { return setImmediate(cb); }); } - // Loop in 10sec intervals (5sec + 5sec connect timeout from pingPeer) + // Loop in 10 sec intervals (5 sec + 5 sec connection timeout from pingPeer) jobsQueue.register('peersDiscoveryAndUpdate', peersDiscoveryAndUpdate, 5000); };