queue
This commit is contained in:
parent
90b3c16837
commit
f17575f864
@ -281,6 +281,7 @@ BlockDB.prototype.removeBlock = function removeBlock(hash, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
// TODO: Add check to make sure we
|
// TODO: Add check to make sure we
|
||||||
// can ONLY remove the last block.
|
// can ONLY remove the last block.
|
||||||
|
assert(block._fileOffset >= 0);
|
||||||
self.data.truncateAsync(block._fileOffset, function(err) {
|
self.data.truncateAsync(block._fileOffset, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -325,10 +326,12 @@ BlockDB.prototype.removeBlock = function removeBlock(hash, callback) {
|
|||||||
if (uaddr)
|
if (uaddr)
|
||||||
batch.del('t/a/' + uaddr + '/' + hash);
|
batch.del('t/a/' + uaddr + '/' + hash);
|
||||||
|
|
||||||
|
assert(input.output._fileOffset >= 0);
|
||||||
|
|
||||||
coinOffset = self.createOffset(
|
coinOffset = self.createOffset(
|
||||||
input.output._size,
|
input.output._size,
|
||||||
block._fileOffset + tx._offset + input.output._offset,
|
input.output._fileOffset,
|
||||||
block.height
|
input.output.height
|
||||||
);
|
);
|
||||||
|
|
||||||
if (address) {
|
if (address) {
|
||||||
@ -470,8 +473,10 @@ BlockDB.prototype.fillTX = function fillTX(tx, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (tx)
|
if (tx) {
|
||||||
input.output = bcoin.coin(tx, input.prevout.index);
|
input.output = bcoin.coin(tx, input.prevout.index);
|
||||||
|
input.output._fileOffset = tx._fileOffset + input.output._offset;
|
||||||
|
}
|
||||||
|
|
||||||
if (!--pending)
|
if (!--pending)
|
||||||
callback(null, tx);
|
callback(null, tx);
|
||||||
@ -790,6 +795,7 @@ BlockDB.prototype.getTX = function getTX(hash, callback) {
|
|||||||
tx.height = record.height;
|
tx.height = record.height;
|
||||||
tx.ts = entry.ts;
|
tx.ts = entry.ts;
|
||||||
tx.block = entry.hash;
|
tx.block = entry.hash;
|
||||||
|
tx._fileOffset = record.offset;
|
||||||
if (self.options.paranoid && tx.hash('hex') !== hash)
|
if (self.options.paranoid && tx.hash('hex') !== hash)
|
||||||
return callback(new Error('BlockDB is corrupt. All is lost.'));
|
return callback(new Error('BlockDB is corrupt. All is lost.'));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,6 +52,7 @@ function Peer(pool, options) {
|
|||||||
this.banScore = 0;
|
this.banScore = 0;
|
||||||
this.orphans = 0;
|
this.orphans = 0;
|
||||||
this.orphanTime = 0;
|
this.orphanTime = 0;
|
||||||
|
this.activeBlocks = 0;
|
||||||
|
|
||||||
if (options.socket) {
|
if (options.socket) {
|
||||||
this.socket = options.socket;
|
this.socket = options.socket;
|
||||||
|
|||||||
@ -102,8 +102,6 @@ function Pool(options) {
|
|||||||
(Math.random() * 0xffffffff) | 0
|
(Math.random() * 0xffffffff) | 0
|
||||||
);
|
);
|
||||||
|
|
||||||
this._handlingBlock = 0;
|
|
||||||
|
|
||||||
this.peers = {
|
this.peers = {
|
||||||
// Peers that are loading blocks themselves
|
// Peers that are loading blocks themselves
|
||||||
regular: [],
|
regular: [],
|
||||||
@ -135,7 +133,8 @@ function Pool(options) {
|
|||||||
this.request = {
|
this.request = {
|
||||||
map: {},
|
map: {},
|
||||||
active: 0,
|
active: 0,
|
||||||
queue: []
|
queue: [],
|
||||||
|
activeBlocks: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
this.validate = {
|
this.validate = {
|
||||||
@ -615,15 +614,33 @@ Pool.prototype._prehandleBlock = function _prehandleBlock(block, peer, callback)
|
|||||||
Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
|
Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
this._pending = this._pending || [];
|
||||||
|
|
||||||
|
if (this._lock) {
|
||||||
|
this._pending.push([block, peer, callback]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._lock = true;
|
||||||
|
|
||||||
callback = utils.asyncify(callback);
|
callback = utils.asyncify(callback);
|
||||||
|
|
||||||
function done(err, result) {
|
function done(err, result) {
|
||||||
if (!--self._handlingBlock)
|
var item;
|
||||||
self.emit('flush');
|
|
||||||
callback(err, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._handlingBlock++;
|
self._lock = false;
|
||||||
|
|
||||||
|
callback(err, result);
|
||||||
|
|
||||||
|
if (self._pending.length === 0) {
|
||||||
|
self._nextBlock(peer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
item = self._pending.shift();
|
||||||
|
|
||||||
|
self._handleBlock(item[0], item[1], item[2]);
|
||||||
|
}
|
||||||
|
|
||||||
this._prehandleBlock(block, peer, function(err) {
|
this._prehandleBlock(block, peer, function(err) {
|
||||||
var requested;
|
var requested;
|
||||||
@ -760,11 +777,6 @@ Pool.prototype._addIndex = function _addIndex(block, peer, callback) {
|
|||||||
|
|
||||||
self.emit('chain-progress', self.chain.fillPercent(), peer);
|
self.emit('chain-progress', self.chain.fillPercent(), peer);
|
||||||
|
|
||||||
if (added > 0 && peer._requesting)
|
|
||||||
delete peer._requesting;
|
|
||||||
|
|
||||||
self._nextBlock(peer);
|
|
||||||
|
|
||||||
self.block.total += added;
|
self.block.total += added;
|
||||||
|
|
||||||
if (self.chain.height() % 20 === 0) {
|
if (self.chain.height() % 20 === 0) {
|
||||||
@ -1588,26 +1600,15 @@ Pool.prototype._request = function _request(peer, type, hash, options, cb) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._startRequests = function _startRequests(peer) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (!this._handlingBlock)
|
|
||||||
return this._nextBlock(peer);
|
|
||||||
|
|
||||||
this.on('flush', function() {
|
|
||||||
self._nextBlock(peer);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Pool.prototype._nextBlock = function _nextBlock(peer) {
|
Pool.prototype._nextBlock = function _nextBlock(peer) {
|
||||||
var item;
|
var item;
|
||||||
|
|
||||||
if (peer._requesting)
|
if (peer.activeBlocks > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// item = peer._blockQueue.shift();
|
// item = peer._blockQueue.shift();
|
||||||
item = peer._blockQueue.slice(0, 5);
|
item = peer._blockQueue.slice(0, 250);
|
||||||
peer._blockQueue = peer._blockQueue.slice(5);
|
peer._blockQueue = peer._blockQueue.slice(250);
|
||||||
|
|
||||||
if (peer.destroyed)
|
if (peer.destroyed)
|
||||||
return;
|
return;
|
||||||
@ -1615,8 +1616,6 @@ Pool.prototype._nextBlock = function _nextBlock(peer) {
|
|||||||
if (!item.length)
|
if (!item.length)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// XXX MAYBE ONLY CREATE LOAD REQUEST HERE
|
|
||||||
|
|
||||||
utils.debug(
|
utils.debug(
|
||||||
'Requesting block %s (%d/%d) from %s with getdata (height %d)',
|
'Requesting block %s (%d/%d) from %s with getdata (height %d)',
|
||||||
utils.revHex(item[item.length - 1].hash),
|
utils.revHex(item[item.length - 1].hash),
|
||||||
@ -1625,7 +1624,8 @@ Pool.prototype._nextBlock = function _nextBlock(peer) {
|
|||||||
peer.host,
|
peer.host,
|
||||||
this.chain.height());
|
this.chain.height());
|
||||||
|
|
||||||
peer._requesting = item;
|
peer.activeBlocks += item.length;
|
||||||
|
this.request.activeBlocks += item.length;
|
||||||
|
|
||||||
peer.getData(item);
|
peer.getData(item);
|
||||||
};
|
};
|
||||||
@ -1648,7 +1648,7 @@ Pool.prototype._response = function _response(hash) {
|
|||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return item;
|
||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype.getBlock = function getBlock(hash, cb) {
|
Pool.prototype.getBlock = function getBlock(hash, cb) {
|
||||||
@ -2026,8 +2026,13 @@ LoadRequest.prototype.finish = function finish() {
|
|||||||
if (this.pool.request.map[this.hash]) {
|
if (this.pool.request.map[this.hash]) {
|
||||||
delete this.pool.request.map[this.hash];
|
delete this.pool.request.map[this.hash];
|
||||||
this.pool.request.active--;
|
this.pool.request.active--;
|
||||||
|
if (this.type !== 'tx')
|
||||||
|
this.pool.request.activeBlocks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.type !== 'tx')
|
||||||
|
this.peer.activeBlocks--;
|
||||||
|
|
||||||
if (this.type === 'tx') {
|
if (this.type === 'tx') {
|
||||||
index = this.peer._txQueue.indexOf(this);
|
index = this.peer._txQueue.indexOf(this);
|
||||||
if (index !== -1)
|
if (index !== -1)
|
||||||
|
|||||||
@ -692,7 +692,6 @@ TX.prototype.addOutput = function addOutput(obj, value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
output = bcoin.output({
|
output = bcoin.output({
|
||||||
tx: this,
|
|
||||||
value: options.value,
|
value: options.value,
|
||||||
script: options.script,
|
script: options.script,
|
||||||
_size: options._size,
|
_size: options._size,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user