Skip to content

Commit

Permalink
Build 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Feb 28, 2020
1 parent 8d0f50a commit 0c97bf2
Show file tree
Hide file tree
Showing 68 changed files with 895 additions and 5,771 deletions.
106 changes: 61 additions & 45 deletions dist/api/Api.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _DataCollector = require("./lib/DataCollector");
var _modules = require("./modules");
var _types = require("../lib/types");
var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));
var _blocksCollections = require("../lib/blocksCollections");
var _apiTools = require("./lib/apiTools");
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
var _config = _interopRequireDefault(require("../lib/config"));
var _NativeContracts = _interopRequireDefault(require("../lib/NativeContracts"));

var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} // It is used only in case Stats cannot provide the circulating supply

class Api extends _DataCollector.DataCollector {
constructor({ db, initConfig, nativeContracts }, { modules, collectionsNames, lastBlocks } = {}) {
constructor({ db, initConfig }, { modules, collectionsNames, lastBlocks } = {}) {
const collectionName = collectionsNames.Blocks;
super(db, { collectionName });
this.collectionsNames = collectionsNames;
this.collections = (0, _blocksCollections.getDbBlocksCollections)(db);
this.lastLimit = lastBlocks || 10;
this.latest = 0;
this.lastBlocks = [];
this.lastTransactions = [];
this.lastLimit = lastBlocks || 100;
this.latest = undefined;
this.lastBlocks = { data: [] };
this.lastTransactions = { data: [] };
this.circulatingSupply = null;
this.stats = { timestamp: 0 };
this.loadModules((0, _modules.getEnabledApiModules)(modules));
this.initConfig = initConfig;
const { isNativeContract } = nativeContracts;
const { isNativeContract } = (0, _NativeContracts.default)(initConfig);
this.isNativeContract = isNativeContract;
this.tick();
}
tick() {
this.setLastBlocks();
this.setCirculatingSupply();
}

loadModules(modules) {
Object.keys(modules).forEach(name => {
const module = new modules[name](this.collections, name);
this.log.info(`Loading module ${name}`);
this.addModule(module, name);
const constructor = modules[name];
if (typeof constructor === 'function') {
const module = new constructor(this.collections, name);
this.log.info(`Loading module ${name}`);
this.addModule(module, name);
}
});
}

Expand Down Expand Up @@ -67,51 +72,48 @@ class Api extends _DataCollector.DataCollector {

async setLastBlocks() {
try {
let { collection, lastLimit } = this;
const Block = this.getModule('Block');
const Tx = this.getModule('Tx');
let blocks = await collection.find().sort({ number: -1 }).limit(lastLimit).toArray();
let txs = await Tx.db.find({ txType: { $in: [_types.txTypes.default, _types.txTypes.contract] } }).
sort({ blockNumber: -1, transactionIndex: -1 }).
limit(this.lastLimit).
toArray();

this.updateLastBlocks(blocks, txs);
let limit = this.lastLimit;
let blocks = await Block.run('getBlocks', { limit, addMetadata: true });
let query = { txType: [_types.txTypes.default, _types.txTypes.contract] };
let transactions = await Tx.run('getTransactions', { query, limit });
this.updateLastBlocks(blocks, transactions);
} catch (err) {
this.log.debug(err);
}
}

async setCirculatingSupply() {
try {
const collection = this.collections.Addrs;
let circulating = await (0, _getCirculatingSupply.default)(collection, this.initConfig.nativeContracts);
this.circulatingSupply = Object.assign({}, circulating);
} catch (err) {
this.log.debug(err);
}
getStats() {
return this.formatData(this.stats);
}
getCirculatingSupply() {
return this.formatData(this.circulatingSupply);
}

getLastBlocks() {
let blocks = this.lastBlocks;
let transactions = this.lastTransactions;
return this.formatData({ blocks, transactions });
let data = this.lastBlocks;
return this.formatData(data);
}

getLastTransactions() {
let data = this.lastTransactions;
return this.formatData(data);
}

getLastBlock() {
return this.lastBlocks[0] || null;
let { data } = this.lastBlocks;
return data[0] || null;
}

updateLastBlocks(blocks, transactions) {
let blockData = blocks.data;
this.lastBlocks = blocks;
this.lastTransactions = transactions;
let latest;
if (blocks && blocks[0]) latest = blocks[0].number;
if (blockData && blockData[0]) latest = blockData[0].number;
if (latest !== this.latest) {
this.latest = latest;
this.events.emit('newBlocks', this.formatData({ blocks, transactions }));
this.events.emit('newBlocks', this.getLastBlocks());
this.updateStats();
}
}
Expand All @@ -132,19 +134,33 @@ class Api extends _DataCollector.DataCollector {

async updateStats() {
const oldStats = this.stats;
const stats = await this.getModule('Stats').run('getLatest');
const Stats = await this.getModule('Stats');
if (!Stats) return;
const stats = await Stats.run('getLatest');
if (!stats) return;

const ExtendedStats = this.getModule('ExtendedStats');
if (ExtendedStats) {
const blockNumber = parseInt(stats.blockNumber);
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber);
Object.assign(stats, extendedStats);
}

/* const ExtendedStats = this.getModule('ExtendedStats')
if (ExtendedStats) {
const blockNumber = parseInt(stats.blockNumber)
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber)
Object.assign(stats, extendedStats)
} */
let circulatingSupply = stats.circulatingSupply || (await this.getCirculatingSupplyFromDb());
this.circulatingSupply = circulatingSupply;
this.stats = Object.assign({}, stats);
if (stats.timestamp !== oldStats.timestamp) {
this.events.emit('newStats', this.stats);
let timestamp = stats.timestamp || 0;
if (timestamp > oldStats.timestamp) {
this.events.emit('newStats', this.getStats());
}
}
async getCirculatingSupplyFromDb() {
try {
const collection = this.collections.Addrs;
const { nativeContracts } = this.initConfig;
let circulating = await (0, _getCirculatingSupply.default)(collection, nativeContracts);
return circulating;
} catch (err) {
this.log.debug(err);
}
}}var _default =

Expand Down
8 changes: 6 additions & 2 deletions dist/api/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ const HttpServer = ({ api, status, log }) => {
app.set('etag', false);
app.set('x-powered-by', false);

// status
app.get('/status', (req, res) => {
const data = status.getState().data;
res.send(data);
});

app.get('/circulating', (req, res) => {
const data = api.getCirculatingSupply().data;
// circulating supply
app.get('/circulating/:field?', (req, res) => {
let { field } = req.params;
let { data } = api.getCirculatingSupply();
data = field ? `${data[field]}` : data;
res.send(data);
});

Expand Down
12 changes: 6 additions & 6 deletions dist/api/Status.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.Status = void 0;var _DataCollector = require("./lib/DataCollector/");
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
const { collectionsNames } = _config.default;
var _blocksCollections = require("../lib/blocksCollections");

class Status extends _DataCollector.DataCollector {
constructor(db) {
const collectionName = collectionsNames.Status;
super(db, { collectionName });
const collections = (0, _blocksCollections.getDbBlocksCollections)(db);
const { Status, Blocks } = collections;
super(db, { collectionName: 'Status' });
this.tickDelay = 5000;
this.state = {};
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Status), 'Status'));
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Blocks), 'Blocks'));
this.addModule(new _DataCollector.DataCollectorItem(Status, 'Status'));
this.addModule(new _DataCollector.DataCollectorItem(Blocks, 'Blocks'));
}
tick() {
this.updateState().then(newState => {
Expand Down
1 change: 1 addition & 0 deletions dist/api/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _Channel = _interopRequireDefault(require("./lib/Channel"));function _intero

const CHANNELS = {
blocksChannel: 'blocks',
txsChannel: 'transactions',
statusChannel: 'status',
txPoolChannel: 'txpool',
statsChannel: 'stats' };exports.CHANNELS = CHANNELS;
Expand Down
8 changes: 8 additions & 0 deletions dist/api/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ definitions:
type: integer
next:
type: string
fields:
type:
oneOf:
- array
- object




Response:
type: object
Expand Down
18 changes: 14 additions & 4 deletions dist/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ var _evaluateError = require("./lib/evaluateError");function _interopRequireDefa
const port = _config.default.api.port || '3003';
const address = _config.default.api.address || 'localhost';

(0, _dataSource.setup)({ log: _log.default, skipCheck: true }).then(({ db, initConfig, nativeContracts }) => {
(0, _dataSource.setup)({ log: _log.default, skipCheck: true }).then(({ db, initConfig }) => {
_log.default.info('Database connected');

// data collectors
const api = new _Api.default({ db, initConfig, nativeContracts }, _config.default.api);
const api = new _Api.default({ db, initConfig }, _config.default.api);
const status = new _Status.default(db);
const txPool = new _TxPool.default(db);
api.start();
Expand All @@ -39,7 +39,7 @@ const address = _config.default.api.address || 'localhost';

// create channels
const channels = (0, _channels.createChannels)(io);
const { blocksChannel, statusChannel, txPoolChannel, statsChannel } = channels.channels;
const { blocksChannel, statusChannel, txPoolChannel, statsChannel, txsChannel } = channels.channels;

// send blocks on join
blocksChannel.on('join', socket => {
Expand All @@ -57,9 +57,19 @@ const address = _config.default.api.address || 'localhost';
socket.emit('data', (0, _apiTools.formatRes)({ action: 'txPoolChart', result: txPool.getPoolChart() }));
});

// send new blocks to channel
// send transactions on join
txsChannel.on('join', socket => {
socket.emit('data', (0, _apiTools.formatRes)({ action: 'newTransactions', result: api.getLastTransactions() }));
});
// send new blocks & transactions to channels
api.events.on('newBlocks', result => {
blocksChannel.emit('newBlocks', result);
txsChannel.emit('newTransactions', api.getLastTransactions());
});

// send stats on join
statsChannel.on('join', socket => {
socket.emit('data', (0, _apiTools.formatRes)({ action: 'stats', result: api.getStats() }));
});

// send status to channel
Expand Down
4 changes: 2 additions & 2 deletions dist/api/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ function Channel(channelName, io) {
}
};

const confirmSubsctption = socket => {
const confirmSubscription = socket => {
socket.emit('subscription', { channel: channelName });
};
const join = socket => {
channelEvent('join', socket);
confirmSubsctption(socket);
confirmSubscription(socket);
};

const leave = socket => {
Expand Down
2 changes: 1 addition & 1 deletion dist/api/lib/DataCollector/DataCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DataCollector {
}

formatData(data) {
return { data: data };
return { data };
}}exports.DataCollector = DataCollector;var _default =


Expand Down
25 changes: 18 additions & 7 deletions dist/api/lib/DataCollector/DataCollectorItem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.formatResponse = formatResponse;exports.getFieldsTypes = getFieldsTypes;exports.filterSort = filterSort;exports.fieldFilterParse = fieldFilterParse;exports.default = exports.DataCollectorItem = void 0;var _mongodb = require("mongodb");
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.formatResponse = formatResponse;exports.getFieldsTypes = getFieldsTypes;exports.filterSort = filterSort;exports.fieldFilterParse = fieldFilterParse;exports.getCursorData = getCursorData;exports.default = exports.DataCollectorItem = void 0;var _mongodb = require("mongodb");
var _pagination = require("./pagination");
var _types = require("../../../lib/types");
class DataCollectorItem {
Expand Down Expand Up @@ -37,6 +37,12 @@ class DataCollectorItem {
}
}

async count(query) {
let collection = this.db;
let data = await (0, _pagination.countDocuments)(collection, query);
return { data };
}

async find(query, sort, limit, project) {
let collection = this.db;
project = project || this.getDefaultsFields();
Expand Down Expand Up @@ -84,10 +90,9 @@ class DataCollectorItem {
}

async setCursorData() {
const cursorField = this.cursorField;
let { cursorField } = this;
const types = await this.getFieldsTypes();
const cursorType = types[cursorField];
this.cursorData = { cursorField, cursorType, fields: types };
this.cursorData = await getCursorData(this.db, cursorField, types);
return this.cursorData;
}

Expand All @@ -101,15 +106,15 @@ class DataCollectorItem {

async getPrevNext(query, project, data) {
try {
let { cursorField } = this;
let { cursorField, db } = this;
project = project || this.getDefaultsFields();
if (!data) data = await this.getOne(query);
if (data) data = data.data;
if (!data) return;
let value = query[cursorField] || data[cursorField];
if (undefined === value) throw new Error(`Missing ${cursorField} value`);
let prev = (await (0, _pagination.find)(this.db, { [cursorField]: { $lt: value } }, { [cursorField]: -1 }, 1, project))[0];
let next = (await (0, _pagination.find)(this.db, { [cursorField]: { $gt: value } }, { [cursorField]: 1 }, 1, project))[0];
let prev = (await (0, _pagination.find)(db, { [cursorField]: { $lt: value } }, { [cursorField]: -1 }, 1, project))[0];
let next = (await (0, _pagination.find)(db, { [cursorField]: { $gt: value } }, { [cursorField]: 1 }, 1, project))[0];
return { prev, data, next };
} catch (err) {
return Promise.reject(err);
Expand Down Expand Up @@ -185,6 +190,12 @@ function fieldFilterParse(field, value, query) {
if (ninArr.length) fieldQuery['$nin'] = ninArr;
if (fieldQuery) query[field] = fieldQuery;
return query;
}

async function getCursorData(collection, cursorField, types) {
types = types || (await getFieldsTypes(collection));
const cursorType = types[cursorField];
return { cursorField, cursorType, fields: types };
}var _default =

DataCollectorItem;exports.default = _default;
14 changes: 12 additions & 2 deletions dist/api/lib/DataCollector/pagination.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.generateCursorQuery = generateCursorQuery;exports.formatSearchValue = formatSearchValue;exports.generateSort = generateSort;exports.generateQuery = generateQuery;exports.parseParams = parseParams;exports.findPages = findPages;exports.aggregatePages = aggregatePages;exports.modifyAggregate = modifyAggregate;exports.getAggregateTotal = getAggregateTotal;exports.paginationResponse = paginationResponse;exports.find = find;exports.encodeValue = encodeValue;exports.generateCursor = generateCursor;var _types = require("../../../lib/types");
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.countDocuments = countDocuments;exports.generateCursorQuery = generateCursorQuery;exports.formatSearchValue = formatSearchValue;exports.generateSort = generateSort;exports.generateQuery = generateQuery;exports.parseParams = parseParams;exports.findPages = findPages;exports.aggregatePages = aggregatePages;exports.modifyAggregate = modifyAggregate;exports.getAggregateTotal = getAggregateTotal;exports.paginationResponse = paginationResponse;exports.find = find;exports.encodeValue = encodeValue;exports.generateCursor = generateCursor;var _types = require("../../../lib/types");
var _mongodb = require("mongodb");
var _config = _interopRequireDefault(require("../../../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
const { MAX_LIMIT, MAX_PAGES } = _config.default.api;
const SEPARATOR = '__';

async function countDocuments(collection, query) {
query = query || {};
try {
let result = await collection.countDocuments(query);
return result;
} catch (err) {
return Promise.reject(err);
}
}

function generateCursorQuery({ cursorField, sortDir, value, sortField }) {
if (!value) return;
const op = sortDir === -1 ? '$lt' : '$gt';
Expand Down Expand Up @@ -113,7 +123,7 @@ async function findPages(collection, cursorData, query, params) {
const $query = generateQuery(params, query);
const $sort = generateSort(params);
let data = !countOnly ? await find(collection, $query, $sort, queryLimit + 1, fields) : null;
let total = count ? await collection.countDocuments(query) : null;
let total = count ? await countDocuments(collection, query) : null;
return paginationResponse(params, data, total);
} catch (err) {
return Promise.reject(err);
Expand Down
Loading

0 comments on commit 0c97bf2

Please sign in to comment.