From 1996ee1f262ce2eed7c69e3979c8a3a2fe563f00 Mon Sep 17 00:00:00 2001 From: presstab Date: Thu, 3 Jan 2019 23:55:19 -0700 Subject: [PATCH] Chainwork ultimately decided by PoS. Add max past time drift. --- src/chain.cpp | 16 ++++++++++++++++ src/chain.h | 5 ++++- src/validation.cpp | 25 +++++-------------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/chain.cpp b/src/chain.cpp index 0f62889c9e..d504c6c66e 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -121,6 +121,22 @@ void CBlockIndex::BuildSkip() pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); } +int64_t CBlockIndex::GetBlockWork() const +{ + int64_t nTimeSpan = 0; + if (pprev && pprev->pprev) + nTimeSpan = pprev->GetBlockTime() - pprev->pprev->GetBlockTime(); + int64_t nBlockWork = 1000 - nTimeSpan; + if (nBlockWork <= 0) + nBlockWork = 1; + + //PoS blocks have the final decision on consensus, if it is between a PoW block and PoS block + if (IsProofOfStake()) + nBlockWork += 1001; + + return nBlockWork; +} + arith_uint256 GetBlockProof(const CBlockIndex& block) { arith_uint256 bnTarget; diff --git a/src/chain.h b/src/chain.h index 711fd653e6..2a78f13815 100644 --- a/src/chain.h +++ b/src/chain.h @@ -21,7 +21,8 @@ * Maximum amount of time that a block timestamp is allowed to exceed the * current network-adjusted time before the block will be accepted. */ -static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60; +static const int64_t MAX_FUTURE_BLOCK_TIME = 75; +static const int64_t MAX_PAST_BLOCK_TIME = 15; /** * Timestamp window used as a grace period by code that compares external @@ -369,6 +370,8 @@ class CBlockIndex return (int64_t)nTimeMax; } + int64_t GetBlockWork() const; + /** Returns the hash of the accumulator for the specified denomination. If it doesn't exist then a new uint256 is returned*/ uint256 GetAccumulatorHash(libzerocoin::CoinDenomination denom) const { diff --git a/src/validation.cpp b/src/validation.cpp index 6da0a51103..3703f48e0a 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3565,15 +3565,7 @@ CBlockIndex* CChainState::AddToBlockIndex(const CBlockHeader& block, bool fProof } pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime); - int nTimeElapsed = 60; - if (pindexNew->pprev) - nTimeElapsed = pindexNew->GetBlockTime() - pindexNew->pprev->GetBlockTime(); - - nTimeElapsed = 1000 - nTimeElapsed; - if (nTimeElapsed <= 0) - nTimeElapsed = 1; - - pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + nTimeElapsed; + pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork(); //pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); pindexNew->RaiseValidity(BLOCK_VALID_TREE); @@ -3998,7 +3990,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationSta return state.DoS(1, error("%s: forked chain older than max reorganization depth (height %d)", __func__, nHeight), REJECT_DEPTH, "bad-fork-prior-to-max-reorg-depth"); // Check timestamp against prev - if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) + if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() || (pindexPrev->nHeight > 5000 && block.GetBlockTime() < pindexPrev->GetBlockTime() - MAX_PAST_BLOCK_TIME)) return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early"); // Check timestamp @@ -4662,16 +4654,9 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo for (const std::pair& item : vSortedByHeight) { CBlockIndex* pindex = item.second; - int64_t nTimeElapsed = 60; - if (pindex->pprev) - nTimeElapsed = pindex->GetBlockTime() - pindex->pprev->GetBlockTime(); - nTimeElapsed = 1000 - nTimeElapsed; - if (nTimeElapsed <= 0) - nTimeElapsed = 1; - - pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + nTimeElapsed; - //pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + 10000 - nTimeElapsed; - //pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); + int64_t nTimeSpan = 0; + + pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork(); pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime); // We can link the chain of blocks for which we've received transactions at some point. // Pruned nodes may have deleted the block.