@@ -24,6 +24,7 @@ map<uint256, CBlockIndex*> mapBlockIndex;
2424const uint256 hashGenesisBlock (" 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" );
2525CBlockIndex* pindexGenesisBlock = NULL ;
2626int nBestHeight = -1 ;
27+ CBigNum bnBestChainWork = 0 ;
2728uint256 hashBestChain = 0 ;
2829CBlockIndex* pindexBest = NULL ;
2930int64 nTimeBestReceived = 0 ;
@@ -848,6 +849,23 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
848849 return bnNew.GetCompact ();
849850}
850851
852+ vector<int > vStartingHeight;
853+ void AddStartingHeight (int nStartingHeight)
854+ {
855+ if (nStartingHeight != -1 )
856+ {
857+ vStartingHeight.push_back (nStartingHeight);
858+ sort (vStartingHeight.begin (), vStartingHeight.end ());
859+ }
860+ }
861+
862+ bool IsInitialBlockDownload ()
863+ {
864+ int nMedian = 69000 ;
865+ if (vStartingHeight.size () >= 5 )
866+ nMedian = vStartingHeight[vStartingHeight.size ()/2 ];
867+ return nBestHeight < nMedian-1000 ;
868+ }
851869
852870
853871
@@ -1208,13 +1226,14 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
12081226 pindexNew->pprev = (*miPrev).second ;
12091227 pindexNew->nHeight = pindexNew->pprev ->nHeight + 1 ;
12101228 }
1229+ pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev ->bnChainWork : 0 ) + pindexNew->GetBlockWork ();
12111230
12121231 CTxDB txdb;
12131232 txdb.TxnBegin ();
12141233 txdb.WriteBlockIndex (CDiskBlockIndex (pindexNew));
12151234
12161235 // New best
1217- if (pindexNew->nHeight > nBestHeight )
1236+ if (pindexNew->bnChainWork > bnBestChainWork )
12181237 {
12191238 if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
12201239 {
@@ -1253,6 +1272,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
12531272 hashBestChain = hash;
12541273 pindexBest = pindexNew;
12551274 nBestHeight = pindexBest->nHeight ;
1275+ bnBestChainWork = pindexNew->bnChainWork ;
12561276 nTimeBestReceived = GetTime ();
12571277 nTransactionsUpdated++;
12581278 printf (" AddToBlockIndex: new best=%s height=%d\n " , hashBestChain.ToString ().substr (0 ,16 ).c_str (), nBestHeight);
@@ -1900,6 +1920,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
19001920 }
19011921
19021922 AddTimeData (pfrom->addr .ip , nTime);
1923+ AddStartingHeight (pfrom->nStartingHeight );
19031924
19041925 // Change version
19051926 if (pfrom->nVersion >= 209 )
@@ -2845,6 +2866,10 @@ int64 GetBalance()
28452866}
28462867
28472868
2869+ int GetRandInt (int nMax)
2870+ {
2871+ return GetRand (nMax);
2872+ }
28482873
28492874bool SelectCoins (int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
28502875{
@@ -2858,9 +2883,14 @@ bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
28582883
28592884 CRITICAL_BLOCK (cs_mapWallet)
28602885 {
2861- for (map<uint256, CWalletTx>::iterator it = mapWallet.begin (); it != mapWallet.end (); ++it)
2862- {
2863- CWalletTx* pcoin = &(*it).second ;
2886+ vector<CWalletTx*> vCoins;
2887+ vCoins.reserve (mapWallet.size ());
2888+ for (map<uint256, CWalletTx>::iterator it = mapWallet.begin (); it != mapWallet.end (); ++it)
2889+ vCoins.push_back (&(*it).second );
2890+ random_shuffle (vCoins.begin (), vCoins.end (), GetRandInt);
2891+
2892+ foreach (CWalletTx* pcoin, vCoins)
2893+ {
28642894 if (!pcoin->IsFinal () || pcoin->fSpent )
28652895 continue ;
28662896 int64 n = pcoin->GetCredit ();
0 commit comments