refactor txdb block handling.

This commit is contained in:
Christopher Jeffrey 2016-07-14 15:54:17 -07:00
parent 3ac22f0338
commit dd70c3167b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 55 additions and 59 deletions

View File

@ -1164,30 +1164,38 @@ ChainDB.prototype.scan = function scan(start, hashes, iter, callback) {
total++;
self.getFullBlock(hash, function(err, block) {
self.getEntry(hash, function(err, entry) {
if (err)
return next(err);
if (!block)
if (!entry)
return next();
self.logger.info('Scanning block %s.', utils.revHex(hash));
utils.forEachSerial(block.txs, function(tx, next) {
var hashes = tx.getHashes('hex');
var i, hash;
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
if (hashMap[hash])
return iter(tx, block.toHeaders(), next);
}
next();
}, function(err) {
self.getFullBlock(hash, function(err, block) {
if (err)
return next(err);
self.getNextHash(hash, next);
if (!block)
return next();
self.logger.info('Scanning block %s.', utils.revHex(hash));
utils.forEachSerial(block.txs, function(tx, next) {
var hashes = tx.getHashes('hex');
var i, hash;
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
if (hashMap[hash])
return iter(tx, entry, next);
}
next();
}, function(err) {
if (err)
return next(err);
self.getNextHash(hash, next);
});
});
});
})(null, start);

View File

@ -194,7 +194,7 @@ Fullnode.prototype._init = function _init() {
});
this.chain.on('connect', function(entry, block) {
self.walletdb.addBlock(block, function(err) {
self.walletdb.addBlock(entry, block.txs, function(err) {
if (err)
self._error(err);
});
@ -209,7 +209,7 @@ Fullnode.prototype._init = function _init() {
});
this.chain.on('disconnect', function(entry, block) {
self.walletdb.removeBlock(block, function(err) {
self.walletdb.removeBlock(entry, function(err) {
if (err)
self._error(err);
});

View File

@ -129,9 +129,9 @@ SPVNode.prototype._init = function _init() {
});
});
this.chain.on('block', function(block) {
this.chain.on('block', function(block, entry) {
self.emit('block', block);
self.walletdb.addBlock(block, function(err) {
self.walletdb.addBlock(entry, block.txs, function(err) {
if (err)
self._error(err);
});

View File

@ -320,35 +320,51 @@ TXDB.prototype.getTip = function getTip(callback) {
}, callback);
};
/**
* Write the best block hash.
* @param {Hash} hash
* @param {Function} callback
*/
TXDB.prototype.writeTip = function writeTip(hash, callback) {
if (typeof hash === 'string')
hash = new Buffer(hash, 'hex');
this.db.put('R', hash, callback);
};
/**
* Add a block's transactions and write the new best hash.
* @param {Block} block
* @param {Function} callback
*/
TXDB.prototype.addBlock = function addBlock(block, callback, force) {
TXDB.prototype.addBlock = function addBlock(block, txs, callback, force) {
var self = this;
var unlock;
unlock = this._lock(addBlock, [block, callback], force);
unlock = this._lock(addBlock, [block, txs, callback], force);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
utils.forEachSerial(block.txs, function(tx, next) {
if (!Array.isArray(txs))
txs = [txs];
utils.forEachSerial(txs, function(tx, next) {
self.add(tx, next, true);
}, function(err) {
if (err)
return callback(err);
self.db.put('R', block.hash(), callback);
self.writeTip(block.hash, callback);
});
};
/**
* Unconfirm a block's transactions and write the new best hash.
* Unconfirm a block's transactions
* and write the new best hash (SPV version).
* @param {Block} block
* @param {Function} callback
*/
@ -364,35 +380,7 @@ TXDB.prototype.removeBlock = function removeBlock(block, callback, force) {
callback = utils.wrap(callback, unlock);
utils.forEachSerial(block.txs, function(tx, next) {
self.unconfirm(tx.hash('hex'), next, true);
}, function(err) {
if (err)
return callback(err);
self.db.put('R', new Buffer(block.prevBlock, 'hex'), callback)
});
};
/**
* Unconfirm a block's transactions
* and write the new best hash (SPV version).
* @param {Block} block
* @param {Function} callback
*/
TXDB.prototype.removeBlockSPV = function removeBlockSPV(block, callback, force) {
var self = this;
var unlock;
unlock = this._lock(removeBlock, [block, callback], force);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
this.tx.getHeightHashes(block.height, function(err, hashes) {
this.getHeightHashes(block.height, function(err, hashes) {
if (err)
return callback(err);
@ -402,7 +390,7 @@ TXDB.prototype.removeBlockSPV = function removeBlockSPV(block, callback, force)
if (err)
return callback(err);
self.db.put('R', new Buffer(block.prevBlock, 'hex'), callback)
self.writeTip(block.prevBlock, callback);
});
});
};

View File

@ -1073,7 +1073,7 @@ WalletDB.prototype.rescan = function rescan(chaindb, callback) {
self.tx.add(tx, function(err) {
if (err)
return next(err);
self.db.put('R', block.hash(), next);
self.tx.writeTip(block.hash, next);
});
}, callback);
});
@ -1305,8 +1305,8 @@ WalletDB.prototype._getKey = function _getKey(id, account, errback, callback) {
* @param {Function} callback
*/
WalletDB.prototype.addBlock = function addBlock(block, callback) {
this.tx.addBlock(block, callback);
WalletDB.prototype.addBlock = function addBlock(block, txs, callback) {
this.tx.addBlock(block, txs, callback);
};
/**