Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: x3m on September 21, 2017, 04:07:27 PM



Title: Block size limit in source code
Post by: x3m on September 21, 2017, 04:07:27 PM
Can anyone please tell me where the heck is the maximum block size (1M) defined and where it is used in the code to check? Thanks.


Title: Re: Block size limit in source code
Post by: Carlton Banks on September 21, 2017, 05:23:09 PM
Can anyone please tell me where the heck is the maximum block size (1M) defined and where it is used in the code to check? Thanks.

It's a variable called MAX_BLOCK_WEIGHT, and it's in a file in the consensus sub-directory called consensus.h (and it's 4M, not 1M)


Title: Re: Block size limit in source code
Post by: BitUsher on September 21, 2017, 05:51:32 PM
Can anyone please tell me where the heck is the maximum block size (1M) defined and where it is used in the code to check? Thanks.

Limit is no longer 1MB , and no longer defined by size but weight of 4 million units-

https://github.com/bitcoin/bitcoin/blob/master/src/consensus/consensus.h

Quote from: Bitcoin Github
/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const int64_t MAX_BLOCK_SIGOPS_COST = 80000;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 100;

This allows Blocks up to 4MB theoretically , but more realistic max blocksizes will be an occasionally rare 3.7MB (assuming near 100% segwit txs)

https://testnet.smartbit.com.au/block/00000000000016a805a7c5d27c3cc0ecb6d51372e15919dfb49d24bd56ae0a8b

90% and up segwit tx usage will result in 1.9-2.1MB average blocksizes based upon the type of tx mix we see today.


Title: Re: Block size limit in source code
Post by: x3m on September 21, 2017, 06:05:45 PM
Thanks a lot BitUsher and Carlton Banks.


Title: Re: Block size limit in source code
Post by: Carlton Banks on September 21, 2017, 07:17:42 PM
This allows Blocks up to 4MB theoretically , but more realistic max blocksizes will be an occasionally rare 3.7MB (assuming near 100% segwit txs)

https://testnet.smartbit.com.au/block/00000000000016a805a7c5d27c3cc0ecb6d51372e15919dfb49d24bd56ae0a8b

90% and up segwit tx usage will result in 1.9-2.1MB average blocksizes based upon the type of tx mix we see today.

I think it may be sensible to point out that "we see today" is an important condition of your statement. In future, Lightning, CT and MAST transactions could push that average higher.


Title: Re: Block size limit in source code
Post by: DannyHamilton on September 21, 2017, 08:47:35 PM
Note that there is also a:
Code:
static const int WITNESS_SCALE_FACTOR = 4;
https://github.com/bitcoin/bitcoin/blob/505955052e60e0681865f3064e005ca0d3aa90bf/src/consensus/consensus.h#L21



And when validating the size of a received block, the following code is used:
Code:
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
{
// These are checks that are independent of context.

. . .

// Size limits
    if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
https://github.com/bitcoin/bitcoin/blob/f088a1bb392eaecd912ff9bca6967a8f4765c2b7/src/validation.cpp#L2814


So, if
block.vtx.size() is greater than 1000000
then
block.vtx.size() * WITNESS_SCALE_FACTOR will be greater than 4000000

which is the value of MAX_BLOCK_WEIGHT

As such, any block such that block.vtx.size() is larger than 1 megabyte will be rejected as invalid.


Title: Re: Block size limit in source code
Post by: x3m on September 22, 2017, 11:39:50 AM
Wow, thanks DannyHamilton ... legendary explanation!
Replies like this help a lot to understand the logic in the code.