Skip to content

Commit

Permalink
add crawl block signer containter
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhson1085 committed Oct 29, 2018
1 parent 38aefda commit ce079cc
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 76 deletions.
6 changes: 3 additions & 3 deletions app/components/candidates/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<b-card class="tomo-card">
<h6 class="tomo-card__title">Current Block</h6>
<p class="tomo-card__text">
<router-link :to="'/blocksigners'">#{{ chainConfig.blockNumber }}</router-link>
#{{ chainConfig.blockNumber }}
</p>
</b-card>
</div>
Expand All @@ -25,8 +25,8 @@
</div>
<div class="col-md-6 col-lg-3">
<b-card class="tomo-card tomo-card">
<h6 class="tomo-card__title">epoch</h6>
<p class="tomo-card__text">{{ chainConfig.epoch }} blocks</p>
<h6 class="tomo-card__title">Epoch</h6>
<p class="tomo-card__text">#{{ chainConfig.blockNumber % chainConfig.epoch }}</p>
</b-card>
</div>
<div class="col-md-6 col-lg-3">
Expand Down
80 changes: 8 additions & 72 deletions crawl.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions crawl_block_signer.js
Original file line number Diff line number Diff line change
@@ -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)
})
2 changes: 1 addition & 1 deletion crawl_reward.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ce079cc

Please sign in to comment.