check for double-spends in txdb.

This commit is contained in:
Christopher Jeffrey 2016-03-02 03:03:14 -08:00
parent d86031c073
commit e8a70f9d7b

View File

@ -237,8 +237,6 @@ TXPool.prototype._add = function add(tx, callback, force) {
return next();
}
// TODO: Check for double-spend by doing self.getTX(input.prevout.hash)
// Only add orphans if this input is ours.
self._hasAddress(input.getAddress(), function(err, result) {
if (err)
@ -247,31 +245,40 @@ TXPool.prototype._add = function add(tx, callback, force) {
if (!result)
return next();
// Add orphan, if no parent transaction is yet known
self.db.get(p + 'o/' + key, function(err, orphans) {
if (err && err.type !== 'NotFoundError')
self.getTX(input.prevout.hash, function(err, result) {
if (err)
return done(err);
if (orphans) {
try {
orphans = JSON.parse(orphans.toString('utf8'));
} catch (e) {
return done(e);
// Are we double-spending?
if (result)
return done(new Error('Transaction is double-spending.'));
// Add orphan, if no parent transaction is yet known
self.db.get(p + 'o/' + key, function(err, orphans) {
if (err && err.type !== 'NotFoundError')
return done(err);
if (orphans) {
try {
orphans = JSON.parse(orphans.toString('utf8'));
} catch (e) {
return done(e);
}
} else {
orphans = [];
}
} else {
orphans = [];
}
orphans.push({
tx: tx.toExtended(true).toString('hex'),
index: i
orphans.push({
tx: tx.toExtended(true).toString('hex'),
index: i
});
orphans = new Buffer(JSON.stringify(orphans), 'utf8');
batch.put(p + 'o/' + key, orphans);
return next();
});
orphans = new Buffer(JSON.stringify(orphans), 'utf8');
batch.put(p + 'o/' + key, orphans);
return next();
});
});
});