Skip to content

Commit

Permalink
Rely on block height to distinguish between POS blocks and POW blocks
Browse files Browse the repository at this point in the history
before the Hybrid phase
  • Loading branch information
FornaxA committed Mar 30, 2020
1 parent 65734c4 commit 31bc2ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/pos/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "wallet/db.h"
#include "kernel.h"
#include "policy/policy.h"
#include "pow.h"
#include "script/interpreter.h"
#include "timedata.h"
#include "util.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ static bool SelectBlockFromCandidates(
// the selection hash is divided by 2**32 so that proof-of-stake block
// is always favored over proof-of-work block. this is to preserve
// the energy efficiency property
if (pindex->IsProofOfStake())
if (IsProofOfStakeHeight(pindex->nHeight, Params().GetConsensus()))
hashSelection = ArithToUint256(UintToArith256(hashSelection) >> 32);

if (fSelected && UintToArith256(hashSelection) < UintToArith256(hashBest)) {
Expand Down
15 changes: 10 additions & 5 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@ unsigned int static GetNextWorkRequiredOrig(const CBlockIndex* pindexLast, const
if (pindexLast == NULL)
return UintToArith256(bnTargetLimit).GetCompact(); // genesis block
const CBlockIndex* pindexPrev = pindexLast;
while (pindexPrev && pindexPrev->pprev && (pindexPrev->IsProofOfStake() != fProofOfStake))
while (pindexPrev && pindexPrev->pprev && (IsProofOfStakeHeight(pindexPrev->nHeight, params) != fProofOfStake))
pindexPrev = pindexPrev->pprev;
if (pindexPrev == NULL)
return UintToArith256(bnTargetLimit).GetCompact(); // first block
const CBlockIndex* pindexPrevPrev = pindexPrev->pprev;
while (pindexPrevPrev && pindexPrevPrev->pprev && (pindexPrevPrev->IsProofOfStake() != fProofOfStake))
while (pindexPrevPrev && pindexPrevPrev->pprev && (IsProofOfStakeHeight(pindexPrevPrev->nHeight, params) != fProofOfStake))
pindexPrevPrev = pindexPrevPrev->pprev;
if (pindexPrevPrev == NULL)
return UintToArith256(bnTargetLimit).GetCompact(); // second block
Expand Down Expand Up @@ -452,10 +452,8 @@ unsigned int static GetNextWorkRequiredOrig(const CBlockIndex* pindexLast, const
return bnNew.GetCompact();
}

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params, const bool fHybridPow)
{
bool IsProofOfStakeHeight(const int nHeight, const Consensus::Params& params) {
bool fProofOfStake;
const int nHeight = pindexLast->nHeight + 1;
if (nHeight >= params.POSStartHeight){
fProofOfStake = true;
} else if (Params().NetworkIDString() == CBaseChainParams::MAIN) {
Expand Down Expand Up @@ -505,6 +503,13 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const Consensus:
} else {
fProofOfStake = false;
}
return fProofOfStake;
}

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params, const bool fHybridPow)
{
const int nHeight = pindexLast->nHeight + 1;
bool fProofOfStake = IsProofOfStakeHeight(nHeight, params);

// this is only active on devnets
if (pindexLast->nHeight < params.nMinimumDifficultyBlocks) {
Expand Down
2 changes: 2 additions & 0 deletions src/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const Consensus:
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);

bool IsProofOfStakeHeight(const int nHeight, const Consensus::Params& params);

#endif // BITCOIN_POW_H

0 comments on commit 31bc2ef

Please sign in to comment.