refactor: rename cost to weight.

This commit is contained in:
Christopher Jeffrey 2016-09-13 02:06:18 -07:00
parent fbb7f25d0a
commit 07285ebdd7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
10 changed files with 39 additions and 39 deletions

View File

@ -406,12 +406,12 @@ Chain.prototype.verify = function verify(block, prev, callback) {
} }
} }
// Check block cost (different from block size // Check block weight (different from block size
// check in non-contextual verification). // check in non-contextual verification).
if (block.getCost() > constants.block.MAX_COST) { if (block.getWeight() > constants.block.MAX_WEIGHT) {
return callback(new VerifyError(block, return callback(new VerifyError(block,
'invalid', 'invalid',
'bad-blk-cost', 'bad-blk-weight',
100)); 100));
} }
@ -709,9 +709,9 @@ Chain.prototype.checkInputs = function checkInputs(block, prev, state, callback)
} }
// Count sigops (legacy + scripthash? + witness?) // Count sigops (legacy + scripthash? + witness?)
sigops += tx.getSigopsCost(state.flags); sigops += tx.getSigopsWeight(state.flags);
if (sigops > constants.block.MAX_SIGOPS_COST) { if (sigops > constants.block.MAX_SIGOPS_WEIGHT) {
return next(new VerifyError(block, return next(new VerifyError(block,
'invalid', 'invalid',
'bad-blk-sigops', 'bad-blk-sigops',

View File

@ -940,7 +940,7 @@ RPC.prototype._blockToJSON = function _blockToJSON(entry, block, txDetails, call
confirmations: self.chain.height - entry.height + 1, confirmations: self.chain.height - entry.height + 1,
strippedsize: block.getBaseSize(), strippedsize: block.getBaseSize(),
size: block.getSize(), size: block.getSize(),
weight: block.getCost(), weight: block.getWeight(),
height: entry.height, height: entry.height,
version: entry.version, version: entry.version,
merkleroot: utils.revHex(entry.merkleRoot), merkleroot: utils.revHex(entry.merkleRoot),
@ -1468,7 +1468,7 @@ RPC.prototype.getblocktemplate = function getblocktemplate(args, callback) {
depends: deps, depends: deps,
fee: tx.getFee(), fee: tx.getFee(),
sigops: tx.getSigops(), sigops: tx.getSigops(),
weight: tx.getCost() weight: tx.getWeight()
}); });
} }
@ -1533,9 +1533,9 @@ RPC.prototype.getblocktemplate = function getblocktemplate(args, callback) {
mintime: attempt.ts, mintime: attempt.ts,
mutable: mutable, mutable: mutable,
noncerange: '00000000ffffffff', noncerange: '00000000ffffffff',
sigoplimit: constants.block.MAX_SIGOPS_COST, sigoplimit: constants.block.MAX_SIGOPS_WEIGHT,
sizelimit: constants.block.MAX_SIZE, sizelimit: constants.block.MAX_SIZE,
weightlimit: constants.block.MAX_COST, weightlimit: constants.block.MAX_WEIGHT,
curtime: block.ts, curtime: block.ts,
bits: String(block.bits), bits: String(block.bits),
height: attempt.height, height: attempt.height,

View File

@ -827,7 +827,7 @@ Mempool.prototype.verify = function verify(entry, callback) {
0)); 0));
} }
if (tx.getSigopsCost(flags) > constants.tx.MAX_SIGOPS_COST) { if (tx.getSigopsWeight(flags) > constants.tx.MAX_SIGOPS_WEIGHT) {
return callback(new VerifyError(tx, return callback(new VerifyError(tx,
'nonstandard', 'nonstandard',
'bad-txns-too-many-sigops', 'bad-txns-too-many-sigops',

View File

@ -209,13 +209,13 @@ MinerBlock.prototype.updateMerkle = function updateMerkle() {
*/ */
MinerBlock.prototype.addTX = function addTX(tx) { MinerBlock.prototype.addTX = function addTX(tx) {
var cost; var weight;
assert(!tx.mutable, 'Cannot add mutable TX to block.'); assert(!tx.mutable, 'Cannot add mutable TX to block.');
cost = this.block.getCost() + tx.getCost(); weight = this.block.getWeight() + tx.getWeight();
if (cost > constants.block.MAX_COST) if (weight > constants.block.MAX_WEIGHT)
return false; return false;
if (this.block.hasTX(tx)) if (this.block.hasTX(tx))

View File

@ -178,15 +178,15 @@ Block.prototype.getSizes = function getSizes() {
Block.prototype.getVirtualSize = function getVirtualSize() { Block.prototype.getVirtualSize = function getVirtualSize() {
var scale = constants.WITNESS_SCALE_FACTOR; var scale = constants.WITNESS_SCALE_FACTOR;
return (this.getCost() + scale - 1) / scale | 0; return (this.getWeight() + scale - 1) / scale | 0;
}; };
/** /**
* Calculate block cost. * Calculate block weight.
* @returns {Number} cost * @returns {Number} weight
*/ */
Block.prototype.getCost = function getCost() { Block.prototype.getWeight = function getWeight() {
var sizes = this.getSizes(); var sizes = this.getSizes();
var base = sizes.size - sizes.witnessSize; var base = sizes.size - sizes.witnessSize;
return base * (constants.WITNESS_SCALE_FACTOR - 1) + sizes.size; return base * (constants.WITNESS_SCALE_FACTOR - 1) + sizes.size;
@ -427,7 +427,7 @@ Block.prototype._verify = function _verify(ret) {
// Count legacy sigops (do not count scripthash or witness) // Count legacy sigops (do not count scripthash or witness)
sigops += tx.getLegacySigops(); sigops += tx.getLegacySigops();
if (sigops * scale > constants.block.MAX_SIGOPS_COST) { if (sigops * scale > constants.block.MAX_SIGOPS_WEIGHT) {
ret.reason = 'bad-blk-sigops'; ret.reason = 'bad-blk-sigops';
ret.score = 100; ret.score = 100;
return false; return false;

View File

@ -353,16 +353,16 @@ TX.prototype.getSizes = function getSizes() {
TX.prototype.getVirtualSize = function getVirtualSize() { TX.prototype.getVirtualSize = function getVirtualSize() {
var scale = constants.WITNESS_SCALE_FACTOR; var scale = constants.WITNESS_SCALE_FACTOR;
return (this.getCost() + scale - 1) / scale | 0; return (this.getWeight() + scale - 1) / scale | 0;
}; };
/** /**
* Calculate the cost of the transaction. * Calculate the weight of the transaction.
* Note that this is cached. * Note that this is cached.
* @returns {Number} cost * @returns {Number} weight
*/ */
TX.prototype.getCost = function getCost() { TX.prototype.getWeight = function getWeight() {
var sizes = this.getSizes(); var sizes = this.getSizes();
var base = sizes.size - sizes.witnessSize; var base = sizes.size - sizes.witnessSize;
return base * (constants.WITNESS_SCALE_FACTOR - 1) + sizes.size; return base * (constants.WITNESS_SCALE_FACTOR - 1) + sizes.size;
@ -1086,23 +1086,23 @@ TX.prototype.getScripthashSigops = function getScripthashSigops() {
}; };
/** /**
* Calculate sigops cost, taking into account witness programs. * Calculate sigops weight, taking into account witness programs.
* @param {VerifyFlags?} flags * @param {VerifyFlags?} flags
* @returns {Number} sigop cost * @returns {Number} sigop weight
*/ */
TX.prototype.getSigopsCost = function getSigopsCost(flags) { TX.prototype.getSigopsWeight = function getSigopsWeight(flags) {
var cost = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR; var weight = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR;
var input, i; var input, i;
if (flags == null) if (flags == null)
flags = constants.flags.STANDARD_VERIFY_FLAGS; flags = constants.flags.STANDARD_VERIFY_FLAGS;
if (this.isCoinbase()) if (this.isCoinbase())
return cost; return weight;
if (flags & constants.flags.VERIFY_P2SH) if (flags & constants.flags.VERIFY_P2SH)
cost += this.getScripthashSigops() * constants.WITNESS_SCALE_FACTOR; weight += this.getScripthashSigops() * constants.WITNESS_SCALE_FACTOR;
for (i = 0; i < this.inputs.length; i++) { for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i]; input = this.inputs[i];
@ -1110,14 +1110,14 @@ TX.prototype.getSigopsCost = function getSigopsCost(flags) {
if (!input.coin) if (!input.coin)
continue; continue;
cost += Script.getWitnessSigops( weight += Script.getWitnessSigops(
input.script, input.script,
input.coin.script, input.coin.script,
input.witness, input.witness,
flags); flags);
} }
return cost; return weight;
}; };
/** /**
@ -1132,7 +1132,7 @@ TX.prototype.getSigops = function getSigops(flags) {
if (flags == null) if (flags == null)
flags = constants.flags.STANDARD_VERIFY_FLAGS; flags = constants.flags.STANDARD_VERIFY_FLAGS;
return (this.getSigopsCost(flags) + scale - 1) / scale | 0; return (this.getSigopsWeight(flags) + scale - 1) / scale | 0;
}; };
/** /**
@ -1251,7 +1251,7 @@ TX.prototype.isStandard = function isStandard(ret) {
return false; return false;
} }
if (this.getCost() >= constants.tx.MAX_COST) { if (this.getWeight() >= constants.tx.MAX_WEIGHT) {
ret.reason = 'tx-size'; ret.reason = 'tx-size';
ret.score = 0; ret.score = 0;
return false; return false;

View File

@ -385,9 +385,9 @@ exports.WITNESS_SCALE_FACTOR = 4;
exports.block = { exports.block = {
MAX_SIZE: 1000000, MAX_SIZE: 1000000,
MAX_COST: 4000000, MAX_WEIGHT: 4000000,
MAX_SIGOPS: 1000000 / 50, MAX_SIGOPS: 1000000 / 50,
MAX_SIGOPS_COST: 80000, MAX_SIGOPS_WEIGHT: 80000,
MEDIAN_TIMESPAN: 11, MEDIAN_TIMESPAN: 11,
BIP16_TIME: 1333238400, BIP16_TIME: 1333238400,
SIGHASH_LIMIT: 1300000000 SIGHASH_LIMIT: 1300000000
@ -414,14 +414,14 @@ exports.bip30 = {
exports.tx = { exports.tx = {
MAX_VERSION: 2, MAX_VERSION: 2,
MAX_SIZE: 100000, MAX_SIZE: 100000,
MAX_COST: 400000, MAX_WEIGHT: 400000,
MIN_FEE: 10000, MIN_FEE: 10000,
MAX_FEE: exports.COIN / 10, MAX_FEE: exports.COIN / 10,
MIN_RELAY: 10000, MIN_RELAY: 10000,
BARE_MULTISIG: true, BARE_MULTISIG: true,
FREE_THRESHOLD: exports.COIN * 144 / 250, FREE_THRESHOLD: exports.COIN * 144 / 250,
MAX_SIGOPS: exports.block.MAX_SIGOPS / 5, MAX_SIGOPS: exports.block.MAX_SIGOPS / 5,
MAX_SIGOPS_COST: exports.block.MAX_SIGOPS_COST / 5, MAX_SIGOPS_WEIGHT: exports.block.MAX_SIGOPS_WEIGHT / 5,
COINBASE_MATURITY: 100 COINBASE_MATURITY: 100
}; };

View File

@ -143,7 +143,7 @@ describe('Block', function() {
assert(block.txs[0].isCoinbase()); assert(block.txs[0].isCoinbase());
assert(block.txs[0].isSane()); assert(block.txs[0].isSane());
assert(!block.hasWitness()); assert(!block.hasWitness());
assert.equal(block.getCost(), 1136924); assert.equal(block.getWeight(), 1136924);
var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG; var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG;
for (var i = 1; i < block.txs.length; i++) { for (var i = 1; i < block.txs.length; i++) {
var tx = block.txs[i]; var tx = block.txs[i];

View File

@ -162,7 +162,7 @@ describe('TX', function() {
'088c919cd8408005f255c411f786928385688a9e8fdb2db4c9bc3578ce8c94cf'); '088c919cd8408005f255c411f786928385688a9e8fdb2db4c9bc3578ce8c94cf');
assert.equal(wtx.getSize(), 62138); assert.equal(wtx.getSize(), 62138);
assert.equal(wtx.getVirtualSize(), 61813); assert.equal(wtx.getVirtualSize(), 61813);
assert.equal(wtx.getCost(), 247250); assert.equal(wtx.getWeight(), 247250);
var raw1 = wtx.toRaw(); var raw1 = wtx.toRaw();
clearCache(wtx, true); clearCache(wtx, true);
var raw2 = wtx.toRaw(); var raw2 = wtx.toRaw();

View File

@ -440,7 +440,7 @@ describe('Wallet', function() {
assert.equal(t2.getFee(), 5250); assert.equal(t2.getFee(), 5250);
assert.equal(t2.getCost(), 2084); assert.equal(t2.getWeight(), 2084);
assert.equal(t2.getBaseSize(), 521); assert.equal(t2.getBaseSize(), 521);
assert.equal(t2.getSize(), 521); assert.equal(t2.getSize(), 521);
assert.equal(t2.getVirtualSize(), 521); assert.equal(t2.getVirtualSize(), 521);