Skip to content

Commit

Permalink
speedup verified blockchain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Jun 21, 2015
1 parent ae03348 commit a85339d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/blockchain/naive.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Naive (connector, opts) {
self.networkName, connector.networkName)
}

self.connector.on('newBlock', self._syncBlockchain.bind(self))
self.connector.on('newBlock', self._syncLatest.bind(self))
self.connector.subscribe({event: 'newBlock'})

self.connector.on('touchAddress', function (address, txid) {
Expand Down
9 changes: 6 additions & 3 deletions lib/blockchain/verified.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ function Verified (connector, opts) {
})
prevHeight = newHeight

// sync blockchain
self._syncBlockchain(newHash, newHeight)
.catch(function (err) { self.emit('error', err) })
// sync blockchain with latest header
self._syncLatest()
})

self.connector.subscribe({event: 'newBlock'})
Expand Down Expand Up @@ -358,6 +357,10 @@ Verified.prototype._sync = function (networkHash, networkHeight) {
self.connector.headersQuery(index * 2016, {count: 2016})
.then(function (res) {
var chunkHex = res.headers
if (index === networkIndex) {
chunkHex = chunkHex.slice(0, 160 * (networkHeight % 2016))
}

var prevHeaderPromise = index === 0
? {hash: util.ZERO_HASH} // special case for zero header
: cachedGetHeader(index * 2016 - 1)
Expand Down
3 changes: 2 additions & 1 deletion lib/storage/abstractsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ AbstractBlockchainSyncStorage.prototype.putHeaders = function (headers) {
rawChunk = rawChunk.slice(0, shift * 160)

while (shift < 2016 && data.headers < totalHeaders) {
rawChunk += headers[data.headers++]
rawChunk += headers[data.headers % 2016]
data.headers += 1
}

return self._storage.set('hc-' + chunk, rawChunk)
Expand Down
10 changes: 5 additions & 5 deletions test/blockchain/naive.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('blockchain.Naive', function () {
var expected = {hash: blockchainjs.util.ZERO_HASH, height: -1}
expect(blockchain.latest).to.deep.equal(expected)
blockchain.once('newBlock', function () {
expect(blockchain.latest.height).to.at.least(300000)
expect(blockchain.latest.height).to.at.least(480000)
done()
})
})
Expand All @@ -72,10 +72,10 @@ describe('blockchain.Naive', function () {
.done(done, done)
})

it('getHeader 300000 by id', function (done) {
blockchain.getHeader(fixtures.headers[300000].hash)
it('getHeader 30000 by id', function (done) {
blockchain.getHeader(fixtures.headers[30000].hash)
.then(function (header) {
expect(header).to.deep.equal(fixtures.headers[300000])
expect(header).to.deep.equal(fixtures.headers[30000])
})
.done(done, done)
})
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('blockchain.Naive', function () {
expect(res).to.be.an('object')
expect(res.transactions).to.deep.equal(fixture.transactions)
expect(res.latest).to.be.an('object')
expect(res.latest.height).to.be.at.least(300000)
expect(res.latest.height).to.be.at.least(480000)
expect(res.latest.hash).to.have.length(64)
})
.done(done, done)
Expand Down
92 changes: 60 additions & 32 deletions test/blockchain/verified.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it, afterEach, beforeEach */
/* global describe, xdescribe, it, afterEach, beforeEach */
'use strict'

var _ = require('lodash')
Expand All @@ -18,19 +18,26 @@ describe('blockchain.Verified', function () {
var blockchain
var timeoutId

function createBeforeEachFunction (Storage, storageOpts, blockchainOpts) {
/**
* @param {function} Storage
* @param {Object} storageOpts
* @param {Object} blockchainOpts
* @param {Object} opts
* @return {function}
*/
function createBeforeEachFunction (Storage, storageOpts, blockchainOpts, opts) {
return function (done) {
connector = new blockchainjs.connector.Chromanode({networkName: 'testnet'})
connector.on('error', helpers.ignoreConnectorErrors)

storage = new Storage(storageOpts)

var opts = _.extend({
blockchainOpts = _.extend({
storage: storage,
networkName: 'testnet',
testnet: true
}, blockchainOpts)
blockchain = new blockchainjs.blockchain.Verified(connector, opts)
blockchain = new blockchainjs.blockchain.Verified(connector, blockchainOpts)
blockchain.on('error', helpers.ignoreConnectorErrors)

// for using syncThroughHeaders in syncing process
Expand All @@ -40,18 +47,33 @@ describe('blockchain.Verified', function () {
return getHeader.call(connector, id)
}

if (!opts.fullChain) {
return getHeader.call(connector, 30010)
}

return getHeader.call(connector, 'latest')
.then(function (lastHeader) {
return getHeader.call(connector, lastHeader.height - 10)
})
}

timeoutId = setTimeout(function () {
connector.getHeader = getHeader.bind(connector)

if (!opts.fullChain) {
connector.getHeader = function (id) {
if (id !== 'latest') {
return getHeader.call(connector, id)
}

return getHeader.call(connector, 30020)
}
}

connector.getHeader('latest')
.then(function (header) {
.done(function (header) {
connector.emit('newBlock', header.hash, header.height)
})
.catch(function () {})
}, _.noop)
}, 2500)

connector.once('connect', done)
Expand All @@ -68,7 +90,9 @@ describe('blockchain.Verified', function () {
blockchain.removeAllListeners()
blockchain.on('error', function () {})

connector = storage = blockchain = null
connector = null
storage = null
blockchain = null

done()
})
Expand All @@ -94,7 +118,7 @@ describe('blockchain.Verified', function () {
}
}

Object.getPrototypeOf(connector).getHeader.call(connector, 'latest')
connector.getHeader('latest')
.then(function (header) {
var bar = new ProgressBar(barFmt, {
total: header.height,
Expand All @@ -120,14 +144,14 @@ describe('blockchain.Verified', function () {
})
})
.then(function () {
return blockchain.getHeader(fixtures.headers[300000].height)
return blockchain.getHeader(fixtures.headers[30000].height)
})
.then(function (header) {
expect(header).to.deep.equal(fixtures.headers[300000])
return blockchain.getHeader(fixtures.headers[300000].hash)
expect(header).to.deep.equal(fixtures.headers[30000])
return blockchain.getHeader(fixtures.headers[30000].hash)
})
.then(function (header) {
expect(header).to.deep.equal(fixtures.headers[300000])
expect(header).to.deep.equal(fixtures.headers[30000])
return blockchain.getTxBlockHash(fixtures.txMerkle.confirmed[0].txid)
})
.then(function (txBlockHash) {
Expand Down Expand Up @@ -182,7 +206,7 @@ describe('blockchain.Verified', function () {
expect(res).to.be.an('object')
expect(res.transactions).to.deep.equal(fixture.transactions)
expect(res.latest).to.be.an('object')
expect(res.latest.height).to.be.at.least(300000)
expect(res.latest.height).to.be.at.least(480000)
expect(res.latest.hash).to.have.length(64)
})
.done(done, done)
Expand Down Expand Up @@ -223,42 +247,45 @@ describe('blockchain.Verified', function () {
})
}

describe.skip('full mode (memory storage)', function () {
this.timeout(30 * 60 * 1000)
describe('full mode (memory storage)', function () {
this.timeout(150 * 1000)

beforeEach(createBeforeEachFunction(
blockchainjs.storage.Memory,
{compactMode: false},
{compactMode: false}))
{compactMode: false},
{fullChain: false}))

runTests()
})

describe.skip('compact mode without pre-saved data (memory storage)', function () {
this.timeout(30 * 60 * 1000)
describe('compact mode without pre-saved data (memory storage)', function () {
this.timeout(150 * 1000)

beforeEach(createBeforeEachFunction(
blockchainjs.storage.Memory,
{compactMode: true},
{compactMode: true}))
{compactMode: true},
{fullChain: false}))

runTests()
})

/* @todo compact mode with pre-saved wrong hashes */

function runWithStorage (Storage) {
if (Storage === undefined) {
function runWithStorage (clsName) {
var StorageCls = blockchainjs.storage[clsName]
if (StorageCls === undefined) {
return
}

var desc = 'compact mode with pre-saved data (' + Storage.name + ' storage)'
var describeFn = Storage.isAvailable() ? describe : describe.skip
describeFn(desc, function () {
var desc = 'compact mode with pre-saved data (' + clsName + ' storage)'
var ldescribe = StorageCls.isAvailable() ? describe : xdescribe
ldescribe(desc, function () {
this.timeout(60 * 1000)

beforeEach(createBeforeEachFunction(
Storage,
StorageCls,
{
compactMode: true,
filename: ':memory:',
Expand All @@ -268,15 +295,16 @@ describe('blockchain.Verified', function () {
{
compactMode: true,
chunkHashes: blockchainjs.chunkHashes.testnet
}))
},
{fullChain: true}))

runTests()
})
}

runWithStorage(blockchainjs.storage.Memory)
runWithStorage(blockchainjs.storage.SQLite)
runWithStorage(blockchainjs.storage.WebSQL)
runWithStorage(blockchainjs.storage.LocalStorage)
runWithStorage(blockchainjs.storage.IndexedDB)
runWithStorage('Memory')
runWithStorage('SQLite')
runWithStorage('WebSQL')
runWithStorage('LocalStorage')
runWithStorage('IndexedDB')
})
10 changes: 5 additions & 5 deletions test/connector/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ module.exports = function (opts) {
.done(done, done)
})

it('getHeader 300000 by height', function (done) {
connector.getHeader(fixtures.headers[300000].height)
it('getHeader 30000 by height', function (done) {
connector.getHeader(fixtures.headers[30000].height)
.then(function (header) {
expect(header).to.deep.equal(fixtures.headers[300000])
expect(header).to.deep.equal(fixtures.headers[30000])
})
.done(done, done)
})
Expand All @@ -113,7 +113,7 @@ module.exports = function (opts) {
var headerHash = blockchainjs.util.sha256x2(rawHeader)
expect(header.hash).to.equal(blockchainjs.util.hashEncode(headerHash))
expect(header.height).to.be.a('number')
expect(header.height).to.be.at.least(300000)
expect(header.height).to.be.at.least(480000)
})
.done(done, done)
})
Expand Down Expand Up @@ -286,7 +286,7 @@ module.exports = function (opts) {
expect(res).to.be.an('object')
expect(res.transactions).to.deep.equal(fixture.transactions)
expect(res.latest).to.be.an('object')
expect(res.latest.height).to.be.at.least(300000)
expect(res.latest.height).to.be.at.least(480000)
expect(res.latest.hash).to.have.length(64)
})
.done(done, done)
Expand Down
34 changes: 15 additions & 19 deletions test/fixtures/connector.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,31 @@
"bits": 486604799,
"nonce": 414098458
},
"300000": {
"hash": "000000000000226f7618566e70a2b5e020e29579b46743f05348427239bf41a1",
"height": 300000,
"30000": {
"hash": "000000001036e9539cc6ed03d7b265618004138eb378ddb3921ee93ba30817ab",
"height": 30000,
"version": 2,
"hashPrevBlock": "00000000dfe970844d1bf983d0745f709368b5c66224837a17ed633f0dabd300",
"hashMerkleRoot": "ca7c7b64204eaa4b0a1632a7d326d4d8255bfd0fa1f5d66f8def8fa72e5b2f32",
"time": 1412899877,
"bits": 453050367,
"nonce": 733842077
"hashPrevBlock": "0000000003eefbfb23fb26552e41b1dbdc0fa4e2904eb2726f7232e4c74eff12",
"hashMerkleRoot": "0415ed1139aabc65c34af0e2cc408614e018045fa8fd210577345d8b8dc41afd",
"time": 1348880042,
"bits": 470996668,
"nonce": 1363279904
}
},
"txMerkle": {
"confirmed": [
{
"txid": "9854bf4761024a1075ebede93d968ce1ba98d240ba282fb1f0170e555d8fdbd8",
"txid": "10cb1f5ffa591edc7dc545c674bdfa830bbdd3555a19d4a16253b194ab0a32d3",
"result": {
"source": "blocks",
"block": {
"height": 279774,
"hash": "00000000ba81453dd2839b8f91b61be98ee82bee5b7697f6dab1f6149885f1ff",
"index": 4,
"height": 27774,
"hash": "000000008408ad6e31ddc9563908d7f11ee4af1f21a35785b2b07837f33ebce4",
"merkle": [
"289eb5dab9aad256a7f508377f8cec7df4c3eae07572a8d7273e303a81313e03",
"fb27fb6ebf46eda58831ca296736d82eec0b51d194f6f6c94c6788ea400a0c8d",
"f43b287ff722b4ab4d14043f732c23071a86a2ae0ea72acb4277ef0a4e250d8f",
"2ea9db3d74a1d9a50cd87931ae455e7c037033ba734981c078b5f4dcd39c14c5",
"b4bd6a5685959e13446d3de03f1375ee3cf37fa9c1488d25c14fb6bbdedc51dc",
"f3ebd6145c5c8d2144e1641eb0bb4a9315cc83d7ebb2ab2199e47f344e37fc28"
]
"33bd911c75e91d0df59c2cccaa090fcb2ad543f5a5cbac20dc1b60a46aa95b34",
"6fbf0f4971241c4f9cafd38a2764b63f410d5bfaad2ed5e2cd1184088c187d94"
],
"index": 1
}
}
},
Expand Down

0 comments on commit a85339d

Please sign in to comment.