diff --git a/crawl.js b/crawl.js index 84eda47a..c89401be 100644 --- a/crawl.js +++ b/crawl.js @@ -155,61 +155,6 @@ async function watchValidator () { } } -function watchNewBlock () { - try { - chain.eth.filter('latest').watch(async (err, block) => { - if (err) { - emitter.emit('error', err) - } - try { - let blk = await chain.eth.getBlock('latest') - await updateSigners(blk) - let epoch = parseInt(config.get('blockchain.epoch')) - let blockNumber = blk.number - if (blockNumber % epoch === 0) { - q.create('reward', { block: blk }) - .priority('high').removeOnComplete(true).save() - } - } catch (e) { - console.error(e) - } - }) - } catch (e) { - emitter.emit('error', e) - } -} - -async function updateSigners (blk) { - try { - if (!blk) { - let latestBlockNumber = await chain.eth.blockNumber - let lastCheckpoint = latestBlockNumber - (latestBlockNumber % parseInt(config.get('blockchain.epoch'))) - if (lastCheckpoint > 0) { - blk = await chain.eth.getBlock(lastCheckpoint) - } else { - return false - } - } - let buff = Buffer.from(blk.extraData.substring(2), 'hex') - let sbuff = buff.slice(32, buff.length - 65) - let signers = [] - if (sbuff.length > 0) { - for (let i = 1; i <= sbuff.length / 20; i++) { - let address = sbuff.slice((i - 1) * 20, i * 20) - signers.push('0x' + address.toString('hex')) - } - await db.Signer.create({ - networkId: config.get('blockchain.networkId'), - blockNumber: blk.number, - signers: signers - }) - } - return signers - } catch (e) { - console.error(e) - } -} - async function updateCandidateInfo (candidate) { try { let validator = await Validator.deployed() @@ -294,8 +239,6 @@ async function getCurrentCandidates () { } getCurrentCandidates() -updateSigners(false) -watchNewBlock() watchBlockSigner() watchValidator() emitter.on('error', e => { diff --git a/crawl_reward.js b/crawl_reward.js new file mode 100644 index 00000000..680f475d --- /dev/null +++ b/crawl_reward.js @@ -0,0 +1,73 @@ +'use strict' + +const chain = require('./models/blockchain/chain') +const db = require('./models/mongodb') +const config = require('config') +const q = require('./queues') +const EventEmitter = require('events').EventEmitter +const emitter = new EventEmitter() + +process.setMaxListeners(200) + +function watchNewBlock () { + try { + chain.eth.filter('latest').watch(async (err, block) => { + if (err) { + emitter.emit('error', err) + } + try { + let blk = await chain.eth.getBlock('latest') + await updateSigners(blk) + let epoch = parseInt(config.get('blockchain.epoch')) + let blockNumber = blk.number + console.log('Get latest block', blockNumber) + if (blockNumber % epoch === 0) { + q.create('reward', { block: blk }) + .priority('high').removeOnComplete(true).save() + } + } catch (e) { + console.error(e) + } + }) + } catch (e) { + emitter.emit('error', e) + } +} + +async function updateSigners (blk) { + try { + if (!blk) { + let latestBlockNumber = await chain.eth.blockNumber + let lastCheckpoint = latestBlockNumber - (latestBlockNumber % parseInt(config.get('blockchain.epoch'))) + if (lastCheckpoint > 0) { + blk = await chain.eth.getBlock(lastCheckpoint) + } else { + return false + } + } + let buff = Buffer.from(blk.extraData.substring(2), 'hex') + let sbuff = buff.slice(32, buff.length - 65) + let signers = [] + if (sbuff.length > 0) { + for (let i = 1; i <= sbuff.length / 20; i++) { + let address = sbuff.slice((i - 1) * 20, i * 20) + signers.push('0x' + address.toString('hex')) + } + await db.Signer.create({ + networkId: config.get('blockchain.networkId'), + blockNumber: blk.number, + signers: signers + }) + } + return signers + } catch (e) { + console.error(e) + } +} + +updateSigners(false) +watchNewBlock() +emitter.on('error', e => { + console.error('ERROR!!!', e) + process.exit(1) +}) diff --git a/package.json b/package.json index dba4a603..11489dab 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,10 @@ "lint-fix": "eslint --cache ./ --ext .vue --ext .js --fix", "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", "start": "node index.js", "crawl": "node ./crawl.js", + "crawl-reward": "node ./crawl_reward.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",