Skip to content

Commit 850c9f6

Browse files
prepare for changing block hash
1 parent 2f4d0a8 commit 850c9f6

6 files changed

Lines changed: 33 additions & 23 deletions

File tree

src/kernel.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ static bool SelectBlockFromCandidates(vector<pair<int64_t, uint256> >& vSortedBy
8585
continue;
8686
// compute the selection hash by hashing its proof-hash and the
8787
// previous proof-of-stake modifier
88-
uint256 hashProof = pindex->IsProofOfStake()? pindex->hashProofOfStake : pindex->GetBlockHash();
8988
CDataStream ss(SER_GETHASH, 0);
90-
ss << hashProof << nStakeModifierPrev;
89+
ss << pindex->hashProof << nStakeModifierPrev;
9190
uint256 hashSelection = Hash(ss.begin(), ss.end());
9291
// the selection hash is divided by 2**32 so that proof-of-stake block
9392
// is always favored over proof-of-work block. this is to preserve
@@ -373,7 +372,7 @@ unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex)
373372
CDataStream ss(SER_GETHASH, 0);
374373
if (pindex->pprev)
375374
ss << pindex->pprev->nStakeModifierChecksum;
376-
ss << pindex->nFlags << pindex->hashProofOfStake << pindex->nStakeModifier;
375+
ss << pindex->nFlags << (pindex->IsProofOfStake() ? pindex->hashProof : 0) << pindex->nStakeModifier;
377376
uint256 hashChecksum = Hash(ss.begin(), ss.end());
378377
hashChecksum >>= (256 - 32);
379378
return hashChecksum.Get64();

src/main.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,7 @@ bool CBlock::GetCoinAge(uint64_t& nCoinAge) const
19541954
return true;
19551955
}
19561956

1957-
bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProofOfStake)
1957+
bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof)
19581958
{
19591959
// Check for duplicate
19601960
uint256 hash = GetHash();
@@ -1980,8 +1980,8 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const u
19801980
if (!pindexNew->SetStakeEntropyBit(GetStakeEntropyBit()))
19811981
return error("AddToBlockIndex() : SetStakeEntropyBit() failed");
19821982

1983-
// ppcoin: record proof-of-stake hash value
1984-
pindexNew->hashProofOfStake = hashProofOfStake;
1983+
// Record proof hash value
1984+
pindexNew->hashProof = hashProof;
19851985

19861986
// ppcoin: compute stake modifier
19871987
uint64_t nStakeModifier = 0;
@@ -2037,7 +2037,7 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c
20372037
return DoS(100, error("CheckBlock() : size limits failed"));
20382038

20392039
// Check proof of work matches claimed amount
2040-
if (fCheckPOW && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits))
2040+
if (fCheckPOW && IsProofOfWork() && !CheckProofOfWork(GetPoWHash(), nBits))
20412041
return DoS(50, error("CheckBlock() : proof of work failed"));
20422042

20432043
// Check timestamp
@@ -2151,16 +2151,22 @@ bool CBlock::AcceptBlock()
21512151
if (!Checkpoints::CheckHardened(nHeight, hash))
21522152
return DoS(100, error("AcceptBlock() : rejected by hardened checkpoint lock-in at %d", nHeight));
21532153

2154+
uint256 hashProof;
21542155
// Verify hash target and signature of coinstake tx
2155-
uint256 hashProofOfStake = 0, targetProofOfStake = 0;
21562156
if (IsProofOfStake())
21572157
{
2158-
if (!CheckProofOfStake(vtx[1], nBits, hashProofOfStake, targetProofOfStake))
2158+
uint256 targetProofOfStake;
2159+
if (!CheckProofOfStake(vtx[1], nBits, hashProof, targetProofOfStake))
21592160
{
21602161
printf("WARNING: AcceptBlock(): check proof-of-stake failed for block %s\n", hash.ToString().c_str());
21612162
return false; // do not error here as we expect this during initial block download
21622163
}
21632164
}
2165+
// PoW is checked in CheckBlock()
2166+
if (IsProofOfWork())
2167+
{
2168+
hashProof = GetPoWHash();
2169+
}
21642170

21652171
bool cpSatisfies = Checkpoints::CheckSync(hash, pindexPrev);
21662172

@@ -2184,7 +2190,7 @@ bool CBlock::AcceptBlock()
21842190
unsigned int nBlockPos = 0;
21852191
if (!WriteToDisk(nFile, nBlockPos))
21862192
return error("AcceptBlock() : WriteToDisk failed");
2187-
if (!AddToBlockIndex(nFile, nBlockPos, hashProofOfStake))
2193+
if (!AddToBlockIndex(nFile, nBlockPos, hashProof))
21882194
return error("AcceptBlock() : AddToBlockIndex failed");
21892195

21902196
// Relay inventory, but don't relay old inventory during initial block download
@@ -2562,7 +2568,7 @@ bool LoadBlockIndex(bool fAllowNew)
25622568
unsigned int nBlockPos;
25632569
if (!block.WriteToDisk(nFile, nBlockPos))
25642570
return error("LoadBlockIndex() : writing genesis block to disk failed");
2565-
if (!block.AddToBlockIndex(nFile, nBlockPos, 0))
2571+
if (!block.AddToBlockIndex(nFile, nBlockPos, hashGenesisBlock))
25662572
return error("LoadBlockIndex() : genesis block not accepted");
25672573

25682574
// ppcoin: initialize synchronized checkpoint

src/main.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ class CBlock
904904
}
905905

906906
uint256 GetHash() const
907+
{
908+
return GetPoWHash();
909+
}
910+
911+
uint256 GetPoWHash() const
907912
{
908913
return scrypt_blockhash(CVOIDBEGIN(nVersion));
909914
}
@@ -1047,7 +1052,7 @@ class CBlock
10471052
}
10481053

10491054
// Check the header
1050-
if (fReadTransactions && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits))
1055+
if (fReadTransactions && IsProofOfWork() && !CheckProofOfWork(GetPoWHash(), nBits))
10511056
return error("CBlock::ReadFromDisk() : errors in block header");
10521057

10531058
return true;
@@ -1081,7 +1086,7 @@ class CBlock
10811086
bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck=false);
10821087
bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
10831088
bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
1084-
bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProofOfStake);
1089+
bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof);
10851090
bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true) const;
10861091
bool AcceptBlock();
10871092
bool GetCoinAge(uint64_t& nCoinAge) const; // ppcoin: calculate total coin age spent in block
@@ -1132,7 +1137,8 @@ class CBlockIndex
11321137
// proof-of-stake specific fields
11331138
COutPoint prevoutStake;
11341139
unsigned int nStakeTime;
1135-
uint256 hashProofOfStake;
1140+
1141+
uint256 hashProof;
11361142

11371143
// block header
11381144
int nVersion;
@@ -1155,7 +1161,7 @@ class CBlockIndex
11551161
nFlags = 0;
11561162
nStakeModifier = 0;
11571163
nStakeModifierChecksum = 0;
1158-
hashProofOfStake = 0;
1164+
hashProof = 0;
11591165
prevoutStake.SetNull();
11601166
nStakeTime = 0;
11611167

@@ -1180,7 +1186,7 @@ class CBlockIndex
11801186
nFlags = 0;
11811187
nStakeModifier = 0;
11821188
nStakeModifierChecksum = 0;
1183-
hashProofOfStake = 0;
1189+
hashProof = 0;
11841190
if (block.IsProofOfStake())
11851191
{
11861192
SetProofOfStake();
@@ -1318,12 +1324,12 @@ class CBlockIndex
13181324

13191325
std::string ToString() const
13201326
{
1321-
return strprintf("CBlockIndex(nprev=%p, pnext=%p, nFile=%u, nBlockPos=%-6d nHeight=%d, nMint=%s, nMoneySupply=%s, nFlags=(%s)(%d)(%s), nStakeModifier=%016"PRIx64", nStakeModifierChecksum=%08x, hashProofOfStake=%s, prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
1327+
return strprintf("CBlockIndex(nprev=%p, pnext=%p, nFile=%u, nBlockPos=%-6d nHeight=%d, nMint=%s, nMoneySupply=%s, nFlags=(%s)(%d)(%s), nStakeModifier=%016"PRIx64", nStakeModifierChecksum=%08x, hashProof=%s, prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
13221328
pprev, pnext, nFile, nBlockPos, nHeight,
13231329
FormatMoney(nMint).c_str(), FormatMoney(nMoneySupply).c_str(),
13241330
GeneratedStakeModifier() ? "MOD" : "-", GetStakeEntropyBit(), IsProofOfStake()? "PoS" : "PoW",
13251331
nStakeModifier, nStakeModifierChecksum,
1326-
hashProofOfStake.ToString().c_str(),
1332+
hashProof.ToString().c_str(),
13271333
prevoutStake.ToString().c_str(), nStakeTime,
13281334
hashMerkleRoot.ToString().c_str(),
13291335
GetBlockHash().ToString().c_str());
@@ -1377,14 +1383,13 @@ class CDiskBlockIndex : public CBlockIndex
13771383
{
13781384
READWRITE(prevoutStake);
13791385
READWRITE(nStakeTime);
1380-
READWRITE(hashProofOfStake);
13811386
}
13821387
else if (fRead)
13831388
{
13841389
const_cast<CDiskBlockIndex*>(this)->prevoutStake.SetNull();
13851390
const_cast<CDiskBlockIndex*>(this)->nStakeTime = 0;
1386-
const_cast<CDiskBlockIndex*>(this)->hashProofOfStake = 0;
13871391
}
1392+
READWRITE(hashProof);
13881393

13891394
// block header
13901395
READWRITE(this->nVersion);

src/rpcblockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPri
119119
result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));
120120

121121
result.push_back(Pair("flags", strprintf("%s%s", blockindex->IsProofOfStake()? "proof-of-stake" : "proof-of-work", blockindex->GeneratedStakeModifier()? " stake-modifier": "")));
122-
result.push_back(Pair("proofhash", blockindex->IsProofOfStake()? blockindex->hashProofOfStake.GetHex() : blockindex->GetBlockHash().GetHex()));
122+
result.push_back(Pair("proofhash", blockindex->hashProof.GetHex()));
123123
result.push_back(Pair("entropybit", (int)blockindex->GetStakeEntropyBit()));
124124
result.push_back(Pair("modifier", strprintf("%016"PRIx64, blockindex->nStakeModifier)));
125125
result.push_back(Pair("modifierchecksum", strprintf("%08x", blockindex->nStakeModifierChecksum)));

src/txdb-leveldb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ bool CTxDB::LoadBlockIndex()
370370
pindexNew->nStakeModifier = diskindex.nStakeModifier;
371371
pindexNew->prevoutStake = diskindex.prevoutStake;
372372
pindexNew->nStakeTime = diskindex.nStakeTime;
373-
pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
373+
pindexNew->hashProof = diskindex.hashProof;
374374
pindexNew->nVersion = diskindex.nVersion;
375375
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
376376
pindexNew->nTime = diskindex.nTime;

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern const std::string CLIENT_DATE;
2424
//
2525
// database format versioning
2626
//
27-
static const int DATABASE_VERSION = 70508;
27+
static const int DATABASE_VERSION = 70509;
2828

2929
//
3030
// network protocol versioning

0 commit comments

Comments
 (0)