bitcore-node-zcash/lib/TransactionDb.js

728 lines
18 KiB
JavaScript
Raw Normal View History

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-05-28 09:17:08 -07: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-05-28 09:17:08 -07:00
var ADDR_PREFIX = 'txa2-'; //txa-<addr>-<tsr>-<txid>-<n>
// tsr = 1e13-js_timestamp
// => + btc_sat [:isConfirmed:[scriptPubKey|isSpendConfirmed:SpentTxid:SpentVout:SpentTs]
// |balance:txApperances
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-05-28 09:17:08 -07:00
var END_OF_WORLD_TS = 1e13;
// 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-05-27 12:14:44 -07:00
this._db = db; // this is only exposed for migration script
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-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-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) {
2014-05-26 12:58:44 -07:00
self.fromTxIdN(i.txid, i.vout, 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-05-28 09:17:08 -07:00
txInfo.firstSeenTs = ret.ts;
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-05-29 19:37:26 -07:00
if (ret.multipleSpentAttempt || !ret.spentTxId ||
(ret.spentTxId && ret.spentTxId !== info.txid)
) {
if (ret.multipleSpentAttempts) {
ret.multipleSpentAttempts.forEach(function(mul) {
if (mul.spentTxId !== info.txid) {
i.doubleSpentTxID = ret.spentTxId;
i.doubleSpentIndex = ret.spentIndex;
}
});
} else if (!ret.spentTxId) {
i.dbError = 'Input spent not registered';
} else {
i.doubleSpentTxID = ret.spentTxId;
i.doubleSpentIndex = ret.spentIndex;
}
} else {
i.doubleSpentTxID = null;
}
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-03-05 18:03:56 -08:00
TransactionDb.prototype.fromIdWithInfo = function(txid, cb) {
var self = this;
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-05-28 09:17:08 -07:00
// Gets address info from an outpoint
2014-05-26 12:58:44 -07:00
TransactionDb.prototype.fromTxIdN = function(txid, n, cb) {
2014-03-05 18:03:56 -08:00
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) {
2014-05-28 09:17:08 -07:00
var ret;
2014-03-05 18:03:56 -08:00
if (!val || (err && err.notFound)) {
2014-05-28 09:17:08 -07:00
err=null;
ret= { unconfirmedInput: 1 };
2014-03-05 18:03:56 -08:00
}
2014-05-28 09:17:08 -07:00
else {
var a = val.split(':');
ret = {
addr: a[0],
valueSat: parseInt(a[1]),
};
}
// spent?
var k = SPENT_PREFIX + txid + '-' + n + '-';
db.createReadStream({
start: k,
end: k + '~'
})
.on('data', function(data) {
var k = data.key.split('-');
self._addSpentInfo(ret, k[3], k[4], data.value);
})
.on('error', function(error) {
return cb(error);
})
.on('end', function() {
return cb(null, ret);
});
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,
2014-05-28 09:17:08 -07:00
value: v[0],
2014-05-25 13:34:49 -07:00
});
})
.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) {
2014-05-28 04:17:47 -07:00
//console.log('[TransactionDb.js.351:infoToCache:]',infoToCache); //TODO
2014-05-25 13:34:49 -07:00
if (txout.spentConfirmations > self.safeConfirmations) {
2014-05-28 04:17:47 -07:00
// if spent, we overwrite scriptPubKey cache (not needed anymore)
// First 1 = txout.isConfirmedCached (must be equal to 1 at this point)
infoToCache = infoToCache.concat([1, 1, txout.spentTxId, txout.spentIndex, txout.spentTs]);
}
else {
if (!txout.isConfirmedCached) infoToCache.push(1);
2014-05-25 13:34:49 -07:00
}
2014-05-28 04:17:47 -07:00
//console.log('[TransactionDb.js.352:infoToCache:]',infoToCache); //TODO
2014-05-25 13:34:49 -07:00
if (infoToCache.length){
2014-05-28 09:17:08 -07:00
infoToCache.unshift(txout.value_sat);
2014-05-28 04:17:47 -07:00
//console.log('[BlockDb.js.373:txs:]' ,txout.key, infoToCache.join(':')); //TODO
2014-05-25 13:34:49 -07:00
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) {
2014-05-28 04:17:47 -07:00
// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO
2014-05-25 13:34:49 -07:00
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(':');
2014-05-28 09:17:08 -07:00
// console.log('[TransactionDb.js.375]',data.key,data.value); //TODO
2014-05-25 13:34:49 -07:00
var item = {
key: data.key,
2014-05-28 09:17:08 -07:00
ts: parseInt(k[2]),
txid: k[3],
index: parseInt(k[4]),
2014-05-25 13:34:49 -07:00
value_sat: parseInt(v[0]),
};
2014-05-28 04:17:47 -07:00
// Cache:
2014-05-28 09:17:08 -07:00
// v[1]== isConfirmedCached
// v[2]=== '1' -> is SpendCached -> [4]=spendTxId [5]=spentIndex [6]=spendTs
// v[3]!== '1' -> is ScriptPubkey -> [[3] = scriptPubkey
if (v[1]){
2014-05-25 13:34:49 -07:00
item.isConfirmed = 1;
item.isConfirmedCached = 1;
2014-05-28 04:17:47 -07:00
// console.log('[TransactionDb.js.356] CACHE HIT CONF:', item.key); //TODO
2014-05-25 13:34:49 -07:00
// Sent, confirmed
2014-05-28 09:17:08 -07:00
if (v[2] === '1'){
2014-05-28 04:17:47 -07:00
// console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO
2014-05-25 13:34:49 -07:00
item.spentIsConfirmed = 1;
item.spentIsConfirmedCached = 1;
2014-05-28 09:17:08 -07:00
item.spentTxId = v[3];
item.spentIndex = parseInt(v[4]);
item.spentTs = parseInt(v[5]);
2014-05-25 13:34:49 -07:00
}
// Scriptpubkey cached
2014-05-28 09:17:08 -07:00
else if (v[2]) {
2014-05-28 04:17:47 -07:00
// console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key); //TODO
2014-05-28 09:17:08 -07:00
item.scriptPubKey = v[2];
2014-05-25 13:34:49 -07:00
item.scriptPubKeyCached = 1;
}
}
return item;
2014-03-05 18:03:56 -08:00
};
2014-05-28 09:17:08 -07:00
TransactionDb.prototype.fromAddr = function(addr, cb, txLimit) {
2014-03-05 18:03:56 -08:00
var self = this;
var k = ADDR_PREFIX + addr + '-';
var ret = [];
db.createReadStream({
start: k,
2014-05-28 09:17:08 -07:00
end: k + '~',
limit: txLimit>0 ? txLimit: -1, // -1 means not limit
2014-03-05 18:03:56 -08:00
})
.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-28 04:17:47 -07:00
// relatedAddrs is an optional hash, to collect related addresses in the transaction
2014-05-27 20:17:42 -07:00
TransactionDb.prototype._addScript = function(tx, relatedAddrs) {
2014-05-23 17:23:44 -07:00
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
2014-05-27 20:17:42 -07:00
if (relatedAddrs) relatedAddrs[addr]=1;
2014-05-23 17:23:44 -07:00
var k = OUTS_PREFIX + txid + '-' + o.n;
2014-05-28 09:17:08 -07:00
var tsr = END_OF_WORLD_TS - ts;
2014-05-23 17:23:44 -07:00
dbScript.push({
type: 'put',
key: k,
value: addr + ':' + sat,
},{
type: 'put',
2014-05-28 09:17:08 -07:00
key: ADDR_PREFIX + addr + '-' + tsr + '-'+ txid + '-' + o.n,
value: sat,
2014-05-23 17:23:44 -07:00
});
}
}
return dbScript;
2014-03-05 18:03:56 -08:00
};
2014-02-08 05:57:37 -08:00
2014-05-27 20:17:42 -07:00
// adds an unconfimed TX
TransactionDb.prototype.add = function(tx, cb) {
var relatedAddrs = {};
var dbScript = this._addScript(tx, relatedAddrs);
db.batch(dbScript, function(err) { return cb(err,relatedAddrs);});
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);
2014-05-27 20:17:42 -07:00
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-27 20:17:42 -07:00
var fn = (typeof txs[0] ==='string') ?
2014-05-23 17:23:44 -07:00
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-05-26 11:16:38 -07:00
TransactionDb.prototype.checkVersion02 = function(cb) {
2014-05-28 09:17:08 -07:00
var k = 'txa-';
2014-05-29 19:37:26 -07:00
var isV2=1;
2014-05-28 09:17:08 -07:00
db.createReadStream({
start: k,
2014-05-29 19:37:26 -07:00
end: k + '~',
limit: 1,
2014-05-28 09:17:08 -07:00
})
.on('data', function(data) {
2014-05-29 19:37:26 -07:00
isV2=0;
2014-05-28 09:17:08 -07:00
})
.on('end', function (){
2014-05-29 19:37:26 -07:00
return cb(isV2);
2014-05-28 09:17:08 -07:00
});
};
2014-05-26 14:49:03 -07:00
2014-05-28 09:17:08 -07:00
TransactionDb.prototype.migrateV02 = function(cb) {
var k = 'txa-';
var dbScript = [];
2014-05-29 19:37:26 -07:00
var c=0;
var c2=0;
var N=50000;
2014-05-28 09:17:08 -07:00
db.createReadStream({
start: k,
end: k + '~'
})
.on('data', function(data) {
var k = data.key.split('-');
var v = data.value.split(':');
dbScript.push({
type: 'put',
key: ADDR_PREFIX + k[1] + '-' + (END_OF_WORLD_TS - parseInt(v[1])) + '-' + k[3] + '-' + [4],
value: v[0],
});
2014-05-29 19:37:26 -07:00
if (c++>N) {
console.log('\t%dM txs processed', ((c2+=N)/1e6).toFixed(3)); //TODO
db.batch(dbScript,function () {
c=0;
dbScript=[];
});
}
2014-05-28 09:17:08 -07:00
})
.on('error', function(err) {
return cb(err);
})
.on('end', function (){
2014-05-29 19:37:26 -07:00
return cb();
2014-05-28 09:17:08 -07:00
});
2014-05-26 11:16:38 -07:00
};
2014-05-28 09:17:08 -07:00
2014-03-05 18:03:56 -08:00
module.exports = require('soop')(TransactionDb);