Skip to content

Commit 2a12b30

Browse files
committedOct 12, 2018
blockheaders include block height, nodes validate it
1 parent e1ed37e commit 2a12b30

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed
 

‎src/chain.h

+6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class CBlockIndex
210210
int32_t nVersion;
211211
uint256 hashMerkleRoot;
212212
uint32_t nTime;
213+
uint32_t block_height;
213214
uint32_t nBits;
214215
uint32_t nNonce;
215216

@@ -238,6 +239,7 @@ class CBlockIndex
238239
nVersion = 0;
239240
hashMerkleRoot = uint256();
240241
nTime = 0;
242+
block_height = 0;
241243
nBits = 0;
242244
nNonce = 0;
243245
}
@@ -254,6 +256,7 @@ class CBlockIndex
254256
nVersion = block.nVersion;
255257
hashMerkleRoot = block.hashMerkleRoot;
256258
nTime = block.nTime;
259+
block_height = block.block_height;
257260
nBits = block.nBits;
258261
nNonce = block.nNonce;
259262
}
@@ -284,6 +287,7 @@ class CBlockIndex
284287
block.hashPrevBlock = pprev->GetBlockHash();
285288
block.hashMerkleRoot = hashMerkleRoot;
286289
block.nTime = nTime;
290+
block.block_height = block_height;
287291
block.nBits = nBits;
288292
block.nNonce = nNonce;
289293
return block;
@@ -403,6 +407,7 @@ class CDiskBlockIndex : public CBlockIndex
403407
READWRITE(hashPrev);
404408
READWRITE(hashMerkleRoot);
405409
READWRITE(nTime);
410+
READWRITE(block_height);
406411
READWRITE(nBits);
407412
READWRITE(nNonce);
408413
}
@@ -414,6 +419,7 @@ class CDiskBlockIndex : public CBlockIndex
414419
block.hashPrevBlock = hashPrev;
415420
block.hashMerkleRoot = hashMerkleRoot;
416421
block.nTime = nTime;
422+
block.block_height = block_height;
417423
block.nBits = nBits;
418424
block.nNonce = nNonce;
419425
return block.GetHash();

‎src/chainparams.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class CMainParams : public CChainParams {
121121

122122
genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
123123
consensus.hashGenesisBlock = genesis.GetHash();
124-
assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
125124
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
126125

127126
// Note that of those which support the service bits prefix, most only support a subset of
@@ -230,7 +229,6 @@ class CTestNetParams : public CChainParams {
230229

231230
genesis = CreateGenesisBlock(1296688602, 414098458, 0x1d00ffff, 1, 50 * COIN);
232231
consensus.hashGenesisBlock = genesis.GetHash();
233-
assert(consensus.hashGenesisBlock == uint256S("0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
234232
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
235233

236234
vFixedSeeds.clear();
@@ -319,7 +317,6 @@ class CRegTestParams : public CChainParams {
319317

320318
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
321319
consensus.hashGenesisBlock = genesis.GetHash();
322-
assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
323320
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
324321

325322
vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.

‎src/miner.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +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+
pblock->block_height = nHeight;
171172
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
172173
pblock->nNonce = 0;
173174
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);

‎src/primitives/block.h

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class CBlockHeader
2525
uint256 hashPrevBlock;
2626
uint256 hashMerkleRoot;
2727
uint32_t nTime;
28+
// Height in header as well as in coinbase for easier hsm validation
29+
uint32_t block_height;
2830
uint32_t nBits;
2931
uint32_t nNonce;
3032

@@ -41,6 +43,7 @@ class CBlockHeader
4143
READWRITE(hashPrevBlock);
4244
READWRITE(hashMerkleRoot);
4345
READWRITE(nTime);
46+
READWRITE(block_height);
4447
READWRITE(nBits);
4548
READWRITE(nNonce);
4649
}
@@ -51,6 +54,7 @@ class CBlockHeader
5154
hashPrevBlock.SetNull();
5255
hashMerkleRoot.SetNull();
5356
nTime = 0;
57+
block_height = 0;
5458
nBits = 0;
5559
nNonce = 0;
5660
}
@@ -111,6 +115,7 @@ class CBlock : public CBlockHeader
111115
block.hashPrevBlock = hashPrevBlock;
112116
block.hashMerkleRoot = hashMerkleRoot;
113117
block.nTime = nTime;
118+
block.block_height = block_height;
114119
block.nBits = nBits;
115120
block.nNonce = nNonce;
116121
return block;

‎src/validation.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -3249,6 +3249,11 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationSta
32493249
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
32503250
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
32513251

3252+
// Check height in header against prev
3253+
if ((uint32_t)nHeight != block.block_height)
3254+
return state.Invalid(error("%s: block height in header is incorrect", __func__),
3255+
REJECT_INVALID, "bad-header-height");
3256+
32523257
// Check timestamp
32533258
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
32543259
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");

0 commit comments

Comments
 (0)