Fixed more tests.

This commit is contained in:
Chris Kleeschulte 2017-07-28 17:17:07 -04:00
parent 2bf06956e1
commit dfdce81287
7 changed files with 47 additions and 99 deletions

View File

@ -171,7 +171,7 @@ Node.prototype._startService = function(serviceInfo, callback) {
Node.prototype._logTitle = function() { Node.prototype._logTitle = function() {
if (this.configPath) { if (this.configPath) {
log.info('Using config:', this.configPath); log.info('Using config:', this.configPath);
log.info('Using network:', this.getNetworkName()); log.info('Using network:', this.network);
} }
}; };

View File

@ -13,7 +13,7 @@ var FeeService = function(options) {
port: 8332 port: 8332
}; };
BaseService.call(this, options); BaseService.call(this, options);
this._client = new BitcoreRPC(this._config);
}; };
inherits(FeeService, BaseService); inherits(FeeService, BaseService);
@ -39,8 +39,7 @@ FeeService.prototype.getAPIMethods = function() {
}; };
FeeService.prototype.estimateFee = function(blocks, callback) { FeeService.prototype.estimateFee = function(blocks, callback) {
var client = new BitcoreRPC(this._config); this._client.estimateFee(blocks || 4, callback);
client.estimateFee(blocks || 4, callback);
}; };

View File

@ -24,7 +24,7 @@ var HeaderService = function(options) {
this.subscriptions = {}; this.subscriptions = {};
this.subscriptions.block = []; this.subscriptions.block = [];
this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.getNetworkName()]; this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network];
}; };
inherits(HeaderService, BaseService); inherits(HeaderService, BaseService);

View File

@ -2,15 +2,13 @@
var should = require('chai').should(); var should = require('chai').should();
var sinon = require('sinon'); var sinon = require('sinon');
var bitcore = require('bitcore-lib');
var Networks = bitcore.Networks;
var proxyquire = require('proxyquire'); var proxyquire = require('proxyquire');
var util = require('util'); var util = require('util');
var BaseService = require('../lib/service'); var BaseService = require('../lib/service');
var index = require('../lib'); var index = require('../lib');
var log = index.log; var log = index.log;
describe('Bitcore Node', function() { describe('Node', function() {
var baseConfig = {}; var baseConfig = {};
@ -22,10 +20,6 @@ describe('Bitcore Node', function() {
Node.prototype._initialize = sinon.spy(); Node.prototype._initialize = sinon.spy();
}); });
after(function() {
Networks.disableRegtest();
});
describe('@constructor', function() { describe('@constructor', function() {
var TestService; var TestService;
before(function() { before(function() {
@ -34,12 +28,13 @@ describe('Bitcore Node', function() {
}); });
it('will set properties', function() { it('will set properties', function() {
var config = { var config = {
network: 'testnet',
services: [ services: [
{ {
name: 'test1', name: 'test1',
module: TestService module: TestService
} }
], ]
}; };
var TestNode = proxyquire('../lib/node', {}); var TestNode = proxyquire('../lib/node', {});
TestNode.prototype.start = sinon.spy(); TestNode.prototype.start = sinon.spy();
@ -47,12 +42,12 @@ describe('Bitcore Node', function() {
node._unloadedServices.length.should.equal(1); node._unloadedServices.length.should.equal(1);
node._unloadedServices[0].name.should.equal('test1'); node._unloadedServices[0].name.should.equal('test1');
node._unloadedServices[0].module.should.equal(TestService); node._unloadedServices[0].module.should.equal(TestService);
node.network.should.equal(Networks.defaultNetwork); node.network.should.equal('testnet');
var node2 = TestNode(config); var node2 = TestNode(config);
node2._unloadedServices.length.should.equal(1); node2._unloadedServices.length.should.equal(1);
node2._unloadedServices[0].name.should.equal('test1'); node2._unloadedServices[0].name.should.equal('test1');
node2._unloadedServices[0].module.should.equal(TestService); node2._unloadedServices[0].module.should.equal(TestService);
node2.network.should.equal(Networks.defaultNetwork); node2.network.should.equal('testnet');
}); });
it('will set network to testnet', function() { it('will set network to testnet', function() {
var config = { var config = {
@ -67,7 +62,7 @@ describe('Bitcore Node', function() {
var TestNode = proxyquire('../lib/node', {}); var TestNode = proxyquire('../lib/node', {});
TestNode.prototype.start = sinon.spy(); TestNode.prototype.start = sinon.spy();
var node = new TestNode(config); var node = new TestNode(config);
node.network.should.equal(Networks.testnet); node.network.should.equal('testnet');
}); });
it('will set network to regtest', function() { it('will set network to regtest', function() {
var config = { var config = {
@ -82,9 +77,7 @@ describe('Bitcore Node', function() {
var TestNode = proxyquire('../lib/node', {}); var TestNode = proxyquire('../lib/node', {});
TestNode.prototype.start = sinon.spy(); TestNode.prototype.start = sinon.spy();
var node = new TestNode(config); var node = new TestNode(config);
var regtest = Networks.get('regtest'); node.network.should.equal('regtest');
should.exist(regtest);
node.network.should.equal(regtest);
}); });
it('will be able to disable log formatting', function() { it('will be able to disable log formatting', function() {
var config = { var config = {
@ -97,11 +90,12 @@ describe('Bitcore Node', function() {
], ],
formatLogs: false formatLogs: false
}; };
var TestNode = proxyquire('../lib/node', {}); var TestNode = proxyquire('../lib/node', {});
var node = new TestNode(config); var node = new TestNode(config);
node.log.formatting.should.equal(false); node.log.formatting.should.equal(false);
var TestNode = proxyquire('../lib/node', {}); TestNode = proxyquire('../lib/node', {});
config.formatLogs = true; config.formatLogs = true;
var node2 = new TestNode(config); var node2 = new TestNode(config);
node2.log.formatting.should.equal(true); node2.log.formatting.should.equal(true);
@ -399,6 +393,7 @@ describe('Bitcore Node', function() {
}); });
}); });
it('will handle service with getAPIMethods undefined', function(done) { it('will handle service with getAPIMethods undefined', function(done) {
var node = new Node(baseConfig); var node = new Node(baseConfig);
@ -423,30 +418,6 @@ describe('Bitcore Node', function() {
}); });
}); });
describe('#getNetworkName', function() {
afterEach(function() {
bitcore.Networks.disableRegtest();
});
it('it will return the network name for livenet', function() {
var node = new Node(baseConfig);
node.getNetworkName().should.equal('livenet');
});
it('it will return the network name for testnet', function() {
var baseConfig = {
network: 'testnet'
};
var node = new Node(baseConfig);
node.getNetworkName().should.equal('testnet');
});
it('it will return the network for regtest', function() {
var baseConfig = {
network: 'regtest'
};
var node = new Node(baseConfig);
node.getNetworkName().should.equal('regtest');
});
});
describe('#stop', function() { describe('#stop', function() {
var sandbox = sinon.sandbox.create(); var sandbox = sinon.sandbox.create();
beforeEach(function() { beforeEach(function() {

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var should = require('chai').should(); var should = require('chai').should();
var Block = require('bitcore-lib').Block; var Block = require('bcoin').block;
var Encoding = require('../../../lib/services/block/encoding'); var Encoding = require('../../../lib/services/block/encoding');
@ -9,20 +9,16 @@ describe('Block service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex'); var servicePrefix = new Buffer('0000', 'hex');
var blockPrefix = new Buffer('00', 'hex');
var metaPrefix = new Buffer('01', 'hex');
var encoding = new Encoding(servicePrefix); var encoding = new Encoding(servicePrefix);
var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var height = 1; var height = 1;
var block = new Block(new Buffer('0100000095194b8567fe2e8bbda931afd01a7acd399b9325cb54683e64129bcd00000000660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d19a76949ffff001d33f3c25d0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000', 'hex')); var block = Block.fromRaw('0100000095194b8567fe2e8bbda931afd01a7acd399b9325cb54683e64129bcd00000000660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d19a76949ffff001d33f3c25d0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000', 'hex');
describe('Block', function() { describe('Block', function() {
it('should encode block key' , function() { it('should encode block key' , function() {
encoding.encodeBlockKey(hash).should.deep.equal(Buffer.concat([ encoding.encodeBlockKey(hash).should.deep.equal(Buffer.concat([
servicePrefix, servicePrefix,
blockPrefix,
new Buffer(hash, 'hex') new Buffer(hash, 'hex')
])); ]));
}); });
@ -30,7 +26,6 @@ describe('Block service encoding', function() {
it('should decode block key' , function() { it('should decode block key' , function() {
var buf = Buffer.concat([ var buf = Buffer.concat([
servicePrefix, servicePrefix,
blockPrefix,
new Buffer(hash, 'hex') new Buffer(hash, 'hex')
]); ]);
@ -40,38 +35,15 @@ describe('Block service encoding', function() {
it('should encode block value', function() { it('should encode block value', function() {
encoding.encodeBlockValue(block).should.deep.equal( encoding.encodeBlockValue(block).should.deep.equal(
block.toBuffer()); block.toRaw());
}); });
it('shound decode block value', function() { it('shound decode block value', function() {
var ret = encoding.decodeBlockValue(block.toBuffer()); var ret = encoding.decodeBlockValue(block.toRaw());
ret.should.deep.equal(ret); ret.should.deep.equal(ret);
}); });
}); });
describe('Meta', function() {
var heightBuf = new Buffer(4);
heightBuf.writeUInt32BE(height);
it('should encode meta key', function() {
encoding.encodeMetaKey(height).should.deep.equal(Buffer.concat([ servicePrefix, metaPrefix, heightBuf ]));
});
it('should decode meta key', function() {
encoding.decodeMetaKey(Buffer.concat([ servicePrefix, metaPrefix, heightBuf ])).should.equal(height);
});
it('should encode meta value', function() {
encoding.encodeMetaValue({ chainwork: '00000001', hash: hash }).should.deep.equal(
Buffer.concat([ new Buffer(hash, 'hex'), new Buffer('00000001', 'hex') ]));
});
it('should decode meta value', function() {
encoding.decodeMetaValue(Buffer.concat([ new Buffer(hash, 'hex'), new Buffer('00000001', 'hex') ])).should.deep.equal(
{ chainwork: '00000001', hash: hash });
});
});
}); });

View File

@ -2,21 +2,22 @@
var sinon = require('sinon'); var sinon = require('sinon');
var FeeService = require('../../../lib/services/fee'); var FeeService = require('../../../lib/services/fee');
var expect = require('chai').expect; var expect = require('chai').expect;
describe.only('#Fee Service', function() { describe('#Fee Service', function() {
var feeService; var feeService;
var sandbox; var sandbox;
beforeEach(function() { beforeEach(function() {
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
feeService = new FeeService({ feeService = new FeeService({
"rpc": { rpc: {
"user": "bitcoin", user: 'bitcoin',
"pass": "local321", pass: 'local321',
"host": "localhost", host: 'localhost',
"protocol": "http", protocol: 'http',
"port": 8332 port: 8332
} }
}); });
}); });
@ -29,19 +30,22 @@ describe.only('#Fee Service', function() {
Running in regtest mode or unsync'd will return -1 Running in regtest mode or unsync'd will return -1
*/ */
it("Has an estimateFee method", function() { it('Has an estimateFee method', function() {
var method = feeService.getAPIMethods()[0][0]; var method = feeService.getAPIMethods()[0][0];
expect(method).to.equal('estimateFee'); expect(method).to.equal('estimateFee');
}) });
it("Can estimate fees", function(done) { it('Can estimate fees', function(done) {
var estimateFee = sinon.stub().callsArgWith(1, null, { result: 0.1 });
feeService._client = { estimateFee: estimateFee };
feeService.estimateFee(4, function(err, fee) { feeService.estimateFee(4, function(err, fee) {
expect(err).to.be.a('null'); if (err) {
expect(fee.result).to.exist; return done(err);
expect(fee.result).to.be.at.least(-1); }
expect(fee.result).to.equal(0.1);
done(); done();
}); });
}) });
}); });

View File

@ -9,6 +9,7 @@ describe('Header service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex'); var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix); var encoding = new Encoding(servicePrefix);
var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var hashBuf = new Buffer(hash, 'hex');
var header = { var header = {
prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade', prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade',
version: 0x2000012, version: 0x2000012,
@ -25,16 +26,15 @@ describe('Header service encoding', function() {
var bitsBuf = new Buffer(4); var bitsBuf = new Buffer(4);
var nonceBuf = new Buffer(4); var nonceBuf = new Buffer(4);
var heightBuf = new Buffer(4); var heightBuf = new Buffer(4);
heightBuf.writeUInt32BE(header.height);
it('should encode header key' , function() { it('should encode header key' , function() {
var hashBuf = new Buffer(hash, 'hex'); encoding.encodeHeaderKey(header.height, hash).should.deep.equal(Buffer.concat([servicePrefix, heightBuf, hashBuf]));
encoding.encodeHeaderKey(hash).should.deep.equal(Buffer.concat([servicePrefix, hashBuf]));
}); });
it('should decode header key', function() { it('should decode header key', function() {
var hashBuf = new Buffer(hash, 'hex'); encoding.decodeHeaderKey(Buffer.concat([servicePrefix, heightBuf, hashBuf]))
encoding.decodeHeaderKey(Buffer.concat([servicePrefix, hashBuf])) .should.deep.equal({ height: 123, hash: hash });
.should.equal(hash);
}); });
it('should encode header value', function() { it('should encode header value', function() {
@ -43,16 +43,18 @@ describe('Header service encoding', function() {
bitsBuf.writeUInt32BE(header.bits); bitsBuf.writeUInt32BE(header.bits);
nonceBuf.writeUInt32BE(header.nonce); nonceBuf.writeUInt32BE(header.nonce);
heightBuf.writeUInt32BE(header.height); heightBuf.writeUInt32BE(header.height);
var chainBuf = new Buffer('0000000000000000000000000000000000000000000000000000000200020002', 'hex');
encoding.encodeHeaderValue(header).should.deep.equal(Buffer.concat([ encoding.encodeHeaderValue(header).should.deep.equal(Buffer.concat([
hashBuf,
versionBuf, versionBuf,
prevHash, prevHash,
merkleRoot, merkleRoot,
tsBuf, tsBuf,
bitsBuf, bitsBuf,
nonceBuf, nonceBuf,
heightBuf heightBuf,
chainBuf
])); ]));
}); });
it('should decode header value', function() { it('should decode header value', function() {