Skip to content

Commit

Permalink
Build 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Feb 5, 2021
1 parent b6d3149 commit 677d346
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 62 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 4 additions & 3 deletions dist/api/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -24,15 +25,15 @@ 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 }));
}

// 404
app.use((req, res, next) => res.status(404).send());
return httpServer;
return { httpServer, app };
};exports.HttpServer = HttpServer;var _default =

HttpServer;exports.default = _default;
4 changes: 2 additions & 2 deletions dist/api/Status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions dist/api/UserEventsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
5 changes: 5 additions & 0 deletions dist/api/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ definitions:

Response:
type: object
description: Response
name: Item
properties:
pages:
type: object
Expand All @@ -90,6 +92,7 @@ definitions:
description: sortable fields
type: object
defaultSort:
type: object
sortDir:
type: boolean
limit:
Expand All @@ -106,6 +109,7 @@ definitions:
type: object

ResponseList:
description: ResponseList
type: object
properties:
pages:
Expand All @@ -118,6 +122,7 @@ definitions:
description: sortable fields
type: object
defaultSort:
type: object
sortDir:
type: boolean
limit:
Expand Down
56 changes: 35 additions & 21 deletions dist/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions dist/api/modules/ContractVerification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 } };
Expand Down
4 changes: 2 additions & 2 deletions dist/api/modules/VerificationResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
32 changes: 17 additions & 15 deletions dist/api/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion dist/lib/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@

{
key: { match: 1 },
name: 'verificationsResultsMatchIndex' }] },
name: 'verificationsResultsMatchIndex' },

{
key: { address: 1, match: 1 },
name: 'verificationResultAddressMatchIndex' }] },



Expand Down
19 changes: 13 additions & 6 deletions dist/services/userEvents/ContractVerifierModule.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 || {});
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsk-explorer-api",
"version": "1.2.0",
"version": "1.2.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 677d346

Please sign in to comment.