Skip to content

Commit

Permalink
Simplify the findSplit code, suggested by miron@google.com
Browse files Browse the repository at this point in the history
git-svn-id: http://bitcoinj.googlecode.com/svn/trunk@68 a743e126-8abc-4207-e589-8f9b47be99bf
  • Loading branch information
hearn@google.com committed May 2, 2011
1 parent a72f1cb commit f3c9d6c
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/com/google/bitcoin/core/BlockChain.java
Expand Up @@ -235,23 +235,13 @@ private StoredBlock findSplit(StoredBlock newChainHead, StoredBlock chainHead) t
//
// findSplit will return block B. chainHead = D and newChainHead = G.
while (!currentChainCursor.equals(newChainCursor)) {
// Move the new chain cursor backwards until it is at the same height as the current chain head.
while (newChainCursor.getHeight() > currentChainCursor.getHeight()) {
newChainCursor = blockStore.get(newChainCursor.getHeader().getPrevBlockHash());
// Stores contain the genesis block which has a height of zero. Thus we should always be able to find
// a block of equal height in this loop. If we fall off the end of the chain it means there is a bug
// in the library.
if (currentChainCursor.getHeight() > newChainCursor.getHeight()) {
currentChainCursor = currentChainCursor.getPrev(blockStore);
assert currentChainCursor != null : "Attempt to follow an orphan chain";
} else {
newChainCursor = newChainCursor.getPrev(blockStore);
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;
}
Expand Down

0 comments on commit f3c9d6c

Please sign in to comment.