Skip to content

Commit ca7550e

Browse files
committed
Merge #8363: Rename "block cost" to "block weight"
2c06bae Rename "block cost" to "block weight" (Suhas Daftuar)
2 parents 5e3557b + 2c06bae commit ca7550e

File tree

18 files changed

+69
-69
lines changed

18 files changed

+69
-69
lines changed

qa/rpc-tests/p2p-segwit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,12 +1065,12 @@ def test_block_relay(self, segwit_activated):
10651065
assert_equal(wit_block.serialize(False), non_wit_block.serialize())
10661066
assert_equal(wit_block.serialize(True), block.serialize(True))
10671067

1068-
# Test size, vsize, cost
1068+
# Test size, vsize, weight
10691069
rpc_details = self.nodes[0].getblock(block.hash, True)
10701070
assert_equal(rpc_details["size"], len(block.serialize(True)))
10711071
assert_equal(rpc_details["strippedsize"], len(block.serialize(False)))
1072-
cost = 3*len(block.serialize(False)) + len(block.serialize(True))
1073-
assert_equal(rpc_details["cost"], cost)
1072+
weight = 3*len(block.serialize(False)) + len(block.serialize(True))
1073+
assert_equal(rpc_details["weight"], weight)
10741074

10751075
# Upgraded node should not ask for blocks from unupgraded
10761076
block4 = self.build_next_block(nVersion=4)

src/consensus/consensus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
1212
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
13-
/** The maximum allowed cost for a block, see BIP 141 (network rule) */
14-
static const unsigned int MAX_BLOCK_COST = 4000000;
13+
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
14+
static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
1515
/** The maximum allowed size for a block excluding witness data, in bytes (network rule) */
1616
static const unsigned int MAX_BLOCK_BASE_SIZE = 1000000;
1717
/** The maximum allowed number of signature check operations in a block (network rule) */

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ std::string HelpMessage(HelpMessageMode mode)
452452
strUsage += HelpMessageOpt("-mempoolreplacement", strprintf(_("Enable transaction replacement in the memory pool (default: %u)"), DEFAULT_ENABLE_REPLACEMENT));
453453

454454
strUsage += HelpMessageGroup(_("Block creation options:"));
455-
strUsage += HelpMessageOpt("-blockmaxcost=<n>", strprintf(_("Set maximum BIP141 block cost (default: %d)"), DEFAULT_BLOCK_MAX_COST));
455+
strUsage += HelpMessageOpt("-blockmaxweight=<n>", strprintf(_("Set maximum BIP141 block weight (default: %d)"), DEFAULT_BLOCK_MAX_WEIGHT));
456456
strUsage += HelpMessageOpt("-blockmaxsize=<n>", strprintf(_("Set maximum block size in bytes (default: %d)"), DEFAULT_BLOCK_MAX_SIZE));
457457
strUsage += HelpMessageOpt("-blockprioritysize=<n>", strprintf(_("Set maximum size of high-priority/low-fee transactions in bytes (default: %d)"), DEFAULT_BLOCK_PRIORITY_SIZE));
458458
if (showDebug)

src/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(c
694694
// have been mined or received.
695695
// 100 orphans, each of which is at most 99,999 bytes big is
696696
// at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
697-
unsigned int sz = GetTransactionCost(tx);
698-
if (sz >= MAX_STANDARD_TX_COST)
697+
unsigned int sz = GetTransactionWeight(tx);
698+
if (sz >= MAX_STANDARD_TX_WEIGHT)
699699
{
700700
LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
701701
return false;
@@ -3596,13 +3596,13 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
35963596
}
35973597

35983598
// After the coinbase witness nonce and commitment are verified,
3599-
// we can check if the block cost passes (before we've checked the
3600-
// coinbase witness, it would be possible for the cost to be too
3599+
// we can check if the block weight passes (before we've checked the
3600+
// coinbase witness, it would be possible for the weight to be too
36013601
// large by filling up the coinbase witness, which doesn't change
36023602
// the block hash, so we couldn't mark the block as permanently
36033603
// failed).
3604-
if (GetBlockCost(block) > MAX_BLOCK_COST) {
3605-
return state.DoS(100, error("ContextualCheckBlock(): cost limit failed"), REJECT_INVALID, "bad-blk-cost");
3604+
if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
3605+
return state.DoS(100, error("ContextualCheckBlock(): weight limit failed"), REJECT_INVALID, "bad-blk-weight");
36063606
}
36073607

36083608
return true;

src/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ typedef boost::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
155155
extern BlockMap mapBlockIndex;
156156
extern uint64_t nLastBlockTx;
157157
extern uint64_t nLastBlockSize;
158-
extern uint64_t nLastBlockCost;
158+
extern uint64_t nLastBlockWeight;
159159
extern const std::string strMessageMagic;
160160
extern CWaitableCriticalSection csBestBlock;
161161
extern CConditionVariable cvBlockChange;

src/miner.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ using namespace std;
4545

4646
uint64_t nLastBlockTx = 0;
4747
uint64_t nLastBlockSize = 0;
48-
uint64_t nLastBlockCost = 0;
48+
uint64_t nLastBlockWeight = 0;
4949

5050
class ScoreCompare
5151
{
@@ -77,30 +77,30 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
7777
: chainparams(_chainparams)
7878
{
7979
// Block resource limits
80-
// If neither -blockmaxsize or -blockmaxcost is given, limit to DEFAULT_BLOCK_MAX_*
80+
// If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
8181
// If only one is given, only restrict the specified resource.
8282
// If both are given, restrict both.
83-
nBlockMaxCost = DEFAULT_BLOCK_MAX_COST;
83+
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
8484
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
85-
bool fCostSet = false;
86-
if (mapArgs.count("-blockmaxcost")) {
87-
nBlockMaxCost = GetArg("-blockmaxcost", DEFAULT_BLOCK_MAX_COST);
85+
bool fWeightSet = false;
86+
if (mapArgs.count("-blockmaxweight")) {
87+
nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
8888
nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
89-
fCostSet = true;
89+
fWeightSet = true;
9090
}
9191
if (mapArgs.count("-blockmaxsize")) {
9292
nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
93-
if (!fCostSet) {
94-
nBlockMaxCost = nBlockMaxSize * WITNESS_SCALE_FACTOR;
93+
if (!fWeightSet) {
94+
nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR;
9595
}
9696
}
9797

98-
// Limit cost to between 4K and MAX_BLOCK_COST-4K for sanity:
99-
nBlockMaxCost = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_COST-4000), nBlockMaxCost));
98+
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
99+
nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_WEIGHT-4000), nBlockMaxWeight));
100100
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
101101
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
102102

103-
// Whether we need to account for byte usage (in addition to cost usage)
103+
// Whether we need to account for byte usage (in addition to weight usage)
104104
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000);
105105
}
106106

@@ -110,7 +110,7 @@ void BlockAssembler::resetBlock()
110110

111111
// Reserve space for coinbase tx
112112
nBlockSize = 1000;
113-
nBlockCost = 4000;
113+
nBlockWeight = 4000;
114114
nBlockSigOpsCost = 400;
115115
fIncludeWitness = false;
116116

@@ -167,7 +167,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
167167

168168
nLastBlockTx = nBlockTx;
169169
nLastBlockSize = nBlockSize;
170-
nLastBlockCost = nBlockCost;
170+
nLastBlockWeight = nBlockWeight;
171171
LogPrintf("CreateNewBlock(): total size %u txs: %u fees: %ld sigops %d\n", nBlockSize, nBlockTx, nFees, nBlockSigOpsCost);
172172

173173
// Create coinbase transaction.
@@ -223,8 +223,8 @@ void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
223223

224224
bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost)
225225
{
226-
// TODO: switch to cost-based accounting for packages instead of vsize-based accounting.
227-
if (nBlockCost + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxCost)
226+
// TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
227+
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
228228
return false;
229229
if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST)
230230
return false;
@@ -257,17 +257,17 @@ bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& packa
257257

258258
bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)
259259
{
260-
if (nBlockCost + iter->GetTxCost() >= nBlockMaxCost) {
260+
if (nBlockWeight + iter->GetTxWeight() >= nBlockMaxWeight) {
261261
// If the block is so close to full that no more txs will fit
262262
// or if we've tried more than 50 times to fill remaining space
263263
// then flag that the block is finished
264-
if (nBlockCost > nBlockMaxCost - 400 || lastFewTxs > 50) {
264+
if (nBlockWeight > nBlockMaxWeight - 400 || lastFewTxs > 50) {
265265
blockFinished = true;
266266
return false;
267267
}
268-
// Once we're within 4000 cost of a full block, only look at 50 more txs
268+
// Once we're within 4000 weight of a full block, only look at 50 more txs
269269
// to try to fill the remaining space.
270-
if (nBlockCost > nBlockMaxCost - 4000) {
270+
if (nBlockWeight > nBlockMaxWeight - 4000) {
271271
lastFewTxs++;
272272
}
273273
return false;
@@ -315,7 +315,7 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
315315
if (fNeedSizeAccounting) {
316316
nBlockSize += ::GetSerializeSize(iter->GetTx(), SER_NETWORK, PROTOCOL_VERSION);
317317
}
318-
nBlockCost += iter->GetTxCost();
318+
nBlockWeight += iter->GetTxWeight();
319319
++nBlockTx;
320320
nBlockSigOpsCost += iter->GetSigOpCost();
321321
nFees += iter->GetFee();

src/miner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ class BlockAssembler
141141

142142
// Configuration parameters for the block size
143143
bool fIncludeWitness;
144-
unsigned int nBlockMaxCost, nBlockMaxSize;
144+
unsigned int nBlockMaxWeight, nBlockMaxSize;
145145
bool fNeedSizeAccounting;
146146

147147
// Information on the current status of the block
148-
uint64_t nBlockCost;
148+
uint64_t nBlockWeight;
149149
uint64_t nBlockSize;
150150
uint64_t nBlockTx;
151151
uint64_t nBlockSigOpsCost;

src/policy/policy.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason)
6464
// almost as much to process as they cost the sender in fees, because
6565
// computing signature hashes is O(ninputs*txsize). Limiting transactions
6666
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
67-
unsigned int sz = GetTransactionCost(tx);
68-
if (sz >= MAX_STANDARD_TX_COST) {
67+
unsigned int sz = GetTransactionWeight(tx);
68+
if (sz >= MAX_STANDARD_TX_WEIGHT) {
6969
reason = "tx-size";
7070
return false;
7171
}
@@ -151,12 +151,12 @@ bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
151151
return true;
152152
}
153153

154-
int64_t GetVirtualTransactionSize(int64_t nCost)
154+
int64_t GetVirtualTransactionSize(int64_t nWeight)
155155
{
156-
return (nCost + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
156+
return (nWeight + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
157157
}
158158

159159
int64_t GetVirtualTransactionSize(const CTransaction& tx)
160160
{
161-
return GetVirtualTransactionSize(GetTransactionCost(tx));
161+
return GetVirtualTransactionSize(GetTransactionWeight(tx));
162162
}

src/policy/policy.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class CCoinsViewCache;
1818
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000;
1919
/** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
2020
static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 0;
21-
/** Default for -blockmaxcost, which control the range of block costs the mining code will create **/
22-
static const unsigned int DEFAULT_BLOCK_MAX_COST = 3000000;
23-
/** The maximum size for transactions we're willing to relay/mine */
24-
static const unsigned int MAX_STANDARD_TX_COST = 400000;
21+
/** Default for -blockmaxweight, which controls the range of block weights the mining code will create **/
22+
static const unsigned int DEFAULT_BLOCK_MAX_WEIGHT = 3000000;
23+
/** The maximum weight for transactions we're willing to relay/mine */
24+
static const unsigned int MAX_STANDARD_TX_WEIGHT = 400000;
2525
/** Maximum number of signature check operations in an IsStandard() P2SH script */
2626
static const unsigned int MAX_P2SH_SIGOPS = 15;
2727
/** The maximum number of sigops we're willing to relay/mine in a single tx */
@@ -66,8 +66,8 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason);
6666
*/
6767
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);
6868

69-
/** Compute the virtual transaction size (cost reinterpreted as bytes). */
70-
int64_t GetVirtualTransactionSize(int64_t nCost);
69+
/** Compute the virtual transaction size (weight reinterpreted as bytes). */
70+
int64_t GetVirtualTransactionSize(int64_t nWeight);
7171
int64_t GetVirtualTransactionSize(const CTransaction& tx);
7272

7373
#endif // BITCOIN_POLICY_POLICY_H

src/primitives/block.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ std::string CBlock::ToString() const
3232
return s.str();
3333
}
3434

35-
int64_t GetBlockCost(const CBlock& block)
35+
int64_t GetBlockWeight(const CBlock& block)
3636
{
37-
// This implements the cost = (stripped_size * 4) + witness_size formula,
37+
// This implements the weight = (stripped_size * 4) + witness_size formula,
3838
// using only serialization with and without witness data. As witness_size
3939
// is equal to total_size - stripped_size, this formula is identical to:
40-
// cost = (stripped_size * 3) + total_size.
40+
// weight = (stripped_size * 3) + total_size.
4141
return ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
4242
}

0 commit comments

Comments
 (0)