private void HandleNewBestChain(StoredBlock newChainHead)
{
// This chain has overtaken the one we currently believe is best. Reorganize is required.
//
// Firstly, calculate the block at which the chain diverged. We only need to examine the
// chain from beyond this block to find differences.
var splitPoint = FindSplit(newChainHead, _chainHead);
_log.InfoFormat("Re-organize after split at height {0}", splitPoint.Height);
_log.InfoFormat("Old chain head: {0}", _chainHead.Header.HashAsString);
_log.InfoFormat("New chain head: {0}", newChainHead.Header.HashAsString);
_log.InfoFormat("Split at block: {0}", splitPoint.Header.HashAsString);
// Then build a list of all blocks in the old part of the chain and the new part.
var oldBlocks = GetPartialChain(_chainHead, splitPoint);
var newBlocks = GetPartialChain(newChainHead, splitPoint);
// Now inform the wallet. This is necessary so the set of currently active transactions (that we can spend)
// can be updated to take into account the re-organize. We might also have received new coins we didn't have
// before and our previous spends might have been undone.
foreach (var wallet in _wallets)
{
wallet.Reorganize(oldBlocks, newBlocks);
}
// Update the pointer to the best known block.
ChainHead = newChainHead;
}