Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24.x-rebase: HasValidProofOfWork updates for Vertcoin Core #247

Open
wants to merge 1 commit into
base: 24.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/headerssync.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class HeadersSyncState {
/** Return the amount of work in the chain received during the PRESYNC phase. */
arith_uint256 GetPresyncWork() const { return m_current_chain_work; }

/** Return the height reached during the REDOWNLOAD phase */
int64_t GetRedownloadHeight() const { return m_redownload_buffer_last_height; }

/** Construct a HeadersSyncState object representing a headers sync via this
* download-twice mechanism).
*
Expand Down
14 changes: 13 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,8 +2378,20 @@ void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlo

bool PeerManagerImpl::CheckHeadersPoW(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, Peer& peer)
{
// Vertcoin Core: need to know the height for correct POW algorithm selection
int nHeight = 0;
if (peer.m_headers_sync) {
if (peer.m_headers_sync->GetState() == HeadersSyncState::State::PRESYNC) {
nHeight = peer.m_headers_sync->GetPresyncHeight();
} else if (peer.m_headers_sync->GetState() == HeadersSyncState::State::REDOWNLOAD) {
nHeight = peer.m_headers_sync->GetRedownloadHeight();
}
} else {
//We're done pre-checking headers from this peer
return true;
}
// Do these headers have proof-of-work matching what's claimed?
if (!HasValidProofOfWork(headers, consensusParams)) {
if (!HasValidProofOfWork(headers, consensusParams, nHeight)) {
Misbehaving(peer, 100, "header with invalid proof of work");
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF
// or decrease beyond the permitted limits.
bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits)
{
// Vertcoin uses KGW for network retargetting
/*
if (params.fPowAllowMinDifficultyBlocks) return true;

if (height % params.DifficultyAdjustmentInterval() == 0) {
Expand Down Expand Up @@ -118,7 +120,8 @@ bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t heig
if (minimum_new_target > observed_new_target) return false;
} else if (old_nbits != new_nbits) {
return false;
}
} */
//Vertcoin uses KGW for network retargetting
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3432,10 +3432,11 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
return commitment;
}

bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams)
bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, int nHeight)
{
int preSyncHeight = nHeight + 1;
return std::all_of(headers.cbegin(), headers.cend(),
[&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
[&](const auto& header) { return CheckProofOfWork(header.GetPoWHash(preSyncHeight++), header.nBits, consensusParams);});
}

arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers)
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ bool TestBlockValidity(BlockValidationState& state,
bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

/** Check with the proof of work on each blockheader matches the value in nBits */
bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams);
bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, int nHeight);

/** Return the sum of the work on a given set of headers */
arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers);
Expand Down