Skip to content

Commit

Permalink
Merge pull request #258 from thanhson1085/master
Browse files Browse the repository at this point in the history
separate to new container
  • Loading branch information
thanhson1085 authored Oct 29, 2018
2 parents 893daf7 + 6b6161b commit 38aefda
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
57 changes: 0 additions & 57 deletions crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -294,8 +239,6 @@ async function getCurrentCandidates () {
}

getCurrentCandidates()
updateSigners(false)
watchNewBlock()
watchBlockSigner()
watchValidator()
emitter.on('error', e => {
Expand Down
73 changes: 73 additions & 0 deletions crawl_reward.js
Original file line number Diff line number Diff line change
@@ -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)
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 38aefda

Please sign in to comment.