miner: better sorting of txs.
This commit is contained in:
parent
749a912f8c
commit
12a5bcc4e2
@ -728,7 +728,7 @@ Mempool.prototype._addUnchecked = co(function* addUnchecked(entry) {
|
|||||||
this.trackEntry(entry);
|
this.trackEntry(entry);
|
||||||
|
|
||||||
this.emit('tx', entry.tx);
|
this.emit('tx', entry.tx);
|
||||||
this.emit('add tx', entry.tx);
|
this.emit('add entry', entry);
|
||||||
|
|
||||||
if (this.fees)
|
if (this.fees)
|
||||||
this.fees.processTX(entry, this.chain.isFull());
|
this.fees.processTX(entry, this.chain.isFull());
|
||||||
@ -809,7 +809,7 @@ Mempool.prototype.removeUnchecked = function removeUnchecked(entry, limit) {
|
|||||||
this.logger.spam('Removed block tx %s from mempool.', entry.tx.rhash);
|
this.logger.spam('Removed block tx %s from mempool.', entry.tx.rhash);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('remove tx', entry.tx);
|
this.emit('remove entry', entry);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -121,25 +121,30 @@ Miner.prototype.start = co(function* start() {
|
|||||||
|
|
||||||
this.running = true;
|
this.running = true;
|
||||||
|
|
||||||
try {
|
while (!block) {
|
||||||
attempt = yield this.createBlock();
|
if (!this.running)
|
||||||
} catch (e) {
|
return;
|
||||||
this.running = false;
|
|
||||||
this.emit('error', e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.running)
|
try {
|
||||||
return;
|
attempt = yield this.createBlock();
|
||||||
|
} catch (e) {
|
||||||
|
this.running = false;
|
||||||
|
this.emit('error', e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.attempt = attempt;
|
if (!this.running)
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
this.attempt = attempt;
|
||||||
block = yield attempt.mineAsync();
|
|
||||||
} catch (e) {
|
try {
|
||||||
this.emit('error', e);
|
block = yield attempt.mineAsync();
|
||||||
this.restart();
|
} catch (e) {
|
||||||
return;
|
this.emit('error', e);
|
||||||
|
this.restart();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -193,7 +198,7 @@ Miner.prototype.restart = function restart() {
|
|||||||
|
|
||||||
Miner.prototype.createBlock = co(function* createBlock(tip) {
|
Miner.prototype.createBlock = co(function* createBlock(tip) {
|
||||||
var version = this.version;
|
var version = this.version;
|
||||||
var i, ts, attempt, txs, tx, target;
|
var ts, attempt, target, entries;
|
||||||
|
|
||||||
if (!tip)
|
if (!tip)
|
||||||
tip = this.chain.tip;
|
tip = this.chain.tip;
|
||||||
@ -211,21 +216,16 @@ Miner.prototype.createBlock = co(function* createBlock(tip) {
|
|||||||
tip: tip,
|
tip: tip,
|
||||||
version: version,
|
version: version,
|
||||||
bits: target,
|
bits: target,
|
||||||
|
flags: this.chain.state.flags,
|
||||||
address: this.address,
|
address: this.address,
|
||||||
coinbaseFlags: this.coinbaseFlags,
|
coinbaseFlags: this.coinbaseFlags,
|
||||||
witness: this.chain.state.hasWitness(),
|
witness: this.chain.state.hasWitness(),
|
||||||
network: this.network
|
network: this.network
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!this.mempool)
|
entries = this.getSorted();
|
||||||
return attempt;
|
|
||||||
|
|
||||||
txs = this.mempool.getHistory();
|
attempt.build(entries);
|
||||||
|
|
||||||
for (i = 0; i < txs.length; i++) {
|
|
||||||
tx = txs[i];
|
|
||||||
attempt.addTX(tx);
|
|
||||||
}
|
|
||||||
|
|
||||||
return attempt;
|
return attempt;
|
||||||
});
|
});
|
||||||
@ -250,8 +250,99 @@ Miner.prototype.addTX = function addTX(tx) {
|
|||||||
if (!this.running)
|
if (!this.running)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this.attempt)
|
if (!this.attempt)
|
||||||
this.attempt.addTX(tx.clone());
|
return;
|
||||||
|
|
||||||
|
this.attempt.addTX(tx);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notify the miner that a new tx has entered the mempool.
|
||||||
|
* @param {MempoolEntry} entry
|
||||||
|
*/
|
||||||
|
|
||||||
|
Miner.prototype.notifyEntry = function notifyEntry() {
|
||||||
|
if (!this.running)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!this.attempt)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.attempt.since++;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a block "attempt".
|
||||||
|
* @param {ChainEntry} tip
|
||||||
|
* @returns {Promise} - Returns {@link MinerBlock}.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Miner.prototype.getSorted = function getSorted() {
|
||||||
|
var depMap = {};
|
||||||
|
var count = {};
|
||||||
|
var result = [];
|
||||||
|
var top = [];
|
||||||
|
var i, j, entry, tx, hash, input;
|
||||||
|
var prev, hasDeps, deps, hashes;
|
||||||
|
|
||||||
|
if (!this.mempool)
|
||||||
|
return [];
|
||||||
|
|
||||||
|
hashes = this.mempool.getSnapshot();
|
||||||
|
|
||||||
|
for (i = 0; i < hashes.length; i++) {
|
||||||
|
hash = hashes[i];
|
||||||
|
entry = this.mempool.getEntry(hash);
|
||||||
|
tx = entry.tx;
|
||||||
|
|
||||||
|
count[hash] = 0;
|
||||||
|
hasDeps = false;
|
||||||
|
|
||||||
|
for (j = 0; j < tx.inputs.length; j++) {
|
||||||
|
input = tx.inputs[j];
|
||||||
|
prev = input.prevout.hash;
|
||||||
|
|
||||||
|
if (!this.mempool.hasTX(prev))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
hasDeps = true;
|
||||||
|
|
||||||
|
if (!depMap[prev])
|
||||||
|
depMap[prev] = [];
|
||||||
|
|
||||||
|
depMap[prev].push(entry);
|
||||||
|
count[hash]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasDeps)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
top.push(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < top.length; i++) {
|
||||||
|
entry = top[i];
|
||||||
|
tx = entry.tx;
|
||||||
|
hash = tx.hash('hex');
|
||||||
|
|
||||||
|
result.push(entry);
|
||||||
|
|
||||||
|
deps = depMap[hash];
|
||||||
|
|
||||||
|
if (!deps)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (j = 0; j < deps.length; j++) {
|
||||||
|
entry = deps[j];
|
||||||
|
tx = entry.tx;
|
||||||
|
hash = tx.hash('hex');
|
||||||
|
|
||||||
|
if (--count[hash] === 0)
|
||||||
|
result.push(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -53,6 +53,7 @@ function MinerBlock(options) {
|
|||||||
this.height = options.tip.height + 1;
|
this.height = options.tip.height + 1;
|
||||||
this.bits = options.bits;
|
this.bits = options.bits;
|
||||||
this.target = utils.fromCompact(this.bits).toArrayLike(Buffer, 'le', 32);
|
this.target = utils.fromCompact(this.bits).toArrayLike(Buffer, 'le', 32);
|
||||||
|
this.flags = options.flags;
|
||||||
this.extraNonce = new bn(0);
|
this.extraNonce = new bn(0);
|
||||||
this.iterations = 0;
|
this.iterations = 0;
|
||||||
this.coinbaseFlags = options.coinbaseFlags;
|
this.coinbaseFlags = options.coinbaseFlags;
|
||||||
@ -60,6 +61,10 @@ function MinerBlock(options) {
|
|||||||
this.address = options.address;
|
this.address = options.address;
|
||||||
this.network = Network.get(options.network);
|
this.network = Network.get(options.network);
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
|
this.since = 0;
|
||||||
|
|
||||||
|
this.sigops = 0;
|
||||||
|
this.weight = 0;
|
||||||
|
|
||||||
this.coinbase = new TX();
|
this.coinbase = new TX();
|
||||||
this.coinbase.mutable = true;
|
this.coinbase.mutable = true;
|
||||||
@ -241,23 +246,27 @@ MinerBlock.prototype.updateMerkle = function updateMerkle() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
MinerBlock.prototype.addTX = function addTX(tx) {
|
MinerBlock.prototype.addTX = function addTX(tx) {
|
||||||
var weight;
|
var weight, sigops;
|
||||||
|
|
||||||
assert(!tx.mutable, 'Cannot add mutable TX to block.');
|
assert(!tx.mutable, 'Cannot add mutable TX to block.');
|
||||||
|
|
||||||
weight = this.block.getWeight() + tx.getWeight();
|
if (this.block.hasTX(tx))
|
||||||
|
|
||||||
if (weight > constants.block.MAX_WEIGHT)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (this.block.hasTX(tx))
|
weight = tx.getWeight();
|
||||||
|
sigops = tx.getSigopsWeight(this.flags);
|
||||||
|
|
||||||
|
if (this.weight + weight > constants.block.MAX_WEIGHT)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (this.sigops + sigops > constants.block.MAX_SIGOPS_WEIGHT)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!this.witness && tx.hasWitness())
|
if (!this.witness && tx.hasWitness())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Add the tx to our block
|
// Add the tx to our block
|
||||||
this.block.addTX(tx);
|
this.block.addTX(tx.clone());
|
||||||
|
|
||||||
// Update coinbase value
|
// Update coinbase value
|
||||||
this.updateCoinbase();
|
this.updateCoinbase();
|
||||||
@ -268,6 +277,47 @@ MinerBlock.prototype.addTX = function addTX(tx) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fill the block with sorted mempool entries.
|
||||||
|
* @param {MempoolEntry[]} entries
|
||||||
|
*/
|
||||||
|
|
||||||
|
MinerBlock.prototype.build = function build(entries) {
|
||||||
|
var len = Math.min(entries.length, constants.block.MAX_SIZE);
|
||||||
|
var i, entry, tx, weight, sigops;
|
||||||
|
|
||||||
|
this.weight = this.block.getWeight() + 12;
|
||||||
|
this.sigops = this.coinbase.getSigopsWeight(this.flags);
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
entry = entries[i];
|
||||||
|
tx = entry.tx;
|
||||||
|
|
||||||
|
weight = tx.getWeight();
|
||||||
|
sigops = tx.getSigopsWeight(this.flags);
|
||||||
|
|
||||||
|
if (this.weight + weight > constants.block.MAX_WEIGHT)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (this.sigops + sigops > constants.block.MAX_SIGOPS_WEIGHT)
|
||||||
|
break;
|
||||||
|
|
||||||
|
this.weight += weight;
|
||||||
|
this.sigops += sigops;
|
||||||
|
|
||||||
|
// Add the tx to our block
|
||||||
|
this.block.addTX(tx.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update coinbase value
|
||||||
|
this.updateCoinbase();
|
||||||
|
|
||||||
|
// Update merkle root for new coinbase and new tx
|
||||||
|
this.updateMerkle();
|
||||||
|
|
||||||
|
assert(this.block.getWeight() <= this.weight);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hash until the nonce overflows.
|
* Hash until the nonce overflows.
|
||||||
* @returns {Boolean} Whether the nonce was found.
|
* @returns {Boolean} Whether the nonce was found.
|
||||||
@ -322,6 +372,9 @@ MinerBlock.prototype.findNonceAsync = co(function* findNonceAsync() {
|
|||||||
if (nonce !== -1)
|
if (nonce !== -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if (this.since > 20)
|
||||||
|
return nonce;
|
||||||
|
|
||||||
block.nonce = max;
|
block.nonce = max;
|
||||||
|
|
||||||
min += interval;
|
min += interval;
|
||||||
@ -381,6 +434,9 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
|
|||||||
if (nonce !== -1)
|
if (nonce !== -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if (this.since > 20)
|
||||||
|
return;
|
||||||
|
|
||||||
this.iterate();
|
this.iterate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -199,7 +199,7 @@ Fullnode.prototype._init = function _init() {
|
|||||||
this.mempool.on('tx', function(tx) {
|
this.mempool.on('tx', function(tx) {
|
||||||
self.emit('tx', tx);
|
self.emit('tx', tx);
|
||||||
self.walletdb.addTX(tx).catch(onError);
|
self.walletdb.addTX(tx).catch(onError);
|
||||||
self.miner.addTX(tx);
|
self.miner.notifyEntry();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.chain.on('block', function(block) {
|
this.chain.on('block', function(block) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user