mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-30 03:21:23 +00:00
Replace for loop with an iterator to make the removal case clearer. Extend the unit test a bit.
This code will all be changing more in future anyway.
This commit is contained in:
@@ -16,10 +16,10 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import static com.google.bitcoin.core.Utils.LOG;
|
import static com.google.bitcoin.core.Utils.LOG;
|
||||||
|
|
||||||
@@ -186,13 +186,14 @@ public class BlockChain {
|
|||||||
*/
|
*/
|
||||||
private void tryConnectingUnconnected() throws VerificationException, ScriptException, BlockStoreException {
|
private void tryConnectingUnconnected() throws VerificationException, ScriptException, BlockStoreException {
|
||||||
// For each block in our unconnected list, try and fit it onto the head of the chain. If we succeed remove it
|
// For each block in our unconnected list, try and fit it onto the head of the chain. If we succeed remove it
|
||||||
// from the list and keep going. If we changed the head of the list at the end of the round,
|
// from the list and keep going. If we changed the head of the list at the end of the round try again until
|
||||||
// try again until we can't fit anything else on the top.
|
// we can't fit anything else on the top.
|
||||||
int blocksConnectedThisRound;
|
int blocksConnectedThisRound;
|
||||||
do {
|
do {
|
||||||
blocksConnectedThisRound = 0;
|
blocksConnectedThisRound = 0;
|
||||||
for (int i = 0; i < unconnectedBlocks.size(); i++) {
|
Iterator<Block> iter = unconnectedBlocks.iterator();
|
||||||
Block block = unconnectedBlocks.get(i);
|
while (iter.hasNext()) {
|
||||||
|
Block block = iter.next();
|
||||||
// Look up the blocks previous.
|
// Look up the blocks previous.
|
||||||
StoredBlock prev = blockStore.get(block.getPrevBlockHash());
|
StoredBlock prev = blockStore.get(block.getPrevBlockHash());
|
||||||
if (prev == null) {
|
if (prev == null) {
|
||||||
@@ -202,8 +203,7 @@ public class BlockChain {
|
|||||||
// Otherwise we can connect it now.
|
// Otherwise we can connect it now.
|
||||||
// False here ensures we don't recurse infinitely downwards when connecting huge chains.
|
// False here ensures we don't recurse infinitely downwards when connecting huge chains.
|
||||||
add(block, false);
|
add(block, false);
|
||||||
unconnectedBlocks.remove(i);
|
iter.remove();
|
||||||
i--; // The next iteration of the for loop will make "i" point to the right index again.
|
|
||||||
blocksConnectedThisRound++;
|
blocksConnectedThisRound++;
|
||||||
}
|
}
|
||||||
if (blocksConnectedThisRound > 0) {
|
if (blocksConnectedThisRound > 0) {
|
||||||
|
@@ -25,7 +25,6 @@ import java.math.BigInteger;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
// Tests still to write:
|
// Tests still to write:
|
||||||
// - Rest of checkDifficultyTransitions: verify we don't accept invalid transitions.
|
|
||||||
// - Fragmented chains can be joined together.
|
// - Fragmented chains can be joined together.
|
||||||
// - Longest testNetChain is selected based on total difficulty not length.
|
// - Longest testNetChain is selected based on total difficulty not length.
|
||||||
// - Many more ...
|
// - Many more ...
|
||||||
@@ -78,8 +77,12 @@ public class BlockChainTest {
|
|||||||
Block b3 = b2.createNextBlock(coinbaseTo);
|
Block b3 = b2.createNextBlock(coinbaseTo);
|
||||||
// Connected.
|
// Connected.
|
||||||
assertTrue(chain.add(b1));
|
assertTrue(chain.add(b1));
|
||||||
// Unconnected.
|
// Unconnected but stored. The head of the chain is still b1.
|
||||||
assertFalse(chain.add(b3));
|
assertFalse(chain.add(b3));
|
||||||
|
assertEquals(chain.getChainHead().getHeader(), b1.cloneAsHeader());
|
||||||
|
// Add in the middle block.
|
||||||
|
assertTrue(chain.add(b2));
|
||||||
|
assertEquals(chain.getChainHead().getHeader(), b3.cloneAsHeader());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user