#include "checkpoints.h"
-#include "db.h"
+#include "txdb.h"
#include "main.h"
#include "uint256.h"
namespace Checkpoints
{
- typedef std::map<int, uint256> MapCheckpoints;
+ typedef std::map<int, std::pair<uint256, unsigned int> > MapCheckpoints;
//
// What makes a good checkpoint block?
//
static MapCheckpoints mapCheckpoints =
boost::assign::map_list_of
- ( 0, hashGenesisBlock )
- ( 6000, uint256("0x000000000945e3c9d8e15df834e802521eb79f9ceb4191a27bdfadad4b777f4a"))
- ( 9690, uint256("0x00000000026561450859c46868099e0df6068a538f038cb18988fd8d47dcdaf5"))
- ( 13560, uint256("0xa1591a0fcbf11f282d671581edb9f0aadcd06fee69761081e0a3245914c13729"))
- ( 14189, uint256("0x00000000020f76474d2522b19c7bfafc43ba6ecbabae54293bcd9546159c8c1d"))
- ( 19600, uint256("0x252ae08d6df4bc7220c1dcdaed7b8a6e78bab05a60173511e8f565a3a38ce3c3"))
- ( 21800, uint256("0x64bdeb35023f240ed01beaa082f840f2d4cc6defccbbcda1260de87e58296b37"))
- ;
+ ( 0, std::make_pair(hashGenesisBlock, 1360105017) )
+ ( 68600, std::make_pair(uint256("0xb646b0562080ab9411f85d4b2a1ca8b197c67ae170c5e328406871b44233cb06"), 1389867276) )
+ ( 92161, std::make_pair(uint256("0x000000000079090eb31e7fa55487e4e666466253e68b213f44b7977ea7a2eeec"), 1398919969) )
+ ( 98661, std::make_pair(uint256("0x48328dfc0eaffc659723739c6202854d1624449340193bdea3e71ece4ccdd43f"), 1401469019) )
+ ;
+ // TestNet has no checkpoints
static MapCheckpoints mapCheckpointsTestnet =
boost::assign::map_list_of
- ( 0, hashGenesisBlockTestNet )
+ ( 0, std::make_pair(hashGenesisBlockTestNet, 1360105017) )
;
bool CheckHardened(int nHeight, const uint256& hash)
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
if (i == checkpoints.end()) return true;
- return hash == i->second;
+ return hash == i->second.first;
}
int GetTotalBlocksEstimate()
return checkpoints.rbegin()->first;
}
+ int GetLastCheckpointTime()
+ {
+ MapCheckpoints& checkpoints = (fTestNet ? mapCheckpointsTestnet : mapCheckpoints);
+
+ return checkpoints.rbegin()->second.second;
+ }
+
CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
{
MapCheckpoints& checkpoints = (fTestNet ? mapCheckpointsTestnet : mapCheckpoints);
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
{
- const uint256& hash = i.second;
+ const uint256& hash = i.second.first;
std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
if (t != mapBlockIndex.end())
return t->second;
CBlockIndex* pindex = pindexSyncCheckpoint;
while (pindex->nHeight > pindexCheckpointRecv->nHeight)
if (!(pindex = pindex->pprev))
- return error("ValidateSyncCheckpoint: pprev1 null - block index structure failure");
+ return error("ValidateSyncCheckpoint: pprev null - block index structure failure");
if (pindex->GetBlockHash() != hashCheckpoint)
{
hashInvalidCheckpoint = hashCheckpoint;
bool WriteSyncCheckpoint(const uint256& hashCheckpoint)
{
- CTxDB txdb;
- txdb.TxnBegin();
- if (!txdb.WriteSyncCheckpoint(hashCheckpoint))
{
- txdb.TxnAbort();
- return error("WriteSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
+ LOCK(Checkpoints::cs_hashSyncCheckpoint);
+
+ if (!pblocktree->WriteSyncCheckpoint(hashCheckpoint))
+ {
+ return error("WriteSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
+ }
}
- if (!txdb.TxnCommit())
- return error("WriteSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
- txdb.Close();
Checkpoints::hashSyncCheckpoint = hashCheckpoint;
return true;
return false;
}
- CTxDB txdb;
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashPendingCheckpoint];
if (!pindexCheckpoint->IsInMainChain())
{
CBlock block;
if (!block.ReadFromDisk(pindexCheckpoint))
return error("AcceptPendingSyncCheckpoint: ReadFromDisk failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
- if (!block.SetBestChain(txdb, pindexCheckpoint))
+ if (!SetBestChain(pindexCheckpoint))
{
hashInvalidCheckpoint = hashPendingCheckpoint;
return error("AcceptPendingSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
}
}
- txdb.Close();
if (!WriteSyncCheckpoint(hashPendingCheckpoint))
return error("AcceptPendingSyncCheckpoint(): failed to write sync checkpoint %s", hashPendingCheckpoint.ToString().c_str());
// Automatically select a suitable sync-checkpoint
uint256 AutoSelectSyncCheckpoint()
{
- // Proof-of-work blocks are immediately checkpointed
- // to defend against 51% attack which rejects other miners block
-
- // Select the last proof-of-work block
- const CBlockIndex *pindex = GetLastBlockIndex(pindexBest, false);
- // Search forward for a block within max span and maturity window
- while (pindex->pnext && (pindex->GetBlockTime() + CHECKPOINT_MAX_SPAN <= pindexBest->GetBlockTime() || pindex->nHeight + std::min(6, nCoinbaseMaturity - 20) <= pindexBest->nHeight))
- pindex = pindex->pnext;
+ const CBlockIndex *pindex = pindexBest;
+ // Search backward for a block within max span and maturity window
+ while (pindex->pprev && (pindex->GetBlockTime() + CHECKPOINT_MAX_SPAN > pindexBest->GetBlockTime() || pindex->nHeight + 8 > pindexBest->nHeight))
+ pindex = pindex->pprev;
return pindex->GetBlockHash();
}
bool ResetSyncCheckpoint()
{
LOCK(cs_hashSyncCheckpoint);
- const uint256& hash = mapCheckpoints.rbegin()->second;
+ const uint256& hash = mapCheckpoints.rbegin()->second.first;
if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain())
{
// checkpoint block accepted but not yet in main chain
printf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str());
- CTxDB txdb;
CBlock block;
if (!block.ReadFromDisk(mapBlockIndex[hash]))
return error("ResetSyncCheckpoint: ReadFromDisk failed for hardened checkpoint %s", hash.ToString().c_str());
- if (!block.SetBestChain(txdb, mapBlockIndex[hash]))
+ if (!SetBestChain(mapBlockIndex[hash]))
{
return error("ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str());
}
- txdb.Close();
}
else if(!mapBlockIndex.count(hash))
{
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
{
- const uint256& hash = i.second;
+ const uint256& hash = i.second.first;
if (mapBlockIndex.count(hash) && mapBlockIndex[hash]->IsInMainChain())
{
if (!WriteSyncCheckpoint(hash))
if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
return false;
- CTxDB txdb;
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
if (!pindexCheckpoint->IsInMainChain())
{
CBlock block;
if (!block.ReadFromDisk(pindexCheckpoint))
return error("ProcessSyncCheckpoint: ReadFromDisk failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
- if (!block.SetBestChain(txdb, pindexCheckpoint))
+ if (!SetBestChain(pindexCheckpoint))
{
Checkpoints::hashInvalidCheckpoint = hashCheckpoint;
return error("ProcessSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
}
}
- txdb.Close();
if (!Checkpoints::WriteSyncCheckpoint(hashCheckpoint))
return error("ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str());