refactor txdb block handling.
This commit is contained in:
parent
3ac22f0338
commit
dd70c3167b
@ -1164,30 +1164,38 @@ ChainDB.prototype.scan = function scan(start, hashes, iter, callback) {
|
|||||||
|
|
||||||
total++;
|
total++;
|
||||||
|
|
||||||
self.getFullBlock(hash, function(err, block) {
|
self.getEntry(hash, function(err, entry) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
if (!block)
|
if (!entry)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
self.logger.info('Scanning block %s.', utils.revHex(hash));
|
self.getFullBlock(hash, function(err, block) {
|
||||||
|
|
||||||
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) {
|
|
||||||
if (err)
|
if (err)
|
||||||
return next(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);
|
})(null, start);
|
||||||
|
|||||||
@ -194,7 +194,7 @@ Fullnode.prototype._init = function _init() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.chain.on('connect', function(entry, block) {
|
this.chain.on('connect', function(entry, block) {
|
||||||
self.walletdb.addBlock(block, function(err) {
|
self.walletdb.addBlock(entry, block.txs, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
self._error(err);
|
self._error(err);
|
||||||
});
|
});
|
||||||
@ -209,7 +209,7 @@ Fullnode.prototype._init = function _init() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.chain.on('disconnect', function(entry, block) {
|
this.chain.on('disconnect', function(entry, block) {
|
||||||
self.walletdb.removeBlock(block, function(err) {
|
self.walletdb.removeBlock(entry, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
self._error(err);
|
self._error(err);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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.emit('block', block);
|
||||||
self.walletdb.addBlock(block, function(err) {
|
self.walletdb.addBlock(entry, block.txs, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
self._error(err);
|
self._error(err);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -320,35 +320,51 @@ TXDB.prototype.getTip = function getTip(callback) {
|
|||||||
}, 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.
|
* Add a block's transactions and write the new best hash.
|
||||||
* @param {Block} block
|
* @param {Block} block
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.addBlock = function addBlock(block, callback, force) {
|
TXDB.prototype.addBlock = function addBlock(block, txs, callback, force) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var unlock;
|
var unlock;
|
||||||
|
|
||||||
unlock = this._lock(addBlock, [block, callback], force);
|
unlock = this._lock(addBlock, [block, txs, callback], force);
|
||||||
|
|
||||||
if (!unlock)
|
if (!unlock)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
callback = utils.wrap(callback, unlock);
|
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);
|
self.add(tx, next, true);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(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 {Block} block
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
@ -364,35 +380,7 @@ TXDB.prototype.removeBlock = function removeBlock(block, callback, force) {
|
|||||||
|
|
||||||
callback = utils.wrap(callback, unlock);
|
callback = utils.wrap(callback, unlock);
|
||||||
|
|
||||||
utils.forEachSerial(block.txs, function(tx, next) {
|
this.getHeightHashes(block.height, function(err, hashes) {
|
||||||
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) {
|
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -402,7 +390,7 @@ TXDB.prototype.removeBlockSPV = function removeBlockSPV(block, callback, force)
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
self.db.put('R', new Buffer(block.prevBlock, 'hex'), callback)
|
self.writeTip(block.prevBlock, callback);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1073,7 +1073,7 @@ WalletDB.prototype.rescan = function rescan(chaindb, callback) {
|
|||||||
self.tx.add(tx, function(err) {
|
self.tx.add(tx, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
self.db.put('R', block.hash(), next);
|
self.tx.writeTip(block.hash, next);
|
||||||
});
|
});
|
||||||
}, callback);
|
}, callback);
|
||||||
});
|
});
|
||||||
@ -1305,8 +1305,8 @@ WalletDB.prototype._getKey = function _getKey(id, account, errback, callback) {
|
|||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.addBlock = function addBlock(block, callback) {
|
WalletDB.prototype.addBlock = function addBlock(block, txs, callback) {
|
||||||
this.tx.addBlock(block, callback);
|
this.tx.addBlock(block, txs, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user