@ -1234,33 +1234,10 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
@@ -1234,33 +1234,10 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
}
bool CBlock : : AddToBlockIndex ( unsigned int nFile , unsigned int nBlockPos )
bool CBlock : : SetBestChain ( CTxDB & txdb , CBlockIndex * pindexNew )
{
// Check for duplicate
uint256 hash = GetHash ( ) ;
if ( mapBlockIndex . count ( hash ) )
return error ( " AddToBlockIndex() : % s already exists " , hash.ToString().substr(0,16).c_str()) ;
// Construct new block index object
CBlockIndex * pindexNew = new CBlockIndex ( nFile , nBlockPos , * this ) ;
if ( ! pindexNew )
return error ( " AddToBlockIndex() : new CBlockIndex failed " ) ;
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . insert ( make_pair ( hash , pindexNew ) ) . first ;
pindexNew - > phashBlock = & ( ( * mi ) . first ) ;
map < uint256 , CBlockIndex * > : : iterator miPrev = mapBlockIndex . find ( hashPrevBlock ) ;
if ( miPrev ! = mapBlockIndex . end ( ) )
{
pindexNew - > pprev = ( * miPrev ) . second ;
pindexNew - > nHeight = pindexNew - > pprev - > nHeight + 1 ;
}
pindexNew - > bnChainWork = ( pindexNew - > pprev ? pindexNew - > pprev - > bnChainWork : 0 ) + pindexNew - > GetBlockWork ( ) ;
CTxDB txdb ;
txdb . WriteBlockIndex ( CDiskBlockIndex ( pindexNew ) ) ;
// New best
if ( pindexNew - > bnChainWork > bnBestChainWork )
{
txdb . TxnBegin ( ) ;
if ( pindexGenesisBlock = = NULL & & hash = = hashGenesisBlock )
{
@ -1274,7 +1251,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
@@ -1274,7 +1251,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
{
txdb . TxnAbort ( ) ;
Lockdown ( pindexNew ) ;
return error ( " AddToBlockIndex () : ConnectBlock failed " ) ;
return error ( " SetBestChain () : ConnectBlock failed " ) ;
}
txdb . TxnCommit ( ) ;
pindexNew - > pprev - > pnext = pindexNew ;
@ -1290,7 +1267,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
@@ -1290,7 +1267,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
{
txdb . TxnAbort ( ) ;
Lockdown ( pindexNew ) ;
return error ( " AddToBlockIndex () : Reorganize failed " ) ;
return error ( " SetBestChain () : Reorganize failed " ) ;
}
}
txdb . TxnCommit ( ) ;
@ -1302,9 +1279,41 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
@@ -1302,9 +1279,41 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
bnBestChainWork = pindexNew - > bnChainWork ;
nTimeBestReceived = GetTime ( ) ;
nTransactionsUpdated + + ;
printf ( " AddToBlockIndex: new best=%s height=%d work=%s \n " , hashBestChain . ToString ( ) . substr ( 0 , 22 ) . c_str ( ) , nBestHeight , bnBestChainWork . ToString ( ) . c_str ( ) ) ;
printf ( " SetBestChain: new best=%s height=%d work=%s \n " , hashBestChain . ToString ( ) . substr ( 0 , 22 ) . c_str ( ) , nBestHeight , bnBestChainWork . ToString ( ) . c_str ( ) ) ;
return true ;
}
bool CBlock : : AddToBlockIndex ( unsigned int nFile , unsigned int nBlockPos )
{
// Check for duplicate
uint256 hash = GetHash ( ) ;
if ( mapBlockIndex . count ( hash ) )
return error ( " AddToBlockIndex() : % s already exists " , hash.ToString().substr(0,16).c_str()) ;
// Construct new block index object
CBlockIndex * pindexNew = new CBlockIndex ( nFile , nBlockPos , * this ) ;
if ( ! pindexNew )
return error ( " AddToBlockIndex() : new CBlockIndex failed " ) ;
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . insert ( make_pair ( hash , pindexNew ) ) . first ;
pindexNew - > phashBlock = & ( ( * mi ) . first ) ;
map < uint256 , CBlockIndex * > : : iterator miPrev = mapBlockIndex . find ( hashPrevBlock ) ;
if ( miPrev ! = mapBlockIndex . end ( ) )
{
pindexNew - > pprev = ( * miPrev ) . second ;
pindexNew - > nHeight = pindexNew - > pprev - > nHeight + 1 ;
}
pindexNew - > bnChainWork = ( pindexNew - > pprev ? pindexNew - > pprev - > bnChainWork : 0 ) + pindexNew - > GetBlockWork ( ) ;
CTxDB txdb ;
txdb . WriteBlockIndex ( CDiskBlockIndex ( pindexNew ) ) ;
// New best
if ( pindexNew - > bnChainWork > bnBestChainWork )
if ( ! SetBestChain ( txdb , pindexNew ) )
return false ;
txdb . Close ( ) ;
if ( pindexNew = = pindexBest )