Skip to content

Commit

Permalink
Build 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Jul 17, 2020
1 parent 401946a commit 833c02a
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 189 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [1.1.2] - 2020-07-17

### Fixed

- blocksBalances memory leak

### Updated

- blocksBalances: Store record of requested block balances to skip on next sync.

## [1.1.1] - 2020-07-16

### Changed
Expand Down
8 changes: 8 additions & 0 deletions dist/lib/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@



BalancesLog: {
indexes: [
{
key: { blockHash: 1 },
unique: true }] },



BlocksTraces: {
indexes: [
{
Expand Down
3 changes: 2 additions & 1 deletion dist/lib/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ reduce((a, v, i) => {
delayedFields: _delayedFields.default },

blocks: {
blocksQueueSize: 100,
blocksQueueSize: 10,
validateCollections: false,
bcTipSize: 120,
batchRequestSize: 20,
Expand All @@ -74,4 +74,5 @@ reduce((a, v, i) => {
VerificationsResults: 'verificationResults',
InternalTransactions: 'internalTransactions',
Balances: 'balances',
BalancesLog: 'balancesLog',
BlocksTraces: 'blockTraces' } };exports.default = _default;
13 changes: 5 additions & 8 deletions dist/services/classes/BlockBalances.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.BlockBalances = void 0;var _BcThing = require("./BcThing");
var _Address = require("./Address");
var _utils = require("../../lib/utils");

class BlockBalances extends _BcThing.BcThing {
Expand All @@ -13,19 +12,17 @@ class BlockBalances extends _BcThing.BcThing {
this.blockHash = hash;
this.blockNumber = number;
this.timestamp = timestamp;
addresses = [...new Set(addresses)];
this.addresses = addresses.map(address => new _Address.Address(address, { nod3, initConfig, collections, block: number }));
this.addresses = [...new Set(addresses)];
this.balances = undefined;
this.collection = this.collections.Balances;
}
async fetch() {
try {
if (this.balances) return this.balances;
let { addresses, blockHash, blockNumber, timestamp } = this;
let { addresses, blockHash, blockNumber, timestamp, nod3 } = this;
const balances = [];
for (let Addr of addresses) {
let { address } = Addr;
let balance = await Addr.getBalance(blockNumber);
for (let address of addresses) {
let balance = await nod3.eth.getBalance(address, blockNumber);
balance = parseInt(balance) ? balance : 0;
let _created = Date.now();
balances.push({ address, balance, blockHash, blockNumber, timestamp, _created });
Expand All @@ -45,7 +42,7 @@ class BlockBalances extends _BcThing.BcThing {
let balances = await this.fetch();
if (!balances.length) {
let { blockHash, blockNumber } = this;
this.log.warn(`No balances for ${blockHash} / ${blockNumber}`);
this.log.info(`No balances for ${blockHash} / ${blockNumber}`);
return;
}
await this.deleteOldBalances();
Expand Down
40 changes: 31 additions & 9 deletions dist/services/classes/UpdateBlockBalances.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,47 @@ class UpdateBlockBalances extends _BlocksBase.BlocksBase {
confirmations = parseInt(confirmations);
this.confirmations = !isNaN(confirmations) ? confirmations : 120;
this.lastBlock = { number: undefined };
let { Blocks, Balances } = this.collections;
let { Blocks, Balances, BalancesLog } = this.collections;
this.blocksCollection = Blocks;
this.balancesCollection = Balances;
this.balancesLogCollection = BalancesLog;
this.missing = undefined;
this.started = undefined;
}

async updateLogBalance(blockHash) {
try {
if (!(0, _utils.isBlockHash)(blockHash)) throw new Error(`Invalid blockHash: ${blockHash}`);
let _created = Date.now();
let result = await this.balancesLogCollection.insertOne({ blockHash, _created });
return result;
} catch (err) {
if (err.code === 11000) return Promise.resolve();else
return Promise.reject(err);
}
}
getLogBalance(blockHash) {
return this.balancesLogCollection.findOne({ blockHash });
}

async updateBalance(blockHash) {
try {
let isChecked = await this.getLogBalance(blockHash);
if (isChecked) {
this.log.debug(`Block balance ${blockHash} skipped`);
return;
}
let { nod3, log, collections, initConfig } = this;
let summary = await (0, _BlockSummary.getBlockSummaryFromDb)(blockHash, collections);
if (!summary) throw new Error(`Missing block summary: ${blockHash}`);
const { block, internalTransactions } = summary.data;
const { number } = block;
const addresses = (0, _InternalTx.filterValueAddresses)(internalTransactions);
let blockBalances = new _BlockBalances.BlockBalances({ block, addresses }, { nod3, log, collections, initConfig });
let result = await blockBalances.save();
blockBalances = undefined;
await this.updateLogBalance(blockHash);
this.log.info(`The balances of block: ${blockHash}/${number} were updated`);
return result;
} catch (err) {
this.log.error(`Error updating balances of ${blockHash}`);
Expand All @@ -44,11 +68,11 @@ class UpdateBlockBalances extends _BlocksBase.BlocksBase {

async updateLastBlock(block, skipStart = false) {
try {
const { lastBlock } = this;
const { lastBlock, confirmations } = this;
const { number, hash } = block;
if (isNaN(parseInt(number))) throw new Error(`Invalid block number: ${number}`);
if (!(0, _utils.isBlockHash)(hash)) throw new Error(`invalid block hash: ${hash}`);
if (!lastBlock.number || number > lastBlock.number) {
if (!lastBlock.number || number > lastBlock.number + confirmations) {
this.log.info(`Last block ${number}/${hash}`);
this.lastBlock = block;
let missing = await this.createMissingBalances();
Expand All @@ -72,7 +96,6 @@ class UpdateBlockBalances extends _BlocksBase.BlocksBase {
let { hash, number } = next;
this.log.info(`Updating balances for block ${hash} / ${number}`);
await this.updateBalance(hash);
this.log.info(`The balances of block: ${hash}/${number} were updated`);
return this.getNextBalances();
} catch (err) {
this.started = undefined;
Expand All @@ -86,7 +109,7 @@ class UpdateBlockBalances extends _BlocksBase.BlocksBase {
if (!this.emit) throw new Error('Set emitter before start');
let { blocksCollection } = this;
let lastBlock = await getLastBlock(blocksCollection);
if (lastBlock) await this.updateLastBlock(lastBlock);
if (lastBlock) await this.updateLastBlock(lastBlock, true);
this.started = this.getNextBalances();
return this.started;
} catch (err) {
Expand All @@ -110,17 +133,16 @@ async function MissingBalances(blocksCollection, balancesCollection, { highestBl
let currentBlock = highestBlock;
let block;
const current = () => currentBlock;
const query = { number: { $lt: highestBlock, $gt: lowestBlock - 1 } };
const cursor = blocksCollection.find(query, { projection, sort });
const next = async () => {
if (currentBlock <= lowestBlock) return;
const query = { number: { $lte: --currentBlock, $gte: lowestBlock } };
const cursor = blocksCollection.find(query, { projection, sort });

while (await cursor.hasNext()) {
block = await cursor.next();
let { hash: blockHash, number } = block;
let balance = await balancesCollection.findOne({ blockHash });

currentBlock = number;
if (currentBlock < lowestBlock) currentBlock = lowestBlock;
if (!balance) break;
}
return block;
Expand Down
Loading

0 comments on commit 833c02a

Please sign in to comment.