diff --git a/test/regtest/data/blocks.json b/test/data/blocks.json similarity index 100% rename from test/regtest/data/blocks.json rename to test/data/blocks.json diff --git a/test/regtest/address.js b/test/regtest/address.js deleted file mode 100644 index 7f2a074e..00000000 --- a/test/regtest/address.js +++ /dev/null @@ -1,768 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var spawn = require('child_process').spawn; -var path = require('path'); -var rimraf = require('rimraf'); -var mkdirp = require('mkdirp'); -var fs = require('fs'); -var async = require('async'); -var RPC = require('bitcoind-rpc'); -var http = require('http'); - -var rpc1Address; -var rpc2Address; - -var rpcConfig = { - protocol: 'http', - user: 'local', - pass: 'localtest', - host: '127.0.0.1', - port: 58332, - rejectUnauthorized: false -}; - -var rpc1 = new RPC(rpcConfig); -rpcConfig.port++; -var rpc2 = new RPC(rpcConfig); -var debug = true; -var bitcoreDataDir = '/tmp/bitcore'; -var bitcoinDataDirs = ['/tmp/bitcoin1', '/tmp/bitcoin2']; - -var bitcoin = { - args: { - datadir: null, - listen: 1, - regtest: 1, - server: 1, - rpcuser: 'local', - rpcpassword: 'localtest', - //printtoconsole: 1 - rpcport: 58332, - }, - datadir: null, - exec: 'bitcoind', //if this isn't on your PATH, then provide the absolute path, e.g. /usr/local/bin/bitcoind - processes: [] -}; - -var bitcore = { - configFile: { - file: bitcoreDataDir + '/bitcore-node.json', - conf: { - network: 'regtest', - port: 53001, - datadir: bitcoreDataDir, - services: [ - 'p2p', - 'db', - 'header', - 'block', - 'address', - 'transaction', - 'mempool', - 'web', - 'insight-api', - 'fee', - 'timestamp' - ], - servicesConfig: { - 'p2p': { - 'peers': [ - { 'ip': { 'v4': '127.0.0.1' }, port: 18444 } - ] - }, - 'insight-api': { - 'routePrefix': 'api' - } - } - } - }, - httpOpts: { - protocol: 'http:', - hostname: 'localhost', - port: 53001, - }, - opts: { cwd: bitcoreDataDir }, - datadir: bitcoreDataDir, - exec: path.resolve(__dirname, '../../bin/bitcore-node'), - args: ['start'], - process: null -}; - -var startBitcoind = function(count, callback) { - - var listenCount = 0; - console.log('starting ' + count + ' bitcoind\'s'); - async.timesSeries(count, function(n, next) { - - var datadir = bitcoinDataDirs.shift(); - - bitcoin.datadir = datadir; - bitcoin.args.datadir = datadir; - - if (listenCount++ > 0) { - bitcoin.args.listen = 0; - bitcoin.args.rpcport++; - bitcoin.args.connect = '127.0.0.1'; - } - - rimraf(datadir, function(err) { - - if(err) { - return next(err); - } - - mkdirp(datadir, function(err) { - - if(err) { - return next(err); - } - - var args = bitcoin.args; - var argList = Object.keys(args).map(function(key) { - return '-' + key + '=' + args[key]; - }); - - var bitcoinProcess = spawn(bitcoin.exec, argList, bitcoin.opts); - bitcoin.processes.push(bitcoinProcess); - - bitcoinProcess.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - - bitcoinProcess.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - next(); - - }); - - }); - }, function(err) { - - if (err) { - return callback(err); - } - - var pids = bitcoin.processes.map(function(process) { - return process.pid; - }); - - console.log(count + ' bitcoind\'s started at pid(s): ' + pids); - callback(); - }); -}; - - -var shutdownBitcoind = function(callback) { - bitcoin.processes.forEach(function(process) { - process.kill(); - }); - callback(); -}; - -var shutdownBitcore = function(callback) { - if (bitcore.process) { - bitcore.process.kill(); - } - callback(); -}; - -var txid; -var buildInitialChain = function(callback) { - async.waterfall([ - function(next) { - console.log('checking to see if bitcoind\'s are connected to each other.'); - rpc1.getinfo(function(err, res) { - if (err || res.result.connections !== 1) { - next(err || new Error('bitcoind\'s not connected to each other.')); - } - next(); - }); - }, - function(next) { - console.log('generating 101 blocks'); - rpc1.generate(101, next); - }, - function(res, next) { - console.log('getting new address from rpc2'); - rpc2.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc2Address = res.result; - console.log(rpc2Address); - next(null, rpc2Address); - }); - }, - function(addr, next) { - rpc1.sendToAddress(rpc2Address, 25, next); - }, - function(res, next) { - console.log('TXID: ' + res.result); - console.log('generating 6 blocks'); - rpc1.generate(7, next); - }, - function(res, next) { - rpc2.getBalance(function(err, res) { - console.log(res); - next(); - }); - }, - function(next) { - console.log('getting new address from rpc1'); - rpc1.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc1Address = res.result; - next(null, rpc1Address); - }); - }, - function(addr, next) { - rpc2.sendToAddress(rpc1Address, 20, next); - }, - function(res, next) { - txid = res.result; - console.log('sending from rpc2Address TXID: ', res); - console.log('generating 6 blocks'); - rpc2.generate(6, next); - } - ], function(err) { - - if (err) { - return callback(err); - } - rpc1.getInfo(function(err, res) { - console.log(res); - callback(); - }); - }); - -}; - -var startBitcore = function(callback) { - - rimraf(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - mkdirp(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - fs.writeFileSync(bitcore.configFile.file, JSON.stringify(bitcore.configFile.conf)); - - var args = bitcore.args; - bitcore.process = spawn(bitcore.exec, args, bitcore.opts); - - bitcore.process.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - bitcore.process.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - callback(); - }); - - }); - - -}; - -describe('Address', function() { - - this.timeout(60000); - - before(function(done) { - - async.series([ - function(next) { - startBitcoind(2, next); - }, - function(next) { - setTimeout(function() { - buildInitialChain(next); - }, 8000); - }, - function(next) { - setTimeout(function() { - startBitcore(next); - }, 6000); - } - ], function(err) { - if (err) { - return done(err); - } - setTimeout(done, 2000); - }); - - }); - - after(function(done) { - shutdownBitcoind(function() { - shutdownBitcore(done); - }); - }); - - it('should get address info correctly: /addr/:addr', function(done) { - - - var request = http.request('http://localhost:53001/api/addr/' + rpc2Address, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.balance).to.equal(0); - expect(data.totalSent).to.equal(25); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get a utxo: /addr/:addr/utxo', function(done) { - - var request = http.request('http://localhost:53001/api/addr/' + rpc1Address + '/utxo', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.length).equal(1); - expect(data[0].amount).equal(20); - expect(data[0].satoshis).equal(2000000000); - expect(data[0].confirmations).equal(6); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should get multi-address utxos: /addrs/:addrs/utxo', function(done) { - - var request = http.request('http://localhost:53001/api/addrs/' + rpc2Address + ',' + rpc1Address + '/utxo', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.length).to.equal(1); - expect(data[0].amount).to.equal(20); - expect(data[0].satoshis).to.equal(2000000000); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should post a utxo: /addrs/:addrs/utxo', function(done) { - - var body = JSON.stringify({ - addrs: [ rpc1Address, rpc2Address ] - }); - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addrs/utxo', - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Content-Length': body.length - } - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.length).to.equal(1); - expect(data[0].amount).to.equal(20); - expect(data[0].satoshis).to.equal(2000000000); - done(); - }); - - }); - - request.write(body); - request.end(); - - }); - - it('should get txs for a set of addresses: /addrs/:addrs/txs', function(done) { - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addrs/' + rpc1Address + ',' + rpc2Address + '/txs', - method: 'GET' - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.items.length).to.equal(3); - expect(data.from).to.equal(0); - expect(data.to).to.equal(3); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should post txs for a set of addresses: /addrs/txs', function(done) { - - var body = JSON.stringify({ - addrs: [ rpc1Address, rpc2Address ] - }); - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addrs/txs', - method: 'POST', - headers: { - 'Content-Type': 'application/json' - } - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.items.length).to.equal(3); - expect(data.from).to.equal(0); - expect(data.to).to.equal(3); - done(); - }); - - }); - - request.write(body); - request.end(); - - }); - - it('should get totalReceived for an address: /addr/:addr/totalReceived', function(done) { - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addr/' + rpc1Address + '/totalReceived', - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - - if (error) { - return; - } - - var data = JSON.parse(resData); - expect(data).to.equal(2000000000); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should get totalSent for an address: /addr/:addr/totalSent', function(done) { - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addr/' + rpc1Address + '/totalSent', - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data).to.equal(0); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should get unconfirmedBalance for an address: /addr/:addr/unconfirmedBalance', function(done) { - - var httpOpts = { - hostname: 'localhost', - port: 53001, - path: '/api/addr/' + rpc1Address + '/unconfirmedBalance', - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }; - - var request = http.request(httpOpts, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data).to.equal(0); - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - -}); - - diff --git a/test/regtest/block.js b/test/regtest/block.js deleted file mode 100644 index 74e9f69d..00000000 --- a/test/regtest/block.js +++ /dev/null @@ -1,502 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var spawn = require('child_process').spawn; -var path = require('path'); -var rimraf = require('rimraf'); -var mkdirp = require('mkdirp'); -var fs = require('fs'); -var async = require('async'); -var RPC = require('bitcoind-rpc'); -var http = require('http'); - -var rpc1Address; -var rpc2Address; -var tx1; -var tx2; -var block; - -var rpcConfig = { - protocol: 'http', - user: 'local', - pass: 'localtest', - host: '127.0.0.1', - port: 58332, - rejectUnauthorized: false -}; - -var rpc1 = new RPC(rpcConfig); -rpcConfig.port++; -var rpc2 = new RPC(rpcConfig); -var debug = true; -var bitcoreDataDir = '/tmp/bitcore'; -var bitcoinDataDirs = ['/tmp/bitcoin1', '/tmp/bitcoin2']; - -var bitcoin = { - args: { - datadir: null, - listen: 1, - regtest: 1, - server: 1, - rpcuser: 'local', - rpcpassword: 'localtest', - //printtoconsole: 1 - rpcport: 58332, - }, - datadir: null, - exec: 'bitcoind', //if this isn't on your PATH, then provide the absolute path, e.g. /usr/local/bin/bitcoind - processes: [] -}; - -var bitcore = { - configFile: { - file: bitcoreDataDir + '/bitcore-node.json', - conf: { - network: 'regtest', - port: 53001, - datadir: bitcoreDataDir, - services: [ - 'p2p', - 'db', - 'header', - 'block', - 'address', - 'transaction', - 'mempool', - 'web', - 'insight-api', - 'fee', - 'timestamp' - ], - servicesConfig: { - 'p2p': { - 'peers': [ - { 'ip': { 'v4': '127.0.0.1' }, port: 18444 } - ] - }, - 'insight-api': { - 'routePrefix': 'api' - } - } - } - }, - httpOpts: { - protocol: 'http:', - hostname: 'localhost', - port: 53001, - }, - opts: { cwd: bitcoreDataDir }, - datadir: bitcoreDataDir, - exec: path.resolve(__dirname, '../../bin/bitcore-node'), - args: ['start'], - process: null -}; - -var startBitcoind = function(count, callback) { - - var listenCount = 0; - console.log('starting ' + count + ' bitcoind\'s'); - async.timesSeries(count, function(n, next) { - - var datadir = bitcoinDataDirs.shift(); - - bitcoin.datadir = datadir; - bitcoin.args.datadir = datadir; - - if (listenCount++ > 0) { - bitcoin.args.listen = 0; - bitcoin.args.rpcport++; - bitcoin.args.connect = '127.0.0.1'; - } - - rimraf(datadir, function(err) { - - if(err) { - return next(err); - } - - mkdirp(datadir, function(err) { - - if(err) { - return next(err); - } - - var args = bitcoin.args; - var argList = Object.keys(args).map(function(key) { - return '-' + key + '=' + args[key]; - }); - - var bitcoinProcess = spawn(bitcoin.exec, argList, bitcoin.opts); - bitcoin.processes.push(bitcoinProcess); - - bitcoinProcess.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - - bitcoinProcess.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - next(); - - }); - - }); - }, function(err) { - - if (err) { - return callback(err); - } - - var pids = bitcoin.processes.map(function(process) { - return process.pid; - }); - - console.log(count + ' bitcoind\'s started at pid(s): ' + pids); - callback(); - }); -}; - - -var shutdownBitcoind = function(callback) { - bitcoin.processes.forEach(function(process) { - process.kill(); - }); - callback(); -}; - -var shutdownBitcore = function(callback) { - if (bitcore.process) { - bitcore.process.kill(); - } - callback(); -}; - - -var buildInitialChain = function(callback) { - async.waterfall([ - function(next) { - console.log('checking to see if bitcoind\'s are connected to each other.'); - rpc1.getinfo(function(err, res) { - if (err || res.result.connections !== 1) { - next(err || new Error('bitcoind\'s not connected to each other.')); - } - next(); - }); - }, - function(next) { - console.log('generating 101 blocks'); - rpc1.generate(101, next); - }, - function(res, next) { - console.log('getting new address from rpc2'); - rpc2.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc2Address = res.result; - console.log(rpc2Address); - next(null, rpc2Address); - }); - }, - function(addr, next) { - rpc1.sendToAddress(rpc2Address, 25, next); - }, - function(res, next) { - tx1 = res.result; - console.log('TXID: ' + res.result); - console.log('generating 7 blocks'); - rpc1.generate(7, next); - }, - function(res, next) { - block = res.result[res.result.length - 1]; - rpc2.getBalance(function(err, res) { - console.log(res); - next(); - }); - }, - function(next) { - console.log('getting new address from rpc1'); - rpc1.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc1Address = res.result; - next(null, rpc1Address); - }); - }, - function(addr, next) { - rpc2.sendToAddress(rpc1Address, 20, next); - }, - function(res, next) { - tx2 = res.result; - console.log('sending from rpc2Address TXID: ', res); - console.log('generating 6 blocks'); - rpc2.generate(6, next); - } - ], function(err) { - - if (err) { - return callback(err); - } - rpc1.getInfo(function(err, res) { - console.log(res); - callback(); - }); - }); - -}; - -var startBitcore = function(callback) { - - rimraf(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - mkdirp(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - fs.writeFileSync(bitcore.configFile.file, JSON.stringify(bitcore.configFile.conf)); - - var args = bitcore.args; - bitcore.process = spawn(bitcore.exec, args, bitcore.opts); - - bitcore.process.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - bitcore.process.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - callback(); - }); - - }); - - -}; - -describe('Block', function() { - - this.timeout(60000); - - before(function(done) { - - async.series([ - function(next) { - startBitcoind(2, next); - }, - function(next) { - setTimeout(function() { - buildInitialChain(next); - }, 8000); - }, - function(next) { - setTimeout(function() { - startBitcore(next); - }, 6000); - } - ], function(err) { - if (err) { - return done(err); - } - setTimeout(done, 2000); - }); - - }); - - after(function(done) { - shutdownBitcoind(function() { - shutdownBitcore(done); - }); - }); - - it('should get blocks: /blocks', function(done) { - - var request = http.request('http://localhost:53001/api/blocks', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data.length).to.equal(114); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get a block: /block/:hash', function(done) { - - var request = http.request('http://localhost:53001/api/block/' + block, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data.hash).to.equal(block); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get a block-index: /block-index/:height', function(done) { - - var request = http.request('http://localhost:53001/api/block-index/' + '108', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.blockHash).to.equal(block); - done(); - }); - - }); - - request.write(''); - request.end(); - }); - - it('should get a raw block: /rawblock/:hash', function(done) { - - var request = http.request('http://localhost:53001/api/rawblock/' + block, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.rawblock).to.not.be.null; - done(); - }); - - }); - - request.write(''); - request.end(); - }); -}); - - - - diff --git a/test/regtest/comms.txt b/test/regtest/comms.txt deleted file mode 100644 index 5ab8a609..00000000 --- a/test/regtest/comms.txt +++ /dev/null @@ -1,30 +0,0 @@ -[2017-08-16T13:44:43.245Z] info: Connecting to p2p network. -client sending: magic:: 0b110907 command:: 76657273696f6e0000000000 length:: 65000000 checksum:: 735475bc message:: 7111010001000000000000004b4c945900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e2b8235df4c158a0f2f626974636f72653a312e312e322f0000000001 -server sending: magic:: 0b110907 command:: 76657273696f6e0000000000 length:: 67000000 checksum:: 46b5c9ae -client sending: magic:: 0b110907 command:: 76657261636b000000000000 length:: 00000000 checksum:: 5df6e0e2 message:: -server sending: magic:: 0b110907 command:: 76657261636b000000000000 length:: 00000000 checksum:: 5df6e0e2 -[2017-08-16T13:44:43.261Z] info: Connected to peer: 192.168.3.5, network: regtest, version: 70015, subversion: /Satoshi:0.14.99/, status: ready, port: 18333, best height: 1178711 -[2017-08-16T13:44:43.262Z] info: Header Service: Gathering: 2001 header(s) from the peer-to-peer network. -[2017-08-16T13:44:43.262Z] info: Header Service: download progress: 1176710/1178711 (99.83%) -client sending: magic:: 0b110907 command:: 676574686561646572730000 length:: 45000000 checksum:: 857caf8b message:: 7111010001145ed5b8587723d506f208c0aaf9c4d628bcba4bacd1d30f90270000000000000000000000000000000000000000000000000000000000000000000000000000 -server sending: magic:: command:: length:: checksum:: -server sending: magic:: 0b110907 command:: 616c65727400000000000000 length:: a8000000 checksum:: 1bf9aaea -server sending: magic:: 0b110907 command:: 70696e670000000000000000 length:: 08000000 checksum:: 7c640b03 -client sending: magic:: 0b110907 command:: 706f6e670000000000000000 length:: 08000000 checksum:: 7c640b03 message:: e79ac440be90a476 -server sending: magic:: 0b110907 command:: 676574686561646572730000 length:: 25040000 checksum:: 552dc886 -server sending: magic:: command:: length:: checksum:: -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: d3780200 checksum:: 29213586 -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: d3780200 checksum:: 29213586 -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: d3780200 checksum:: 29213586 -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: d3780200 checksum:: 29213586 -server sending: magic:: command:: length:: checksum:: -[2017-08-16T13:44:43.411Z] info: Header Service: download progress: 1178710/1178711 (100.00%) -client sending: magic:: 0b110907 command:: 676574686561646572730000 length:: 45000000 checksum:: cd33b9da message:: 71110100019f1309c60de611c5cdec7e0b24fb00da0d16fb706f1ae21a500f0000000000000000000000000000000000000000000000000000000000000000000000000000 -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: 52000000 checksum:: a4022af1 -server sending: magic:: 0b110907 command:: 686561646572730000000000 length:: 52000000 checksum:: a4022af1 -server sending: magic:: command:: length:: checksum:: -[2017-08-16T13:44:43.419Z] info: localhost-header subscribe: p2p/block total: 1 -[2017-08-16T13:44:43.419Z] info: Header Service: emitting headers to block service. -[2017-08-16T13:44:43.419Z] info: Block Service: Gathering: 0 block(s) from the peer-to-peer network. -[2017-08-16T13:44:43.419Z] info: Block Service: The best block hash is: 00000000000004842ea914123b8010541a41174a11ba62b244d0aec19840467c at height: 1178711 - diff --git a/test/regtest/data/blocks_reorg.json b/test/regtest/data/blocks_reorg.json deleted file mode 100644 index 81968429..00000000 --- a/test/regtest/data/blocks_reorg.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - "000000201a3c951a20b5d603144ce060c86e95fed1869524e66acfc46bdf08d96f664209b4b1c32ec485f4ad27c5402a1b16a0b1135364b7c9b0dcf4276f9fa3fd215d1b08cc9559ffff7f20000000000102000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03570101ffffffff0200f2052a01000000232102a5566542d1f0f202541d98755628a41dcd4416b50db820e2b04d5ecb0bd02b73ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf900000000" -] - diff --git a/test/regtest/reorg.js b/test/regtest/reorg.js deleted file mode 100644 index 179fa865..00000000 --- a/test/regtest/reorg.js +++ /dev/null @@ -1,276 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var net = require('net'); -var spawn = require('child_process').spawn; -var path = require('path'); -var rimraf = require('rimraf'); -var mkdirp = require('mkdirp'); -var fs = require('fs'); -var p2p = require('bitcore-p2p'); -var bitcore = require('bitcore-lib'); -var Networks = bitcore.Networks; -var Header = bitcore.BlockHeader; -var Block = bitcore.Block; -var BcoinBlock = require('bcoin').block; -var http = require('http'); - -Networks.enableRegtest(); -var messages = new p2p.Messages({ network: Networks.get('regtest'), Block: BcoinBlock }); -var server; -var rawBlocks = require('./data/blocks.json'); -var rawReorgBlocks = require('./data/blocks_reorg.json')[0]; - -var reorgBlock = BcoinBlock.fromRaw(rawReorgBlocks, 'hex'); - -var blocks = rawBlocks.map(function(rawBlock) { - return new Block(new Buffer(rawBlock, 'hex')); -}); - -var headers = blocks.map(function(block) { - return block.header; -}); - -var debug = true; -var bitcoreDataDir = '/tmp/bitcore'; - -var bitcore = { - configFile: { - file: bitcoreDataDir + '/bitcore-node.json', - conf: { - network: 'regtest', - port: 53001, - datadir: bitcoreDataDir, - services: [ - 'p2p', - 'db', - 'header', - 'block', - 'address', - 'transaction', - 'mempool', - 'web', - 'insight-api', - 'fee', - 'timestamp' - ], - servicesConfig: { - 'p2p': { - 'peers': [ - { 'ip': { 'v4': '127.0.0.1' }, port: 18444 } - ] - }, - 'insight-api': { - 'routePrefix': 'api' - } - } - } - }, - httpOpts: { - protocol: 'http:', - hostname: 'localhost', - port: 53001, - }, - opts: { cwd: bitcoreDataDir }, - datadir: bitcoreDataDir, - exec: path.resolve(__dirname, '../../bin/bitcore-node'), - args: ['start'], - process: null -}; - - -var blockIndex = 0; -var tcpSocket; - -var startFakeNode = function() { - server = net.createServer(function(socket) { - - tcpSocket = socket; - socket.on('end', function() { - console.log('bitcore-node has ended the connection'); - }); - - socket.on('data', function(data) { - - var command = data.slice(4, 16).toString('hex'); - var message; - - if (command === '76657273696f6e0000000000') { //version - message = messages.Version(); - } - - if (command === '76657261636b000000000000') { //verack - message = messages.VerAck(); - } - - if (command === '676574686561646572730000') { //getheaders - message = messages.Headers(headers, { BlockHeader: Header }); - } - - if (command === '676574626c6f636b73000000') { //getblocks - var block = blocks[blockIndex]; - if (!block) { - return; - } - var blockHash = block.hash; - var inv = p2p.Inventory.forBlock(blockHash); - message = messages.Inventory([inv]); - } - - if (command === '676574646174610000000000') { //getdata - var raw = rawBlocks[blockIndex++]; - var blk = BcoinBlock.fromRaw(raw, 'hex'); - message = messages.Block(blk, { Block: BcoinBlock }); - } - - if (message) { - socket.write(message.toBuffer()); - } - - }); - - socket.pipe(socket); - }); - - server.listen(18444, '127.0.0.1'); -}; - - -var shutdownFakeNode = function() { - server.close(); -}; - -var shutdownBitcore = function(callback) { - if (bitcore.process) { - bitcore.process.kill(); - } - callback(); -}; - -var startBitcore = function(callback) { - - rimraf(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - mkdirp(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - fs.writeFileSync(bitcore.configFile.file, JSON.stringify(bitcore.configFile.conf)); - - var args = bitcore.args; - bitcore.process = spawn(bitcore.exec, args, bitcore.opts); - - bitcore.process.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - bitcore.process.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - callback(); - }); - - }); - - -}; - -describe('Reorg', function() { - // 1. spin up bitcore-node and have it connect to our custom tcp socket - // 2. feed it a few headers - // 3. feed it a few blocks - // 4. feed it a block that reorgs - - this.timeout(60000); - - before(function(done) { - startFakeNode(); - startBitcore(done); - }); - - after(function(done) { - shutdownFakeNode(); - shutdownBitcore(done); - }); - - it('should reorg correctly when already synced', function(done) { - - // at this point we have a fully synced chain at height 7.... - // we now want to send a new block number 7 whose prev hash is block 6 (it should be block 7) - // we then should reorg back to block 6 then back up to the new block 7 - - setTimeout(function() { - - console.log('From Test: reorging to block: ' + reorgBlock.rhash()); - - // send the reorg block - rawBlocks.push(rawReorgBlocks); - var blockHash = reorgBlock.rhash(); - var inv = p2p.Inventory.forBlock(blockHash); - var msg = messages.Inventory([inv]); - tcpSocket.write(msg.toBuffer()); - - // wait 2 secs until the reorg happens, if it takes any longer the test ought to fail anyway - setTimeout(function() { - var error; - var request = http.request('http://localhost:53001/api/block/' + reorgBlock.rhash(), function(res) { - - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data.height).to.equal(7); - expect(data.hash).to.equal(reorgBlock.rhash()); - done(resError, resData); - }); - - }); - - request.on('error', function(e) { - error = e; - done(error); - }); - - request.write(''); - request.end(); - }, 2000); - }, 2000); - - - }); - -}); - diff --git a/test/regtest/status.js b/test/regtest/status.js deleted file mode 100644 index 2f3bdabe..00000000 --- a/test/regtest/status.js +++ /dev/null @@ -1,551 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var spawn = require('child_process').spawn; -var path = require('path'); -var rimraf = require('rimraf'); -var mkdirp = require('mkdirp'); -var fs = require('fs'); -var async = require('async'); -var RPC = require('bitcoind-rpc'); -var http = require('http'); - -var rpc1Address; -var rpc2Address; -var tx1; -var tx2; -var block; - -var rpcConfig = { - protocol: 'http', - user: 'local', - pass: 'localtest', - host: '127.0.0.1', - port: 58332, - rejectUnauthorized: false -}; - -var rpc1 = new RPC(rpcConfig); -rpcConfig.port++; -var rpc2 = new RPC(rpcConfig); -var debug = true; -var bitcoreDataDir = '/tmp/bitcore'; -var bitcoinDataDirs = ['/tmp/bitcoin1', '/tmp/bitcoin2']; - -var bitcoin = { - args: { - datadir: null, - listen: 1, - regtest: 1, - server: 1, - rpcuser: 'local', - rpcpassword: 'localtest', - //printtoconsole: 1 - rpcport: 58332, - }, - datadir: null, - exec: 'bitcoind', //if this isn't on your PATH, then provide the absolute path, e.g. /usr/local/bin/bitcoind - processes: [] -}; - -var bitcore = { - configFile: { - file: bitcoreDataDir + '/bitcore-node.json', - conf: { - network: 'regtest', - port: 53001, - datadir: bitcoreDataDir, - services: [ - 'p2p', - 'db', - 'header', - 'block', - 'address', - 'transaction', - 'mempool', - 'web', - 'insight-api', - 'fee', - 'timestamp' - ], - servicesConfig: { - 'p2p': { - 'peers': [ - { 'ip': { 'v4': '127.0.0.1' }, port: 18444 } - ] - }, - 'fee': { - 'rpc': { - 'user': 'local', - 'pass': 'localtest', - 'host': 'localhost', - 'protocol': 'http', - 'port': 58332 - } - }, - 'insight-api': { - 'routePrefix': 'api' - } - } - } - }, - httpOpts: { - protocol: 'http:', - hostname: 'localhost', - port: 53001, - }, - opts: { cwd: bitcoreDataDir }, - datadir: bitcoreDataDir, - exec: path.resolve(__dirname, '../../bin/bitcore-node'), - args: ['start'], - process: null -}; - -var startBitcoind = function(count, callback) { - - var listenCount = 0; - console.log('starting ' + count + ' bitcoind\'s'); - async.timesSeries(count, function(n, next) { - - var datadir = bitcoinDataDirs.shift(); - - bitcoin.datadir = datadir; - bitcoin.args.datadir = datadir; - - if (listenCount++ > 0) { - bitcoin.args.listen = 0; - bitcoin.args.rpcport++; - bitcoin.args.connect = '127.0.0.1'; - } - - rimraf(datadir, function(err) { - - if(err) { - return next(err); - } - - mkdirp(datadir, function(err) { - - if(err) { - return next(err); - } - - var args = bitcoin.args; - var argList = Object.keys(args).map(function(key) { - return '-' + key + '=' + args[key]; - }); - - var bitcoinProcess = spawn(bitcoin.exec, argList, bitcoin.opts); - bitcoin.processes.push(bitcoinProcess); - - bitcoinProcess.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - - bitcoinProcess.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - next(); - - }); - - }); - }, function(err) { - - if (err) { - return callback(err); - } - - var pids = bitcoin.processes.map(function(process) { - return process.pid; - }); - - console.log(count + ' bitcoind\'s started at pid(s): ' + pids); - callback(); - }); -}; - - -var shutdownBitcoind = function(callback) { - bitcoin.processes.forEach(function(process) { - process.kill(); - }); - callback(); -}; - -var shutdownBitcore = function(callback) { - if (bitcore.process) { - bitcore.process.kill(); - } - callback(); -}; - - -var buildInitialChain = function(callback) { - async.waterfall([ - function(next) { - console.log('checking to see if bitcoind\'s are connected to each other.'); - rpc1.getinfo(function(err, res) { - if (err || res.result.connections !== 1) { - next(err || new Error('bitcoind\'s not connected to each other.')); - } - next(); - }); - }, - function(next) { - console.log('generating 101 blocks'); - rpc1.generate(101, next); - }, - function(res, next) { - console.log('getting new address from rpc2'); - rpc2.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc2Address = res.result; - console.log(rpc2Address); - next(null, rpc2Address); - }); - }, - function(addr, next) { - rpc1.sendToAddress(rpc2Address, 25, next); - }, - function(res, next) { - tx1 = res.result; - console.log('TXID: ' + res.result); - console.log('generating 7 blocks'); - rpc1.generate(7, next); - }, - function(res, next) { - block = res.result[res.result.length - 1]; - rpc2.getBalance(function(err, res) { - console.log(res); - next(); - }); - }, - function(next) { - console.log('getting new address from rpc1'); - rpc1.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc1Address = res.result; - next(null, rpc1Address); - }); - }, - function(addr, next) { - rpc2.sendToAddress(rpc1Address, 20, next); - }, - function(res, next) { - tx2 = res.result; - console.log('sending from rpc2Address TXID: ', res); - console.log('generating 6 blocks'); - rpc2.generate(6, next); - } - ], function(err) { - - if (err) { - return callback(err); - } - rpc1.getInfo(function(err, res) { - console.log(res); - callback(); - }); - }); - -}; - -var startBitcore = function(callback) { - - rimraf(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - mkdirp(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - fs.writeFileSync(bitcore.configFile.file, JSON.stringify(bitcore.configFile.conf)); - - var args = bitcore.args; - bitcore.process = spawn(bitcore.exec, args, bitcore.opts); - - bitcore.process.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - bitcore.process.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - callback(); - }); - - }); - - -}; - -describe('Status', function() { - - this.timeout(60000); - - before(function(done) { - - async.series([ - function(next) { - startBitcoind(2, next); - }, - function(next) { - setTimeout(function() { - buildInitialChain(next); - }, 8000); - }, - function(next) { - setTimeout(function() { - startBitcore(next); - }, 6000); - } - ], function(err) { - if (err) { - return done(err); - } - setTimeout(done, 2000); - }); - - }); - - after(function(done) { - shutdownBitcoind(function() { - shutdownBitcore(done); - }); - }); - - it('should get status: /status', function(done) { - - var request = http.request('http://localhost:53001/api/status', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data.info).to.not.be.null; - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get status: /sync', function(done) { - - var request = http.request('http://localhost:53001/api/sync', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.status).to.equal('finished'); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get peer: /peer', function(done) { - - var request = http.request('http://localhost:53001/api/peer', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.connected).to.be.true; - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get version: /version', function(done) { - - var request = http.request('http://localhost:53001/api/version', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data.version).to.not.be.null; - done(); - }); - - }); - - request.write(''); - request.end(); - - }); - - it('should estimate fee: /estimateFee', function(done) { - - var request = http.request('http://localhost:53001/api/utils/estimateFee', function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - - if (error) { - return; - } - - return done('Error from bitcore-node webserver: ' + res.statusCode); - - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(data); - expect(data['2']).to.not.be.null; - done(); - }); - - }); - - request.write(''); - request.end(); - - }); -}); diff --git a/test/regtest/transaction.js b/test/regtest/transaction.js deleted file mode 100644 index b185012c..00000000 --- a/test/regtest/transaction.js +++ /dev/null @@ -1,450 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var spawn = require('child_process').spawn; -var path = require('path'); -var rimraf = require('rimraf'); -var mkdirp = require('mkdirp'); -var fs = require('fs'); -var async = require('async'); -var RPC = require('bitcoind-rpc'); -var http = require('http'); - -var rpc1Address; -var rpc2Address; -var tx1; -var tx2; -var block; - -var rpcConfig = { - protocol: 'http', - user: 'local', - pass: 'localtest', - host: '127.0.0.1', - port: 58332, - rejectUnauthorized: false -}; - -var rpc1 = new RPC(rpcConfig); -rpcConfig.port++; -var rpc2 = new RPC(rpcConfig); -var debug = true; -var bitcoreDataDir = '/tmp/bitcore'; -var bitcoinDataDirs = ['/tmp/bitcoin1', '/tmp/bitcoin2']; - -var bitcoin = { - args: { - datadir: null, - listen: 1, - regtest: 1, - server: 1, - rpcuser: 'local', - rpcpassword: 'localtest', - //printtoconsole: 1 - rpcport: 58332, - }, - datadir: null, - exec: 'bitcoind', //if this isn't on your PATH, then provide the absolute path, e.g. /usr/local/bin/bitcoind - processes: [] -}; - -var bitcore = { - configFile: { - file: bitcoreDataDir + '/bitcore-node.json', - conf: { - network: 'regtest', - port: 53001, - datadir: bitcoreDataDir, - services: [ - 'p2p', - 'db', - 'header', - 'block', - 'address', - 'transaction', - 'mempool', - 'web', - 'insight-api', - 'fee', - 'timestamp' - ], - servicesConfig: { - 'p2p': { - 'peers': [ - { 'ip': { 'v4': '127.0.0.1' }, port: 18444 } - ] - }, - 'insight-api': { - 'routePrefix': 'api' - } - } - } - }, - httpOpts: { - protocol: 'http:', - hostname: 'localhost', - port: 53001, - }, - opts: { cwd: bitcoreDataDir }, - datadir: bitcoreDataDir, - exec: path.resolve(__dirname, '../../bin/bitcore-node'), - args: ['start'], - process: null -}; - -var startBitcoind = function(count, callback) { - - var listenCount = 0; - console.log('starting ' + count + ' bitcoind\'s'); - async.timesSeries(count, function(n, next) { - - var datadir = bitcoinDataDirs.shift(); - - bitcoin.datadir = datadir; - bitcoin.args.datadir = datadir; - - if (listenCount++ > 0) { - bitcoin.args.listen = 0; - bitcoin.args.rpcport++; - bitcoin.args.connect = '127.0.0.1'; - } - - rimraf(datadir, function(err) { - - if(err) { - return next(err); - } - - mkdirp(datadir, function(err) { - - if(err) { - return next(err); - } - - var args = bitcoin.args; - var argList = Object.keys(args).map(function(key) { - return '-' + key + '=' + args[key]; - }); - - var bitcoinProcess = spawn(bitcoin.exec, argList, bitcoin.opts); - bitcoin.processes.push(bitcoinProcess); - - bitcoinProcess.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - - bitcoinProcess.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - next(); - - }); - - }); - }, function(err) { - - if (err) { - return callback(err); - } - - var pids = bitcoin.processes.map(function(process) { - return process.pid; - }); - - console.log(count + ' bitcoind\'s started at pid(s): ' + pids); - callback(); - }); -}; - - -var shutdownBitcoind = function(callback) { - bitcoin.processes.forEach(function(process) { - process.kill(); - }); - callback(); -}; - -var shutdownBitcore = function(callback) { - if (bitcore.process) { - bitcore.process.kill(); - } - callback(); -}; - - -var buildInitialChain = function(callback) { - async.waterfall([ - function(next) { - console.log('checking to see if bitcoind\'s are connected to each other.'); - rpc1.getinfo(function(err, res) { - if (err || res.result.connections !== 1) { - next(err || new Error('bitcoind\'s not connected to each other.')); - } - next(); - }); - }, - function(next) { - console.log('generating 101 blocks'); - rpc1.generate(101, next); - }, - function(res, next) { - console.log('getting new address from rpc2'); - rpc2.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc2Address = res.result; - console.log(rpc2Address); - next(null, rpc2Address); - }); - }, - function(addr, next) { - rpc1.sendToAddress(rpc2Address, 25, next); - }, - function(res, next) { - tx1 = res.result; - console.log('TXID: ' + res.result); - console.log('generating 6 blocks'); - rpc1.generate(7, next); - }, - function(res, next) { - block = res.result[res.result.length - 1]; - rpc2.getBalance(function(err, res) { - console.log(res); - next(); - }); - }, - function(next) { - console.log('getting new address from rpc1'); - rpc1.getNewAddress(function(err, res) { - if (err) { - return next(err); - } - rpc1Address = res.result; - next(null, rpc1Address); - }); - }, - function(addr, next) { - rpc2.sendToAddress(rpc1Address, 20, next); - }, - function(res, next) { - tx2 = res.result; - console.log('sending from rpc2Address TXID: ', res); - console.log('generating 6 blocks'); - rpc2.generate(6, next); - } - ], function(err) { - - if (err) { - return callback(err); - } - rpc1.getInfo(function(err, res) { - console.log(res); - callback(); - }); - }); - -}; - -var startBitcore = function(callback) { - - rimraf(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - mkdirp(bitcoreDataDir, function(err) { - - if(err) { - return callback(err); - } - - fs.writeFileSync(bitcore.configFile.file, JSON.stringify(bitcore.configFile.conf)); - - var args = bitcore.args; - bitcore.process = spawn(bitcore.exec, args, bitcore.opts); - - bitcore.process.stdout.on('data', function(data) { - - if (debug) { - process.stdout.write(data.toString()); - } - - }); - bitcore.process.stderr.on('data', function(data) { - - if (debug) { - process.stderr.write(data.toString()); - } - - }); - - callback(); - }); - - }); - - -}; - -describe('Transaction', function() { - - this.timeout(60000); - - before(function(done) { - - async.series([ - function(next) { - startBitcoind(2, next); - }, - function(next) { - setTimeout(function() { - buildInitialChain(next); - }, 8000); - }, - function(next) { - setTimeout(function() { - startBitcore(next); - }, 6000); - } - ], function(err) { - if (err) { - return done(err); - } - setTimeout(done, 2000); - }); - - }); - - after(function(done) { - shutdownBitcoind(function() { - shutdownBitcore(done); - }); - }); - - it('should get a transaction: /tx/:txid', function(done) { - - var request = http.request('http://localhost:53001/api/tx/' + tx1, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - expect(data.txid).to.equal(tx1); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get transactions: /txs', function(done) { - - var request = http.request('http://localhost:53001/api/txs?block=' + block, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.txs.length).to.equal(1); - done(); - }); - - }); - request.write(''); - request.end(); - }); - - it('should get a raw transactions: /rawtx/:txid', function(done) { - - var request = http.request('http://localhost:53001/api/rawtx/' + tx2, function(res) { - - var error; - if (res.statusCode !== 200 && res.statusCode !== 201) { - if (error) { - return; - } - return done('Error from bitcore-node webserver: ' + res.statusCode); - } - - var resError; - var resData = ''; - - res.on('error', function(e) { - resError = e; - }); - - res.on('data', function(data) { - resData += data; - }); - - res.on('end', function() { - if (error) { - return; - } - var data = JSON.parse(resData); - console.log(resData); - expect(data.rawtx).to.not.be.null; - done(); - }); - - }); - request.write(''); - request.end(); - }); -}); - - - diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index fcf5e467..c349b0c2 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -12,7 +12,7 @@ var bcoin = require('bcoin'); describe('Address Service', function() { var tx = Tx.fromRaw( '0100000004de9b4bb17f627096a9ee0b4528e4eae17df5b5c69edc29704c2e84a7371db29f010000006b483045022100f5b1a0d33b7be291c3953c25f8ae39d98601aa7099a8674daf638a08b86c7173022006ce372da5ad088a1cc6e5c49c2760a1b6f085eb1b51b502211b6bc9508661f9012102ec5e3731e54475dd2902326f43602a03ae3d62753324139163f81f20e787514cffffffff7a1d4e5fc2b8177ec738cd723a16cf2bf493791e55573445fc0df630fe5e2d64010000006b483045022100cf97f6cb8f126703e9768545dfb20ffb10ba78ae3d101aa46775f5a239b075fc02203150c4a89a11eaf5e404f4f96b62efa4455e9525765a025525c7105a7e47b6db012102c01e11b1d331f999bbdb83e8831de503cd52a01e3834a95ccafd615c67703d77ffffffff9e52447116415ca0d0567418a1a4ef8f27be3ff5a96bf87c922f3723d7db5d7c000000006b483045022100f6c117e536701be41a6b0b544d7c3b1091301e4e64a6265b6eb167b15d16959d022076916de4b115e700964194ce36a24cb9105f86482f4abbc63110c3f537cd5770012102ddf84cc7bee2d6a82ac09628a8ad4a26cd449fc528b81e7e6cc615707b8169dfffffffff5815d9750eb3572e30d6fd9df7afb4dbd76e042f3aa4988ac763b3fdf8397f80010000006a473044022028f4402b736066d93d2a32b28ccd3b7a21d84bb58fcd07fe392a611db94cdec5022018902ee0bf2c3c840c1b81ead4e6c87c88c48b2005bf5eea796464e561a620a8012102b6cdd1a6cd129ef796faeedb0b840fcd0ca00c57e16e38e46ee7028d59812ae7ffffffff0220a10700000000001976a914c342bcd1a7784d9842f7386b8b3b8a3d4171a06e88ac59611100000000001976a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac00000000', 'hex'); - var blocks = require('../../regtest/data/blocks.json'); + var blocks = require('../../data/blocks.json'); var addressService; var sandbox; @@ -54,6 +54,7 @@ describe('Address Service', function() { describe('#getAddressHistory', function() { + it('should get the address history', function(done) { sandbox.stub(addressService, '_getAddressHistory').callsArgWith(2, null, {}); @@ -78,8 +79,14 @@ describe('Address Service', function() { it('should get the address history', function(done) { var encoding = new Encoding(new Buffer('0001', 'hex')); addressService._encoding = encoding; + var getHeaderHash = sandbox.stub().callsArgWith(1, null, 'aa'); - addressService._header = { getHeaderHash: getHeaderHash }; + var getBlockHeader = sandbox.stub().callsArgWith(1, null, 'aa'); + + addressService._header = { + getHeaderHash: getHeaderHash, + getBlockHeader: getBlockHeader + }; var address = 'a'; var opts = { from: 12, to: 14 }; var txid = '1c6ea4a55a3edaac0a05e93b52908f607376a8fdc5387c492042f8baa6c05085'; @@ -198,7 +205,7 @@ describe('Address Service', function() { expect(res[0]).to.deep.equal({ address: 'a', amount: 0.0012, - confirmations: 27, + height: 123, confirmationsFromCache: true, satoshis: 120000, scriptPubKey: '76a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac', @@ -229,8 +236,6 @@ describe('Address Service', function() { expect(ops[0].type).to.equal('del'); done(); - - }); }); diff --git a/test/services/block/index.unit.js b/test/services/block/index.unit.js index d2225727..01d63027 100644 --- a/test/services/block/index.unit.js +++ b/test/services/block/index.unit.js @@ -10,7 +10,7 @@ var Encoding = require('../../../lib/services/block/encoding'); describe('Block Service', function() { var blockService; - var blocks = require('../../regtest/data/blocks.json'); + var blocks = require('../../data/blocks.json'); var block1 = Block.fromRaw('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex'); var block2 = Block.fromRaw('01000000fb3c5deea3902d5e6e0222435688795152ae0f737715b0bed6a88b00000000008ec0f92d33b05617cb3c3b4372aa0c2ae3aeb8aa7f34fe587db8e55b578cfac6b601514c5a0c011c98a831000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c027f01ffffffff0100f2052a0100000043410495fee5189566db550919ad2b4e5f9111dbdc2cb60b5c71ea4c0fdad59a961c42eb289e5b9fdc4cb3f3fec6dd866172720bae3e3b881fc203fcaf98bf902c53f1ac00000000', 'hex');