2014-02-03 18:30:46 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var imports = require('soop').imports();
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
// to show tx outs
|
|
|
|
var OUTS_PREFIX = 'txo-'; //txo-<txid>-<n> => [addr, btc_sat]
|
|
|
|
var SPENT_PREFIX = 'txs-'; //txs-<txid(out)>-<n(out)>-<txid(in)>-<n(in)> = ts
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
// to sum up addr balance (only outs, spents are gotten later)
|
2014-04-15 05:15:47 -07:00
|
|
|
var ADDR_PREFIX = 'txa-'; //txa-<addr>-<txid>-<n> => + btc_sat:ts [:<txid>-<n>](spent)
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
// TODO: use bitcore networks module
|
|
|
|
var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
|
|
|
|
var CONCURRENCY = 10;
|
2014-05-25 13:34:49 -07:00
|
|
|
var DEFAULT_SAFE_CONFIRMATIONS = 6;
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var MAX_OPEN_FILES = 500;
|
2014-02-16 08:38:34 -08:00
|
|
|
// var CONFIRMATION_NR_TO_NOT_CHECK = 10; //Spend
|
2014-03-05 18:03:56 -08:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-04-17 05:32:49 -07:00
|
|
|
|
2014-05-07 06:42:45 -07:00
|
|
|
var bitcore = require('bitcore'),
|
|
|
|
Rpc = imports.rpc || require('./Rpc'),
|
|
|
|
util = bitcore.util,
|
|
|
|
networks = bitcore.networks,
|
|
|
|
levelup = require('levelup'),
|
|
|
|
async = require('async'),
|
|
|
|
config = require('../config/config'),
|
2014-05-25 19:54:08 -07:00
|
|
|
assert = require('assert'),
|
|
|
|
Script = bitcore.Script,
|
|
|
|
bitcoreUtil = bitcore.util,
|
|
|
|
buffertools = require('buffertools');
|
2014-04-17 05:32:49 -07:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
var logger = require('./logger').logger;
|
|
|
|
var info = logger.info;
|
|
|
|
|
2014-05-25 19:54:08 -07:00
|
|
|
var db = imports.db || levelup(config.leveldb + '/txs',{maxOpenFiles: MAX_OPEN_FILES} );
|
2014-05-23 17:23:44 -07:00
|
|
|
var PoolMatch = imports.poolMatch || require('soop').load('./PoolMatch',config);
|
2014-05-25 19:54:08 -07:00
|
|
|
// This is 0.1.2 = > c++ version of base58-native
|
|
|
|
var base58 = require('base58-native').base58Check;
|
|
|
|
var encodedData = require('soop').load('bitcore/util/EncodedData',{
|
|
|
|
base58: base58
|
|
|
|
});
|
|
|
|
var versionedData= require('soop').load('bitcore/util/VersionedData',{
|
|
|
|
parent: encodedData
|
|
|
|
});
|
|
|
|
|
|
|
|
var Address = require('soop').load('bitcore/lib/Address',{
|
|
|
|
parent: versionedData
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
|
|
|
|
var TransactionDb = function() {
|
|
|
|
TransactionDb.super(this, arguments);
|
|
|
|
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
|
2014-05-21 11:51:24 -07:00
|
|
|
this.poolMatch = new PoolMatch();
|
2014-05-25 13:34:49 -07:00
|
|
|
this.safeConfirmations = config.safeConfirmations || DEFAULT_SAFE_CONFIRMATIONS;
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
TransactionDb.prototype.close = function(cb) {
|
|
|
|
db.close(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
TransactionDb.prototype.drop = function(cb) {
|
|
|
|
var path = config.leveldb + '/txs';
|
|
|
|
db.close(function() {
|
|
|
|
require('leveldown').destroy(path, function() {
|
|
|
|
db = levelup(path, {maxOpenFiles: 500});
|
|
|
|
return cb();
|
2014-02-03 18:30:46 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
TransactionDb.prototype._addSpentInfo = function(r, txid, index, ts) {
|
|
|
|
if (r.spentTxId) {
|
|
|
|
if (!r.multipleSpentAttempts) {
|
|
|
|
r.multipleSpentAttempts = [{
|
|
|
|
txid: r.spentTxId,
|
|
|
|
index: r.index,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
r.multipleSpentAttempts.push({
|
|
|
|
txid: txid,
|
|
|
|
index: parseInt(index),
|
2014-02-03 18:30:46 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
} else {
|
|
|
|
r.spentTxId = txid;
|
|
|
|
r.spentIndex = parseInt(index);
|
|
|
|
r.spentTs = parseInt(ts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This is not used now
|
|
|
|
TransactionDb.prototype.fromTxId = function(txid, cb) {
|
|
|
|
var self = this;
|
|
|
|
var k = OUTS_PREFIX + txid;
|
|
|
|
var ret = [];
|
|
|
|
var idx = {};
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
// outs.
|
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
|
|
|
var k = data.key.split('-');
|
|
|
|
var v = data.value.split(':');
|
|
|
|
ret.push({
|
|
|
|
addr: v[0],
|
|
|
|
value_sat: parseInt(v[1]),
|
|
|
|
index: parseInt(k[2]),
|
2014-02-10 12:12:40 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
idx[parseInt(k[2])] = i++;
|
2014-02-14 11:35:32 -08:00
|
|
|
})
|
2014-03-05 18:03:56 -08:00
|
|
|
.on('error', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('end', function() {
|
2014-02-10 12:12:40 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var k = SPENT_PREFIX + txid + '-';
|
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
|
|
|
var k = data.key.split('-');
|
|
|
|
var j = idx[parseInt(k[2])];
|
2014-02-10 09:09:28 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
assert(typeof j !== 'undefined', 'Spent could not be stored: tx ' + txid +
|
|
|
|
'spent in TX:' + k[1] + ',' + k[2] + ' j:' + j);
|
2014-02-10 09:09:28 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
self._addSpentInfo(ret[j], k[3], k[4], data.value);
|
|
|
|
})
|
|
|
|
.on('error', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('end', function(err) {
|
|
|
|
return cb(err, ret);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-02-11 09:50:19 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
TransactionDb.prototype._fillSpent = function(info, cb) {
|
|
|
|
var self = this;
|
2014-02-11 09:50:19 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
if (!info) return cb();
|
2014-02-11 09:50:19 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var k = SPENT_PREFIX + info.txid + '-';
|
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
|
|
|
var k = data.key.split('-');
|
|
|
|
self._addSpentInfo(info.vout[k[2]], k[3], k[4], data.value);
|
2014-02-14 11:35:32 -08:00
|
|
|
})
|
2014-03-05 18:03:56 -08:00
|
|
|
.on('error', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('end', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
2014-02-11 09:50:19 -08:00
|
|
|
|
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
TransactionDb.prototype._fillOutpoints = function(txInfo, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-02-04 18:50:18 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
if (!txInfo || txInfo.isCoinBase) return cb();
|
2014-02-04 18:50:18 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var valueIn = 0;
|
|
|
|
var incompleteInputs = 0;
|
2014-02-04 18:50:18 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
async.eachLimit(txInfo.vin, CONCURRENCY, function(i, c_in) {
|
|
|
|
self.fromTxIdN(i.txid, i.vout, txInfo.confirmations, function(err, ret) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (!ret || !ret.addr || !ret.valueSat) {
|
2014-05-25 13:34:49 -07:00
|
|
|
info('Could not get TXouts in %s,%d from %s ', i.txid, i.vout, txInfo.txid);
|
2014-03-05 18:03:56 -08:00
|
|
|
if (ret) i.unconfirmedInput = ret.unconfirmedInput;
|
|
|
|
incompleteInputs = 1;
|
|
|
|
return c_in(); // error not scalated
|
|
|
|
}
|
2014-02-11 08:31:33 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
txInfo.firstSeenTs = ret.spentTs;
|
2014-03-05 18:03:56 -08:00
|
|
|
i.unconfirmedInput = i.unconfirmedInput;
|
|
|
|
i.addr = ret.addr;
|
|
|
|
i.valueSat = ret.valueSat;
|
|
|
|
i.value = ret.valueSat / util.COIN;
|
|
|
|
valueIn += i.valueSat;
|
2014-02-11 08:31:33 -08:00
|
|
|
|
2014-02-15 15:26:17 -08:00
|
|
|
/*
|
2014-03-05 18:03:56 -08:00
|
|
|
* If confirmed by bitcoind, we could not check for double spents
|
|
|
|
* but we prefer to keep the flag of double spent attempt
|
|
|
|
*
|
2014-05-25 13:34:49 -07:00
|
|
|
if (txInfo.confirmations
|
|
|
|
&& txInfo.confirmations >= CONFIRMATION_NR_TO_NOT_CHECK)
|
2014-03-05 18:03:56 -08:00
|
|
|
return c_in();
|
2014-02-16 08:38:34 -08:00
|
|
|
isspent
|
2014-02-15 15:26:17 -08:00
|
|
|
*/
|
2014-03-05 18:03:56 -08:00
|
|
|
// Double spent?
|
|
|
|
if (ret.multipleSpentAttempt || !ret.spentTxId ||
|
2014-05-25 13:34:49 -07:00
|
|
|
(ret.spentTxId && ret.spentTxId !== txInfo.txid)
|
2014-03-05 18:03:56 -08:00
|
|
|
) {
|
|
|
|
if (ret.multipleSpentAttempts) {
|
2014-04-23 19:45:01 -07:00
|
|
|
ret.multipleSpentAttempts.forEach(function(mul) {
|
2014-05-25 13:34:49 -07:00
|
|
|
if (mul.spentTxId !== txInfo.txid) {
|
2014-03-05 18:03:56 -08:00
|
|
|
i.doubleSpentTxID = ret.spentTxId;
|
|
|
|
i.doubleSpentIndex = ret.spentIndex;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (!ret.spentTxId) {
|
|
|
|
i.dbError = 'Input spent not registered';
|
2014-02-14 11:35:32 -08:00
|
|
|
} else {
|
2014-03-05 18:03:56 -08:00
|
|
|
i.doubleSpentTxID = ret.spentTxId;
|
|
|
|
i.doubleSpentIndex = ret.spentIndex;
|
2014-02-11 08:31:33 -08:00
|
|
|
}
|
2014-02-14 11:35:32 -08:00
|
|
|
} else {
|
2014-03-05 18:03:56 -08:00
|
|
|
i.doubleSpentTxID = null;
|
2014-02-14 11:35:32 -08:00
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
return c_in();
|
2014-02-04 18:50:18 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
},
|
|
|
|
function() {
|
|
|
|
if (!incompleteInputs) {
|
2014-05-25 13:34:49 -07:00
|
|
|
txInfo.valueIn = valueIn / util.COIN;
|
|
|
|
txInfo.fees = (valueIn - (txInfo.valueOut * util.COIN)).toFixed(0) / util.COIN;
|
2014-03-05 18:03:56 -08:00
|
|
|
} else {
|
2014-05-25 13:34:49 -07:00
|
|
|
txInfo.incompleteInputs = 1;
|
2014-03-05 18:03:56 -08:00
|
|
|
}
|
|
|
|
return cb();
|
2014-02-04 18:50:18 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-02-04 18:50:18 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
TransactionDb.prototype._getInfo = function(txid, next) {
|
|
|
|
var self = this;
|
2014-02-04 18:50:18 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
Rpc.getTxInfo(txid, function(err, txInfo) {
|
2014-03-05 18:03:56 -08:00
|
|
|
if (err) return next(err);
|
2014-05-25 13:34:49 -07:00
|
|
|
self._fillOutpoints(txInfo, function() {
|
|
|
|
self._fillSpent(txInfo, function() {
|
|
|
|
return next(null, txInfo);
|
2014-02-14 11:35:32 -08:00
|
|
|
});
|
2014-02-04 18:50:18 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
// Simplified / faster Info version: No spent / outpoints info.
|
|
|
|
TransactionDb.prototype.fromIdInfoSimple = function(txid, cb) {
|
|
|
|
Rpc.getTxInfo(txid, true, function(err, info) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (!info) return cb();
|
|
|
|
return cb(err, info);
|
|
|
|
});
|
|
|
|
};
|
2014-02-11 08:31:33 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
TransactionDb.prototype.fromIdWithInfo = function(txid, cb) {
|
|
|
|
var self = this;
|
2014-02-15 15:26:17 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
self._getInfo(txid, function(err, info) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (!info) return cb();
|
|
|
|
return cb(err, {
|
|
|
|
txid: txid,
|
|
|
|
info: info
|
2014-02-03 18:30:46 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-06 05:20:49 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
TransactionDb.prototype.fromTxIdN = function(txid, n, confirmations, cb) {
|
|
|
|
var self = this;
|
|
|
|
var k = OUTS_PREFIX + txid + '-' + n;
|
2014-02-06 05:20:49 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
db.get(k, function(err, val) {
|
|
|
|
if (!val || (err && err.notFound)) {
|
|
|
|
return cb(null, {
|
|
|
|
unconfirmedInput: 1
|
|
|
|
});
|
|
|
|
}
|
2014-02-10 12:12:40 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var a = val.split(':');
|
|
|
|
var ret = {
|
|
|
|
addr: a[0],
|
|
|
|
valueSat: parseInt(a[1]),
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this TxID comes from an RPC request
|
|
|
|
* the .confirmations value from bitcoind is available
|
2014-05-20 14:07:25 -07:00
|
|
|
* so we could avoid checking if the input was double spent
|
2014-03-05 18:03:56 -08:00
|
|
|
*
|
|
|
|
* This speed up address calculations by ~30%
|
|
|
|
*
|
|
|
|
if (confirmations >= CONFIRMATION_NR_TO_NOT_CHECK) {
|
|
|
|
return cb(null, ret);
|
2014-02-10 12:12:40 -08:00
|
|
|
}
|
2014-03-05 18:03:56 -08:00
|
|
|
*/
|
2014-02-03 23:06:03 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
// spent?
|
|
|
|
var k = SPENT_PREFIX + txid + '-' + n + '-';
|
2014-02-14 11:35:32 -08:00
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
2014-02-03 23:06:03 -08:00
|
|
|
var k = data.key.split('-');
|
2014-03-05 18:03:56 -08:00
|
|
|
self._addSpentInfo(ret, k[3], k[4], data.value);
|
2014-02-03 23:06:03 -08:00
|
|
|
})
|
2014-03-05 18:03:56 -08:00
|
|
|
.on('error', function(error) {
|
|
|
|
return cb(error);
|
2014-02-03 23:06:03 -08:00
|
|
|
})
|
2014-02-14 11:35:32 -08:00
|
|
|
.on('end', function() {
|
2014-03-05 18:03:56 -08:00
|
|
|
return cb(null, ret);
|
2014-02-03 23:06:03 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-03 23:06:03 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
|
|
|
|
TransactionDb.prototype.deleteCacheForAddress = function(addr,cb) {
|
|
|
|
var k = ADDR_PREFIX + addr + '-';
|
|
|
|
var dbScript = [];
|
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
|
|
|
var v = data.value.split(':');
|
|
|
|
dbScript.push({
|
|
|
|
type: 'put',
|
|
|
|
key: data.key,
|
|
|
|
value: v.slice(0,2).join(':'),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on('error', function(err) {
|
|
|
|
return cb(err);
|
|
|
|
})
|
|
|
|
.on('end', function (){
|
|
|
|
db.batch(dbScript,cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
TransactionDb.prototype.cacheConfirmations = function(txouts,cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var self = this;
|
2014-02-03 23:06:03 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
var dbScript=[];
|
|
|
|
for(var ii in txouts){
|
|
|
|
var txout=txouts[ii];
|
2014-05-23 17:23:44 -07:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
//everything already cached?
|
|
|
|
if (txout.spentIsConfirmedCached) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
var infoToCache = [];
|
|
|
|
if (txout.confirmations > self.safeConfirmations) {
|
|
|
|
if (!txout.isConfirmedCached) infoToCache.push(1);
|
|
|
|
|
|
|
|
if (txout.spentConfirmations > self.safeConfirmations) {
|
|
|
|
// console.log('[TransactionDb.js.309]',txout); //TODO
|
|
|
|
infoToCache = infoToCache.concat([1, txout.spentTxId, txout.spentIndex, txout.spentTs]);
|
|
|
|
}
|
|
|
|
if (infoToCache.length){
|
|
|
|
|
|
|
|
// if spent, we overwrite scriptPubKey cache (not needed anymore)
|
|
|
|
// Last 1 = txout.isConfirmedCached (must be equal to 1 at this point)
|
|
|
|
infoToCache.unshift(txout.value_sat,txout.ts, 1);
|
|
|
|
dbScript.push({
|
|
|
|
type: 'put',
|
|
|
|
key: txout.key,
|
|
|
|
value: infoToCache.join(':'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log('[TransactionDb.js.339:dbScript:]',dbScript); //TODO
|
|
|
|
db.batch(dbScript,cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var dbScript=[];
|
|
|
|
for(var ii in txouts){
|
|
|
|
var txout=txouts[ii];
|
|
|
|
|
|
|
|
//everything already cached?
|
|
|
|
if (txout.scriptPubKeyCached || txout.spentTxId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (txout.scriptPubKey) {
|
|
|
|
var infoToCache = [txout.value_sat,txout.ts, txout.isConfirmedCached?1:0, txout.scriptPubKey];
|
|
|
|
dbScript.push({
|
|
|
|
type: 'put',
|
|
|
|
key: txout.key,
|
|
|
|
value: infoToCache.join(':'),
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
}
|
2014-05-25 13:34:49 -07:00
|
|
|
}
|
|
|
|
db.batch(dbScript,cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TransactionDb.prototype._parseAddrData = function(data) {
|
|
|
|
var k = data.key.split('-');
|
|
|
|
var v = data.value.split(':');
|
|
|
|
var item = {
|
|
|
|
key: data.key,
|
|
|
|
txid: k[2],
|
|
|
|
index: parseInt(k[3]),
|
|
|
|
value_sat: parseInt(v[0]),
|
|
|
|
ts: parseInt(v[1]),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Cache
|
|
|
|
if (v[2]){
|
|
|
|
item.isConfirmed = 1;
|
|
|
|
item.isConfirmedCached = 1;
|
|
|
|
//console.log('[TransactionDb.js.356] CACHE HIT CONF:', item.key); //TODO
|
|
|
|
// Sent, confirmed
|
|
|
|
if (v[3] === 1){
|
|
|
|
|
|
|
|
//console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO
|
|
|
|
item.spentIsConfirmed = 1;
|
|
|
|
item.spentIsConfirmedCached = 1;
|
|
|
|
item.spentTxId = v[4];
|
|
|
|
item.spentIndex = parseInt(v[5]);
|
|
|
|
item.spentTs = parseInt(v[6]);
|
|
|
|
}
|
|
|
|
// Scriptpubkey cached
|
|
|
|
else if (v[3]) {
|
|
|
|
item.scriptPubKey = v[3];
|
|
|
|
item.scriptPubKeyCached = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item;
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
TransactionDb.prototype.fromAddr = function(addr, cb) {
|
|
|
|
var self = this;
|
|
|
|
var k = ADDR_PREFIX + addr + '-';
|
|
|
|
var ret = [];
|
|
|
|
|
|
|
|
db.createReadStream({
|
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
2014-05-25 13:34:49 -07:00
|
|
|
ret.push(self._parseAddrData(data));
|
2014-03-05 18:03:56 -08:00
|
|
|
})
|
2014-05-25 13:34:49 -07:00
|
|
|
.on('error', cb)
|
2014-03-05 18:03:56 -08:00
|
|
|
.on('end', function() {
|
2014-05-25 13:34:49 -07:00
|
|
|
async.eachLimit(ret.filter(function(x){return !x.spentIsConfirmed;}), CONCURRENCY, function(o, e_c) {
|
2014-03-05 18:03:56 -08:00
|
|
|
var k = SPENT_PREFIX + o.txid + '-' + o.index + '-';
|
2014-02-14 11:35:32 -08:00
|
|
|
db.createReadStream({
|
2014-03-05 18:03:56 -08:00
|
|
|
start: k,
|
|
|
|
end: k + '~'
|
|
|
|
})
|
|
|
|
.on('data', function(data) {
|
|
|
|
var k = data.key.split('-');
|
|
|
|
self._addSpentInfo(o, k[3], k[4], data.value);
|
|
|
|
})
|
2014-05-25 13:34:49 -07:00
|
|
|
.on('error', e_c)
|
|
|
|
.on('end', e_c);
|
2014-02-14 11:35:32 -08:00
|
|
|
},
|
2014-05-25 13:34:49 -07:00
|
|
|
function(err) {
|
|
|
|
return cb(err, ret);
|
2014-02-14 11:35:32 -08:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-14 11:35:32 -08:00
|
|
|
|
2014-05-25 19:54:08 -07:00
|
|
|
TransactionDb.prototype._fromBuffer = function (buf) {
|
|
|
|
var buf2 = buffertools.reverse(buf);
|
|
|
|
return parseInt(buf2.toString('hex'), 16);
|
|
|
|
};
|
|
|
|
|
|
|
|
TransactionDb.prototype.getStandardizedTx = function (tx, time, isCoinBase) {
|
|
|
|
var self = this;
|
|
|
|
tx.txid = bitcoreUtil.formatHashFull(tx.getHash());
|
|
|
|
var ti=0;
|
|
|
|
|
|
|
|
tx.vin = tx.ins.map(function(txin) {
|
|
|
|
var ret = {n: ti++};
|
|
|
|
if (isCoinBase) {
|
|
|
|
ret.isCoinBase = true;
|
|
|
|
} else {
|
|
|
|
ret.txid = buffertools.reverse(new Buffer(txin.getOutpointHash())).toString('hex');
|
|
|
|
ret.vout = txin.getOutpointIndex();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
var to = 0;
|
|
|
|
tx.vout = tx.outs.map(function(txout) {
|
|
|
|
var val;
|
|
|
|
if (txout.s) {
|
|
|
|
var s = new Script(txout.s);
|
|
|
|
var addrs = new Address.fromScriptPubKey(s, config.network);
|
|
|
|
// support only for p2pubkey p2pubkeyhash and p2sh
|
|
|
|
if (addrs && addrs.length === 1) {
|
|
|
|
val = {addresses: [addrs[0].toString() ] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
valueSat: self._fromBuffer(txout.v),
|
|
|
|
scriptPubKey: val,
|
|
|
|
n: to++,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
tx.time = time;
|
|
|
|
return tx;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
TransactionDb.prototype.fillScriptPubKey = function(txouts, cb) {
|
|
|
|
var self=this;
|
|
|
|
// Complete utxo info
|
|
|
|
async.eachLimit(txouts, CONCURRENCY, function (txout, a_c) {
|
|
|
|
self.fromIdInfoSimple(txout.txid, function(err, info) {
|
|
|
|
if (!info || !info.vout) return a_c(err);
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
txout.scriptPubKey = info.vout[txout.index].scriptPubKey.hex;
|
|
|
|
return a_c();
|
|
|
|
});
|
|
|
|
}, function(){
|
|
|
|
self.cacheScriptPubKey(txouts, cb);
|
|
|
|
});
|
|
|
|
};
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
TransactionDb.prototype.removeFromTxId = function(txid, cb) {
|
2014-03-05 18:03:56 -08:00
|
|
|
async.series([
|
|
|
|
function(c) {
|
|
|
|
db.createReadStream({
|
|
|
|
start: OUTS_PREFIX + txid + '-',
|
|
|
|
end: OUTS_PREFIX + txid + '~',
|
|
|
|
}).pipe(
|
|
|
|
db.createWriteStream({
|
|
|
|
type: 'del'
|
|
|
|
})
|
|
|
|
).on('close', c);
|
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
db.createReadStream({
|
|
|
|
start: SPENT_PREFIX + txid + '-',
|
|
|
|
end: SPENT_PREFIX + txid + '~'
|
|
|
|
})
|
|
|
|
.pipe(
|
|
|
|
db.createWriteStream({
|
|
|
|
type: 'del'
|
|
|
|
})
|
|
|
|
).on('close', c);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
2014-02-14 13:02:57 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
|
|
|
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
TransactionDb.prototype._addScript = function(tx) {
|
|
|
|
var relatedAddrs = [];
|
|
|
|
var dbScript = [];
|
|
|
|
var ts = tx.time;
|
2014-05-25 19:54:08 -07:00
|
|
|
var txid = tx.txid || tx.hash;
|
|
|
|
// var u=require('util');
|
|
|
|
// console.log('[TransactionDb.js.518]', u.inspect(tx,{depth:10})); //TODO
|
2014-05-23 17:23:44 -07:00
|
|
|
// Input Outpoints (mark them as spent)
|
2014-05-25 13:34:49 -07:00
|
|
|
for(var ii in tx.vin) {
|
|
|
|
var i = tx.vin[ii];
|
|
|
|
if (i.txid){
|
|
|
|
var k = SPENT_PREFIX + i.txid + '-' + i.vout + '-' + txid + '-' + i.n;
|
2014-05-23 17:23:44 -07:00
|
|
|
dbScript.push({
|
|
|
|
type: 'put',
|
2014-05-25 13:34:49 -07:00
|
|
|
key: k,
|
2014-05-23 17:23:44 -07:00
|
|
|
value: ts || 0,
|
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
}
|
2014-05-23 17:23:44 -07:00
|
|
|
}
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
for(var ii in tx.vout) {
|
|
|
|
var o = tx.vout[ii];
|
2014-05-25 13:34:49 -07:00
|
|
|
if ( o.scriptPubKey && o.scriptPubKey.addresses &&
|
2014-05-23 17:23:44 -07:00
|
|
|
o.scriptPubKey.addresses[0] && !o.scriptPubKey.addresses[1] // TODO : not supported=> standard multisig
|
|
|
|
) {
|
|
|
|
var addr = o.scriptPubKey.addresses[0];
|
2014-05-25 13:34:49 -07:00
|
|
|
var sat = o.valueSat || ((o.value||0) * util.COIN).toFixed(0);
|
2014-05-23 17:23:44 -07:00
|
|
|
|
|
|
|
relatedAddrs[addr]=1;
|
|
|
|
var k = OUTS_PREFIX + txid + '-' + o.n;
|
|
|
|
dbScript.push({
|
|
|
|
type: 'put',
|
|
|
|
key: k,
|
|
|
|
value: addr + ':' + sat,
|
|
|
|
},{
|
|
|
|
type: 'put',
|
|
|
|
key: ADDR_PREFIX + addr + '-' + txid + '-' + o.n,
|
|
|
|
value: sat + ':' + ts,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.relatedAddrs=relatedAddrs;
|
|
|
|
return dbScript;
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-02-08 05:57:37 -08:00
|
|
|
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
TransactionDb.prototype.add = function(tx, blockhash, cb) {
|
|
|
|
var dbScript = this._addScript(tx, blockhash);
|
|
|
|
db.batch(dbScript, cb);
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-02-06 05:20:49 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
TransactionDb.prototype._addManyFromObjs = function(txs, next) {
|
|
|
|
var dbScript = [];
|
|
|
|
for(var ii in txs){
|
|
|
|
var s = this._addScript(txs[ii]);
|
|
|
|
dbScript = dbScript.concat(s);
|
|
|
|
}
|
|
|
|
db.batch(dbScript, next);
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
TransactionDb.prototype._addManyFromHashes = function(txs, next) {
|
|
|
|
var self=this;
|
|
|
|
var dbScript = [];
|
|
|
|
async.eachLimit(txs, CONCURRENCY, function(tx, each_cb) {
|
|
|
|
if (tx === genesisTXID)
|
|
|
|
return each_cb();
|
|
|
|
|
|
|
|
Rpc.getTxInfo(tx, function(err, inInfo) {
|
|
|
|
if (!inInfo) return each_cb(err);
|
|
|
|
dbScript = dbScript.concat(self._addScript(inInfo));
|
|
|
|
return each_cb();
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err) {
|
2014-05-23 17:23:44 -07:00
|
|
|
if (err) return next(err);
|
|
|
|
db.batch(dbScript,next);
|
2014-03-05 18:03:56 -08:00
|
|
|
});
|
|
|
|
};
|
2014-02-03 18:30:46 -08:00
|
|
|
|
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
TransactionDb.prototype.addMany = function(txs, next) {
|
|
|
|
if (!txs) return next();
|
2014-02-03 18:30:46 -08:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
var fn = (typeof txs[0] ==='string') ?
|
|
|
|
this._addManyFromHashes : this._addManyFromObjs;
|
2014-05-21 11:51:24 -07:00
|
|
|
|
2014-05-23 17:23:44 -07:00
|
|
|
return fn.apply(this,[txs, next]);
|
2014-05-21 11:51:24 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-25 13:34:49 -07:00
|
|
|
TransactionDb.prototype.getPoolInfo = function(txid, cb) {
|
2014-05-21 11:51:24 -07:00
|
|
|
var self = this;
|
2014-05-25 13:34:49 -07:00
|
|
|
|
|
|
|
Rpc.getTxInfo(txid, function(err, txInfo) {
|
|
|
|
if (err) return cb(false);
|
|
|
|
var ret;
|
|
|
|
|
|
|
|
if (txInfo && txInfo.isCoinBase)
|
|
|
|
ret = self.poolMatch.match(new Buffer(txInfo.vin[0].coinbase, 'hex'));
|
|
|
|
|
|
|
|
return cb(ret);
|
2014-05-21 11:51:24 -07:00
|
|
|
});
|
2014-03-05 18:03:56 -08:00
|
|
|
};
|
2014-02-03 22:22:58 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
module.exports = require('soop')(TransactionDb);
|