Skip to content

Commit

Permalink
Pow fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrankovi committed Apr 17, 2018
1 parent bbb5db7 commit ee2afde
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ class CBlockIndex
return *phashBlock;
}

uint256 GetBlockPoWHash() const
uint256 GetBlockPoWHash(bool forceCalc = false) const
{
return GetBlockHeader().GetPoWHash(nHeight);
return GetBlockHeader().GetPoWHash(nHeight, forceCalc);
}

int64_t GetBlockTime() const
Expand Down
9 changes: 7 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,9 @@ bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, int nHeight, con
}
// Check the header
if (!CheckProofOfWork(block.GetPoWHash(nHeight), block.nBits, consensusParams))
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
//Maybe cache is not valid
if (!CheckProofOfWork(block.GetPoWHash(nHeight, true), block.nBits, consensusParams))
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
return true;
}

Expand Down Expand Up @@ -3699,7 +3701,10 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
bool CheckBlockHeader(const CBlockHeader &block, CValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW) {
int nHeight = ZerocoinGetNHeight(block);
if (fCheckPOW && !CheckProofOfWork(block.GetPoWHash(nHeight), block.nBits, consensusParams)) {
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
//Maybe cache is not valid
if (fCheckPOW && !CheckProofOfWork(block.GetPoWHash(nHeight, true), block.nBits, consensusParams)) {
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
}
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ uint256 CBlockHeader::GetHash() const {
return SerializeHash(*this);
}

uint256 CBlockHeader::GetPoWHash(int nHeight) const {
uint256 CBlockHeader::GetPoWHash(int nHeight, bool forceCalc) const {
// int64_t start = std::chrono::duration_cast<std::chrono::milliseconds>(
// std::chrono::system_clock::now().time_since_epoch()).count();
bool fTestNet = (Params().NetworkIDString() == CBaseChainParams::TESTNET);
Expand All @@ -61,7 +61,7 @@ uint256 CBlockHeader::GetPoWHash(int nHeight) const {
buildMapPoWHash();
}
}
if (mapPoWHash.count(nHeight)) {
if (!forceCalc && mapPoWHash.count(nHeight)) {
// std::cout << "GetPowHash nHeight=" << nHeight << ", hash= " << mapPoWHash[nHeight].ToString() << std::endl;
return mapPoWHash[nHeight];
}
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CBlockHeader
// powHash = hash;
}

uint256 GetPoWHash(int nHeight) const;
uint256 GetPoWHash(int nHeight, bool forceCalc = false) const;

uint256 GetHash() const;

Expand Down
3 changes: 2 additions & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256
pindexNew->spentSerials = diskindex.spentSerials;

if (!CheckProofOfWork(pindexNew->GetBlockPoWHash(), pindexNew->nBits, Params().GetConsensus()))
return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
if (!CheckProofOfWork(pindexNew->GetBlockPoWHash(true), pindexNew->nBits, Params().GetConsensus()))
return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());

pcursor->Next();
} else {
Expand Down

0 comments on commit ee2afde

Please sign in to comment.