pull changes from bitcoin for FindMostWorkChain

This commit is contained in:
Evan Duffield 2015-08-11 12:06:07 -07:00
parent 2d8843c4de
commit 48eecea7e5

View File

@ -2504,15 +2504,29 @@ static CBlockIndex* FindMostWorkChain() {
CBlockIndex *pindexTest = pindexNew; CBlockIndex *pindexTest = pindexNew;
bool fInvalidAncestor = false; bool fInvalidAncestor = false;
while (pindexTest && !chainActive.Contains(pindexTest)) { while (pindexTest && !chainActive.Contains(pindexTest)) {
assert(pindexTest->nStatus & BLOCK_HAVE_DATA);
assert(pindexTest->nChainTx || pindexTest->nHeight == 0); assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
// Candidate has an invalid ancestor, remove entire chain from the set. // Pruned nodes may have entries in setBlockIndexCandidates for
if (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork) // which block files have been deleted. Remove those as candidates
// for the most work chain if we come across them; we can't switch
// to a chain unless we have all the non-active-chain parent blocks.
bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
if (fFailedChain || fMissingData) {
// Candidate chain is not usable (either invalid or missing data)
if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
pindexBestInvalid = pindexNew; pindexBestInvalid = pindexNew;
CBlockIndex *pindexFailed = pindexNew; CBlockIndex *pindexFailed = pindexNew;
// Remove the entire chain from the set.
while (pindexTest != pindexFailed) { while (pindexTest != pindexFailed) {
if (fFailedChain) {
pindexFailed->nStatus |= BLOCK_FAILED_CHILD; pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
} else if (fMissingData) {
// If we're missing data, then add back to mapBlocksUnlinked,
// so that if the block arrives in the future we can try adding
// to setBlockIndexCandidates again.
mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
}
setBlockIndexCandidates.erase(pindexFailed); setBlockIndexCandidates.erase(pindexFailed);
pindexFailed = pindexFailed->pprev; pindexFailed = pindexFailed->pprev;
} }