3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +00:00

Simplify the findSplit code, suggested by miron@google.com

This commit is contained in:
Mike Hearn 2011-05-02 13:13:31 +00:00
parent 068dcba122
commit 62302611f6

View File

@ -235,23 +235,13 @@ public class BlockChain {
// //
// findSplit will return block B. chainHead = D and newChainHead = G. // findSplit will return block B. chainHead = D and newChainHead = G.
while (!currentChainCursor.equals(newChainCursor)) { while (!currentChainCursor.equals(newChainCursor)) {
// Move the new chain cursor backwards until it is at the same height as the current chain head. if (currentChainCursor.getHeight() > newChainCursor.getHeight()) {
while (newChainCursor.getHeight() > currentChainCursor.getHeight()) { currentChainCursor = currentChainCursor.getPrev(blockStore);
newChainCursor = blockStore.get(newChainCursor.getHeader().getPrevBlockHash()); assert currentChainCursor != null : "Attempt to follow an orphan chain";
// Stores contain the genesis block which has a height of zero. Thus we should always be able to find } else {
// a block of equal height in this loop. If we fall off the end of the chain it means there is a bug newChainCursor = newChainCursor.getPrev(blockStore);
// in the library.
assert newChainCursor != null : "Attempt to follow an orphan chain"; assert newChainCursor != null : "Attempt to follow an orphan chain";
} }
// We found a place where the chains have equal height. In the first iteration with the above example
// newChainCursor will be F. Did we find the fork yet?
if (newChainCursor.equals(currentChainCursor))
break;
// No, we did not. First iteration D != F so move currentChainCursor backwards one and try again.
currentChainCursor = blockStore.get(currentChainCursor.getHeader().getPrevBlockHash());
assert currentChainCursor != null : "Attempt to follow an orphan chain";
// Eventually currentChainCursor will move from C to B, the inner while loop will move newChainCursor
// from E to B and we will exit.
} }
return currentChainCursor; return currentChainCursor;
} }