block/tx: sigops cost/size/weight.

This commit is contained in:
Christopher Jeffrey 2016-12-13 16:52:31 -08:00
parent f9a1e18437
commit 33a8e1e511
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
11 changed files with 24 additions and 22 deletions

View File

@ -653,9 +653,9 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) {
}
// Count sigops (legacy + scripthash? + witness?)
sigops += tx.getSigopsWeight(view, state.flags);
sigops += tx.getSigopsCost(view, state.flags);
if (sigops > constants.block.MAX_SIGOPS_WEIGHT) {
if (sigops > constants.block.MAX_SIGOPS_COST) {
throw new VerifyError(block,
'invalid',
'bad-blk-sigops',

View File

@ -1548,6 +1548,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
var vbrules = [];
var attempt = yield this._getAttempt(false);
var block = attempt.block;
var scale = attempt.witness ? 1 : constants.WITNESS_SCALE_FACTOR;
var i, j, entry, tx, deps, input, dep, output, raw, rwhash;
var template, name, deployment, state;
@ -1576,7 +1577,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
hash: tx.rwhash(),
depends: deps,
fee: entry.fee,
sigops: entry.sigops,
sigops: entry.sigops / scale | 0,
weight: tx.getWeight()
});
}
@ -1629,10 +1630,8 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
maxtime: this.network.now() + 2 * 60 * 60,
mutable: mutable,
noncerange: '00000000ffffffff',
sigoplimit: attempt.witness
? constants.block.MAX_SIGOPS_WEIGHT
: constants.block.MAX_SIGOPS,
sizelimit: constants.block.MAX_SIZE,
sigoplimit: constants.block.MAX_SIGOPS_COST / scale | 0,
sizelimit: constants.block.MAX_RAW_SIZE,
weightlimit: constants.block.MAX_WEIGHT,
curtime: block.ts,
bits: util.hex32(block.bits),
@ -1661,7 +1660,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
hash: rwhash,
depends: [],
fee: 0,
sigops: tx.getSigopsWeight(),
sigops: tx.getSigopsCost() / scale | 0,
weight: tx.getWeight()
};
} else {

View File

@ -552,6 +552,8 @@ HTTPServer.prototype._init = function _init() {
req.body.method = 'getworklp';
}
this.logger.info(req.body);
try {
json = yield this.rpc.execute(req.body);
} catch (err) {

View File

@ -902,7 +902,7 @@ Mempool.prototype.verify = co(function* verify(entry, view) {
}
// Annoying process known as sigops counting.
if (tx.getSigopsWeight(view, flags) > constants.tx.MAX_SIGOPS_WEIGHT) {
if (tx.getSigopsCost(view, flags) > constants.tx.MAX_SIGOPS_COST) {
throw new VerifyError(tx,
'nonstandard',
'bad-txns-too-many-sigops',

View File

@ -84,7 +84,7 @@ MempoolEntry.fromOptions = function fromOptions(options) {
MempoolEntry.prototype.fromTX = function fromTX(tx, view, height) {
var priority = tx.getPriority(view, height);
var value = tx.getChainValue(view, height);
var sigops = tx.getSigopsWeight(view);
var sigops = tx.getSigopsCost(view);
var dependencies = false;
var size = tx.getVirtualSize();
var fee = tx.getFee(view);

View File

@ -54,7 +54,7 @@ function Miner(options) {
this.minWeight = 0;
this.maxWeight = 750000 * constants.WITNESS_SCALE_FACTOR;
this.maxSigops = constants.block.MAX_SIGOPS_WEIGHT;
this.maxSigops = constants.block.MAX_SIGOPS_COST;
this.priorityWeight = 50000 * constants.WITNESS_SCALE_FACTOR;
this.minPriority = constants.tx.FREE_THRESHOLD;
@ -106,7 +106,7 @@ Miner.prototype._initOptions = function _initOptions(options) {
if (options.maxSigops != null) {
assert(util.isNumber(options.maxSigops));
assert(options.maxSigops <= constants.block.MAX_SIGOPS_WEIGHT);
assert(options.maxSigops <= constants.block.MAX_SIGOPS_COST);
this.maxSigops = options.maxSigops;
}
@ -462,7 +462,7 @@ Miner.prototype.build = function build(attempt) {
sigops += item.sigops;
// If we had a view we could do:
// sigops += tx.getSigopsWeight(view, attempt.flags);
// sigops += tx.getSigopsCost(view, attempt.flags);
if (sigops > this.maxSigops)
continue;

View File

@ -190,7 +190,7 @@ MinerBlock.prototype._init = function _init() {
this.weight += 8 * scale;
// Initialize sigops weight.
this.sigops = cb.getSigopsWeight(null, this.flags);
this.sigops = cb.getSigopsCost(null, this.flags);
};
/**
@ -288,7 +288,7 @@ MinerBlock.prototype.addTX = function addTX(tx, view) {
if (this.weight + weight > constants.block.MAX_WEIGHT)
return false;
if (this.sigops + sigops > constants.block.MAX_SIGOPS_WEIGHT)
if (this.sigops + sigops > constants.block.MAX_SIGOPS_COST)
return false;
if (!this.witness && tx.hasWitness())
@ -522,7 +522,7 @@ BlockEntry.fromTX = function fromTX(tx, view, attempt) {
entry.rate = tx.getRate(view);
entry.priority = tx.getPriority(view, attempt.height);
entry.free = false;
entry.sigops = tx.getSigopsWeight(view, attempt.flags);
entry.sigops = tx.getSigopsCost(view, attempt.flags);
return entry;
};

View File

@ -484,7 +484,7 @@ Block.prototype._verify = function _verify(ret) {
// Count legacy sigops (do not count scripthash or witness).
sigops += tx.getLegacySigops();
if (sigops * scale > constants.block.MAX_SIGOPS_WEIGHT) {
if (sigops * scale > constants.block.MAX_SIGOPS_COST) {
ret.reason = 'bad-blk-sigops';
ret.score = 100;
return false;

View File

@ -1177,7 +1177,7 @@ TX.prototype.getScripthashSigops = function getScripthashSigops(view) {
* @returns {Number} sigop weight
*/
TX.prototype.getSigopsWeight = function getSigopsWeight(view, flags) {
TX.prototype.getSigopsCost = function getSigopsCost(view, flags) {
var weight = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR;
var i, input, coin;
@ -1220,7 +1220,7 @@ TX.prototype.getSigops = function getSigops(view, flags) {
if (flags == null)
flags = constants.flags.STANDARD_VERIFY_FLAGS;
return (this.getSigopsWeight(view, flags) + scale - 1) / scale | 0;
return (this.getSigopsCost(view, flags) + scale - 1) / scale | 0;
};
/**

View File

@ -385,9 +385,10 @@ exports.WITNESS_SCALE_FACTOR = 4;
exports.block = {
MAX_SIZE: 1000000,
MAX_RAW_SIZE: 4000000,
MAX_WEIGHT: 4000000,
MAX_SIGOPS: 1000000 / 50,
MAX_SIGOPS_WEIGHT: 80000,
MAX_SIGOPS_COST: 80000,
MEDIAN_TIMESPAN: 11,
BIP16_TIME: 1333238400,
SIGHASH_LIMIT: 1300000000
@ -421,7 +422,7 @@ exports.tx = {
BARE_MULTISIG: true,
FREE_THRESHOLD: exports.COIN * 144 / 250,
MAX_SIGOPS: exports.block.MAX_SIGOPS / 5,
MAX_SIGOPS_WEIGHT: exports.block.MAX_SIGOPS_WEIGHT / 5,
MAX_SIGOPS_COST: exports.block.MAX_SIGOPS_COST / 5,
BYTES_PER_SIGOP: 20,
COINBASE_MATURITY: 100
};

View File

@ -180,7 +180,7 @@ describe('Block', function() {
assert(tx.checkInputs(view, height));
assert(tx.verify(view, flags));
assert(!tx.hasWitness());
sigops += tx.getSigopsWeight(view, flags);
sigops += tx.getSigopsCost(view, flags);
view.addTX(tx, height);
}