diff --git a/lib/node.js b/lib/node.js index 2070eef0..7825669d 100644 --- a/lib/node.js +++ b/lib/node.js @@ -171,7 +171,7 @@ Node.prototype._startService = function(serviceInfo, callback) { Node.prototype._logTitle = function() { if (this.configPath) { log.info('Using config:', this.configPath); - log.info('Using network:', this.getNetworkName()); + log.info('Using network:', this.network); } }; diff --git a/lib/services/fee/index.js b/lib/services/fee/index.js index 7299b56f..895784f0 100644 --- a/lib/services/fee/index.js +++ b/lib/services/fee/index.js @@ -13,7 +13,7 @@ var FeeService = function(options) { port: 8332 }; BaseService.call(this, options); - + this._client = new BitcoreRPC(this._config); }; inherits(FeeService, BaseService); @@ -39,8 +39,7 @@ FeeService.prototype.getAPIMethods = function() { }; FeeService.prototype.estimateFee = function(blocks, callback) { - var client = new BitcoreRPC(this._config); - client.estimateFee(blocks || 4, callback); + this._client.estimateFee(blocks || 4, callback); }; diff --git a/lib/services/header/index.js b/lib/services/header/index.js index a96986c9..58c784e4 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -24,7 +24,7 @@ var HeaderService = function(options) { this.subscriptions = {}; 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); diff --git a/test/node.unit.js b/test/node.unit.js index fce6f009..37a4171b 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -2,15 +2,13 @@ var should = require('chai').should(); var sinon = require('sinon'); -var bitcore = require('bitcore-lib'); -var Networks = bitcore.Networks; var proxyquire = require('proxyquire'); var util = require('util'); var BaseService = require('../lib/service'); var index = require('../lib'); var log = index.log; -describe('Bitcore Node', function() { +describe('Node', function() { var baseConfig = {}; @@ -22,10 +20,6 @@ describe('Bitcore Node', function() { Node.prototype._initialize = sinon.spy(); }); - after(function() { - Networks.disableRegtest(); - }); - describe('@constructor', function() { var TestService; before(function() { @@ -34,12 +28,13 @@ describe('Bitcore Node', function() { }); it('will set properties', function() { var config = { + network: 'testnet', services: [ { name: 'test1', module: TestService } - ], + ] }; var TestNode = proxyquire('../lib/node', {}); TestNode.prototype.start = sinon.spy(); @@ -47,12 +42,12 @@ describe('Bitcore Node', function() { node._unloadedServices.length.should.equal(1); node._unloadedServices[0].name.should.equal('test1'); node._unloadedServices[0].module.should.equal(TestService); - node.network.should.equal(Networks.defaultNetwork); + node.network.should.equal('testnet'); var node2 = TestNode(config); node2._unloadedServices.length.should.equal(1); node2._unloadedServices[0].name.should.equal('test1'); 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() { var config = { @@ -67,7 +62,7 @@ describe('Bitcore Node', function() { var TestNode = proxyquire('../lib/node', {}); TestNode.prototype.start = sinon.spy(); var node = new TestNode(config); - node.network.should.equal(Networks.testnet); + node.network.should.equal('testnet'); }); it('will set network to regtest', function() { var config = { @@ -82,9 +77,7 @@ describe('Bitcore Node', function() { var TestNode = proxyquire('../lib/node', {}); TestNode.prototype.start = sinon.spy(); var node = new TestNode(config); - var regtest = Networks.get('regtest'); - should.exist(regtest); - node.network.should.equal(regtest); + node.network.should.equal('regtest'); }); it('will be able to disable log formatting', function() { var config = { @@ -97,11 +90,12 @@ describe('Bitcore Node', function() { ], formatLogs: false }; + var TestNode = proxyquire('../lib/node', {}); var node = new TestNode(config); node.log.formatting.should.equal(false); - var TestNode = proxyquire('../lib/node', {}); + TestNode = proxyquire('../lib/node', {}); config.formatLogs = true; var node2 = new TestNode(config); node2.log.formatting.should.equal(true); @@ -399,6 +393,7 @@ describe('Bitcore Node', function() { }); }); + it('will handle service with getAPIMethods undefined', function(done) { 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() { var sandbox = sinon.sandbox.create(); beforeEach(function() { diff --git a/test/services/block/encoding.unit.js b/test/services/block/encoding.unit.js index 40491698..fb0d5897 100644 --- a/test/services/block/encoding.unit.js +++ b/test/services/block/encoding.unit.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var Block = require('bitcore-lib').Block; +var Block = require('bcoin').block; var Encoding = require('../../../lib/services/block/encoding'); @@ -9,20 +9,16 @@ describe('Block service encoding', function() { var servicePrefix = new Buffer('0000', 'hex'); - var blockPrefix = new Buffer('00', 'hex'); - var metaPrefix = new Buffer('01', 'hex'); - var encoding = new Encoding(servicePrefix); var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; var height = 1; - var block = new Block(new Buffer('0100000095194b8567fe2e8bbda931afd01a7acd399b9325cb54683e64129bcd00000000660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d19a76949ffff001d33f3c25d0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000', 'hex')); + var block = Block.fromRaw('0100000095194b8567fe2e8bbda931afd01a7acd399b9325cb54683e64129bcd00000000660802c98f18fd34fd16d61c63cf447568370124ac5f3be626c2e1c3c9f0052d19a76949ffff001d33f3c25d0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d014dffffffff0100f2052a01000000434104e70a02f5af48a1989bf630d92523c9d14c45c75f7d1b998e962bff6ff9995fc5bdb44f1793b37495d80324acba7c8f537caaf8432b8d47987313060cc82d8a93ac00000000', 'hex'); describe('Block', function() { it('should encode block key' , function() { encoding.encodeBlockKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, - blockPrefix, new Buffer(hash, 'hex') ])); }); @@ -30,7 +26,6 @@ describe('Block service encoding', function() { it('should decode block key' , function() { var buf = Buffer.concat([ servicePrefix, - blockPrefix, new Buffer(hash, 'hex') ]); @@ -40,38 +35,15 @@ describe('Block service encoding', function() { it('should encode block value', function() { encoding.encodeBlockValue(block).should.deep.equal( - block.toBuffer()); + block.toRaw()); }); it('shound decode block value', function() { - var ret = encoding.decodeBlockValue(block.toBuffer()); + var ret = encoding.decodeBlockValue(block.toRaw()); 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 }); - }); - }); }); diff --git a/test/services/fee/index.unit.js b/test/services/fee/index.unit.js index 668b438c..8b1757a7 100644 --- a/test/services/fee/index.unit.js +++ b/test/services/fee/index.unit.js @@ -2,21 +2,22 @@ var sinon = require('sinon'); var FeeService = require('../../../lib/services/fee'); - var expect = require('chai').expect; -describe.only('#Fee Service', function() { +describe('#Fee Service', function() { + var feeService; var sandbox; + beforeEach(function() { sandbox = sinon.sandbox.create(); feeService = new FeeService({ - "rpc": { - "user": "bitcoin", - "pass": "local321", - "host": "localhost", - "protocol": "http", - "port": 8332 + rpc: { + user: 'bitcoin', + pass: 'local321', + host: 'localhost', + protocol: 'http', + port: 8332 } }); }); @@ -29,19 +30,22 @@ describe.only('#Fee Service', function() { 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]; 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) { - expect(err).to.be.a('null'); - expect(fee.result).to.exist; - expect(fee.result).to.be.at.least(-1); + if (err) { + return done(err); + } + expect(fee.result).to.equal(0.1); done(); }); - }) + }); -}); \ No newline at end of file +}); diff --git a/test/services/header/encoding.unit.js b/test/services/header/encoding.unit.js index 3c76671f..fdd7fd8d 100644 --- a/test/services/header/encoding.unit.js +++ b/test/services/header/encoding.unit.js @@ -9,6 +9,7 @@ describe('Header service encoding', function() { var servicePrefix = new Buffer('0000', 'hex'); var encoding = new Encoding(servicePrefix); var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add'; + var hashBuf = new Buffer(hash, 'hex'); var header = { prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade', version: 0x2000012, @@ -25,16 +26,15 @@ describe('Header service encoding', function() { var bitsBuf = new Buffer(4); var nonceBuf = new Buffer(4); var heightBuf = new Buffer(4); + heightBuf.writeUInt32BE(header.height); it('should encode header key' , function() { - var hashBuf = new Buffer(hash, 'hex'); - encoding.encodeHeaderKey(hash).should.deep.equal(Buffer.concat([servicePrefix, hashBuf])); + encoding.encodeHeaderKey(header.height, hash).should.deep.equal(Buffer.concat([servicePrefix, heightBuf, hashBuf])); }); it('should decode header key', function() { - var hashBuf = new Buffer(hash, 'hex'); - encoding.decodeHeaderKey(Buffer.concat([servicePrefix, hashBuf])) - .should.equal(hash); + encoding.decodeHeaderKey(Buffer.concat([servicePrefix, heightBuf, hashBuf])) + .should.deep.equal({ height: 123, hash: hash }); }); it('should encode header value', function() { @@ -43,16 +43,18 @@ describe('Header service encoding', function() { 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, tsBuf, bitsBuf, nonceBuf, - heightBuf + heightBuf, + chainBuf ])); - }); it('should decode header value', function() {