query bitcoind for spents and transactions
This commit is contained in:
parent
b8b4ac02bf
commit
c00ca5b23f
113
lib/db.js
113
lib/db.js
@ -29,7 +29,6 @@ function DB(options) {
|
|||||||
util.inherits(DB, BaseDB);
|
util.inherits(DB, BaseDB);
|
||||||
|
|
||||||
DB.PREFIXES = {
|
DB.PREFIXES = {
|
||||||
TX: 'tx',
|
|
||||||
SPENTS: 'sp',
|
SPENTS: 'sp',
|
||||||
OUTPUTS: 'outs'
|
OUTPUTS: 'outs'
|
||||||
};
|
};
|
||||||
@ -53,9 +52,15 @@ DB.prototype.putBlock = function(block, callback) {
|
|||||||
this._updatePrevHashIndex(block, callback);
|
this._updatePrevHashIndex(block, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
||||||
|
this.bitcoind.getTransaction(txid, function(err, txBuffer) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
};*/
|
callback(null, Transaction().fromBuffer(txBuffer));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
DB.prototype.validateBlockData = function(block, callback) {
|
DB.prototype.validateBlockData = function(block, callback) {
|
||||||
// bitcoind does the validation
|
// bitcoind does the validation
|
||||||
@ -216,54 +221,6 @@ DB.prototype._updateOutputs = function(block, addOutput, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype._updateTransactions = function(block, addTransaction, callback) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
DB.super_.prototype._updateTransactions.call(self, block, addTransaction, function(err, operations) {
|
|
||||||
if(err || !addTransaction) {
|
|
||||||
return callback(err, operations);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove transactions from mempool with inputs that were spent
|
|
||||||
var mempoolTransactions = self.mempool.getTransactions();
|
|
||||||
var blockTransactions = self.getTransactionsFromBlock(block);
|
|
||||||
var newMempoolTransactions = [];
|
|
||||||
|
|
||||||
for(var i = 0; i < mempoolTransactions.length; i++) {
|
|
||||||
var txHasInputsInBlock = false;
|
|
||||||
for(var j = 0; j < mempoolTransactions[i].inputs.length; j++) {
|
|
||||||
for(var k = 0; k < blockTransactions.length; k++) {
|
|
||||||
for(var l = 0; l < blockTransactions[k].inputs.length; l++) {
|
|
||||||
var mempoolInput = mempoolTransactions[i].inputs[j];
|
|
||||||
var blockInput = blockTransactions[k].inputs[l];
|
|
||||||
if(mempoolInput.prevTxId === blockInput.prevTxId &&
|
|
||||||
mempoolInput.outputIndex === blockInput.outputIndex) {
|
|
||||||
txHasInputsInBlock = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(txHasInputsInBlock) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(txHasInputsInBlock) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!txHasInputsInBlock) {
|
|
||||||
newMempoolTransactions.push(mempoolTransactions[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.mempool.transactions = newMempoolTransactions;
|
|
||||||
|
|
||||||
callback(null, operations);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
DB.prototype._onChainAddBlock = function(block, callback) {
|
DB.prototype._onChainAddBlock = function(block, callback) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -275,7 +232,6 @@ DB.prototype._onChainAddBlock = function(block, callback) {
|
|||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
this._updateOutputs.bind(this, block, true), // add outputs
|
this._updateOutputs.bind(this, block, true), // add outputs
|
||||||
this._updateTransactions.bind(this, block, true) // add transactions
|
|
||||||
], function(err, results) {
|
], function(err, results) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -302,7 +258,6 @@ DB.prototype._onChainRemoveBlock = function(block, callback) {
|
|||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
this._updateOutputs.bind(this, block, false), // remove outputs
|
this._updateOutputs.bind(this, block, false), // remove outputs
|
||||||
this._updateTransactions.bind(this, block, false) // remove transactions
|
|
||||||
], function(err, results) {
|
], function(err, results) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -390,11 +345,11 @@ DB.prototype.getOutputs = function(address, queryMempool, callback) {
|
|||||||
return callback(error);
|
return callback(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(queryMempool) {
|
/*if(queryMempool) {
|
||||||
var mempoolOutputs = self._getMempoolOutputs(address);
|
var mempoolOutputs = self._getMempoolOutputs(address);
|
||||||
|
|
||||||
outputs = outputs.concat(self._getMempoolOutputs(address));
|
outputs = outputs.concat(self._getMempoolOutputs(address));
|
||||||
}
|
}*/
|
||||||
|
|
||||||
callback(null, outputs);
|
callback(null, outputs);
|
||||||
});
|
});
|
||||||
@ -403,35 +358,6 @@ DB.prototype.getOutputs = function(address, queryMempool, callback) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype._getMempoolOutputs = function(address) {
|
|
||||||
var outputs = [];
|
|
||||||
|
|
||||||
var transactions = this.mempool.getTransactions();
|
|
||||||
transactions.forEach(function(tx) {
|
|
||||||
// add additional info to outputs
|
|
||||||
var outputObjects = [];
|
|
||||||
for(var i = 0; i < tx.outputs.length; i++) {
|
|
||||||
var output = {};
|
|
||||||
output.script = tx.outputs[i].script.toString();
|
|
||||||
output.satoshis = tx.outputs[i].satoshis;
|
|
||||||
output.txid = tx.hash;
|
|
||||||
output.outputIndex = i;
|
|
||||||
output.address = tx.outputs[i].script.toAddress().toString();
|
|
||||||
outputObjects.push(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
var filtered = outputObjects.filter(function(output) {
|
|
||||||
return address.toString() === output.address;
|
|
||||||
});
|
|
||||||
|
|
||||||
if(filtered.length) {
|
|
||||||
outputs = outputs.concat(filtered);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return outputs;
|
|
||||||
};
|
|
||||||
|
|
||||||
DB.prototype.getUnspentOutputs = function(address, queryMempool, callback) {
|
DB.prototype.getUnspentOutputs = function(address, queryMempool, callback) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -460,21 +386,12 @@ DB.prototype.isUnspent = function(output, queryMempool, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype.isSpent = function(output, queryMempool, callback) {
|
DB.prototype.isSpent = function(output, queryMempool, callback) {
|
||||||
if(queryMempool && this._isSpentMempool(output)) {
|
var self = this;
|
||||||
return callback(true);
|
var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid;
|
||||||
}
|
|
||||||
|
|
||||||
this.isSpentDB(output, callback);
|
setImmediate(function() {
|
||||||
};
|
callback(self.bitcoind.isSpent(txid, output.outputIndex));
|
||||||
|
});
|
||||||
DB.prototype.isSpentDB = function(output, callback) {
|
|
||||||
// Query bitcoind
|
|
||||||
return callback(null, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
DB.prototype._isSpentMempool = function(output) {
|
|
||||||
// Query bitcoind
|
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = DB;
|
module.exports = DB;
|
||||||
|
|||||||
@ -111,6 +111,7 @@ Node.prototype._loadDB = function(config) {
|
|||||||
// Other modules can inherit from our DB and replace it with their own
|
// Other modules can inherit from our DB and replace it with their own
|
||||||
DB = config.DB;
|
DB = config.DB;
|
||||||
}
|
}
|
||||||
|
config.db.network = this.network;
|
||||||
|
|
||||||
this.db = new DB(config.db);
|
this.db = new DB(config.db);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user