Skip to content

Commit 3f9ffa1

Browse files
committed
Replace the concept of virtual block size, introduce the concept of block cost, and express CBS as a function of it
1 parent 0f9efb0 commit 3f9ffa1

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/consensus/consensus.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#define BITCOIN_CONSENSUS_CONSENSUS_H
88

99
/** The maximum allowed size for a serialized block, in bytes (network rule) */
10-
static const unsigned int MAX_BLOCK_SIZE = 1000000;
10+
static const unsigned int MAX_BLOCK_COST = 4000000;
11+
12+
/** The maximum allowed size for a serialized block, in bytes (network rule) */
13+
static const unsigned int MAX_BLOCK_SIZE = MAX_BLOCK_COST/4;
1114
/** The maximum allowed number of signature check operations in a block (network rule) */
1215
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
1316
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3004,7 +3004,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
30043004
// because we receive the wrong transactions for it.
30053005

30063006
// Size limits
3007-
if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || GetVirtualBlockSize(block) > MAX_BLOCK_SIZE)
3007+
if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || GetBlockCost(block) > MAX_BLOCK_COST)
30083008
return state.DoS(100, error("CheckBlock(): size limits failed"),
30093009
REJECT_INVALID, "bad-blk-length");
30103010

src/primitives/block.cpp

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

35-
size_t GetVirtualBlockSize(const CBlock& block)
36-
{
37-
// The formula is: vsize = base_size + witness_size / 4.
38-
// We can only serialize base or totalbase+witness, however, so the formula
39-
// becomes: vsize = base_size + (total_size - base_size) / 4 or
40-
// vsize = (total_size + 3 * base_size) / 4.
41-
return (::GetSerializeSize(block, SER_NETWORK, 0) * 3 + ::GetSerializeSize(block, SER_NETWORK, SERIALIZE_TRANSACTION_WITNESS) + 3) / 4;
35+
size_t GetBlockCost(const CBlock& block)
36+
{
37+
// coreSize is the size of block without witness data
38+
size_t coreSize = ::GetSerializeSize(block, SER_NETWORK, 0);
39+
size_t totalSize = ::GetSerializeSize(block, SER_NETWORK, SERIALIZE_TRANSACTION_WITNESS);
40+
// size_t witSize = totalSize - coreSize;
41+
// return witSize + 4 * coreSize;
42+
// Since totalSize == witSize + coreSize, we can avoid a substraction
43+
return totalSize + 3 * coreSize;
4244
}

0 commit comments

Comments
 (0)