Improve network height estimation

This commit is contained in:
Jack Grigg
2017-07-28 10:50:07 +00:00
parent 4a651837be
commit 92bfde0edf
3 changed files with 30 additions and 24 deletions

View File

@@ -106,12 +106,17 @@ double GetLocalSolPS()
int EstimateNetHeightInner(int height, int64_t tipmediantime,
int heightLastCheckpoint, int64_t timeLastCheckpoint,
int64_t targetSpacing)
int64_t genesisTime, int64_t targetSpacing)
{
// We average the target spacing with the observed spacing to the last
// checkpoint, and use that to estimate the current network height.
int medianHeight = height - CBlockIndex::nMedianTimeSpan / 2;
double checkpointSpacing = (double (tipmediantime - timeLastCheckpoint)) / (medianHeight - heightLastCheckpoint);
// checkpoint (either from below or above depending on the current height),
// and use that to estimate the current network height.
int medianHeight = height > CBlockIndex::nMedianTimeSpan ?
height - (1 + ((CBlockIndex::nMedianTimeSpan - 1) / 2)) :
height / 2;
double checkpointSpacing = medianHeight > heightLastCheckpoint ?
(double (tipmediantime - timeLastCheckpoint)) / (medianHeight - heightLastCheckpoint) :
(double (timeLastCheckpoint - genesisTime)) / heightLastCheckpoint;
double averageSpacing = (targetSpacing + checkpointSpacing) / 2;
int netheight = medianHeight + ((GetTime() - tipmediantime) / averageSpacing);
// Round to nearest ten to reduce noise
@@ -125,6 +130,7 @@ int EstimateNetHeight(int height, int64_t tipmediantime, CChainParams chainParam
height, tipmediantime,
Checkpoints::GetTotalBlocksEstimate(checkpointData),
checkpointData.nTimeLastCheckpoint,
chainParams.GenesisBlock().nTime,
chainParams.GetConsensus().nPowTargetSpacing);
}