Skip to content

Commit

Permalink
Merge pull request #268 from thanhson1085/master
Browse files Browse the repository at this point in the history
readd signers
  • Loading branch information
thanhson1085 committed Oct 31, 2018
2 parents 73bbb92 + 7f57dde commit 576c134
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { Validator } = require('./models/blockchain/validator')
const { BlockSigner } = require('./models/blockchain/blockSigner')
const chain = require('./models/blockchain/chain')
const config = require('config')
const db = require('./models/mongodb')
const q = require('./queues')
const moment = require('moment')
Expand Down Expand Up @@ -162,6 +163,55 @@ async function getCurrentCandidates () {
}
}

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)
}
}

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)
} catch (e) {
console.error(e)
}
})
} catch (e) {
emitter.emit('error', e)
}
}

async function watchBlockSigner () {
try {
let bs = await BlockSigner.deployed()
Expand Down Expand Up @@ -204,9 +254,12 @@ async function watchBlockSigner () {

watchBlockSigner()

getCurrentCandidates().then(() => {
watchValidator()
watchBlockSigner()
updateSigners(false).then(() => {
return getCurrentCandidates().then(() => {
watchNewBlock()
watchValidator()
watchBlockSigner()
})
}).catch(e => {
console.log('getCurrentCandidates', e)
process.exit(1)
Expand Down

0 comments on commit 576c134

Please sign in to comment.