new txout-spend- schema

This commit is contained in:
Matias Alejo Garcia 2014-02-10 15:50:07 -03:00
parent 9910fa4d40
commit 8b892edfbc
1 changed files with 24 additions and 16 deletions

View File

@ -16,7 +16,7 @@ function spec(b) {
// to sum up addr balance // to sum up addr balance
var ADDR_PREFIX = 'txouts-addr-'; //txouts-addr-<addr>-<ts>-<txid>-<n> => + btc_sat var ADDR_PREFIX = 'txouts-addr-'; //txouts-addr-<addr>-<ts>-<txid>-<n> => + btc_sat
var SPEND_PREFIX = 'txouts-spend-';//txouts-spend-<txid(out)>-<n(out)> => [txid(in),n(in),ts] var SPEND_PREFIX = 'txouts-spend-';//txouts-spend-<txid(out)>-<n(out)>-<txid(in)>-<n(in)> = ts
// TODO: use bitcore networks module // TODO: use bitcore networks module
var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
@ -97,7 +97,6 @@ function spec(b) {
db.createReadStream({start: k, end: k + '~'}) db.createReadStream({start: k, end: k + '~'})
.on('data', function (data) { .on('data', function (data) {
var k = data.key.split('-'); var k = data.key.split('-');
var v = data.value.split(':');
var j = idx[parseInt(k[3])]; var j = idx[parseInt(k[3])];
assert(typeof j !== 'undefined','Spent could not be stored: tx ' + txid + assert(typeof j !== 'undefined','Spent could not be stored: tx ' + txid +
@ -109,8 +108,8 @@ function spec(b) {
assert(0); assert(0);
} }
else { else {
ret[j].spendTxId = v[0]; ret[j].spendTxId = k[4];
ret[j].spendIndex = parseInt(v[1]); ret[j].spendIndex = parseInt(k[5]);
} }
}) })
.on('error', function (err) { .on('error', function (err) {
@ -236,16 +235,26 @@ function spec(b) {
async.each(ret, function(o, e_c) { async.each(ret, function(o, e_c) {
var k = SPEND_PREFIX + o.txid + '-' + o.index; var k = SPEND_PREFIX + o.txid + '-' + o.index;
db.get(k, function(err, val) { db.createReadStream({start: k, end: k + '~'})
if (err && err.notFound) err=null; .on('data', function (data) {
if (err || !val) return e_c(err); var k = data.key.split('-');
var v = data.value;
var v = val.split(':'); if (o.spendTxId) {
o.spendTxId= v[0]; // double spend!
o.spendIndex=parseInt(v[1]); assert(0);
o.spendTs=parseInt(v[2]); }
return e_c(); else {
}); o.spendTxId = k[4];
o.spendIndex = parseInt(k[5]);
o.spendTs = parseInt(v);
}
})
.on('error', function (err) {
return e_c(err);
})
.on('end', function (err) {
return e_c(err);
});
}, },
function() { function() {
async.each(ret, function(o, e_c){ async.each(ret, function(o, e_c){
@ -341,8 +350,7 @@ function spec(b) {
async.forEachLimit(tx.vin, CONCURRENCY, async.forEachLimit(tx.vin, CONCURRENCY,
function(i, next_out) { function(i, next_out) {
db.batch() db.batch()
.put( SPEND_PREFIX + i.txid + '-' + i.vout , .put( SPEND_PREFIX + i.txid + '-' + i.vout + '-' + tx.txid + '-' + i.n, ts)
tx.txid + ':' + i.n + ':' + ts)
.write(next_out); .write(next_out);
}, },
function (err) { function (err) {