Skip to content

Commit

Permalink
Fixed all tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kleeschulte committed Jul 28, 2017
1 parent 272fb54 commit b8e8926
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/services/header/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/services/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/services/p2p/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions test/services/address/index.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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;
});

});
Expand Down
23 changes: 14 additions & 9 deletions test/services/header/encoding.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand All @@ -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);
});
});
Expand Down
9 changes: 4 additions & 5 deletions test/services/header/index.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion test/services/web/web.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down

0 comments on commit b8e8926

Please sign in to comment.