From b8e8926dad838aa684241637e8a392fadced02e0 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 28 Jul 2017 18:19:10 -0400 Subject: [PATCH] Fixed all tests. --- lib/services/header/encoding.js | 2 +- lib/services/header/index.js | 4 ++++ lib/services/p2p/index.js | 4 ++-- test/services/address/index.unit.js | 2 ++ test/services/header/encoding.unit.js | 23 ++++++++++++++--------- test/services/header/index.unit.js | 9 ++++----- test/services/web/web.unit.js | 2 +- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/services/header/encoding.js b/lib/services/header/encoding.js index a05826dcc..219f9de73 100644 --- a/lib/services/header/encoding.js +++ b/lib/services/header/encoding.js @@ -38,7 +38,7 @@ Encoding.prototype.encodeHeaderValue = function(header) { var heightBuf = new Buffer(4); heightBuf.writeUInt32BE(header.height); var chainworkBuf = new Buffer(header.chainwork, 'hex'); - return Buffer.concat([hashBuf, versionBuf, prevHash.reverse(), merkleRoot.reverse(), tsBuf, bitsBuf, nonceBuf, heightBuf, chainworkBuf ]); + return Buffer.concat([hashBuf, versionBuf, prevHash, merkleRoot, tsBuf, bitsBuf, nonceBuf, heightBuf, chainworkBuf ]); }; Encoding.prototype.decodeHeaderValue = function(buffer) { diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 58c784e40..70c00854c 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -11,6 +11,7 @@ var BN = require('bn.js'); var consensus = require('bcoin').consensus; var assert = require('assert'); var constants = require('../../constants'); +var Header = require('bitcore-lib').BlockHeader; var HeaderService = function(options) { @@ -215,6 +216,9 @@ HeaderService.prototype._onHeaders = function(headers) { var dbOps = []; for(var i = 0; i < headers.length; i++) { var header = headers[i]; + if (header instanceof Header) { + header = header.toObject(); + } header.height = ++self._tip.height; self._tip.hash = header.hash; header.chainwork = self._getChainwork(header, prevHeader).toString(16, 64); diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index e3afb76d6..14f49dae8 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -307,7 +307,7 @@ P2P.prototype._setListeners = function() { self._pool.on('peertx', self._onPeerTx.bind(self)); self._pool.on('peerblock', self._onPeerBlock.bind(self)); self._pool.on('peerheaders', self._onPeerHeaders.bind(self)); - self.node.once('ready', self._connect.bind(self)); + self.node.on('ready', self._connect.bind(self)); if (self._bcoin) { self._bcoin.emitter.once('connect', self._connect.bind(self)); } @@ -333,7 +333,7 @@ P2P.prototype._setResourceFilter = function(filter, resource) { P2P.prototype._startBcoin = function() { this._bcoin = new Bcoin({ - network: this.node.getNetworkName(), + network: this.node.network, prefix: this.node.datadir }); this._bcoin.start(); diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index fb3d94e37..2eb331d46 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -205,6 +205,7 @@ describe('Address Service', function() { it('should reorg', function() { var getAddressString = sandbox.stub().returns('a'); + var oldEncodeTip = utils.encodeTip; var encodeTip = sandbox.stub().returns({ key: 'key', value: 'value' }); var batch = sandbox.stub(); addressService._tip = { height: 100 }; @@ -216,6 +217,7 @@ describe('Address Service', function() { addressService._onReorg(oldList, common); expect(batch.calledOnce).to.be.true; expect(encodeTip.calledOnce).to.be.true; + utils.encodeTip = oldEncodeTip; }); }); diff --git a/test/services/header/encoding.unit.js b/test/services/header/encoding.unit.js index fdd7fd8df..5fcd72109 100644 --- a/test/services/header/encoding.unit.js +++ b/test/services/header/encoding.unit.js @@ -11,21 +11,24 @@ describe('Header service encoding', function() { var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var hashBuf = new Buffer(hash, 'hex'); var header = { + hash: hash, prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade', version: 0x2000012, merkleRoot: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03adf', timestamp: 1E9, bits: 400000, nonce: 123456, - height: 123 + height: 123, + chainwork: '0000000000000000000000000000000000000000000000000000000200020002' }; var versionBuf = new Buffer(4); - var prevHash = new Buffer(header.prevHash, 'hex'); - var merkleRoot = new Buffer(header.merkleRoot, 'hex'); + var prevHashBuf = new Buffer(header.prevHash, 'hex'); + var merkleRootBuf = new Buffer(header.merkleRoot, 'hex'); var tsBuf = new Buffer(4); var bitsBuf = new Buffer(4); var nonceBuf = new Buffer(4); var heightBuf = new Buffer(4); + var chainBuf = new Buffer('0000000000000000000000000000000000000000000000000000000200020002', 'hex'); heightBuf.writeUInt32BE(header.height); it('should encode header key' , function() { @@ -38,17 +41,17 @@ describe('Header service encoding', function() { }); it('should encode header value', function() { + var prevHashBuf = new Buffer(header.prevHash, 'hex'); versionBuf.writeInt32BE(header.version); // signed tsBuf.writeUInt32BE(header.timestamp); bitsBuf.writeUInt32BE(header.bits); nonceBuf.writeUInt32BE(header.nonce); heightBuf.writeUInt32BE(header.height); - var chainBuf = new Buffer('0000000000000000000000000000000000000000000000000000000200020002', 'hex'); encoding.encodeHeaderValue(header).should.deep.equal(Buffer.concat([ hashBuf, versionBuf, - prevHash, - merkleRoot, + prevHashBuf, + merkleRootBuf, tsBuf, bitsBuf, nonceBuf, @@ -59,13 +62,15 @@ describe('Header service encoding', function() { it('should decode header value', function() { encoding.decodeHeaderValue(Buffer.concat([ + hashBuf, versionBuf, - prevHash, - merkleRoot, + prevHashBuf, + merkleRootBuf, tsBuf, bitsBuf, nonceBuf, - heightBuf + heightBuf, + chainBuf ])).should.deep.equal(header); }); }); diff --git a/test/services/header/index.unit.js b/test/services/header/index.unit.js index ee639b305..8c8e211ce 100644 --- a/test/services/header/index.unit.js +++ b/test/services/header/index.unit.js @@ -7,17 +7,15 @@ var assert = chai.assert; var expect = chai.expect; var Encoding = require('../../../lib/services/header/encoding'); var utils = require('../../../lib/utils'); -var EventEmitter = require('events').EventEmitter; -var Block = require('bcoin').block; -var BitcoreBlock = require('bitcore-lib').Block; +var Block = require('bitcore-lib').Block; var BN = require('bn.js'); describe('Header Service', function() { var headerService; var sandbox; - var prevHeader = new BitcoreBlock(new Buffer('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')); - var header = new BitcoreBlock(new Buffer('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header; + var prevHeader = new Block(new Buffer('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')).header.toObject(); + var header = new Block(new Buffer('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header.toObject(); beforeEach(function() { sandbox = sinon.sandbox.create(); headerService = new HeaderService({ @@ -122,6 +120,7 @@ describe('Header Service', function() { header.chainwork = '0000000000000000000000000000000000000000000000000000000000000001'; expect(batch.calledOnce).to.be.true; + expect(getChainwork.calledOnce).to.be.true; var expected = batch.args[0][0]; expect(expected[0]).to.deep.equal({ diff --git a/test/services/web/web.unit.js b/test/services/web/web.unit.js index 7c3c561ed..d9ac564dd 100644 --- a/test/services/web/web.unit.js +++ b/test/services/web/web.unit.js @@ -82,7 +82,7 @@ describe('WebService', function() { it('should pass json request limit to json body parser', function(done) { var node = new EventEmitter(); var jsonStub = sinon.stub(); - var TestWebService = proxyquire('../../lib/services/web', { + var TestWebService = proxyquire('../../../lib/services/web', { http: { createServer: sinon.stub() },