Skip to content

Commit

Permalink
Merge pull request bitcoin#18 from bitgreen/dup-hashproof
Browse files Browse the repository at this point in the history
Remove possibility of stake being accepted more than once.
  • Loading branch information
barrystyle committed Mar 2, 2020
2 parents 1e63463 + 40ddc26 commit 2083327
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ CChain& ChainActive() { return g_chainstate.m_chain; }
*/
RecursiveMutex cs_main;

std::vector<uint256> vecStakeSeen;
CBlockIndex *pindexBestHeader = nullptr;
Mutex g_best_block_mutex;
std::condition_variable g_best_block_cv;
Expand Down Expand Up @@ -1760,6 +1761,12 @@ static int64_t nBlocksTotal = 0;
/**
* proof-of-stake
*/
void maintainStakeSeen()
{
if (vecStakeSeen.size() > 1024)
vecStakeSeen.erase(vecStakeSeen.begin(), vecStakeSeen.begin() + 128);
}

bool PoSContextualBlockChecks(const CBlock& block, CValidationState& state, CBlockIndex* pindex, bool fJustCheck)
{
uint256 hashProofOfStake = uint256();
Expand All @@ -1769,6 +1776,15 @@ bool PoSContextualBlockChecks(const CBlock& block, CValidationState& state, CBlo
return false; // do not error here as we expect this during initial block download
}

//! make sure we havent seen this stake previously
for (const auto& seenStake : vecStakeSeen) {
if (seenStake == hashProofOfStake) {
LogPrintf(" * already seen this stake (%s), discarding block..\n", hashProofOfStake.ToString().c_str());
return false;
}
}
maintainStakeSeen();

// compute stake entropy bit for stake modifier
unsigned int nEntropyBit = GetStakeEntropyBit(block);

Expand Down Expand Up @@ -1804,6 +1820,9 @@ bool PoSContextualBlockChecks(const CBlock& block, CValidationState& state, CBlo
if (fJustCheck)
return true;

//! if we get this far, its good
vecStakeSeen.push_back(hashProofOfStake);

// write everything to index
if (block.IsProofOfStake())
{
Expand Down

0 comments on commit 2083327

Please sign in to comment.