Skip to content

Commit bd02b16

Browse files
authored
Merge pull request #1622 from cyrossignol/remove-block-nonce
Remove block nonce for version 11
2 parents 8d5faed + 58cf974 commit bd02b16

File tree

2 files changed

+100
-49
lines changed

2 files changed

+100
-49
lines changed

src/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,8 @@ bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
16231623
{
16241624
if (!fReadTransactions)
16251625
{
1626-
*this = pindex->GetBlockHeader();
1626+
SetNull();
1627+
*(static_cast<CBlockHeader*>(this)) = pindex->GetBlockHeader();
16271628
return true;
16281629
}
16291630
if (!ReadFromDisk(pindex->nFile, pindex->nBlockPos, fReadTransactions))
@@ -4940,7 +4941,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
49404941
pindex = pindex->pnext;
49414942
}
49424943

4943-
vector<CBlock> vHeaders;
4944+
vector<CBlockHeader> vHeaders;
49444945
int nLimit = 1000;
49454946
LogPrintf("getheaders %d to %s", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,20));
49464947
for (; pindex; pindex = pindex->pnext)

src/main.h

+97-47
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static const uint256 hashGenesisBlockTestNet = uint256S("0x00006e037d7b84104208e
7373
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7474
inline bool IsProtocolV2(int nHeight)
7575
{
76-
return (fTestNet ? nHeight > 2060 : nHeight > 85400);
76+
return (fTestNet ? nHeight > 2060 : nHeight > 85400);
7777
}
7878

7979
inline bool IsResearchAgeEnabled(int nHeight)
@@ -119,7 +119,7 @@ inline bool IsV11Enabled(int nHeight)
119119

120120
inline int GetSuperblockAgeSpacing(int nHeight)
121121
{
122-
return (fTestNet ? 86400 : (nHeight > 364500) ? 86400 : 43200);
122+
return (fTestNet ? 86400 : (nHeight > 364500) ? 86400 : 43200);
123123
}
124124

125125
inline bool IsV9Enabled_Tally(int nHeight)
@@ -617,7 +617,7 @@ class CTransaction
617617
// Denial-of-service detection:
618618
mutable int nDoS;
619619
bool DoS(int nDoSIn, bool fIn) const { nDoS += nDoSIn; return fIn; }
620-
std::string hashBoinc;
620+
std::string hashBoinc;
621621

622622
CTransaction()
623623
{
@@ -635,7 +635,7 @@ class CTransaction
635635
READWRITE(vout);
636636
READWRITE(nLockTime);
637637

638-
READWRITE(hashBoinc);
638+
READWRITE(hashBoinc);
639639
}
640640

641641
void SetNull()
@@ -646,7 +646,7 @@ class CTransaction
646646
vout.clear();
647647
nLockTime = 0;
648648
nDoS = 0; // Denial-of-service prevention
649-
hashBoinc="";
649+
hashBoinc="";
650650
}
651651

652652
bool IsNull() const
@@ -1003,19 +1003,83 @@ class CTxIndex
10031003
* Blocks are appended to blk0001.dat files on disk. Their location on disk
10041004
* is indexed by CBlockIndex objects in memory.
10051005
*/
1006-
class CBlock
1006+
class CBlockHeader
10071007
{
10081008
public:
1009+
static const int32_t CURRENT_VERSION = 10;
1010+
10091011
// header
1010-
static const int CURRENT_VERSION = 10;
1011-
int nVersion;
1012+
int32_t nVersion;
10121013
uint256 hashPrevBlock;
10131014
uint256 hashMerkleRoot;
1014-
unsigned int nTime;
1015-
unsigned int nBits;
1015+
uint32_t nTime;
1016+
uint32_t nBits;
1017+
uint32_t nNonce;
10161018

1017-
unsigned int nNonce;
1019+
CBlockHeader()
1020+
{
1021+
SetNull();
1022+
}
1023+
1024+
ADD_SERIALIZE_METHODS;
1025+
1026+
template <typename Stream, typename Operation>
1027+
inline void SerializationOp(Stream& s, Operation ser_action)
1028+
{
1029+
READWRITE(nVersion);
1030+
READWRITE(hashPrevBlock);
1031+
READWRITE(hashMerkleRoot);
1032+
READWRITE(nTime);
1033+
READWRITE(nBits);
1034+
1035+
// Besides early blocks, Gridcoin uses Proof-of-Stake for consensus,
1036+
// so we don't need the nonce field. Don't serialize it after blocks
1037+
// version 11 and later:
1038+
//
1039+
if (nVersion <= 10) {
1040+
READWRITE(nNonce);
1041+
}
1042+
}
1043+
1044+
void SetNull()
1045+
{
1046+
nVersion = CURRENT_VERSION;
1047+
hashPrevBlock.SetNull();
1048+
hashMerkleRoot.SetNull();
1049+
nTime = 0;
1050+
nBits = 0;
1051+
nNonce = 0;
1052+
}
10181053

1054+
bool IsNull() const
1055+
{
1056+
return (nBits == 0);
1057+
}
1058+
1059+
uint256 GetHash() const
1060+
{
1061+
if (nVersion >= 11)
1062+
return Hash(BEGIN(nVersion), END(nBits));
1063+
else if (nVersion >= 7)
1064+
return Hash(BEGIN(nVersion), END(nNonce));
1065+
else
1066+
return GetPoWHash();
1067+
}
1068+
1069+
uint256 GetPoWHash() const
1070+
{
1071+
return scrypt_blockhash(CVOIDBEGIN(nVersion));
1072+
}
1073+
1074+
int64_t GetBlockTime() const
1075+
{
1076+
return (int64_t)nTime;
1077+
}
1078+
};
1079+
1080+
class CBlock : public CBlockHeader
1081+
{
1082+
public:
10191083
// network and disk
10201084
std::vector<CTransaction> vtx;
10211085

@@ -1037,17 +1101,18 @@ class CBlock
10371101
SetNull();
10381102
}
10391103

1104+
CBlock(const CBlockHeader &header)
1105+
{
1106+
SetNull();
1107+
*(static_cast<CBlockHeader*>(this)) = header;
1108+
}
1109+
10401110
ADD_SERIALIZE_METHODS;
10411111

10421112
template <typename Stream, typename Operation>
10431113
inline void SerializationOp(Stream& s, Operation ser_action)
10441114
{
1045-
READWRITE(nVersion);
1046-
READWRITE(hashPrevBlock);
1047-
READWRITE(hashMerkleRoot);
1048-
READWRITE(nTime);
1049-
READWRITE(nBits);
1050-
READWRITE(nNonce);
1115+
READWRITEAS(CBlockHeader, *this);
10511116

10521117
// ConnectBlock depends on vtx following header to generate CDiskTxPos
10531118
if (!(s.GetType() & (SER_GETHASH|SER_BLOCKHEADERONLY))) {
@@ -1077,35 +1142,25 @@ class CBlock
10771142

10781143
void SetNull()
10791144
{
1080-
nVersion = CBlock::CURRENT_VERSION;
1081-
hashPrevBlock.SetNull();
1082-
hashMerkleRoot.SetNull();
1083-
nTime = 0;
1084-
nBits = 0;
1085-
nNonce = 0;
1145+
CBlockHeader::SetNull();
1146+
10861147
vtx.clear();
10871148
vchBlockSig.clear();
1088-
vMerkleTree.clear();
1149+
vMerkleTree.clear();
10891150
nDoS = 0;
10901151
m_claim = NN::Claim();
10911152
}
10921153

1093-
bool IsNull() const
1154+
CBlockHeader GetBlockHeader() const
10941155
{
1095-
return (nBits == 0);
1096-
}
1097-
1098-
uint256 GetHash() const
1099-
{
1100-
if (nVersion > 6)
1101-
return Hash(BEGIN(nVersion), END(nNonce));
1102-
else
1103-
return GetPoWHash();
1104-
}
1105-
1106-
uint256 GetPoWHash() const
1107-
{
1108-
return scrypt_blockhash(CVOIDBEGIN(nVersion));
1156+
CBlockHeader block;
1157+
block.nVersion = nVersion;
1158+
block.hashPrevBlock = hashPrevBlock;
1159+
block.hashMerkleRoot = hashMerkleRoot;
1160+
block.nTime = nTime;
1161+
block.nBits = nBits;
1162+
block.nNonce = nNonce;
1163+
return block;
11091164
}
11101165

11111166
const NN::Claim& GetClaim() const
@@ -1140,11 +1195,6 @@ class CBlock
11401195
return GetClaim().m_superblock;
11411196
}
11421197

1143-
int64_t GetBlockTime() const
1144-
{
1145-
return (int64_t)nTime;
1146-
}
1147-
11481198
// entropy bit for stake modifier if chosen by modifier
11491199
unsigned int GetStakeEntropyBit() const
11501200
{
@@ -1438,9 +1488,9 @@ class CBlockIndex
14381488
nIsContract = 0;
14391489
}
14401490

1441-
CBlock GetBlockHeader() const
1491+
CBlockHeader GetBlockHeader() const
14421492
{
1443-
CBlock block;
1493+
CBlockHeader block;
14441494
block.nVersion = nVersion;
14451495
if (pprev)
14461496
block.hashPrevBlock = pprev->GetBlockHash();
@@ -1689,7 +1739,7 @@ class CDiskBlockIndex : public CBlockIndex
16891739
if (fUseFastIndex && (nTime < GetAdjustedTime() - 24 * 60 * 60) && !blockHash.IsNull())
16901740
return blockHash;
16911741

1692-
CBlock block;
1742+
CBlockHeader block;
16931743
block.nVersion = nVersion;
16941744
block.hashPrevBlock = hashPrev;
16951745
block.hashMerkleRoot = hashMerkleRoot;

0 commit comments

Comments
 (0)