Fixed tests.

This commit is contained in:
Chris Kleeschulte 2017-08-24 09:38:07 -04:00
parent a1ca054fb8
commit 5e5a58db19
9 changed files with 1338 additions and 1172 deletions

View File

@ -4,6 +4,7 @@ var bitcore = require('bitcore-lib');
var async = require('async');
var TxController = require('./transactions');
var Common = require('./common');
var _ = require('lodash');
function AddressController(node) {
this.node = node;
@ -26,10 +27,10 @@ AddressController.prototype.show = function(req, res) {
}
this._address.getAddressSummary(req.addr, options, function(err, data) {
if(err) {
return self.common.handleErrors(err, res);
}
res.jsonp(data);
});
};
@ -63,7 +64,7 @@ AddressController.prototype.addressSummarySubQuery = function(req, res, param) {
AddressController.prototype.getAddressSummary = function(address, options, callback) {
this.node.getAddressSummary(address, options, function(err, summary) {
this._address.getAddressSummary(address, options, function(err, summary) {
if(err) {
return callback(err);
}
@ -128,7 +129,7 @@ AddressController.prototype.check = function(req, res, next, addresses) {
AddressController.prototype.utxo = function(req, res) {
var self = this;
this.node.getAddressUnspentOutputs(req.addr, {}, function(err, utxos) {
this._address.getAddressUnspentOutputs(req.addr, {}, function(err, utxos) {
if(err) {
return self.common.handleErrors(err, res);
} else if (!utxos.length) {
@ -143,9 +144,16 @@ AddressController.prototype.multiutxo = function(req, res) {
var finalUtxos = [];
async.eachLimit(req.addrs, 4, function(addr, next) {
var addresses;
if (_.isArray(req.addrs)) {
addresses = req.addrs;
} else {
addresses = req.addrs.split(',');
}
self.node.getAddressUnspentOutputs(addr, {}, function(err, utxos) {
async.eachLimit(addresses, 4, function(addr, next) {
self._address.getAddressUnspentOutputs(addr, {}, function(err, utxos) {
if (err) {
return next(err);
@ -204,7 +212,7 @@ AddressController.prototype.multitxs = function(req, res) {
options.to = parseInt(req.query.to) || parseInt(req.body.to) || parseInt(options.from) + 10;
self.node.getAddressHistory(req.addrs, options, function(err, result) {
self._address.getAddressHistory(req.addrs, options, function(err, result) {
if(err) {
return self.common.handleErrors(err, res);

View File

@ -3,7 +3,6 @@
var Stream = require('stream');
var util = require('util');
var async = require('async');
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var pools = require('../pools.json');
@ -136,8 +135,7 @@ BlockController.prototype.transformBlock = function(block, info) {
return {
hash: block.rhash(),
size: block.size,
virtualSize: block.virtualSize,
size: block.getSize(),
height: info.height,
version: block.version,
merkleroot: block.merkleRoot,

View File

@ -12,6 +12,8 @@ function TxController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
this._block = this.node.services.block;
this._transaction = this.node.services.transaction;
this._address = this.node.services.address;
}
TxController.prototype.show = function(req, res) {
@ -27,7 +29,7 @@ TxController.prototype.transaction = function(req, res, next) {
var self = this;
var txid = req.params.txid;
this.node.getDetailedTransaction(txid, function(err, transaction) {
this._transaction.getDetailedTransaction(txid, function(err, transaction) {
if (err) {
return self.common.handleErrors(err, res);
}
@ -47,6 +49,7 @@ TxController.prototype.transaction = function(req, res, next) {
});
};
// transaction parameter is a bcoin transaction
TxController.prototype.transformTransaction = function(transaction, options, callback) {
if (_.isFunction(options)) {
@ -57,8 +60,9 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
$.checkArgument(_.isFunction(callback));
var confirmations = 0;
if(transaction.height >= 0) {
confirmations = this._block.getTip().height - transaction.height + 1;
if(transaction.__height >= 0) {
confirmations = this._block.getTip().height - transaction.__height + 1;
}
var transformed = {
@ -68,6 +72,7 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
};
if(transaction.inputs[0].isCoinbase()) {
transformed.isCoinBase = true;
transformed.vin = [
{
coinbase: transaction.inputs[0].script.toJSON(),
@ -78,13 +83,15 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
} else {
options.inputValues = transaction.__inputValues;
transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options));
transformed.valueIn = transaction.inputSatoshis / 1e8;
transformed.fees = transaction.feeSatoshis / 1e8;
}
transformed.vout = transaction.outputs.map(this.transformOutput.bind(this, options));
transformed.blockhash = transaction.blockHash;
transformed.blockhash = transaction.__blockhash;
transformed.blockheight = transaction.__height;
transformed.confirmations = transaction.confirmations;
transformed.confirmations = confirmations;
var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000);
transformed.time = time;
@ -92,16 +99,8 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
transformed.blocktime = transformed.time;
}
if(transaction.inputs[0].isCoinbase()) {
transformed.isCoinBase = true;
}
transformed.valueOut = transaction.outputSatoshis / 1e8;
transformed.size = transaction.getSize();
if (!transaction.inputs[0].isCoinbase()) {
transformed.valueIn = transaction.inputSatoshis / 1e8;
transformed.fees = transaction.feeSatoshis / 1e8;
}
callback(null, transformed);
};
@ -155,9 +154,10 @@ TxController.prototype.transformOutput = function(options, output, index) {
}
if (!options.noSpent) {
transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation
transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex;
transformed.spentHeight = output.spentHeight || null;
// These aren't implemented in the new api
//transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation
//transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex;
//transformed.spentHeight = output.spentHeight || null;
}
var address = output.getAddress();
@ -206,7 +206,7 @@ TxController.prototype.rawTransaction = function(req, res, next) {
var self = this;
var txid = req.params.txid;
this.node.getTransaction(txid, function(err, transaction) {
this._transaction.getTransaction(txid, function(err, transaction) {
if (err && err.code === -5) {
return self.common.handleErrors(null, res);
} else if(err) {
@ -238,7 +238,7 @@ TxController.prototype.list = function(req, res) {
var pagesTotal = 1;
if(blockHash) {
self.node.getBlockOverview(blockHash, function(err, block) {
self._block.getBlockOverview(blockHash, function(err, block) {
if(err && err.code === -5) {
return self.common.handleErrors(null, res);
} else if(err) {
@ -257,7 +257,7 @@ TxController.prototype.list = function(req, res) {
}
async.mapSeries(txids, function(txid, next) {
self.node.getDetailedTransaction(txid, function(err, transaction) {
self._transaction.getDetailedTransaction(txid, {}, function(err, transaction) {
if (err) {
return next(err);
}
@ -281,19 +281,13 @@ TxController.prototype.list = function(req, res) {
to: (page + 1) * pageLength
};
self.node.getAddressHistory(address, options, function(err, result) {
self._address.getAddressHistory(address, options, function(err, result) {
if(err) {
return self.common.handleErrors(err, res);
}
var txs = result.items.map(function(info) {
return info.tx;
}).filter(function(value, index, self) {
return self.indexOf(value) === index;
});
async.map(
txs,
result.items,
function(tx, next) {
self.transformTransaction(tx, next);
},
@ -315,7 +309,7 @@ TxController.prototype.list = function(req, res) {
TxController.prototype.send = function(req, res) {
var self = this;
this.node.sendTransaction(req.body.rawtx, function(err, txid) {
this._transaction.sendTransaction(req.body.rawtx, function(err, txid) {
if(err) {
// TODO handle specific errors
return self.common.handleErrors(err, res);

999
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,7 @@
"dependencies": {
"JSONStream": "^1.3.1",
"async": "*",
"bcoin": "bcoin-org/bcoin#886008a1822ce1da7fa8395ee7db4bcc1750a28a",
"bitcore-lib": "5.0.0-beta.1",
"bitcore-message": "^1.0.1",
"body-parser": "^1.13.3",

View File

@ -4,6 +4,7 @@ var should = require('should');
var AddressController = require('../lib/addresses');
var _ = require('lodash');
var bitcore = require('bitcore-lib');
var bcoin = require('bcoin');
var txinfos = {
totalCount: 2,
@ -144,13 +145,17 @@ var tx = {
locktime: 0
};
var rawHex = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d010bffffffff0100f2052a010000004341047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac00000000";
var bcoinTx = bcoin.tx.fromRaw(rawHex, 'hex');
bcoinTx.__blockhash = '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013';
bcoinTx.__height = 534181;
bcoinTx.__timestamp = 1441116143;
bcoinTx.outputSatoshis = 53829829;
var txinfos2 = {
totalCount: 1,
items: [
{
tx: tx
}
]
items: [ bcoinTx ]
};
var utxos = [
@ -191,8 +196,13 @@ describe('Addresses', function() {
};
describe('/addr/:addr', function() {
var node = {
services: { address: {} },
getAddressSummary: sinon.stub().callsArgWith(2, null, summary)
services: {
address: {
getAddressSummary: sinon.stub().callsArgWith(2, null, summary)
}
}
};
var addresses = new AddressController(node);
@ -201,40 +211,17 @@ describe('Addresses', function() {
query: {}
};
it('should have correct data', function(done) {
var insight = {
'addrStr': 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er',
'balance': 0,
'balanceSat': 0,
'totalReceived': 27.82729129,
'totalReceivedSat': 2782729129,
'totalSent': 27.82729129,
'totalSentSat': 2782729129,
'unconfirmedBalance': 0,
'unconfirmedBalanceSat': 0,
'unconfirmedTxApperances': 0,
'txApperances': 2,
'transactions': [
'bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7',
'01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3'
]
};
var res = {
jsonp: function(data) {
should(data).eql(insight);
done();
it('handle error', function() {
var testnode = {
services: {
address: {
getAddressSummary: sinon.stub().callsArgWith(2, new Error('test'))
}
}
};
addresses.show(req, res);
});
it('handle error', function() {
var testnode = { services: { address: { getTip: sinon.stub().returns({ height: 123 }) } } };
testnode.log = {};
testnode.log.error = sinon.stub();
var controller = new AddressController(testnode);
controller.getAddressSummary = sinon.stub().callsArgWith(2, new Error('test'));
var req = {
query: {
noTxList: 1
@ -329,8 +316,15 @@ describe('Addresses', function() {
];
var node = {
services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534230 }) } },
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1))
services: {
block: {
getTip: sinon.stub().returns({ height: 534230 })
},
address: {
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1))
}
},
};
var addresses = new AddressController(node);
@ -352,7 +346,9 @@ describe('Addresses', function() {
});
describe('/addrs/:addrs/utxo', function() {
it('should have the correct data', function(done) {
var insight = [
{
'address': 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK',
@ -388,9 +384,20 @@ describe('Addresses', function() {
}
];
var utxoStub = sinon.stub();
utxoStub.onCall(0).callsArgWith(2, null, [utxos[0]]);
utxoStub.onCall(1).callsArgWith(2, null, [utxos[1]]);
var node = {
services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534230 }) } },
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos)
services: {
address: {
getAddressUnspentOutputs: utxoStub
},
block: {
getTip: sinon.stub().returns({ height: 534230 })
}
},
};
var addresses = new AddressController(node);
@ -412,94 +419,38 @@ describe('Addresses', function() {
});
describe('/addrs/:addrs/txs', function() {
it('should have correct data', function(done) {
var insight = {
'totalItems': 1,
'from': 0,
'to': 1,
'items': [
{
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'txid': '9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a7a1cde251e54ccfdd5',
'version': 1,
'isCoinBase': true,
'locktime': 0,
'vin': [
{
'txid': 'ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3',
'vout': 1,
'scriptSig': {
'asm': '3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d',
'hex': '473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d'
},
'coinbase': '04ffff001d010b',
'sequence': 4294967295,
'n': 0,
'addr': 'moFfnRwt77pApKnnU6m5uocFaa43aAYpt5',
'valueSat': 53540000,
'value': 0.5354,
'doubleSpentTxID': null
},
{
'txid': '980a9cc2dbc2d3464eb9900ae6d579a03045408563320f62d99316c3d4ff58b7',
'vout': 2,
'scriptSig': {
'asm': '3044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b01 04d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2',
'hex': '473044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b014104d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2'
},
'sequence': 4294967295,
'n': 1,
'addr': 'n1XJBAyU4hNR4xRtY3UxnmAteoJX83p5qv',
'valueSat': 299829,
'value': 0.00299829,
'doubleSpentTxID': null
'n': 0
}
],
'vout': [
{
'value': '0.00220000',
'value': '50.00000000',
'n': 0,
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d OP_EQUALVERIFY OP_CHECKSIG',
'hex': '76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac',
'reqSigs': 1,
'asm': '047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77 OP_CHECKSIG',
'hex': '41047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac',
'type': 'pubkeyhash',
'addresses': [
'mxT2KzTUQvsaYYothDtjcdvyAdaHA9ofMp'
'1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1'
]
},
'spentHeight': null,
'spentIndex': null,
'spentTxId': null
},
{
'value': '0.53320000',
'n': 1,
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 d2ec20bb8e5f25a52f730384b803d95683250e0b OP_EQUALVERIFY OP_CHECKSIG',
'hex': '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': [
'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK'
],
},
'spentHeight': null,
'spentIndex': null,
'spentTxId': null
},
{
'value': '0.00289829',
'n': 2,
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 583df9fa56ad961051e00ca93e68dfaf1eab9ec5 OP_EQUALVERIFY OP_CHECKSIG',
'hex': '76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': [
'moZY18rGNmh4YCPeugtGW46AkkWMQttBUD'
]
},
'spentHeight': null,
'spentIndex': null,
'spentTxId': null
}
}
],
'blockhash': '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013',
@ -508,43 +459,20 @@ describe('Addresses', function() {
'time': 1441116143,
'blocktime': 1441116143,
'valueOut': 0.53829829,
'size': 470,
'valueIn': 0.53839829,
'fees': 0.0001,
'firstSeenTs': 1441108193
}
]
};
var todos = {
'items': [
{
'vout': [
{
'scriptPubKey': {
'reqSigs': 1,
}
},
{
'scriptPubKey': {
'reqSigs': 1,
}
},
{
'scriptPubKey': {
'reqSigs': 1,
}
}
],
'firstSeenTs': 1441108193
'size': 134
}
]
};
var node = {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
services: { address: {}, block: { getTip: sinon.stub().returns({ height: 534232 }) } },
network: 'testnet'
services: {
address: {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
},
block: {
getTip: sinon.stub().returns({ height: 534232 })
}
}
};
var addresses = new AddressController(node);
@ -557,14 +485,14 @@ describe('Addresses', function() {
var res = {
jsonp: function(data) {
var merged = _.merge(data, todos);
should(merged).eql(insight);
should(data).eql(insight);
done();
}
};
addresses.multitxs(req, res);
});
it('should have trimmed data', function(done) {
var insight = {
'totalItems': 1,
@ -572,65 +500,26 @@ describe('Addresses', function() {
'to': 1,
'items': [
{
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'txid': '9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a7a1cde251e54ccfdd5',
'version': 1,
'locktime': 0,
'isCoinBase': true,
'vin': [
{
'txid': 'ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3',
'vout': 1,
'coinbase': '04ffff001d010b',
'sequence': 4294967295,
'n': 0,
'addr': 'moFfnRwt77pApKnnU6m5uocFaa43aAYpt5',
'valueSat': 53540000,
'value': 0.5354,
'doubleSpentTxID': null
},
{
'txid': '980a9cc2dbc2d3464eb9900ae6d579a03045408563320f62d99316c3d4ff58b7',
'vout': 2,
'sequence': 4294967295,
'n': 1,
'addr': 'n1XJBAyU4hNR4xRtY3UxnmAteoJX83p5qv',
'valueSat': 299829,
'value': 0.00299829,
'doubleSpentTxID': null
'n': 0
}
],
'vout': [
{
'value': '0.00220000',
'value': '50.00000000',
'n': 0,
'scriptPubKey': {
'hex': '76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac',
'reqSigs': 1,
'hex': '41047211a824f55b505228e4c3d5194c1fcfaa15a456abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b417ab79a0fcae412ae3316b77ac',
'type': 'pubkeyhash',
'addresses': [
'mxT2KzTUQvsaYYothDtjcdvyAdaHA9ofMp'
]
}
},
{
'value': '0.53320000',
'n': 1,
'scriptPubKey': {
'hex': '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': [
'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK'
],
}
},
{
'value': '0.00289829',
'n': 2,
'scriptPubKey': {
'hex': '76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': [
'moZY18rGNmh4YCPeugtGW46AkkWMQttBUD'
'1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1'
]
}
}
@ -641,43 +530,20 @@ describe('Addresses', function() {
'time': 1441116143,
'blocktime': 1441116143,
'valueOut': 0.53829829,
'size': 470,
'valueIn': 0.53839829,
'fees': 0.0001,
'firstSeenTs': 1441108193
}
]
};
var todos = {
'items': [
{
'vout': [
{
'scriptPubKey': {
'reqSigs': 1,
}
},
{
'scriptPubKey': {
'reqSigs': 1,
}
},
{
'scriptPubKey': {
'reqSigs': 1,
}
}
],
'firstSeenTs': 1441108193
'size': 134
}
]
};
var node = {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
services: { block: { getTip: sinon.stub().returns({ height: 534232 }) }, address: {} },
network: 'testnet'
services: {
address: {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
},
block: {
getTip: sinon.stub().returns({ height: 534232 })
}
}
};
var addresses = new AddressController(node);
@ -690,8 +556,7 @@ describe('Addresses', function() {
var res = {
jsonp: function(data) {
var merged = _.merge(data, todos);
should(merged).eql(insight);
should(data).eql(insight);
done();
}
};
@ -700,6 +565,7 @@ describe('Addresses', function() {
});
});
describe('#_getTransformOptions', function() {
it('will return false with value of string "0"', function() {
var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node);
@ -717,6 +583,7 @@ describe('Addresses', function() {
noSpent: false
});
});
it('will return true with value of string "1"', function() {
var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node);
@ -734,6 +601,7 @@ describe('Addresses', function() {
noSpent: true
});
});
it('will return true with value of number "1"', function() {
var node = { services: { address: {}, block: {} } };
var addresses = new AddressController(node);

View File

@ -3,15 +3,14 @@
var should = require('should');
var sinon = require('sinon');
var BlockController = require('../lib/blocks');
var bitcore = require('bitcore-lib');
var _ = require('lodash');
var bcoin = require('bcoin');
var blocks = require('./data/blocks.json');
var blockIndexes = {
'0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7': {
hash: '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7',
chainWork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
chainwork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
prevHash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
nextHash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
confirmations: 119,
@ -51,7 +50,7 @@ describe('Blocks', function() {
'size': 1011,
'height': 533974,
'version': 536870919,
'merkleroot': 'b06437355844b8178173f3e18ca141472e4b0861daa81ef0f701cf9e51f0283e',
'merkleroot': '3e28f0519ecf01f7f01ea8da61084b2e4741a18ce1f3738117b84458353764b0',
'tx': [
'25a988e54b02e0e5df146a0f8fa7b9db56210533a9f04bdfda5f4ceb6f77aadd',
'b85334bf2df35c6dd5b294efe92ffc793a78edff75a2ca666fc296ffb04bbba0',
@ -59,26 +58,28 @@ describe('Blocks', function() {
],
'time': 1440987503,
'nonce': 1868753784,
'bits': '1a0cf267',
'bits': 437056103,
'difficulty': 1295829.93087696,
'chainwork': '0000000000000000000000000000000000000000000000054626b1839ade284a',
'previousblockhash': '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
'nextblockhash': '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
'reward': 12.5,
'nextblockhash': null,
'reward': null,
'isMainChain': true,
'poolInfo': {}
};
var bitcoreBlock = bitcore.Block.fromBuffer(new Buffer(blocks['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'], 'hex'));
var bcoinBlock = bcoin.block.fromRaw(blocks['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'], 'hex');
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, bitcoreBlock),
services: {
bitcoind: {
header: {
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7']),
isMainChain: sinon.stub().returns(true),
height: 534092
getCurrentDifficulty: sinon.stub().returns(1295829.93087696)
},
block: {
getBlock: sinon.stub().callsArgWith(1, null, bcoinBlock),
getTip: sinon.stub().returns({ height: 533974+118 })
}
}
};
@ -102,19 +103,24 @@ describe('Blocks', function() {
});
it('block pool info should be correct', function(done) {
var block = bitcore.Block.fromString(blocks['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']);
var block = bcoin.block.fromRaw(blocks['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4'], 'hex');
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, block),
services: {
bitcoind: {
header: {
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']),
isMainChain: sinon.stub().returns(true),
getCurrentDifficulty: sinon.stub().returns('aa'),
height: 534092
},
block: {
getBlock: sinon.stub().callsArgWith(1, null, block),
getTip: sinon.stub().returns({ height: 123 })
}
}
};
var controller = new BlockController({node: node});
var hash = '000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4';
var req = {
params: {
blockHash: hash
@ -123,109 +129,20 @@ describe('Blocks', function() {
var res = {};
var next = function() {
should.exist(req.block);
var block = req.block;
req.block.poolInfo.poolName.should.equal('Discus Fish');
req.block.poolInfo.url.should.equal('http://f2pool.com/');
done();
};
var hash = '000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4';
controller.block(req, res, next);
});
});
describe('/blocks route', function() {
var insight = {
'blocks': [
{
'height': 533951,
'size': 206,
'hash': '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7',
'time': 1440978683,
'txlength': 1,
'poolInfo': {
'poolName': 'AntMiner',
'url': 'https://bitmaintech.com/'
}
},
{
'height': 533950,
'size': 206,
'hash': '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
'time': 1440977479,
'txlength': 1,
'poolInfo': {
'poolName': 'AntMiner',
'url': 'https://bitmaintech.com/'
}
}
],
'length': 2,
'pagination': {
'current': '2015-08-30',
'currentTs': 1440979199,
'isToday': false,
'more': false,
'next': '2015-08-31',
'prev': '2015-08-29'
}
};
var stub = sinon.stub();
stub.onFirstCall().callsArgWith(1, null, new Buffer(blocks['000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'], 'hex'));
stub.onSecondCall().callsArgWith(1, null, new Buffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex'));
var hashes = [
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'
];
var node = {
log: sinon.stub(),
services: {
bitcoind: {
getRawBlock: stub,
getBlockHeader: function(hash, callback) {
callback(null, blockIndexes[hash]);
},
getBlockHashesByTimestamp: sinon.stub().callsArgWith(2, null, hashes)
}
}
};
it('should have correct data', function(done) {
var blocks = new BlockController({node: node});
var req = {
query: {
limit: 2,
blockDate: '2015-08-30'
}
};
var res = {
jsonp: function(data) {
should(data).eql(insight);
done();
}
};
blocks.list(req, res);
});
});
describe('/block-index/:height route', function() {
var node = {
log: sinon.stub(),
services: {
bitcoind: {
getBlockHeader: function(height, callback) {
callback(null, blockIndexes[height]);
}
}
}
services: { header: { getBlockHeader: sinon.stub().callsArgWith(1, null, { hash: '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7' }) }, block: {}, timestamp: {} },
};
it('should have correct data', function(done) {
@ -255,6 +172,7 @@ describe('Blocks', function() {
describe('#getBlockReward', function() {
var node = {
services: { header: {}, block: {}, timestamp: {} },
log: sinon.stub()
};
var blocks = new BlockController({node: node});

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ describe('Utils', function() {
it('should give the correct fee', function(done) {
var node = {
services: {
bitcoind: {
fee: {
estimateFee: function(blocks, callback) {
switch(blocks) {
case 1: