Skip to content

Commit cf08bbd

Browse files
committed
f'blockheaders include block height, nodes validate it'
1 parent 42cc5b1 commit cf08bbd

File tree

8 files changed

+28
-25
lines changed

8 files changed

+28
-25
lines changed

src/chain.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
#include <vector>
1616

17-
#include <util.h>
18-
1917
/**
2018
* Maximum amount of time that a block timestamp is allowed to exceed the
2119
* current network-adjusted time before the block will be accepted.
@@ -409,7 +407,9 @@ class CDiskBlockIndex : public CBlockIndex
409407
READWRITE(hashPrev);
410408
READWRITE(hashMerkleRoot);
411409
READWRITE(nTime);
412-
READWRITE(block_height);
410+
if (g_con_blockheightinheader) {
411+
READWRITE(block_height);
412+
}
413413
READWRITE(nBits);
414414
READWRITE(nNonce);
415415
}

src/chainparams.cpp

+5-17
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,7 @@ class CMainParams : public CChainParams {
125125

126126
genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
127127
consensus.hashGenesisBlock = genesis.GetHash();
128-
consensus.blockheight_in_header = gArgs.GetBoolArg("-con_blockheightinheader", false);
129-
// Serialization will not match if true
130-
if (!consensus.blockheight_in_header) {
131-
assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
132-
}
128+
assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
133129
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
134130

135131
// Note that of those which support the service bits prefix, most only support a subset of
@@ -238,11 +234,7 @@ class CTestNetParams : public CChainParams {
238234

239235
genesis = CreateGenesisBlock(1296688602, 414098458, 0x1d00ffff, 1, 50 * COIN);
240236
consensus.hashGenesisBlock = genesis.GetHash();
241-
consensus.blockheight_in_header = gArgs.GetBoolArg("-con_blockheightinheader", false);
242-
// Serialization will not match if true
243-
if (!consensus.blockheight_in_header) {
244-
assert(consensus.hashGenesisBlock == uint256S("0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
245-
}
237+
assert(consensus.hashGenesisBlock == uint256S("0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
246238
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
247239

248240
vFixedSeeds.clear();
@@ -333,11 +325,7 @@ class CRegTestParams : public CChainParams {
333325

334326
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
335327
consensus.hashGenesisBlock = genesis.GetHash();
336-
consensus.blockheight_in_header = gArgs.GetBoolArg("-con_blockheightinheader", false);
337-
// Serialization will not match if true
338-
if (!consensus.blockheight_in_header) {
339-
assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
340-
}
328+
assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
341329
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
342330

343331
vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
@@ -440,9 +428,9 @@ class CCustomParams : public CRegTestParams {
440428
consensus.nMinimumChainWork = uint256S(args.GetArg("-con_nminimumchainwork", "0x0"));
441429
consensus.defaultAssumeValid = uint256S(args.GetArg("-con_defaultassumevalid", "0x00"));
442430

443-
// Note: This gArg is accessed one more time in block.h for serialization to avoid
431+
// Note: g_con_blockheightinheader is used instead of consensus.blockheight_in_header to avoid
444432
// circular dependency
445-
consensus.blockheight_in_header = gArgs.GetBoolArg("-con_blockheightinheader", false);
433+
consensus.blockheight_in_header = g_con_blockheightinheader = gArgs.GetBoolArg("-con_blockheightinheader", false);
446434

447435
// All non-zero coinbase outputs must go to this scriptPubKey
448436
std::vector<unsigned char> man_bytes = ParseHex(gArgs.GetArg("-con_mandatorycoinbase", ""));

src/consensus/params.h

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct Params {
7878
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
7979
uint256 nMinimumChainWork;
8080
uint256 defaultAssumeValid;
81+
8182
// Elements-specific chainparams
8283
CScript mandatory_coinbase_destination;
8384
bool blockheight_in_header;

src/miner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
168168
// Fill in header
169169
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
170170
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
171-
if (Params().GetConsensus().blockheight_in_header) {
171+
if (g_con_blockheightinheader) {
172172
pblock->block_height = nHeight;
173173
}
174174
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());

src/primitives/block.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <utilstrencodings.h>
1111
#include <crypto/common.h>
1212

13+
bool g_con_blockheightinheader = false;
14+
1315
uint256 CBlockHeader::GetHash() const
1416
{
1517
return SerializeHash(*this);

src/primitives/block.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#include <primitives/transaction.h>
1010
#include <serialize.h>
1111
#include <uint256.h>
12-
#include <util.h>
12+
13+
/**
14+
* Also found in consensus params as blockheight_in_header (circular dep otherwise)
15+
*/
16+
extern bool g_con_blockheightinheader;
1317

1418
/** Nodes collect new transactions into a block, hash them into a hash tree,
1519
* and scan through nonce values to make the block's hash satisfy proof-of-work
@@ -45,8 +49,7 @@ class CBlockHeader
4549
READWRITE(hashPrevBlock);
4650
READWRITE(hashMerkleRoot);
4751
READWRITE(nTime);
48-
// Also found in consensus params as blockheight_in_header (circular dep otherwise)
49-
if (gArgs.GetBoolArg("-con_blockheightinheader", false)) {
52+
if (g_con_blockheightinheader) {
5053
READWRITE(block_height);
5154
}
5255
READWRITE(nBits);

src/validation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3264,7 +3264,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationSta
32643264
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
32653265

32663266
// Check height in header against prev
3267-
if (consensusParams.blockheight_in_header && (uint32_t)nHeight != block.block_height)
3267+
if (g_con_blockheightinheader && (uint32_t)nHeight != block.block_height)
32683268
return state.Invalid(error("%s: block height in header is incorrect", __func__),
32693269
REJECT_INVALID, "bad-header-height");
32703270

test/functional/feature_genesishash.py

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
'-con_genesis_style=default_style',
2727
],
2828
},
29+
30+
{
31+
'memo': 'default_style with height in header',
32+
'genesis': 'be878d13f0b4fd63c9e3d07c8a09a1f47f4a915e017a4648fff6bdfda751189e',
33+
'args': [
34+
'-con_genesis_style=default_style',
35+
'-con_blockheightinheader',
36+
],
37+
},
2938
]
3039

3140
class GenesisHashTest(BitcoinTestFramework):

0 commit comments

Comments
 (0)