PRCYCoin  2.0.0.7rc1
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-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "checkpoints.h"
9 
10 #include "chainparams.h"
11 #include "main.h"
12 #include "uint256.h"
13 
14 #include <stdint.h>
15 
16 #include <boost/foreach.hpp>
17 
18 namespace Checkpoints
19 {
27 static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
28 
29 bool fEnabled = true;
30 
31 bool CheckBlock(int nHeight, const uint256& hash, bool fMatchesCheckpoint)
32 {
33  if (!fEnabled)
34  return true;
35 
36  const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
37 
38  MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
39  // If looking for an exact match, then return false
40  if (i == checkpoints.end()) return !fMatchesCheckpoint;
41  return hash == i->second;
42 }
43 
45 double GuessVerificationProgress(const CBlockIndex* pindex, bool fSigchecks)
46 {
47  if (pindex == NULL)
48  return 0.0;
49 
50  int64_t nNow = time(NULL);
51 
52  double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
53  double fWorkBefore = 0.0; // Amount of work done before pindex
54  double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
55  // Work is defined as: 1.0 per transaction before the last checkpoint, and
56  // fSigcheckVerificationFactor per transaction after.
57 
58  const CCheckpointData& data = Params().Checkpoints();
59 
60  if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
61  double nCheapBefore = pindex->nChainTx;
62  double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
63  double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint) / 86400.0 * data.fTransactionsPerDay;
64  fWorkBefore = nCheapBefore;
65  fWorkAfter = nCheapAfter + nExpensiveAfter * fSigcheckVerificationFactor;
66  } else {
67  double nCheapBefore = data.nTransactionsLastCheckpoint;
68  double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
69  double nExpensiveAfter = (nNow - pindex->GetBlockTime()) / 86400.0 * data.fTransactionsPerDay;
70  fWorkBefore = nCheapBefore + nExpensiveBefore * fSigcheckVerificationFactor;
71  fWorkAfter = nExpensiveAfter * fSigcheckVerificationFactor;
72  }
73 
74  return fWorkBefore / (fWorkBefore + fWorkAfter);
75 }
76 
78 {
79  if (!fEnabled)
80  return 0;
81 
82  const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
83 
84  return checkpoints.rbegin()->first;
85 }
86 
88 {
89  if (!fEnabled)
90  return NULL;
91 
92  const MapCheckpoints& checkpoints = *Params().Checkpoints().mapCheckpoints;
93 
94  BOOST_REVERSE_FOREACH (const MapCheckpoints::value_type& i, checkpoints) {
95  const uint256& hash = i.second;
96  BlockMap::const_iterator t = mapBlockIndex.find(hash);
97  if (t != mapBlockIndex.end())
98  return t->second;
99  }
100  return NULL;
101 }
102 
103 } // namespace Checkpoints
CBlockIndex::GetBlockTime
int64_t GetBlockTime() const
Definition: chain.h:364
Checkpoints::CCheckpointData
Definition: checkpoints.h:22
Checkpoints::GetTotalBlocksEstimate
int GetTotalBlocksEstimate()
Return conservative estimate of total number of blocks, 0 if unknown.
Definition: checkpoints.cpp:77
Checkpoints::CCheckpointData::nTransactionsLastCheckpoint
int64_t nTransactionsLastCheckpoint
Definition: checkpoints.h:25
uint256.h
CBlockIndex::nChainTx
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:202
Checkpoints::GetLastCheckpoint
CBlockIndex * GetLastCheckpoint()
Returns last CBlockIndex* in mapBlockIndex that is a checkpoint.
Definition: checkpoints.cpp:87
chainparams.h
CChainParams::Checkpoints
virtual const Checkpoints::CCheckpointData & Checkpoints() const =0
Checkpoints::CCheckpointData::mapCheckpoints
const MapCheckpoints * mapCheckpoints
Definition: checkpoints.h:23
Checkpoints
Block-chain checkpoints are compiled-in sanity checks.
Definition: checkpoints.cpp:18
Checkpoints::fEnabled
bool fEnabled
Definition: checkpoints.cpp:29
Checkpoints::MapCheckpoints
std::map< int, uint256 > MapCheckpoints
Definition: checkpoints.h:20
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
checkpoints.h
Checkpoints::CheckBlock
bool CheckBlock(int nHeight, const uint256 &hash, bool fMatchesCheckpoint)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:31
Checkpoints::GuessVerificationProgress
double GuessVerificationProgress(const CBlockIndex *pindex, bool fSigchecks)
Guess how far we are in the verification process at the given block index.
Definition: checkpoints.cpp:45
Checkpoints::CCheckpointData::fTransactionsPerDay
double fTransactionsPerDay
Definition: checkpoints.h:26
main.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:463
Checkpoints::CCheckpointData::nTimeLastCheckpoint
int64_t nTimeLastCheckpoint
Definition: checkpoints.h:24
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:162
mapBlockIndex
BlockMap mapBlockIndex
Definition: main.cpp:67