2014-01-17 13:20:43 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-09-08 22:50:58 +02:00
|
|
|
|
2011-09-08 18:51:43 +02:00
|
|
|
#include "checkpoints.h"
|
2011-09-08 22:50:58 +02:00
|
|
|
|
2014-08-31 21:32:23 +02:00
|
|
|
#include "chainparams.h"
|
2012-04-15 22:10:54 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "uint256.h"
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2014-06-24 14:17:43 +02:00
|
|
|
namespace Checkpoints {
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* How many times we expect transactions after the last checkpoint to
|
|
|
|
* be slower. This number is a compromise, as it can't be accurate for
|
|
|
|
* every system. When reindexing from a fast disk with a slow CPU, it
|
|
|
|
* can be up to 20, while when downloading from a slow network with a
|
|
|
|
* fast multicore CPU, it won't be much higher than 1.
|
|
|
|
*/
|
2014-03-19 00:26:14 +01:00
|
|
|
static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
|
2013-02-10 19:46:42 +01:00
|
|
|
|
2013-05-24 17:10:53 +02:00
|
|
|
bool fEnabled = true;
|
|
|
|
|
2011-09-08 22:50:58 +02:00
|
|
|
bool CheckBlock(int nHeight, const uint256& hash)
|
|
|
|
{
|
2013-05-24 17:10:53 +02:00
|
|
|
if (!fEnabled)
|
2012-11-28 18:07:42 +01:00
|
|
|
return true;
|
|
|
|
|
2014-08-31 21:32:23 +02:00
|
|
|
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
|
2011-09-08 22:50:58 +02:00
|
|
|
|
2012-04-13 20:53:31 +02:00
|
|
|
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
|
|
|
|
if (i == checkpoints.end()) return true;
|
2011-09-08 22:50:58 +02:00
|
|
|
return hash == i->second;
|
|
|
|
}
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Guess how far we are in the verification process at the given block index
|
2014-03-19 00:26:14 +01:00
|
|
|
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks) {
|
2013-02-10 19:46:42 +01:00
|
|
|
if (pindex==NULL)
|
|
|
|
return 0.0;
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nNow = time(NULL);
|
2013-02-10 19:46:42 +01:00
|
|
|
|
2014-03-19 00:26:14 +01:00
|
|
|
double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
|
2013-02-10 19:46:42 +01:00
|
|
|
double fWorkBefore = 0.0; // Amount of work done before pindex
|
|
|
|
double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
|
2014-01-17 13:20:43 +01:00
|
|
|
// Work is defined as: 1.0 per transaction before the last checkpoint, and
|
2013-02-10 19:46:42 +01:00
|
|
|
// fSigcheckVerificationFactor per transaction after.
|
|
|
|
|
2014-08-31 21:32:23 +02:00
|
|
|
const CCheckpointData &data = Params().Checkpoints();
|
2013-02-10 19:46:42 +01:00
|
|
|
|
|
|
|
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
|
|
|
|
double nCheapBefore = pindex->nChainTx;
|
|
|
|
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
|
|
|
|
double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
|
|
|
|
fWorkBefore = nCheapBefore;
|
|
|
|
fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
|
|
|
|
} else {
|
|
|
|
double nCheapBefore = data.nTransactionsLastCheckpoint;
|
|
|
|
double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
|
2014-06-28 23:36:06 +02:00
|
|
|
double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
|
2013-02-10 19:46:42 +01:00
|
|
|
fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
|
|
|
|
fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fWorkBefore / (fWorkBefore + fWorkAfter);
|
|
|
|
}
|
|
|
|
|
2011-09-08 22:50:58 +02:00
|
|
|
int GetTotalBlocksEstimate()
|
|
|
|
{
|
2013-05-24 17:10:53 +02:00
|
|
|
if (!fEnabled)
|
2012-11-28 18:07:42 +01:00
|
|
|
return 0;
|
|
|
|
|
2014-08-31 21:32:23 +02:00
|
|
|
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
|
2011-09-08 22:50:58 +02:00
|
|
|
|
2012-04-13 20:53:31 +02:00
|
|
|
return checkpoints.rbegin()->first;
|
2011-09-08 22:50:58 +02:00
|
|
|
}
|
2011-09-08 18:51:43 +02:00
|
|
|
|
2014-09-04 02:01:10 +02:00
|
|
|
CBlockIndex* GetLastCheckpoint()
|
2011-09-08 18:51:43 +02:00
|
|
|
{
|
2013-05-24 17:10:53 +02:00
|
|
|
if (!fEnabled)
|
2012-11-28 18:07:42 +01:00
|
|
|
return NULL;
|
|
|
|
|
2014-08-31 21:32:23 +02:00
|
|
|
const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
|
2011-09-08 18:51:43 +02:00
|
|
|
|
2012-04-13 20:53:31 +02:00
|
|
|
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
|
2011-09-08 18:51:43 +02:00
|
|
|
{
|
|
|
|
const uint256& hash = i.second;
|
2014-09-04 02:02:44 +02:00
|
|
|
BlockMap::const_iterator t = mapBlockIndex.find(hash);
|
2011-09-08 18:51:43 +02:00
|
|
|
if (t != mapBlockIndex.end())
|
|
|
|
return t->second;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-24 14:17:43 +02:00
|
|
|
|
|
|
|
} // namespace Checkpoints
|