Merge pull request #40 from eordano/blockConfirmation

Block and Transaction confirmation
This commit is contained in:
Manuel Aráoz 2015-03-13 17:22:42 -03:00
commit 0cd5323261
7 changed files with 453 additions and 1197 deletions

View File

@ -1,466 +0,0 @@
'use strict';
var imports = require('soop').imports();
var TIMESTAMP_PREFIX = 'bts-'; // bts-<ts> => <hash>
var PREV_PREFIX = 'bpr-'; // bpr-<hash> => <prev_hash>
var NEXT_PREFIX = 'bne-'; // bne-<hash> => <next_hash>
var MAIN_PREFIX = 'bma-'; // bma-<hash> => <height> (0 is unconnected)
var TIP = 'bti-'; // bti = <hash>:<height> last block on the chain
var LAST_FILE_INDEX = 'file-'; // last processed file index
// txid - blockhash mapping (only for confirmed txs, ONLY FOR BEST BRANCH CHAIN)
var IN_BLK_PREFIX = 'btx-'; //btx-<txid> = <block>
var MAX_OPEN_FILES = 500;
var CONCURRENCY = 5;
var DFLT_REQUIRED_CONFIRMATIONS = 1;
/**
* Module dependencies.
*/
var levelup = require('levelup'),
config = require('../config/config');
var db = imports.db || levelup(config.leveldb + '/blocks',{maxOpenFiles: MAX_OPEN_FILES} );
var Rpc = imports.rpc || require('./Rpc');
var async = require('async');
var logger = require('./logger').logger;
var info = logger.info;
var BlockDb = function(opts) {
this.txDb = require('./TransactionDb').default();
this.safeConfirmations = config.safeConfirmations || DEFAULT_SAFE_CONFIRMATIONS;
BlockDb.super(this, arguments);
};
BlockDb.prototype.close = function(cb) {
db.close(cb);
};
BlockDb.prototype.drop = function(cb) {
var path = config.leveldb + '/blocks';
db.close(function() {
require('leveldown').destroy(path, function () {
db = levelup(path,{maxOpenFiles: MAX_OPEN_FILES} );
return cb();
});
});
};
BlockDb.prototype._addBlockScript = function(b, height) {
var time_key = TIMESTAMP_PREFIX +
( b.time || Math.round(new Date().getTime() / 1000) );
return [
{
type: 'put',
key: time_key,
value: b.hash,
},
{
type: 'put',
key: MAIN_PREFIX + b.hash,
value: height,
},
{
type: 'put',
key:PREV_PREFIX + b.hash,
value: b.previousblockhash,
},
];
};
BlockDb.prototype._delTxsScript = function(txs) {
var dbScript =[];
for(var ii in txs){
dbScript.push({
type: 'del',
key: IN_BLK_PREFIX + txs[ii],
});
}
return dbScript;
};
BlockDb.prototype._addTxsScript = function(txs, hash, height) {
var dbScript =[];
for(var ii in txs){
dbScript.push({
type: 'put',
key: IN_BLK_PREFIX + txs[ii],
value: hash+':'+height,
});
}
return dbScript;
};
// Returns blockHash and height for a given txId (If the tx is on the MAIN chain).
BlockDb.prototype.getBlockForTx = function(txId, cb) {
db.get(IN_BLK_PREFIX + txId,function (err, val) {
if (err && err.notFound) return cb();
if (err) return cb(err);
var v = val.split(':');
return cb(err,v[0],parseInt(v[1]));
});
};
BlockDb.prototype._changeBlockHeight = function(hash, height, cb) {
var self = this;
var dbScript1 = this._setHeightScript(hash,height);
logger.log('Getting TXS FROM %s to set it Main', hash);
this.fromHashWithInfo(hash, function(err, bi) {
if (!bi || !bi.info || !bi.info.tx)
throw new Error('unable to get info for block:'+ hash);
var dbScript2;
if (height>=0) {
dbScript2 = self._addTxsScript(bi.info.tx, hash, height);
logger.info('\t%s %d Txs', 'Confirming', bi.info.tx.length);
} else {
dbScript2 = self._delTxsScript(bi.info.tx);
logger.info('\t%s %d Txs', 'Unconfirming', bi.info.tx.length);
}
db.batch(dbScript2.concat(dbScript1),cb);
});
};
BlockDb.prototype.setBlockMain = function(hash, height, cb) {
this._changeBlockHeight(hash,height,cb);
};
BlockDb.prototype.setBlockNotMain = function(hash, cb) {
this._changeBlockHeight(hash,-1,cb);
};
// adds a block (and its txs). Does not update Next pointer in
// the block prev to the new block, nor TIP pointer
//
BlockDb.prototype.add = function(b, height, cb) {
var txs = typeof b.tx[0] === 'string' ? b.tx : b.tx.map( function(o){ return o.txid; });
var dbScript = this._addBlockScript(b,height);
dbScript = dbScript.concat(this._addTxsScript(txs, b.hash, height));
this.txDb.addMany(b.tx, function(err) {
if (err) return cb(err);
db.batch(dbScript, cb);
});
};
BlockDb.prototype.getTip = function(cb) {
if (this.cachedTip){
var v = this.cachedTip.split(':');
return cb(null,v[0], parseInt(v[1]));
}
var self = this;
db.get(TIP, function(err, val) {
if (!val) return cb();
self.cachedTip = val;
var v = val.split(':');
return cb(err,v[0], parseInt(v[1]));
});
};
BlockDb.prototype.setTip = function(hash, height, cb) {
this.cachedTip = hash + ':' + height;
db.put(TIP, this.cachedTip, function(err) {
return cb(err);
});
};
BlockDb.prototype.getDepth = function(hash, cb) {
var v = this.cachedTip.split(':');
if (!v) throw new Error('getDepth called with not cachedTip');
this.getHeight(hash, function(err,h){
return cb(err,parseInt(v[1]) - h);
});
};
//mainly for testing
BlockDb.prototype.setPrev = function(hash, prevHash, cb) {
db.put(PREV_PREFIX + hash, prevHash, function(err) {
return cb(err);
});
};
BlockDb.prototype.getPrev = function(hash, cb) {
db.get(PREV_PREFIX + hash, function(err,val) {
if (err && err.notFound) { err = null; val = null;}
return cb(err,val);
});
};
BlockDb.prototype.setLastFileIndex = function(idx, cb) {
var self = this;
if (this.lastFileIndexSaved === idx) return cb();
db.put(LAST_FILE_INDEX, idx, function(err) {
self.lastFileIndexSaved = idx;
return cb(err);
});
};
BlockDb.prototype.getLastFileIndex = function(cb) {
db.get(LAST_FILE_INDEX, function(err,val) {
if (err && err.notFound) { err = null; val = null;}
return cb(err,val);
});
};
BlockDb.prototype.getNext = function(hash, cb) {
db.get(NEXT_PREFIX + hash, function(err,val) {
if (err && err.notFound) { err = null; val = null;}
return cb(err,val);
});
};
BlockDb.prototype.getHeight = function(hash, cb) {
db.get(MAIN_PREFIX + hash, function(err, val) {
if (err && err.notFound) { err = null; val = 0;}
return cb(err,parseInt(val));
});
};
BlockDb.prototype._setHeightScript = function(hash, height) {
logger.log('setHeight: %s #%d', hash,height);
return ([{
type: 'put',
key: MAIN_PREFIX + hash,
value: height,
}]);
};
BlockDb.prototype.setNext = function(hash, nextHash, cb) {
db.put(NEXT_PREFIX + hash, nextHash, function(err) {
return cb(err);
});
};
// Unused
BlockDb.prototype.countConnected = function(cb) {
var c = 0;
console.log('Counting connected blocks. This could take some minutes');
db.createReadStream({start: MAIN_PREFIX, end: MAIN_PREFIX + '~' })
.on('data', function (data) {
if (data.value !== 0) c++;
})
.on('error', function (err) {
return cb(err);
})
.on('end', function () {
return cb(null, c);
});
};
// .has() return true orphans also
BlockDb.prototype.has = function(hash, cb) {
var k = PREV_PREFIX + hash;
db.get(k, function (err) {
var ret = true;
if (err && err.notFound) {
err = null;
ret = false;
}
return cb(err, ret);
});
};
BlockDb.prototype.fromHashWithInfo = function(hash, cb) {
var self = this;
Rpc.getBlock(hash, function(err, info) {
if (err || !info) return cb(err);
//TODO can we get this from RPC .height?
self.getHeight(hash, function(err, height) {
if (err) return cb(err);
info.isMainChain = height>=0 ? true : false;
return cb(null, {
hash: hash,
info: info,
});
});
});
};
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, limit, cb) {
var list = [];
var opts = {
start: TIMESTAMP_PREFIX + end_ts, //Inverted since list is reversed
end: TIMESTAMP_PREFIX + start_ts,
limit: limit,
reverse: 1,
};
db.createReadStream(opts)
.on('data', function (data) {
var k = data.key.split('-');
list.push({
ts: k[1],
hash: data.value,
});
})
.on('error', function (err) {
return cb(err);
})
.on('end', function () {
return cb(null, list.reverse());
});
};
BlockDb.prototype.blockIndex = function(height, cb) {
return Rpc.blockIndex(height,cb);
};
BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) {
var self = this;
if (!o.spentTxId) return cb();
if (o.multipleSpentAttempts) {
async.eachLimit(o.multipleSpentAttempts, CONCURRENCY,
function(oi, e_c) {
// Only one will be confirmed
self.getBlockForTx(oi.txid, function(err, hash, height) {
if (err) return;
if (height>=0) {
o.spentTxId = oi.txid;
o.index = oi.index;
o.spentIsConfirmed = chainHeight >= height;
o.spentConfirmations = chainHeight - height +1;
}
return e_c();
});
}, cb);
} else {
self.getBlockForTx(o.spentTxId, function(err, hash, height) {
if (err) return cb(err);
if (height >=0 ) {
o.spentIsConfirmed = chainHeight >= height;
o.spentConfirmations = chainHeight - height +1;
}
return cb();
});
}
};
BlockDb.prototype._fillConfirmationsOne = function(o, chainHeight, cb) {
var self = this;
self.getBlockForTx(o.txid, function(err, hash, height) {
if (err) return cb(err);
if (height>=0) {
o.isConfirmed = chainHeight >= height;
o.confirmations = chainHeight - height +1;
return self._fillConfirmationsOneSpent(o,chainHeight,cb);
}
else return cb();
});
};
BlockDb.prototype.fillConfirmations = function(txouts, cb) {
var self = this;
this.getTip(function(err, hash, height){
var txs = txouts.filter(function(x){
return !x.spentIsConfirmedCached // not 100%cached
&& !(x.isConfirmedCached && !x.spentTxId); // and not partial cached but not spent
});
//console.log('[BlockDb.js.373:txs:]',txs.length, txs.slice(0,5)); //TODO
async.eachLimit(txs, CONCURRENCY, function(txout, e_c) {
if(txout.isConfirmedCached) {
self._fillConfirmationsOneSpent(txout,height, e_c);
} else {
self._fillConfirmationsOne(txout,height, e_c);
}
}, cb);
});
};
/* this is only for migration scripts */
BlockDb.prototype._runScript = function(script, cb) {
db.batch(script,cb);
};
BlockDb.prototype.migrateV02 = function(cb) {
var k = 'txb-';
var dbScript = [];
var c=0;
var c2=0;
var N=50000;
this.txDb._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: IN_BLK_PREFIX + k[1],
value: data.value,
});
if (c++>N) {
console.log('\t%dM txs processed', ((c2+=N)/1e6).toFixed(3));
db.batch(dbScript,function () {
c=0;
dbScript=[];
});
}
})
.on('error', function(err) {
return cb(err);
})
.on('end', function (){
return cb();
});
};
BlockDb.prototype.migrateV02cleanup = function(cb) {
var self = this;
console.log('## deleting txb- from txs db'); //todo
var k = 'txb-';
var d = this.txDb._db;
d.createReadStream({
start: k,
end: k + '~'
})
.pipe(d.createWriteStream({type:'del'}))
.on('close', function(err){
if (err) return cb(err);
console.log('## deleting tx- from txs db'); //todo
var k = 'tx-';
var d = self.txDb._db;
d.createReadStream({
start: k,
end: k + '~'
})
.pipe(d.createWriteStream({type:'del'}))
.on('close', function(err){
if (err) return cb(err);
var k = 'txa-';
var d = self.txDb._db;
d.createReadStream({
start: k,
end: k + '~'
})
.pipe(d.createWriteStream({type:'del'}))
.on('close', cb);
});
});
};
module.exports = require('soop')(BlockDb);

View File

@ -1,729 +0,0 @@
var OUTS_PREFIX = 'txo-'; //txo-<txid>-<n> => [addr, btc_sat]
var SPENT_PREFIX = 'txs-'; //txs-<txid(out)>-<n(out)>-<txid(in)>-<n(in)> = ts
// to sum up addr balance (only outs, spents are gotten later)
var ADDR_PREFIX = 'txa2-'; //txa-<addr>-<tsr>-<txid>-<n>
// tsr = 1e13-js_timestamp
// => + btc_sat [:isConfirmed:[scriptPubKey|isSpendConfirmed:SpentTxid:SpentVout:SpentTs]
// |balance:txApperances
// TODO: use bitcore networks module
var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
var CONCURRENCY = 10;
var DEFAULT_SAFE_CONFIRMATIONS = 6;
var MAX_OPEN_FILES = 500;
var END_OF_WORLD_TS = 1e13;
// var CONFIRMATION_NR_TO_NOT_CHECK = 10; //Spend
/**
* Module dependencies.
*/
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'),
assert = require('assert'),
Script = bitcore.Script,
bitcoreUtil = bitcore.util,
buffertools = require('buffertools');
var logger = require('./logger').logger;
var db = imports.db || levelup(config.leveldb + '/txs', {
maxOpenFiles: MAX_OPEN_FILES
});
var PoolMatch = imports.poolMatch || require('soop').load('./PoolMatch', config);
var Address = require('bitcore').Address;
var TransactionDb = function() {
TransactionDb.super(this, arguments);
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
this.poolMatch = new PoolMatch();
this.safeConfirmations = config.safeConfirmations || DEFAULT_SAFE_CONFIRMATIONS;
this._db = db; // this is only exposed for migration script
};
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();
});
});
};
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),
});
} 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]),
});
idx[parseInt(k[2])] = i++;
})
.on('error', function(err) {
return cb(err);
})
.on('end', function() {
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])];
assert(typeof j !== 'undefined', 'Spent could not be stored: tx ' + txid +
'spent in TX:' + k[1] + ',' + k[2] + ' j:' + j);
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);
});
});
};
TransactionDb.prototype._fillSpent = function(info, cb) {
var self = this;
if (!info) return cb();
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);
})
.on('error', function(err) {
return cb(err);
})
.on('end', function(err) {
return cb(err);
});
};
TransactionDb.prototype._fillOutpoints = function(txInfo, cb) {
var self = this;
if (!txInfo || txInfo.isCoinBase) return cb();
var valueIn = 0;
var incompleteInputs = 0;
async.eachLimit(txInfo.vin, CONCURRENCY, function(i, c_in) {
self.fromTxIdN(i.txid, i.vout, function(err, ret) {
if (!ret || !ret.addr || !ret.valueSat) {
logger.info('Could not get TXouts in %s,%d from %s ', i.txid, i.vout, txInfo.txid);
if (ret) i.unconfirmedInput = ret.unconfirmedInput;
incompleteInputs = 1;
return c_in(); // error not scalated
}
txInfo.firstSeenTs = ret.ts;
i.unconfirmedInput = i.unconfirmedInput;
i.addr = ret.addr;
i.valueSat = ret.valueSat;
i.value = ret.valueSat / util.COIN;
valueIn += i.valueSat;
if (ret.multipleSpentAttempt || !ret.spentTxId ||
(ret.spentTxId && ret.spentTxId !== txInfo.txid)
) {
if (ret.multipleSpentAttempts) {
ret.multipleSpentAttempts.forEach(function(mul) {
if (mul.spentTxId !== txInfo.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;
}
return c_in();
});
},
function() {
if (!incompleteInputs) {
txInfo.valueIn = valueIn / util.COIN;
txInfo.fees = (valueIn - (txInfo.valueOut * util.COIN)).toFixed(0) / util.COIN;
} else {
txInfo.incompleteInputs = 1;
}
return cb();
});
};
TransactionDb.prototype._getInfo = function(txid, next) {
var self = this;
Rpc.getTxInfo(txid, function(err, txInfo) {
if (err) return next(err);
self._fillOutpoints(txInfo, function() {
self._fillSpent(txInfo, function() {
return next(null, txInfo);
});
});
});
};
// 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);
});
};
TransactionDb.prototype.fromIdWithInfo = function(txid, cb) {
var self = this;
self._getInfo(txid, function(err, info) {
if (err) return cb(err);
if (!info) return cb();
return cb(err, {
txid: txid,
info: info
});
});
};
// Gets address info from an outpoint
TransactionDb.prototype.fromTxIdN = function(txid, n, cb) {
var self = this;
var k = OUTS_PREFIX + txid + '-' + n;
db.get(k, function(err, val) {
var ret;
if (!val || (err && err.notFound)) {
err = null;
ret = {
unconfirmedInput: 1
};
} 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);
});
});
};
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[0],
});
})
.on('error', function(err) {
return cb(err);
})
.on('end', function() {
db.batch(dbScript, cb);
});
};
TransactionDb.prototype.cacheConfirmations = function(txouts, cb) {
var self = this;
var dbScript = [];
for (var ii in txouts) {
var txout = txouts[ii];
//everything already cached?
if (txout.spentIsConfirmedCached) {
continue;
}
var infoToCache = [];
if (txout.confirmations >= self.safeConfirmations) {
if (txout.spentConfirmations >= self.safeConfirmations) {
// if spent, we overwrite scriptPubKey cache (not needed anymore)
// First 1 = txout.isConfirmedCached (must be equal to 1 at this point)
infoToCache = [1, 1, txout.spentTxId, txout.spentIndex, txout.spentTs];
} else {
if (!txout.isConfirmedCached) {
infoToCache.push(1);
txout.confirmedWillBeCached = 1;
}
}
//console.log('[TransactionDb.js.352:infoToCache:]',infoToCache); //TODO
if (infoToCache.length) {
infoToCache.unshift(txout.value_sat);
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) {
// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO
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.isConfirmedCached || txout.confirmedWillBeCached) ? 1 : 0, txout.scriptPubKey];
dbScript.push({
type: 'put',
key: txout.key,
value: infoToCache.join(':'),
});
}
}
db.batch(dbScript, cb);
};
TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
var v = data.value.split(':');
// console.log('[TransactionDb.js.375]',data.key,data.value);
var item = {
key: data.key,
ts: END_OF_WORLD_TS - parseInt(k[2]),
txid: k[3],
index: parseInt(k[4]),
value_sat: parseInt(v[0]),
};
if (ignoreCache)
return item;
// Cache:
// v[1]== isConfirmedCached
// v[2]=== '1' -> is SpendCached -> [4]=spendTxId [5]=spentIndex [6]=spendTs
// v[2]!== '1' -> is ScriptPubkey -> [[2] = scriptPubkey
if (v[1] === '1') {
item.isConfirmed = 1;
item.isConfirmedCached = 1;
// console.log('[TransactionDb.js.356] CACHE HIT CONF:', item.key);
// Sent, confirmed
if (v[2] === '1') {
// console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key);
item.spentIsConfirmed = 1;
item.spentIsConfirmedCached = 1;
item.spentTxId = v[3];
item.spentIndex = parseInt(v[4]);
item.spentTs = parseInt(v[5]);
}
// Scriptpubkey cached
else if (v[2]) {
item.scriptPubKey = v[2];
item.scriptPubKeyCached = 1;
// console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key, v, item.scriptPubKey);
}
}
return item;
};
TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
opts = opts || {};
var self = this;
var k = ADDR_PREFIX + addr + '-';
var ret = [];
var unique = {};
db.createReadStream({
start: k,
end: k + '~',
limit: opts.txLimit > 0 ? opts.txLimit : -1, // -1 means not limit
})
.on('data', function(data) {
var k = data.key.split('-');
var index = k[3] + k[4];
if (!unique[index]) {
unique[index] = 1;
ret.push(self._parseAddrData(k, data, opts.ignoreCache));
}
})
.on('error', cb)
.on('end', function() {
async.eachLimit(ret.filter(function(x) {
return !x.spentIsConfirmed;
}), CONCURRENCY, function(o, e_c) {
var k = SPENT_PREFIX + o.txid + '-' + o.index + '-';
db.createReadStream({
start: k,
end: k + '~'
})
.on('data', function(data) {
var k = data.key.split('-');
self._addSpentInfo(o, k[3], k[4], data.value);
})
.on('error', e_c)
.on('end', e_c);
},
function(err) {
return cb(err, ret);
});
});
};
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;
};
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);
txout.scriptPubKey = info.vout[txout.index].scriptPubKey.hex;
return a_c();
});
}, function() {
self.cacheScriptPubKey(txouts, cb);
});
};
TransactionDb.prototype.removeFromTxId = function(txid, cb) {
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);
});
};
// relatedAddrs is an optional hash, to collect related addresses in the transaction
TransactionDb.prototype._addScript = function(tx, relatedAddrs) {
var dbScript = [];
var ts = tx.time;
var txid = tx.txid || tx.hash;
// var u=require('util');
// console.log('[TransactionDb.js.518]', u.inspect(tx,{depth:10})); //TODO
// Input Outpoints (mark them as spent)
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;
dbScript.push({
type: 'put',
key: k,
value: ts || 0,
});
}
}
for (var ii in tx.vout) {
var o = tx.vout[ii];
if (o.scriptPubKey && o.scriptPubKey.addresses &&
o.scriptPubKey.addresses[0] && !o.scriptPubKey.addresses[1] // TODO : not supported=> standard multisig
) {
var addr = o.scriptPubKey.addresses[0];
var sat = o.valueSat || ((o.value || 0) * util.COIN).toFixed(0);
if (relatedAddrs) relatedAddrs[addr] = 1;
var k = OUTS_PREFIX + txid + '-' + o.n;
var tsr = END_OF_WORLD_TS - ts;
dbScript.push({
type: 'put',
key: k,
value: addr + ':' + sat,
}, {
type: 'put',
key: ADDR_PREFIX + addr + '-' + tsr + '-' + txid + '-' + o.n,
value: sat,
});
}
}
return dbScript;
};
// 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);
});
};
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);
};
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();
});
},
function(err) {
if (err) return next(err);
db.batch(dbScript, next);
});
};
TransactionDb.prototype.addMany = function(txs, next) {
if (!txs) return next();
var fn = (typeof txs[0] === 'string') ?
this._addManyFromHashes : this._addManyFromObjs;
return fn.apply(this, [txs, next]);
};
TransactionDb.prototype.getPoolInfo = function(txid, cb) {
var self = this;
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);
});
};
TransactionDb.prototype.checkVersion02 = function(cb) {
var k = 'txa-';
var isV2 = 1;
db.createReadStream({
start: k,
end: k + '~',
limit: 1,
})
.on('data', function(data) {
isV2 = 0;
})
.on('end', function() {
return cb(isV2);
});
};
TransactionDb.prototype.migrateV02 = function(cb) {
var k = 'txa-';
var dbScript = [];
var c = 0;
var c2 = 0;
var N = 50000;
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[2] + '-' + k[3],
value: v[0],
});
if (c++ > N) {
console.log('\t%dM txs outs processed', ((c2 += N) / 1e6).toFixed(3)); //TODO
db.batch(dbScript, function() {
c = 0;
dbScript = [];
});
}
})
.on('error', function(err) {
return cb(err);
})
.on('end', function() {
return cb();
});
};
module.exports = require('soop')(TransactionDb);

View File

@ -1,6 +1,7 @@
'use strict';
var LevelUp = require('levelup');
var LevelLock = require('level-lock');
var Promise = require('bluebird');
var RPC = require('bitcoind-rpc');
var TransactionService = require('./transaction');
@ -13,7 +14,42 @@ var $ = bitcore.util.preconditions;
var JSUtil = bitcore.util.js;
var _ = bitcore.deps._;
var LATEST_BLOCK = 'latest-block';
var LOCK = 'lock-';
var NULLBLOCKHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
var GENESISPARENT = {
height: -1,
prevBlockHash: NULLBLOCKHASH
};
var helper = function(index) {
return function(maybeHash) {
if (_.isString(maybeHash)) {
return index + maybeHash;
} else if (bitcore.util.buffer.isBuffer(maybeHash)) {
return index + maybeHash.toString('hex');
} else if (maybeHash instanceof bitcore.Block) {
return index + maybeHash.id;
} else {
throw new bitcore.errors.InvalidArgument();
}
};
};
var Index = {
timestamp: 'bts-', // bts-<timestamp> -> hash for the block that was mined at this TS
prev: 'prev-', // prev-<hash> -> parent hash
next: 'nxt-', // nxt-<hash> -> hash for the next block in the main chain that is a child
height: 'bh-', // bh-<hash> -> height (-1 means disconnected)
tip: 'tip' // tip -> { hash: hex, height: int }, the latest tip
};
_.extend(Index, {
getNextBlock: helper(Index.next),
getPreviousBlock: helper(Index.prev),
getBlockHeight: helper(Index.height),
getBlockByTs: function(block) {
return Index.timestamp + block.header.time;
}
});
function BlockService (opts) {
opts = _.extend({}, opts);
@ -25,6 +61,29 @@ function BlockService (opts) {
});
}
BlockService.prototype.writeLock = function() {
var self = this;
return Promise.try(function() {
// TODO
});
};
/**
* Transforms data as received from an RPC result structure for `getblock`,
* plus a list of transactions, and build a block based on thosė.
*
* @param {Object} blockData
* @param {Number} blockData.version a 32 bit number with the version of the block
* @param {string} blockData.previousblockhash a string of length 64 with the hexa encoding of the
* hash for the previous block
* @param {Number} blockData.time a 32 bit number with the timestamp when this block was created
* @param {Number} blockData.nonce a 32 bit number with a random number
* @param {string} blockData.bits a 32 bit "varint" encoded number with the length of the block
* @param {string} blockData.merkleRoot an hex string of length 64 with the hash of the block
* @param {Array} transactions an array of bitcore.Transaction objects, in the order that forms the
* merkle root hash
* @return {bitcore.Block}
*/
BlockService.blockRPCtoBitcore = function(blockData, transactions) {
$.checkArgument(_.all(transactions, function(transaction) {
return transaction instanceof bitcore.Transaction;
@ -48,11 +107,23 @@ BlockService.blockRPCtoBitcore = function(blockData, transactions) {
});
};
/**
* A helper function to return an error when a block couldn't be found
*
* @param {*} err
* @return {Promise} a promise that will always be rejected
*/
var blockNotFound = function(err) {
console.log(err);
return Promise.reject(new BitcoreNode.errors.Blocks.NotFound());
};
/**
* Fetch a block using the hash of that block
*
* @param {string} blockHash the hash of the block to be fetched
* @return {Promise<Block>}
*/
BlockService.prototype.getBlock = function(blockHash) {
$.checkArgument(JSUtil.isHexa(blockHash), 'Block hash must be hexa');
@ -78,6 +149,12 @@ BlockService.prototype.getBlock = function(blockHash) {
}).catch(blockNotFound);
};
/**
* Fetch the block that is currently taking part of the main chain at a given height
*
* @param {Number} height the height of the block
* @return {Promise<Block>}
*/
BlockService.prototype.getBlockByHeight = function(height) {
$.checkArgument(_.isNumber(height), 'Block height must be a number');
@ -94,13 +171,18 @@ BlockService.prototype.getBlockByHeight = function(height) {
}).catch(blockNotFound);
};
/**
* Fetch the block that is currently the tip of the blockchain
*
* @return {Promise<Block>}
*/
BlockService.prototype.getLatest = function() {
var self = this;
return Promise.try(function() {
return self.database.getAsync(LATEST_BLOCK);
return self.database.getAsync(Index.tip);
}).then(function(blockHash) {
@ -109,4 +191,136 @@ BlockService.prototype.getLatest = function() {
}).catch(blockNotFound);
};
/**
* Set a block as the current tip of the blockchain
*
* @param {bitcore.Block} block
* @return {Promise<Block>} a promise of the same block, for chaining
*/
BlockService.prototype._confirmBlock = function(block) {
$.checkArgument(block instanceof bitcore.Block);
var self = this;
var ops = [];
this.writeLock().then(
self._setNextBlock.bind(self, ops, block.header.prevHash, block)
).then(function() {
if (block.header.prevHash.toString('hex') !== NULLBLOCKHASH) {
return self.getBlock(block.header.prevHash);
} else {
return GENESISPARENT;
}
}).then(function(parent) {
return self._setBlockHeight(ops, block, parent.height + 1);
}).then(
self._setBlockByTs.bind(self, ops, block)
).then(function() {
return Promise.all(block.transactions.map(function(transaction) {
return self.transactionService._confirmTransaction(ops, block, transaction);
}));
}).then(
self.database.batchAsync.bind(self, ops)
).then(this.unlock);
};
BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {
return Promise.try(function() {
ops.push({
type: 'put',
key: Index.getNextBlock(prevBlockHash),
value: block.hash
});
ops.push({
type: 'put',
key: Index.getPreviousBlock(block.hash),
value: prevBlockHash.toString('hex')
});
});
};
BlockService.prototype._setBlockHeight = function(ops, block, height) {
return Promise.try(function() {
ops.push({
type: 'put',
key: Index.getBlockHeight(block),
value: height
});
return ops;
});
};
BlockService.prototype._setBlockByTs = function(ops, block) {
var self = this;
var key = Index.timestamp + block.time;
return Promise.try(function() {
return self.database.getAsync(key);
}).then(function(result) {
if (result === block.hash) {
return Promise.resolve();
} else {
// TODO: Retry or provide a strategy to insert correctly the block
throw new Error('Found blocks that have same timestamp');
}
}).error(function(err) {
// TODO: Check if err is not found
return ops.push({
type: 'put',
key: Index.getBlockByTs(block),
value: block.hash
});
});
};
/**
* Retrieve the block hash that forms part of the current main chain that confirmed a given
* transaction.
*
* @param {bitcore.Transaction} transaction
* @return {Promise<string>} a promise of the hash of the block
*/
BlockService.prototype.getBlockHashForTransaction = function(transaction) {
return this.database.getAsync(Index.getBlockForTransaction(transaction))
.error(function(error) {
// TODO: Handle error
});
};
/**
* Retrieve the block that forms part of the current main chain that confirmed a given transaction.
*
* @param {bitcore.Transaction} transaction
* @return {Promise<Block>} a promise of a block
*/
BlockService.prototype.getBlockForTransaction = function(transaction) {
if (transaction instanceof bitcore.Transaction) {
transaction = transaction.id;
}
$.checkArgument(_.isString(transaction));
var self = this;
return self.getBlockHashForTransaction(transaction).then(function(hash) {
return self.getBlock(hash);
});
};
module.exports = BlockService;

View File

@ -24,6 +24,55 @@ var config = require('config');
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var NULLTXHASH = bitcore.util.buffer.emptyBuffer(32).toString('hex');
var helper = function(name) {
return function(txId, output) {
if (txId instanceof bitcore.Transaction) {
txId = txId.hash;
}
$.checkArgument(_.isString(txId), 'txId must be a string');
$.checkArgument(_.isNumber(output), 'output must be a number');
return name + txId + '-' + output;
};
};
var helperAddress = function(index) {
return function(address) {
if (_.isString(address)) {
address = new bitcore.Address(address);
}
$.checkArgument(address instanceof bitcore.Address, 'address must be a string or bitcore.Address');
return index + address.toString();
};
};
var Index = {
output: 'txo-', // txo-<txid>-<n> -> serialized Output
spent: 'txs-', // txo-<txid>-<n>-<spend txid>-<m> -> block height of confirmation for spend
address: 'txa-', // txa-<address>-<txid>-<n> -> Output
addressSpent: 'txas-', // txa-<address>-<txid>-<n> -> {
// heightSpent: number, (may be -1 for unconfirmed tx)
// spentTx: string, spentTxInputIndex: number, spendInput: Input
// }
transaction: 'btx-' // btx-<txid> -> block in main chain that confirmed the tx
}
_.extend(Index, {
getOutput: helper(Index.output),
getSpentHeight: helper(Index.spent),
getOutputsForAddress: helperAddress(Index.address),
getSpentOutputsForAddress: helperAddress(Index.addressSpent),
getBlockForTransaction: function(transaction) {
if (_.isString(transaction)) {
return Index.transaction + transaction;
} else if (transaction instanceof bitcore.Transaction) {
return Index.transaction + transaction.id;
} else {
throw new bitcore.errors.InvalidArgument(transaction + ' is not a transaction');
}
}
});
function TransactionService (opts) {
opts = _.extend({}, opts);
this.database = opts.database || Promise.promisifyAll(new LevelUp(config.get('LevelUp')));
@ -48,4 +97,78 @@ TransactionService.prototype.getTransaction = function(transactionId) {
});
};
TransactionService.prototype._confirmOutput = function(ops, block, transaction) {
return function(output, index) {
ops.push({
type: 'put',
key: Index.getOutput(transaction.id, index),
value: output.toObject()
});
var address;
// TODO: Move this logic to bitcore
if (output.script.isPublicKeyOut()) {
var hash = bitcore.crypto.Hash.sha256ripemd160(output.script.chunks[0].buf);
address = new bitcore.Address(hash, bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash);
} else if (output.script.isPublicKeyHashOut() || output.script.isScriptHashOut()) {
address = output.script.toAddress();
}
if (address) {
ops.push({
type: 'put',
key: Index.getOutputsForAddress(address),
value: output.toObject()
});
}
};
};
TransactionService.prototype._confirmInput = function(ops, block, transaction) {
return function(input, index) {
if (input.prevTxId.toString('hex') !== NULLTXHASH) {
ops.push({
type: 'put',
key: Index.getOutput(transaction.id, index),
value: _.extend(input.toObject(), {
heightConfirmed: block.height
})
});
var script = input.script;
if (script.isPublicKeyHashIn() || script.isScriptHashIn()) {
// TODO: Move this logic to bitcore
var address = script.isPublicKeyHashIn()
? new PublicKey(script.chunks[0].buf).toAddress()
: new Script(script.chunks[script.chunks.length - 1]).toAddress();
ops.push({
type: 'put',
key: Index.getOutputsForAddress(address),
value: input.toObject()
});
ops.push({
type: 'put',
key: Index.getSpentOutputsForAddress(address),
value: {
heightSpent: block.height,
spentTx: transaction.id,
spentTxInputIndex: index,
spendInput: input.toObject()
}
});
}
}
};
};
TransactionService.prototype._confirmTransaction = function(ops, block, transaction) {
var self = this;
return Promise.try(function() {
ops.push({
type: 'put',
key: Index.getBlockForTransaction(transaction),
value: block.id
});
_.each(transaction.outputs, self._confirmOutput(ops, block, transaction));
_.each(transaction.inputs, self._confirmInput(ops, block, transaction));
});
};
module.exports = TransactionService;

View File

@ -59,6 +59,7 @@
"express": "4.11.1",
"glob": "*",
"js-yaml": "^3.2.7",
"level-lock": "^1.0.1",
"levelup": "~0.19.0",
"moment": "~2.5.0",
"morgan": "^1.5.1",

View File

@ -77,4 +77,67 @@ describe('BlockService', function() {
});
});
describe('block confirmation', function() {
var mockRpc, transactionMock, database, blockService, writeLock;
var thenCaller = {
then: function(arg) {
return arg();
}
};
var genesisBlock = new bitcore.Block(
new Buffer(
'0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a'
+'7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c010'
+'1000000010000000000000000000000000000000000000000000000000000000000000000ffffffff'
+'4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f7'
+'2206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffff'
+'ff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0e'
+'a1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'
, 'hex')
);
beforeEach(function() {
database = sinon.mock();
mockRpc = sinon.mock();
transactionMock = sinon.mock();
blockService = new BlockService({
rpc: mockRpc,
transactionService: transactionMock,
database: database
});
blockService.writeLock = sinon.mock();
blockService.getBlock = sinon.mock();
});
it('makes the expected calls when confirming the genesis block', function(callback) {
database.batchAsync = function(ops) {
ops.should.deep.equal([
{ type: 'put',
key: 'nxt-0000000000000000000000000000000000000000000000000000000000000000',
value: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' },
{ type: 'put',
key: 'prev-000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
value: '0000000000000000000000000000000000000000000000000000000000000000' },
{ type: 'put',
key: 'bh-000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
value: 0 },
{ type: 'put',
key: 'bts-1231006505',
value: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' }
]);
return thenCaller;
};
blockService.unlock = callback;
blockService.writeLock.onFirstCall().returns(thenCaller);
database.getAsync = function() {
return Promise.reject({notFound: true});
};
transactionMock._confirmTransaction = sinon.mock();
blockService._confirmBlock(genesisBlock);
});
});
});

View File

@ -49,4 +49,54 @@ describe('TransactionService', function() {
});
});
});
describe('transaction confirmation', function() {
var database, rpc, service;
beforeEach(function() {
database = sinon.mock();
rpc = sinon.mock();
service = new TransactionService({
rpc: rpc,
database: database
});
});
var genesisBlock = new bitcore.Block(
new Buffer(
'0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a'
+'7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c010'
+'1000000010000000000000000000000000000000000000000000000000000000000000000ffffffff'
+'4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f7'
+'2206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffff'
+'ff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0e'
+'a1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'
, 'hex')
);
genesisBlock.height = 0;
var genesisTx = genesisBlock.transactions[0];
it('confirms correctly the first transaction on genesis block', function(callback) {
var ops = [];
service._confirmTransaction(ops, genesisBlock, genesisTx).then(function() {
ops.should.deep.equal([
{ type: 'put',
key: 'btx-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
value: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' },
{ type: 'put',
key: 'txo-4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b-0',
value:
{ satoshis: 5000000000,
script: '65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG' } },
{ type: 'put',
key: 'txa-1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
value:
{ satoshis: 5000000000,
script: '65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG' } }
]);
callback();
});
});
});
});