Skip to content

Commit

Permalink
Update util/hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Nov 21, 2015
1 parent 89925a3 commit 4bdd54d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 83 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"karma-firefox-launcher": "^0.1.6",
"karma-mocha": "^0.2.0",
"mocha": "^2.2.5",
"optimist": "^0.6.1",
"pg": "^4.4.3",
"progress": "^1.1.8",
"sqlite3": "^3.1.1",
Expand Down
1 change: 0 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash'
import { createHash } from 'crypto'
import bitcore from 'bitcore-lib'
import request from 'request'
import io from 'socket.io-client'
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ describe('blockchainjs', () => {
// blockchain
require('./blockchain/blockchain')
require('./blockchain/naive')
require('./blockchain/verified')
// require('./blockchain/verified')
})
132 changes: 52 additions & 80 deletions util/hashes.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,94 +1,66 @@
#!/usr/bin/env node
'use strict'
import { writeFileSync } from 'fs'
import _ from 'lodash'
import ProgressBar from 'progress'
import bitcore from 'bitcore-lib'

var fs = require('fs')
import Chromanode from '../src/network/chromanode'
import { sha256x2, hashEncode } from '../src/util/crypto'

var _ = require('lodash')
var ProgressBar = require('progress')
var Promise = require('bluebird')
// network
let networkName = process.env.NETWORK || 'livenet'
let networkObj = bitcore.Networks.get(networkName)
if (networkObj === undefined || networkObj.name !== networkName) {
throw new Error(`Network ${networkName} not allowed.`)
}

var blockchainjs = require('../')
var Chromanode = blockchainjs.connector.Chromanode
var util = blockchainjs.util
// output
let output = process.env.OUTPUT
if (/\.js$/.test(output) === false) {
throw new Error('Output file must have js extension')
}
writeFileSync(output, '')

var optimist = require('optimist')
.usage('Usage: $0 [-h] [-n NETWORK]')
.options('n', {
alias: 'network',
describe: 'cryptocurrency network',
default: 'livenet'
})
.check(function (argv) {
var availableNetworks = [
'livenet',
'testnet'
]
// chromanode url
let URL = process.env.CHROMANODE_URL || Chromanode.getSources(networkName)[0]
console.log(`URL: ${URL}`)

if (availableNetworks.indexOf(argv.network) === -1) {
var msg = 'Network ' + argv.network + ' not allowed. You can use only: ' + availableNetworks.join(', ')
throw new Error(msg)
}
})
.options('o', {
alias: 'out',
describe: 'outpuf js file'
})
.check(function (argv) {
if (/\.js$/.test(argv.out) === false) {
throw new Error('Output file must have js extension')
}
// start
let network = new Chromanode({url: URL, concurrency: 3})
network.on('error', err => console.error(err.stack))
network.on('connect', async () => {
console.log('Connected!')

fs.writeFileSync(argv.out, '')
})
.options('h', {
alias: 'help',
describe: 'show this help',
default: false
})
try {
let latest = await network.getHeader('latest')
let chunksTotal = Math.floor(latest.height / 2016)

var argv = optimist.argv
if (argv.help) {
optimist.showHelp()
process.exit(0)
}
let bar = new ProgressBar(
'Progress: :percent (:current/:total), :elapseds elapsed, eta :etas',
{total: chunksTotal})

var connector = new Chromanode({networkName: argv.network, requestTimeout: 30000})
new Promise(function (resolve) { connector.once('connect', resolve) })
.then(function () { return connector.getHeader('latest') })
.then(function (header) {
var height = header.height
var chunksTotal = Math.floor(height / 2016)
var barFmt = 'Progress: :percent (:current/:total), :elapseds elapsed, eta :etas'
var bar = new ProgressBar(barFmt, {total: chunksTotal})
let data = {lastBlockHash: null}
data.hashes = await* _.range(chunksTotal).map(async (chunkIndex) => {
let headers = await network.getHeaders(chunkIndex === 0 ? null : (chunkIndex * 2016 - 1))

var lastBlockHash
Promise.map(_.range(chunksTotal), function (chunkIndex) {
return connector.headersQuery(chunkIndex * 2016, {count: 2016})
.then(function (result) {
var rawChunk = new Buffer(result.headers, 'hex')
if (chunkIndex + 1 === chunksTotal) {
data.lastBlockHash = hashEncode(sha256x2(new Buffer(headers.slice(-160), 'hex')))
}

if (chunkIndex === chunksTotal - 1) {
lastBlockHash = util.hashEncode(util.sha256x2(rawChunk.slice(-80)))
}
bar.tick()
return sha256x2(new Buffer(headers, 'hex')).toString('hex')
})

bar.tick()
return util.hashEncode(util.sha256x2(rawChunk))
})
}, {concurrency: 3})
.finally(function () {
connector.disconnect()
})
.then(function (hashes) {
var data = {lastBlockHash: lastBlockHash, chunkHashes: hashes}
var content = [
'// Network: ' + argv.network,
'// ' + new Date().toUTCString(),
'module.exports = ' + JSON.stringify(data, null, 2).replace(/"/g, '\'')
].join('\n') + '\n'
network.disconnect()

writeFileSync(output, `// Network: ${networkName}
// ${new Date().toUTCString()}
module.exports = ${JSON.stringify(data, null, 2).replace(/"/g, '\'')}
`)

fs.writeFileSync(argv.out, content)
process.exit(0)
})
} catch (err) {
console.error(err.stack)
process.exit(1)
}
})

connector.connect()
network.connect()

0 comments on commit 4bdd54d

Please sign in to comment.