From ce079ccbffdbd43ccdc28367ecae3c0a7f0ef242 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 29 Oct 2018 11:13:25 +0000 Subject: [PATCH] add crawl block signer containter --- app/components/candidates/List.vue | 6 +-- crawl.js | 80 +++------------------------- crawl_block_signer.js | 84 ++++++++++++++++++++++++++++++ crawl_reward.js | 2 +- package.json | 3 ++ 5 files changed, 99 insertions(+), 76 deletions(-) create mode 100644 crawl_block_signer.js diff --git a/app/components/candidates/List.vue b/app/components/candidates/List.vue index 20632d66..287e03d0 100644 --- a/app/components/candidates/List.vue +++ b/app/components/candidates/List.vue @@ -13,7 +13,7 @@
Current Block

- #{{ chainConfig.blockNumber }} + #{{ chainConfig.blockNumber }}

@@ -25,8 +25,8 @@
-
epoch
-

{{ chainConfig.epoch }} blocks

+
Epoch
+

#{{ chainConfig.blockNumber % chainConfig.epoch }}

diff --git a/crawl.js b/crawl.js index c89401be..8dbf4bd1 100644 --- a/crawl.js +++ b/crawl.js @@ -1,82 +1,14 @@ 'use strict' const { Validator } = require('./models/blockchain/validator') -const { BlockSigner } = require('./models/blockchain/blockSigner') const chain = require('./models/blockchain/chain') const db = require('./models/mongodb') -const config = require('config') const q = require('./queues') const moment = require('moment') const EventEmitter = require('events').EventEmitter const emitter = new EventEmitter() -process.setMaxListeners(200) - -async function watchBlockSigner () { - try { - let bs = await BlockSigner.deployed() - let cs = await db.CrawlState.findOne({ - smartContractAddress: bs.address - }) - let blockNumber = parseInt((cs || {}).blockNumber || 0) + 1 - let epoch = parseInt(config.get('blockchain.epoch')) - let latestBlockNumber = await chain.eth.blockNumber - if (blockNumber < (latestBlockNumber - 3 * epoch)) { - blockNumber = latestBlockNumber - 3 * epoch - } - console.info('BlockSigner %s - Listen events from block number %s ...', bs.address, blockNumber) - const allEvents = bs.allEvents({ - fromBlock: blockNumber, - toBlock: 'latest' - }) - allEvents.watch(async (err, res) => { - if (err || !(res || {}).args) { - console.error(err, res) - return false - } - console.info('BlockSigner - New event %s from block %s', res.event, res.blockNumber) - - try { - await db.CrawlState.updateOne({ - smartContractAddress: bs.address - }, { $set:{ - smartContractAddress: bs.address, - blockNumber: res.blockNumber - } }, { upsert: true }) - - let signer = res.args._signer - let tx = res.transactionHash - let bN = String(res.args._blockNumber) - let bH = String(res.args._blockHash) - - let blk = await chain.eth.getBlock(res.blockNumber) - let createdAt = moment.unix(blk.timestamp).utc() - - return db.BlockSigner.updateOne({ - smartContractAddress: bs.address, - blockHash: bH - }, { - $set: { - smartContractAddress: bs.address, - blockNumber: bN, - blockHash: bH - }, - $addToSet: { - signers: { - signer: signer, - tx: tx, - createdAt: createdAt - } - } - }, { upsert: true }) - } catch (e) { - console.error(e) - } - }) - } catch (e) { - emitter.emit('error', e) - } -} +process.setMaxListeners(100) async function watchValidator () { try { @@ -238,9 +170,13 @@ async function getCurrentCandidates () { } } -getCurrentCandidates() -watchBlockSigner() -watchValidator() +getCurrentCandidates().then(() => { + watchValidator() +}).catch(e => { + console.log('getCurrentCandidates', e) + process.exit(1) +}) + emitter.on('error', e => { console.error('ERROR!!!', e) process.exit(1) diff --git a/crawl_block_signer.js b/crawl_block_signer.js new file mode 100644 index 00000000..61105004 --- /dev/null +++ b/crawl_block_signer.js @@ -0,0 +1,84 @@ +'use strict' + +const { BlockSigner } = require('./models/blockchain/blockSigner') +const chain = require('./models/blockchain/chain') +const db = require('./models/mongodb') +const config = require('config') +const moment = require('moment') +const EventEmitter = require('events').EventEmitter +const emitter = new EventEmitter() + +process.setMaxListeners(100) + +async function watchBlockSigner () { + try { + let bs = await BlockSigner.deployed() + let cs = await db.CrawlState.findOne({ + smartContractAddress: bs.address + }) + let blockNumber = parseInt((cs || {}).blockNumber || 0) + 1 + let epoch = parseInt(config.get('blockchain.epoch')) + let latestBlockNumber = await chain.eth.blockNumber + if (blockNumber < (latestBlockNumber - 1 * epoch)) { + blockNumber = latestBlockNumber - 1 * epoch + } + console.info('BlockSigner %s - Listen events from block number %s ...', bs.address, blockNumber) + const allEvents = bs.allEvents({ + fromBlock: blockNumber, + toBlock: 'latest' + }) + allEvents.watch(async (err, res) => { + if (err || !(res || {}).args) { + console.error(err, res) + return false + } + console.info('BlockSigner - New event %s from block %s', res.event, res.blockNumber) + + try { + await db.CrawlState.updateOne({ + smartContractAddress: bs.address + }, { $set:{ + smartContractAddress: bs.address, + blockNumber: res.blockNumber + } }, { upsert: true }) + + let signer = res.args._signer + let tx = res.transactionHash + let bN = String(res.args._blockNumber) + let bH = String(res.args._blockHash) + + let blk = await chain.eth.getBlock(res.blockNumber) + let createdAt = moment.unix(blk.timestamp).utc() + + return db.BlockSigner.updateOne({ + smartContractAddress: bs.address, + blockHash: bH + }, { + $set: { + smartContractAddress: bs.address, + blockNumber: bN, + blockHash: bH + }, + $addToSet: { + signers: { + signer: signer, + tx: tx, + createdAt: createdAt + } + } + }, { upsert: true }) + } catch (e) { + console.error(e) + } + }) + } catch (e) { + emitter.emit('error', e) + } +} + +watchBlockSigner() + +emitter.on('error', e => { + console.error('ERROR!!!', e) + process.exit(1) +}) diff --git a/crawl_reward.js b/crawl_reward.js index 680f475d..05ee7cd3 100644 --- a/crawl_reward.js +++ b/crawl_reward.js @@ -7,7 +7,7 @@ const q = require('./queues') const EventEmitter = require('events').EventEmitter const emitter = new EventEmitter() -process.setMaxListeners(200) +process.setMaxListeners(100) function watchNewBlock () { try { diff --git a/package.json b/package.json index 11489dab..54749cc8 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,12 @@ "nodemon": "./node_modules/.bin/nodemon index.js", "crawl-dev": "./node_modules/.bin/nodemon ./crawl.js", "crawl-reward-dev": "./node_modules/.bin/nodemon ./crawl_reward.js", + "crawl-block-signer-dev": "./node_modules/.bin/nodemon ./crawl_block_signer.js", "start": "node index.js", "crawl": "node ./crawl.js", "crawl-reward": "node ./crawl_reward.js", + "crawl-block-signer": "node ./crawl_block_signer.js", + "start": "node index.js", "webpack": "node ./node_modules/webpack/bin/webpack.js -d --w", "test": "./node_modules/.bin/truffle test", "dev": "npm run crawl-dev & npm run nodemon & cross-env NODE_ENV=development webpack-dev-server --hot --host 0.0.0.0 --port 3000",