/** * Called as part of connecting a block when the new block results in a different chain having * higher total work. */ private void handleNewBestChain(StoredBlock newChainHead) throws BlockStoreException, VerificationException { // 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. StoredBlock splitPoint = findSplit(newChainHead, chainHead); log.info("Re-organize after split at height {}", splitPoint.getHeight()); log.info("Old chain head: {}", chainHead.getHeader().getHashAsString()); log.info("New chain head: {}", newChainHead.getHeader().getHashAsString()); log.info("Split at block: {}", splitPoint.getHeader().getHashAsString()); // Then build a list of all blocks in the old part of the chain and the new part. List<StoredBlock> oldBlocks = getPartialChain(chainHead, splitPoint); List<StoredBlock> 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. wallet.reorganize(oldBlocks, newBlocks); // Update the pointer to the best known block. setChainHead(newChainHead); }