Fix comparison between main and alternate chain's cumulative

difficulty.

This fixes the continual reorganization between a main and alternate
chain, using the same two latest blocks from each.

The check that cumulative difficulty of the alternate chain is bigger
than main's was not using main's last block, but incorrectly using the
passed-in block's previous block. main_chain_cumulative_difficulty was
being used in two different ways. This has been split up to keep use
of main_chain_cumulative_difficulty consistent.
This commit is contained in:
warptangent 2015-01-11 18:46:08 -08:00
parent 909ea81067
commit 63051bea1c
No known key found for this signature in database
GPG Key ID: 0E490BEBFBE4E92D
1 changed files with 10 additions and 2 deletions

View File

@ -1188,8 +1188,16 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
// FIXME: // FIXME:
// this brings up an interesting point: consider allowing to get block // this brings up an interesting point: consider allowing to get block
// difficulty both by height OR by hash, not just height. // difficulty both by height OR by hash, not just height.
auto main_chain_cumulative_difficulty = m_db->get_block_cumulative_difficulty(m_db->get_block_height(b.prev_id)); difficulty_type main_chain_cumulative_difficulty = m_db->get_block_cumulative_difficulty(m_db->height() - 1);
bei.cumulative_difficulty = alt_chain.size() ? it_prev->second.cumulative_difficulty : main_chain_cumulative_difficulty; if (alt_chain.size())
{
bei.cumulative_difficulty = it_prev->second.cumulative_difficulty;
}
else
{
// passed-in block's previous block's cumulative difficulty, found on the main chain
bei.cumulative_difficulty = m_db->get_block_cumulative_difficulty(m_db->get_block_height(b.prev_id));
}
bei.cumulative_difficulty += current_diff; bei.cumulative_difficulty += current_diff;
// add block to alternate blocks storage, // add block to alternate blocks storage,