diff --git a/CHANGELOG.md b/CHANGELOG.md index 06650d76..ef09110d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog -## [1.1.9] - 2021-01-29 + +## [1.2.1] - 2021-02-05 + +### Added + +- UserEvents support to HTTP request. + +## [1.2.0] - 2021-01-29 ### Added diff --git a/dist/api/HttpServer.js b/dist/api/HttpServer.js index 22fdad08..c36ba81a 100644 --- a/dist/api/HttpServer.js +++ b/dist/api/HttpServer.js @@ -4,9 +4,10 @@ var _api = _interopRequireDefault(require("./routes/api")); var _doc = _interopRequireDefault(require("./routes/doc")); var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} -const HttpServer = ({ api, status, log }) => { +const HttpServer = ({ api, status, log }, send) => { const app = (0, _express.default)(); const httpServer = _http.default.Server(app); + app.set('etag', false); app.set('x-powered-by', false); @@ -24,7 +25,7 @@ const HttpServer = ({ api, status, log }) => { res.send(data); }); - app.use('/api', (0, _api.default)({ log, api })); + app.use('/api', (0, _api.default)({ log, api }, send)); if (_config.default.api.exposeDoc) { app.use('/doc', (0, _doc.default)({ log, app })); @@ -32,7 +33,7 @@ const HttpServer = ({ api, status, log }) => { // 404 app.use((req, res, next) => res.status(404).send()); - return httpServer; + return { httpServer, app }; };exports.HttpServer = HttpServer;var _default = HttpServer;exports.default = _default; \ No newline at end of file diff --git a/dist/api/Status.js b/dist/api/Status.js index 0cb39af4..431b3211 100644 --- a/dist/api/Status.js +++ b/dist/api/Status.js @@ -26,7 +26,7 @@ class Status extends _DataCollector.DataCollector { return Status.find({}, { timestamp: -1 }, 1). then(res => { res = res.data[0]; - delete res._id; + if (res) delete res._id; return res; }); } @@ -57,7 +57,7 @@ class Status extends _DataCollector.DataCollector { this.getHighestBlock(), this.getTotalBlocks()]); - const status = Object.assign(blocksStatus, { + const status = Object.assign(blocksStatus || {}, { dbLastBlockReceived: last.number, dbLastBlockReceivedTime: last._received, dbHighBlock: high.number, diff --git a/dist/api/UserEventsApi.js b/dist/api/UserEventsApi.js index 54c1a2dd..a39519d7 100644 --- a/dist/api/UserEventsApi.js +++ b/dist/api/UserEventsApi.js @@ -20,6 +20,7 @@ const UserEventsApi = (io, api, { log }) => { let result = res.data; let req = payload; let error = res.error; + if (!msg.socketId) return; const socket = io.sockets.connected[msg.socketId]; log.trace(`Sending message to client ${module}.${action} error:${JSON.stringify(error)}`); if (socket) socket.emit('data', (0, _apiTools.formatRes)({ module, action, result, req, error })); diff --git a/dist/api/docs.yaml b/dist/api/docs.yaml index 02cbd443..29534c65 100644 --- a/dist/api/docs.yaml +++ b/dist/api/docs.yaml @@ -79,6 +79,8 @@ definitions: Response: type: object + description: Response + name: Item properties: pages: type: object @@ -90,6 +92,7 @@ definitions: description: sortable fields type: object defaultSort: + type: object sortDir: type: boolean limit: @@ -106,6 +109,7 @@ definitions: type: object ResponseList: + description: ResponseList type: object properties: pages: @@ -118,6 +122,7 @@ definitions: description: sortable fields type: object defaultSort: + type: object sortDir: type: boolean limit: diff --git a/dist/api/index.js b/dist/api/index.js index fffe174b..1f42bb53 100644 --- a/dist/api/index.js +++ b/dist/api/index.js @@ -25,13 +25,43 @@ const address = _config.default.api.address || 'localhost'; status.start(); txPool.start(); + let userEvents; + + const delayedResult = (res, payload, socket) => { + const { params, result, delayed } = res; + if (delayed && userEvents) { + const registry = delayed.registry || !result.data && delayed.runIfEmpty; + if (payload.getDelayed) { + const lastBlock = api.getLastBlock(); + const block = lastBlock ? lastBlock.number : null; + + userEvents.send({ + action: delayed.action, + module: delayed.module, + params, + socketId: socket ? socket.id : undefined, + payload, + block, + result }); + + } + res.result.delayed = { fields: delayed.fields, registry }; + } + return res; + }; + + const send = ({ res, response, payload }) => { + const { result } = delayedResult(response, payload); + res.send(result); + }; + // http server - const httpServer = (0, _HttpServer.HttpServer)({ api, status, log: _log.default }); + const { httpServer } = (0, _HttpServer.HttpServer)({ api, status, log: _log.default }, send); httpServer.listen(port, address); const io = new _socket.default(httpServer); // start userEvents api - const userEvents = (0, _UserEventsApi.default)(io, api, { log: _log.default }); + userEvents = (0, _UserEventsApi.default)(io, api, { log: _log.default }); io.httpServer.on('listening', () => { _log.default.info(`Server listening on: ${address || '0.0.0.0'}:${port}`); @@ -127,25 +157,9 @@ const address = _config.default.api.address || 'localhost'; socket.on('data', async payload => { try { const res = await api.run(payload); - const { module, action, params, result, delayed } = res; - if (delayed && userEvents) { - const registry = delayed.registry || !result.data && delayed.runIfEmpty; - if (payload.getDelayed) { - const lastBlock = api.getLastBlock(); - const block = lastBlock ? lastBlock.number : null; - userEvents.send({ - action: delayed.action, - module: delayed.module, - params, - socketId: socket.id, - payload, - block, - result }); - - } - result.delayed = { fields: delayed.fields, registry }; - } - socket.emit('data', (0, _apiTools.formatRes)({ module, action, result, req: payload })); + const req = payload; + const { module, action, result } = delayedResult(res, payload, socket); + socket.emit('data', (0, _apiTools.formatRes)({ module, action, result, req })); } catch (err) { _log.default.debug(`Action: ${payload.action}: ERROR: ${err}`); _log.default.trace(err); diff --git a/dist/api/modules/ContractVerification.js b/dist/api/modules/ContractVerification.js index b9c461e6..45f24fcd 100644 --- a/dist/api/modules/ContractVerification.js +++ b/dist/api/modules/ContractVerification.js @@ -2,8 +2,8 @@ var _StoredConfig = require("../../lib/StoredConfig"); var _ContractVerifierModule = require("../../services/userEvents/ContractVerifierModule"); var _Errors = require("../lib/Errors"); -var _mongodb = require("mongodb"); -var _types = require("../../lib/types"); + +var _types = require("../../lib/types"); // import { ObjectID } from 'mongodb' class ContractVerification extends _DataCollector.DataCollectorItem { constructor(collections, name) { @@ -58,12 +58,14 @@ class ContractVerification extends _DataCollector.DataCollectorItem { // TODO Check if has pending verifications - const { creationCode, code } = data; + // const { creationCode, code } = data + const { creationCode } = data; if (!creationCode) throw new _Errors.Error404('Contract creation data not found'); // Contract verifier payload request.bytecode = creationCode; - request.deployedBytecode = code; + // request.deployedBytecode = code + request._id = (0, _ContractVerifierModule.getVerificationId)(request); return { data: request }; } catch (err) { return Promise.reject(err); @@ -130,8 +132,7 @@ class ContractVerification extends _DataCollector.DataCollectorItem { try { let { id } = params; if (!id) throw new Error('Invalid id'); - const _id = (0, _mongodb.ObjectID)(id); - const verification = await this.getOne({ _id }); + const verification = await this.getOne({ _id: id }); if (verification && verification.data) { const { result, match } = verification.data; return { data: { result, match } }; diff --git a/dist/api/modules/VerificationResults.js b/dist/api/modules/VerificationResults.js index 672ac111..c1c6d0e3 100644 --- a/dist/api/modules/VerificationResults.js +++ b/dist/api/modules/VerificationResults.js @@ -45,9 +45,9 @@ class VerificationResults extends _DataCollector.DataCollectorItem { * $ref: '#/responses/NotFound' */ getVerification: async params => { - const { address } = params; + const { address, fields } = params; const query = { address, match: true }; - return this.getItem(query, params); + return this.getOne(query, fields); } }; }}exports.VerificationResults = VerificationResults;var _default = diff --git a/dist/api/routes/api.js b/dist/api/routes/api.js index 3b46e901..7376a470 100644 --- a/dist/api/routes/api.js +++ b/dist/api/routes/api.js @@ -2,28 +2,30 @@ var _bodyParser = _interopRequireDefault(require("body-parser"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} const router = _express.default.Router(); -const Routes = ({ log, api }) => { - const getResult = async (api, { module, action, params }) => { +const Routes = ({ log, api }, send) => { + const getResult = async ({ module, action, params }) => { try { - const { result } = await api.run({ module, action, params }); - if (!result) throw new Error('Missing result'); - if (!result.data) throw new Error('Missing data'); - return result; + const response = await api.run({ module, action, params }); + if (!response.result) throw new Error('Missing result'); + // if (!result.data) throw new Error('Missing data') + return response; } catch (err) { + log.debug({ module, action, params }); return Promise.reject(err); } }; - const sendResult = async (api, res, { module, action, params }) => { - let result; + const sendResult = async ({ res, req, next }, payload) => { + const { module, action } = payload; + let response; try { if (!!module !== !!action) { res.status(400).send(); return; } - if (!module && !action) result = api.info();else - result = await getResult(api, { module, action, params }); - if (!result) throw new Error('Empty result'); - res.send(result); + if (!module && !action) response = { result: api.info() };else + response = await getResult(payload); + if (typeof send === 'function') send({ response, res, req, next, payload });else + res.send(response.result); } catch (err) { res.status(404).send(); log.error(err); @@ -35,11 +37,11 @@ const Routes = ({ log, api }) => { const { module, action } = params; delete params.module; delete params.action; - return sendResult(api, res, { module, action, params }); + return sendResult({ res, req, next }, { module, action, params }); }); - router.post('/', _bodyParser.default.json(), (req, res, next) => { - return sendResult(api, res, req.body); + router.post('/', _bodyParser.default.json({ limit: '5mb' }), (req, res, next) => { + return sendResult({ res, req, next }, req.body); }); return router; diff --git a/dist/lib/collections.js b/dist/lib/collections.js index b7dcad4b..18f7d3d8 100644 --- a/dist/lib/collections.js +++ b/dist/lib/collections.js @@ -257,7 +257,11 @@ { key: { match: 1 }, - name: 'verificationsResultsMatchIndex' }] }, + name: 'verificationsResultsMatchIndex' }, + + { + key: { address: 1, match: 1 }, + name: 'verificationResultAddressMatchIndex' }] }, diff --git a/dist/services/userEvents/ContractVerifierModule.js b/dist/services/userEvents/ContractVerifierModule.js index 5224fbd6..70f4dcbf 100644 --- a/dist/services/userEvents/ContractVerifierModule.js +++ b/dist/services/userEvents/ContractVerifierModule.js @@ -1,6 +1,6 @@ -"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.ContractVerifierModule = ContractVerifierModule;exports.replaceImport = replaceImport;exports.extractUsedSourcesFromRequest = extractUsedSourcesFromRequest;exports.default = exports.versionsId = void 0;var _socket = _interopRequireDefault(require("socket.io-client")); +"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.ContractVerifierModule = ContractVerifierModule;exports.replaceImport = replaceImport;exports.extractUsedSourcesFromRequest = extractUsedSourcesFromRequest;exports.getVerificationId = getVerificationId;exports.default = exports.versionsId = void 0;var _socket = _interopRequireDefault(require("socket.io-client")); var _StoredConfig = require("../../lib/StoredConfig"); -var _mongodb = require("mongodb");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} +var _rskUtils = require("@rsksmart/rsk-utils");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} // id to store solc versions list on Config collection const versionsId = '_contractVerifierVersions';exports.versionsId = versionsId; @@ -41,7 +41,7 @@ function ContractVerifierModule(db, collections, { url } = {}, { log } = {}) { const result = data ? data.result : null; let { _id, address } = request; if (!_id) throw new Error(`Missing _id {$request}`); - _id = (0, _mongodb.ObjectID)(_id); + // _id = ObjectID(_id) log.debug(`New verification received ${address}`); // Update verification const match = checkResult(result || {}); @@ -81,11 +81,12 @@ function ContractVerifierModule(db, collections, { url } = {}, { log } = {}) { const { data } = result; const { module, action } = payload; const { address } = data; - delete data._id; + // delete data._id + const { _id } = data; if (!address) throw new Error(`Missing address in verification`); - let res = await collection.insertOne({ address, request: data, timestamp: Date.now() }); + let res = await collection.insertOne({ _id, address, request: data, timestamp: Date.now() }); const id = res.insertedId; - if (!id) throw new Error(`Error creating pending verification`); + if (!id || id !== _id) throw new Error(`Error creating pending verification`); data._id = id; log.debug(`Sending verification to verifier ID:${id}`); if (!socket.connected) throw new Error('Cannot connect to contract verifier'); @@ -140,6 +141,12 @@ function extractUsedSourcesFromRequest({ source, imports }, { usedSources }) { const { contents } = imp; return { name, contents }; }); +} + +function getVerificationId({ address }) { + if (!(0, _rskUtils.isAddress)(address)) throw new Error(`Invalid address ${address}`); + const timestamp = Date.now(); + return (0, _rskUtils.add0x)((0, _rskUtils.keccak256)(`${address}${timestamp}`)); }var _default = ContractVerifierModule;exports.default = _default; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 719527d0..03b2ad5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rsk-explorer-api", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4da67ffd..3ec3675b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsk-explorer-api", - "version": "1.2.0", + "version": "1.2.1", "description": "", "main": "index.js", "scripts": { diff --git a/public/swagger.json b/public/swagger.json index 87971e4c..57030fb0 100644 --- a/public/swagger.json +++ b/public/swagger.json @@ -1,7 +1,7 @@ { "info": { "title": "rsk-explorer-api", - "version": "1.2.0", + "version": "1.2.1", "description": "explorer API Documentation" }, "swagger": "2.0", @@ -1928,6 +1928,8 @@ }, "Response": { "type": "object", + "description": "Response", + "name": "Item", "properties": { "pages": { "type": "object", @@ -1940,7 +1942,9 @@ "description": "sortable fields", "type": "object" }, - "defaultSort": null, + "defaultSort": { + "type": "object" + }, "sortDir": { "type": "boolean" }, @@ -1967,6 +1971,7 @@ } }, "ResponseList": { + "description": "ResponseList", "type": "object", "properties": { "pages": { @@ -1980,7 +1985,9 @@ "description": "sortable fields", "type": "object" }, - "defaultSort": null, + "defaultSort": { + "type": "object" + }, "sortDir": { "type": "boolean" },