Merge pull request #467 from braydonf/bitcoind

bitcoind address index
This commit is contained in:
Chris Kleeschulte 2016-05-27 14:20:40 -04:00
commit 692578fd12
19 changed files with 2150 additions and 1328 deletions

View File

@ -1,5 +1,6 @@
language: node_js
node_js:
- 'v0.12.7'
- 'v4'
install:
- npm install

166
README.md
View File

@ -18,9 +18,61 @@ The API endpoints will be available by default at: `http://localhost:3001/insigh
## Prerequisites
- [Bitcore Node 0.2.x](https://github.com/bitpay/bitcore-node)
- [Bitcore Node 3.x](https://github.com/bitpay/bitcore-node)
**Note:** You can use an existing Bitcoin data directory, however `txindex` needs to be set to true in `bitcoin.conf`.
**Note:** You can use an existing Bitcoin data directory, however `txindex`, `addressindex`, `timestampindex` and `spentindex` needs to be set to true in `bitcoin.conf`, as well as a few other additional fields.
## Notes on Upgrading from v0.3
The unspent outputs format now has `satoshis` and `height`:
```
[
{
"address":"mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs",
"txid":"d5f8a96faccf79d4c087fa217627bb1120e83f8ea1a7d84b1de4277ead9bbac1",
"vout":0,
"scriptPubKey":"76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac",
"amount":0.000006,
"satoshis":600,
"confirmations":0,
"ts":1461349425
},
{
"address": "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs",
"txid": "bc9df3b92120feaee4edc80963d8ed59d6a78ea0defef3ec3cb374f2015bfc6e",
"vout": 1,
"scriptPubKey": "76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac",
"amount": 0.12345678,
"satoshis: 12345678,
"confirmations": 1,
"height": 300001
}
]
```
The `timestamp` property will only be set for unconfirmed transactions and `height` can be used for determining block order. The `confirmationsFromCache` is nolonger set or necessary, confirmation count is only cached for the time between blocks.
There is a new `GET` endpoint or raw blocks at `/rawblock/<blockHash>`:
Response format:
```
{
"rawblock": "blockhexstring..."
}
```
There are a few changes to the `GET` endpoint for `/addr/[:address]`:
- The list of txids in an address summary does not include orphaned transactions
- The txids will be sorted in block order
- The list of txids will be limited at 1000 txids
- There are two new query options "from" and "to" for pagination of the txids (e.g. `/addr/[:address]?from=1000&to=2000`)
Some additional general notes:
- The transaction history for an address will be sorted in block order
- The response for the `/sync` endpoint does not include `startTs` and `endTs` as the sync is no longer relevant as indexes are built in bitcoind.
- The endpoint for `/peer` is no longer relevant connection to bitcoind is via ZMQ.
- `/tx` endpoint results will now include block height, and spentTx related fields will be set to `null` if unspent.
- `/block` endpoint results does not include `confirmations` and will include `poolInfo`.
## Notes on Upgrading from v0.2
@ -61,10 +113,62 @@ Get block hash by height
```
This would return:
```
{"blockHash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"}
{
"blockHash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
}
```
which is the hash of the Genesis block (0 height)
### Raw Block
```
/insight-api/rawblock/[:blockHash]
/insight-api/rawblock/[:blockHeight]
```
This would return:
```
{
"rawblock":"blockhexstring..."
}
```
### Block Summaries
Get block summaries by date:
```
/insight-api/blocks?limit=3&blockDate=2016-04-22
```
Example response:
```
{
"blocks": [
{
"height": 408495,
"size": 989237,
"hash": "00000000000000000108a1f4d4db839702d72f16561b1154600a26c453ecb378",
"time": 1461360083,
"txlength": 1695,
"poolInfo": {
"poolName": "BTCC Pool",
"url": "https://pool.btcc.com/"
}
}
],
"length": 1,
"pagination": {
"next": "2016-04-23",
"prev": "2016-04-21",
"currentTs": 1461369599,
"current": "2016-04-22",
"isToday": true,
"more": true,
"moreTs": 1461369600
}
}
```
### Transaction
```
/insight-api/tx/[:txid]
@ -75,8 +179,9 @@ which is the hash of the Genesis block (0 height)
### Address
```
/insight-api/addr/[:addr][?noTxList=1&noCache=1]
/insight-api/addr/[:addr][?noTxList=1][&from=&to=]
/insight-api/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?noTxList=1
/insight-api/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?from=1000&to=2000
```
### Address Properties
@ -90,30 +195,31 @@ The response contains the value in Satoshis.
### Unspent Outputs
```
/insight-api/addr/[:addr]/utxo[?noCache=1]
/insight-api/addr/[:addr]/utxo
```
Sample return:
``` json
```
[
{
"address": "n2PuaAguxZqLddRbTnAoAuwKYgN2w2hZk7",
"txid": "dbfdc2a0d22a8282c4e7be0452d595695f3a39173bed4f48e590877382b112fc",
"vout": 0,
"ts": 1401276201,
"scriptPubKey": "76a914e50575162795cd77366fb80d728e3216bd52deac88ac",
"amount": 0.001,
"confirmations": 3
},
{
"address": "n2PuaAguxZqLddRbTnAoAuwKYgN2w2hZk7",
"txid": "e2b82af55d64f12fd0dd075d0922ee7d6a300f58fe60a23cbb5831b31d1d58b4",
"vout": 0,
"ts": 1401226410,
"scriptPubKey": "76a914e50575162795cd77366fb80d728e3216bd52deac88ac",
"amount": 0.001,
"confirmation": 6,
"confirmationsFromCache": true
}
{
"address":"mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs",
"txid":"d5f8a96faccf79d4c087fa217627bb1120e83f8ea1a7d84b1de4277ead9bbac1",
"vout":0,
"scriptPubKey":"76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac",
"amount":0.000006,
"satoshis":600,
"confirmations":0,
"ts":1461349425
},
{
"address": "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs",
"txid": "bc9df3b92120feaee4edc80963d8ed59d6a78ea0defef3ec3cb374f2015bfc6e",
"vout": 1,
"scriptPubKey": "76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac",
"amount": 0.12345678,
"satoshis: 12345678,
"confirmations": 1,
"height": 300001
}
]
```
@ -226,7 +332,7 @@ POST response:
/insight-api/sync
```
### Live Network P2P Data Sync Status (Bitcoind runs in the same process)
### Live Network P2P Data Sync Status
```
/insight-api/peer
```
@ -255,7 +361,7 @@ The web socket API is served using [socket.io](http://socket.io).
The following are the events published by insight:
'tx': new transaction received from network. This event is published in the 'inv' room. Data will be a app/models/Transaction object.
`tx`: new transaction received from network. This event is published in the 'inv' room. Data will be a app/models/Transaction object.
Sample output:
```
{
@ -266,7 +372,7 @@ Sample output:
```
'block': new block received from network. This event is published in the 'inv' room. Data will be a app/models/Block object.
`block`: new block received from network. This event is published in the `inv` room. Data will be a app/models/Block object.
Sample output:
```
{
@ -276,9 +382,9 @@ Sample output:
}
```
'<bitcoinAddress>': new transaction concerning <bitcoinAddress> received from network. This event is published in the '<bitcoinAddress>' room.
`<bitcoinAddress>`: new transaction concerning <bitcoinAddress> received from network. This event is published in the `<bitcoinAddress>` room.
'status': every 1% increment on the sync task, this event will be triggered. This event is published in the 'sync' room.
`status`: every 1% increment on the sync task, this event will be triggered. This event is published in the `sync` room.
Sample output:
```

View File

@ -1,23 +1,30 @@
'use strict';
var common = require('./common');
var bitcore = require('bitcore-lib');
var async = require('async');
var TxController = require('./transactions');
var Common = require('./common');
function AddressController(node) {
this.node = node;
this.txController = new TxController(node);
this.common = new Common({log: this.node.log});
}
AddressController.prototype.show = function(req, res) {
var self = this;
var options = {
noTxList: parseInt(req.query.noTxList)
};
if (req.query.from && req.query.to) {
options.from = parseInt(req.query.from);
options.to = parseInt(req.query.to);
}
this.getAddressSummary(req.addr, options, function(err, data) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp(data);
@ -41,9 +48,10 @@ AddressController.prototype.unconfirmedBalance = function(req, res) {
};
AddressController.prototype.addressSummarySubQuery = function(req, res, param) {
var self = this;
this.getAddressSummary(req.addr, {}, function(err, data) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp(data[param]);
@ -51,7 +59,6 @@ AddressController.prototype.addressSummarySubQuery = function(req, res, param) {
};
AddressController.prototype.getAddressSummary = function(address, options, callback) {
var self = this;
this.node.getAddressSummary(address, options, function(err, summary) {
if(err) {
@ -90,11 +97,12 @@ AddressController.prototype.checkAddrs = function(req, res, next) {
}
this.check(req, res, next, req.addrs);
}
};
AddressController.prototype.check = function(req, res, next, addresses) {
var self = this;
if(!addresses.length || !addresses[0]) {
return common.handleErrors({
return self.common.handleErrors({
message: 'Must include address',
code: 1
}, res);
@ -104,7 +112,7 @@ AddressController.prototype.check = function(req, res, next, addresses) {
try {
var a = new bitcore.Address(addresses[i]);
} catch(e) {
return common.handleErrors({
return self.common.handleErrors({
message: 'Invalid address: ' + e.message,
code: 1
}, res);
@ -117,60 +125,67 @@ AddressController.prototype.check = function(req, res, next, addresses) {
AddressController.prototype.utxo = function(req, res) {
var self = this;
this.node.getUnspentOutputs(req.addr, true, function(err, utxos) {
if(err && err instanceof self.node.errors.NoOutputs) {
this.node.getAddressUnspentOutputs(req.addr, {}, function(err, utxos) {
if(err) {
return self.common.handleErrors(err, res);
} else if (!utxos.length) {
return res.jsonp([]);
} else if(err) {
return common.handleErrors(err, res);
}
res.jsonp(utxos.map(self.transformUtxo.bind(self)));
});
};
AddressController.prototype.multiutxo = function(req, res) {
var self = this;
this.node.getUnspentOutputs(req.addrs, true, function(err, utxos) {
if(err && err instanceof self.node.errors.NoOutputs) {
this.node.getAddressUnspentOutputs(req.addrs, true, function(err, utxos) {
if(err && err.code === -5) {
return res.jsonp([]);
} else if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp(utxos.map(self.transformUtxo.bind(self)));
});
};
AddressController.prototype.transformUtxo = function(utxo) {
return {
address: utxo.address,
txid: utxo.txid,
vout: utxo.outputIndex,
ts: utxo.timestamp ? parseInt(utxo.timestamp) : Date.now(),
scriptPubKey: utxo.script,
amount: utxo.satoshis / 1e8,
confirmations: utxo.confirmations
AddressController.prototype.transformUtxo = function(utxoArg) {
var utxo = {
address: utxoArg.address,
txid: utxoArg.txid,
vout: utxoArg.outputIndex,
scriptPubKey: utxoArg.script,
amount: utxoArg.satoshis / 1e8,
satoshis: utxoArg.satoshis
};
if (utxoArg.height && utxoArg.height > 0) {
utxo.height = utxoArg.height;
utxo.confirmations = this.node.services.bitcoind.height - utxoArg.height + 1;
} else {
utxo.confirmations = 0;
}
if (utxoArg.timestamp) {
utxo.ts = utxoArg.timestamp;
}
return utxo;
};
AddressController.prototype.multitxs = function(req, res, next) {
var self = this;
var options = {
from: req.query.from || req.body.from || 0
from: parseInt(req.query.from) || parseInt(req.body.from) || 0
};
options.to = req.query.to || req.body.to || options.from + 10;
options.to = parseInt(req.query.to) || parseInt(req.body.to) || parseInt(options.from) + 10;
self.node.getAddressHistory(req.addrs, options, function(err, result) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
self.transformAddressHistoryForMultiTxs(result.items, function(err, items) {
if (err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp({
totalItems: result.totalCount,

View File

@ -1,14 +1,20 @@
'use strict';
var common = require('./common');
var async = require('async');
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var pools = require('../pools.json');
var BN = bitcore.crypto.BN;
var LRU = require('lru-cache');
var Common = require('./common');
function BlockController(node) {
function BlockController(options) {
var self = this;
this.node = node;
this.node = options.node;
this.blockSummaryCache = LRU(options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE);
this.blockCacheConfirmations = 6;
this.blockCache = LRU(options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE);
this.poolStrings = {};
pools.forEach(function(pool) {
@ -19,30 +25,93 @@ function BlockController(node) {
};
});
});
this.common = new Common({log: this.node.log});
}
var BLOCK_LIMIT = 200;
BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE = 1000000;
BlockController.DEFAULT_BLOCK_CACHE_SIZE = 1000;
function isHexadecimal(hash) {
if (!_.isString(hash)) {
return false;
}
return /^[0-9a-fA-F]+$/.test(hash);
}
BlockController.prototype.checkBlockHash = function(req, res, next) {
var self = this;
var hash = req.params.blockHash;
if (hash.length < 64 || !isHexadecimal(hash)) {
return self.common.handleErrors(null, res);
}
next();
};
/**
* Find block by hash ...
*/
BlockController.prototype.block = function(req, res, next, hash) {
BlockController.prototype.block = function(req, res, next) {
var self = this;
var hash = req.params.blockHash;
var blockCached = self.blockCache.get(hash);
this.node.getBlock(hash, function(err, block) {
if(err && err.message === 'Block not found.') {
// TODO libbitcoind should pass an instance of errors.Block.NotFound
return common.handleErrors(null, res);
if (blockCached) {
blockCached.confirmations = self.node.services.bitcoind.height - blockCached.height + 1;
req.block = blockCached;
next();
} else {
self.node.getBlock(hash, function(err, block) {
if((err && err.code === -5) || (err && err.code === -8)) {
return self.common.handleErrors(null, res);
} else if(err) {
return self.common.handleErrors(err, res);
}
self.node.services.bitcoind.getBlockHeader(hash, function(err, info) {
if (err) {
return self.common.handleErrors(err, res);
}
var blockResult = self.transformBlock(block, info);
if (blockResult.confirmations >= self.blockCacheConfirmations) {
self.blockCache.set(hash, blockResult);
}
req.block = blockResult;
next();
});
});
}
};
/**
* Find rawblock by hash and height...
*/
BlockController.prototype.rawBlock = function(req, res, next) {
var self = this;
var blockHash = req.params.blockHash;
self.node.getRawBlock(blockHash, function(err, blockBuffer) {
if((err && err.code === -5) || (err && err.code === -8)) {
return self.common.handleErrors(null, res);
} else if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
var info = self.node.services.bitcoind.getBlockIndex(hash);
info.isMainChain = self.node.services.bitcoind.isMainChain(hash);
req.block = self.transformBlock(block, info);
req.rawBlock = {
rawblock: blockBuffer.toString('hex')
};
next();
});
};
BlockController.prototype._normalizePrevHash = function(hash) {
// TODO fix bitcore to give back null instead of null hash
if (hash !== '0000000000000000000000000000000000000000000000000000000000000000') {
return hash;
} else {
return null;
}
};
BlockController.prototype.transformBlock = function(block, info) {
@ -52,7 +121,6 @@ BlockController.prototype.transformBlock = function(block, info) {
});
return {
hash: block.hash,
confirmations: this.node.services.db.tip.__height - info.height + 1,
size: block.toBuffer().length,
height: info.height,
version: blockObj.header.version,
@ -63,10 +131,11 @@ BlockController.prototype.transformBlock = function(block, info) {
bits: blockObj.header.bits.toString(16),
difficulty: block.header.getDifficulty(),
chainwork: info.chainWork,
previousblockhash: blockObj.header.prevHash,
nextblockhash: this.node.services.bitcoind.getNextBlockHash(block.hash),
confirmations: info.confirmations,
previousblockhash: this._normalizePrevHash(blockObj.header.prevHash),
nextblockhash: info.nextHash,
reward: this.getBlockReward(info.height) / 1e8,
isMainChain: info.isMainChain,
isMainChain: (info.confirmations !== -1),
poolInfo: this.getPoolInfo(block)
};
};
@ -80,15 +149,83 @@ BlockController.prototype.show = function(req, res) {
}
};
BlockController.prototype.blockIndex = function(req, res, next, height) {
var info = this.node.services.bitcoind.getBlockIndex(parseInt(height));
if(!info) {
return common.handleErrors(null, res);
BlockController.prototype.showRaw = function(req, res) {
if (req.rawBlock) {
res.jsonp(req.rawBlock);
}
};
BlockController.prototype.blockIndex = function(req, res) {
var self = this;
var height = req.params.height;
this.node.services.bitcoind.getBlockHeader(parseInt(height), function(err, info) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp({
blockHash: info.hash
});
});
};
BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) {
var self = this;
function finish(result) {
if (moreTimestamp > result.time) {
moreTimestamp = result.time;
}
return next(null, result);
}
res.jsonp({
blockHash: info.hash
});
var summaryCache = self.blockSummaryCache.get(hash);
if (summaryCache) {
finish(summaryCache);
} else {
self.node.services.bitcoind.getRawBlock(hash, function(err, blockBuffer) {
if (err) {
return next(err);
}
var br = new bitcore.encoding.BufferReader(blockBuffer);
// take a shortcut to get number of transactions and the blocksize.
// Also reads the coinbase transaction and only that.
// Old code parsed all transactions in every block _and_ then encoded
// them all back together to get the binary size of the block.
// FIXME: This code might still read the whole block. Fixing that
// would require changes in bitcore-node.
var header = bitcore.BlockHeader.fromBufferReader(br);
var info = {};
var txlength = br.readVarintNum();
info.transactions = [bitcore.Transaction().fromBufferReader(br)];
self.node.services.bitcoind.getBlockHeader(hash, function(err, blockHeader) {
if (err) {
return next(err);
}
var height = blockHeader.height;
var summary = {
height: height,
size: blockBuffer.length,
hash: hash,
time: header.time,
txlength: txlength,
poolInfo: self.getPoolInfo(info)
};
var confirmations = self.node.services.bitcoind.height - height + 1;
if (confirmations >= self.blockCacheConfirmations) {
self.blockSummaryCache.set(hash, summary);
}
finish(summary);
});
});
}
};
// List blocks by date
@ -103,7 +240,7 @@ BlockController.prototype.list = function(req, res) {
dateStr = req.query.blockDate;
var datePattern = /\d{4}-\d{2}-\d{2}/;
if(!datePattern.test(dateStr)) {
return common.handleErrors(new Error('Please use yyyy-mm-dd format'), res);
return self.common.handleErrors(new Error('Please use yyyy-mm-dd format'), res);
}
isToday = dateStr === todayStr;
@ -120,13 +257,15 @@ BlockController.prototype.list = function(req, res) {
var next = lte ? this.formatTimestamp(new Date(lte * 1000)) : null;
var limit = parseInt(req.query.limit || BLOCK_LIMIT);
var more = false;
var moreTs = lte;
var moreTimestamp = lte;
self.node.services.db.getBlockHashesByTimestamp(lte, gte, function(err, hashes) {
self.node.services.bitcoind.getBlockHashesByTimestamp(lte, gte, function(err, hashes) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
hashes.reverse();
if(hashes.length > limit) {
more = true;
hashes = hashes.slice(0, limit);
@ -135,41 +274,19 @@ BlockController.prototype.list = function(req, res) {
async.mapSeries(
hashes,
function(hash, next) {
self.node.getBlock(hash, function(err, block) {
if(err) {
return next(err);
}
var info = self.node.services.bitcoind.getBlockIndex(hash);
block.__height = info.height;
if(moreTs > block.header.timestamp) {
moreTs = block.header.timestamp;
}
return next(null, block);
});
self._getBlockSummary(hash, moreTimestamp, next);
},
function(err, blocks) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
blocks.sort(function(a, b) {
return b.__height - a.__height;
return b.height - a.height;
});
var data = {
blocks: blocks.map(function(block) {
return {
height: block.__height,
size: block.toBuffer().length,
hash: block.hash,
time: block.header.time,
txlength: block.transactions.length,
poolInfo: self.getPoolInfo(block)
};
}),
blocks: blocks,
length: blocks.length,
pagination: {
next: next,
@ -182,7 +299,7 @@ BlockController.prototype.list = function(req, res) {
};
if(more) {
data.pagination.moreTs = moreTs;
data.pagination.moreTs = moreTimestamp;
}
res.jsonp(data);

View File

@ -1,19 +1,24 @@
'use strict';
exports.notReady = function (err, res, p) {
function Common(options) {
this.log = options.log;
}
Common.prototype.notReady = function (err, res, p) {
res.status(503).send('Server not yet ready. Sync Percentage:' + p);
};
exports.handleErrors = function (err, res) {
Common.prototype.handleErrors = function (err, res) {
if (err) {
if (err.code) {
res.status(400).send(err.message + '. Code:' + err.code);
}
else {
} else {
this.log.error(err.stack);
res.status(503).send(err.message);
}
}
else {
} else {
res.status(404).send('Not found');
}
};
module.exports = Common;

View File

@ -1,6 +1,8 @@
'use strict';
var Writable = require('stream').Writable;
var bodyParser = require('body-parser');
var compression = require('compression');
var BaseService = require('./service');
var inherits = require('util').inherits;
var BlockController = require('./blocks');
@ -10,6 +12,8 @@ var StatusController = require('./status');
var MessagesController = require('./messages');
var UtilsController = require('./utils');
var CurrencyController = require('./currency');
var RateLimiter = require('./ratelimiter');
var morgan = require('morgan');
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
@ -42,6 +46,9 @@ var InsightAPI = function(options) {
this.cacheShortSeconds = options.cacheShortSeconds;
this.cacheLongSeconds = options.cacheLongSeconds;
this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE;
this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE;
if (!_.isUndefined(options.routePrefix)) {
this.routePrefix = options.routePrefix;
} else {
@ -51,7 +58,7 @@ var InsightAPI = function(options) {
this.txController = new TxController(this.node);
};
InsightAPI.dependencies = ['address', 'web'];
InsightAPI.dependencies = ['bitcoind', 'web'];
inherits(InsightAPI, BaseService);
@ -80,13 +87,55 @@ InsightAPI.prototype.getRoutePrefix = function() {
};
InsightAPI.prototype.start = function(callback) {
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
this.node.services.bitcoind.on('tx', this.transactionEventHandler.bind(this));
this.node.services.bitcoind.on('block', this.blockEventHandler.bind(this));
setImmediate(callback);
};
InsightAPI.prototype.createLogInfoStream = function() {
var self = this;
function Log(options) {
Writable.call(this, options);
}
inherits(Log, Writable);
Log.prototype._write = function (chunk, enc, callback) {
self.node.log.info(chunk.slice(0, chunk.length - 1)); // remove new line and pass to logger
callback();
};
var stream = new Log();
return stream;
};
InsightAPI.prototype.getRemoteAddress = function(req) {
if (req.headers['cf-connecting-ip']) {
return req.headers['cf-connecting-ip'];
}
return req.socket.remoteAddress;
};
InsightAPI.prototype.setupRoutes = function(app) {
var self = this;
//Enable rate limiter
var limiter = new RateLimiter({node: this.node});
app.use(limiter.middleware());
//Setup logging
morgan.token('remote-forward-addr', function(req){
return self.getRemoteAddress(req);
});
var logFormat = ':remote-forward-addr ":method :url" :status :res[content-length] :response-time ":user-agent" ';
var logStream = this.createLogInfoStream();
app.use(morgan(logFormat, {stream: logStream}));
//Enable compression
app.use(compression());
//Enable urlencoded data
app.use(bodyParser.urlencoded({extended: true}));
//Enable CORS
@ -97,19 +146,26 @@ InsightAPI.prototype.setupRoutes = function(app) {
});
//Block routes
var blocks = new BlockController(this.node);
var blockOptions = {
node: this.node,
blockSummaryCacheSize: this.blockSummaryCacheSize,
blockCacheSize: this.blockCacheSize
};
var blocks = new BlockController(blockOptions);
app.get('/blocks', this.cacheShort(), blocks.list.bind(blocks));
app.get('/block/:blockHash', this.cacheLong(), blocks.show.bind(blocks));
app.get('/block/:blockHash', this.cacheShort(), blocks.checkBlockHash.bind(blocks), blocks.show.bind(blocks));
app.param('blockHash', blocks.block.bind(blocks));
app.get('/block-index/:height', this.cacheLong(), blocks.blockIndex.bind(blocks));
app.get('/rawblock/:blockHash', this.cacheLong(), blocks.checkBlockHash.bind(blocks), blocks.showRaw.bind(blocks));
app.param('blockHash', blocks.rawBlock.bind(blocks));
app.get('/block-index/:height', this.cacheShort(), blocks.blockIndex.bind(blocks));
app.param('height', blocks.blockIndex.bind(blocks));
// Transaction routes
var transactions = new TxController(this.node);
app.get('/tx/:txid', this.cacheLong(), transactions.show.bind(transactions));
app.get('/tx/:txid', this.cacheShort(), transactions.show.bind(transactions));
app.param('txid', transactions.transaction.bind(transactions));
app.get('/txs', this.cacheShort(), transactions.list.bind(transactions));
app.post('/tx/send', transactions.send.bind(transactions));
@ -156,6 +212,15 @@ InsightAPI.prototype.setupRoutes = function(app) {
});
app.get('/currency', currency.index.bind(currency));
// Not Found
app.use(function(req, res) {
res.status(404).jsonp({
status: 404,
url: req.originalUrl,
error: 'Not found'
});
});
};
InsightAPI.prototype.getPublishEvents = function() {
@ -170,25 +235,18 @@ InsightAPI.prototype.getPublishEvents = function() {
];
};
InsightAPI.prototype.blockHandler = function(block, add, callback) {
InsightAPI.prototype.blockEventHandler = function(hashBuffer) {
// Notify inv subscribers
for (var i = 0; i < this.subscriptions.inv.length; i++) {
this.subscriptions.inv[i].emit('block', block.hash);
this.subscriptions.inv[i].emit('block', hashBuffer.toString('hex'));
}
setImmediate(function() {
callback(null, []);
});
};
InsightAPI.prototype.transactionEventHandler = function(txBuffer) {
var tx = new Transaction().fromBuffer(txBuffer);
var result = this.txController.transformInvTransaction(tx);
InsightAPI.prototype.transactionHandler = function(txInfo) {
if(txInfo.mempool) {
var tx = Transaction().fromBuffer(txInfo.buffer);
tx = this.txController.transformInvTransaction(tx);
for (var i = 0; i < this.subscriptions.inv.length; i++) {
this.subscriptions.inv[i].emit('tx', tx);
}
for (var i = 0; i < this.subscriptions.inv.length; i++) {
this.subscriptions.inv[i].emit('tx', result);
}
};

View File

@ -3,18 +3,20 @@
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var Message = require('bitcore-message');
var common = require('./common');
var Common = require('./common');
function MessagesController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
}
MessagesController.prototype.verify = function(req, res) {
var self = this;
var address = req.body.address || req.query.address;
var signature = req.body.signature || req.query.signature;
var message = req.body.message || req.query.message;
if(_.isUndefined(address) || _.isUndefined(signature) || _.isUndefined(message)) {
return common.handleErrors({
return self.common.handleErrors({
message: 'Missing parameters (expected "address", "signature" and "message")',
code: 1
}, res);
@ -23,7 +25,7 @@ MessagesController.prototype.verify = function(req, res) {
try {
valid = new Message(message).verify(address, signature);
} catch(err) {
return common.handleErrors({
return self.common.handleErrors({
message: 'Unexpected error: ' + err.message,
code: 1
}, res);

133
lib/ratelimiter.js Normal file
View File

@ -0,0 +1,133 @@
'use strict';
var THREE_HOURS = 3 * 60 * 60 * 1000;
/**
* A rate limiter to be used as an express middleware.
*
* @param {Object} options
* @param {Object} options.node - The bitcore node object
* @param {Number} options.limit - Number of requests for normal rate limiter
* @param {Number} options.interval - Interval of the normal rate limiter
* @param {Array} options.whitelist - IP addresses that should have whitelist rate limiting
* @param {Array} options.blacklist - IP addresses that should be blacklist rate limiting
* @param {Number} options.whitelistLimit - Number of requests for whitelisted clients
* @param {Number} options.whitelistInterval - Interval for whitelisted clients
* @param {Number} options.blacklistLimit - Number of requests for blacklisted clients
* @param {Number} options.blacklistInterval - Interval for blacklisted clients
*/
function RateLimiter(options) {
if (!(this instanceof RateLimiter)) {
return new RateLimiter(options);
}
if (!options){
options = {};
}
this.node = options.node;
this.clients = {};
this.whitelist = options.whitelist || [];
this.blacklist = options.blacklist || [];
this.config = {
whitelist: {
totalRequests: options.whitelistLimit || 3 * 60 * 60 * 10, // 108,000
interval: options.whitelistInterval || THREE_HOURS
},
blacklist: {
totalRequests: options.blacklistLimit || 0,
interval: options.blacklistInterval || THREE_HOURS
},
normal: {
totalRequests: options.limit || 3 * 60 * 60, // 10,800
interval: options.interval || THREE_HOURS
}
};
}
RateLimiter.prototype.middleware = function() {
var self = this;
return function(req, res, next) {
self._middleware(req, res, next);
};
};
RateLimiter.prototype._middleware = function(req, res, next) {
var name = this.getClientName(req);
var client = this.clients[name];
res.ratelimit = {
clients: this.clients,
exceeded: false
};
if (!client) {
client = this.addClient(name);
}
res.setHeader('X-RateLimit-Limit', this.config[client.type].totalRequests);
res.setHeader('X-RateLimit-Remaining', this.config[client.type].totalRequests - client.visits);
res.ratelimit.exceeded = this.exceeded(client);
res.ratelimit.client = client;
if (!this.exceeded(client)) {
client.visits++;
next();
} else {
this.node.log.warn('Rate limited:', client);
res.status(429).jsonp({
status: 429,
error: 'Rate limit exceeded'
});
}
};
RateLimiter.prototype.exceeded = function(client) {
if (this.config[client.type].totalRequests === -1) {
return false;
} else {
return client.visits > this.config[client.type].totalRequests;
}
};
RateLimiter.prototype.getClientType = function(name) {
if (this.whitelist.indexOf(name) > -1) {
return 'whitelist';
}
if (this.blacklist.indexOf(name) > -1) {
return 'blacklist';
}
return 'normal';
};
RateLimiter.prototype.getClientName = function(req) {
var name = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
return name;
};
RateLimiter.prototype.addClient = function(name) {
var self = this;
var client = {
name: name,
type: this.getClientType(name),
visits: 1
};
var resetTime = this.config[client.type].interval;
setTimeout(function() {
delete self.clients[name];
}, resetTime).unref();
this.clients[name] = client;
return client;
};
module.exports = RateLimiter;

View File

@ -1,78 +1,132 @@
'use strict';
var Common = require('./common');
function StatusController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
}
StatusController.prototype.show = function(req, res) {
var self = this;
var option = req.query.q;
switch(option) {
case 'getDifficulty':
res.jsonp(this.getDifficulty());
break;
case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash());
break;
case 'getBestBlockHash':
res.jsonp(this.getBestBlockHash());
break;
case 'getInfo':
default:
res.jsonp(this.getInfo());
case 'getDifficulty':
this.getDifficulty(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash());
break;
case 'getBestBlockHash':
this.getBestBlockHash(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getInfo':
default:
this.getInfo(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp({
info: result
});
});
}
};
StatusController.prototype.getInfo = function() {
var info = this.node.services.bitcoind.getInfo();
return {
info: info
};
StatusController.prototype.getInfo = function(callback) {
this.node.services.bitcoind.getInfo(function(err, result) {
if (err) {
return callback(err);
}
var info = {
version: result.version,
protocolversion: result.protocolVersion,
blocks: result.blocks,
timeoffset: result.timeOffset,
connections: result.connections,
proxy: result.proxy,
difficulty: result.difficulty,
testnet: result.testnet,
relayfee: result.relayFee,
errors: result.errors,
network: result.network
};
callback(null, info);
});
};
StatusController.prototype.getLastBlockHash = function() {
var hash = this.node.services.db.tip.hash;
var hash = this.node.services.bitcoind.tiphash;
return {
syncTipHash: hash,
lastblockhash: hash
};
};
StatusController.prototype.getBestBlockHash = function() {
var hash = this.node.services.bitcoind.getBestBlockHash();
return {
bestblockhash: hash
};
StatusController.prototype.getBestBlockHash = function(callback) {
this.node.services.bitcoind.getBestBlockHash(function(err, hash) {
if (err) {
return callback(err);
}
callback(null, {
bestblockhash: hash
});
});
};
StatusController.prototype.getDifficulty = function() {
var info = this.node.services.bitcoind.getInfo();
return {
difficulty: info.difficulty
};
StatusController.prototype.getDifficulty = function(callback) {
this.node.services.bitcoind.getInfo(function(err, info) {
if (err) {
return callback(err);
}
callback(null, {
difficulty: info.difficulty
});
});
};
StatusController.prototype.sync = function(req, res) {
var self = this;
var status = 'syncing';
if(this.node.services.bitcoind.isSynced() && this.node.services.db.tip.__height === this.node.services.bitcoind.height) {
status = 'finished';
}
// Not exactly the total blockchain height,
// but we will reach 100% when our db and bitcoind are both fully synced
var totalHeight = this.node.services.bitcoind.height / (this.node.services.bitcoind.syncPercentage() / 100);
this.node.services.bitcoind.isSynced(function(err, synced) {
if (err) {
return self.common.handleErrors(err, res);
}
if (synced) {
status = 'finished';
}
var info = {
status: status,
blockChainHeight: this.node.services.bitcoind.height,
syncPercentage: Math.round(this.node.services.db.tip.__height / totalHeight * 100),
height: this.node.services.db.tip.__height,
error: null,
type: 'bitcore node'
};
self.node.services.bitcoind.syncPercentage(function(err, percentage) {
if (err) {
return self.common.handleErrors(err, res);
}
var info = {
status: status,
blockChainHeight: self.node.services.bitcoind.height,
syncPercentage: Math.round(percentage),
height: self.node.services.bitcoind.height,
error: null,
type: 'bitcore node'
};
res.jsonp(info);
});
});
res.jsonp(info);
};
// Hard coded to make insight ui happy, but not applicable

View File

@ -3,13 +3,14 @@
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var common = require('./common');
var Common = require('./common');
var async = require('async');
var MAXINT = 0xffffffff; // Math.pow(2, 32) - 1;
function TxController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
}
TxController.prototype.show = function(req, res) {
@ -21,179 +22,125 @@ TxController.prototype.show = function(req, res) {
/**
* Find transaction by hash ...
*/
TxController.prototype.transaction = function(req, res, next, txid) {
TxController.prototype.transaction = function(req, res, next) {
var self = this;
var txid = req.params.txid;
this.node.getTransactionWithBlockInfo(txid, true, function(err, transaction) {
if (err && err instanceof self.node.errors.Transaction.NotFound) {
return common.handleErrors(null, res);
this.node.getDetailedTransaction(txid, function(err, transaction) {
if (err && err.code === -5) {
return self.common.handleErrors(null, res);
} else if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
transaction.populateInputs(self.node.services.db, [], function(err) {
if(err) {
return res.send({
error: err.toString()
});
self.transformTransaction(transaction, function(err, transformedTransaction) {
if (err) {
return self.common.handleErrors(err, res);
}
self.transformTransaction(transaction, function(err, transformedTransaction) {
if (err) {
return common.handleErrors(err, res);
}
req.transaction = transformedTransaction;
next();
});
req.transaction = transformedTransaction;
next();
});
});
};
TxController.prototype.transformTransaction = function(transaction, callback) {
$.checkArgument(_.isFunction(callback));
var self = this;
var txid = transaction.id;
var txObj = transaction.toObject();
var confirmations = 0;
if(transaction.__height >= 0) {
confirmations = this.node.services.db.tip.__height - transaction.__height + 1;
if(transaction.height >= 0) {
confirmations = this.node.services.bitcoind.height - transaction.height + 1;
}
var transformed = {
txid: txObj.hash,
version: txObj.version,
locktime: txObj.nLockTime
txid: transaction.hash,
version: transaction.version,
locktime: transaction.locktime
};
if(transaction.isCoinbase()) {
if(transaction.coinbase) {
transformed.vin = [
{
coinbase: txObj.inputs[0].script,
sequence: txObj.inputs[0].sequenceNumber,
coinbase: transaction.inputs[0].script,
sequence: transaction.inputs[0].sequence,
n: 0
}
];
} else {
transformed.vin = txObj.inputs.map(this.transformInput.bind(this));
transformed.vin = transaction.inputs.map(this.transformInput.bind(this));
}
async.map(
Object.keys(txObj.outputs),
function(outputIndex, next) {
outputIndex = parseInt(outputIndex);
var output = txObj.outputs[outputIndex];
self.transformOutput(txid, output, outputIndex, next);
},
function(err, vout) {
if (err) {
return callback(err);
}
transformed.vout = transaction.outputs.map(this.transformOutput.bind(this));
transformed.vout = vout;
transformed.blockhash = transaction.blockHash;
transformed.blockheight = transaction.height;
transformed.confirmations = confirmations;
// TODO consider mempool txs with receivedTime?
var time = transaction.blockTimestamp ? transaction.blockTimestamp : Math.round(Date.now() / 1000);
transformed.time = time;
if (transformed.confirmations) {
transformed.blocktime = transformed.time;
}
transformed.blockhash = transaction.__blockHash;
transformed.blockheight = transaction.__height;
transformed.confirmations = confirmations;
var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000);
transformed.time = time;
if (transformed.confirmations) {
transformed.blocktime = transformed.time;
}
if(transaction.coinbase) {
transformed.isCoinBase = true;
}
if(transaction.isCoinbase()) {
transformed.isCoinBase = true;
}
transformed.valueOut = transaction.outputAmount / 1e8;
transformed.size = transaction.toBuffer().length;
if(transaction.hasAllUtxoInfo()) {
transformed.valueIn = transaction.inputAmount / 1e8;
transformed.fees = transaction.getFee() / 1e8;
}
callback(null, transformed);
}
);
transformed.valueOut = transaction.outputSatoshis / 1e8;
transformed.size = transaction.hex.length / 2; // in bytes
if (!transaction.coinbase) {
transformed.valueIn = transaction.inputSatoshis / 1e8;
transformed.fees = transaction.feeSatoshis / 1e8;
}
callback(null, transformed);
};
TxController.prototype.transformInput = function(input, index) {
// Input scripts are validated and can be assumed to be valid
var script = new bitcore.Script(input.script);
var transformed = {
txid: input.prevTxId,
vout: input.outputIndex,
scriptSig: {
asm: script.toASM(),
asm: input.scriptAsm,
hex: input.script
},
sequence: input.sequenceNumber,
sequence: input.sequence,
n: index
};
if(input.output) {
transformed.addr = bitcore.Script(input.output.script).toAddress(this.node.network).toString();
transformed.valueSat = input.output.satoshis;
transformed.value = input.output.satoshis / 1e8;
transformed.doubleSpentTxID = null; // TODO
//transformed.isConfirmed = null; // TODO
//transformed.confirmations = null; // TODO
//transformed.unconfirmedInput = null; // TODO
}
transformed.addr = input.address;
transformed.valueSat = input.satoshis;
transformed.value = input.satoshis / 1e8;
transformed.doubleSpentTxID = null; // TODO
//transformed.isConfirmed = null; // TODO
//transformed.confirmations = null; // TODO
//transformed.unconfirmedInput = null; // TODO
return transformed;
};
TxController.prototype.transformOutput = function(txid, output, index, callback) {
var self = this;
TxController.prototype.transformOutput = function(output, index) {
var transformed = {
value: (output.satoshis / 1e8).toFixed(8),
n: index,
scriptPubKey: {
hex: output.script,
asm: output.scriptAsm
//reqSigs: null, // TODO
}
},
spentTxId: output.spentTxId || null,
spentIndex: _.isUndefined(output.spentIndex) ? null : output.spentIndex,
spentHeight: output.spentHeight || null
//spentTs: undefined // TODO
};
var script;
try {
// Output scripts can be invalid, so we need to try/catch
script = new bitcore.Script(output.script);
} catch (err) {
script = false;
if (output.address) {
transformed.scriptPubKey.addresses = [output.address];
var address = bitcore.Address(output.address); //TODO return type from bitcore-node
transformed.scriptPubKey.type = address.type;
}
if (script) {
transformed.scriptPubKey.asm = script.toASM();
var address = script.toAddress(this.node.network);
if (address) {
transformed.scriptPubKey.addresses = [address.toString()];
transformed.scriptPubKey.type = address.type;
}
}
var options = {
queryMempool: true
};
self.node.services.address.getInputForOutput(
txid,
index,
options,
function(err, inputResult) {
if (err) {
return callback(err);
}
if (inputResult) {
transformed.spentTxId = inputResult.inputTxId;
transformed.spentIndex = inputResult.inputIndex;
}
callback(null, transformed);
}
);
return transformed;
};
TxController.prototype.transformInvTransaction = function(transaction) {
@ -228,14 +175,15 @@ TxController.prototype.transformInvTransaction = function(transaction) {
return transformed;
};
TxController.prototype.rawTransaction = function(req, res, next, txid) {
TxController.prototype.rawTransaction = function(req, res, next) {
var self = this;
var txid = req.params.txid;
this.node.getTransaction(txid, true, function(err, transaction) {
if (err && err instanceof self.node.errors.Transaction.NotFound) {
return common.handleErrors(null, res);
this.node.getTransaction(txid, function(err, transaction) {
if (err && err.code === -5) {
return self.common.handleErrors(null, res);
} else if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
req.rawTransaction = {
@ -262,36 +210,34 @@ TxController.prototype.list = function(req, res) {
var pagesTotal = 1;
if(blockHash) {
self.node.getBlock(blockHash, function(err, block) {
if(err && err.message === 'Block not found.') {
return common.handleErrors(null, res);
self.node.getBlockOverview(blockHash, function(err, block) {
if(err && err.code === -5) {
return self.common.handleErrors(null, res);
} else if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
var blockInfo = self.node.services.bitcoind.getBlockIndex(block.hash);
var txs = block.transactions;
var totalTxs = txs.length;
var totalTxs = block.txids.length;
var txids;
if(!_.isUndefined(page)) {
txs = txs.splice(page * pageLength, pageLength);
var start = page * pageLength;
txids = block.txids.slice(start, start + pageLength);
pagesTotal = Math.ceil(totalTxs / pageLength);
} else {
txids = block.txids;
}
async.mapSeries(txs, function(tx, next) {
tx.__blockHash = block.hash;
tx.__height = blockInfo.height;
tx.__timestamp = block.header.time;
tx.populateInputs(self.node.services.db, [], function(err) {
if(err) {
async.mapSeries(txids, function(txid, next) {
self.node.getDetailedTransaction(txid, function(err, transaction) {
if (err) {
return next(err);
}
self.transformTransaction(tx, next);
self.transformTransaction(transaction, next);
});
}, function(err, transformed) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp({
@ -299,6 +245,7 @@ TxController.prototype.list = function(req, res) {
txs: transformed
});
});
});
} else if(address) {
var options = {
@ -308,7 +255,7 @@ TxController.prototype.list = function(req, res) {
self.node.getAddressHistory(address, options, function(err, result) {
if(err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
var txs = result.items.map(function(info) {
@ -324,7 +271,7 @@ TxController.prototype.list = function(req, res) {
},
function(err, transformed) {
if (err) {
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.jsonp({
pagesTotal: Math.ceil(result.totalCount / pageLength),
@ -334,15 +281,16 @@ TxController.prototype.list = function(req, res) {
);
});
} else {
return common.handleErrors(new Error('Block hash or address expected'), res);
return self.common.handleErrors(new Error('Block hash or address expected'), res);
}
};
TxController.prototype.send = function(req, res) {
var self = this;
this.node.sendTransaction(req.body.rawtx, function(err, txid) {
if(err) {
// TODO handle specific errors
return common.handleErrors(err, res);
return self.common.handleErrors(err, res);
}
res.json({'txid': txid});

View File

@ -1,8 +1,12 @@
'use strict';
var _ = require('lodash');
var async = require('async');
var Common = require('./common');
function UtilsController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
}
UtilsController.prototype.estimateFee = function(req, res) {
@ -10,14 +14,22 @@ UtilsController.prototype.estimateFee = function(req, res) {
var args = req.query.nbBlocks || '2';
var nbBlocks = args.split(',');
var result = nbBlocks.map(function(n) {
async.map(nbBlocks, function(n, next) {
var num = parseInt(n);
// Insight and Bitcoin JSON-RPC return bitcoin for this value (instead of satoshis).
var fee = self.node.services.bitcoind.estimateFee(num) / 1e8;
return [num, fee];
self.node.services.bitcoind.estimateFee(num, function(err, fee) {
if (err) {
return next(err);
}
next(null, [num, fee]);
});
}, function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp(_.zipObject(result));
});
res.jsonp(_.zipObject(result));
};
module.exports = UtilsController;

View File

@ -64,14 +64,17 @@
"bitcore-lib": "^0.13.7",
"bitcore-message": "^1.0.1",
"body-parser": "^1.13.3",
"compression": "^1.6.1",
"lodash": "^2.4.1",
"lru-cache": "^4.0.1",
"morgan": "^1.7.0",
"request": "^2.64.0"
},
"devDependencies": {
"chai": "*",
"mocha": "~1.16.2",
"chai": "^3.5.0",
"mocha": "^2.4.5",
"proxyquire": "^1.7.2",
"should": "^2.1.1",
"should": "^8.3.1",
"sinon": "^1.10.3"
}
}

View File

@ -9,135 +9,138 @@ var txinfos = {
totalCount: 2,
items: [
{
"address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er",
"satoshis": 2782729129,
"height": 534105,
"confirmations": 123,
"timestamp": 1441068774,
"fees": 35436,
"outputIndexes": [
'address': 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er',
'satoshis': 2782729129,
'height': 534105,
'confirmations': 123,
'timestamp': 1441068774,
'fees': 35436,
'outputIndexes': [
0
],
"inputIndexes": [],
"tx": {
"hash": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7",
"version": 1,
"inputs": [
'inputIndexes': [],
'tx': {
'hash': 'bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7',
'version': 1,
'inputs': [
{
"prevTxId": "ea5e5bafbf29cdf6f6097ab344128477e67889d4d6203cb43594836daa6cc425",
"outputIndex": 1,
"sequenceNumber": 4294967294,
"script": "483045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a6012103acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6",
"scriptString": "72 0x3045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a601 33 0x03acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6",
"output": {
"satoshis": 2796764565,
"script": "76a91488b1fe8aec5ae4358a11447a2f22b2781faedb9b88ac"
'prevTxId': 'ea5e5bafbf29cdf6f6097ab344128477e67889d4d6203cb43594836daa6cc425',
'outputIndex': 1,
'sequenceNumber': 4294967294,
'script': '483045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a6012103acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6',
'scriptString': '72 0x3045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a601 33 0x03acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6',
'output': {
'satoshis': 2796764565,
'script': '76a91488b1fe8aec5ae4358a11447a2f22b2781faedb9b88ac'
}
}
],
"outputs": [
'outputs': [
{
"satoshis": 2782729129,
"script": "76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac"
'satoshis': 2782729129,
'script': '76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac'
},
{
"satoshis": 14000000,
"script": "76a9149713201957f42379e574d7c70d506ee49c2c8ad688ac"
'satoshis': 14000000,
'script': '76a9149713201957f42379e574d7c70d506ee49c2c8ad688ac'
}
],
"nLockTime": 534089
'nLockTime': 534089
}
},
{
"address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er",
"satoshis": -2782729129,
"height": 534110,
"confirmations": 118,
"timestamp": 1441072817,
"fees": 35437,
"outputIndexes": [],
"inputIndexes": [
"0"
'address': 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er',
'satoshis': -2782729129,
'height': 534110,
'confirmations': 118,
'timestamp': 1441072817,
'fees': 35437,
'outputIndexes': [],
'inputIndexes': [
'0'
],
"tx": {
"hash": "01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3",
"version": 1,
"inputs": [
'tx': {
'hash': '01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3',
'version': 1,
'inputs': [
{
"prevTxId": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7",
"outputIndex": 0,
"sequenceNumber": 4294967294,
"script": "47304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d5640121034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24",
"scriptString": "71 0x304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d56401 33 0x034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24",
"output": {
"satoshis": 2782729129,
"script": "76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac"
'prevTxId': 'bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7',
'outputIndex': 0,
'sequenceNumber': 4294967294,
'script': '47304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d5640121034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24',
'scriptString': '71 0x304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d56401 33 0x034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24',
'output': {
'satoshis': 2782729129,
'script': '76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac'
}
}
],
"outputs": [
'outputs': [
{
"satoshis": 2764693692,
"script": "76a91456e446bc3489543d8324c6d0271524c0bd0506dd88ac"
'satoshis': 2764693692,
'script': '76a91456e446bc3489543d8324c6d0271524c0bd0506dd88ac'
},
{
"satoshis": 18000000,
"script": "76a914011d2963b619186a318f768dddfd98cd553912a088ac"
'satoshis': 18000000,
'script': '76a914011d2963b619186a318f768dddfd98cd553912a088ac'
}
],
"nLockTime": 534099
'nLockTime': 534099
}
}
]
};
var tx = bitcore.Transaction().fromObject({
"hash": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"version": 1,
"inputs": [
var tx = {
height: 534181,
blockTimestamp: 1441116143,
blockHash: '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013',
hex: '0100000002f379708395d0a0357514205a3758a0317926428356e54a09089852fc6f7297ea010000008a473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964dffffffffb758ffd4c31693d9620f326385404530a079d5e60a90b94e46d3c2dbc29c0a98020000008a473044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b014104d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2ffffffff03605b0300000000001976a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac40992d03000000001976a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac256c0400000000001976a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac00000000',
hash: '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
version: 1,
inputSatoshis: 53839829,
outputSatoshis: 53829829,
feeSatoshis: 10000,
inputs: [
{
"prevTxId": "ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3",
"outputIndex": 1,
"sequenceNumber": 4294967295,
"script": "473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d",
"scriptString": "71 0x3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 65 0x040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d",
"output": {
"satoshis": 53540000,
"script": "76a91454dcfbff9e109bf369e457f6b0f869f4e647076488ac"
}
address: 'moFfnRwt77pApKnnU6m5uocFaa43aAYpt5',
prevTxId: 'ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3',
outputIndex: 1,
sequence: 4294967295,
script: '473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d',
scriptAsm: '71 0x3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 65 0x040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d',
satoshis: 53540000,
},
{
"prevTxId": "980a9cc2dbc2d3464eb9900ae6d579a03045408563320f62d99316c3d4ff58b7",
"outputIndex": 2,
"sequenceNumber": 4294967295,
"script": "473044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b014104d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2",
"scriptString": "71 0x3044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b01 65 0x04d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2",
"output": {
"satoshis": 299829,
"script": "76a914db731c9ebf3874d75ee26b9c19b692d278c283f788ac"
}
address: 'n1XJBAyU4hNR4xRtY3UxnmAteoJX83p5qv',
prevTxId: '980a9cc2dbc2d3464eb9900ae6d579a03045408563320f62d99316c3d4ff58b7',
outputIndex: 2,
sequence: 4294967295,
script: '473044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b014104d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2',
scriptAsm: '71 0x3044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b01 65 0x04d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2',
satoshis: 299829,
}
],
"outputs": [
outputs: [
{
"satoshis": 220000,
"script": "76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac"
satoshis: 220000,
script: '76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac',
address: 'mxT2KzTUQvsaYYothDtjcdvyAdaHA9ofMp'
},
{
"satoshis": 53320000,
"script": "76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac"
satoshis: 53320000,
address: 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK',
script: '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac'
},
{
"satoshis": 289829,
"script": "76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac"
address: 'moZY18rGNmh4YCPeugtGW46AkkWMQttBUD',
satoshis: 289829,
script: '76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac'
}
],
"nLockTime": 0
});
locktime: 0
};
tx.__height = 534181;
tx.__timestamp = 1441116143;
tx.__blockHash = '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013';
var txinfos2 = {
totalCount: 1,
items: [
@ -149,24 +152,24 @@ var txinfos2 = {
var utxos = [
{
"address": "mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK",
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"outputIndex": 1,
"timestamp": 1441116143,
"satoshis": 53320000,
"script": "76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac",
"blockHeight": 534181,
"confirmations": 50
'address': 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK',
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'outputIndex': 1,
'timestamp': 1441116143,
'satoshis': 53320000,
'script': '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac',
'height': 534181,
'confirmations': 50
},
{
"address": "moZY18rGNmh4YCPeugtGW46AkkWMQttBUD",
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"outputIndex": 2,
"timestamp": 1441116143,
"satoshis": 289829,
"script": "76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac",
"blockHeight": 534181,
"confirmations": 50
'address': 'moZY18rGNmh4YCPeugtGW46AkkWMQttBUD',
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'outputIndex': 2,
'timestamp': 1441116143,
'satoshis': 289829,
'script': '76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac',
'height': 534181,
'confirmations': 50
}
];
@ -196,21 +199,21 @@ describe('Addresses', function() {
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"
]
'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 = {
@ -222,6 +225,30 @@ describe('Addresses', function() {
addresses.show(req, res);
});
it('handle error', function() {
var testnode = {};
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
},
addr: 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er'
};
var send = sinon.stub();
var status = sinon.stub().returns({send: send});
var res = {
status: status
};
controller.show(req, res);
send.callCount.should.equal(1);
status.callCount.should.equal(1);
status.args[0][0].should.equal(503);
send.args[0][0].should.equal('test');
});
it('/balance', function(done) {
var insight = 0;
@ -277,16 +304,18 @@ describe('Addresses', function() {
describe('/addr/:addr/utxo', function() {
it('should have correct data', function(done) {
var insight = [
{
"address": "mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK",
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"vout": 1,
"ts": 1441116143,
"scriptPubKey": "76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac",
"amount": 0.5332,
"confirmations": 50,
"confirmationsFromCache": true
}
{
'address': 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK',
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'vout': 1,
'ts': 1441116143,
'scriptPubKey': '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac',
'amount': 0.5332,
'confirmations': 50,
'height': 534181,
'satoshis': 53320000,
'confirmationsFromCache': true
}
];
var todos = [
@ -296,7 +325,12 @@ describe('Addresses', function() {
];
var node = {
getUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1))
services: {
bitcoind: {
height: 534230
}
},
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos.slice(0, 1))
};
var addresses = new AddressController(node);
@ -320,26 +354,30 @@ describe('Addresses', function() {
describe('/addrs/:addrs/utxo', function() {
it('should have the correct data', function(done) {
var insight = [
{
"address": "mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK",
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"vout": 1,
"ts": 1441116143,
"scriptPubKey": "76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac",
"amount": 0.5332,
"confirmations": 50,
"confirmationsFromCache": true
},
{
"address": "moZY18rGNmh4YCPeugtGW46AkkWMQttBUD",
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"vout": 2,
"ts": 1441116143,
"scriptPubKey": "76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac",
"amount": 0.00289829,
"confirmations": 50,
"confirmationsFromCache": true
}
{
'address': 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK',
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'vout': 1,
'ts': 1441116143,
'scriptPubKey': '76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac',
'amount': 0.5332,
'height': 534181,
'satoshis': 53320000,
'confirmations': 50,
'confirmationsFromCache': true
},
{
'address': 'moZY18rGNmh4YCPeugtGW46AkkWMQttBUD',
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'vout': 2,
'ts': 1441116143,
'scriptPubKey': '76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac',
'amount': 0.00289829,
'height': 534181,
'satoshis': 289829,
'confirmations': 50,
'confirmationsFromCache': true
}
];
var todos = [
@ -351,7 +389,12 @@ describe('Addresses', function() {
];
var node = {
getUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos)
services: {
bitcoind: {
height: 534230
}
},
getAddressUnspentOutputs: sinon.stub().callsArgWith(2, null, utxos)
};
var addresses = new AddressController(node);
@ -375,141 +418,150 @@ describe('Addresses', function() {
describe('/addrs/:addrs/txs', function() {
it('should have correct data', function(done) {
var insight = {
"totalItems": 1,
"from": 0,
"to": 1,
"items": [
'totalItems': 1,
'from': 0,
'to': 1,
'items': [
{
'txid': '63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73',
'version': 1,
'locktime': 0,
'vin': [
{
"txid": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73",
"version": 1,
"locktime": 0,
"vin": [
{
"txid": "ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3",
"vout": 1,
"scriptSig": {
"asm": "3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d",
"hex": "473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d"
},
"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
}
],
"vout": [
{
"value": "0.00220000",
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mxT2KzTUQvsaYYothDtjcdvyAdaHA9ofMp"
]
}
},
{
"value": "0.53320000",
"n": 1,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 d2ec20bb8e5f25a52f730384b803d95683250e0b OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914d2ec20bb8e5f25a52f730384b803d95683250e0b88ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK"
]
}
},
{
"value": "0.00289829",
"n": 2,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 583df9fa56ad961051e00ca93e68dfaf1eab9ec5 OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914583df9fa56ad961051e00ca93e68dfaf1eab9ec588ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"moZY18rGNmh4YCPeugtGW46AkkWMQttBUD"
]
}
}
],
"blockhash": "0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013",
"blockheight": 534181,
"confirmations": 52,
"time": 1441116143,
"blocktime": 1441116143,
"valueOut": 0.53829829,
"size": 470,
"valueIn": 0.53839829,
"fees": 0.0001,
"firstSeenTs": 1441108193
'txid': 'ea97726ffc529808094ae5568342267931a058375a20147535a0d095837079f3',
'vout': 1,
'scriptSig': {
'asm': '3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d',
'hex': '473044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e40141040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d'
},
'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
}
]
],
'vout': [
{
'value': '0.00220000',
'n': 0,
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d OP_EQUALVERIFY OP_CHECKSIG',
'hex': '76a914b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d88ac',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': [
'mxT2KzTUQvsaYYothDtjcdvyAdaHA9ofMp'
]
},
'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',
'blockheight': 534181,
'confirmations': 52,
'time': 1441116143,
'blocktime': 1441116143,
'valueOut': 0.53829829,
'size': 470,
'valueIn': 0.53839829,
'fees': 0.0001,
'firstSeenTs': 1441108193
}
]
};
var todos = {
"items": [
'items': [
{
"vin": [
'vin': [
{
"scriptSig": {
"asm": "3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d"
'scriptSig': {
'asm': '3044022054233934268b30be779fad874ef42e8db928ba27a1b612d5f111b3ee95eb271c022024272bbaf2dcc4050bd3b9dfa3c93884f6ba6ad7d257598b8245abb65b5ab1e401 040682fdb281a8533e21e13dfd1fcfa424912a85b6cdc4136b5842c85de05ac1f0e4a013f20702adeb53329de13b2ef388e5ed6244676f4f1ee4ee685ab607964d'
}
},
{
"scriptSig": {
"asm": "3044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b01 04d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2"
'scriptSig': {
'asm': '3044022044938ac3f8fcb8da29011df6397ed28cc7e894cdc35d596d4f3623bd8c7e465f022014829c6e0bd7ee97a1bcfef6b85c5fd232653f289394fc6ce6ebb41c73403f1b01 04d9ccf88efc6e5be3151fae5e848efd94c91d75e7bf621f9f724a8caff51415338525d3239fae6b93826edf759dd562f77693e55dfa852ffd96a92d683db590f2'
}
}
],
"vout": [
'vout': [
{
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d OP_EQUALVERIFY OP_CHECKSIG",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": []
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 b9bbd76588d9e4e09f0369a9aa0b2749a11c4e8d OP_EQUALVERIFY OP_CHECKSIG',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': []
}
},
{
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 d2ec20bb8e5f25a52f730384b803d95683250e0b OP_EQUALVERIFY OP_CHECKSIG",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": []
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 d2ec20bb8e5f25a52f730384b803d95683250e0b OP_EQUALVERIFY OP_CHECKSIG',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': []
}
},
{
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 583df9fa56ad961051e00ca93e68dfaf1eab9ec5 OP_EQUALVERIFY OP_CHECKSIG",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": []
'scriptPubKey': {
'asm': 'OP_DUP OP_HASH160 583df9fa56ad961051e00ca93e68dfaf1eab9ec5 OP_EQUALVERIFY OP_CHECKSIG',
'reqSigs': 1,
'type': 'pubkeyhash',
'addresses': []
}
}
],
"firstSeenTs": 1441108193
'firstSeenTs': 1441108193
}
]
};
@ -517,13 +569,8 @@ describe('Addresses', function() {
var node = {
getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos2),
services: {
db: {
tip: {
__height: 534232
}
},
address: {
getInputForOutput: sinon.stub().callsArgWith(3, null, false),
bitcoind: {
height: 534232
}
},
network: 'testnet'

View File

@ -13,12 +13,15 @@ var blockIndexes = {
hash: '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7',
chainWork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
prevHash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
nextHash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
confirmations: 119,
height: 533974
},
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7': {
hash: '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7',
chainWork: '00000000000000000000000000000000000000000000000544ea52e1575ca753',
prevHash: '00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
confirmations: 119,
height: 533951
},
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441': {
@ -50,9 +53,9 @@ describe('Blocks', function() {
'version': 536870919,
'merkleroot': 'b06437355844b8178173f3e18ca141472e4b0861daa81ef0f701cf9e51f0283e',
'tx': [
'25a988e54b02e0e5df146a0f8fa7b9db56210533a9f04bdfda5f4ceb6f77aadd',
'b85334bf2df35c6dd5b294efe92ffc793a78edff75a2ca666fc296ffb04bbba0',
'2e01c7a4a0e335112236b711c4aaddd02e8dc59ba2cda416e8f80ff06dddd7e1'
'25a988e54b02e0e5df146a0f8fa7b9db56210533a9f04bdfda5f4ceb6f77aadd',
'b85334bf2df35c6dd5b294efe92ffc793a78edff75a2ca666fc296ffb04bbba0',
'2e01c7a4a0e335112236b711c4aaddd02e8dc59ba2cda416e8f80ff06dddd7e1'
],
'time': 1440987503,
'nonce': 1868753784,
@ -69,24 +72,25 @@ describe('Blocks', function() {
var bitcoreBlock = bitcore.Block.fromBuffer(new Buffer(blocks['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'], 'hex'));
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, bitcoreBlock),
services: {
bitcoind: {
getNextBlockHash: sinon.stub().returns('000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d'),
getBlockIndex: sinon.stub().returns(blockIndexes['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7']),
isMainChain: sinon.stub().returns(true)
},
db: {
tip: {
__height: 534092
}
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7']),
isMainChain: sinon.stub().returns(true),
height: 534092
}
}
};
it('block data should be correct', function(done) {
var controller = new BlockController(node);
var req = {};
var controller = new BlockController({node: node});
var hash = '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7';
var req = {
params: {
blockHash: hash
}
};
var res = {};
var next = function() {
should.exist(req.block);
@ -94,31 +98,28 @@ describe('Blocks', function() {
should(block).eql(insight);
done();
};
var hash = '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7';
controller.block(req, res, next, hash);
controller.block(req, res, next);
});
it('block pool info should be correct', function(done) {
var block = bitcore.Block.fromString(blocks['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']);
var node = {
log: sinon.stub(),
getBlock: sinon.stub().callsArgWith(1, null, block),
services: {
bitcoind: {
getNextBlockHash: sinon.stub().returns('000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d'),
getBlockIndex: sinon.stub().returns(blockIndexes['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']),
isMainChain: sinon.stub().returns(true)
},
db: {
tip: {
__height: 534092
}
getBlockHeader: sinon.stub().callsArgWith(1, null, blockIndexes['000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4']),
isMainChain: sinon.stub().returns(true),
height: 534092
}
}
};
var controller = new BlockController(node);
var req = {};
var controller = new BlockController({node: node});
var req = {
params: {
blockHash: hash
}
};
var res = {};
var next = function() {
should.exist(req.block);
@ -130,7 +131,7 @@ describe('Blocks', function() {
var hash = '000000000000000004a118407a4e3556ae2d5e882017e7ce526659d8073f13a4';
controller.block(req, res, next, hash);
controller.block(req, res, next);
});
});
@ -138,65 +139,64 @@ describe('Blocks', function() {
describe('/blocks route', function() {
var insight = {
"blocks": [
'blocks': [
{
"height": 533951,
"size": 206,
"hash": "000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7",
"time": 1440978683,
"txlength": 1,
"poolInfo": {
"poolName": "AntMiner",
"url": "https://bitmaintech.com/"
'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/"
'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"
'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, bitcore.Block.fromBuffer(blocks['000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'], 'hex'));
stub.onSecondCall().callsArgWith(1, null, bitcore.Block.fromBuffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex'))
stub.onFirstCall().callsArgWith(1, null, new Buffer(blocks['000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'], 'hex'));
stub.onSecondCall().callsArgWith(1, null, new Buffer(blocks['00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'], 'hex'));
var hashes = [
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7',
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441'
'00000000000006bd8fe9e53780323c0e85719eca771022e1eb6d10c62195c441',
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7'
];
var node = {
getBlock: stub,
log: sinon.stub(),
services: {
bitcoind: {
getBlockIndex: function(hash) {
return blockIndexes[hash];
}
},
db: {
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);
var blocks = new BlockController({node: node});
var req = {
query: {
@ -218,38 +218,46 @@ describe('Blocks', function() {
describe('/block-index/:height route', function() {
var node = {
log: sinon.stub(),
services: {
bitcoind: {
getBlockIndex: function(height) {
return blockIndexes[height];
getBlockHeader: function(height, callback) {
callback(null, blockIndexes[height]);
}
}
}
};
it('should have correct data', function(done) {
var blocks = new BlockController(node);
var blocks = new BlockController({node: node});
var insight = {
"blockHash": "0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7"
'blockHash': '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'
};
var req = {};
var height = 533974;
var req = {
params: {
height: height
}
};
var res = {
jsonp: function(data) {
should(data).eql(insight);
done();
}
};
var next = function() {};
var height = 533974;
blocks.blockIndex(req, res, next, height);
blocks.blockIndex(req, res);
});
});
describe('#getBlockReward', function() {
var blocks = new BlockController({});
var node = {
log: sinon.stub()
};
var blocks = new BlockController({node: node});
it('should give a block reward of 50 * 1e8 for block before first halvening', function() {
blocks.getBlockReward(100000).should.equal(50 * 1e8);

View File

@ -7,8 +7,12 @@ var InsightAPI = require('../lib/index');
describe('Index', function() {
describe('#cache', function() {
it('will set cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: true
enableCache: true,
node: node
});
var req = {};
var res = {
@ -23,8 +27,12 @@ describe('Index', function() {
});
});
it('will NOT set cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: false
enableCache: false,
node: node
});
var req = {};
var res = {
@ -39,9 +47,13 @@ describe('Index', function() {
});
describe('#cacheShort', function() {
it('will set SHORT cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: true,
cacheShortSeconds: 35
cacheShortSeconds: 35,
node: node
});
var req = {};
var res = {
@ -56,8 +68,12 @@ describe('Index', function() {
});
});
it('will set SHORT DEFAULT cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: true
enableCache: true,
node: node
});
var req = {};
var res = {
@ -74,9 +90,13 @@ describe('Index', function() {
});
describe('#cacheLong', function() {
it('will set LONG cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: true,
cacheLongSeconds: 86400000
cacheLongSeconds: 86400000,
node: node
});
var req = {};
var res = {
@ -91,8 +111,12 @@ describe('Index', function() {
});
});
it('will set LONG DEFAULT cache control header', function(done) {
var node = {
log: sinon.stub()
};
var index = new InsightAPI({
enableCache: true
enableCache: true,
node: node
});
var req = {};
var res = {

189
test/ratelimeter.js Normal file
View File

@ -0,0 +1,189 @@
'use strict';
var should = require('should');
var sinon = require('sinon');
var RateLimiter = require('../lib/ratelimiter');
describe('RateLimiter', function() {
describe('@constructor', function() {
it('will instantiate without options', function() {
var limiter = new RateLimiter();
should.exist(limiter);
});
it('will instantiate without new', function() {
/* jshint newcap:false */
var limiter = RateLimiter();
should.exist(limiter);
});
it('will instantiate with options', function() {
var whitelist = [];
var blacklist = [];
var node = {};
var limiter = new RateLimiter({
node: node,
whitelist: whitelist,
blacklist: blacklist,
limit: 1,
interval: 1,
whitelistLimit: 1,
whitelistInterval: 1,
blacklistLimit: 1,
blacklistInterval: 1
});
should.exist(limiter);
should.exist(limiter.config);
should.exist(limiter.clients);
should.exist(limiter.node);
limiter.whitelist.should.equal(whitelist);
limiter.blacklist.should.equal(blacklist);
limiter.config.whitelist.totalRequests.should.equal(1);
limiter.config.whitelist.interval.should.equal(1);
limiter.config.blacklist.totalRequests.should.equal(1);
limiter.config.blacklist.interval.should.equal(1);
limiter.config.normal.interval.should.equal(1);
limiter.config.normal.totalRequests.should.equal(1);
});
});
describe('#middleware', function() {
it('will set ratelimit headers', function(done) {
var limiter = new RateLimiter();
var req = {
headers: {
'cf-connecting-ip': '127.0.0.1'
}
};
var setHeader = sinon.stub();
var res = {
setHeader: setHeader
};
limiter.middleware()(req, res, function() {
setHeader.callCount.should.equal(2);
setHeader.args[0][0].should.equal('X-RateLimit-Limit');
setHeader.args[0][1].should.equal(10800);
setHeader.args[1][0].should.equal('X-RateLimit-Remaining');
setHeader.args[1][1].should.equal(10799);
done();
});
});
it('will give rate limit error', function() {
var node = {
log: {
warn: sinon.stub()
}
};
var limiter = new RateLimiter({node: node});
limiter.exceeded = sinon.stub().returns(true);
var req = {
headers: {
'cf-connecting-ip': '127.0.0.1'
}
};
var jsonp = sinon.stub();
var status = sinon.stub().returns({
jsonp: jsonp
});
var res = {
status: status,
setHeader: sinon.stub()
};
limiter.middleware()(req, res);
status.callCount.should.equal(1);
status.args[0][0].should.equal(429);
jsonp.callCount.should.equal(1);
jsonp.args[0][0].should.eql({
status: 429,
error: 'Rate limit exceeded'
});
});
});
describe('#exceeded', function() {
it('should not be exceeded', function() {
var node = {};
var limiter = new RateLimiter({node: node});
var client = limiter.addClient('127.0.0.1');
var exceeded = limiter.exceeded(client);
exceeded.should.equal(false);
});
it('should be exceeded', function() {
var node = {};
var limiter = new RateLimiter({node: node});
var client = limiter.addClient('127.0.0.1');
client.visits = 3 * 60 * 60 + 1;
var exceeded = limiter.exceeded(client);
exceeded.should.equal(true);
});
it('should exclude whitelisted with no limit', function() {
var node = {};
var limiter = new RateLimiter({
whitelist: [
'127.0.0.1'
],
node: node,
whitelistLimit: -1
});
var client = limiter.addClient('127.0.0.1');
client.visits = Infinity;
var exceeded = limiter.exceeded(client);
exceeded.should.equal(false);
});
});
describe('#getClientName', function() {
it('should get client name from cloudflare header', function() {
var node = {};
var limiter = new RateLimiter({node: node});
var req = {
headers: {
'cf-connecting-ip': '127.0.0.1'
}
};
var name = limiter.getClientName(req);
name.should.equal('127.0.0.1');
});
it('should get client name from x forwarded header', function() {
var node = {};
var limiter = new RateLimiter({node: node});
var req = {
headers: {
'x-forwarded-for': '127.0.0.1'
}
};
var name = limiter.getClientName(req);
name.should.equal('127.0.0.1');
});
it('should get client name from connection remote address', function() {
var node = {};
var limiter = new RateLimiter({node: node});
var req = {
headers: {},
connection: {
remoteAddress: '127.0.0.1'
}
};
var name = limiter.getClientName(req);
name.should.equal('127.0.0.1');
});
});
describe('#addClient', function() {
var sandbox = sinon.sandbox.create();
afterEach(function() {
sandbox.restore();
});
it('will remove client after interval', function() {
var THREE_HOURS_PLUS = 3 * 60 * 60 * 1000 + 1;
var clock = sandbox.useFakeTimers();
var node = {};
var limiter = new RateLimiter({node: node});
limiter.addClient('127.0.0.1');
should.exist(limiter.clients['127.0.0.1']);
clock.tick(THREE_HOURS_PLUS);
should.not.exist(limiter.clients['127.0.0.1']);
});
});
});

View File

@ -8,13 +8,13 @@ describe('Status', function() {
describe('/status', function() {
var info = {
version: 110000,
protocolversion: 70002,
protocolVersion: 70002,
blocks: 548645,
timeoffset: 0,
timeOffset: 0,
connections: 8,
difficulty: 21546.906405522557,
testnet: true,
relayfee: 1000,
relayFee: 1000,
errors: ''
};
@ -31,13 +31,9 @@ describe('Status', function() {
var node = {
services: {
bitcoind: {
getInfo: sinon.stub().returns(info),
getBestBlockHash: sinon.stub().returns(outSetInfo.bestblock)
},
db: {
tip: {
hash: outSetInfo.bestblock
}
getInfo: sinon.stub().callsArgWith(0, null, info),
getBestBlockHash: sinon.stub().callsArgWith(0, null, outSetInfo.bestblock),
tiphash: outSetInfo.bestblock
}
}
};
@ -118,15 +114,10 @@ describe('Status', function() {
it('should have correct data', function(done) {
var node = {
services: {
db: {
tip: {
__height: 500000
}
},
bitcoind: {
height: 500000,
isSynced: sinon.stub().returns(true),
syncPercentage: sinon.stub().returns(99.99)
isSynced: sinon.stub().callsArgWith(0, null, true),
syncPercentage: sinon.stub().callsArgWith(0, null, 99.99)
}
}
};

File diff suppressed because it is too large Load Diff

View File

@ -10,12 +10,12 @@ describe('Utils', function() {
var node = {
services: {
bitcoind: {
estimateFee: function(blocks) {
estimateFee: function(blocks, callback) {
switch(blocks) {
case 1:
return 1000;
case 3:
return 3000;
case 1:
return callback(null, 1000 / 1e8);
case 3:
return callback(null, 3000 / 1e8);
}
}
}