This commit is contained in:
Christopher Jeffrey 2016-08-11 04:32:03 -07:00
parent 092a0968ad
commit 400680e208
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -227,8 +227,7 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) {
if (buf) if (buf)
p.writeBytes(buf); p.writeBytes(buf);
p.writeHash(hash); bcoin.outpoint(hash, index).toRaw(p);
p.writeU32(index);
return callback(null, p.render()); return callback(null, p.render());
}); });
@ -243,17 +242,14 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) {
TXDB.prototype._getOrphans = function _getOrphans(key, callback) { TXDB.prototype._getOrphans = function _getOrphans(key, callback) {
var self = this; var self = this;
var out = [];
this.db.fetch('o/' + key, function(buf) { this.db.fetch('o/' + key, function(buf) {
var p = new BufferReader(buf); var p = new BufferReader(buf);
var orphans = []; var orphans = [];
while (p.left()) { while (p.left())
orphans.push({ orphans.push(bcoin.outpoint.fromRaw(p));
hash: p.readHash('hex'),
index: p.readU32()
});
}
return orphans; return orphans;
}, function(err, orphans) { }, function(err, orphans) {
@ -268,7 +264,7 @@ TXDB.prototype._getOrphans = function _getOrphans(key, callback) {
if (err) if (err)
return next(err); return next(err);
orphan.tx = tx; out.push([orphan, tx]);
next(); next();
}); });
@ -276,7 +272,7 @@ TXDB.prototype._getOrphans = function _getOrphans(key, callback) {
if (err) if (err)
return callback(err); return callback(err);
return callback(null, orphans); return callback(null, out);
}); });
}); });
}; };
@ -496,7 +492,8 @@ TXDB.prototype._add = function add(tx, info, callback, force) {
key = prevout.hash + '/' + prevout.index; key = prevout.hash + '/' + prevout.index;
batch.put('s/' + key, tx.hash()); // s/[outpoint-key] -> [spender-hash]|[spender-input-index]
batch.put('s/' + key, bcoin.outpoint.fromTX(tx, i).toRaw());
if (coin) { if (coin) {
// Add TX to inputs and spend money // Add TX to inputs and spend money
@ -517,6 +514,7 @@ TXDB.prototype._add = function add(tx, info, callback, force) {
} }
batch.del('c/' + key); batch.del('c/' + key);
batch.put('d/' + tx.hash('hex') + '/' + pad32(i), coin.toRaw());
self.coinCache.remove(key); self.coinCache.remove(key);
@ -607,32 +605,31 @@ TXDB.prototype._add = function add(tx, info, callback, force) {
return finish(); return finish();
// Add input to orphan // Add input to orphan
utils.forEachSerial(orphans, function(orphan, next) { utils.forEachSerial(orphans, function(pair, next) {
if (some) if (some)
return next(); return next();
var input = pair[0];
var orphan = pair[1];
// Probably removed by some other means. // Probably removed by some other means.
if (!orphan.tx) if (!orphan)
return next(); return next();
orphan.tx.inputs[orphan.index].coin = coin; orphan.inputs[input.index].coin = coin;
assert(orphan.tx.inputs[orphan.index].prevout.hash === hash); assert(orphan.inputs[input.index].prevout.hash === hash);
assert(orphan.tx.inputs[orphan.index].prevout.index === i); assert(orphan.inputs[input.index].prevout.index === i);
// Verify that input script is correct, if not - add // Verify that input script is correct, if not - add
// output to unspent and remove orphan from storage // output to unspent and remove orphan from storage
if (!self.options.verify) { if (!self.options.verify || orphan.verifyInput(input.index)) {
some = true; some = true;
batch.put('d/' + input.hash + '/' + pad32(input.index), coin.toRaw());
return next(); return next();
} }
if (orphan.tx.verifyInput(orphan.index)) { self.lazyRemove(orphan, next, true);
some = true;
return next();
}
self.lazyRemove(orphan.tx, next, true);
}, function(err) { }, function(err) {
if (err) if (err)
return next(err); return next(err);
@ -821,7 +818,7 @@ TXDB.prototype.isDoubleSpend = function isDoubleSpend(tx, callback) {
TXDB.prototype.isSpent = function isSpent(hash, index, callback) { TXDB.prototype.isSpent = function isSpent(hash, index, callback) {
var key = 's/' + hash + '/' + index; var key = 's/' + hash + '/' + index;
return this.db.fetch(key, function(hash) { return this.db.fetch(key, function(hash) {
return hash.toString('hex'); return hash.toString('hex', 0, 32);
}, callback); }, callback);
}; };
@ -894,8 +891,10 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback, force) {
if (err) if (err)
return next(err); return next(err);
if (!coin) if (!coin) {
// TODO: Update spent coin here!
return next(); return next();
}
coin.height = tx.height; coin.height = tx.height;
coin = coin.toRaw(); coin = coin.toRaw();
@ -1055,6 +1054,7 @@ TXDB.prototype._remove = function remove(tx, info, callback, force) {
coin = input.coin.toRaw(); coin = input.coin.toRaw();
batch.put('c/' + key, coin); batch.put('c/' + key, coin);
batch.del('d/' + tx.hash('hex') + '/' + pad32(i));
batch.del('s/' + key); batch.del('s/' + key);
batch.del('o/' + key); batch.del('o/' + key);
@ -1179,8 +1179,10 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
if (err) if (err)
return next(err); return next(err);
if (!coin) if (!coin) {
// TODO: Update spent coin here
return next(); return next();
}
coin.height = tx.height; coin.height = tx.height;
coin = coin.toRaw(); coin = coin.toRaw();
@ -1589,25 +1591,28 @@ TXDB.prototype.getCoins = function getCoins(id, callback) {
TXDB.prototype.fillHistory = function fillHistory(tx, callback) { TXDB.prototype.fillHistory = function fillHistory(tx, callback) {
var self = this; var self = this;
var hash, index, coin, input;
if (tx.isCoinbase()) { if (tx.isCoinbase()) {
callback = utils.asyncify(callback); callback = utils.asyncify(callback);
return callback(null, tx); return callback(null, tx);
} }
utils.forEachSerial(tx.inputs, function(input, next) { hash = tx.hash('hex');
if (input.coin)
return next();
self.getTX(input.prevout.hash, function(err, tx) { this.db.iterate({
if (err) gte: 'd/' + hash + '/' + pad32(0),
return next(err); lte: 'd/' + hash + '/' + pad32(0xffffffff),
keys: true,
if (tx) values: true,
input.coin = bcoin.coin.fromTX(tx, input.prevout.index); parse: function(value, key) {
index = +key.split('/')[2];
next(); coin = bcoin.coin.fromRaw(value);
}); input = tx.inputs[index];
coin.hash = input.prevout.hash;
coin.index = input.prevout.index;
input.coin = coin;
}
}, function(err) { }, function(err) {
if (err) if (err)
return callback(err); return callback(err);
@ -2034,9 +2039,9 @@ function Details(id, tx, table) {
this.block = tx.block; this.block = tx.block;
this.index = tx.index; this.index = tx.index;
this.confirmations = tx.getConfirmations(); this.confirmations = tx.getConfirmations();
this.fee = tx.hasCoins() ? tx.getFee() : 0;
this.ts = tx.ts; this.ts = tx.ts;
this.ps = tx.ps; this.ps = tx.ps;
this.fee = tx.getFee();
this.tx = tx; this.tx = tx;
this.inputs = []; this.inputs = [];
this.outputs = []; this.outputs = [];