Skip to content

Commit

Permalink
Chainwork ultimately decided by PoS. Add max past time drift.
Browse files Browse the repository at this point in the history
  • Loading branch information
presstab committed Jan 4, 2019
1 parent 03a8b74 commit 1996ee1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
16 changes: 16 additions & 0 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
25 changes: 5 additions & 20 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4662,16 +4654,9 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
for (const std::pair<int, CBlockIndex*>& 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.
Expand Down

0 comments on commit 1996ee1

Please sign in to comment.