PIVX Core  5.6.99
P2P Digital Currency
checkpoints.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2021 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "checkpoints.h"
8 
9 #include "chain.h"
10 #include "chainparams.h"
11 #include "reverse_iterate.h"
12 
13 #include <stdint.h>
14 
15 
16 namespace Checkpoints
17 {
25 static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
26 
27 bool fEnabled = true;
28 
29 bool CheckBlock(int nHeight, const uint256& hash, bool fMatchesCheckpoint)
30 {
31  if (!fEnabled)
32  return true;
33 
34  const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
35 
36  MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
37  // If looking for an exact match, then return false
38  if (i == checkpoints.end()) return !fMatchesCheckpoint;
39  return hash == i->second;
40 }
41 
43 double GuessVerificationProgress(const CBlockIndex* pindex, bool fSigchecks)
44 {
45  if (pindex == nullptr)
46  return 0.0;
47 
48  int64_t nNow = time(nullptr);
49 
50  double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
51  double fWorkBefore = 0.0; // Amount of work done before pindex
52  double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
53  // Work is defined as: 1.0 per transaction before the last checkpoint, and
54  // fSigcheckVerificationFactor per transaction after.
55 
56  const CCheckpointData& data = Params().Checkpoints();
57 
58  if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
59  double nCheapBefore = pindex->nChainTx;
60  double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
61  double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint) / 86400.0 * data.fTransactionsPerDay;
62  fWorkBefore = nCheapBefore;
63  fWorkAfter = nCheapAfter + nExpensiveAfter * fSigcheckVerificationFactor;
64  } else {
65  double nCheapBefore = data.nTransactionsLastCheckpoint;
66  double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
67  double nExpensiveAfter = (nNow - pindex->GetBlockTime()) / 86400.0 * data.fTransactionsPerDay;
68  fWorkBefore = nCheapBefore + nExpensiveBefore * fSigcheckVerificationFactor;
69  fWorkAfter = nExpensiveAfter * fSigcheckVerificationFactor;
70  }
71 
72  return fWorkBefore / (fWorkBefore + fWorkAfter);
73 }
74 
76 {
77  if (!fEnabled)
78  return 0;
79 
80  const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
81 
82  return checkpoints.rbegin()->first;
83 }
84 
85 } // namespace Checkpoints
const CChainParams & Params()
Return the currently selected parameters.
std::map< int, uint256 > MapCheckpoints
Definition: chainparams.h:26
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
int64_t GetBlockTime() const
Definition: chain.h:216
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:172
virtual const CCheckpointData & Checkpoints() const =0
256-bit opaque blob.
Definition: uint256.h:138
Block-chain checkpoints are compiled-in sanity checks.
Definition: checkpoints.cpp:17
bool CheckBlock(int nHeight, const uint256 &hash, bool fMatchesCheckpoint)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:29
int GetTotalBlocksEstimate()
Return conservative estimate of total number of blocks, 0 if unknown.
Definition: checkpoints.cpp:75
double GuessVerificationProgress(const CBlockIndex *pindex, bool fSigchecks)
Guess how far we are in the verification process at the given block index.
Definition: checkpoints.cpp:43
double fTransactionsPerDay
Definition: chainparams.h:32
int64_t nTransactionsLastCheckpoint
Definition: chainparams.h:31
int64_t nTimeLastCheckpoint
Definition: chainparams.h:30
const MapCheckpoints * mapCheckpoints
Definition: chainparams.h:29