refactor: rename cost to weight.
This commit is contained in:
parent
fbb7f25d0a
commit
07285ebdd7
@ -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).
|
||||
if (block.getCost() > constants.block.MAX_COST) {
|
||||
if (block.getWeight() > constants.block.MAX_WEIGHT) {
|
||||
return callback(new VerifyError(block,
|
||||
'invalid',
|
||||
'bad-blk-cost',
|
||||
'bad-blk-weight',
|
||||
100));
|
||||
}
|
||||
|
||||
@ -709,9 +709,9 @@ Chain.prototype.checkInputs = function checkInputs(block, prev, state, callback)
|
||||
}
|
||||
|
||||
// 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,
|
||||
'invalid',
|
||||
'bad-blk-sigops',
|
||||
|
||||
@ -940,7 +940,7 @@ RPC.prototype._blockToJSON = function _blockToJSON(entry, block, txDetails, call
|
||||
confirmations: self.chain.height - entry.height + 1,
|
||||
strippedsize: block.getBaseSize(),
|
||||
size: block.getSize(),
|
||||
weight: block.getCost(),
|
||||
weight: block.getWeight(),
|
||||
height: entry.height,
|
||||
version: entry.version,
|
||||
merkleroot: utils.revHex(entry.merkleRoot),
|
||||
@ -1468,7 +1468,7 @@ RPC.prototype.getblocktemplate = function getblocktemplate(args, callback) {
|
||||
depends: deps,
|
||||
fee: tx.getFee(),
|
||||
sigops: tx.getSigops(),
|
||||
weight: tx.getCost()
|
||||
weight: tx.getWeight()
|
||||
});
|
||||
}
|
||||
|
||||
@ -1533,9 +1533,9 @@ RPC.prototype.getblocktemplate = function getblocktemplate(args, callback) {
|
||||
mintime: attempt.ts,
|
||||
mutable: mutable,
|
||||
noncerange: '00000000ffffffff',
|
||||
sigoplimit: constants.block.MAX_SIGOPS_COST,
|
||||
sigoplimit: constants.block.MAX_SIGOPS_WEIGHT,
|
||||
sizelimit: constants.block.MAX_SIZE,
|
||||
weightlimit: constants.block.MAX_COST,
|
||||
weightlimit: constants.block.MAX_WEIGHT,
|
||||
curtime: block.ts,
|
||||
bits: String(block.bits),
|
||||
height: attempt.height,
|
||||
|
||||
@ -827,7 +827,7 @@ Mempool.prototype.verify = function verify(entry, callback) {
|
||||
0));
|
||||
}
|
||||
|
||||
if (tx.getSigopsCost(flags) > constants.tx.MAX_SIGOPS_COST) {
|
||||
if (tx.getSigopsWeight(flags) > constants.tx.MAX_SIGOPS_WEIGHT) {
|
||||
return callback(new VerifyError(tx,
|
||||
'nonstandard',
|
||||
'bad-txns-too-many-sigops',
|
||||
|
||||
@ -209,13 +209,13 @@ MinerBlock.prototype.updateMerkle = function updateMerkle() {
|
||||
*/
|
||||
|
||||
MinerBlock.prototype.addTX = function addTX(tx) {
|
||||
var cost;
|
||||
var weight;
|
||||
|
||||
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;
|
||||
|
||||
if (this.block.hasTX(tx))
|
||||
|
||||
@ -178,15 +178,15 @@ Block.prototype.getSizes = function getSizes() {
|
||||
|
||||
Block.prototype.getVirtualSize = function getVirtualSize() {
|
||||
var scale = constants.WITNESS_SCALE_FACTOR;
|
||||
return (this.getCost() + scale - 1) / scale | 0;
|
||||
return (this.getWeight() + scale - 1) / scale | 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate block cost.
|
||||
* @returns {Number} cost
|
||||
* Calculate block weight.
|
||||
* @returns {Number} weight
|
||||
*/
|
||||
|
||||
Block.prototype.getCost = function getCost() {
|
||||
Block.prototype.getWeight = function getWeight() {
|
||||
var sizes = this.getSizes();
|
||||
var base = sizes.size - sizes.witnessSize;
|
||||
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)
|
||||
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.score = 100;
|
||||
return false;
|
||||
|
||||
@ -353,16 +353,16 @@ TX.prototype.getSizes = function getSizes() {
|
||||
|
||||
TX.prototype.getVirtualSize = function getVirtualSize() {
|
||||
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.
|
||||
* @returns {Number} cost
|
||||
* @returns {Number} weight
|
||||
*/
|
||||
|
||||
TX.prototype.getCost = function getCost() {
|
||||
TX.prototype.getWeight = function getWeight() {
|
||||
var sizes = this.getSizes();
|
||||
var base = sizes.size - sizes.witnessSize;
|
||||
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
|
||||
* @returns {Number} sigop cost
|
||||
* @returns {Number} sigop weight
|
||||
*/
|
||||
|
||||
TX.prototype.getSigopsCost = function getSigopsCost(flags) {
|
||||
var cost = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR;
|
||||
TX.prototype.getSigopsWeight = function getSigopsWeight(flags) {
|
||||
var weight = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR;
|
||||
var input, i;
|
||||
|
||||
if (flags == null)
|
||||
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
||||
|
||||
if (this.isCoinbase())
|
||||
return cost;
|
||||
return weight;
|
||||
|
||||
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++) {
|
||||
input = this.inputs[i];
|
||||
@ -1110,14 +1110,14 @@ TX.prototype.getSigopsCost = function getSigopsCost(flags) {
|
||||
if (!input.coin)
|
||||
continue;
|
||||
|
||||
cost += Script.getWitnessSigops(
|
||||
weight += Script.getWitnessSigops(
|
||||
input.script,
|
||||
input.coin.script,
|
||||
input.witness,
|
||||
flags);
|
||||
}
|
||||
|
||||
return cost;
|
||||
return weight;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1132,7 +1132,7 @@ TX.prototype.getSigops = function getSigops(flags) {
|
||||
if (flags == null)
|
||||
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;
|
||||
}
|
||||
|
||||
if (this.getCost() >= constants.tx.MAX_COST) {
|
||||
if (this.getWeight() >= constants.tx.MAX_WEIGHT) {
|
||||
ret.reason = 'tx-size';
|
||||
ret.score = 0;
|
||||
return false;
|
||||
|
||||
@ -385,9 +385,9 @@ exports.WITNESS_SCALE_FACTOR = 4;
|
||||
|
||||
exports.block = {
|
||||
MAX_SIZE: 1000000,
|
||||
MAX_COST: 4000000,
|
||||
MAX_WEIGHT: 4000000,
|
||||
MAX_SIGOPS: 1000000 / 50,
|
||||
MAX_SIGOPS_COST: 80000,
|
||||
MAX_SIGOPS_WEIGHT: 80000,
|
||||
MEDIAN_TIMESPAN: 11,
|
||||
BIP16_TIME: 1333238400,
|
||||
SIGHASH_LIMIT: 1300000000
|
||||
@ -414,14 +414,14 @@ exports.bip30 = {
|
||||
exports.tx = {
|
||||
MAX_VERSION: 2,
|
||||
MAX_SIZE: 100000,
|
||||
MAX_COST: 400000,
|
||||
MAX_WEIGHT: 400000,
|
||||
MIN_FEE: 10000,
|
||||
MAX_FEE: exports.COIN / 10,
|
||||
MIN_RELAY: 10000,
|
||||
BARE_MULTISIG: true,
|
||||
FREE_THRESHOLD: exports.COIN * 144 / 250,
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
@ -143,7 +143,7 @@ describe('Block', function() {
|
||||
assert(block.txs[0].isCoinbase());
|
||||
assert(block.txs[0].isSane());
|
||||
assert(!block.hasWitness());
|
||||
assert.equal(block.getCost(), 1136924);
|
||||
assert.equal(block.getWeight(), 1136924);
|
||||
var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG;
|
||||
for (var i = 1; i < block.txs.length; i++) {
|
||||
var tx = block.txs[i];
|
||||
|
||||
@ -162,7 +162,7 @@ describe('TX', function() {
|
||||
'088c919cd8408005f255c411f786928385688a9e8fdb2db4c9bc3578ce8c94cf');
|
||||
assert.equal(wtx.getSize(), 62138);
|
||||
assert.equal(wtx.getVirtualSize(), 61813);
|
||||
assert.equal(wtx.getCost(), 247250);
|
||||
assert.equal(wtx.getWeight(), 247250);
|
||||
var raw1 = wtx.toRaw();
|
||||
clearCache(wtx, true);
|
||||
var raw2 = wtx.toRaw();
|
||||
|
||||
@ -440,7 +440,7 @@ describe('Wallet', function() {
|
||||
|
||||
assert.equal(t2.getFee(), 5250);
|
||||
|
||||
assert.equal(t2.getCost(), 2084);
|
||||
assert.equal(t2.getWeight(), 2084);
|
||||
assert.equal(t2.getBaseSize(), 521);
|
||||
assert.equal(t2.getSize(), 521);
|
||||
assert.equal(t2.getVirtualSize(), 521);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user