Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the concept of virtual block size by block cost #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#define BITCOIN_CONSENSUS_CONSENSUS_H

/** The maximum allowed size for a serialized block, in bytes (network rule) */
static const unsigned int MAX_BLOCK_SIZE = 1000000;
static const unsigned int MAX_BLOCK_COST = 4000000;

/** The maximum allowed size for a serialized block, in bytes (network rule) */
static const unsigned int MAX_BLOCK_SIZE = MAX_BLOCK_COST/4;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3223,7 +3223,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
// Note: we aren't checking virtual size for blocks that aren't
// witness enabled, but that's okay because CheckBlock still
// checks the block size without any witnesses.
if (GetVirtualBlockSize(block) > MAX_BLOCK_SIZE) {
if (GetBlockCost(block) > MAX_BLOCK_COST) {
return state.DoS(100, error("ContextualCheckBlock(): witness size limits failed"), REJECT_INVALID, "bad-blk-wit-length");
}
return true;
Expand Down
17 changes: 9 additions & 8 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ std::string CBlock::ToString() const
return s.str();
}

size_t GetVirtualBlockSize(const CBlock& block)
{
// The intended approximate formula is: vsize = base_size + witness_size / 4.
// We can only serialize base or base+witness, so the formula
// becomes: vsize = base_size + (total_size - base_size) / 4 or
// vsize = (total_size + 3 * base_size) / 4, which we round up to
// vsize = (total_size + 3 * base_size + 3) / 4.
return (::GetSerializeSize(block, SER_NETWORK, 0) * 3 + ::GetSerializeSize(block, SER_NETWORK, SERIALIZE_TRANSACTION_WITNESS) + 3) / 4;
size_t GetBlockCost(const CBlock& block)
{
// coreSize is the size of block without witness data
size_t coreSize = ::GetSerializeSize(block, SER_NETWORK, 0);
size_t totalSize = ::GetSerializeSize(block, SER_NETWORK, SERIALIZE_TRANSACTION_WITNESS);
// size_t witSize = totalSize - coreSize;
// return witSize + 4 * coreSize;
// Since totalSize == witSize + coreSize, we can avoid a substraction
return totalSize + 3 * coreSize;
}
2 changes: 1 addition & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@ struct CBlockLocator
};

/** Compute the consensus-critical virtual block size. */
size_t GetVirtualBlockSize(const CBlock& tx);
size_t GetBlockCost(const CBlock& block);

#endif // BITCOIN_PRIMITIVES_BLOCK_H
2 changes: 1 addition & 1 deletion src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
result.push_back(Pair("confirmations", confirmations));
result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
result.push_back(Pair("wsize", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_WITNESS)));
result.push_back(Pair("vsize", (int)::GetVirtualBlockSize(block)));
result.push_back(Pair("bcost", (int)::GetBlockCost(block)));
result.push_back(Pair("height", blockindex->nHeight));
result.push_back(Pair("version", block.nVersion));
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
Expand Down