Fabcoin Core  0.16.2
P2P Digital Currency
checkpoints.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <checkpoints.h>
6 
7 #include <chain.h>
8 #include <chainparams.h>
9 #include <reverse_iterator.h>
10 #include <validation.h>
11 #include <uint256.h>
12 
13 #include <stdint.h>
14 
15 static const int nCheckpointSpan = 800;
16 
17 namespace Checkpoints {
18 
20  {
21  const MapCheckpoints& checkpoints = data.mapCheckpoints;
22 
23  for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints))
24  {
25  const uint256& hash = i.second;
26  BlockMap::const_iterator t = mapBlockIndex.find(hash);
27  if (t != mapBlockIndex.end())
28  return t->second;
29  }
30  return nullptr;
31  }
32  // Automatically select a suitable sync-checkpoint
34  {
35  const CBlockIndex *pindexBest = chainActive.Tip();
36  const CBlockIndex *pindex = pindexBest;
37  // Search backward for a block within max span and maturity window
38  while (pindex->pprev && pindex->nHeight + nCheckpointSpan > pindexBest->nHeight)
39  pindex = pindex->pprev;
40  return pindex;
41  }
42 
43  // Check against synchronized checkpoint
44  bool CheckSync(int nHeight)
45  {
46  const CBlockIndex* pindexSync;
47  if(nHeight)
48  pindexSync = AutoSelectSyncCheckpoint();
49 
50  if(nHeight && nHeight <= pindexSync->nHeight)
51  return false;
52  return true;
53  }
54 
55 } // namespace Checkpoints
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:184
std::map< int, uint256 > MapCheckpoints
Definition: chainparams.h:28
reverse_range< T > reverse_iterate(T &x)
bool CheckSync(int nHeight)
Check against automatically selected checkpoint.
Definition: checkpoints.cpp:44
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:512
Block-chain checkpoints are compiled-in sanity checks.
Definition: checkpoints.cpp:17
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:80
CBlockIndex * GetLastCheckpoint(const CCheckpointData &data)
Returns last CBlockIndex* in mapBlockIndex that is a checkpoint.
Definition: checkpoints.cpp:19
256-bit opaque blob.
Definition: uint256.h:132
MapCheckpoints mapCheckpoints
Definition: chainparams.h:31
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:193
uint8_t const * data
Definition: sha3.h:19
BlockMap mapBlockIndex
Definition: validation.cpp:79
const CBlockIndex * AutoSelectSyncCheckpoint()
Returns last CBlockIndex* from the auto selected checkpoint.
Definition: checkpoints.cpp:33